Arduino Hardware I2C for AVR MCUs (plain c)  1.3
Arduino Hardware I2C for AVR MCUs (plain c) documentation
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Data Structures | Macros | Typedefs | Enumerations | Functions
ci2c.h File Reference

arduino i2c in plain c declarations More...

#include <Arduino.h>
#include <inttypes.h>
#include <stdbool.h>
Include dependency graph for ci2c.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  StructI2CSlave
 ci2c slave config and control parameters More...
 

Macros

#define __attribute__(a)
 GCC attribute (ignored by Doxygen) More...
 
#define DEF_CI2C_NB_RETRIES   3
 Default cI2C transaction retries. More...
 
#define DEF_CI2C_TIMEOUT   100
 Default cI2C timeout. More...
 

Typedefs

typedef enum enI2C_RW I2C_RW
 
typedef enum enI2C_SPEED I2C_SPEED
 
typedef enum enI2C_STATUS I2C_STATUS
 
typedef enum enI2C_INT_SIZE I2C_INT_SIZE
 
typedef bool(* ci2c_fct_ptr) (void *, const uint16_t, uint8_t *, const uint16_t)
 i2c read/write function pointer typedef More...
 
typedef struct StructI2CSlave I2C_SLAVE
 

Enumerations

enum  enI2C_RW { I2C_WRITE = 0, I2C_READ }
 I2C RW bit enumeration. More...
 
enum  enI2C_SPEED { I2C_STD = 100, I2C_FM = 400, I2C_FMP = 1000, I2C_HS = 3400 }
 I2C bus speed. More...
 
enum  enI2C_STATUS { I2C_OK = 0x00, I2C_BUSY, I2C_NACK }
 I2C slave status. More...
 
enum  enI2C_INT_SIZE { I2C_NO_REG = 0x00, I2C_8B_REG, I2C_16B_REG }
 I2C slave internal address registers size. 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...
 
uint8_t I2C_slave_get_addr (const I2C_SLAVE *slave)
 Get I2C slave address. More...
 
bool I2C_slave_get_reg_size (const I2C_SLAVE *slave)
 Get I2C register map size (for access) More...
 
uint16_t I2C_slave_get_reg_addr (const I2C_SLAVE *slave)
 Get I2C current register address (addr may passed this way in procedures if contigous accesses) 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...
 
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_write_next (I2C_SLAVE *slave, uint8_t *data, const uint16_t bytes)
 This inline is a wrapper to I2C_write in case of contigous operations. 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...
 
I2C_STATUS I2C_read_next (I2C_SLAVE *slave, uint8_t *data, const uint16_t bytes)
 This inline is a wrapper to I2C_read in case of contigous operations. More...
 
void I2C_reset (void)
 I2C bus reset (Release SCL and SDA lines and re-enable module) 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...
 

Detailed Description

arduino i2c in plain c declarations

Author
SMFSW
Warning
Don't access (r/w) last 16b internal address byte alone right after init, this would lead to hazardous result (in such case, make a dummy read of addr 0 before)

Macro Definition Documentation

◆ __attribute__

#define __attribute__ (   a)

GCC attribute (ignored by Doxygen)

◆ DEF_CI2C_NB_RETRIES

#define DEF_CI2C_NB_RETRIES   3

Default cI2C transaction retries.

◆ DEF_CI2C_TIMEOUT

#define DEF_CI2C_TIMEOUT   100

Default cI2C timeout.

Typedef Documentation

◆ ci2c_fct_ptr

typedef bool(* ci2c_fct_ptr) (void *, const uint16_t, uint8_t *, const uint16_t)

i2c read/write function pointer typedef

◆ I2C_INT_SIZE

◆ I2C_RW

typedef enum enI2C_RW I2C_RW

◆ I2C_SLAVE

typedef struct StructI2CSlave I2C_SLAVE

◆ I2C_SPEED

typedef enum enI2C_SPEED I2C_SPEED

