Microchip® Advanced Software Framework

twi.h File Reference

Two-Wire Interface (TWI) driver for SAM.

Copyright (c) 2011-2018 Microchip Technology Inc. and its subsidiaries.

#include "compiler.h"

Data Structures

struct  twi_options
 Input parameters when initializing the TWI module mode. More...
 
struct  twi_packet
 Information concerning the data transmission. More...
 

Macros

#define TWI_TIMEOUT   30000
 Time-out value (number of attempts). More...
 
#define TWI_SUCCESS   0
 Return codes for TWI APIs. More...
 
#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_ERROR_TIMEOUT   9
 

Typedefs

typedef struct twi_options twi_options_t
 Input parameters when initializing the TWI module mode. More...
 
typedef struct twi_packet twi_packet_t
 Information concerning the data transmission. More...
 

Enumerations

enum  twi_source_clock {
  TWI_SOURCE_PERIPH_CLK = TWI_CWGR_BRSRCCLK_PERIPH_CLK,
  TWI_SOURCE_PCK_CLK = TWI_CWGR_BRSRCCLK_PMC_PCK
}
 

Functions

static void twi_clear_disable_clock_wait_state (Twi *p_twi)
 Clear clock wait state disable mode. More...
 
static void twi_disable_alternative_command (Twi *p_twi)
 Enable alternative command mode. More...
 
static void twi_disable_clock_wait_state (Twi *p_twi)
 Clock stretching disabled in slave mode, OVRE and UNRE will indicate overrun and underrun. More...
 
static void twi_disable_highspeed (Twi *p_twi)
 Disable high speed mode. More...
 
void twi_disable_interrupt (Twi *p_twi, uint32_t ul_sources)
 Disable TWI interrupts. More...
 
void twi_disable_master_mode (Twi *p_twi)
 Disable TWI master mode. More...
 
static void twi_disable_pec (Twi *p_twi)
 Disable packet error checking. More...
 
static void twi_disable_slave_addr1_matching (Twi *p_twi)
 Slave Address 1 matching disabled. More...
 
static void twi_disable_slave_addr2_matching (Twi *p_twi)
 Slave Address 2 matching disabled. More...
 
static void twi_disable_slave_addr3_matching (Twi *p_twi)
 Slave Address 3 matching disabled. More...
 
static void twi_disable_slave_data_matching (Twi *p_twi)
 First received data matching disabled. More...
 
static void twi_disable_slave_default_addr (Twi *p_twi)
 Acknowledge of the SMBus Default Address disabled. More...
 
void twi_disable_slave_mode (Twi *p_twi)
 Disable TWI slave mode. More...
 
static void twi_disable_slave_nack (Twi *p_twi)
 Normal value to be returned in the ACK cycle of the data phase in slave receiver mode. More...
 
static void twi_disable_smbus (Twi *p_twi)
 Disable SMBus mode. More...
 
static void twi_disable_smbus_host_header (Twi *p_twi)
 Acknowledge of the SMBus Host Header disabled. More...
 
static void twi_enable_alternative_command (Twi *p_twi)
 Enable alternative command mode. More...
 
static void twi_enable_highspeed (Twi *p_twi)
 Enable high speed mode. More...
 
void twi_enable_interrupt (Twi *p_twi, uint32_t ul_sources)
 Enable TWI interrupts. More...
 
void twi_enable_master_mode (Twi *p_twi)
 Enable TWI master mode. More...
 
static void twi_enable_pec (Twi *p_twi)
 Enable packet error checking. More...
 
static void twi_enable_slave_addr1_matching (Twi *p_twi)
 Slave Address 1 matching enabled. More...
 
static void twi_enable_slave_addr2_matching (Twi *p_twi)
 Slave Address 2 matching enabled. More...
 
static void twi_enable_slave_addr3_matching (Twi *p_twi)
 Slave Address 3 matching enabled. More...
 
static void twi_enable_slave_default_addr (Twi *p_twi)
 Acknowledge of the SMBus Default Address enabled. More...
 
void twi_enable_slave_mode (Twi *p_twi)
 Enable TWI slave mode. More...
 
static void twi_enable_slave_nack (Twi *p_twi)
 NACK value to be returned in the ACK cycle of the data phase in slave receiver mode. More...
 
static void twi_enable_smbus (Twi *p_twi)
 Enable SMBus mode. More...
 
static void twi_enable_smbus_host_header (Twi *p_twi)
 Acknowledge of the SMBus Host Header enabled. More...
 
uint32_t twi_get_interrupt_mask (Twi *p_twi)
 Read TWI interrupt mask. More...
 
uint32_t twi_get_interrupt_status (Twi *p_twi)
 Get TWI interrupt status. More...
 
Pdc * twi_get_pdc_base (Twi *p_twi)
 Get TWI PDC base address. More...
 
