1 /**
2  * @file i2c.h
3  * @copyright Copyright (C) 2015-2018 Alibaba Group Holding Limited
4  */
5 
6 #ifndef HAL_I2C_H
7 #define HAL_I2C_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /** @addtogroup hal_i2c I2C
14  *  i2c hal API.
15  *
16  *  @{
17  */
18 
19 #include <stdint.h>
20 
21 /* Define the wait forever timeout macro */
22 #define HAL_WAIT_FOREVER 0xFFFFFFFFU
23 
24 #define I2C_MODE_MASTER 1 /**< i2c communication is master mode */
25 #define I2C_MODE_SLAVE  2 /**< i2c communication is slave mode */
26 
27 #define I2C_MEM_ADDR_SIZE_8BIT  1 /**< i2c memory address size 8bit */
28 #define I2C_MEM_ADDR_SIZE_16BIT 2 /**< i2c memory address size 16bit */
29 
30 /*
31  * Specifies one of the standard I2C bus bit rates for I2C communication
32  */
33 #define I2C_BUS_BIT_RATES_100K  100000
34 #define I2C_BUS_BIT_RATES_400K  400000
35 #define I2C_BUS_BIT_RATES_3400K 3400000
36 
37 /* Addressing mode */
38 #define I2C_HAL_ADDRESS_WIDTH_7BIT  0   /**< 7 bit mode */
39 #define I2C_HAL_ADDRESS_WIDTH_10BIT 1   /**< 10 bit mode */
40 
41 /* This struct define i2c config args */
42 typedef struct {
43     uint32_t address_width; /**< Addressing mode: 7 bit or 10 bit */
44     uint32_t freq;          /**< CLK freq */
45     uint8_t  mode;          /**< master or slave mode */
46     uint16_t dev_addr;      /**< slave device addr */
47 } i2c_config_t;
48 
49 /* This struct define i2c main handle */
50 typedef struct {
51     uint8_t       port;   /**< i2c port */
52     i2c_config_t  config; /**< i2c config */
53     void         *priv;   /**< priv data */
54 } i2c_dev_t;
55 
56 /**
57  * Initialises an I2C interface
58  * Prepares an I2C hardware interface for communication as a master or slave
59  *
60  * @param[in]  i2c  the device for which the i2c port should be initialised
61  *
62  * @return  0 : on success,  otherwise is error
63  */
64 int32_t hal_i2c_init(i2c_dev_t *i2c);
65 
66 /**
67  * I2c master send
68  *
69  * @param[in]  i2c       the i2c device
70  * @param[in]  dev_addr  device address
71  * @param[in]  data      i2c send data
72  * @param[in]  size      i2c send data size
73  * @param[in]  timeout   timeout in milisecond, set this value to HAL_WAIT_FOREVER
74  *                       if you want to wait forever
75  *
76  * @return  0 : on success,  otherwise is error
77  */
78 int32_t hal_i2c_master_send(i2c_dev_t *i2c, uint16_t dev_addr, const uint8_t *data,
79                             uint16_t size, uint32_t timeout);
80 
81 /**
82  * I2c master recv
83  *
84  * @param[in]   i2c       the i2c device
85  * @param[in]   dev_addr  device address
86  * @param[out]  data      i2c receive data
87  * @param[in]   size      i2c receive data size
88  * @param[in]   timeout   timeout in milisecond, set this value to HAL_WAIT_FOREVER
89  *                        if you want to wait forever
90  *
91  * @return  0 : on success,  otherwise is error
92  */
93 int32_t hal_i2c_master_recv(i2c_dev_t *i2c, uint16_t dev_addr, uint8_t *data,
94                             uint16_t size, uint32_t timeout);
95 
96 /**
97  * I2c slave send
98  *
99  * @param[in]  i2c      the i2c device
100  * @param[in]  data     i2c slave send data
101  * @param[in]  size     i2c slave send data size
102  * @param[in]  timeout  timeout in milisecond, set this value to HAL_WAIT_FOREVER
103  *                      if you want to wait forever
104  *
105  * @return  0 : on success,  otherwise is error
106  */
107 int32_t hal_i2c_slave_send(i2c_dev_t *i2c, const uint8_t *data, uint16_t size, uint32_t timeout);
108 
109 /**
110  * I2c slave receive
111  *
112  * @param[in]   i2c      tthe i2c device
113  * @param[out]  data     i2c slave receive data
114  * @param[in]   size     i2c slave receive data size
115  * @param[in]  timeout   timeout in milisecond, set this value to HAL_WAIT_FOREVER
116  *                       if you want to wait forever
117  *
118  * @return  0 : on success,  otherwise is error
119  */
120 int32_t hal_i2c_slave_recv(i2c_dev_t *i2c, uint8_t *data, uint16_t size, uint32_t timeout);
121 
122 /**
123  * I2c mem write
124  *
125  * @param[in]  i2c            the i2c device
126  * @param[in]  dev_addr       device address
127  * @param[in]  mem_addr       mem address
128  * @param[in]  mem_addr_size  mem address
129  * @param[in]  data           i2c master send data
130  * @param[in]  size           i2c master send data size
131  * @param[in]  timeout        timeout in milisecond, set this value to HAL_WAIT_FOREVER
132  *                            if you want to wait forever
133  *
134  * @return  0 : on success,  otherwise is error
135  */
136 int32_t hal_i2c_mem_write(i2c_dev_t *i2c, uint16_t dev_addr, uint16_t mem_addr,
137                           uint16_t mem_addr_size, const uint8_t *data, uint16_t size,
138                           uint32_t timeout);
139 
140 /**
141  * I2c master mem read
142  *
143  * @param[in]   i2c            the i2c device
144  * @param[in]   dev_addr       device address
145  * @param[in]   mem_addr       mem address
146  * @param[in]   mem_addr_size  mem address
147  * @param[out]  data           i2c master send data
148  * @param[in]   size           i2c master send data size
149  * @param[in]  timeout         timeout in milisecond, set this value to HAL_WAIT_FOREVER
150  *                             if you want to wait forever
151  *
152  * @return  0 : on success,  otherwise is error
153  */
154 int32_t hal_i2c_mem_read(i2c_dev_t *i2c, uint16_t dev_addr, uint16_t mem_addr,
155                          uint16_t mem_addr_size, uint8_t *data, uint16_t size,
156                          uint32_t timeout);
157 
158 /**
159  * Deinitialises an I2C device
160  *
161  * @param[in]  i2c  the i2c device
162  *
163  * @return  0 : on success,  otherwise is error
164  */
165 int32_t hal_i2c_finalize(i2c_dev_t *i2c);
166 
167 /** @} */
168 
169 #ifdef __cplusplus
170 }
171 #endif
172 
173 #endif /* HAL_I2C_H */
174 
175