◆ I2C_STATUS

typedef enum enI2C_STATUS I2C_STATUS

Enumeration Type Documentation

◆ enI2C_INT_SIZE

I2C slave internal address registers size.

GCC Attributes:
packed enum
Enumerator
I2C_NO_REG 

Internal address registers not applicable for slave.

I2C_8B_REG 

Slave internal address registers space is 8bits wide.

I2C_16B_REG 

Slave internal address registers space is 16bits wide.

◆ enI2C_RW

enum enI2C_RW

I2C RW bit enumeration.

GCC Attributes:
packed enum
Enumerator
I2C_WRITE 

I2C rw bit (write)

I2C_READ 

I2C rw bit (read)

◆ enI2C_SPEED

I2C bus speed.

GCC Attributes:
packed enum
Enumerator
I2C_STD 

I2C Standard (100KHz)

I2C_FM 

I2C Fast Mode (400KHz)

I2C_FMP 

I2C Fast mode + (1MHz): will set speed to Fast Mode (up to 400KHz on avr)

I2C_HS 

I2C High Speed (3.4MHz): will set speed to Fast Mode (up to 400KHz on avr)

◆ enI2C_STATUS

I2C slave status.

GCC Attributes:
packed enum
Enumerator
I2C_OK 

I2C OK.

I2C_BUSY 

I2C Bus busy.

I2C_NACK 

I2C Not Acknowledge.

Function Documentation

◆ I2C_init()

void I2C_init ( const uint16_t  speed)

Enable I2c module on arduino board (including pull-ups, enabling of ACK, and setting clock frequency)

Parameters
[in]speed- I2C bus speed in KHz
Returns
nothing
Here is the call graph for this function:
Here is the caller graph for this function:

◆ I2C_is_busy()

bool I2C_is_busy ( void  )

Get I2C busy status.

Returns
true if busy
Here is the caller graph for this function:

◆ I2C_rd8()

uint8_t I2C_rd8 ( const bool  ack)

Receive byte from bus.

Parameters
[in]ack- true if wait for ack
Returns
true if data reception acknowledged (false otherwise)
Here is the caller graph for this function:

◆ I2C_read()

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.

