Skip to content

Commit 3fb7461

Browse files
committed
refactor(i2c): rename some LL functions according to TRM descriptions
1 parent 5c9e767 commit 3fb7461

File tree

17 files changed

+162
-144
lines changed

17 files changed

+162
-144
lines changed

components/driver/i2c/i2c.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ esp_err_t i2c_param_config(i2c_port_t i2c_num, const i2c_config_t *i2c_conf)
810810
#if SOC_I2C_SUPPORT_SLAVE
811811
if (i2c_conf->mode == I2C_MODE_SLAVE) { //slave mode
812812
i2c_hal_slave_init(&(i2c_context[i2c_num].hal));
813-
i2c_ll_slave_tx_auto_start_en(i2c_context[i2c_num].hal.dev, true);
813+
i2c_ll_slave_enable_auto_start(i2c_context[i2c_num].hal.dev, true);
814814
I2C_CLOCK_SRC_ATOMIC() {
815815
i2c_ll_set_source_clk(i2c_context[i2c_num].hal.dev, src_clk);
816816
}
@@ -1504,7 +1504,7 @@ static void IRAM_ATTR i2c_master_cmd_begin_static(i2c_port_t i2c_num, BaseType_t
15041504
}
15051505
}
15061506
i2c_ll_update(i2c_context[i2c_num].hal.dev);
1507-
i2c_ll_master_trans_start(i2c_context[i2c_num].hal.dev);
1507+
i2c_ll_start_trans(i2c_context[i2c_num].hal.dev);
15081508
return;
15091509
}
15101510

components/esp_driver_i2c/i2c_master.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ static esp_err_t s_i2c_hw_fsm_reset(i2c_master_bus_handle_t i2c_master)
130130
}
131131

132132
i2c_hal_master_init(hal);
133+
i2c_ll_enable_fifo_mode(hal->dev, true);
133134
i2c_ll_disable_intr_mask(hal->dev, I2C_LL_INTR_MASK);
134135
i2c_ll_clear_intr_mask(hal->dev, I2C_LL_INTR_MASK);
135136
i2c_hal_set_timing_config(hal, &timing_config);

