1 /**
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 * Description: Provides V150 i2c register operation api \n
16 *
17 * History: \n
18 * 2023-03-06, Create file. \n
19 */
20
21 #ifndef HAL_I2C_V150_REGS_OP_H
22 #define HAL_I2C_V150_REGS_OP_H
23
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include "soc_osal.h"
27 #include "hal_i2c_v150_regs_def.h"
28 #include "i2c_porting.h"
29
30 #ifdef __cplusplus
31 #if __cplusplus
32 extern "C" {
33 #endif /* __cplusplus */
34 #endif /* __cplusplus */
35
36 /**
37 * @defgroup drivers_hal_i2c_v150_regs_op I2C V150 Regs Operation
38 * @ingroup drivers_hal_i2c
39 * @{
40 */
41
42 /* 操作类型定义 */
43 #define I2C_OP_START (1 << 3)
44 #define I2C_OP_READ (1 << 2)
45 #define I2C_OP_WRITE (1 << 1)
46 #define I2C_OP_STOP (1 << 0)
47
48 /*
49 * 中断类型与状态定义
50 * I2C中断状态/中断清除/中断屏蔽寄存器的比特位不对齐, 通过掩码将其对齐为中断类型, 方便统一处理;
51 * 注意, 以下定义的"中断类型"为代码逻辑中计算使用的中断类型值, 与寄存器真实值不同。
52 */
53 /* 中断类型 */
54 #define I2C_INT_TYPE_DONE (uint32_t)(1 << 0)
55 #define I2C_INT_TYPE_ARB_LOSS (uint32_t)(1 << 1)
56 #define I2C_INT_TYPE_ACK_ERR (uint32_t)(1 << 2)
57 #define I2C_INT_TYPE_RX (uint32_t)(1 << 3)
58 #define I2C_INT_TYPE_TX (uint32_t)(1 << 4)
59 #define I2C_INT_TYPE_STOP (uint32_t)(1 << 5)
60 #define I2C_INT_TYPE_START (uint32_t)(1 << 6)
61 #define I2C_INT_TYPE_RXTIDE (uint32_t)(1 << 7)
62 #define I2C_INT_TYPE_TXTIDE (uint32_t)(1 << 8)
63 #define I2C_INT_TYPE_TXFIFO_OVER (uint32_t)(1 << 9)
64
65 /* 中断有效类型掩码 */
66 #define I2C_INT_TYPE_MASK 0x3FF
67
68 /* I2C_CTRL寄存器 中断屏蔽状态掩码 */
69 #define I2C_CTRL_INT_MASK 0x167F
70 #define I2C_CTRL_INT_MASK_L 0x007F
71 #define I2C_CTRL_INT_MASK_L_OFFSET 0
72 #define I2C_CTRL_INT_MASK_M 0x0600
73 #define I2C_CTRL_INT_MASK_M_OFFSET 2
74 #define I2C_CTRL_INT_MASK_H 0x1000
75 #define I2C_CTRL_INT_MASK_H_OFFSET 3
76
77 /* I2C_ICR寄存器 中断清除状态掩码 */
78 #define I2C_ICR_INT_MASK 0x03FF
79
80 /* I2C_SR寄存器 中断上报状态掩码 */
81 #define I2C_SR_INT_MASK 0x077F
82 #define I2C_SR_INT_MASK_L 0x007F
83 #define I2C_SR_INT_MASK_L_OFFSET 0
84 #define I2C_SR_INT_MASK_H 0x0700
85 #define I2C_SR_INT_MASK_H_OFFSET 1
86
87 /* 毛刺过滤标准值, 默认使用逻辑配置参数0x8 */
88 #define I2C_FTRPER_STANDARD_VAL 0x8
89
90 /**
91 * @if Eng
92 * @brief Bit definition of I2C registers.
93 * @else
94 * @brief I2C寄存器比特位定义。
95 * @endif
96 */
97 typedef enum i2c_con_reg_config {
98 I2C_BIT_FALSE, /*!< 比特置为0 */
99 I2C_BIT_TRUE, /*!< 比特置为1 */
100 } i2c_bit_config_t;
101
102 extern i2c_regs_t *g_i2c_regs[I2C_BUS_MAX_NUM];
103
104 /**
105 * @if Eng
106 * @brief Transform Interrupt type to I2C_CTRL Interrupt mask status bit.
107 * @param [in] int_type Interrupt type.
108 * @return I2C_CTRL Interrupt mask status bit.
109 * @else
110 * @brief 中断类型 转换为 I2C_CTRL 中断屏蔽状态比特。
111 * @param [in] int_type 中断类型。
112 * @return I2C_CTRL 中断屏蔽状态比特。
113 * @endif
114 */
hal_i2c_v150_int_type_to_ctrl(uint32_t int_type)115 static inline uint32_t hal_i2c_v150_int_type_to_ctrl(uint32_t int_type)
116 {
117 return (((int_type & (I2C_CTRL_INT_MASK_L >> I2C_CTRL_INT_MASK_L_OFFSET)) << I2C_CTRL_INT_MASK_L_OFFSET) |
118 ((int_type & (I2C_CTRL_INT_MASK_M >> I2C_CTRL_INT_MASK_M_OFFSET)) << I2C_CTRL_INT_MASK_M_OFFSET) |
119 ((int_type & (I2C_CTRL_INT_MASK_H >> I2C_CTRL_INT_MASK_H_OFFSET)) << I2C_CTRL_INT_MASK_H_OFFSET));
120 }
121
122 /**
123 * @if Eng
124 * @brief Transform I2C_CTRL Interrupt mask status bit to Interrupt type.
125 * @param [in] int_bits Interrupt mask status bit.
126 * @return Interrupt type.
127 * @else
128 * @brief I2C_CTRL中断屏蔽状态比特 转换为 中断类型。
129 * @param [in] int_bits I2C_CTRL中断屏蔽状态比特。
130 * @return 中断类型。
131 * @endif
132 */
hal_i2c_v150_ctrl_to_int_type(uint32_t int_bits)133 static inline uint32_t hal_i2c_v150_ctrl_to_int_type(uint32_t int_bits)
134 {
135 return (((int_bits & I2C_CTRL_INT_MASK_L) >> I2C_CTRL_INT_MASK_L_OFFSET) |
136 ((int_bits & I2C_CTRL_INT_MASK_M) >> I2C_CTRL_INT_MASK_M_OFFSET) |
137 ((int_bits & I2C_CTRL_INT_MASK_H) >> I2C_CTRL_INT_MASK_H_OFFSET));
138 }
139
140 /**
141 * @if Eng
142 * @brief Transform Interrupt type to I2C_ICR Interrupt clear status bit.
143 * @param [in] int_type Interrupt type.
144 * @return I2C_ICR Interrupt clear status bit.
145 * @else
146 * @brief 中断类型 转换为 I2C_ICR 中断清除状态比特。
147 * @param [in] int_type 中断类型。
148 * @return I2C_ICR 中断清除状态比特。
149 * @endif
150 */
hal_i2c_v150_int_type_to_icr(uint32_t int_type)151 static inline uint32_t hal_i2c_v150_int_type_to_icr(uint32_t int_type)
152 {
153 return (int_type & I2C_ICR_INT_MASK);
154 }
155
156 /**
157 * @if Eng
158 * @brief Transform I2C_ICR Interrupt clear status bit to Interrupt type.
159 * @param [in] reg_icr Interrupt clear status bit.
160 * @return Interrupt type.
161 * @else
162 * @brief I2C_ICR 中断清除状态比特 转换为 中断类型。
163 * @param [in] reg_icr I2C_ICR中断清除状态比特。
164 * @return 中断类型。
165 * @endif
166 */
hal_i2c_v150_icr_to_int_type(uint32_t reg_icr)167 static inline uint32_t hal_i2c_v150_icr_to_int_type(uint32_t reg_icr)
168 {
169 return (reg_icr & I2C_ICR_INT_MASK);
170 }
171
172 /**
173 * @if Eng
174 * @brief Transform Interrupt type to I2C_SR Interrupt report status bit.
175 * @param [in] int_type Interrupt type.
176 * @return I2C_SR Interrupt report status bit.
177 * @else
178 * @brief 中断类型 转换为 I2C_SR 中断上报状态比特。
179 * @param [in] int_type 中断类型。
180 * @return I2C_SR 中断上报状态比特。
181 * @endif
182 */
hal_i2c_v150_int_type_to_sr(uint32_t int_type)183 static inline uint32_t hal_i2c_v150_int_type_to_sr(uint32_t int_type)
184 {
185 return (((int_type & (I2C_SR_INT_MASK_L >> I2C_SR_INT_MASK_L_OFFSET)) << I2C_SR_INT_MASK_L_OFFSET) |
186 ((int_type & (I2C_SR_INT_MASK_H >> I2C_SR_INT_MASK_H_OFFSET)) << I2C_SR_INT_MASK_H_OFFSET));
187 }
188
189 /**
190 * @if Eng
191 * @brief Transform I2C_SR Interrupt report status bit to Interrupt type.
192 * @param [in] reg_sr Interrupt report status bit.
193 * @return Interrupt type.
194 * @else
195 * @brief I2C_SR 中断上报状态比特 转换为 中断类型。
196 * @param [in] reg_sr I2C_SR 中断上报状态比特。
197 * @return 中断类型。
198 * @endif
199 */
hal_i2c_v150_sr_to_int_type(uint32_t reg_sr)200 static inline uint32_t hal_i2c_v150_sr_to_int_type(uint32_t reg_sr)
201 {
202 return (((reg_sr & I2C_SR_INT_MASK_L) >> I2C_SR_INT_MASK_L_OFFSET) |
203 ((reg_sr & I2C_SR_INT_MASK_H) >> I2C_SR_INT_MASK_H_OFFSET));
204 }
205
206 /**
207 * @if Eng
208 * @brief Set I2C enable.
209 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
210 * @else
211 * @brief 使能I2C。
212 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
213 * @endif
214 */
hal_i2c_v150_set_i2c_enable(i2c_bus_t bus)215 static inline void hal_i2c_v150_set_i2c_enable(i2c_bus_t bus)
216 {
217 i2c_ctrl_data_t i2c_ctrl;
218 i2c_ctrl.d32 = g_i2c_regs[bus]->i2c_ctrl;
219 i2c_ctrl.b.i2c_en = I2C_BIT_TRUE;
220 g_i2c_regs[bus]->i2c_ctrl = i2c_ctrl.d32;
221 }
222
223 /**
224 * @if Eng
225 * @brief Set I2C disable.
226 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
227 * @else
228 * @brief 去使能I2C。
229 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
230 * @endif
231 */
hal_i2c_v150_set_i2c_disable(i2c_bus_t bus)232 static inline void hal_i2c_v150_set_i2c_disable(i2c_bus_t bus)
233 {
234 i2c_ctrl_data_t i2c_ctrl;
235 i2c_ctrl.d32 = g_i2c_regs[bus]->i2c_ctrl;
236 i2c_ctrl.b.i2c_en = I2C_BIT_FALSE;
237 g_i2c_regs[bus]->i2c_ctrl = i2c_ctrl.d32;
238 }
239
240 /**
241 * @if Eng
242 * @brief Enable FIFO transfer mode.
243 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
244 * @else
245 * @brief 使能FIFO传输模式。
246 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
247 * @endif
248 */
hal_i2c_v150_set_fifo_enable(i2c_bus_t bus)249 static inline void hal_i2c_v150_set_fifo_enable(i2c_bus_t bus)
250 {
251 i2c_ctrl_data_t i2c_ctrl;
252 i2c_ctrl.d32 = g_i2c_regs[bus]->i2c_ctrl;
253 i2c_ctrl.b.mode_ctrl = I2C_BIT_TRUE;
254 g_i2c_regs[bus]->i2c_ctrl = i2c_ctrl.d32;
255 }
256
257 /**
258 * @if Eng
259 * @brief Disable FIFO transfer mode.
260 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
261 * @else
262 * @brief 去使能FIFO传输模式。
263 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
264 * @endif
265 */
hal_i2c_v150_set_fifo_disable(i2c_bus_t bus)266 static inline void hal_i2c_v150_set_fifo_disable(i2c_bus_t bus)
267 {
268 i2c_ctrl_data_t i2c_ctrl;
269 i2c_ctrl.d32 = g_i2c_regs[bus]->i2c_ctrl;
270 i2c_ctrl.b.mode_ctrl = I2C_BIT_FALSE;
271 g_i2c_regs[bus]->i2c_ctrl = i2c_ctrl.d32;
272 }
273
274 /**
275 * @if Eng
276 * @brief Mask main entry of interrupt.
277 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
278 * @else
279 * @brief 屏蔽中断总入口。
280 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
281 * @endif
282 */
hal_i2c_v150_mask_main_int(i2c_bus_t bus)283 static inline void hal_i2c_v150_mask_main_int(i2c_bus_t bus)
284 {
285 i2c_ctrl_data_t i2c_ctrl;
286 i2c_ctrl.d32 = g_i2c_regs[bus]->i2c_ctrl;
287 i2c_ctrl.b.int_mask = I2C_BIT_FALSE;
288 g_i2c_regs[bus]->i2c_ctrl = i2c_ctrl.d32;
289 }
290
291 /**
292 * @if Eng
293 * @brief Unmask main entry of interrupt.
294 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
295 * @else
296 * @brief 不屏蔽中断总入口。
297 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
298 * @endif
299 */
hal_i2c_v150_unmask_main_int(i2c_bus_t bus)300 static inline void hal_i2c_v150_unmask_main_int(i2c_bus_t bus)
301 {
302 i2c_ctrl_data_t i2c_ctrl;
303 i2c_ctrl.d32 = g_i2c_regs[bus]->i2c_ctrl;
304 i2c_ctrl.b.int_mask = I2C_BIT_TRUE;
305 g_i2c_regs[bus]->i2c_ctrl = i2c_ctrl.d32;
306 }
307
308 /**
309 * @if Eng
310 * @brief Mask specific interrupt.
311 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
312 * @param [in] int_type Interrupt type.
313 * @else
314 * @brief 屏蔽指定中断。
315 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
316 * @param [in] int_type 中断类型。
317 * @endif
318 */
hal_i2c_v150_mask_int(i2c_bus_t bus,uint32_t int_type)319 static inline void hal_i2c_v150_mask_int(i2c_bus_t bus, uint32_t int_type)
320 {
321 i2c_ctrl_data_t i2c_ctrl;
322 i2c_ctrl.d32 = g_i2c_regs[bus]->i2c_ctrl;
323 i2c_ctrl.d32 &= ~(hal_i2c_v150_int_type_to_ctrl(int_type));
324 g_i2c_regs[bus]->i2c_ctrl = i2c_ctrl.d32;
325 }
326
327 /**
328 * @if Eng
329 * @brief Unmask specific interrupt.
330 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
331 * @param [in] int_type Interrupt type.
332 * @else
333 * @brief 不屏蔽指定中断。
334 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
335 * @param [in] int_type 中断类型。
336 * @endif
337 */
hal_i2c_v150_unmask_int(i2c_bus_t bus,uint32_t int_type)338 static inline void hal_i2c_v150_unmask_int(i2c_bus_t bus, uint32_t int_type)
339 {
340 i2c_ctrl_data_t i2c_ctrl;
341 i2c_ctrl.d32 = g_i2c_regs[bus]->i2c_ctrl;
342 i2c_ctrl.d32 |= hal_i2c_v150_int_type_to_ctrl(int_type);
343 g_i2c_regs[bus]->i2c_ctrl = i2c_ctrl.d32;
344 }
345
346 /**
347 * @if Eng
348 * @brief Mask all interrupt.
349 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
350 * @else
351 * @brief 屏蔽所有中断。
352 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
353 * @endif
354 */
hal_i2c_v150_mask_all_int(i2c_bus_t bus)355 static inline void hal_i2c_v150_mask_all_int(i2c_bus_t bus)
356 {
357 hal_i2c_v150_mask_int(bus, I2C_INT_TYPE_DONE | I2C_INT_TYPE_ARB_LOSS | I2C_INT_TYPE_ACK_ERR |
358 I2C_INT_TYPE_RX | I2C_INT_TYPE_TX | I2C_INT_TYPE_STOP | I2C_INT_TYPE_START |
359 I2C_INT_TYPE_RXTIDE | I2C_INT_TYPE_TXTIDE | I2C_INT_TYPE_TXFIFO_OVER);
360 }
361
362 /**
363 * @if Eng
364 * @brief Unask all interrupt.
365 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
366 * @else
367 * @brief 不屏蔽所有中断。
368 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
369 * @endif
370 */
hal_i2c_v150_unmask_all_int(i2c_bus_t bus)371 static inline void hal_i2c_v150_unmask_all_int(i2c_bus_t bus)
372 {
373 hal_i2c_v150_unmask_int(bus, I2C_INT_TYPE_DONE | I2C_INT_TYPE_ARB_LOSS | I2C_INT_TYPE_ACK_ERR |
374 I2C_INT_TYPE_RX | I2C_INT_TYPE_TX | I2C_INT_TYPE_STOP | I2C_INT_TYPE_START |
375 I2C_INT_TYPE_RXTIDE | I2C_INT_TYPE_TXTIDE | I2C_INT_TYPE_TXFIFO_OVER);
376 }
377
378 /**
379 * @if Eng
380 * @brief Set I2C to send ACK when receive.
381 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
382 * @else
383 * @brief 设置I2C接收时发送ACK。
384 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
385 * @endif
386 */
hal_i2c_v150_enable_ack(i2c_bus_t bus)387 static inline void hal_i2c_v150_enable_ack(i2c_bus_t bus)
388 {
389 i2c_com_data_t i2c_com = {0};
390 i2c_com.b.op_ack = I2C_BIT_FALSE;
391 g_i2c_regs[bus]->i2c_com = i2c_com.d32;
392 }
393
394 /**
395 * @if Eng
396 * @brief Set I2C NOT to send ACK when receive.
397 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
398 * @else
399 * @brief 设置I2C接收时不发送ACK。
400 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
401 * @endif
402 */
hal_i2c_v150_disable_ack(i2c_bus_t bus)403 static inline void hal_i2c_v150_disable_ack(i2c_bus_t bus)
404 {
405 i2c_com_data_t i2c_com = {0};
406 i2c_com.b.op_ack = I2C_BIT_TRUE;
407 g_i2c_regs[bus]->i2c_com = i2c_com.d32;
408 }
409
410 /**
411 * @if Eng
412 * @brief Set I2C work operation.
413 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
414 * @param [in] command_type I2C command(restart, stop, write, read).
415 * @else
416 * @brief 设置I2C工作命令。
417 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
418 * @param [in] command_type I2C命令类型(重新启动, 停止, 写, 读)。
419 * @endif
420 */
hal_i2c_v150_set_command(i2c_bus_t bus,uint32_t command_type)421 static inline void hal_i2c_v150_set_command(i2c_bus_t bus, uint32_t command_type)
422 {
423 i2c_com_data_t i2c_com;
424 i2c_com.d32 = g_i2c_regs[bus]->i2c_com;
425 i2c_com.b.op_stop = I2C_BIT_FALSE;
426 i2c_com.b.op_we = I2C_BIT_FALSE;
427 i2c_com.b.op_rd = I2C_BIT_FALSE;
428 i2c_com.b.op_start = I2C_BIT_FALSE;
429 i2c_com.d32 |= command_type;
430 g_i2c_regs[bus]->i2c_com = i2c_com.d32;
431 }
432
433 /**
434 * @if Eng
435 * @brief Clear specific interrupt.
436 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
437 * @param [in] int_type Interrupt type.
438 * @else
439 * @brief 清除指定中断。
440 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
441 * @param [in] int_type 中断类型。
442 * @endif
443 */
hal_i2c_v150_clear_int(i2c_bus_t bus,uint32_t int_type)444 static inline void hal_i2c_v150_clear_int(i2c_bus_t bus, uint32_t int_type)
445 {
446 i2c_icr_data_t i2c_icr;
447 i2c_icr.d32 = hal_i2c_v150_int_type_to_icr(int_type);
448 g_i2c_regs[bus]->i2c_icr = i2c_icr.d32;
449 }
450
451 /**
452 * @if Eng
453 * @brief Clear all interrupt.
454 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
455 * @else
456 * @brief 清除所有中断。
457 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
458 * @endif
459 */
hal_i2c_v150_clear_all_int(i2c_bus_t bus)460 static inline void hal_i2c_v150_clear_all_int(i2c_bus_t bus)
461 {
462 hal_i2c_v150_clear_int(bus, I2C_INT_TYPE_DONE | I2C_INT_TYPE_ARB_LOSS | I2C_INT_TYPE_ACK_ERR |
463 I2C_INT_TYPE_RX | I2C_INT_TYPE_TX | I2C_INT_TYPE_STOP | I2C_INT_TYPE_START |
464 I2C_INT_TYPE_RXTIDE | I2C_INT_TYPE_TXTIDE | I2C_INT_TYPE_TXFIFO_OVER);
465 }
466
467 /**
468 * @if Eng
469 * @brief Get interrupt state.
470 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
471 * @return Interrupt type.
472 * @else
473 * @brief 获取中断状态。
474 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
475 * @return 中断类型。
476 * @endif
477 */
hal_i2c_v150_get_int_state(i2c_bus_t bus)478 static inline uint32_t hal_i2c_v150_get_int_state(i2c_bus_t bus)
479 {
480 i2c_sr_data_t i2c_sr;
481 i2c_sr.d32 = g_i2c_regs[bus]->i2c_sr;
482 return hal_i2c_v150_sr_to_int_type(i2c_sr.d32);
483 }
484
485 /**
486 * @if Eng
487 * @brief Get bus busy status.
488 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
489 * @return Bus busy status. 1: Bus busy. 0: Bus idle.
490 * @else
491 * @brief 获取总线忙状态。
492 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
493 * @return 总线忙状态, 1:总线忙, 0:总线空闲。
494 * @endif
495 */
hal_i2c_v150_is_bus_busy(i2c_bus_t bus)496 static inline uint8_t hal_i2c_v150_is_bus_busy(i2c_bus_t bus)
497 {
498 i2c_sr_data_t i2c_sr;
499 i2c_sr.d32 = g_i2c_regs[bus]->i2c_sr;
500 return i2c_sr.b.bus_busy;
501 }
502
503 /**
504 * @if Eng
505 * @brief Set number of clock high-level cycles scl_h.
506 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
507 * @param [in] val Number of clock high-level cycles scl_h.
508 * @else
509 * @brief 设置时钟高周期数scl_h。
510 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
511 * @param [in] val 时钟高周期数scl_h。
512 * @endif
513 */
hal_i2c_v150_set_scl_h(i2c_bus_t bus,uint16_t val)514 static inline void hal_i2c_v150_set_scl_h(i2c_bus_t bus, uint16_t val)
515 {
516 i2c_scl_h_data_t i2c_scl_h = {0};
517 i2c_scl_h.b.scl_h = val;
518 g_i2c_regs[bus]->i2c_scl_h = i2c_scl_h.d32;
519 }
520
521 /**
522 * @if Eng
523 * @brief Set number of clock low-level cycles scl_h.
524 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
525 * @param [in] val Number of clock low-level cycles scl_h.
526 * @else
527 * @brief 设置时钟低周期数scl_l。
528 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
529 * @param [in] val 时钟低周期数scl_l。
530 * @endif
531 */
hal_i2c_v150_set_scl_l(i2c_bus_t bus,uint16_t val)532 static inline void hal_i2c_v150_set_scl_l(i2c_bus_t bus, uint16_t val)
533 {
534 i2c_scl_l_data_t i2c_scl_l = {0};
535 i2c_scl_l.b.scl_l = val;
536 g_i2c_regs[bus]->i2c_scl_l = i2c_scl_l.d32;
537 }
538
539 /**
540 * @if Eng
541 * @brief Set data to send.
542 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
543 * @param [in] data Data to send, one byte.
544 * @else
545 * @brief 设置发送数据。
546 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
547 * @param [in] data 待发送数据, 一个字节。
548 * @endif
549 */
hal_i2c_v150_set_tx_data(i2c_bus_t bus,uint8_t data)550 static inline void hal_i2c_v150_set_tx_data(i2c_bus_t bus, uint8_t data)
551 {
552 i2c_txr_data_t i2c_txr = {0};
553 i2c_txr.b.i2c_txr = data;
554 g_i2c_regs[bus]->i2c_txr = i2c_txr.d32;
555 }
556
557 /**
558 * @if Eng
559 * @brief Set data to receive.
560 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
561 * @return Data to receive, one byte.
562 * @else
563 * @brief 获取接收数据。
564 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
565 * @return 接收的数据, 一个字节。
566 * @endif
567 */
hal_i2c_v150_get_rx_data(i2c_bus_t bus)568 static inline uint8_t hal_i2c_v150_get_rx_data(i2c_bus_t bus)
569 {
570 i2c_rxr_data_t i2c_rxr;
571 i2c_rxr.d32 = g_i2c_regs[bus]->i2c_rxr;
572 return i2c_rxr.b.i2c_rxr;
573 }
574
575 /**
576 * @if Eng
577 * @brief Set number of glitch filtering cycles.
578 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
579 * @param [in] ftrper Number of glitch filtering cycles, Signal be regard as receive data when
580 * duration is longer than this number. Width 4 bit.
581 * @else
582 * @brief 设置毛刺过滤周期数。
583 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
584 * @param [in] ftrper 毛刺过滤周期数, 数据持续时间大于该周期数才被认为是接收值, 宽度为4bit。
585 * @endif
586 */
hal_i2c_v150_set_ftrper(i2c_bus_t bus,uint8_t ftrper)587 static inline void hal_i2c_v150_set_ftrper(i2c_bus_t bus, uint8_t ftrper)
588 {
589 i2c_ftrper_data_t i2c_ftrper = {0};
590 i2c_ftrper.b.ftrper = ftrper;
591 g_i2c_regs[bus]->i2c_ftrper = i2c_ftrper.d32;
592 }
593
594 /**
595 * @if Eng
596 * @brief Get number of glitch filtering cycles.
597 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
598 * @return Number of glitch filtering cycles, Signal be regard as receive data when
599 * duration is longer than this number. Width 4 bit.
600 * @else
601 * @brief 获取毛刺过滤周期数。
602 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
603 * @return 毛刺过滤周期数, 数据持续时间大于该周期数才被认为是接收值, 宽度为4bit。
604 * @endif
605 */
hal_i2c_v150_get_ftrper(i2c_bus_t bus)606 static inline uint8_t hal_i2c_v150_get_ftrper(i2c_bus_t bus)
607 {
608 i2c_ftrper_data_t i2c_ftrper;
609 i2c_ftrper.d32 = g_i2c_regs[bus]->i2c_ftrper;
610 return i2c_ftrper.b.ftrper;
611 }
612
613 /**
614 * @if Eng
615 * @brief Initialize base address of I2C registers.
616 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
617 * @else
618 * @brief 初始化I2C寄存器基地址。
619 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
620 * @endif
621 */
622 void hal_i2c_v150_regs_init(i2c_bus_t bus);
623
624 /**
625 * @if Eng
626 * @brief Deinitialize base address of I2C registers.
627 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
628 * @else
629 * @brief 去初始化I2C寄存器基地址。
630 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
631 * @endif
632 */
633 void hal_i2c_v150_regs_deinit(i2c_bus_t bus);
634
635 /**
636 * @if Eng
637 * @brief Reset all writeable registers to initial state.
638 * @param [in] bus I2C bus id, see @ref i2c_bus_t.
639 * @else
640 * @brief 设置所有可写寄存器恢复初始状态。
641 * @param [in] bus I2C总线id, 参考 @ref i2c_bus_t 。
642 * @endif
643 */
644 void hal_i2c_v150_reset_all_regs(i2c_bus_t bus);
645
646 /**
647 * @}
648 */
649
650 #ifdef __cplusplus
651 #if __cplusplus
652 }
653 #endif /* __cplusplus */
654 #endif /* __cplusplus */
655
656 #endif