static void twi_lock_clear (Twi *p_twi)
 Clear the TWI FSM lock. More...
 
void twi_mask_slave_addr (Twi *p_twi, uint32_t ul_mask)
 
uint32_t twi_master_init (Twi *p_twi, const twi_options_t *p_opt)
 Initialize TWI master mode. More...
 
uint32_t twi_master_read (Twi *p_twi, twi_packet_t *p_packet)
 Read multiple bytes from a TWI compatible slave device. More...
 
uint32_t twi_master_write (Twi *p_twi, twi_packet_t *p_packet)
 Write multiple bytes to a TWI compatible slave device. More...
 
uint32_t twi_mk_addr (const uint8_t *addr, int len)
 Construct the TWI module address register field. More...
 
uint32_t twi_probe (Twi *p_twi, uint8_t uc_slave_addr)
 Test if a chip answers a given I2C address. More...
 
uint8_t twi_read_byte (Twi *p_twi)
 Reads a byte from the TWI bus. More...
 
void twi_read_write_protection_status (Twi *p_twi, uint32_t *p_status)
 
static void twi_request_pec (Twi *p_twi)
 Request a packet error checking. More...
 
void twi_reset (Twi *p_twi)
 Reset TWI. More...
 
static void twi_select_source_clock (Twi *p_twi, enum twi_source_clock src_clk)
 Select the souce clock for the bit rate generation. More...
 
static void twi_send_clear (Twi *p_twi)
 If master mode is enabled, send a bus clear command. More...
 
void twi_set_alternative_command (Twi *p_twi, uint32_t ul_alt_cmd)
 
void twi_set_filter (Twi *p_twi, uint32_t ul_filter)
 
void twi_set_slave_addr (Twi *p_twi, uint32_t ul_device_addr)
 Set TWI slave address. More...
 
void twi_set_sleepwalking (Twi *p_twi, uint32_t ul_matching_addr1, bool flag1, uint32_t ul_matching_addr2, bool flag2, uint32_t ul_matching_addr3, bool flag3, uint32_t ul_matching_data, bool flag)
 
uint32_t twi_set_speed (Twi *p_twi, uint32_t ul_speed, uint32_t ul_mck)
 Set the I2C bus speed in conjunction with the clock frequency. More...
 
void twi_set_write_protection (Twi *p_twi, bool flag)
 
void twi_slave_init (Twi *p_twi, uint32_t ul_device_addr)
 Initialize TWI slave mode. More...
 
uint32_t twi_slave_read (Twi *p_twi, uint8_t *p_data)
 Read data from master. More...
 
uint32_t twi_slave_write (Twi *p_twi, uint8_t *p_data)
 Write data to TWI bus. More...
 
void twi_smbus_set_timing (Twi *p_twi, uint32_t ul_timing)
 
static void twi_thr_clear (Twi *p_twi)
 Clear the Transmit Holding Register and set TXRDY, TXCOMP flags. More...
 
void twi_write_byte (Twi *p_twi, uint8_t uc_byte)
 Sends a byte of data to one of the TWI slaves on the bus. More...
 

#define TWI_ARBITRATION_LOST   2
#define TWI_BUSY   8
#define TWI_ERROR_TIMEOUT   9

Referenced by twi_master_read().

#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
#define TWI_SUCCESS   0

Return codes for TWI APIs.

Referenced by twi_master_init(), twi_master_read(), and twi_master_write().

#define TWI_TIMEOUT   30000

Time-out value (number of attempts).

Referenced by twi_master_read().

typedef struct twi_options twi_options_t

Input parameters when initializing the TWI module mode.

typedef struct twi_packet twi_packet_t

Information concerning the data transmission.

Enumerator
TWI_SOURCE_PERIPH_CLK 
TWI_SOURCE_PCK_CLK 

static void twi_clear_disable_clock_wait_state ( Twi *  p_twi)
inlinestatic

Clear clock wait state disable mode.

Parameters
p_twiBase address of the TWI instance.
static void twi_disable_alternative_command ( Twi *  p_twi)
inlinestatic

Enable alternative command mode.

Parameters
p_twiBase address of the TWI instance.
static void twi_disable_clock_wait_state ( Twi *  p_twi)
inlinestatic

Clock stretching disabled in slave mode, OVRE and UNRE will indicate overrun and underrun.

Parameters
p_twiBase address of the TWI instance.
static void twi_disable_highspeed ( Twi *  p_twi)
inlinestatic

Disable high speed mode.

Parameters
p_twiBase address of the TWI instance.
static void twi_disable_pec ( Twi *  p_twi)
inlinestatic

Disable packet error checking.

Parameters
p_twiBase address of the TWI instance.
static void twi_disable_slave_addr1_matching ( Twi *  p_twi)
inlinestatic

Slave Address 1 matching disabled.

Parameters
p_twiBase address of the TWI instance.
static void twi_disable_slave_addr2_matching ( Twi *  p_twi)
inlinestatic

