Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
TWI - Two-Wire Interface

Driver for the TWI (Two-Wire Interface).

This driver provides access to the main features of the TWI controller. The TWI interconnects components on a unique two-wire bus. The TWI is programmable as a master or a slave with sequential or single-byte access. Multiple master capability is supported.

Data Structures

struct  twi_options_t
 Input parameters when initializing the twi module mode. More...
 
struct  twi_package_t
 Information concerning the data transmission. More...
 
struct  twi_slave_fct_t
 Pointer on TWI slave application routines. More...
 

Functions

void twi_disable_interrupt (volatile avr32_twi_t *twi)
 Disable all TWI interrupts. More...
 
bool twi_is_busy (void)
 Test if a TWI read/write is pending. More...
 
static void twi_master_disable (volatile avr32_twi_t *twi)
 Disable Master Mode of the TWI. More...
 
static void twi_master_enable (volatile avr32_twi_t *twi)
 Enable Master Mode of the TWI. More...
 
int twi_master_init (volatile avr32_twi_t *twi, const twi_options_t *opt)
 Initialize the twi master module. More...
 
int twi_master_read (volatile avr32_twi_t *twi, const twi_package_t *package)
 Read multiple bytes from a TWI compatible slave device. More...
 
int twi_master_write (volatile avr32_twi_t *twi, const twi_package_t *package)
 Write multiple bytes to a TWI compatible slave device. More...
 
int twi_master_write_ex (volatile avr32_twi_t *twi, const twi_package_t *package)
 Write multiple bytes to a TWI compatible slave device. This function is not blocking. More...
 
int twi_probe (volatile avr32_twi_t *twi, char chip_addr)
 Test if a chip answers for a given twi address. More...
 
int twi_slave_init (volatile avr32_twi_t *twi, const twi_options_t *opt, const twi_slave_fct_t *slave_fct)
 Initialize the twi slave module. More...
 

Error Codes for the Module

#define TWI_SUCCESS   0
 
#define TWI_INVALID_ARGUMENT   -1
 
#define TWI_ARBITRATION_LOST   -2
 
#define TWI_NO_CHIP_FOUND   -3
 
#define TWI_RECEIVE_OVERRUN   -4
 
#define TWI_RECEIVE_NACK   -5
 
#define TWI_SEND_OVERRUN   -6
 
#define TWI_SEND_NACK   -7
 
#define TWI_BUSY   -8
 

#define TWI_ARBITRATION_LOST   -2
#define TWI_BUSY   -8

Referenced by twi_master_write_ex().

#define TWI_INVALID_ARGUMENT   -1
#define TWI_NO_CHIP_FOUND   -3
#define TWI_RECEIVE_NACK   -5
#define TWI_RECEIVE_OVERRUN   -4
#define TWI_SEND_NACK   -7
#define TWI_SEND_OVERRUN   -6

void twi_disable_interrupt ( volatile avr32_twi_t *  twi)

Disable all TWI interrupts.

Parameters
twiBase address of the TWI (i.e. &AVR32_TWI).

References cpu_irq_restore(), and cpu_irq_save().

bool twi_is_busy ( void  )

Test if a TWI read/write is pending.

Returns
true if a write/read access is pending, false otherwise

References twi_busy.

Referenced by twi_master_read(), and twi_master_write().

static void twi_master_disable ( volatile avr32_twi_t *  twi)
inlinestatic

Disable Master Mode of the TWI.

Parameters
twiBase address of the TWI instance.
static void twi_master_enable ( volatile avr32_twi_t *  twi)
inlinestatic

Enable Master Mode of the TWI.

Parameters
twiBase address of the TWI instance.
int twi_master_init ( volatile avr32_twi_t *  twi,
const twi_options_t opt 
)

Initialize the twi master module.

Parameters
twiBase address of the TWI (i.e. &AVR32_TWI).
*optOptions for initializing the twi module (see twi_options_t)

