71#define LOW_LEVEL_TIME_LIMIT 384000
72#define I2C_FAST_MODE_SPEED 400000
73#define TWI_CLK_DIVIDER 2
75#define TWI_CLK_CALC_ARGU 3
77#define TWI_CLK_CALC_ARGU 4
79#define TWI_CLK_DIV_MAX 0xFF
80#define TWI_CLK_DIV_MIN 7
82#define TWI_WP_KEY_VALUE TWI_WPMR_WPKEY_PASSWD
139 if (p_opt->
smbus == 1) {
160 uint32_t cldiv, chdiv;
229 packet.
chip = (uint32_t) uc_slave_addr;
281 uint32_t cnt = p_packet->
length;
282 uint8_t *buffer = p_packet->
buffer;
283 uint8_t stop_sent = 0;
321 if (cnt == 1 && !stop_sent) {
356 uint32_t cnt = p_packet->
length;
357 uint8_t *buffer = p_packet->
buffer;
553 uint32_t status, cnt = 0;
561 *p_data++ = (uint8_t) p_twi->
TWI_RHR;
585 uint32_t status, cnt = 0;
625 Pdc *p_pdc_base = NULL;
638 p_pdc_base = PDC_TWI2;
648#if (SAM4E || SAM4C || SAMG || SAM4CP || SAM4CM)
655void twi_set_write_protection(
Twi *p_twi,
bool flag)
667void twi_read_write_protection_status(
Twi *p_twi, uint32_t *p_status)
670 *p_status = p_twi->TWI_WPSR;
681void twi_smbus_set_timing(
Twi *p_twi, uint32_t ul_timing)
683 p_twi->TWI_SMBTR = ul_timing;;
692void twi_set_alternative_command(
Twi *p_twi, uint32_t ul_alt_cmd)
694 p_twi->TWI_ACR = ul_alt_cmd;;
703void twi_set_filter(
Twi *p_twi, uint32_t ul_filter)
705 p_twi->TWI_FILTR = ul_filter;;
715void twi_mask_slave_addr(
Twi *p_twi, uint32_t ul_mask)
717 p_twi->
TWI_SMR |= TWI_SMR_MASK(ul_mask);
733void twi_set_sleepwalking(
Twi *p_twi,
734 uint32_t ul_matching_addr1,
bool flag1,
735 uint32_t ul_matching_addr2,
bool flag2,
736 uint32_t ul_matching_addr3,
bool flag3,
737 uint32_t ul_matching_data,
bool flag)
742 temp |= TWI_SWMR_SADR1(ul_matching_addr1);
746 temp |= TWI_SWMR_SADR2(ul_matching_addr2);
750 temp |= TWI_SWMR_SADR3(ul_matching_addr3);
754 temp |= TWI_SWMR_DATAM(ul_matching_data);
757 p_twi->TWI_SWMR = temp;
#define TWI_MMR_MREAD
(TWI_MMR) Master Read Direction
#define TWI_SR_TXCOMP
(TWI_SR) Transmission Completed (automatically set / reset)
#define TWI_SR_RXRDY
(TWI_SR) Receive Holding Register Ready (automatically set / reset)
#define TWI_CWGR_CHDIV(value)
#define TWI_CR_START
(TWI_CR) Send a START Condition
#define TWI_CR_SWRST
(TWI_CR) Software Reset
#define TWI_SR_GACC
(TWI_SR) General Call Access (clear on read)
#define TWI_SMR_SADR(value)
#define TWI_CR_MSDIS
(TWI_CR) TWI Master Mode Disabled
#define TWI_SR_TXRDY
(TWI_SR) Transmit Holding Register Ready (automatically set / reset)
#define TWI_SR_NACK
(TWI_SR) Not Acknowledged (clear on read)
#define TWI_CR_SVEN
(TWI_CR) TWI Slave Mode Enabled
#define TWI_MMR_IADRSZ_Msk
(TWI_MMR) Internal Device Address Size
#define TWI_SR_EOSACC
(TWI_SR) End Of Slave Access (clear on read)
#define TWI_CWGR_CLDIV(value)
#define TWI_CR_STOP
(TWI_CR) Send a STOP Condition
#define TWI_MMR_IADRSZ_Pos
#define TWI_SR_SVREAD
(TWI_SR) Slave Read (automatically set / reset)
#define TWI_MMR_DADR(value)
#define TWI_CR_QUICK
(TWI_CR) SMBUS Quick Command
#define TWI_SR_SVACC
(TWI_SR) Slave Access (automatically set / reset)
#define TWI_CR_SVDIS
(TWI_CR) TWI Slave Mode Disabled
#define TWI_CWGR_CKDIV(value)
#define TWI_CR_MSEN
(TWI_CR) TWI Master Mode Enabled
uint32_t twi_get_interrupt_mask(Twi *p_twi)
Read TWI interrupt mask.
void twi_disable_interrupt(Twi *p_twi, uint32_t ul_sources)
Disable TWI interrupts.
uint8_t twi_read_byte(Twi *p_twi)
Reads a byte from the TWI bus.
uint32_t twi_slave_write(Twi *p_twi, uint8_t *p_data)
Write data to TWI bus.
uint32_t twi_slave_read(Twi *p_twi, uint8_t *p_data)
Read data from master.
#define TWI_CLK_CALC_ARGU
uint32_t twi_probe(Twi *p_twi, uint8_t uc_slave_addr)
Test if a chip answers a given I2C address.
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.
#define I2C_FAST_MODE_SPEED
uint32_t twi_mk_addr(const uint8_t *addr, int len)
Construct the TWI module address register field.
void twi_set_slave_addr(Twi *p_twi, uint32_t ul_device_addr)
Set TWI slave address.
uint32_t twi_get_interrupt_status(Twi *p_twi)
Get TWI interrupt status.
Pdc * twi_get_pdc_base(Twi *p_twi)
Get TWI PDC base address.
void twi_enable_master_mode(Twi *p_twi)
Enable TWI master mode.
#define LOW_LEVEL_TIME_LIMIT
void twi_disable_master_mode(Twi *p_twi)
Disable TWI master mode.
void twi_enable_interrupt(Twi *p_twi, uint32_t ul_sources)
Enable TWI interrupts.
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.
void twi_enable_slave_mode(Twi *p_twi)
Enable TWI slave mode.
void twi_reset(Twi *p_twi)
Reset TWI.
void twi_disable_slave_mode(Twi *p_twi)
Disable TWI slave mode.
uint32_t twi_master_write(Twi *p_twi, twi_packet_t *p_packet)
Write multiple bytes to a TWI compatible slave device.
uint32_t twi_master_init(Twi *p_twi, const twi_options_t *p_opt)
Initialize TWI master mode.
void twi_slave_init(Twi *p_twi, uint32_t ul_device_addr)
Initialize TWI slave mode.
uint32_t twi_master_read(Twi *p_twi, twi_packet_t *p_packet)
Read multiple bytes from a TWI compatible slave device.
#define PDC_TWI0
(PDC_TWI0 ) Base Address
#define TWI0
(TWI0 ) Base Address
#define TWI1
(TWI1 ) Base Address
#define PDC_TWI1
(PDC_TWI1 ) Base Address
__I uint32_t TWI_SR
(Twi Offset: 0x20) Status Register
__I uint32_t TWI_RHR
(Twi Offset: 0x30) Receive Holding Register
__IO uint32_t TWI_CWGR
(Twi Offset: 0x10) Clock Waveform Generator Register
__IO uint32_t TWI_IADR
(Twi Offset: 0x0C) Internal Address Register
__O uint32_t TWI_IDR
(Twi Offset: 0x28) Interrupt Disable Register
__O uint32_t TWI_CR
(Twi Offset: 0x00) Control Register
__O uint32_t TWI_IER
(Twi Offset: 0x24) Interrupt Enable Register
__IO uint32_t TWI_MMR
(Twi Offset: 0x04) Master Mode Register
__O uint32_t TWI_THR
(Twi Offset: 0x34) Transmit Holding Register
__I uint32_t TWI_IMR
(Twi Offset: 0x2C) Interrupt Mask Register
__IO uint32_t TWI_SMR
(Twi Offset: 0x08) Slave Mode Register
uint32_t speed
The baud rate of the TWI bus.
uint8_t smbus
SMBUS mode (set 1 to use SMBUS quick command, otherwise don't).
uint32_t master_clk
MCK for TWI.
uint32_t addr_length
Length of the TWI data address segment (1-3 bytes).
void * buffer
Where to find the data to be transferred.
uint32_t length
How many bytes do we want to transfer.
uint8_t chip
TWI chip address to communicate with.
uint8_t addr[3]
TWI address/commands to issue to the other chip (node).
Two-Wire Interface (TWI) driver for SAM.
struct twi_packet twi_packet_t
Information concerning the data transmission.
struct twi_options twi_options_t
Input parameters when initializing the TWI module mode.
#define TWI_TIMEOUT
Time-out value (number of attempts).
#define TWI_SUCCESS
Return codes for TWI APIs.
#define TWI_INVALID_ARGUMENT
#define TWI_ERROR_TIMEOUT