1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
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 // The LL layer for I2C register operations
16
17 #pragma once
18 #include "soc/i2c_periph.h"
19 #include "hal/i2c_types.h"
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
25 #define I2C_LL_INTR_MASK (0x3fff) /*!< I2C all interrupt bitmap */
26
27 /**
28 * @brief I2C hardware cmd register filed.
29 */
30 typedef union {
31 struct {
32 uint32_t byte_num: 8,
33 ack_en: 1,
34 ack_exp: 1,
35 ack_val: 1,
36 op_code: 3,
37 reserved14: 17,
38 done: 1;
39 };
40 uint32_t val;
41 } i2c_hw_cmd_t;
42
43 /**
44 * @brief I2C interrupt event
45 */
46 typedef enum {
47 I2C_INTR_EVENT_ERR,
48 I2C_INTR_EVENT_ARBIT_LOST, /*!< I2C arbition lost event */
49 I2C_INTR_EVENT_NACK, /*!< I2C NACK event */
50 I2C_INTR_EVENT_TOUT, /*!< I2C time out event */
51 I2C_INTR_EVENT_END_DET, /*!< I2C end detected event */
52 I2C_INTR_EVENT_TRANS_DONE, /*!< I2C trans done event */
53 I2C_INTR_EVENT_RXFIFO_FULL, /*!< I2C rxfifo full event */
54 I2C_INTR_EVENT_TXFIFO_EMPTY, /*!< I2C txfifo empty event */
55 } i2c_intr_event_t;
56
57 /**
58 * @brief Data structure for calculating I2C bus timing.
59 */
60 typedef struct {
61 uint16_t scl_low; /*!< I2C scl low period */
62 uint16_t scl_high; /*!< I2C scl hight period */
63 uint16_t sda_hold; /*!< I2C scl low period */
64 uint16_t sda_sample; /*!< I2C sda sample time */
65 uint16_t setup; /*!< I2C start and stop condition setup period */
66 uint16_t hold; /*!< I2C start and stop condition hold period */
67 uint16_t tout; /*!< I2C bus timeout period */
68 } i2c_clk_cal_t;
69
70 // I2C operation mode command
71 #define I2C_LL_CMD_RESTART 0 /*!<I2C restart command */
72 #define I2C_LL_CMD_WRITE 1 /*!<I2C write command */
73 #define I2C_LL_CMD_READ 2 /*!<I2C read command */
74 #define I2C_LL_CMD_STOP 3 /*!<I2C stop command */
75 #define I2C_LL_CMD_END 4 /*!<I2C end command */
76
77 // Get the I2C hardware instance
78 #define I2C_LL_GET_HW(i2c_num) (((i2c_num) == 0) ? &I2C0 : &I2C1)
79 // Get the I2C hardware FIFO address
80 #define I2C_LL_GET_FIFO_ADDR(i2c_num) (I2C_DATA_APB_REG(i2c_num))
81 // I2C master TX interrupt bitmap
82 #define I2C_LL_MASTER_TX_INT (I2C_ACK_ERR_INT_ENA_M|I2C_TIME_OUT_INT_ENA_M|I2C_TRANS_COMPLETE_INT_ENA_M|I2C_ARBITRATION_LOST_INT_ENA_M|I2C_END_DETECT_INT_ENA_M)
83 // I2C master RX interrupt bitmap
84 #define I2C_LL_MASTER_RX_INT (I2C_TIME_OUT_INT_ENA_M|I2C_TRANS_COMPLETE_INT_ENA_M|I2C_ARBITRATION_LOST_INT_ENA_M|I2C_END_DETECT_INT_ENA_M)
85 // I2C slave TX interrupt bitmap
86 #define I2C_LL_SLAVE_TX_INT (I2C_TXFIFO_EMPTY_INT_ENA_M)
87 // I2C slave RX interrupt bitmap
88 #define I2C_LL_SLAVE_RX_INT (I2C_RXFIFO_FULL_INT_ENA_M | I2C_TRANS_COMPLETE_INT_ENA_M)
89 // I2C source clock frequency
90 #define I2C_LL_CLK_SRC_FREQ(src_clk) (80*1000*1000)
91 /**
92 * @brief Calculate I2C bus frequency
93 *
94 * @param source_clk I2C source clock
95 * @param bus_freq I2C bus frequency
96 * @param clk_cal Pointer to accept the clock configuration
97 *
98 * @return None
99 */
i2c_ll_cal_bus_clk(uint32_t source_clk,uint32_t bus_freq,i2c_clk_cal_t * clk_cal)100 static inline void i2c_ll_cal_bus_clk(uint32_t source_clk, uint32_t bus_freq, i2c_clk_cal_t *clk_cal)
101 {
102 uint32_t half_cycle = source_clk / bus_freq / 2;
103 clk_cal->scl_low = half_cycle;
104 clk_cal->scl_high = half_cycle;
105 clk_cal->sda_hold = half_cycle / 2;
106 clk_cal->sda_sample = clk_cal->scl_high / 2;
107 clk_cal->setup = half_cycle;
108 clk_cal->hold = half_cycle;
109 clk_cal->tout = half_cycle * 20; //default we set the timeout value to 10 bus cycles.
110 }
111
112 /**
113 * @brief Configure the I2C bus timing related register.
114 *
115 * @param hw Beginning address of the peripheral registers
116 * @param bus_cfg Pointer to the data structure holding the register configuration.
117 *
118 * @return None
119 */
i2c_ll_set_bus_timing(i2c_dev_t * hw,i2c_clk_cal_t * bus_cfg)120 static inline void i2c_ll_set_bus_timing(i2c_dev_t *hw, i2c_clk_cal_t *bus_cfg)
121 {
122 //scl period
123 hw->scl_low_period.period = bus_cfg->scl_low;
124 hw->scl_high_period.period = bus_cfg->scl_high;
125 //sda sample
126 hw->sda_hold.time = bus_cfg->sda_hold;
127 hw->sda_sample.time = bus_cfg->sda_sample;
128 //setup
129 hw->scl_rstart_setup.time = bus_cfg->setup;
130 hw->scl_stop_setup.time = bus_cfg->setup;
131 //hold
132 hw->scl_start_hold.time = bus_cfg->hold;
133 hw->scl_stop_hold.time = bus_cfg->hold;
134 hw->timeout.tout = bus_cfg->tout;
135 }
136
137 /**
138 * @brief Reset I2C txFIFO
139 *
140 * @param hw Beginning address of the peripheral registers
141 *
142 * @return None
143 */
i2c_ll_txfifo_rst(i2c_dev_t * hw)144 static inline void i2c_ll_txfifo_rst(i2c_dev_t *hw)
145 {
146 hw->fifo_conf.tx_fifo_rst = 1;
147 hw->fifo_conf.tx_fifo_rst = 0;
148 }
149
150 /**
151 * @brief Reset I2C rxFIFO
152 *
153 * @param hw Beginning address of the peripheral registers
154 *
155 * @return None
156 */
i2c_ll_rxfifo_rst(i2c_dev_t * hw)157 static inline void i2c_ll_rxfifo_rst(i2c_dev_t *hw)
158 {
159 hw->fifo_conf.rx_fifo_rst = 1;
160 hw->fifo_conf.rx_fifo_rst = 0;
161 }
162
163 /**
164 * @brief Configure I2C SCL timing
165 *
166 * @param hw Beginning address of the peripheral registers
167 * @param hight_period The I2C SCL hight period (in APB cycle)
168 * @param low_period The I2C SCL low period (in APB cycle)
169 *
170 * @return None.
171 */
i2c_ll_set_scl_timing(i2c_dev_t * hw,int hight_period,int low_period)172 static inline void i2c_ll_set_scl_timing(i2c_dev_t *hw, int hight_period, int low_period)
173 {
174 hw->scl_low_period.period = low_period;
175 hw->scl_high_period.period = hight_period;
176 }
177
178 /**
179 * @brief Clear I2C interrupt status
180 *
181 * @param hw Beginning address of the peripheral registers
182 * @param mask Interrupt mask needs to be cleared
183 *
184 * @return None
185 */
i2c_ll_clr_intsts_mask(i2c_dev_t * hw,uint32_t mask)186 static inline void i2c_ll_clr_intsts_mask(i2c_dev_t *hw, uint32_t mask)
187 {
188 hw->int_clr.val = mask;
189 }
190
191 /**
192 * @brief Enable I2C interrupt
193 *
194 * @param hw Beginning address of the peripheral registers
195 * @param mask Interrupt mask needs to be enabled
196 *
197 * @return None
198 */
i2c_ll_enable_intr_mask(i2c_dev_t * hw,uint32_t mask)199 static inline void i2c_ll_enable_intr_mask(i2c_dev_t *hw, uint32_t mask)
200 {
201 hw->int_ena.val |= mask;
202 }
203
204 /**
205 * @brief Disable I2C interrupt
206 *
207 * @param hw Beginning address of the peripheral registers
208 * @param mask Interrupt mask needs to be disabled
209 *
210 * @return None
211 */
i2c_ll_disable_intr_mask(i2c_dev_t * hw,uint32_t mask)212 static inline void i2c_ll_disable_intr_mask(i2c_dev_t *hw, uint32_t mask)
213 {
214 hw->int_ena.val &= (~mask);
215 }
216
217 /**
218 * @brief Get I2C interrupt status
219 *
220 * @param hw Beginning address of the peripheral registers
221 *
222 * @return I2C interrupt status
223 */
i2c_ll_get_intsts_mask(i2c_dev_t * hw)224 static inline uint32_t i2c_ll_get_intsts_mask(i2c_dev_t *hw)
225 {
226 return hw->int_status.val;
227 }
228
229 /**
230 * @brief Configure I2C memory access mode, FIFO mode or non-FIFO mode
231 *
232 * @param hw Beginning address of the peripheral registers
233 * @param fifo_mode_en Set true to enable FIFO access mode, else, set it false
234 *
235 * @return None
236 */
i2c_ll_set_fifo_mode(i2c_dev_t * hw,bool fifo_mode_en)237 static inline void i2c_ll_set_fifo_mode(i2c_dev_t *hw, bool fifo_mode_en)
238 {
239 hw->fifo_conf.nonfifo_en = fifo_mode_en ? 0 : 1;
240 }
241
242 /**
243 * @brief Configure I2C timeout
244 *
245 * @param hw Beginning address of the peripheral registers
246 * @param tout_num The I2C timeout value needs to be set (in APB cycle)
247 *
248 * @return None
249 */
i2c_ll_set_tout(i2c_dev_t * hw,int tout)250 static inline void i2c_ll_set_tout(i2c_dev_t *hw, int tout)
251 {
252 hw->timeout.tout = tout;
253 }
254
255 /**
256 * @brief Configure I2C slave address
257 *
258 * @param hw Beginning address of the peripheral registers
259 * @param slave_addr I2C slave address needs to be set
260 * @param addr_10bit_en Set true to enable 10-bit slave address mode, set false to enable 7-bit address mode
261 *
262 * @return None
263 */
i2c_ll_set_slave_addr(i2c_dev_t * hw,uint16_t slave_addr,bool addr_10bit_en)264 static inline void i2c_ll_set_slave_addr(i2c_dev_t *hw, uint16_t slave_addr, bool addr_10bit_en)
265 {
266 hw->slave_addr.addr = slave_addr;
267 hw->slave_addr.en_10bit = addr_10bit_en;
268 }
269
270 /**
271 * @brief Write I2C hardware command register
272 *
273 * @param hw Beginning address of the peripheral registers
274 * @param cmd I2C hardware command
275 * @param cmd_idx The index of the command register, should be less than 16
276 *
277 * @return None
278 */
i2c_ll_write_cmd_reg(i2c_dev_t * hw,i2c_hw_cmd_t cmd,int cmd_idx)279 static inline void i2c_ll_write_cmd_reg(i2c_dev_t *hw, i2c_hw_cmd_t cmd, int cmd_idx)
280 {
281 hw->command[cmd_idx].val = cmd.val;
282 }
283
284 /**
285 * @brief Configure I2C start timing
286 *
287 * @param hw Beginning address of the peripheral registers
288 * @param start_setup The start condition setup period (in APB cycle)
289 * @param start_hold The start condition hold period (in APB cycle)
290 *
291 * @return None
292 */
i2c_ll_set_start_timing(i2c_dev_t * hw,int start_setup,int start_hold)293 static inline void i2c_ll_set_start_timing(i2c_dev_t *hw, int start_setup, int start_hold)
294 {
295 hw->scl_rstart_setup.time = start_setup;
296 hw->scl_start_hold.time = start_hold;
297 }
298
299 /**
300 * @brief Configure I2C stop timing
301 *
302 * @param hw Beginning address of the peripheral registers
303 * @param stop_setup The stop condition setup period (in APB cycle)
304 * @param stop_hold The stop condition hold period (in APB cycle)
305 *
306 * @return None
307 */
i2c_ll_set_stop_timing(i2c_dev_t * hw,int stop_setup,int stop_hold)308 static inline void i2c_ll_set_stop_timing(i2c_dev_t *hw, int stop_setup, int stop_hold)
309 {
310 hw->scl_stop_setup.time = stop_setup;
311 hw->scl_stop_hold.time = stop_hold;
312 }
313
314 /**
315 * @brief Configure I2C stop timing
316 *
317 * @param hw Beginning address of the peripheral registers
318 * @param sda_sample The SDA sample time (in APB cycle)
319 * @param sda_hold The SDA hold time (in APB cycle)
320 *
321 * @return None
322 */
i2c_ll_set_sda_timing(i2c_dev_t * hw,int sda_sample,int sda_hold)323 static inline void i2c_ll_set_sda_timing(i2c_dev_t *hw, int sda_sample, int sda_hold)
324 {
325 hw->sda_hold.time = sda_hold;
326 hw->sda_sample.time = sda_sample;
327 }
328
329 /**
330 * @brief Set I2C txFIFO empty threshold
331 *
332 * @param hw Beginning address of the peripheral registers
333 * @param empty_thr The txFIFO empty threshold
334 *
335 * @return None
336 */
i2c_ll_set_txfifo_empty_thr(i2c_dev_t * hw,uint8_t empty_thr)337 static inline void i2c_ll_set_txfifo_empty_thr(i2c_dev_t *hw, uint8_t empty_thr)
338 {
339 hw->fifo_conf.tx_fifo_empty_thrhd = empty_thr;
340 }
341
342 /**
343 * @brief Set I2C rxFIFO full threshold
344 *
345 * @param hw Beginning address of the peripheral registers
346 * @param full_thr The rxFIFO full threshold
347 *
348 * @return None
349 */
i2c_ll_set_rxfifo_full_thr(i2c_dev_t * hw,uint8_t full_thr)350 static inline void i2c_ll_set_rxfifo_full_thr(i2c_dev_t *hw, uint8_t full_thr)
351 {
352 hw->fifo_conf.rx_fifo_full_thrhd = full_thr;
353 }
354
355 /**
356 * @brief Set the I2C data mode, LSB or MSB
357 *
358 * @param hw Beginning address of the peripheral registers
359 * @param tx_mode Tx data bit mode
360 * @param rx_mode Rx data bit mode
361 *
362 * @return None
363 */
i2c_ll_set_data_mode(i2c_dev_t * hw,i2c_trans_mode_t tx_mode,i2c_trans_mode_t rx_mode)364 static inline void i2c_ll_set_data_mode(i2c_dev_t *hw, i2c_trans_mode_t tx_mode, i2c_trans_mode_t rx_mode)
365 {
366 hw->ctr.tx_lsb_first = tx_mode;
367 hw->ctr.rx_lsb_first = rx_mode;
368 }
369
370 /**
371 * @brief Get the I2C data mode
372 *
373 * @param hw Beginning address of the peripheral registers
374 * @param tx_mode Pointer to accept the received bytes mode
375 * @param rx_mode Pointer to accept the sended bytes mode
376 *
377 * @return None
378 */
i2c_ll_get_data_mode(i2c_dev_t * hw,i2c_trans_mode_t * tx_mode,i2c_trans_mode_t * rx_mode)379 static inline void i2c_ll_get_data_mode(i2c_dev_t *hw, i2c_trans_mode_t *tx_mode, i2c_trans_mode_t *rx_mode)
380 {
381 *tx_mode = hw->ctr.tx_lsb_first;
382 *rx_mode = hw->ctr.rx_lsb_first;
383 }
384
385 /**
386 * @brief Get I2C sda timing configuration
387 *
388 * @param hw Beginning address of the peripheral registers
389 * @param sda_sample Pointer to accept the SDA sample timing configuration
390 * @param sda_hold Pointer to accept the SDA hold timing configuration
391 *
392 * @return None
393 */
i2c_ll_get_sda_timing(i2c_dev_t * hw,int * sda_sample,int * sda_hold)394 static inline void i2c_ll_get_sda_timing(i2c_dev_t *hw, int *sda_sample, int *sda_hold)
395 {
396 *sda_hold = hw->sda_hold.time;
397 *sda_sample = hw->sda_sample.time;
398 }
399
400 /**
401 * @brief Get the I2C hardware version
402 *
403 * @param hw Beginning address of the peripheral registers
404 *
405 * @return The I2C hardware version
406 */
i2c_ll_get_hw_version(i2c_dev_t * hw)407 static inline uint32_t i2c_ll_get_hw_version(i2c_dev_t *hw)
408 {
409 return hw->date;
410 }
411
412 /**
413 * @brief Check if the I2C bus is busy
414 *
415 * @param hw Beginning address of the peripheral registers
416 *
417 * @return True if I2C state machine is busy, else false will be returned
418 */
i2c_ll_is_bus_busy(i2c_dev_t * hw)419 static inline bool i2c_ll_is_bus_busy(i2c_dev_t *hw)
420 {
421 return hw->status_reg.bus_busy;
422 }
423
424 /**
425 * @brief Check if I2C is master mode
426 *
427 * @param hw Beginning address of the peripheral registers
428 *
429 * @return True if I2C is master mode, else false will be returned
430 */
i2c_ll_is_master_mode(i2c_dev_t * hw)431 static inline bool i2c_ll_is_master_mode(i2c_dev_t *hw)
432 {
433 return hw->ctr.ms_mode;
434 }
435
436 /**
437 * @brief Get the rxFIFO readable length
438 *
439 * @param hw Beginning address of the peripheral registers
440 *
441 * @return RxFIFO readable length
442 */
i2c_ll_get_rxfifo_cnt(i2c_dev_t * hw)443 static inline uint32_t i2c_ll_get_rxfifo_cnt(i2c_dev_t *hw)
444 {
445 return hw->status_reg.rx_fifo_cnt;
446 }
447
448 /**
449 * @brief Get I2C txFIFO writable length
450 *
451 * @param hw Beginning address of the peripheral registers
452 *
453 * @return TxFIFO writable length
454 */
i2c_ll_get_txfifo_len(i2c_dev_t * hw)455 static inline uint32_t i2c_ll_get_txfifo_len(i2c_dev_t *hw)
456 {
457 return SOC_I2C_FIFO_LEN - hw->status_reg.tx_fifo_cnt;
458 }
459
460 /**
461 * @brief Get I2C timeout configuration
462 *
463 * @param hw Beginning address of the peripheral registers
464 *
465 * @return The I2C timeout value
466 */
i2c_ll_get_tout(i2c_dev_t * hw)467 static inline uint32_t i2c_ll_get_tout(i2c_dev_t *hw)
468 {
469 return hw->timeout.tout;
470 }
471
472 /**
473 * @brief Start I2C transfer
474 *
475 * @param hw Beginning address of the peripheral registers
476 *
477 * @return None
478 */
i2c_ll_trans_start(i2c_dev_t * hw)479 static inline void i2c_ll_trans_start(i2c_dev_t *hw)
480 {
481 hw->ctr.trans_start = 1;
482 }
483
484 /**
485 * @brief Get I2C start timing configuration
486 *
487 * @param hw Beginning address of the peripheral registers
488 * @param setup_time Pointer to accept the start condition setup period
489 * @param hold_time Pointer to accept the start condition hold period
490 *
491 * @return None
492 */
i2c_ll_get_start_timing(i2c_dev_t * hw,int * setup_time,int * hold_time)493 static inline void i2c_ll_get_start_timing(i2c_dev_t *hw, int *setup_time, int *hold_time)
494 {
495 *setup_time = hw->scl_rstart_setup.time;
496 *hold_time = hw->scl_start_hold.time;
497 }
498
499 /**
500 * @brief Get I2C stop timing configuration
501 *
502 * @param hw Beginning address of the peripheral registers
503 * @param setup_time Pointer to accept the stop condition setup period
504 * @param hold_time Pointer to accept the stop condition hold period
505 *
506 * @return None
507 */
i2c_ll_get_stop_timing(i2c_dev_t * hw,int * setup_time,int * hold_time)508 static inline void i2c_ll_get_stop_timing(i2c_dev_t *hw, int *setup_time, int *hold_time)
509 {
510 *setup_time = hw->scl_stop_setup.time;
511 *hold_time = hw->scl_stop_hold.time;
512 }
513
514 /**
515 * @brief Get I2C SCL timing configuration
516 *
517 * @param hw Beginning address of the peripheral registers
518 * @param high_period Pointer to accept the SCL high period
519 * @param low_period Pointer to accept the SCL low period
520 *
521 * @return None
522 */
i2c_ll_get_scl_timing(i2c_dev_t * hw,int * high_period,int * low_period)523 static inline void i2c_ll_get_scl_timing(i2c_dev_t *hw, int *high_period, int *low_period)
524 {
525 *high_period = hw->scl_high_period.period;
526 *low_period = hw->scl_low_period.period;
527 }
528
529 /**
530 * @brief Write the I2C hardware txFIFO
531 *
532 * @param hw Beginning address of the peripheral registers
533 * @param ptr Pointer to data buffer
534 * @param len Amount of data needs to be writen
535 *
536 * @return None.
537 */
i2c_ll_write_txfifo(i2c_dev_t * hw,uint8_t * ptr,uint8_t len)538 static inline void i2c_ll_write_txfifo(i2c_dev_t *hw, uint8_t *ptr, uint8_t len)
539 {
540 uint32_t fifo_addr = (hw == &I2C0) ? 0x6001301c : 0x6002701c;
541 for(int i = 0; i < len; i++) {
542 WRITE_PERI_REG(fifo_addr, ptr[i]);
543 }
544 }
545
546 /**
547 * @brief Read the I2C hardware rxFIFO
548 *
549 * @param hw Beginning address of the peripheral registers
550 * @param ptr Pointer to data buffer
551 * @param len Amount of data needs read
552 *
553 * @return None
554 */
i2c_ll_read_rxfifo(i2c_dev_t * hw,uint8_t * ptr,uint8_t len)555 static inline void i2c_ll_read_rxfifo(i2c_dev_t *hw, uint8_t *ptr, uint8_t len)
556 {
557 for(int i = 0; i < len; i++) {
558 ptr[i] = hw->fifo_data.data;
559 }
560 }
561
562 /**
563 * @brief Configure I2C hardware filter
564 *
565 * @param hw Beginning address of the peripheral registers
566 * @param filter_num If the glitch period on the line is less than this value, it can be filtered out
567 * If `filter_num == 0`, the filter will be disabled
568 *
569 * @return None
570 */
i2c_ll_set_filter(i2c_dev_t * hw,uint8_t filter_num)571 static inline void i2c_ll_set_filter(i2c_dev_t *hw, uint8_t filter_num)
572 {
573 if(filter_num > 0) {
574 hw->scl_filter_cfg.thres = filter_num;
575 hw->sda_filter_cfg.thres = filter_num;
576 hw->scl_filter_cfg.en = 1;
577 hw->sda_filter_cfg.en = 1;
578 } else {
579 hw->scl_filter_cfg.en = 0;
580 hw->sda_filter_cfg.en = 0;
581 }
582 }
583
584 /**
585 * @brief Get I2C hardware filter configuration
586 *
587 * @param hw Beginning address of the peripheral registers
588 *
589 * @return The hardware filter configuration
590 */
i2c_ll_get_filter(i2c_dev_t * hw)591 static inline uint8_t i2c_ll_get_filter(i2c_dev_t *hw)
592 {
593 return hw->sda_filter_cfg.thres;
594 }
595
596 /**
597 * @brief Enable I2C master TX interrupt
598 *
599 * @param hw Beginning address of the peripheral registers
600 *
601 * @return None
602 */
i2c_ll_master_enable_tx_it(i2c_dev_t * hw)603 static inline void i2c_ll_master_enable_tx_it(i2c_dev_t *hw)
604 {
605 hw->int_clr.val = ~0;
606 hw->int_ena.val = I2C_LL_MASTER_TX_INT;
607 }
608
609 /**
610 * @brief Enable I2C master RX interrupt
611 *
612 * @param hw Beginning address of the peripheral registers
613 *
614 * @return None
615 */
i2c_ll_master_enable_rx_it(i2c_dev_t * hw)616 static inline void i2c_ll_master_enable_rx_it(i2c_dev_t *hw)
617 {
618 hw->int_clr.val = ~0;
619 hw->int_ena.val = I2C_LL_MASTER_RX_INT;
620 }
621
622 /**
623 * @brief Disable I2C master TX interrupt
624 *
625 * @param hw Beginning address of the peripheral registers
626 *
627 * @return None
628 */
i2c_ll_master_disable_tx_it(i2c_dev_t * hw)629 static inline void i2c_ll_master_disable_tx_it(i2c_dev_t *hw)
630 {
631 hw->int_ena.val &= (~I2C_LL_MASTER_TX_INT);
632 }
633
634 /**
635 * @brief Disable I2C master RX interrupt
636 *
637 * @param hw Beginning address of the peripheral registers
638 *
639 * @return None
640 */
i2c_ll_master_disable_rx_it(i2c_dev_t * hw)641 static inline void i2c_ll_master_disable_rx_it(i2c_dev_t *hw)
642 {
643 hw->int_ena.val &= (~I2C_LL_MASTER_RX_INT);
644 }
645
646 /**
647 * @brief Clear I2C master TX interrupt status register
648 *
649 * @param hw Beginning address of the peripheral registers
650 *
651 * @return None
652 */
i2c_ll_master_clr_tx_it(i2c_dev_t * hw)653 static inline void i2c_ll_master_clr_tx_it(i2c_dev_t *hw)
654 {
655 hw->int_clr.val = I2C_LL_MASTER_TX_INT;
656 }
657
658 /**
659 * @brief Clear I2C master RX interrupt status register
660 *
661 * @param hw Beginning address of the peripheral registers
662 *
663 * @return None
664 */
i2c_ll_master_clr_rx_it(i2c_dev_t * hw)665 static inline void i2c_ll_master_clr_rx_it(i2c_dev_t *hw)
666 {
667 hw->int_clr.val = I2C_LL_MASTER_RX_INT;
668 }
669
670 /**
671 * @brief
672 *
673 * @param hw Beginning address of the peripheral registers
674 *
675 * @return None
676 */
i2c_ll_slave_enable_tx_it(i2c_dev_t * hw)677 static inline void i2c_ll_slave_enable_tx_it(i2c_dev_t *hw)
678 {
679 hw->int_ena.val |= I2C_LL_SLAVE_TX_INT;
680 }
681
682 /**
683 * @brief Enable I2C slave RX interrupt
684 *
685 * @param hw Beginning address of the peripheral registers
686 *
687 * @return None
688 */
i2c_ll_slave_enable_rx_it(i2c_dev_t * hw)689 static inline void i2c_ll_slave_enable_rx_it(i2c_dev_t *hw)
690 {
691 hw->int_ena.val |= I2C_LL_SLAVE_RX_INT;
692 }
693
694 /**
695 * @brief Disable I2C slave TX interrupt
696 *
697 * @param hw Beginning address of the peripheral registers
698 *
699 * @return None
700 */
i2c_ll_slave_disable_tx_it(i2c_dev_t * hw)701 static inline void i2c_ll_slave_disable_tx_it(i2c_dev_t *hw)
702 {
703 hw->int_ena.val &= (~I2C_LL_SLAVE_TX_INT);
704 }
705
706 /**
707 * @brief Disable I2C slave RX interrupt
708 *
709 * @param hw Beginning address of the peripheral registers
710 *
711 * @return None
712 */
i2c_ll_slave_disable_rx_it(i2c_dev_t * hw)713 static inline void i2c_ll_slave_disable_rx_it(i2c_dev_t *hw)
714 {
715 hw->int_ena.val &= (~I2C_LL_SLAVE_RX_INT);
716 }
717
718 /**
719 * @brief Clear I2C slave TX interrupt status register
720 *
721 * @param hw Beginning address of the peripheral registers
722 *
723 * @return None
724 */
i2c_ll_slave_clr_tx_it(i2c_dev_t * hw)725 static inline void i2c_ll_slave_clr_tx_it(i2c_dev_t *hw)
726 {
727 hw->int_clr.val = I2C_LL_SLAVE_TX_INT;
728 }
729
730 /**
731 * @brief Clear I2C slave RX interrupt status register.
732 *
733 * @param hw Beginning address of the peripheral registers
734 *
735 * @return None
736 */
i2c_ll_slave_clr_rx_it(i2c_dev_t * hw)737 static inline void i2c_ll_slave_clr_rx_it(i2c_dev_t *hw)
738 {
739 hw->int_clr.val = I2C_LL_SLAVE_RX_INT;
740 }
741
742 /**
743 * @brief Reste I2C master FSM. When the master FSM is stuck, call this function to reset the FSM
744 *
745 * @param hw Beginning address of the peripheral registers
746 *
747 * @return None
748 */
i2c_ll_master_fsm_rst(i2c_dev_t * hw)749 static inline void i2c_ll_master_fsm_rst(i2c_dev_t *hw)
750 {
751 ;//ESP32 do not support
752 }
753
754 /**
755 * @brief Clear I2C bus, when the slave is stuck in a deadlock and keeps pulling the bus low,
756 * master can controls the SCL bus to generate 9 CLKs.
757 *
758 * Note: The master cannot detect if deadlock happens, but when the scl_st_to interrupt is generated, a deadlock may occur.
759 *
760 * @param hw Beginning address of the peripheral registers
761 *
762 * @return None
763 */
i2c_ll_master_clr_bus(i2c_dev_t * hw)764 static inline void i2c_ll_master_clr_bus(i2c_dev_t *hw)
765 {
766 ;//ESP32 do not support
767 }
768
769 /**
770 * @brief Set I2C source clock
771 *
772 * @param hw Beginning address of the peripheral registers
773 * @param src_clk Source clock of the I2C
774 *
775 * @return None
776 */
i2c_ll_set_source_clk(i2c_dev_t * hw,i2c_sclk_t src_clk)777 static inline void i2c_ll_set_source_clk(i2c_dev_t *hw, i2c_sclk_t src_clk)
778 {
779 ;//Not support on ESP32
780 }
781
782 /**
783 * @brief Get I2C master interrupt event
784 *
785 * @param hw Beginning address of the peripheral registers
786 * @param event Pointer to accept the interrupt event
787 *
788 * @return None
789 */
i2c_ll_master_get_event(i2c_dev_t * hw,i2c_intr_event_t * event)790 static inline void i2c_ll_master_get_event(i2c_dev_t *hw, i2c_intr_event_t *event)
791 {
792 typeof(hw->int_status) int_sts = hw->int_status;
793 if (int_sts.arbitration_lost) {
794 *event = I2C_INTR_EVENT_ARBIT_LOST;
795 } else if (int_sts.ack_err) {
796 *event = I2C_INTR_EVENT_NACK;
797 } else if (int_sts.time_out) {
798 *event = I2C_INTR_EVENT_TOUT;
799 } else if (int_sts.end_detect) {
800 *event = I2C_INTR_EVENT_END_DET;
801 } else if (int_sts.trans_complete) {
802 *event = I2C_INTR_EVENT_TRANS_DONE;
803 } else {
804 *event = I2C_INTR_EVENT_ERR;
805 }
806 }
807
808 /**
809 * @brief Get I2C slave interrupt event
810 *
811 * @param hw Beginning address of the peripheral registers
812 * @param event Pointer to accept the interrupt event
813 *
814 * @return None
815 */
i2c_ll_slave_get_event(i2c_dev_t * hw,i2c_intr_event_t * event)816 static inline void i2c_ll_slave_get_event(i2c_dev_t *hw, i2c_intr_event_t *event)
817 {
818 typeof(hw->int_status) int_sts = hw->int_status;
819 if (int_sts.tx_fifo_empty) {
820 *event = I2C_INTR_EVENT_TXFIFO_EMPTY;
821 } else if (int_sts.trans_complete) {
822 *event = I2C_INTR_EVENT_TRANS_DONE;
823 } else if (int_sts.rx_fifo_full) {
824 *event = I2C_INTR_EVENT_RXFIFO_FULL;
825 } else {
826 *event = I2C_INTR_EVENT_ERR;
827 }
828 }
829
830 /**
831 * @brief Init I2C master
832 *
833 * @param hw Beginning address of the peripheral registers
834 *
835 * @return None
836 */
i2c_ll_master_init(i2c_dev_t * hw)837 static inline void i2c_ll_master_init(i2c_dev_t *hw)
838 {
839 typeof(hw->ctr) ctrl_reg;
840 ctrl_reg.val = 0;
841 ctrl_reg.ms_mode = 1;
842 ctrl_reg.sda_force_out = 1;
843 ctrl_reg.scl_force_out = 1;
844 hw->ctr.val = ctrl_reg.val;
845 }
846
847 /**
848 * @brief Init I2C slave
849 *
850 * @param hw Beginning address of the peripheral registers
851 *
852 * @return None
853 */
i2c_ll_slave_init(i2c_dev_t * hw)854 static inline void i2c_ll_slave_init(i2c_dev_t *hw)
855 {
856 typeof(hw->ctr) ctrl_reg;
857 ctrl_reg.val = 0;
858 ctrl_reg.sda_force_out = 1;
859 ctrl_reg.scl_force_out = 1;
860 hw->ctr.val = ctrl_reg.val;
861 hw->fifo_conf.fifo_addr_cfg_en = 0;
862 }
863
864 /**
865 * @brief Update I2C configuration
866 *
867 * @param hw Beginning address of the peripheral registers
868 *
869 * @return None
870 */
i2c_ll_update(i2c_dev_t * hw)871 static inline void i2c_ll_update(i2c_dev_t *hw)
872 {
873 ;// ESP32 do not support
874 }
875
876 #ifdef __cplusplus
877 }
878 #endif
879