References CONF_TWI_IRQ_LEVEL, CONF_TWI_IRQ_LINE, cpu_irq_disable, cpu_irq_restore(), cpu_irq_save(), irq_register_handler, twi_options_t::pba_hz, twi_options_t::speed, sysreg_read, twi_inst, twi_set_speed(), and TWI_SUCCESS.

Referenced by main().

int twi_master_read ( volatile avr32_twi_t *  twi,
const twi_package_t package 
)

Read multiple bytes from a TWI compatible slave device.

Parameters
twiBase address of the TWI (i.e. &AVR32_TWI).
packagePackage information and data (see twi_package_t)
Returns
TWI_SUCCESS if all bytes were read, error code otherwise

References twi_package_t::addr, twi_package_t::addr_length, twi_package_t::buffer, twi_package_t::chip, cpu_relax, twi_package_t::length, twi_busy, twi_inst, TWI_INVALID_ARGUMENT, twi_is_busy(), twi_it_mask, twi_mk_addr(), twi_nack, TWI_RECEIVE_NACK, twi_rx_data, twi_rx_nb_bytes, and TWI_SUCCESS.

Referenced by main().

int twi_master_write ( volatile avr32_twi_t *  twi,
const twi_package_t package 
)

Write multiple bytes to a TWI compatible slave device.

Parameters
twiBase address of the TWI (i.e. &AVR32_TWI).
*packagePackage information and data (see twi_package_t)
Returns
TWI_SUCCESS if all bytes were written, error code otherwise

References twi_package_t::addr, twi_package_t::addr_length, twi_package_t::buffer, twi_package_t::chip, cpu_relax, twi_package_t::length, twi_busy, twi_inst, TWI_INVALID_ARGUMENT, twi_is_busy(), twi_it_mask, twi_mk_addr(), twi_nack, TWI_RECEIVE_NACK, TWI_SUCCESS, twi_tx_data, and twi_tx_nb_bytes.

Referenced by main(), and twi_probe().

int twi_master_write_ex ( volatile avr32_twi_t *  twi,
const twi_package_t package 
)

Write multiple bytes to a TWI compatible slave device. This function is not blocking.

The function does not wait that all the bytes are written.

Parameters
twiBase address of the TWI (i.e. &AVR32_TWI).
*packagePackage information and data (see twi_package_t)
Returns
TWI_SUCCESS if all bytes were written, error code otherwise

Write multiple bytes to a TWI compatible slave device. This function is not blocking.

References twi_package_t::addr, twi_package_t::addr_length, twi_package_t::buffer, twi_package_t::chip, twi_package_t::length, TWI_BUSY, twi_inst, TWI_INVALID_ARGUMENT, twi_it_mask, twi_mk_addr(), twi_nack, TWI_RECEIVE_NACK, TWI_SUCCESS, twi_tx_data, and twi_tx_nb_bytes.

int twi_probe ( volatile avr32_twi_t *  twi,
char  chip_addr 
)

Test if a chip answers for a given twi address.

Parameters
twiBase address of the TWI (i.e. &AVR32_TWI).
chip_addrAddress of the chip which is searched for
Returns
TWI_SUCCESS if a chip was found, error code otherwise

References twi_package_t::addr, twi_package_t::addr_length, twi_package_t::buffer, twi_package_t::chip, twi_package_t::length, and twi_master_write().

int twi_slave_init ( volatile avr32_twi_t *  twi,
const twi_options_t opt,
const twi_slave_fct_t slave_fct 
)

Initialize the twi slave module.

Parameters
twiBase address of the TWI (i.e. &AVR32_TWI).
*optOptions for initializing the twi module (see twi_options_t)
*slave_fctPointer on application functions

References twi_options_t::chip, CONF_TWI_IRQ_LEVEL, CONF_TWI_IRQ_LINE, cpu_irq_disable, cpu_irq_restore(), cpu_irq_save(), irq_register_handler, sysreg_read, twi_inst, twi_it_mask, and TWI_SUCCESS.