components/esp_driver_i2c/i2c_slave.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,11 @@ esp_err_t i2c_new_slave_device(const i2c_slave_config_t *slave_config, i2c_slave
247247

248248
#if SOC_I2C_SLAVE_SUPPORT_I2CRAM_ACCESS
249249
if (i2c_slave->fifo_mode == I2C_SLAVE_NONFIFO) {
250-
i2c_ll_slave_set_fifo_mode(hal->dev, false);
251-
i2c_ll_enable_mem_access_nonfifo(hal->dev, true);
250+
i2c_ll_enable_fifo_mode(hal->dev, false);
251+
i2c_ll_slave_enable_dual_addressing_mode(hal->dev, true);
252252
} else {
253-
i2c_ll_slave_set_fifo_mode(hal->dev, true);
254-
i2c_ll_enable_mem_access_nonfifo(hal->dev, false);
253+
i2c_ll_enable_fifo_mode(hal->dev, true);
254+
i2c_ll_slave_enable_dual_addressing_mode(hal->dev, false);
255255
}
256256
#endif
257257

@@ -273,7 +273,7 @@ esp_err_t i2c_new_slave_device(const i2c_slave_config_t *slave_config, i2c_slave
273273
#if SOC_I2C_SLAVE_CAN_GET_STRETCH_CAUSE
274274
i2c_ll_slave_enable_scl_stretch(hal->dev, slave_config->flags.stretch_en);
275275
#endif
276-
i2c_ll_slave_tx_auto_start_en(hal->dev, true);
276+
i2c_ll_slave_enable_auto_start(hal->dev, true);
277277

278278
i2c_ll_update(hal->dev);
279279
portEXIT_CRITICAL(&i2c_slave->base->spinlock);
@@ -386,7 +386,7 @@ esp_err_t i2c_slave_read_ram(i2c_slave_dev_handle_t i2c_slave, uint8_t ram_offse
386386
portEXIT_CRITICAL(&i2c_slave->base->spinlock);
387387
return ESP_ERR_INVALID_SIZE;
388388
}
389-
i2c_ll_read_by_nonfifo(hal->dev, ram_offset, data, fifo_size);
389+
i2c_ll_read_rx_by_nonfifo(hal->dev, ram_offset, data, fifo_size);
390390
portEXIT_CRITICAL(&i2c_slave->base->spinlock);
391391

392392
return ESP_OK;
@@ -407,7 +407,7 @@ esp_err_t i2c_slave_write_ram(i2c_slave_dev_handle_t i2c_slave, uint8_t ram_offs
407407
ESP_EARLY_LOGE(TAG, "No extra fifo to fill your buffer, please split your buffer");
408408
return ESP_ERR_INVALID_SIZE;
409409
}
410-
i2c_ll_write_by_nonfifo(hal->dev, ram_offset, data, size);
410+
i2c_ll_write_tx_by_nonfifo(hal->dev, ram_offset, data, size);
411411
xSemaphoreGive(i2c_slave->slv_tx_mux);
412412
return ESP_OK;
413413
}

components/esp_driver_i2c/i2c_slave_v2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ esp_err_t i2c_new_slave_device(const i2c_slave_config_t *slave_config, i2c_slave
290290

291291
portENTER_CRITICAL(&i2c_slave->base->spinlock);
292292
i2c_hal_slave_init(hal);
293-
i2c_ll_slave_set_fifo_mode(hal->dev, true);
293+
i2c_ll_enable_fifo_mode(hal->dev, true);
294294
i2c_ll_set_slave_addr(hal->dev, slave_config->slave_addr, false);
295295
i2c_ll_set_tout(hal->dev, I2C_LL_MAX_TIMEOUT);
296296

components/hal/esp32/include/hal/i2c_ll.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ static inline void i2c_ll_get_intr_mask(i2c_dev_t *hw, uint32_t *intr_status)
280280
*
281281
* @return None
282282
*/
283-
static inline void i2c_ll_slave_set_fifo_mode(i2c_dev_t *hw, bool fifo_mode_en)
283+
static inline void i2c_ll_enable_fifo_mode(i2c_dev_t *hw, bool fifo_mode_en)
284284
{
285285
hw->fifo_conf.nonfifo_en = fifo_mode_en ? 0 : 1;
286286
}
@@ -299,7 +299,7 @@ static inline void i2c_ll_set_tout(i2c_dev_t *hw, int tout)
299299
}
300300

301301
/**
302-
* @brief Configure I2C slave broadcasting mode.
302+
* @brief Enable the I2C slave to respond to broadcast address
303303
*
304304
* @param hw Beginning address of the peripheral registers
305305
* @param broadcast_en Set true to enable broadcast, else, set it false
@@ -509,7 +509,7 @@ static inline void i2c_ll_get_tout(i2c_dev_t *hw, int *timeout)
509509
* @return None
510510
*/
511511
__attribute__((always_inline))
512-
static inline void i2c_ll_master_trans_start(i2c_dev_t *hw)
512+
static inline void i2c_ll_start_trans(i2c_dev_t *hw)
513513
{
514514
hw->ctr.trans_start = 1;
515515
}
@@ -708,7 +708,6 @@ static inline void i2c_ll_slave_init(i2c_dev_t *hw)
708708
ctrl_reg.sda_force_out = 1;
709709
ctrl_reg.scl_force_out = 1;
710710
hw->ctr.val = ctrl_reg.val;
711-
hw->fifo_conf.fifo_addr_cfg_en = 0;
712711
}
713712

