1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stddef.h>
8 #include "hal/i2c_hal.h"
9 #include "hal/i2c_ll.h"
10 #include "hal/i2c_types.h"
11 
12 #if SOC_I2C_SUPPORT_SLAVE
13 
i2c_hal_slave_init(i2c_hal_context_t * hal)14 void i2c_hal_slave_init(i2c_hal_context_t *hal)
15 {
16     i2c_ll_slave_init(hal->dev);
17     //Use fifo mode
18     i2c_ll_set_fifo_mode(hal->dev, true);
19     //MSB
20     i2c_ll_set_data_mode(hal->dev, I2C_DATA_MODE_MSB_FIRST, I2C_DATA_MODE_MSB_FIRST);
21     //Reset fifo
22     i2c_ll_txfifo_rst(hal->dev);
23     i2c_ll_rxfifo_rst(hal->dev);
24 }
25 #endif
26 
i2c_hal_set_bus_timing(i2c_hal_context_t * hal,int scl_freq,i2c_clock_source_t src_clk,int source_freq)27 void i2c_hal_set_bus_timing(i2c_hal_context_t *hal, int scl_freq, i2c_clock_source_t src_clk, int source_freq)
28 {
29     i2c_ll_set_source_clk(hal->dev, src_clk);
30     i2c_hal_clk_config_t clk_cal = {0};
31     i2c_ll_cal_bus_clk(source_freq, scl_freq, &clk_cal);
32     i2c_ll_set_bus_timing(hal->dev, &clk_cal);
33 }
34 
i2c_hal_master_fsm_rst(i2c_hal_context_t * hal)35 void i2c_hal_master_fsm_rst(i2c_hal_context_t *hal)
36 {
37     i2c_ll_master_fsm_rst(hal->dev);
38 }
39 
i2c_hal_master_init(i2c_hal_context_t * hal)40 void i2c_hal_master_init(i2c_hal_context_t *hal)
41 {
42     i2c_ll_master_init(hal->dev);
43     //Use fifo mode
44     i2c_ll_set_fifo_mode(hal->dev, true);
45     //MSB
46     i2c_ll_set_data_mode(hal->dev, I2C_DATA_MODE_MSB_FIRST, I2C_DATA_MODE_MSB_FIRST);
47     //Reset fifo
48     i2c_ll_txfifo_rst(hal->dev);
49     i2c_ll_rxfifo_rst(hal->dev);
50 }
51 
i2c_hal_init(i2c_hal_context_t * hal,int i2c_port)52 void i2c_hal_init(i2c_hal_context_t *hal, int i2c_port)
53 {
54     if (hal->dev == NULL) {
55         hal->dev = I2C_LL_GET_HW(i2c_port);
56         i2c_ll_enable_controller_clock(hal->dev, true);
57     }
58 }
59 
i2c_hal_deinit(i2c_hal_context_t * hal)60 void i2c_hal_deinit(i2c_hal_context_t *hal)
61 {
62     i2c_ll_enable_controller_clock(hal->dev, false);
63     hal->dev = NULL;
64 }
65 
i2c_hal_master_trans_start(i2c_hal_context_t * hal)66 void i2c_hal_master_trans_start(i2c_hal_context_t *hal)
67 {
68     i2c_ll_update(hal->dev);
69     i2c_ll_trans_start(hal->dev);
70 }
71 
i2c_hal_get_timing_config(i2c_hal_context_t * hal,i2c_hal_timing_config_t * timing_config)72 void i2c_hal_get_timing_config(i2c_hal_context_t *hal, i2c_hal_timing_config_t *timing_config)
73 {
74     i2c_ll_get_scl_clk_timing(hal->dev, &timing_config->high_period, &timing_config->low_period, &timing_config->wait_high_period);
75     i2c_ll_get_start_timing(hal->dev, &timing_config->rstart_setup, &timing_config->start_hold);
76     i2c_ll_get_stop_timing(hal->dev, &timing_config->stop_setup, &timing_config->stop_hold);
77     i2c_ll_get_sda_timing(hal->dev, &timing_config->sda_sample, &timing_config->sda_hold);
78     i2c_ll_get_tout(hal->dev, &timing_config->timeout);
79 }
80 
i2c_hal_set_timing_config(i2c_hal_context_t * hal,i2c_hal_timing_config_t * timing_config)81 void i2c_hal_set_timing_config(i2c_hal_context_t *hal, i2c_hal_timing_config_t *timing_config)
82 {
83     i2c_ll_set_scl_clk_timing(hal->dev, timing_config->high_period, timing_config->low_period, timing_config->wait_high_period);
84     i2c_ll_set_start_timing(hal->dev, timing_config->rstart_setup, timing_config->start_hold);
85     i2c_ll_set_stop_timing(hal->dev, timing_config->stop_setup, timing_config->stop_hold);
86     i2c_ll_set_sda_timing(hal->dev, timing_config->sda_sample, timing_config->sda_hold);
87     i2c_ll_set_tout(hal->dev, timing_config->timeout);
88 }
89