Microchip® Advanced Software Framework

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

Driver for the TWIM (Two-Wire Master Interface).

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

Data Structures

struct  twim_config
 Input parameters when initializing the TWIM module mode. More...
 
struct  twim_package
 Information concerning the data transmission. More...
 

Macros

#define TWIM_IER_NAK_MASK   (TWIM_IER_ANAK | TWIM_IER_DNAK)
 Interrupt Enable Register Mask for No Acknowledgements. More...
 
#define TWIM_IER_STD_MASK   (TWIM_IER_ANAK | TWIM_IER_ARBLST)
 Frequently used Interrupt Enable Register Mask. More...
 
#define TWIM_LOW_POWER_ENABLE   1
 Enable TWIM Low Power Transfer in default. More...
 
#define TWIM_SCR_NAK_MASK   (TWIM_SCR_ANAK | TWIM_SCR_DNAK)
 Status Clear Register Mask for No Acknowledgements. More...
 
#define TWIM_SR_NAK_MASK   (TWIM_SR_ANAK | TWIM_SR_DNAK)
 Status Register Mask for No Acknowledgements. More...
 
#define TWIM_SR_STD_MASK   (TWIM_SR_ANAK | TWIM_SR_ARBLST)
 Frequently used Status Clear Register Mask. More...
 

Typedefs

typedef void(* twim_callback_t )(Twim *)
 

Functions

status_code_t twi_master_read (Twim *twim, struct twim_package *package)
 Read multiple bytes from a TWI compatible slave device. More...
 
status_code_t twi_master_write (Twim *twim, struct twim_package *package)
 Write multiple bytes to a TWI compatible slave device. More...
 
void twim_clear_status (Twim *twim, uint32_t clear_status)
 Clear the current status of the TWIM. More...
 
void twim_default_callback (Twim *twim)
 TWIM default callback function. More...
 
static void twim_disable (Twim *twim)
 Disable Master Mode of the TWI. More...
 
void twim_disable_interrupt (Twim *twim, uint32_t interrupt_source)
 Disable the TWIM interrupts and clear their status. More...
 
static void twim_enable (Twim *twim)
 Enable Master Mode of the TWI. More...
 
void twim_enable_interrupt (Twim *twim, uint32_t interrupt_source)
 Enable the TWIM interrupts. More...
 
uint32_t twim_get_interrupt_mask (Twim *twim)
 Get the TWIM interrupt mask. More...
 
uint32_t twim_get_status (Twim *twim)
 Information about the current status of the TWIM. More...
 
void twim_pdca_transfer_prepare (Twim *twim, twi_package_t *package, bool read)
 Set TWIM for PDCA transfer. More...
 
status_code_t twim_probe (Twim *twim, uint32_t chip_addr)
 Test if a chip answers for a given TWI address. More...
 
void twim_set_callback (Twim *twim, uint32_t interrupt_source, twim_callback_t callback, uint8_t irq_level)
 Set callback for TWIM. More...
 
status_code_t twim_set_config (Twim *twim, struct twim_config *config)
 Initialize the TWIM module. More...
 
status_code_t twim_set_hsmode_speed (Twim *twim, uint32_t speed, uint32_t clk, uint8_t cycles)
 Set the TWI bus speed in conjunction with the clock frequency in high speed mode. More...
 
status_code_t twim_set_speed (Twim *twim, uint32_t speed, uint32_t clk, uint8_t cycles)
 Set the TWI bus speed in conjunction with the clock frequency. More...
 

TWI transfer speed definitions

#define TWI_STD_MODE_SPEED   ( 100000 /* kbit/s */)
 TWI Standard Mode. More...
 
#define TWI_FAST_MODE_SPEED   ( 400000 /* kbit/s */)
 TWI Fast Mode. More...
 
#define TWI_FAST_MODE_PLUS_SPEED   (1000000 /* kbit/s */)
 TWI Fast Mode Plus. More...
 
#define TWI_HIGH_SPEED_MODE_SPEED   (3400000 /* kbit/s */)
 TWI High Speed Mode. More...
 
enum  twim_transfer_status {
  TWI_SUCCESS = 0,
  TWI_INVALID_ARGUMENT = -1,
  TWI_ARBITRATION_LOST = -2,
  TWI_NO_CHIP_FOUND = -3,
  TWI_RECEIVE_NACK = -4,
  TWI_SEND_NACK = -5,
  TWI_INVALID_CLOCK_DIV = -6
}
 Status Codes for TWI Transfer. More...
 
typedef enum twim_transfer_status twim_transfer_status_t
 

TWI Driver Compatibility

Codes for SAM devices using TWI modules can easily be ported to SAM devices with TWIM module

#define twi_options_t   struct twim_config
 
#define twi_package_t   struct twim_package
 
#define twi_master_init   twim_set_config
 
#define twi_probe   twim_probe
 

#define TWI_FAST_MODE_PLUS_SPEED   (1000000 /* kbit/s */)

TWI Fast Mode Plus.

Referenced by twim_set_config().

#define TWI_FAST_MODE_SPEED   ( 400000 /* kbit/s */)

TWI Fast Mode.

#define TWI_HIGH_SPEED_MODE_SPEED   (3400000 /* kbit/s */)

TWI High Speed Mode.

#define twi_master_init   twim_set_config
#define twi_options_t   struct twim_config
#define twi_package_t   struct twim_package
#define twi_probe   twim_probe
#define TWI_STD_MODE_SPEED   ( 100000 /* kbit/s */)

