Arduino Hardware I2C for AVR MCUs (plain c) 1.3
Arduino Hardware I2C for AVR MCUs (plain c) documentation
Loading...
Searching...
No Matches
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
28extern "C" {
29#endif
30
31#define DEF_CI2C_NB_RETRIES 3
32#define DEF_CI2C_TIMEOUT 100
33
34
39typedef enum __attribute__((__packed__)) enI2C_RW {
43
44
49typedef enum __attribute__((__packed__)) enI2C_SPEED {
50 I2C_STD = 100,
51 I2C_FM = 400,
52 I2C_FMP = 1000,
53 I2C_HS = 3400
55
56
66
76
77
78typedef bool (*ci2c_fct_ptr) (void*, const uint16_t, uint8_t*, const uint16_t);
79
80
98
99
100/***************************/
101/*** I2C SLAVE FUNCTIONS ***/
102/***************************/
103
110void I2C_slave_init(I2C_SLAVE * slave, const uint8_t sl_addr, const I2C_INT_SIZE reg_sz);
111
118void I2C_slave_set_rw_func(I2C_SLAVE * slave, const ci2c_fct_ptr func, const I2C_RW rw);
119
125bool I2C_slave_set_addr(I2C_SLAVE * slave, const uint8_t sl_addr);
126
132bool I2C_slave_set_reg_size(I2C_SLAVE * slave, const I2C_INT_SIZE reg_sz);
133
139inline uint8_t __attribute__((__always_inline__)) I2C_slave_get_addr(const I2C_SLAVE * slave) {
140 return slave->cfg.addr; }
141
147inline bool __attribute__((__always_inline__)) I2C_slave_get_reg_size(const I2C_SLAVE * slave) {
148 return slave->cfg.reg_size; }
149
155inline 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
168void I2C_init(const uint16_t speed);
169
173void I2C_uninit();
174
179uint16_t I2C_set_speed(const uint16_t speed);
180
185uint16_t I2C_set_timeout(const uint16_t timeout);
186
191uint8_t I2C_set_retries(const uint8_t retries);
192
196bool I2C_is_busy(void);
197
205I2C_STATUS I2C_write(I2C_SLAVE * slave, const uint16_t reg_addr, uint8_t * data, const uint16_t bytes);
206
214inline 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
225I2C_STATUS I2C_read(I2C_SLAVE * slave, const uint16_t reg_addr, uint8_t * data, const uint16_t bytes);
226
234inline 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/***********************************/
245void I2C_reset(void);
246
250bool I2C_start(void);
251
255bool I2C_stop(void);
256
261bool I2C_wr8(const uint8_t dat);
262
267uint8_t I2C_rd8(const bool ack);
268
274bool I2C_sndAddr(I2C_SLAVE * slave, const I2C_RW rw);
275
276
277#ifdef __cplusplus
278}
279#endif
280
281#endif
I2C_SPEED speed
i2c bus speed
Definition ci2c.c:41
uint8_t retries
i2c message retries when fail
Definition ci2c.c:42
uint16_t timeout
i2c timeout (ms)
Definition ci2c.c:43
enum enI2C_INT_SIZE I2C_INT_SIZE
uint8_t I2C_slave_get_addr(const I2C_SLAVE *slave)
Get I2C slave address.
Definition ci2c.h:139
enum enI2C_SPEED I2C_SPEED
void I2C_reset(void)
I2C bus reset (Release SCL and SDA lines and re-enable module)
Definition ci2c.c:157
struct StructI2CSlave I2C_SLAVE
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
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
bool I2C_stop(void)
Send stop condition.
Definition ci2c.c:301
enI2C_SPEED
I2C bus speed.
Definition ci2c.h:49
@ I2C_HS
I2C High Speed (3.4MHz): will set speed to Fast Mode (up to 400KHz on avr)
Definition ci2c.h:53
@ I2C_FMP
I2C Fast mode + (1MHz): will set speed to Fast Mode (up to 400KHz on avr)
Definition ci2c.h:52
@ I2C_STD
I2C Standard (100KHz)
Definition ci2c.h:50
@ I2C_FM
I2C Fast Mode (400KHz)
Definition ci2c.h:51
enI2C_STATUS
I2C slave status.
Definition ci2c.h:61
@ I2C_BUSY
I2C Bus busy.
Definition ci2c.h:63
@ I2C_NACK
I2C Not Acknowledge.
Definition ci2c.h:64
@ I2C_OK
I2C OK.
Definition ci2c.h:62
void I2C_uninit()
Disable I2c module on arduino board (releasing pull-ups, and TWI control)
Definition ci2c.c:139
uint16_t I2C_set_speed(const uint16_t speed)
Change I2C frequency.
Definition ci2c.c:168
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
uint8_t I2C_rd8(const bool ack)
Receive byte from bus.
Definition ci2c.c:340
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
#define __attribute__(a)
GCC attribute (ignored by Doxygen)
Definition ci2c.h:14
enI2C_RW
I2C RW bit enumeration.
Definition ci2c.h:39
@ I2C_READ
I2C rw bit (read)
Definition ci2c.h:41
@ I2C_WRITE
I2C rw bit (write)
Definition ci2c.h:40
enI2C_INT_SIZE
I2C slave internal address registers size.
Definition ci2c.h:71
@ I2C_NO_REG
Internal address registers not applicable for slave.
Definition ci2c.h:72
@ I2C_8B_REG
Slave internal address registers space is 8bits wide.
Definition ci2c.h:73
@ I2C_16B_REG
Slave internal address registers space is 16bits wide.
Definition ci2c.h:74
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
enum enI2C_RW I2C_RW
bool I2C_start(void)
Send start condition.
Definition ci2c.c:283
bool I2C_sndAddr(I2C_SLAVE *slave, const I2C_RW rw)
Send I2C address.
Definition ci2c.c:360
bool I2C_wr8(const uint8_t dat)
Send byte on bus.
Definition ci2c.c:317
bool I2C_is_busy(void)
Get I2C busy status.
Definition ci2c.c:209
bool I2C_slave_get_reg_size(const I2C_SLAVE *slave)
Get I2C register map size (for access)
Definition ci2c.h:147
uint16_t I2C_set_timeout(const uint16_t timeout)
Change I2C ack timeout.
Definition ci2c.c:188
bool I2C_slave_set_addr(I2C_SLAVE *slave, const uint8_t sl_addr)
Change I2C slave address.
Definition ci2c.c:88
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
uint8_t I2C_set_retries(const uint8_t retries)
Change I2C message retries (in case of failure)
Definition ci2c.c:199
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
enum enI2C_STATUS I2C_STATUS
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
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_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
ci2c slave config and control parameters
Definition ci2c.h:85
I2C_INT_SIZE reg_size
Slave internal registers size.
Definition ci2c.h:91
uint16_t reg_addr
Internal current register address.
Definition ci2c.h:95
ci2c_fct_ptr rd
Slave read function pointer.
Definition ci2c.h:93
ci2c_fct_ptr wr
Slave write function pointer.
Definition ci2c.h:92
uint8_t addr
Slave address.
Definition ci2c.h:90
I2C_STATUS status
Status of the last communications.
Definition ci2c.h:96
struct StructI2CSlave::@044243043304327257271367302002014306363224227265 cfg
ci2c bus parameters