Skip to content

Commit 9ac6e10

Browse files
committed
Merge branch 'fix/fix_gpio_config_option' into 'master'
fix(i2c): Fix i2c gpio configuration and move i2c_platform public Closes IDF-11117 See merge request espressif/esp-idf!34366
2 parents fa3f6ac + d7e0904 commit 9ac6e10

File tree

9 files changed

+69
-34
lines changed

9 files changed

+69
-34
lines changed

components/esp_driver_i2c/i2c_common.c

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -312,29 +312,27 @@ static esp_err_t s_hp_i2c_pins_config(i2c_bus_handle_t handle)
312312
int port_id = handle->port_num;
313313

314314
// SDA pin configurations
315-
gpio_config_t sda_conf = {
316-
.intr_type = GPIO_INTR_DISABLE,
317-
.mode = GPIO_MODE_INPUT_OUTPUT_OD,
318-
.pull_down_en = false,
319-
.pull_up_en = handle->pull_up_enable ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE,
320-
.pin_bit_mask = 1ULL << handle->sda_num,
321-
};
322315
ESP_RETURN_ON_ERROR(gpio_set_level(handle->sda_num, 1), TAG, "i2c sda pin set level failed");
323-
ESP_RETURN_ON_ERROR(gpio_config(&sda_conf), TAG, "config GPIO failed");
316+
gpio_input_enable(handle->sda_num);
317+
gpio_od_enable(handle->sda_num);
318+
if (handle->pull_up_enable) {
319+
gpio_pullup_en(handle->sda_num);
320+
} else {
321+
gpio_pullup_dis(handle->sda_num);
322+
}
324323
gpio_func_sel(handle->sda_num, PIN_FUNC_GPIO);
325324
esp_rom_gpio_connect_out_signal(handle->sda_num, i2c_periph_signal[port_id].sda_out_sig, 0, 0);
326325
esp_rom_gpio_connect_in_signal(handle->sda_num, i2c_periph_signal[port_id].sda_in_sig, 0);
327326

328327
// SCL pin configurations
329-
gpio_config_t scl_conf = {
330-
.intr_type = GPIO_INTR_DISABLE,
331-
.mode = GPIO_MODE_INPUT_OUTPUT_OD,
332-
.pull_down_en = false,
333-
.pull_up_en = handle->pull_up_enable ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE,
334-
.pin_bit_mask = 1ULL << handle->scl_num,
335-
};
336328
ESP_RETURN_ON_ERROR(gpio_set_level(handle->scl_num, 1), TAG, "i2c scl pin set level failed");
337-
ESP_RETURN_ON_ERROR(gpio_config(&scl_conf), TAG, "config GPIO failed");
329+
gpio_input_enable(handle->scl_num);
330+
gpio_od_enable(handle->scl_num);
331+
if (handle->pull_up_enable) {
332+
gpio_pullup_en(handle->scl_num);
333+
} else {
334+
gpio_pullup_dis(handle->scl_num);
335+
}
338336
gpio_func_sel(handle->scl_num, PIN_FUNC_GPIO);
339337
esp_rom_gpio_connect_out_signal(handle->scl_num, i2c_periph_signal[port_id].scl_out_sig, 0, 0);
340338
esp_rom_gpio_connect_in_signal(handle->scl_num, i2c_periph_signal[port_id].scl_in_sig, 0);
@@ -406,3 +404,28 @@ esp_err_t i2c_common_set_pins(i2c_bus_handle_t handle)
406404

407405
return ret;
408406
}
407+
408+
esp_err_t i2c_common_deinit_pins(i2c_bus_handle_t handle)
409+
{
410+
int port_id = handle->port_num;
411+
412+
if (handle->is_lp_i2c == false) {
413+
ESP_RETURN_ON_ERROR(gpio_output_disable(handle->sda_num), TAG, "disable i2c pins failed");
414+
esp_rom_gpio_connect_in_signal(GPIO_MATRIX_CONST_ZERO_INPUT, i2c_periph_signal[port_id].sda_in_sig, 0);
415+
416+
ESP_RETURN_ON_ERROR(gpio_output_disable(handle->scl_num), TAG, "disable i2c pins failed");
417+
esp_rom_gpio_connect_in_signal(GPIO_MATRIX_CONST_ZERO_INPUT, i2c_periph_signal[port_id].scl_in_sig, 0);
418+
}
419+
#if SOC_LP_I2C_SUPPORTED
420+
else {
421+
ESP_RETURN_ON_ERROR(rtc_gpio_deinit(handle->sda_num), TAG, "deinit rtc gpio failed");
422+
ESP_RETURN_ON_ERROR(rtc_gpio_deinit(handle->scl_num), TAG, "deinit rtc gpio failed");
423+
#if SOC_LP_GPIO_MATRIX_SUPPORTED
424+
lp_gpio_connect_in_signal(GPIO_MATRIX_CONST_ZERO_INPUT, i2c_periph_signal[port_id].scl_in_sig, 0);
425+
lp_gpio_connect_in_signal(GPIO_MATRIX_CONST_ZERO_INPUT, i2c_periph_signal[port_id].sda_in_sig, 0);
426+
#endif
427+
}
428+
#endif
429+
430+
return ESP_OK;
431+
}