Slave Address 2 matching disabled.

Parameters
p_twiBase address of the TWI instance.
static void twi_disable_slave_addr3_matching ( Twi *  p_twi)
inlinestatic

Slave Address 3 matching disabled.

Parameters
p_twiBase address of the TWI instance.
static void twi_disable_slave_data_matching ( Twi *  p_twi)
inlinestatic

First received data matching disabled.

Parameters
p_twiBase address of the TWI instance.
static void twi_disable_slave_default_addr ( Twi *  p_twi)
inlinestatic

Acknowledge of the SMBus Default Address disabled.

Parameters
p_twiBase address of the TWI instance.
static void twi_disable_slave_nack ( Twi *  p_twi)
inlinestatic

Normal value to be returned in the ACK cycle of the data phase in slave receiver mode.

Parameters
p_twiBase address of the TWI instance.
static void twi_disable_smbus ( Twi *  p_twi)
inlinestatic

Disable SMBus mode.

Parameters
p_twiBase address of the TWI instance.
static void twi_disable_smbus_host_header ( Twi *  p_twi)
inlinestatic

Acknowledge of the SMBus Host Header disabled.

Parameters
p_twiBase address of the TWI instance.
static void twi_enable_alternative_command ( Twi *  p_twi)
inlinestatic

Enable alternative command mode.

Parameters
p_twiBase address of the TWI instance.
static void twi_enable_highspeed ( Twi *  p_twi)
inlinestatic

Enable high speed mode.

Parameters
p_twiBase address of the TWI instance.
static void twi_enable_pec ( Twi *  p_twi)
inlinestatic

Enable packet error checking.

Parameters
p_twiBase address of the TWI instance.
static void twi_enable_slave_addr1_matching ( Twi *  p_twi)
inlinestatic

Slave Address 1 matching enabled.

Parameters
p_twiBase address of the TWI instance.
static void twi_enable_slave_addr2_matching ( Twi *  p_twi)
inlinestatic

Slave Address 2 matching enabled.

Parameters
p_twiBase address of the TWI instance.
static void twi_enable_slave_addr3_matching ( Twi *  p_twi)
inlinestatic

Slave Address 3 matching enabled.

Parameters
p_twiBase address of the TWI instance.
static void twi_enable_slave_default_addr ( Twi *  p_twi)
inlinestatic

Acknowledge of the SMBus Default Address enabled.

Parameters
p_twiBase address of the TWI instance.
static void twi_enable_slave_nack ( Twi *  p_twi)
inlinestatic

NACK value to be returned in the ACK cycle of the data phase in slave receiver mode.

Parameters
p_twiBase address of the TWI instance.
static void twi_enable_smbus ( Twi *  p_twi)
inlinestatic

Enable SMBus mode.

Parameters
p_twiBase address of the TWI instance.
static void twi_enable_smbus_host_header ( Twi *  p_twi)
inlinestatic

Acknowledge of the SMBus Host Header enabled.

Parameters
p_twiBase address of the TWI instance.
static void twi_lock_clear ( Twi *  p_twi)
inlinestatic

Clear the TWI FSM lock.

Parameters
p_twiBase address of the TWI instance.
void twi_mask_slave_addr ( Twi *  p_twi,
uint32_t  ul_mask 
)
void twi_read_write_protection_status ( Twi *  p_twi,
uint32_t *  p_status 
)
static void twi_request_pec ( Twi *  p_twi)
inlinestatic

Request a packet error checking.

Parameters
p_twiBase address of the TWI instance.
static void twi_select_source_clock ( Twi *  p_twi,
enum twi_source_clock  src_clk 
)
inlinestatic

Select the souce clock for the bit rate generation.

Parameters
p_twiBase address of the TWI instance.
src_clkSource clock.
static void twi_send_clear ( Twi *  p_twi)
inlinestatic

If master mode is enabled, send a bus clear command.

Parameters
p_twiBase address of the TWI instance.
void twi_set_alternative_command ( Twi *  p_twi,
uint32_t  ul_alt_cmd 
)
void twi_set_filter ( Twi *  p_twi,
uint32_t  ul_filter 
)
void twi_set_sleepwalking ( Twi *  p_twi,
uint32_t  ul_matching_addr1,
bool  flag1,
uint32_t  ul_matching_addr2,
bool  flag2,
uint32_t  ul_matching_addr3,
bool  flag3,
uint32_t  ul_matching_data,
bool  flag 
)
void twi_set_write_protection ( Twi *  p_twi,
bool  flag 
)
void twi_smbus_set_timing ( Twi *  p_twi,
uint32_t  ul_timing 
)
static void twi_thr_clear ( Twi *  p_twi)
inlinestatic

Clear the Transmit Holding Register and set TXRDY, TXCOMP flags.

Parameters
p_twiBase address of the TWI instance.