CryptoAuthLib
Microchip CryptoAuthentication Library
i2c_bitbang_samd21.c File Reference

Hardware Interface Functions - I2C bit-bang for SAMD21. More...

#include <asf.h>
#include <stdint.h>
#include "i2c_bitbang_samd21.h"

Macros

#define DEFAULT_I2C_BUS   2
 

Functions

void i2c_discover_buses (int i2c_bitbang_buses[], int max_buses)
 Assigns the logical bus number for discovering the devices. More...
 
void i2c_set_pin (uint8_t sda, uint8_t scl)
 Set I2C data and clock pin. Other functions will use these pins. More...
 
void i2c_enable (void)
 Configure GPIO pins for I2C clock and data as output. More...
 
void i2c_disable (void)
 Configure GPIO pins for I2C clock and data as input. More...
 
void i2c_send_start (void)
 Send a START condition. More...
 
void i2c_send_ack (uint8_t ack)
 Send an ACK or NACK (after receive). More...
 
void i2c_send_stop (void)
 Send a STOP condition. More...
 
void i2c_send_wake_token (void)
 Send a Wake Token. More...
 
ATCA_STATUS i2c_send_byte (uint8_t i2c_byte)
 Send one byte. More...
 
ATCA_STATUS i2c_send_bytes (uint8_t count, uint8_t *data)
 Send a number of bytes. More...
 
uint8_t i2c_receive_one_byte (uint8_t ack)
 Receive one byte (MSB first). More...
 
void i2c_receive_byte (uint8_t *data)
 Receive one byte and send ACK. More...
 
void i2c_receive_bytes (uint8_t count, uint8_t *data)
 Receive a number of bytes. More...
 

Variables

I2CBuses i2c_buses_default
 
uint8_t pin_sda
 
uint8_t pin_scl
 

Detailed Description

Hardware Interface Functions - I2C bit-bang for SAMD21.

Macro Definition Documentation

◆ DEFAULT_I2C_BUS

#define DEFAULT_I2C_BUS   2

Function Documentation

◆ i2c_disable()

void i2c_disable ( void  )

Configure GPIO pins for I2C clock and data as input.

◆ i2c_discover_buses()

void i2c_discover_buses ( int  i2c_bitbang_buses[],
int  max_buses 
)

Assigns the logical bus number for discovering the devices.

Parameters
[in]i2c_bitbang_busesThe logical bus numbers are assigned to the variables.
[in]max_busesMaximum number of bus used for discovering.

◆ i2c_enable()

void i2c_enable ( void  )

Configure GPIO pins for I2C clock and data as output.

◆ i2c_receive_byte()

void i2c_receive_byte ( uint8_t *  data)

Receive one byte and send ACK.

Parameters
[out]datapointer to received byte

◆ i2c_receive_bytes()

void i2c_receive_bytes ( uint8_t  count,
uint8_t *  data 
)

Receive a number of bytes.

Parameters
[out]datapointer to receive buffer
[in]countnumber of bytes to receive

◆ i2c_receive_one_byte()

uint8_t i2c_receive_one_byte ( uint8_t  ack)

Receive one byte (MSB first).

Parameters
[in]ack0:NACK, else:ACK
Returns
Number of bytes received

We don't need to delay after the last bit because it takes time to switch the pin to output for acknowledging.

◆ i2c_send_ack()

void i2c_send_ack ( uint8_t  ack)

Send an ACK or NACK (after receive).

Parameters
[in]ack0: NACK, else: ACK

< Low data line indicates an ACK.

< High data line indicates a NACK.

Clock out acknowledgment.

◆ i2c_send_byte()

ATCA_STATUS i2c_send_byte ( uint8_t  i2c_byte)

Send one byte.

Parameters
[in]i2c_bytebyte to write
Returns
ATCA_STATUS

This avoids spikes but adds an if condition. We could parametrize the call to I2C_SET_OUTPUT and translate the msb to OUTSET or OUTCLR, but then the code would become target specific.





Send 8 bits of data.

Clock out the data bit.

Shifting while clock is high compensates for the time it takes to evaluate the bit while clock is low. That way, the low and high time of the clock pin is almost equal.


Clock in last data bit.

Set data line to be an input.

Wait for the ack.

◆ i2c_send_bytes()

ATCA_STATUS i2c_send_bytes ( uint8_t  count,
uint8_t *  data 
)

Send a number of bytes.

Parameters
[in]countnumber of bytes to send
[in]datapointer to buffer containing bytes to send
Returns
ATCA_STATUS

◆ i2c_send_start()

void i2c_send_start ( void  )

Send a START condition.

Set clock high in case we re-start.

◆ i2c_send_stop()

void i2c_send_stop ( void  )

Send a STOP condition.

◆ i2c_send_wake_token()

void i2c_send_wake_token ( void  )

Send a Wake Token.

◆ i2c_set_pin()

void i2c_set_pin ( uint8_t  sda,
uint8_t  scl 
)

Set I2C data and clock pin. Other functions will use these pins.

Parameters
[in]sdadefinition of GPIO pin to be used as data pin
[in]scldefinition of GPIO pin to be used as clock pin

Variable Documentation

◆ i2c_buses_default

I2CBuses i2c_buses_default
Initial value:
= {
{EXT3_PIN_3, EXT3_PIN_9, EXT3_PIN_I2C_SDA, EXT3_PIN_13, EXT2_PIN_3, EXT2_PIN_5, EXT2_PIN_7, EXT2_PIN_9, EXT2_PIN_13, EXT2_PIN_15, EXT2_PIN_17, EXT1_PIN_3, EXT1_PIN_5, EXT1_PIN_7, EXT1_PIN_9, EXT1_PIN_13, EXT1_PIN_15, EXT1_PIN_17},
{EXT3_PIN_7, EXT3_PIN_10, EXT3_PIN_I2C_SCL, EXT3_PIN_14, EXT2_PIN_4, EXT2_PIN_6, EXT2_PIN_8, EXT2_PIN_10, EXT2_PIN_14, EXT2_PIN_16, EXT2_PIN_18, EXT1_PIN_4, EXT1_PIN_6, EXT1_PIN_8, EXT1_PIN_10, EXT1_PIN_14, EXT1_PIN_16, EXT1_PIN_18}
}

◆ pin_scl

uint8_t pin_scl

◆ pin_sda

uint8_t pin_sda