Nut/OS  5.0.5
API Reference
AT91 Two Wire Interface

Two wire interface. More...

Collaboration diagram for AT91 Two Wire Interface:

Defines

#define TWI_PIO_ASR   PIOA_ASR
#define TWI_PIO_PDR   PIOA_PDR
#define TWI_PIO_MDER   PIOA_MDER

Functions

int NutTwiMasterTranceive (NUTTWIBUS *bus, uint8_t sla, const void *txdata, uint16_t txlen, void *rxdata, uint16_t rxsiz, uint32_t tmo)
 Transmit and/or receive data as a master.
int NutTwiMasterRegRead (NUTTWIBUS *bus, uint8_t sla, uint32_t iadr, uint8_t iadrlen, void *rxdata, uint16_t rxsiz, uint32_t tmo)
 Receive data as a master from a device having internal addressable registers.
int NutTwiMasterRegWrite (NUTTWIBUS *bus, uint8_t sla, uint32_t iadr, uint8_t iadrlen, const void *txdata, uint16_t txsiz, uint32_t tmo)
 Transmit data as a master to a device having internal addressable registers.
int NutTwiMasterError (NUTTWIBUS *bus)
 Get last master mode error.
int NutTwiSlaveListen (NUTTWIBUS *bus, uint8_t *sla, void *rxdata, uint16_t rxsiz, uint32_t tmo)
 Listen for incoming data from a master.
int NutTwiSlaveRespond (NUTTWIBUS *bus, void *txdata, uint16_t txlen, uint32_t tmo)
 Send response to a master.
int NutTwiSlaveError (NUTTWIBUS *bus)
 Get last slave mode error.
uint16_t NutTwiIndexes (NUTTWIBUS *bus, uint8_t idx)
 Get last transfer results.
int NutTwiSetSpeed (NUTTWIBUS *bus, uint32_t speed)
 Set Speed of I2C Interface.
int NutTwiGetSpeed (NUTTWIBUS *bus)
 Request Current Speed of I2C Interface.
int NutTwiIOCtl (NUTTWIBUS *bus, int req, void *conf)
 Perform TWI control functions.
int At91TwiInit (void)
 Initialize TWI interface hardware.
int NutRegisterTwiBus (NUTTWIBUS *bus, uint8_t sla)
 Initialize TWI interface bus.
int NutDestroyTwiBus (NUTTWIBUS *bus)

Variables

NUTTWIBUS At91TwiBus
 TWI/I2C bus structure.

TWI Control Register

#define TWI_CR_OFF   0x00000000
 Control register offset.
#define TWI_CR   (TWI_BASE + TWI_CR_OFF)
 Control register address.
#define TWI_START   0x00000001
 Send start condition.
#define TWI_STOP   0x00000002
 Send stop condition.
#define TWI_MSEN   0x00000004
 Enable master mode.
#define TWI_MSDIS   0x00000008
 Disable master mode.
#define TWI_SVEN   0x00000010
 Enable slave mode.
#define TWI_SVDIS   0x00000020
 Disable slave mode.
#define TWI_SWRST   0x00000080
 Software reset.

TWI Master Mode Register

#define TWI_MMR_OFF   0x00000004
 Master mode register offset.
#define TWI_MMR   (TWI_BASE + TWI_MMR_OFF)
 Master mode register address.
#define TWI_IADRSZ   0x00000300
 Internal device address size mask.
#define TWI_IADRSZ_NONE   0x00000000
 No internal device address.
#define TWI_IADRSZ_1BYTE   0x00000100
 One byte internal device address.
#define TWI_IADRSZ_2BYTE   0x00000200
 Two byte internal device address.
#define TWI_IADRSZ_3BYTE   0x00000300
 Three byte internal device address.
#define TWI_MREAD   0x00001000
 Master read direction.
#define TWI_DADR   0x007F0000
 Device address mask.
#define TWI_DADR_LSB   16
 Device address LSB.

TWI Slave Mode Register

#define TWI_SMR_OFF   0x00000008
 Slave mode register offset.
#define TWI_SMR   (TWI_BASE + TWI_SMR_OFF)
 Slave mode register address.
#define TWI_SADR   0x007F0000
 Slave address mask.
#define TWI_SADR_LSB   16
 Slave address LSB.

TWI Internal Address Register

#define TWI_IADRR_OFF   0x0000000C
 Internal address register offset.
#define TWI_IADRR   (TWI_BASE + TWI_IADRR_OFF)
 Internal address register address.