components/esp_driver_i2c/i2c_master.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ static esp_err_t i2c_master_bus_destroy(i2c_master_bus_handle_t bus_handle)
784784
{
785785
ESP_RETURN_ON_FALSE(bus_handle, ESP_ERR_INVALID_ARG, TAG, "no memory for i2c master bus");
786786
i2c_master_bus_handle_t i2c_master = bus_handle;
787+
i2c_common_deinit_pins(i2c_master->base);
787788
if (i2c_release_bus_handle(i2c_master->base) == ESP_OK) {
788789
if (i2c_master) {
789790
if (i2c_master->bus_lock_mux) {

components/esp_driver_i2c/i2c_private.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,17 @@ esp_err_t i2c_select_periph_clock(i2c_bus_handle_t handle, soc_module_clk_t clk_
258258
*/
259259
esp_err_t i2c_common_set_pins(i2c_bus_handle_t handle);
260260

261+
/**
262+
* @brief Deinit I2C SCL/SDA pins
263+
*
264+
* @param handle I2C bus handle
265+
* @return
266+
* - ESP_OK: I2C set SCL/SDA pins successfully.
267+
* - ESP_ERR_INVALID_ARG: Argument error.
268+
* - Otherwise: Set SCL/SDA IOs error.
269+
*/
270+
esp_err_t i2c_common_deinit_pins(i2c_bus_handle_t handle);
271+
261272
/**
262273
* @brief Check whether bus is acquired
263274
*

components/esp_driver_i2c/i2c_slave.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ static esp_err_t i2c_slave_bus_destroy(i2c_slave_dev_handle_t i2c_slave)
294294
{
295295
if (i2c_slave) {
296296
i2c_ll_disable_intr_mask(i2c_slave->base->hal.dev, I2C_LL_SLAVE_EVENT_INTR);
297+
i2c_common_deinit_pins(i2c_slave->base);
297298
if (i2c_slave->slv_rx_mux) {
298299
vSemaphoreDeleteWithCaps(i2c_slave->slv_rx_mux);
299300
i2c_slave->slv_rx_mux = NULL;

components/esp_driver_i2c/include/driver/i2c_master.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,22 @@ esp_err_t i2c_master_bus_reset(i2c_master_bus_handle_t bus_handle);
260260
*/
261261
esp_err_t i2c_master_bus_wait_all_done(i2c_master_bus_handle_t bus_handle, int timeout_ms);
262262

263+
/**
264+
* @brief Retrieves the I2C master bus handle for a specified I2C port number.
265+
*
266+
* This function retrieves the I2C master bus handle for the
267+
* given I2C port number. Please make sure the handle has already been initialized, and this
268+
* function would simply returns the existing handle. Note that the returned handle still can't be used concurrently
269+
*
270+
* @param port_num I2C port number for which the handle is to be retrieved.
271+
* @param ret_handle Pointer to a variable where the retrieved handle will be stored.
272+
* @return
273+
* - ESP_OK: Success. The handle is retrieved successfully.
274+
* - ESP_ERR_INVALID_ARG: Invalid argument, such as invalid port number
275+
* - ESP_ERR_INVALID_STATE: Invalid state, such as the I2C port is not initialized.
276+
*/
277+
esp_err_t i2c_master_get_bus_handle(i2c_port_num_t port_num, i2c_master_bus_handle_t *ret_handle);
278+
263279
#ifdef __cplusplus
264280
}
265281
#endif

components/esp_driver_i2c/include/esp_private/i2c_platform.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,7 @@
1515
extern "C" {
1616
#endif
1717

18-
/**
19-
* @brief Retrieves the I2C master bus handle for a specified I2C port number.
20-
*
21-
* This function retrieves the I2C master bus handle for the
22-
* given I2C port number. Please make sure the handle has already been initialized, and this
23-
* function would simply returns the existing handle. Note that the returned handle still can't be used concurrently
24-
*
25-
* @param port_num I2C port number for which the handle is to be retrieved.
26-
* @param ret_handle Pointer to a variable where the retrieved handle will be stored.
27-
* @return
28-
* - ESP_OK: Success. The handle is retrieved successfully.
29-
* - ESP_ERR_INVALID_ARG: Invalid argument, such as invalid port number
30-
* - ESP_ERR_INVALID_STATE: Invalid state, such as the I2C port is not initialized.
31-
*/
32-
esp_err_t i2c_master_get_bus_handle(i2c_port_num_t port_num, i2c_master_bus_handle_t *ret_handle);
18+
// Empty file in order not cause breaking change. Should be removed in next version.
3319

3420
#ifdef __cplusplus
3521
}

components/esp_driver_i2c/test_apps/i2c_test_apps/main/test_i2c_common.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "esp_private/periph_ctrl.h"
2020
#include "driver/gpio.h"
2121
#include "driver/i2c_master.h"
22-
#include "esp_private/i2c_platform.h"
2322
#include "esp_rom_gpio.h"
2423
#include "esp_log.h"
2524
#include "test_utils.h"

docs/en/api-reference/peripherals/i2c.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ When the I2C master handle has been initialized in one module (e.g. the audio mo
166166
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &bus_handle));
167167
168168
// Source File 2
169-
#include "esp_private/i2c_platform.h"
170169
#include "driver/i2c_master.h"
171170
i2c_master_bus_handle_t handle;
172171
ESP_ERROR_CHECK(i2c_master_get_bus_handle(0, &handle));

docs/zh_CN/api-reference/peripherals/i2c.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ I2C 主机设备需要 :cpp:type:`i2c_device_config_t` 指定的配置:
167167
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &bus_handle));
168168
169169
// 源文件 2
170-
#include "esp_private/i2c_platform.h"
171170
#include "driver/i2c_master.h"
172171
i2c_master_bus_handle_t handle;
173172
ESP_ERROR_CHECK(i2c_master_get_bus_handle(0, &handle));

0 commit comments

Comments
 (0)