• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <stdbool.h>
18 #include <driver/i2c_types.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /**
25  * @brief     Init the I2C driver
26  *
27  * This API init the resoure common:
28  *   - Init I2C driver control memory
29  *
30  * @attention 1. This API should be called before any other I2C APIs.
31  *
32  * @return
33  *    - BK_OK: succeed
34  *    - others: other errors.
35  */
36 bk_err_t bk_i2c_driver_init(void);
37 
38 /**
39  * @brief     Deinit the I2C driver
40  *
41  * This API free all resource related to I2C and disable I2C.
42  *
43  * @return
44  *    - BK_OK: succeed
45  *    - others: other errors.
46  */
47 bk_err_t bk_i2c_driver_deinit(void);
48 
49 /**
50  * @brief     Init the I2C id
51  *
52  * This API init the I2C id:
53  *  - Power up the I2C id
54  *  - Configure the I2C id clock
55  *  - Map the I2C id to dedicated GPIO port
56  *  - Set the I2C parameters
57  *  - Start the I2C id
58  *
59  * @param id I2C id
60  * @param config I2C parameter settings
61 
62  * @attention Multifunction GPIO initialization affects other functions
63 
64  * @return
65  *    - BK_OK: succeed
66  *    - BK_ERR_NULL_PARAM: I2C config paramter is NULL
67  *    - BK_ERR_I2C_NOT_INIT: I2C driver not init
68  *    - others: other errors.
69  */
70 bk_err_t bk_i2c_init(i2c_id_t id, const i2c_config_t *cfg);
71 
72 /**
73  * @brief     Deinit the I2C id
74  *
75  * This API deinit the I2C id:
76  *   - Stop the I2C id
77  *   - Disable the I2C id interrupt
78  *   - Power down the I2C id
79  *
80  * @param id I2C id
81  *
82  * @return
83  *    - BK_OK: succeed
84  *    - others: other errors.
85  */
86 bk_err_t bk_i2c_deinit(i2c_id_t id);
87 
88 /**
89  * @brief     Write data to the I2C port from a given buffer and length,
90  *            It shall only be called in I2C master mode.
91  *
92  * @param id I2C id
93  * @param dev_addr slave device address
94  * @param data pointer to the buffer
95  * @param size data length to write
96  * @param timeout_ms timeout ms
97  *
98  * @return
99  *    - BK_OK: succeed
100  *    - BK_ERR_I2C_NOT_INIT: I2C driver not init
101  *    - BK_ERR_I2C_INVALID_ID: I2C id number is invalid
102  *    - BK_ERR_I2C_ID_NOT_INIT: I2C id not init
103  *    - others: other errors.
104  */
105 bk_err_t bk_i2c_master_write(i2c_id_t id, uint32_t dev_addr, const uint8_t *data, uint32_t size, uint32_t timeout_ms);
106 
107 /**
108  * @brief     I2C read data from I2C buffer,
109  *            It shall only be called in I2C master mode.
110  *
111  * @param id I2C id
112  * @param dev_addr slave device address
113  * @param data pointer to the buffer
114  * @param size data length to read
115  * @param timeout_ms timeout ms
116  *
117  * @return
118  *    - BK_OK: succeed
119  *    - BK_ERR_I2C_NOT_INIT: I2C driver not init
120  *    - BK_ERR_I2C_INVALID_ID: I2C id number is invalid
121  *    - BK_ERR_I2C_ID_NOT_INIT: I2C id not init
122  *    - others: other errors.
123  */
124 bk_err_t bk_i2c_master_read(i2c_id_t id, uint32_t dev_addr, uint8_t *data, uint32_t size, uint32_t timeout_ms);
125 
126 /**
127  * @brief     Write data to the I2C port from a given buffer and length,
128  *            It shall only be called in I2C slave mode.
129  *
130  * @param id I2C id
131  * @param data pointer to the buffer
132  * @param size data length to write
133  * @param timeout_ms timeout ms
134  *
135  * @return
136  *    - BK_OK: succeed
137  *    - BK_ERR_I2C_NOT_INIT: I2C driver not init
138  *    - BK_ERR_I2C_INVALID_ID: I2C id number is invalid
139  *    - BK_ERR_I2C_ID_NOT_INIT: I2C id not init
140  *    - others: other errors.
141  */
142 bk_err_t bk_i2c_slave_write(i2c_id_t id, const uint8_t *data, uint32_t size, uint32_t timeout_ms);
143 
144 /**
145  * @brief     I2C read data from I2C buffer,
146  *            It shall only be called in I2C slave mode.
147  *
148  * @param id I2C id
149  * @param dev_addr slave device address
150  * @param data pointer to the buffer
151  * @param size data length to read
152  * @param timeout_ms timeout ms
153  *
154  * @return
155  *    - BK_OK: succeed
156  *    - BK_ERR_I2C_NOT_INIT: I2C driver not init
157  *    - BK_ERR_I2C_INVALID_ID: I2C id number is invalid
158  *    - BK_ERR_I2C_ID_NOT_INIT: I2C id not init
159  *    - others: other errors.
160  */
161 bk_err_t bk_i2c_slave_read(i2c_id_t id, uint8_t *data, uint32_t size, uint32_t timeout_ms);
162 
163 /**
164  * @brief     Write data to the specific memory address from a given buffer and length,
165  *            It shall only be called in I2C master mode.
166  *
167  * @param id I2C id
168  * @param mem_param memory parameter
169  *
170  * @return
171  *    - BK_OK: succeed
172  *    - BK_ERR_NULL_PARAM: I2C mem_param is NULL
173  *    - BK_ERR_I2C_NOT_INIT: I2C driver not init
174  *    - BK_ERR_I2C_INVALID_ID: I2C id number is invalid
175  *    - BK_ERR_I2C_ID_NOT_INIT: I2C id not init
176  *    - others: other errors.
177  */
178 bk_err_t bk_i2c_memory_write(i2c_id_t id, const i2c_mem_param_t *mem_param);
179 
180 /**
181  * @brief     I2C read data from I2C specific memory address,
182  *            It shall only be called in I2C master mode.
183  *
184  * @param id I2C id
185  * @param mem_param memory parameter
186  *
187  * @return
188  *    - BK_OK: succeed
189  *    - BK_ERR_NULL_PARAM: I2C mem_param is NULL
190  *    - BK_ERR_I2C_NOT_INIT: I2C driver not init
191  *    - BK_ERR_I2C_INVALID_ID: I2C id number is invalid
192  *    - BK_ERR_I2C_ID_NOT_INIT: I2C id not init
193  *    - others: other errors.
194  */
195 bk_err_t bk_i2c_memory_read(i2c_id_t id, const i2c_mem_param_t *mem_param);
196 
197 /**
198  * @brief     I2C set baud rate
199  *
200  * @param id I2C id
201  * @param baud_rate baud rate
202  *
203  * @return
204  *    - BK_OK: succeed
205  *    - BK_ERR_I2C_NOT_INIT: I2C driver not init
206  *    - BK_ERR_I2C_INVALID_ID: I2C id number is invalid
207  *    - BK_ERR_I2C_ID_NOT_INIT: I2C id not init
208  *    - others: other errors.
209  */
210 bk_err_t bk_i2c_set_baud_rate(i2c_id_t id, uint32_t baud_rate);
211 
212 /**
213  * @brief     Set slave address when current role is i2c slave
214  *
215  * @param id I2C id
216  * @param slave_addr slave address
217  *
218  * @return
219  *    - BK_OK: succeed
220  *    - BK_ERR_I2C_NOT_INIT: I2C driver not init
221  *    - others: other errors.
222  */
223 bk_err_t bk_i2c_set_slave_address(i2c_id_t id, uint16_t slave_addr);
224 
225 /**
226  * @brief    Enable I2C interrupt
227  *
228  * @param id I2C id
229  *
230  * @return
231  *    - BK_OK: succeed
232  *    - others: other errors.
233  */
234 bk_err_t bk_i2c_enable_interrupt(i2c_id_t id);
235 
236 /**
237  * @brief    Disable I2C interrupt
238  *
239  * @param id I2C id
240  *
241  * @return
242  *    - BK_OK: succeed
243  *    - others: other errors.
244  */
245 bk_err_t bk_i2c_disable_interrupt(i2c_id_t id);
246 
247 /**
248  * @brief     Check if I2C is busy
249  *
250  * @param id I2C id
251  *
252  * @return true: busy, false: not busy
253  */
254 bool bk_i2c_is_bus_busy(i2c_id_t id);
255 
256 /**
257  * @brief     Get i2c current action, such as start,stop,send write_addr
258  *
259  * @param id I2C id
260  *
261  * @return i2c action
262  */
263 uint32_t bk_i2c_get_cur_action(i2c_id_t id);
264 
265 /**
266  * @brief     bk_i2c_timer_callback
267  *
268  * This API set timer call back
269  *
270  * @return
271 */
272 void bk_i2c_timer_callback(int id, void* myTimer);
273 
274 /**
275  * @brief	  bk_i2c_get_busstate
276  *
277  * This API get bus status idle or busy
278  *
279  * @return
280  *	  - 1: idle
281  *	  - 0: busy.
282  */
283 uint8_t bk_i2c_get_busstate ( int id );
284 
285 /**
286  * @brief	  bk_i2c_get_transstate
287  *
288  * This API get bus status idle or busy
289  *
290  * @return
291  *	  - 1: ok
292  *	  - 0: fail.
293  */
294 uint8_t bk_i2c_get_transstate ( int id );
295 
296 #ifdef __cplusplus
297 }
298 #endif
299 
300