Arduino Hardware I2C for AVR MCUs (plain c)  1.3
Arduino Hardware I2C for AVR MCUs (plain c) documentation
ci2c.h
Go to the documentation of this file.
1 
7 /****************************************************************/
8 #ifndef __CI2C_H__
9  #define __CI2C_H__
10 /****************************************************************/
11 
12 #if defined(DOXY)
13  // Define gcc __attribute__ as void when Doxygen runs
14  #define __attribute__(a)
15 #endif
16 
17 #if (ARDUINO >= 100)
18  #include <Arduino.h>
19 #else
20  #include <WProgram.h>
21 #endif
22 
23 #include <inttypes.h>
24 #include <stdbool.h>
25 
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #define DEF_CI2C_NB_RETRIES 3
32 #define DEF_CI2C_TIMEOUT 100
33 
34 
39 typedef enum __attribute__((__packed__)) enI2C_RW {
40  I2C_WRITE = 0,
42 } I2C_RW;
43 
44 
49 typedef enum __attribute__((__packed__)) enI2C_SPEED {
50  I2C_STD = 100,
51  I2C_FM = 400,
52  I2C_FMP = 1000,
53  I2C_HS = 3400
54 } I2C_SPEED;
55 
56 
61 typedef enum __attribute__((__packed__)) enI2C_STATUS {
62  I2C_OK = 0x00,
65 } I2C_STATUS;
66 
71 typedef enum __attribute__((__packed__)) enI2C_INT_SIZE {
72  I2C_NO_REG = 0x00,
75 } I2C_INT_SIZE;
76 
77 
78 typedef bool (*ci2c_fct_ptr) (void*, const uint16_t, uint8_t*, const uint16_t);
79 
80 
85 typedef struct __attribute__((__packed__)) StructI2CSlave {
89  struct {
90  uint8_t addr;
91  I2C_INT_SIZE reg_size;
94  } cfg;
95  uint16_t reg_addr;
96  I2C_STATUS status;
97 } I2C_SLAVE;
98 
99 
100 /***************************/
101 /*** I2C SLAVE FUNCTIONS ***/
102 /***************************/
103 
110 void I2C_slave_init(I2C_SLAVE * slave, const uint8_t sl_addr, const I2C_INT_SIZE reg_sz);
111 
118 void I2C_slave_set_rw_func(I2C_SLAVE * slave, const ci2c_fct_ptr func, const I2C_RW rw);
119 
125 bool I2C_slave_set_addr(I2C_SLAVE * slave, const uint8_t sl_addr);
126 
132 bool I2C_slave_set_reg_size(I2C_SLAVE * slave, const I2C_INT_SIZE reg_sz);
133 
139 inline uint8_t __attribute__((__always_inline__)) I2C_slave_get_addr(const I2C_SLAVE * slave) {
140  return slave->cfg.addr; }
141 
147 inline bool __attribute__((__always_inline__)) I2C_slave_get_reg_size(const I2C_SLAVE * slave) {
148  return slave->cfg.reg_size; }
149 
155 inline uint16_t __attribute__((__always_inline__)) I2C_slave_get_reg_addr(const I2C_SLAVE * slave) {
156  return slave->reg_addr; }
157 
158 
159 /*************************/
160 /*** I2C BUS FUNCTIONS ***/
161 /*************************/
162 
168 void I2C_init(const uint16_t speed);
169 
173 void I2C_uninit();
174 
179 uint16_t I2C_set_speed(const uint16_t speed);
180 
185 uint16_t I2C_set_timeout(const uint16_t timeout);
186 
191 uint8_t I2C_set_retries(const uint8_t retries);
192 
196 bool I2C_is_busy(void);
197 
205 I2C_STATUS I2C_write(I2C_SLAVE * slave, const uint16_t reg_addr, uint8_t * data, const uint16_t bytes);
206 
214 inline I2C_STATUS __attribute__((__always_inline__)) I2C_write_next(I2C_SLAVE * slave, uint8_t * data, const uint16_t bytes) {
215  return I2C_write(slave, slave->reg_addr, data, bytes); }
216 
225 I2C_STATUS I2C_read(I2C_SLAVE * slave, const uint16_t reg_addr, uint8_t * data, const uint16_t bytes);
226 
234 inline I2C_STATUS __attribute__((__always_inline__)) I2C_read_next(I2C_SLAVE * slave, uint8_t * data, const uint16_t bytes) {
235  return I2C_read(slave, slave->reg_addr, data, bytes); }
236 
237 
238 /***********************************/
239 /*** cI2C LOW LEVEL FUNCTIONS ***/
240 /*** THAT MAY BE USEFUL FOR DVPT ***/
241 /***********************************/
245 void I2C_reset(void);
246 
250 bool I2C_start(void);
251 
255 bool I2C_stop(void);
256 
261 bool I2C_wr8(const uint8_t dat);
262 
267 uint8_t I2C_rd8(const bool ack);
268 
274 bool I2C_sndAddr(I2C_SLAVE * slave, const I2C_RW rw);
275 
276 
277 #ifdef __cplusplus
278 }
279 #endif
280 
281 #endif
struct @0::@1 cfg
#define __attribute__(a)
GCC attribute (ignored by Doxygen)
Definition: ci2c.h:14
enum enI2C_SPEED I2C_SPEED
bool I2C_slave_get_reg_size(const I2C_SLAVE *slave)
Get I2C register map size (for access)
Definition: ci2c.h:147
I2C OK.
Definition: ci2c.h:62
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.
Definition: ci2c.c:61
struct StructI2CSlave I2C_SLAVE
uint8_t I2C_slave_get_addr(const I2C_SLAVE *slave)
Get I2C slave address.
Definition: ci2c.h:139
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) ...
Definition: ci2c.h:155
uint8_t addr
Slave address.
Definition: ci2c.h:90
I2C Bus busy.
Definition: ci2c.h:63
I2C rw bit (write)
Definition: ci2c.h:40
uint16_t reg_addr
Internal current register address.
Definition: ci2c.h:95
ci2c_fct_ptr rd
Slave read function pointer.
Definition: ci2c.h:93
I2C Standard (100KHz)
Definition: ci2c.h:50
bool I2C_slave_set_reg_size(I2C_SLAVE *slave, const I2C_INT_SIZE reg_sz)
Change I2C registers map size (for access)
Definition: ci2c.c:100
Slave internal address registers space is 8bits wide.
Definition: ci2c.h:73
struct StructI2CSlave::@2 cfg
uint16_t timeout
i2c timeout (ms)
Definition: ci2c.c:43
bool I2C_start(void)
Send start condition.
Definition: ci2c.c:283
I2C rw bit (read)
Definition: ci2c.h:41
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 ...
Definition: ci2c.c:262
enI2C_STATUS
I2C slave status.
Definition: ci2c.h:61
enI2C_SPEED
I2C bus speed.
Definition: ci2c.h:49
enum enI2C_STATUS I2C_STATUS
uint8_t I2C_rd8(const bool ack)
Receive byte from bus.
Definition: ci2c.c:340
uint16_t I2C_set_speed(const uint16_t speed)
Change I2C frequency.
Definition: ci2c.c:168
uint8_t I2C_set_retries(const uint8_t retries)
Change I2C message retries (in case of failure)
Definition: ci2c.c:199
Internal address registers not applicable for slave.
Definition: ci2c.h:72
void I2C_init(const uint16_t speed)
Enable I2c module on arduino board (including pull-ups, enabling of ACK, and setting clock frequency)...
Definition: ci2c.c:122
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.
Definition: ci2c.h:234
uint16_t I2C_set_timeout(const uint16_t timeout)
Change I2C ack timeout.
Definition: ci2c.c:188
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)
Definition: ci2c.c:77
bool(* ci2c_fct_ptr)(void *, const uint16_t, uint8_t *, const uint16_t)
i2c read/write function pointer typedef
Definition: ci2c.h:78
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.
Definition: ci2c.h:214
bool I2C_sndAddr(I2C_SLAVE *slave, const I2C_RW rw)
Send I2C address.
Definition: ci2c.c:360
void I2C_uninit()
Disable I2c module on arduino board (releasing pull-ups, and TWI control)
Definition: ci2c.c:139
I2C Not Acknowledge.
Definition: ci2c.h:64
enum enI2C_RW I2C_RW
ci2c slave config and control parameters
Definition: ci2c.h:85
I2C_STATUS status
Status of the last communications.
Definition: ci2c.h:96
I2C_INT_SIZE reg_size
Slave internal registers size.
Definition: ci2c.h:91
bool I2C_stop(void)
Send stop condition.
Definition: ci2c.c:301
ci2c_fct_ptr wr
Slave write function pointer.
Definition: ci2c.h:92
enum enI2C_INT_SIZE I2C_INT_SIZE
I2C High Speed (3.4MHz): will set speed to Fast Mode (up to 400KHz on avr)
Definition: ci2c.h:53
void I2C_reset(void)
I2C bus reset (Release SCL and SDA lines and re-enable module)
Definition: ci2c.c:157
I2C Fast mode + (1MHz): will set speed to Fast Mode (up to 400KHz on avr)
Definition: ci2c.h:52
I2C Fast Mode (400KHz)
Definition: ci2c.h:51
bool I2C_is_busy(void)
Get I2C busy status.
Definition: ci2c.c:209
I2C_SPEED speed
i2c bus speed
Definition: ci2c.c:41
bool I2C_wr8(const uint8_t dat)
Send byte on bus.
Definition: ci2c.c:317
Slave internal address registers space is 16bits wide.
Definition: ci2c.h:74
enI2C_RW
I2C RW bit enumeration.
Definition: ci2c.h:39
enI2C_INT_SIZE
I2C slave internal address registers size.
Definition: ci2c.h:71
uint8_t retries
i2c message retries when fail
Definition: ci2c.c:42
bool I2C_slave_set_addr(I2C_SLAVE *slave, const uint8_t sl_addr)
Change I2C slave address.
Definition: ci2c.c:88
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.
Definition: ci2c.c:251