#define TWI_IADR   0x00FFFFFF
 Internal address mask.
#define TWI_IADR_LSB   0
 Internal address LSB.

TWI Clock Waveform Generator Register

#define TWI_CWGR_OFF   0x00000010
 Clock waveform generator register offset.
#define TWI_CWGR   (TWI_BASE + TWI_CWGR_OFF)
 Clock waveform generator register address.
#define TWI_CLDIV   0x000000FF
 Clock low divider mask.
#define TWI_CLDIV_LSB   0
 Clock low divider LSB.
#define TWI_CHDIV   0x0000FF00
 Clock high divider mask.
#define TWI_CHDIV_LSB   8
 Clock high divider LSB.
#define TWI_CKDIV   0x00070000
 Clock divider mask.
#define TWI_CKDIV_LSB   16
 Clock divider LSB.

TWI Status and Interrupt Registers

#define TWI_SR_OFF   0x00000020
 Status register offset.
#define TWI_SR   (TWI_BASE + TWI_SR_OFF)
 Status register address.
#define TWI_IER_OFF   0x00000024
 Interrupt enable register offset.
#define TWI_IER   (TWI_BASE + TWI_IER_OFF)
 Interrupt enable register address.
#define TWI_IDR_OFF   0x00000028
 Interrupt disable register offset.
#define TWI_IDR   (TWI_BASE + TWI_IDR_OFF)
 Interrupt disable register address.
#define TWI_IMR_OFF   0x0000002C
 Interrupt mask register offset.
#define TWI_IMR   (TWI_BASE + TWI_IMR_OFF)
 Interrupt mask register address.
#define TWI_TXCOMP   0x00000001
 Transmission completed.
#define TWI_RXRDY   0x00000002
 Receive holding register ready.
#define TWI_TXRDY   0x00000004
 Transmit holding register ready.
#define TWI_SVREAD   0x00000008
 Slave read.
#define TWI_SVACC   0x00000010
 Slave access.
#define TWI_GACC   0x00000020
 General call access.
#define TWI_OVRE   0x00000040
 Overrun error.
#define TWI_NACK   0x00000100
 Not acknowledged.
#define TWI_ARBLST   0x00000200
 Arbitration lost.
#define TWI_SCLWS   0x00000400
 Clock wait state.
#define TWI_EOSACC   0x00000800
 End of slave access.

TWI Receive Holding Register

#define TWI_RHR_OFF   0x00000030
 Receive holding register offset.
#define TWI_RHR   (TWI_BASE + TWI_RHR_OFF)
 Receive holding register address.

TWI Transmit Holding Register

#define TWI_THR_OFF   0x00000034
 Transmit holding register offset.
#define TWI_THR   (TWI_BASE + TWI_THR_OFF)
 Transmit holding register address.

Detailed Description

Two wire interface.


Define Documentation

#define TWI_CR_OFF   0x00000000

Control register offset.

#define TWI_CR   (TWI_BASE + TWI_CR_OFF)

Control register address.

Referenced by At91TwiInit(), NutTwiMasterRegRead(), and NutTwiMasterTranceive().

#define TWI_START   0x00000001

Send start condition.

Referenced by NutTwiMasterRegRead(), and NutTwiMasterTranceive().

#define TWI_STOP   0x00000002

Send stop condition.

Referenced by NutTwiMasterRegRead(), and NutTwiMasterTranceive().

#define TWI_MSEN   0x00000004

Enable master mode.

Referenced by At91TwiInit().

#define TWI_MSDIS   0x00000008

Disable master mode.

#define TWI_SVEN   0x00000010

Enable slave mode.

#define TWI_SVDIS   0x00000020

Disable slave mode.

Referenced by At91TwiInit().

#define TWI_SWRST   0x00000080

Software reset.

Referenced by At91TwiInit().

#define TWI_MMR_OFF   0x00000004

Master mode register offset.

#define TWI_MMR   (TWI_BASE + TWI_MMR_OFF)

Master mode register address.

Referenced by NutTwiMasterRegRead(), NutTwiMasterRegWrite(), and NutTwiMasterTranceive().

#define TWI_IADRSZ   0x00000300

Internal device address size mask.

#define TWI_IADRSZ_NONE   0x00000000

No internal device address.

#define TWI_IADRSZ_1BYTE   0x00000100

One byte internal device address.

#define TWI_IADRSZ_2BYTE   0x00000200

Two byte internal device address.

#define TWI_IADRSZ_3BYTE   0x00000300

Three byte internal device address.

#define TWI_MREAD   0x00001000

Master read direction.

Referenced by NutTwiMasterRegRead(), and NutTwiMasterTranceive().

#define TWI_DADR   0x007F0000

Device address mask.

#define TWI_DADR_LSB   16

Device address LSB.

Referenced by NutTwiMasterRegRead(), NutTwiMasterRegWrite(), and NutTwiMasterTranceive().

#define TWI_SMR_OFF   0x00000008

Slave mode register offset.

#define TWI_SMR   (TWI_BASE + TWI_SMR_OFF)

Slave mode register address.

#define TWI_SADR   0x007F0000

Slave address mask.

#define TWI_SADR_LSB   16

Slave address LSB.

#define TWI_IADRR_OFF   0x0000000C

Internal address register offset.

#define TWI_IADRR   (TWI_BASE + TWI_IADRR_OFF)

Internal address register address.

Referenced by NutTwiMasterRegRead(), and NutTwiMasterRegWrite().

#define TWI_IADR   0x00FFFFFF

Internal address mask.

#define TWI_IADR_LSB   0

Internal address LSB.

#define TWI_CWGR_OFF   0x00000010

Clock waveform generator register offset.

#define TWI_CWGR   (TWI_BASE + TWI_CWGR_OFF)

Clock waveform generator register address.

Referenced by NutTwiGetSpeed(), and NutTwiSetSpeed().

#define TWI_CLDIV   0x000000FF

Clock low divider mask.

#define TWI_CLDIV_LSB   0

Clock low divider LSB.

#define TWI_CHDIV   0x0000FF00

Clock high divider mask.

#define TWI_CHDIV_LSB   8

Clock high divider LSB.

#define TWI_CKDIV   0x00070000

Clock divider mask.

#define TWI_CKDIV_LSB   16

Clock divider LSB.

#define TWI_SR_OFF   0x00000020

Status register offset.

#define TWI_SR   (TWI_BASE + TWI_SR_OFF)

Status register address.

#define TWI_IER_OFF   0x00000024

Interrupt enable register offset.

#define TWI_IER   (TWI_BASE + TWI_IER_OFF)

Interrupt enable register address.

Referenced by NutTwiMasterRegRead(), NutTwiMasterRegWrite(), and NutTwiMasterTranceive().

#define TWI_IDR_OFF   0x00000028

Interrupt disable register offset.

#define TWI_IDR   (TWI_BASE + TWI_IDR_OFF)

Interrupt disable register address.

Referenced by At91TwiInit(), NutTwiMasterRegRead(), NutTwiMasterRegWrite(), and NutTwiMasterTranceive().

#define TWI_IMR_OFF   0x0000002C

Interrupt mask register offset.

#define TWI_IMR   (TWI_BASE + TWI_IMR_OFF)

Interrupt mask register address.

#define TWI_TXCOMP   0x00000001

Transmission completed.

#define TWI_RXRDY   0x00000002

Receive holding register ready.

Referenced by NutTwiMasterRegRead(), and NutTwiMasterTranceive().

#define TWI_TXRDY   0x00000004

Transmit holding register ready.

Referenced by NutTwiMasterRegWrite(), and NutTwiMasterTranceive().

#define TWI_SVREAD   0x00000008

Slave read.

#define TWI_SVACC   0x00000010

Slave access.

#define TWI_GACC   0x00000020

General call access.

#define TWI_OVRE   0x00000040

Overrun error.

#define TWI_NACK   0x00000100

Not acknowledged.

#define TWI_ARBLST   0x00000200

Arbitration lost.

#define TWI_SCLWS   0x00000400

Clock wait state.

#define TWI_EOSACC   0x00000800

End of slave access.

#define TWI_RHR_OFF   0x00000030

Receive holding register offset.

#define TWI_RHR   (TWI_BASE + TWI_RHR_OFF)

Receive holding register address.

#define TWI_THR_OFF   0x00000034

Transmit holding register offset.

#define TWI_THR   (TWI_BASE + TWI_THR_OFF)

Transmit holding register address.

Referenced by NutTwiMasterRegWrite(), and NutTwiMasterTranceive().

#define TWI_PIO_ASR   PIOA_ASR

Referenced by At91TwiInit().

#define TWI_PIO_PDR   PIOA_PDR

Referenced by At91TwiInit().

#define TWI_PIO_MDER   PIOA_MDER

