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