Parameters
[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
Returns
I2C_STATUS status of read attempt
Here is the caller graph for this function:

◆ I2C_read_next()

I2C_STATUS I2C_read_next ( I2C_SLAVE slave,
uint8_t *  data,
const uint16_t  bytes 
)
inline

This inline is a wrapper to I2C_read in case of contigous operations.

GCC Attributes:
inline
Parameters
[in,out]slave- pointer to the I2C slave structure
[in]data- pointer to the first byte of a block of data to read
[in]bytes- indicates how many bytes of data to read
Returns
I2C_STATUS status of read attempt
Here is the call graph for this function:
Here is the caller graph for this function:

◆ I2C_reset()

void I2C_reset ( void  )

I2C bus reset (Release SCL and SDA lines and re-enable module)

Returns
nothing
Here is the caller graph for this function:

◆ I2C_set_retries()

uint8_t I2C_set_retries ( const uint8_t  retries)

Change I2C message retries (in case of failure)

Parameters
[in]retries- I2C number of retries (max of 8)
Returns
Configured number of retries
Here is the caller graph for this function:

◆ I2C_set_speed()

uint16_t I2C_set_speed ( const uint16_t  speed)

Change I2C frequency.

Parameters
[in]speed- I2C bus speed in KHz (max 400KHz on AVR)
Returns
Configured bus speed
Parameters
[in]speed- I2C speed in KHz (max 400KHz on avr)
Returns
Configured bus speed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ I2C_set_timeout()

uint16_t I2C_set_timeout ( const uint16_t  timeout)

Change I2C ack timeout.

Parameters
[in]timeout- I2C ack timeout (500 ms max)
Returns
Configured timeout
Here is the caller graph for this function:

◆ I2C_slave_get_addr()

uint8_t I2C_slave_get_addr ( const I2C_SLAVE slave)
inline

Get I2C slave address.

GCC Attributes:
inline
Parameters
[in]slave- pointer to the I2C slave structure
Returns
I2C slave address

◆ I2C_slave_get_reg_addr()

uint16_t I2C_slave_get_reg_addr ( const I2C_SLAVE slave)
inline

Get I2C current register address (addr may passed this way in procedures if contigous accesses)

GCC Attributes:
inline
Parameters
[in]slave- pointer to the I2C slave structure
Returns
current register map address
Here is the call graph for this function:

◆ I2C_slave_get_reg_size()

bool I2C_slave_get_reg_size ( const I2C_SLAVE slave)
inline

Get I2C register map size (for access)

GCC Attributes:
inline
Parameters
[in]slave- pointer to the I2C slave structure
Returns
register map using 16bits if true (1Byte otherwise)

◆ I2C_slave_init()

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.

Parameters
[in]slave- pointer to the I2C slave structure to init
[in]sl_addr- I2C slave address
[in]reg_sz- internal register map size
Returns
nothing
Here is the call graph for this function:
Here is the caller graph for this function:

◆ I2C_slave_set_addr()

bool I2C_slave_set_addr ( I2C_SLAVE slave,
const uint8_t  sl_addr 
)

Change I2C slave address.

Parameters
[in,out]slave- pointer to the I2C slave structure to init
[in]sl_addr- I2C slave address
Returns
true if new address set (false if address is >7Fh)
Here is the caller graph for this function:

◆ I2C_slave_set_reg_size()

bool I2C_slave_set_reg_size ( I2C_SLAVE slave,
const I2C_INT_SIZE  reg_sz 
)

Change I2C registers map size (for access)

Parameters
[in,out]slave- pointer to the I2C slave structure
[in]reg_sz- internal register map size
Returns
true if new size is correct (false otherwise and set to 16bit by default)
Here is the caller graph for this function:

◆ I2C_slave_set_rw_func()

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)

Parameters
[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
Returns
nothing
Here is the caller graph for this function:

◆ I2C_sndAddr()

bool I2C_sndAddr ( I2C_SLAVE slave,
const I2C_RW  rw 
)

Send I2C address.

Parameters
[in]slave- pointer to the I2C slave structure
[in]rw- read/write transaction
Returns
true if I2C chip address sent acknowledged (false otherwise)
Here is the caller graph for this function:

◆ I2C_start()

bool I2C_start ( void  )

Send start condition.

Returns
true if start condition acknowledged (false otherwise)
Here is the caller graph for this function:

◆ I2C_stop()

bool I2C_stop ( void  )

Send stop condition.

Returns
true if stop condition acknowledged (false otherwise)
Here is the caller graph for this function:

◆ I2C_uninit()

void I2C_uninit ( )

Disable I2c module on arduino board (releasing pull-ups, and TWI control)

Returns
nothing
Here is the caller graph for this function:

◆ I2C_wr8()

bool I2C_wr8 ( const uint8_t  dat)

Send byte on bus.

Parameters
[in]dat- data to be sent
Returns
true if data sent acknowledged (false otherwise)
Here is the caller graph for this function:

◆ I2C_write()

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.

Parameters
[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
Returns
I2C_STATUS status of write attempt
Here is the caller graph for this function:

◆ I2C_write_next()

I2C_STATUS I2C_write_next ( I2C_SLAVE slave,
uint8_t *  data,
const uint16_t  bytes 
)
inline

This inline is a wrapper to I2C_write in case of contigous operations.

GCC Attributes:
inline
Parameters
[in,out]slave- pointer to the I2C slave structure
[in]data- pointer to the first byte of a block of data to write
[in]bytes- indicates how many bytes of data to write
Returns
I2C_STATUS status of write attempt
Here is the call graph for this function: