Arduino Hardware I2C for AVR MCUs (plain c)
1.3
Arduino Hardware I2C for AVR MCUs (plain c) documentation
|
arduino master i2c in plain c code More...
#include "ci2c.h"
Macros | |
#define | START 0x08 |
#define | REPEATED_START 0x10 |
#define | MT_SLA_ACK 0x18 |
#define | MT_SLA_NACK 0x20 |
#define | MT_DATA_ACK 0x28 |
#define | MT_DATA_NACK 0x30 |
#define | MR_SLA_ACK 0x40 |
#define | MR_SLA_NACK 0x48 |
#define | MR_DATA_ACK 0x50 |
#define | MR_DATA_NACK 0x58 |
#define | LOST_ARBTRTN 0x38 |
#define | TWI_STATUS (TWSR & 0xF8) |
#define | setRegBit(r, b) r |= (1 << b) |
set bit b in register r More... | |
#define | clrRegBit(r, b) r &= (uint8_t) (~(1 << b)) |
clear bit b in register r More... | |
#define | invRegBit(r, b) r ^= (1 << b) |
invert bit b in register r More... | |
Functions | |
void | I2C_slave_init (I2C_SLAVE *slave, const uint8_t sl_addr, const I2C_INT_SIZE reg_sz) |
Init an I2C slave structure for cMI2C communication. More... | |
void | I2C_slave_set_rw_func (I2C_SLAVE *slave, const ci2c_fct_ptr func, const I2C_RW rw) |
Redirect slave I2C read/write function (if needed for advanced use) More... | |
bool | I2C_slave_set_addr (I2C_SLAVE *slave, const uint8_t sl_addr) |
Change I2C slave address. More... | |
bool | I2C_slave_set_reg_size (I2C_SLAVE *slave, const I2C_INT_SIZE reg_sz) |
Change I2C registers map size (for access) More... | |
void | I2C_init (const uint16_t speed) |
Enable I2c module on arduino board (including pull-ups, enabling of ACK, and setting clock frequency) More... | |
void | I2C_uninit () |
Disable I2c module on arduino board (releasing pull-ups, and TWI control) More... | |
void | I2C_reset (void) |
I2C bus reset (Release SCL and SDA lines and re-enable module) More... | |
uint16_t | I2C_set_speed (const uint16_t speed) |
Change I2C frequency. More... | |
uint16_t | I2C_set_timeout (const uint16_t timeout) |
Change I2C ack timeout. More... | |
uint8_t | I2C_set_retries (const uint8_t retries) |
Change I2C message retries (in case of failure) More... | |
bool | I2C_is_busy (void) |
Get I2C busy status. More... | |
I2C_STATUS | I2C_write (I2C_SLAVE *slave, const uint16_t reg_addr, uint8_t *data, const uint16_t bytes) |
This function writes the provided data to the address specified. More... | |
I2C_STATUS | I2C_read (I2C_SLAVE *slave, const uint16_t reg_addr, uint8_t *data, const uint16_t bytes) |
This function reads data from the address specified and stores this data in the area provided by the pointer. More... | |
bool | I2C_start (void) |
Send start condition. More... | |
bool | I2C_stop (void) |
Send stop condition. More... | |
bool | I2C_wr8 (const uint8_t dat) |
Send byte on bus. More... | |
uint8_t | I2C_rd8 (const bool ack) |
Receive byte from bus. More... | |
bool | I2C_sndAddr (I2C_SLAVE *slave, const I2C_RW rw) |
Send I2C address. More... | |
arduino master i2c in plain c code
#define clrRegBit | ( | r, | |
b | |||
) | r &= (uint8_t) (~(1 << b)) |
clear bit b in register r
#define invRegBit | ( | r, | |
b | |||
) | r ^= (1 << b) |
invert bit b in register r
#define LOST_ARBTRTN 0x38 |
#define MR_DATA_ACK 0x50 |
#define MR_DATA_NACK 0x58 |
#define MR_SLA_ACK 0x40 |
#define MR_SLA_NACK 0x48 |
#define MT_DATA_ACK 0x28 |
#define MT_DATA_NACK 0x30 |
#define MT_SLA_ACK 0x18 |
#define MT_SLA_NACK 0x20 |
#define REPEATED_START 0x10 |
#define setRegBit | ( | r, | |
b | |||
) | r |= (1 << b) |
set bit b in register r
#define START 0x08 |
#define TWI_STATUS (TWSR & 0xF8) |
void I2C_init | ( | const uint16_t | speed | ) |
Enable I2c module on arduino board (including pull-ups, enabling of ACK, and setting clock frequency)
[in] | speed | - I2C bus speed in KHz |
bool I2C_is_busy | ( | void | ) |
Get I2C busy status.
uint8_t I2C_rd8 | ( | const bool | ack | ) |
Receive byte from bus.
[in] | ack | - true if wait for ack |
I2C_STATUS I2C_read | ( | I2C_SLAVE * | slave, |
const uint16_t | reg_addr, | ||
uint8_t * | data, | ||
const uint16_t | bytes | ||
) |
This function reads data from the address specified and stores this data in the area provided by the pointer.
[in,out] | slave | - pointer to the I2C slave structure |
[in] | reg_addr | - register address in register map |
[in,out] | data | - pointer to the first byte of a block of data to read |
[in] | bytes | - indicates how many bytes of data to read |
void I2C_reset | ( | void | ) |
I2C bus reset (Release SCL and SDA lines and re-enable module)
uint8_t I2C_set_retries | ( | const uint8_t | retries | ) |
Change I2C message retries (in case of failure)
[in] | retries | - I2C number of retries (max of 8) |
uint16_t I2C_set_speed | ( | const uint16_t | speed | ) |
Change I2C frequency.
[in] | speed | - I2C speed in KHz (max 400KHz on avr) |
uint16_t I2C_set_timeout | ( | const uint16_t | timeout | ) |
Change I2C ack timeout.
[in] | timeout | - I2C ack timeout (500 ms max) |
void I2C_slave_init | ( | I2C_SLAVE * | slave, |
const uint8_t | sl_addr, | ||
const I2C_INT_SIZE | reg_sz | ||
) |
Init an I2C slave structure for cMI2C communication.
[in] | slave | - pointer to the I2C slave structure to init |
[in] | sl_addr | - I2C slave address |
[in] | reg_sz | - internal register map size |
bool I2C_slave_set_addr | ( | I2C_SLAVE * | slave, |
const uint8_t | sl_addr | ||
) |
Change I2C slave address.
[in,out] | slave | - pointer to the I2C slave structure to init |
[in] | sl_addr | - I2C slave address |
bool I2C_slave_set_reg_size | ( | I2C_SLAVE * | slave, |
const I2C_INT_SIZE | reg_sz | ||
) |
Change I2C registers map size (for access)
[in,out] | slave | - pointer to the I2C slave structure |
[in] | reg_sz | - internal register map size |
void I2C_slave_set_rw_func | ( | I2C_SLAVE * | slave, |
const ci2c_fct_ptr | func, | ||
const I2C_RW | rw | ||
) |
Redirect slave I2C read/write function (if needed for advanced use)
[in] | slave | - pointer to the I2C slave structure to init |
[in] | func | - pointer to read/write function to affect |
[in] | rw | - 0 = write function, 1 = read function |
Send I2C address.
[in] | slave | - pointer to the I2C slave structure |
[in] | rw | - read/write transaction |
bool I2C_start | ( | void | ) |
Send start condition.
bool I2C_stop | ( | void | ) |
Send stop condition.
void I2C_uninit | ( | ) |
Disable I2c module on arduino board (releasing pull-ups, and TWI control)
bool I2C_wr8 | ( | const uint8_t | dat | ) |
Send byte on bus.
[in] | dat | - data to be sent |
I2C_STATUS I2C_write | ( | I2C_SLAVE * | slave, |
const uint16_t | reg_addr, | ||
uint8_t * | data, | ||
const uint16_t | bytes | ||
) |
This function writes the provided data to the address specified.
[in,out] | slave | - pointer to the I2C slave structure |
[in] | reg_addr | - register address in register map |
[in] | data | - pointer to the first byte of a block of data to write |
[in] | bytes | - indicates how many bytes of data to write |
bool busy |
true if already busy (in case of interrupts implementation)
struct { ... } cfg |
uint8_t retries |
i2c message retries when fail
uint16_t start_wait |
time start waiting for acknowledge
uint16_t timeout |
i2c timeout (ms)