Referenced by At91TwiInit().


Function Documentation

int NutTwiMasterTranceive ( NUTTWIBUS bus,
uint8_t  sla,
const void *  txdata,
uint16_t  txlen,
void *  rxdata,
uint16_t  rxsiz,
uint32_t  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.

Parameters:
slaSlave 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.
txdataPoints to the data to transmit. Ignored, if the number of data bytes to transmit is zero.
txlenNumber 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.
rxdataPoints to a buffer, where the received data will be stored. Ignored, if the maximum number of bytes to receive is zero.
rxsizMaximum number of bytes to receive. Set to zero, if no bytes are expected from the slave device.
tmoTimeout 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.

References _NUTTWIBUS::bus_icb, _NUTTWIBUS::bus_mutex, NutEventPost(), NutEventWait(), outr, rc, _NUTTWIICB::tw_mm_buf, _NUTTWIICB::tw_mm_err, _NUTTWIICB::tw_mm_error, _NUTTWIICB::tw_mm_idx, _NUTTWIICB::tw_mm_len, _NUTTWIICB::tw_mm_mtx, TWERR_IF_LOCKED, TWERR_TIMEOUT, TWI_CR, TWI_DADR_LSB, TWI_IDR, TWI_IER, TWI_MMR, TWI_MREAD, TWI_RXRDY, TWI_START, TWI_STOP, TWI_THR, and TWI_TXRDY.

Here is the call graph for this function:

int NutTwiMasterRegRead ( NUTTWIBUS bus,
uint8_t  sla,
uint32_t  iadr,
uint8_t  iadrlen,
void *  rxdata,
uint16_t  rxsiz,
uint32_t  tmo 
)

Receive data as a master from a device having internal addressable registers.

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

Parameters:
slaSlave 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.
iadrAddress send to the device to access certain registers or memory addresses of it.
iadrlenNumber of bytes to send as address, maximum 3 bytes are supported from AT91SAM7
rxdataPoints to a buffer, where the received data will be stored.
rxsizMaximum number of bytes to receive.
tmoTimeout 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.

References _NUTTWIBUS::bus_icb, _NUTTWIBUS::bus_mutex, NutEventPost(), NutEventWait(), outr, rc, _NUTTWIICB::tw_mm_buf, _NUTTWIICB::tw_mm_err, _NUTTWIICB::tw_mm_error, _NUTTWIICB::tw_mm_idx, _NUTTWIICB::tw_mm_len, _NUTTWIICB::tw_mm_mtx, TWERR_IF_LOCKED, TWERR_TIMEOUT, TWI_CR, TWI_DADR_LSB, TWI_IADRR, TWI_IDR, TWI_IER, TWI_MMR, TWI_MREAD, TWI_RXRDY, TWI_START, and TWI_STOP.

Here is the call graph for this function:

int NutTwiMasterRegWrite ( NUTTWIBUS bus,
uint8_t  sla,
uint32_t  iadr,
uint8_t  iadrlen,
const void *  txdata,
uint16_t  txsiz,
uint32_t  tmo 
)

Transmit data as a master to a device having internal addressable registers.

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

Parameters:
slaSlave 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.
iadrAddress send to the device to access certain registers or memory addresses of it.
iadrlenNumber of bytes to send as address, maximum 3 bytes are supported from AT91SAM7
txdataPoints to a buffer, where the data to transmit will be stored.
txsizMaximum number of bytes to transmit.
tmoTimeout 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. Number could be less if slave end transmission with NAK.

References _NUTTWIBUS::bus_icb, _NUTTWIBUS::bus_mutex, NutEventPost(), NutEventWait(), outb, outr, rc, _NUTTWIICB::tw_mm_buf, _NUTTWIICB::tw_mm_err, _NUTTWIICB::tw_mm_error, _NUTTWIICB::tw_mm_idx, _NUTTWIICB::tw_mm_len, _NUTTWIICB::tw_mm_mtx, TWERR_IF_LOCKED, TWERR_TIMEOUT, TWI_DADR_LSB, TWI_IADRR, TWI_IDR, TWI_IER, TWI_MMR, TWI_THR, and TWI_TXRDY.

Here is the call graph for this function:

int NutTwiMasterError ( NUTTWIBUS bus)

Get last master mode error.

You may call this function to determine the specific cause of an error after twi transaction failed.

References _NUTTWIBUS::bus_icb, rc, and _NUTTWIICB::tw_mm_error.

int NutTwiSlaveListen ( NUTTWIBUS bus,
uint8_t sla,
void *  rxdata,
uint16_t  rxsiz,
uint32_t  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:
Slave mode is not implemented in the AT91 driver. Thus the function always returns -1.
Parameters:
slaPoints 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.
rxdataPoints to a data buffer where the received data bytes are stored.
rxsizSpecifies the maximum number of data bytes to receive.
tmoTimeout 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.
int NutTwiSlaveRespond ( NUTTWIBUS bus,
void *  txdata,
uint16_t  txlen,
uint32_t  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:
Slave mode is not implemented in the AT91 driver. Thus the function always returns -1.
Parameters:
txdataPoints to the data to transmit. Ignored, if the number of bytes to transmit is zero.
txlenNumber of data bytes to transmit.
tmoTimeout 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.
int NutTwiSlaveError ( NUTTWIBUS bus)

Get last slave mode error.

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

Returns:
Error code or 0 if no error occurred.
Note:
Slave mode is not implemented in the AT91 driver. Thus the function always returns TWERR_BUS.

References TWERR_BUS.

uint16_t NutTwiIndexes ( NUTTWIBUS bus,
uint8_t  idx 
)

Get last transfer results.

Todo:
Do we really need this. It may not work with competing threads.

You may call this function to determine how many bytes where transferred before the twi transaction failed.

References _NUTTWIBUS::bus_icb, _NUTTWIICB::tw_mm_error, and _NUTTWIICB::tw_mm_len.

int NutTwiSetSpeed ( NUTTWIBUS bus,
uint32_t  speed 
)

Set Speed of I2C Interface.

Setup Interface Speed

References NULL, NUT_HWCLK_PERIPHERAL, NutClockGet, outr, rc, and TWI_CWGR.

Referenced by NutRegisterTwiBus(), and NutTwiIOCtl().

int NutTwiGetSpeed ( NUTTWIBUS bus)

Request Current Speed of I2C Interface.

Returns:
0..400000 for speed, -1 in case of error.

References inr, NutGetCpuClock(), rc, and TWI_CWGR.

Referenced by NutTwiIOCtl().

Here is the call graph for this function:

int NutTwiIOCtl ( NUTTWIBUS bus,
int  req,
void *  conf 
)

Perform TWI control functions.

Parameters:
reqRequested control function. May be set to one of the following constants:
  • TWI_SETSPEED, if conf points to an uint32_t value containing the bitrate.
  • TWI_GETSPEED, if conf points to an uint32_t value receiving the current bitrate.
confPoints 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.

References _NUTTWIBUS::bus_icb, NutTwiGetSpeed(), NutTwiSetSpeed(), rc, _NUTTWIICB::tw_mm_sla, TWI_GETSLAVEADDRESS, TWI_GETSPEED, TWI_GETSTATUS, TWI_SETSLAVEADDRESS, TWI_SETSPEED, and TWI_SETSTATUS.

Here is the call graph for this function:

int At91TwiInit ( void  )

Initialize TWI interface hardware.

References _BV, outr, PMC_PCER, rc, TWI_CR, TWI_ID, TWI_IDR, TWI_MSEN, TWI_PIO_ASR, TWI_PIO_MDER, TWI_PIO_PDR, TWI_SVDIS, and TWI_SWRST.

int NutRegisterTwiBus ( NUTTWIBUS bus,
uint8_t  sla 
)

Initialize TWI interface bus.

The specified slave address is not used here as we don't support twi-slave on AT91SAM7X

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

References _NUTTWIBUS::bus_icb, _NUTTWIBUS::bus_initbus, _NUTTWIBUS::bus_mutex, _NUTTWIBUS::bus_sig_ev, memset(), NULL, NUT_IRQMODE_LEVEL, NutEventPost(), NutHeapAlloc, NutIrqEnable(), NutIrqSetMode(), NutRegisterIrqHandler(), NutTwiSetSpeed(), and rc.

Here is the call graph for this function:

int NutDestroyTwiBus ( NUTTWIBUS bus)

References _NUTTWIBUS::bus_icb, _NUTTWIBUS::bus_sig_ev, NutHeapFree, and NutIrqDisable().

Here is the call graph for this function:


Variable Documentation

Initial value:
 {
    TWI_CR,      
   &sig_TWI,     
    NULL,        
    NULL,        
    NULL,        
    0,           
    0,           
    At91TwiInit, 
    NULL,        
}

TWI/I2C bus structure.