TWI Standard Mode.

#define TWIM_IER_NAK_MASK   (TWIM_IER_ANAK | TWIM_IER_DNAK)

Interrupt Enable Register Mask for No Acknowledgements.

#define TWIM_IER_STD_MASK   (TWIM_IER_ANAK | TWIM_IER_ARBLST)

Frequently used Interrupt Enable Register Mask.

Referenced by twi_master_read(), and twi_master_write().

#define TWIM_LOW_POWER_ENABLE   1

Enable TWIM Low Power Transfer in default.

#define TWIM_SCR_NAK_MASK   (TWIM_SCR_ANAK | TWIM_SCR_DNAK)

Status Clear Register Mask for No Acknowledgements.

#define TWIM_SR_NAK_MASK   (TWIM_SR_ANAK | TWIM_SR_DNAK)

Status Register Mask for No Acknowledgements.

#define TWIM_SR_STD_MASK   (TWIM_SR_ANAK | TWIM_SR_ARBLST)

Frequently used Status Clear Register Mask.

Referenced by twim_default_callback().

typedef void(* twim_callback_t)(Twim *)

Status Codes for TWI Transfer.

Enumerator
TWI_SUCCESS 
TWI_INVALID_ARGUMENT 

TWI Transaction Success.

TWI_ARBITRATION_LOST 

Invalid Argument Passed.

TWI_NO_CHIP_FOUND 

Bus Arbitration Lost.

TWI_RECEIVE_NACK 

Slave Not Found.

TWI_SEND_NACK 

Data No Acknowledgement Received.

TWI_INVALID_CLOCK_DIV 

Data No Acknowledgement Send.

void twim_clear_status ( Twim *  twim,
uint32_t  clear_status 
)

Clear the current status of the TWIM.

Parameters
twimBase address of the TWIM
clear_statusThe TWIM status to be clear
static void twim_disable ( Twim *  twim)
inlinestatic

Disable Master Mode of the TWI.

Parameters
twimBase address of the TWIM instance.
void twim_disable_interrupt ( Twim *  twim,
uint32_t  interrupt_source 
)

Disable the TWIM interrupts and clear their status.

Parameters
twimBase address of the TWIM.
interrupt_sourceThe TWI interrupt to be disabled
static void twim_enable ( Twim *  twim)
inlinestatic

Enable Master Mode of the TWI.

Parameters
twimBase address of the TWIM instance.
void twim_enable_interrupt ( Twim *  twim,
uint32_t  interrupt_source 
)

Enable the TWIM interrupts.

Parameters
twimBase address of the TWIM
interrupt_sourceThe TWI interrupt to be enabled

Referenced by twim_set_callback().

uint32_t twim_get_interrupt_mask ( Twim *  twim)

Get the TWIM interrupt mask.

Parameters
twimBase address of the TWIM
Return values
TWIMinterrupt mask
uint32_t twim_get_status ( Twim *  twim)

Information about the current status of the TWIM.

Parameters
twimBase address of the TWIM

Referenced by twi_master_read(), and twi_master_write().

void twim_pdca_transfer_prepare ( Twim *  twim,
twi_package_t package,
bool  read 
)

Set TWIM for PDCA transfer.

Parameters
twimBase address of the TWIM
packagePackage information and data (see twim_package_t)
readTrue if it's a read trasnfer

Referenced by read_test_pdca(), and write_test_pdca().

status_code_t twim_probe ( Twim *  twim,
uint32_t  chip_addr 
)

Test if a chip answers for a given TWI address.

Parameters
twimBase address of the TWIM
chip_addrAddress of the chip which is searched for
Return values
STATUS_OKSlave Found
ERR_IO_ERRORANAK received or Bus Arbitration lost

References twim_package::addr_length, twim_package::buffer, twim_package::chip, twim_package::length, and twi_master_read().

void twim_set_callback ( Twim *  twim,
uint32_t  interrupt_source,
twim_callback_t  callback,
uint8_t  irq_level 
)

Set callback for TWIM.

Parameters
twimBase address of the TWIM
interrupt_sourceTWIM interrupt source
callbackCallback function pointer
irq_levelInterrupt level

References find_twim_channel_num(), twim_callback_pointer, and twim_enable_interrupt().

Referenced by init_test().

status_code_t twim_set_hsmode_speed ( Twim *  twim,
uint32_t  speed,
uint32_t  clk,
uint8_t  cycles 
)

Set the TWI bus speed in conjunction with the clock frequency in high speed mode.

Parameters
twimBase address of the TWIM
speedThe desired twim bus speed
clkThe current running system clock frequency
cyclesClock cycles for data setup count
Return values
STATUS_OKTransaction is successful
ERR_INVALID_ARGInvalid arg resulting in wrong CWGR Exponential

References ERR_INVALID_ARG, and STATUS_OK.

Referenced by twim_set_config().

status_code_t twim_set_speed ( Twim *  twim,
uint32_t  speed,
uint32_t  clk,
uint8_t  cycles 
)

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

Parameters
twimBase address of the TWIM
speedThe desired twim bus speed
clkThe current running system clock frequency
cyclesClock cycles for data setup count
Return values
STATUS_OKTransaction is successful
ERR_INVALID_ARGInvalid arg resulting in wrong CWGR Exponential

References ERR_INVALID_ARG, and STATUS_OK.

Referenced by twim_set_config().