twif.c File Reference

Go to the source code of this file.

Defines

#define TWGO   (_BV(TWINT) | _BV(TWEN) | _BV(TWIE))

Functions

int TwMasterTransact (u_char sla, CONST void *txdata, u_short txlen, void *rxdata, u_short rxsiz, u_long tmo)
 Transmit and/or receive data as a master.
int TwMasterError (void)
 Get last master mode error.
int TwSlaveListen (u_char *sla, void *rxdata, u_short rxsiz, u_long tmo)
 Listen for incoming data from a master.
int TwSlaveRespond (void *txdata, u_short txlen, u_long tmo)
 Send response to a master.
int TwSlaveError (void)
 Get last slave mode error.
int TwIOCtl (int req, void *conf)
 Perform TWI control functions.
int TwInit (u_char sla)
 Initialize TWI interface.

Variables

HANDLE tw_mm_mutex
HANDLE tw_mm_que
HANDLE tw_sr_que
HANDLE tw_st_que


Define Documentation

#define TWGO   (_BV(TWINT) | _BV(TWEN) | _BV(TWIE))

Definition at line 140 of file twif.c.

Referenced by TwSlaveRespond().


Function Documentation

int TwMasterTransact ( u_char  sla,
CONST void *  txdata,
u_short  txlen,
void *  rxdata,
u_short  rxsiz,
u_long  tmo 
)

Transmit and/or receive data as a master.

The two-wire serial interface must have been initialized by calling TwInit() before this function can be used.

Note:
This function is only available on ATmega128 systems.
Parameters:
sla Slave address of the destination. This slave address must be specified as a 7-bit address. For example, the PCF8574A may be configured to slave addresses from 0x38 to 0x3F.
txdata Points to the data to transmit. Ignored, if the number of data bytes to transmit is zero.
txlen Number of data bytes to transmit. If zero, then the interface will not send any data to the slave device and will directly enter the master receive mode.
rxdata Points to a buffer, where the received data will be stored. Ignored, if the maximum number of bytes to receive is zero.
rxsiz Maximum number of bytes to receive. Set to zero, if no bytes are expected from the slave device.
tmo Timeout in milliseconds. To disable timeout, set this parameter to NUT_WAIT_INFINITE.
Returns:
The number of bytes received, -1 in case of an error or timeout.

Definition at line 532 of file twif.c.

References _BV, inb, NutEnterCritical, NutEventPost(), NutEventWait(), NutExitCritical, NutSleep(), outb, SIGNALED, tw_mm_mutex, tw_mm_que, TWERR_IF_LOCKED, and TWERR_TIMEOUT.

int TwMasterError ( void   ) 

Get last master mode error.

You may call this function to determine the specific cause of an error after TwMasterTransact() failed.

Note:
This function is only available on ATmega128 systems.

Definition at line 617 of file twif.c.

int TwSlaveListen ( u_char sla,
void *  rxdata,
u_short  rxsiz,
u_long  tmo 
)

Listen for incoming data from a master.

If this function returns without error, the bus is blocked. The caller must immediately process the request and return a response by calling TwSlaveRespond().

Note:
This function is only available on ATmega128 systems. The function is not reentrant.
Parameters:
sla Points to a byte variable, which receives the slave address sent by the master. This can be used by the caller to determine whether the the interface has been addressed by a general call or its individual address.
rxdata Points to a data buffer where the received data bytes are stored.
rxsiz Specifies the maximum number of data bytes to receive.
tmo Timeout in milliseconds. To disable timeout, set this parameter to NUT_WAIT_INFINITE.
Returns:
The number of bytes received, -1 in case of an error or timeout.

Definition at line 650 of file twif.c.

References _BV, inb, NutEnterCritical, NutEventWait(), NutExitCritical, outb, SIGNALED, tw_sr_que, and TWERR_TIMEOUT.

int TwSlaveRespond ( void *  txdata,
u_short  txlen,
u_long  tmo 
)

Send response to a master.

This function must be called as soon as possible after TwSlaveListen() returned successfully, even if no data needs to be returned. Not doing so will completely block the bus.

Note:
This function is only available on ATmega128 systems.
Parameters:
txdata Points to the data to transmit. Ignored, if the number of bytes to transmit is zero.
txlen Number of data bytes to transmit.
tmo Timeout in milliseconds. To disable timeout, set this parameter to NUT_WAIT_INFINITE.
Returns:
The number of bytes transmitted, -1 in case of an error or timeout.

Definition at line 724 of file twif.c.

References _BV, inb, NutEnterCritical, NutEventWait(), NutExitCritical, outb, SIGNALED, tw_st_que, TWERR_TIMEOUT, and TWGO.

int TwSlaveError ( void   ) 

Get last slave mode error.

You may call this function to determine the specific cause of an error after TwSlaveListen() or TwSlaveRespond() failed.

Note:
This function is only available on ATmega128 systems.

Definition at line 799 of file twif.c.

int TwIOCtl ( int  req,
void *  conf 
)

Perform TWI control functions.

This function is only available on ATmega128 systems.

Parameters:
req Requested control function. May be set to one of the following constants:
  • TWI_SETSPEED, if conf points to an u_long value containing the bitrate.
  • TWI_GETSPEED, if conf points to an u_long value receiving the current bitrate.
conf Points to a buffer that contains any data required for the given control function or receives data from that function.
Returns:
0 on success, -1 otherwise.
Note:
Timeout is limited to the granularity of the system timer.

Definition at line 827 of file twif.c.

References bit_is_set, cbi, inb, NutGetCpuClock(), outb, sbi, TWI_GETSLAVEADDRESS, TWI_GETSPEED, TWI_GETSTATUS, TWI_SETSLAVEADDRESS, TWI_SETSPEED, and TWI_SETSTATUS.

int TwInit ( u_char  sla  ) 

Initialize TWI interface.

The specified slave address is used only, if the local system is running as a slave. Anyway, care must be taken that it doesn't conflict with another connected device.

Note:
This function is only available on ATmega128 systems.
Parameters:
sla Slave address, must be specified as a 7-bit address, always lower than 128.

Definition at line 899 of file twif.c.

References _BV, NutEventPost(), NutRegisterIrqHandler(), outb, sig_2WIRE_SERIAL, tw_mm_mutex, TWI_SETSPEED, and TwIOCtl().


Variable Documentation

HANDLE tw_mm_mutex

Definition at line 106 of file twif.c.

Referenced by TwInit(), and TwMasterTransact().

HANDLE tw_mm_que

Definition at line 107 of file twif.c.

Referenced by TwMasterTransact().

HANDLE tw_sr_que

Definition at line 108 of file twif.c.

Referenced by TwSlaveListen().

HANDLE tw_st_que

Definition at line 109 of file twif.c.

Referenced by TwSlaveRespond().


© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/