714713
/**
@@ -770,12 +769,12 @@ static inline void i2c_ll_reset_register(int i2c_port)
770769
#define i2c_ll_reset_register(...) do {(void)__DECLARE_RCC_ATOMIC_ENV; i2c_ll_reset_register(__VA_ARGS__);} while(0)
771770

772771
/**
773-
* @brief Set whether slave should auto start, or only start with start signal from master
772+
* @brief Enable I2C slave to automatically send data when addressed by the master
774773
*
775774
* @param hw Beginning address of the peripheral registers
776775
* @param slv_ex_auto_en 1 if slave auto start data transaction, otherwise, 0.
777776
*/
778-
static inline void i2c_ll_slave_tx_auto_start_en(i2c_dev_t *hw, bool slv_ex_auto_en)
777+
static inline void i2c_ll_slave_enable_auto_start(i2c_dev_t *hw, bool slv_ex_auto_en)
779778
{
780779
;// ESP32 do not support
781780
}

components/hal/esp32c2/include/hal/i2c_ll.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ typedef enum {
7676
*/
7777
static inline void i2c_ll_master_cal_bus_clk(uint32_t source_clk, uint32_t bus_freq, i2c_hal_clk_config_t *clk_cal)
7878
{
79-
uint32_t clkm_div = source_clk / (bus_freq * 1024) +1;
79+
uint32_t clkm_div = source_clk / (bus_freq * 1024) + 1;
8080
uint32_t sclk_freq = source_clk / clkm_div;
8181
uint32_t half_cycle = sclk_freq / bus_freq / 2;
8282
//SCL
@@ -85,7 +85,7 @@ static inline void i2c_ll_master_cal_bus_clk(uint32_t source_clk, uint32_t bus_f
8585
// default, scl_wait_high < scl_high
8686
// Make 80KHz as a boundary here, because when working at lower frequency, too much scl_wait_high will faster the frequency
8787
// according to some hardware behaviors.
88-
clk_cal->scl_wait_high = (bus_freq >= 80*1000) ? (half_cycle / 2 - 2) : (half_cycle / 4);
88+
clk_cal->scl_wait_high = (bus_freq >= 80 * 1000) ? (half_cycle / 2 - 2) : (half_cycle / 4);
8989
clk_cal->scl_high = half_cycle - clk_cal->scl_wait_high;
9090
clk_cal->sda_hold = half_cycle / 4;
9191
clk_cal->sda_sample = half_cycle / 2;
@@ -326,7 +326,7 @@ static inline void i2c_ll_get_intr_mask(i2c_dev_t *hw, uint32_t *intr_status)
326326
*
327327
* @return None
328328
*/
329-
static inline void i2c_ll_slave_set_fifo_mode(i2c_dev_t *hw, bool fifo_mode_en)
329+
static inline void i2c_ll_enable_fifo_mode(i2c_dev_t *hw, bool fifo_mode_en)
330330
{
331331
hw->fifo_conf.nonfifo_en = fifo_mode_en ? 0 : 1;
332332
}
@@ -414,6 +414,7 @@ static inline void i2c_ll_set_sda_timing(i2c_dev_t *hw, int sda_sample, int sda_
414414
*/
415415
static inline void i2c_ll_set_txfifo_empty_thr(i2c_dev_t *hw, uint8_t empty_thr)
416416
{
417+
hw->fifo_conf.fifo_prt_en = 1;
417418
hw->fifo_conf.txfifo_wm_thrhd = empty_thr;
418419
}
419420

@@ -427,6 +428,8 @@ static inline void i2c_ll_set_txfifo_empty_thr(i2c_dev_t *hw, uint8_t empty_thr)
427428
*/
428429
static inline void i2c_ll_set_rxfifo_full_thr(i2c_dev_t *hw, uint8_t full_thr)
429430
{
431+
hw->fifo_conf.fifo_prt_en = 1;
432+
hw->ctr.rx_full_ack_level = 0;
430433
hw->fifo_conf.rxfifo_wm_thrhd = full_thr;
431434
}
432435

@@ -543,7 +546,7 @@ static inline void i2c_ll_get_tout(i2c_dev_t *hw, int *timeout)
543546
* @return None
544547
*/
545548
__attribute__((always_inline))
546-
static inline void i2c_ll_master_trans_start(i2c_dev_t *hw)
549+
static inline void i2c_ll_start_trans(i2c_dev_t *hw)
547550
{
548551
hw->ctr.trans_start = 1;
549552
}
@@ -590,8 +593,8 @@ static inline void i2c_ll_get_stop_timing(i2c_dev_t *hw, int *setup_time, int *h
590593
__attribute__((always_inline))
591594
static inline void i2c_ll_write_txfifo(i2c_dev_t *hw, const uint8_t *ptr, uint8_t len)
592595
{
593-
for (int i = 0; i< len; i++) {
594-
HAL_FORCE_MODIFY_U32_REG_FIELD(hw->data, fifo_rdata, ptr[i]);
596+
for (int i = 0; i < len; i++) {
597+
hw->data.val = ptr[i];
595598
}
596599
}
597600

@@ -607,7 +610,7 @@ static inline void i2c_ll_write_txfifo(i2c_dev_t *hw, const uint8_t *ptr, uint8_
607610
__attribute__((always_inline))
608611
static inline void i2c_ll_read_rxfifo(i2c_dev_t *hw, uint8_t *ptr, uint8_t len)
609612
{
610-
for(int i = 0; i < len; i++) {
613+
for (int i = 0; i < len; i++) {
611614
ptr[i] = HAL_FORCE_READ_U32_REG_FIELD(hw->data, fifo_rdata);
612615
}
613616
}

components/hal/esp32c3/include/hal/i2c_ll.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ static inline void i2c_ll_get_intr_mask(i2c_dev_t *hw, uint32_t *intr_status)
342342
*
343343
* @return None
344344
*/
345-
static inline void i2c_ll_slave_set_fifo_mode(i2c_dev_t *hw, bool fifo_mode_en)
345+
static inline void i2c_ll_enable_fifo_mode(i2c_dev_t *hw, bool fifo_mode_en)
346346
{
347347
hw->fifo_conf.nonfifo_en = fifo_mode_en ? 0 : 1;
348348
}
@@ -361,7 +361,7 @@ static inline void i2c_ll_set_tout(i2c_dev_t *hw, int tout)
361361
}
362362

363363
/**
364-
* @brief Configure I2C slave broadcasting mode.
364+
* @brief Enable the I2C slave to respond to broadcast address
365365
*
366366
* @param hw Beginning address of the peripheral registers
367367
* @param broadcast_en Set true to enable broadcast, else, set it false
@@ -495,6 +495,7 @@ static inline void i2c_ll_set_sda_timing(i2c_dev_t *hw, int sda_sample, int sda_
495495
*/
496496
static inline void i2c_ll_set_txfifo_empty_thr(i2c_dev_t *hw, uint8_t empty_thr)
497497
{
498+
hw->fifo_conf.fifo_prt_en = 1;
498499
hw->fifo_conf.tx_fifo_wm_thrhd = empty_thr;
499500
}
500501

@@ -509,6 +510,7 @@ static inline void i2c_ll_set_txfifo_empty_thr(i2c_dev_t *hw, uint8_t empty_thr)
509510
static inline void i2c_ll_set_rxfifo_full_thr(i2c_dev_t *hw, uint8_t full_thr)
510511
{
511512
hw->fifo_conf.fifo_prt_en = 1;
513+
hw->ctr.rx_full_ack_level = 0;
512514
hw->fifo_conf.rx_fifo_wm_thrhd = full_thr;
513515
}
514516

@@ -625,7 +627,7 @@ static inline void i2c_ll_get_tout(i2c_dev_t *hw, int *timeout)
625627
* @return None
626628
*/
627629
__attribute__((always_inline))
628-
static inline void i2c_ll_master_trans_start(i2c_dev_t *hw)
630+
static inline void i2c_ll_start_trans(i2c_dev_t *hw)
629631
{
630632
hw->ctr.trans_start = 1;
631633
}
@@ -673,7 +675,7 @@ __attribute__((always_inline))
673675
static inline void i2c_ll_write_txfifo(i2c_dev_t *hw, const uint8_t *ptr, uint8_t len)
674676
{
675677
for (int i = 0; i < len; i++) {
676-
HAL_FORCE_MODIFY_U32_REG_FIELD(hw->fifo_data, data, ptr[i]);
678+
hw->fifo_data.val = ptr[i];
677679
}
678680
}
679681

@@ -695,44 +697,46 @@ static inline void i2c_ll_read_rxfifo(i2c_dev_t *hw, uint8_t *ptr, uint8_t len)
695697
}
696698

697699
/**
698-
* @brief Write the I2C hardware txFIFO
700+
* @brief Write to the TX RAM by direct address
699701
*
700702
* @param hw Beginning address of the peripheral registers
701703
* @param ram_offset Offset value of I2C RAM.
702704
* @param ptr Pointer to data buffer
703705
* @param len Amount of data needs to be written
704706
*/
705-
static inline void i2c_ll_write_by_nonfifo(i2c_dev_t *hw, uint8_t ram_offset, const uint8_t *ptr, uint8_t len)
707+
static inline void i2c_ll_write_tx_by_nonfifo(i2c_dev_t *hw, uint8_t ram_offset, const uint8_t *ptr, uint8_t len)
706708
{
707709
for (int i = 0; i < len; i++) {
708710
hw->txfifo_mem[i + ram_offset] = ptr[i];
709711
}
710712
}
711713

712714
/**
713-
* @brief Read the I2C hardware ram
715+
* @brief Read from the RX RAM by direct address
714716
*
715717
* @param hw Beginning address of the peripheral registers
716718
* @param ram_offset Offset value of I2C RAM.
717719
* @param ptr Pointer to data buffer
718720
* @param len Amount of data needs read
719721
*/
720-
static inline void i2c_ll_read_by_nonfifo(i2c_dev_t *hw, uint8_t ram_offset, uint8_t *ptr, uint8_t len)
722+
static inline void i2c_ll_read_rx_by_nonfifo(i2c_dev_t *hw, uint8_t ram_offset, uint8_t *ptr, uint8_t len)
721723
{
722724
for (int i = 0; i < len; i++) {
723725
ptr[i] = hw->rxfifo_mem[i + ram_offset];
724726
}
725727
}
726728

727729
/**
728-
* @brief Get access to I2C RAM address directly
730+
* @brief Enable I2C slave dual addressing mode
731+
*
732+
* @note When enable the dual addressing mode, I2C RAM must be accessed in non-FIFO mode
729733
*
730734
* @param hw Beginning address of the peripheral registers
731735
* @param addr_wr_en Enable I2C ram address read and write
732736
*
733737
* @return None
734738
*/
735-
static inline void i2c_ll_enable_mem_access_nonfifo(i2c_dev_t *hw, bool addr_wr_en)
739+
static inline void i2c_ll_slave_enable_dual_addressing_mode(i2c_dev_t *hw, bool addr_wr_en)
736740
{
737741
hw->fifo_conf.fifo_addr_cfg_en = addr_wr_en;
738742
}
@@ -874,16 +878,15 @@ static inline void i2c_ll_slave_init(i2c_dev_t *hw)
874878
ctrl_reg.sda_force_out = 1;
875879
ctrl_reg.scl_force_out = 1;
876880
hw->ctr.val = ctrl_reg.val;
877-
hw->fifo_conf.fifo_addr_cfg_en = 0;
878881
}
879882

880883
/**
881-
* @brief Set whether slave should auto start, or only start with start signal from master
884+
* @brief Enable I2C slave to automatically send data when addressed by the master
882885
*
883886
* @param hw Beginning address of the peripheral registers
884887
* @param slv_ex_auto_en 1 if slave auto start data transaction, otherwise, 0.
885888
*/
886-
static inline void i2c_ll_slave_tx_auto_start_en(i2c_dev_t *hw, bool slv_ex_auto_en)
889+
static inline void i2c_ll_slave_enable_auto_start(i2c_dev_t *hw, bool slv_ex_auto_en)
887890
{
888891
hw->ctr.slv_tx_auto_start_en = slv_ex_auto_en;
889892
}

0 commit comments

Comments
 (0)