From 47010efe09344433953d12aaefb5ba56167c948a Mon Sep 17 00:00:00 2001 From: Chen Jichang Date: Thu, 17 Oct 2024 17:29:31 +0800 Subject: [PATCH] refactor(sdm): refactor gpio config mode --- components/esp_driver_sdm/src/sdm.c | 18 ++++++++---------- docs/en/api-reference/peripherals/sdm.rst | 1 - docs/zh_CN/api-reference/peripherals/sdm.rst | 1 - 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/components/esp_driver_sdm/src/sdm.c b/components/esp_driver_sdm/src/sdm.c index d5300a48ec..9675e504b6 100644 --- a/components/esp_driver_sdm/src/sdm.c +++ b/components/esp_driver_sdm/src/sdm.c @@ -29,6 +29,7 @@ #include "soc/sdm_periph.h" #include "esp_private/esp_clk.h" #include "esp_private/io_mux.h" +#include "esp_private/gpio.h" #if CONFIG_SDM_CTRL_FUNC_IN_IRAM #define SDM_MEM_ALLOC_CAPS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) @@ -229,16 +230,12 @@ esp_err_t sdm_new_channel(const sdm_config_t *config, sdm_channel_handle_t *ret_ // SDM clock comes from IO MUX, but IO MUX clock might be shared with other submodules as well ESP_GOTO_ON_ERROR(io_mux_set_clock_source((soc_module_clk_t)(group->clk_src)), err, TAG, "set IO MUX clock source failed"); - // GPIO configuration - gpio_config_t gpio_conf = { - .intr_type = GPIO_INTR_DISABLE, - // also enable the input path is `io_loop_back` is on, this is useful for debug - .mode = GPIO_MODE_OUTPUT | (config->flags.io_loop_back ? GPIO_MODE_INPUT : 0), - .pull_down_en = false, - .pull_up_en = true, - .pin_bit_mask = 1ULL << config->gpio_num, - }; - ESP_GOTO_ON_ERROR(gpio_config(&gpio_conf), err, TAG, "config GPIO failed"); + gpio_func_sel(config->gpio_num, PIN_FUNC_GPIO); + // deprecated, to be removed in in esp-idf v6.0 + if (config->flags.io_loop_back) { + gpio_input_enable(config->gpio_num); + } + // connect the signal to the GPIO by matrix, it will also enable the output path properly esp_rom_gpio_connect_out_signal(config->gpio_num, sigma_delta_periph_signals.channels[chan_id].sd_sig, config->flags.invert_out, false); chan->gpio_num = config->gpio_num; @@ -283,6 +280,7 @@ esp_err_t sdm_del_channel(sdm_channel_handle_t chan) sdm_group_t *group = chan->group; int group_id = group->group_id; int chan_id = chan->chan_id; + gpio_output_disable(chan->gpio_num); ESP_LOGD(TAG, "del channel (%d,%d)", group_id, chan_id); // recycle memory resource ESP_RETURN_ON_ERROR(sdm_destroy(chan), TAG, "destroy channel failed"); diff --git a/docs/en/api-reference/peripherals/sdm.rst b/docs/en/api-reference/peripherals/sdm.rst index 85edb53f73..32dc851fe6 100644 --- a/docs/en/api-reference/peripherals/sdm.rst +++ b/docs/en/api-reference/peripherals/sdm.rst @@ -42,7 +42,6 @@ To install an SDM channel, you should call :cpp:func:`sdm_new_channel` to get a - :cpp:member:`sdm_config_t::clk_src` selects the source clock for the SDM module. Note that, all channels should select the same clock source. - :cpp:member:`sdm_config_t::sample_rate_hz` sets the sample rate of the SDM module. A higher sample rate can help to output signals with higher SNR (Signal to Noise Ratio), and easier to restore the original signal after the filter. - :cpp:member:`sdm_config_t::invert_out` sets whether to invert the output signal. -- :cpp:member:`sdm_config_t::io_loop_back` is for debugging purposes only. It enables both the GPIO's input and output ability through the GPIO matrix peripheral. The function :cpp:func:`sdm_new_channel` can fail due to various errors such as insufficient memory, invalid arguments, etc. Specifically, when there are no more free channels (i.e., all hardware SDM channels have been used up), :c:macro:`ESP_ERR_NOT_FOUND` will be returned. diff --git a/docs/zh_CN/api-reference/peripherals/sdm.rst b/docs/zh_CN/api-reference/peripherals/sdm.rst index 0547cd99c7..5230e67e1a 100644 --- a/docs/zh_CN/api-reference/peripherals/sdm.rst +++ b/docs/zh_CN/api-reference/peripherals/sdm.rst @@ -42,7 +42,6 @@ Sigma-Delta 调制通道通常应用于以下场景: - :cpp:member:`sdm_config_t::clk_src` 选择 SDM 模块的时钟源。注意,所有通道选择的时钟源应保持一致。 - :cpp:member:`sdm_config_t::sample_rate_hz` 设置 SDM 模块的采样率。提高采样率可以提高输出信号的信噪比,更容易在后级通过滤波获取高精度的原始信号。 - :cpp:member:`sdm_config_t::invert_out` 设置是否反转输出信号。 -- :cpp:member:`sdm_config_t::io_loop_back` 通过 GPIO 矩阵外设,启用 GPIO 的输入和输出功能。注意,该字段仅供调试使用。 函数 :cpp:func:`sdm_new_channel` 可能因为各种原因失败,如内存不足、参数无效等。当缺少空闲通道(即所有的硬件 SDM 通道均在使用中)时,将返回 :c:macro:`ESP_ERR_NOT_FOUND`。