Microchip® Advanced Software Framework

twim.c File Reference
#include <avr32/io.h>
#include "compiler.h"
#include "intc.h"
#include "twim.h"
#include <includes.h>

Functions

CPU_INT32U BSP_INTC_IntReg (CPU_FNCT_VOID handler, CPU_INT32U irq, CPU_INT32U int_level)
 
int twi_master_init (volatile avr32_twim_t *twi, const twi_options_t *opt)
 Initialize the twi master module. More...
 
static void twi_master_interrupt_handler (void)
 TWI interrupt handler. More...
 
int twi_master_read (volatile avr32_twim_t *twi, const twi_package_t *package)
 Read multiple bytes from a TWI compatible slave device. More...
 
int twi_probe (volatile avr32_twim_t *twi, char chip_addr)
 Test if a chip answers for a given twi address. More...
 
int twi_set_speed (volatile avr32_twim_t *twi, unsigned int speed, unsigned long pba_hz)
 Set the twi bus speed in conjunction with the clock frequency. More...
 
int twim_chained_transfer (volatile avr32_twim_t *twi, volatile twim_transfer_t *first, volatile twim_transfer_t *second, bool tenbit)
 
void twim_disable_interrupt (volatile avr32_twim_t *twi)
 
int twim_read (volatile avr32_twim_t *twi, unsigned char *buffer, int nbytes, int saddr, bool tenbit)
 
int twim_read_packet (volatile avr32_twim_t *twi, const twi_package_t *package)
 Read multiple bytes from a TWI compatible slave device. More...
 
int twim_write (volatile avr32_twim_t *twi, unsigned const char *buffer, int nbytes, int saddr, bool tenbit)
 
int twim_write_packet (volatile avr32_twim_t *twi, const twi_package_t *package)
 Write multiple bytes to a TWI compatible slave device. More...
 

Variables

static volatile avr32_twim_t * twim_inst
 Pointer to the instance 1 of the TWIM registers for IT. More...
 
static volatile unsigned long twim_it_mask
 IT mask. More...
 
static volatile bool twim_nack = false
 Add NACK boolean. More...
 
static volatile unsigned char
*volatile 
twim_rx_data = NULL
 Pointer to the applicative TWI receive buffer. More...
 
static volatile int twim_rx_nb_bytes = 0
 Remaining number of bytes to receive. More...
 
static const unsigned char
*volatile 
twim_tx_data = NULL
 Pointer to the applicative TWI transmit buffer. More...
 
static volatile int twim_tx_nb_bytes = 0
 Remaining number of bytes to transmit. More...
 

CPU_INT32U BSP_INTC_IntReg ( CPU_FNCT_VOID  handler,
CPU_INT32U  irq,
CPU_INT32U  int_level 
)
int twi_master_init ( volatile avr32_twim_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 twim_options_t)

References BSP_INTC_IntReg(), Disable_global_interrupt, Enable_global_interrupt, Is_global_interrupt_enabled, twi_options_t::pba_hz, twi_options_t::smbus, twi_options_t::speed, TWI_INVALID_CLOCK_DIV, twi_master_interrupt_handler(), twi_set_speed(), TWI_SUCCESS, and UL.

Referenced by twi_init().

static void twi_master_interrupt_handler ( void  )
static

TWI interrupt handler.

References twim_inst, twim_it_mask, twim_nack, twim_rx_data, twim_rx_nb_bytes, twim_tx_data, and twim_tx_nb_bytes.

Referenced by twi_master_init().

int twi_master_read ( volatile avr32_twim_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 otherwhise

References twi_package_t::addr, twi_package_t::addr_length, twi_package_t::buffer, twi_package_t::chip, twi_package_t::length, Swap32, TWI_SUCCESS, twim_read(), and twim_write().

Referenced by at42qt1060_get_status(), and at42qt1060_read_reg().

int twi_probe ( volatile avr32_twim_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 otherwhise

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

int twi_set_speed ( volatile avr32_twim_t *  twi,
unsigned int  speed,
unsigned long  pba_hz 
)

Set the twi bus speed in conjunction with the clock frequency.

Parameters
twiBase address of the TWI (i.e. &AVR32_TWI).
speedThe desired twi bus speed
pba_hzThe current running PBA clock frequency
Returns
TWI_SUCCESS

References TWI_INVALID_CLOCK_DIV, and TWI_SUCCESS.

Referenced by twi_master_init().

int twim_chained_transfer ( volatile avr32_twim_t *  twi,
volatile twim_transfer_t first,
volatile twim_transfer_t second,
bool  tenbit 
)
void twim_disable_interrupt ( volatile avr32_twim_t *  twi)
int twim_read ( volatile avr32_twim_t *  twi,
unsigned char *  buffer,
int  nbytes,
int  saddr,
bool  tenbit 
)
int twim_read_packet ( volatile avr32_twim_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 otherwhise

References twi_package_t::addr, twi_package_t::addr_length, twi_package_t::buffer, twi_package_t::chip, Enable_global_interrupt, twi_package_t::length, TWI_RECEIVE_NACK, TWI_SUCCESS, twim_disable_interrupt(), twim_inst, twim_it_mask, twim_nack, twim_rx_data, twim_rx_nb_bytes, twim_tx_data, and twim_tx_nb_bytes.

int twim_write ( volatile avr32_twim_t *  twi,
unsigned const char *  buffer,
int  nbytes,
int  saddr,
bool  tenbit 
)
int twim_write_packet ( volatile avr32_twim_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 otherwhise

References twi_package_t::addr, twi_package_t::addr_length, twi_package_t::buffer, twi_package_t::chip, twi_package_t::length, TWI_RECEIVE_NACK, TWI_SUCCESS, twim_tx_data, and twim_tx_nb_bytes.

Referenced by twi_probe().

volatile avr32_twim_t* twim_inst
static

Pointer to the instance 1 of the TWIM registers for IT.

Referenced by twi_master_interrupt_handler(), twim_read(), twim_read_packet(), and twim_write().

volatile unsigned long twim_it_mask
static
volatile bool twim_nack = false
static
volatile unsigned char* volatile twim_rx_data = NULL
static

Pointer to the applicative TWI receive buffer.

Referenced by twi_master_interrupt_handler(), twim_chained_transfer(), twim_read(), and twim_read_packet().

volatile int twim_rx_nb_bytes = 0
static

Remaining number of bytes to receive.

Referenced by twi_master_interrupt_handler(), and twim_read_packet().

const unsigned char* volatile twim_tx_data = NULL
static

Pointer to the applicative TWI transmit buffer.

Referenced by twi_master_interrupt_handler(), twim_chained_transfer(), twim_read_packet(), twim_write(), and twim_write_packet().

volatile int twim_tx_nb_bytes = 0
static

Remaining number of bytes to transmit.

Referenced by twi_master_interrupt_handler(), twim_chained_transfer(), twim_read_packet(), twim_write(), and twim_write_packet().