From d908150adc8f26ddc49fea9b78496e5d3ce86ea5 Mon Sep 17 00:00:00 2001 From: morris Date: Mon, 1 Apr 2024 14:22:53 +0800 Subject: [PATCH 1/2] fix(rmt): calarify partial receive is not support on esp32 because of lack hardware support --- components/esp_driver_mcpwm/include/driver/mcpwm_cap.h | 2 +- components/esp_driver_rmt/src/rmt_rx.c | 5 ++++- components/esp_driver_rmt/src/rmt_tx.c | 1 - docs/en/api-reference/peripherals/rmt.rst | 2 +- docs/zh_CN/api-reference/peripherals/rmt.rst | 2 +- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/components/esp_driver_mcpwm/include/driver/mcpwm_cap.h b/components/esp_driver_mcpwm/include/driver/mcpwm_cap.h index 0d94f00921..a32cfe40f4 100644 --- a/components/esp_driver_mcpwm/include/driver/mcpwm_cap.h +++ b/components/esp_driver_mcpwm/include/driver/mcpwm_cap.h @@ -85,7 +85,7 @@ esp_err_t mcpwm_capture_timer_disable(mcpwm_cap_timer_handle_t cap_timer); esp_err_t mcpwm_capture_timer_start(mcpwm_cap_timer_handle_t cap_timer); /** - * @brief Start MCPWM capture timer + * @brief Stop MCPWM capture timer * * @param[in] cap_timer MCPWM capture timer, allocated by `mcpwm_new_capture_timer()` * @return diff --git a/components/esp_driver_rmt/src/rmt_rx.c b/components/esp_driver_rmt/src/rmt_rx.c index cc33f75d9f..b7ee610859 100644 --- a/components/esp_driver_rmt/src/rmt_rx.c +++ b/components/esp_driver_rmt/src/rmt_rx.c @@ -286,12 +286,12 @@ esp_err_t rmt_new_rx_channel(const rmt_rx_channel_config_t *config, rmt_channel_ .pull_up_en = true, .pin_bit_mask = BIT64(config->gpio_num), }; + // gpio_config also connects the IO_MUX to the GPIO matrix ESP_GOTO_ON_ERROR(gpio_config(&gpio_conf), err, TAG, "config GPIO failed"); rx_channel->base.gpio_num = config->gpio_num; esp_rom_gpio_connect_in_signal(config->gpio_num, rmt_periph_signals.groups[group_id].channels[channel_id + RMT_RX_CHANNEL_OFFSET_IN_GROUP].rx_sig, config->flags.invert_in); - gpio_func_sel(config->gpio_num, PIN_FUNC_GPIO); // initialize other members of rx channel portMUX_INITIALIZE(&rx_channel->base.spinlock); @@ -355,6 +355,9 @@ esp_err_t rmt_receive(rmt_channel_handle_t channel, void *buffer, size_t buffer_ { ESP_RETURN_ON_FALSE_ISR(channel && buffer && buffer_size && config, ESP_ERR_INVALID_ARG, TAG, "invalid argument"); ESP_RETURN_ON_FALSE_ISR(channel->direction == RMT_CHANNEL_DIRECTION_RX, ESP_ERR_INVALID_ARG, TAG, "invalid channel direction"); +#if !SOC_RMT_SUPPORT_RX_PINGPONG + ESP_RETURN_ON_FALSE_ISR(!config->flags.en_partial_rx, ESP_ERR_NOT_SUPPORTED, TAG, "partial receive not supported"); +#endif rmt_rx_channel_t *rx_chan = __containerof(channel, rmt_rx_channel_t, base); size_t per_dma_block_size = 0; size_t last_dma_block_size = 0; diff --git a/components/esp_driver_rmt/src/rmt_tx.c b/components/esp_driver_rmt/src/rmt_tx.c index 2d59b0017f..1d9e1a06e3 100644 --- a/components/esp_driver_rmt/src/rmt_tx.c +++ b/components/esp_driver_rmt/src/rmt_tx.c @@ -355,7 +355,6 @@ esp_err_t rmt_new_tx_channel(const rmt_tx_channel_config_t *config, rmt_channel_ esp_rom_gpio_connect_out_signal(config->gpio_num, rmt_periph_signals.groups[group_id].channels[channel_id + RMT_TX_CHANNEL_OFFSET_IN_GROUP].tx_sig, config->flags.invert_out, false); - gpio_func_sel(config->gpio_num, PIN_FUNC_GPIO); tx_channel->base.gpio_num = config->gpio_num; portMUX_INITIALIZE(&tx_channel->base.spinlock); diff --git a/docs/en/api-reference/peripherals/rmt.rst b/docs/en/api-reference/peripherals/rmt.rst index 95b3f37520..ef40858192 100644 --- a/docs/en/api-reference/peripherals/rmt.rst +++ b/docs/en/api-reference/peripherals/rmt.rst @@ -331,7 +331,7 @@ As also discussed in the :ref:`rmt-enable-and-disable-channel`, calling :cpp:fun - :cpp:member:`rmt_receive_config_t::signal_range_min_ns` specifies the minimal valid pulse duration in either high or low logic levels. A pulse width that is smaller than this value is treated as a glitch, and ignored by the hardware. - :cpp:member:`rmt_receive_config_t::signal_range_max_ns` specifies the maximum valid pulse duration in either high or low logic levels. A pulse width that is bigger than this value is treated as **Stop Signal**, and the receiver generates receive-complete event immediately. -- If the incoming packet is long, that they cannot be stored in the user buffer at once, you can enable the partial reception feature by setting :cpp:member:`rmt_receive_config_t::extra_flags::en_partial_rx` to ``true``. In this case, the driver invokes :cpp:member:`rmt_rx_event_callbacks_t::on_recv_done` callback multiple times during one transaction, when the user buffer is **almost full**. You can check the value of :cpp:member::`rmt_rx_done_event_data_t::is_last` to know if the transaction is about to finish. +- If the incoming packet is long, that they cannot be stored in the user buffer at once, you can enable the partial reception feature by setting :cpp:member:`rmt_receive_config_t::extra_flags::en_partial_rx` to ``true``. In this case, the driver invokes :cpp:member:`rmt_rx_event_callbacks_t::on_recv_done` callback multiple times during one transaction, when the user buffer is **almost full**. You can check the value of :cpp:member::`rmt_rx_done_event_data_t::is_last` to know if the transaction is about to finish. Please note this features is not supported on all ESP series chips because it relies on hardware abilities like "ping-pong receive" or "DMA receive". The RMT receiver starts the RX machine after the user calls :cpp:func:`rmt_receive` with the provided configuration above. Note that, this configuration is transaction specific, which means, to start a new round of reception, the user needs to set the :cpp:type:`rmt_receive_config_t` again. The receiver saves the incoming signals into its internal memory block or DMA buffer, in the format of :cpp:type:`rmt_symbol_word_t`. diff --git a/docs/zh_CN/api-reference/peripherals/rmt.rst b/docs/zh_CN/api-reference/peripherals/rmt.rst index 0c7c73645f..826a35f7b4 100644 --- a/docs/zh_CN/api-reference/peripherals/rmt.rst +++ b/docs/zh_CN/api-reference/peripherals/rmt.rst @@ -331,7 +331,7 @@ RMT 是一种特殊的通信外设,无法像 SPI 和 I2C 那样发送原始字 - :cpp:member:`rmt_receive_config_t::signal_range_min_ns` 指定高电平或低电平有效脉冲的最小持续时间。如果脉冲宽度小于指定值,硬件会将其视作干扰信号并忽略。 - :cpp:member:`rmt_receive_config_t::signal_range_max_ns` 指定高电平或低电平有效脉冲的最大持续时间。如果脉冲宽度大于指定值,接收器会将其视作 **停止信号**,并立即生成接收完成事件。 -- 如果传入的数据包很长,无法一次性保存在用户缓冲区中,可以通过将 :cpp:member:`rmt_receive_config_t::extra_flags::en_partial_rx` 设置为 ``true`` 来开启部分接收功能。在这种情况下,当用户缓冲区快满的时候,驱动会多次调用 :cpp:member:`rmt_rx_event_callbacks_t::on_recv_done` 回调函数来通知用户去处理已经收到的数据。你可以检查 :cpp:member::`rmt_rx_done_event_data_t::is_last` 的值来了解当前事务是否已经结束。 +- 如果传入的数据包很长,无法一次性保存在用户缓冲区中,可以通过将 :cpp:member:`rmt_receive_config_t::extra_flags::en_partial_rx` 设置为 ``true`` 来开启部分接收功能。在这种情况下,当用户缓冲区快满的时候,驱动会多次调用 :cpp:member:`rmt_rx_event_callbacks_t::on_recv_done` 回调函数来通知用户去处理已经收到的数据。你可以检查 :cpp:member::`rmt_rx_done_event_data_t::is_last` 的值来了解当前事务是否已经结束。请注意,并不是所有 ESP 系列芯片都支持这个功能,它依赖硬件提供的 “ping-pong 接收” 或者 “DMA 接收” 的能力。 根据以上配置调用 :cpp:func:`rmt_receive` 后,RMT 接收器会启动 RX 机制。注意,以上配置均针对特定事务存在,也就是说,要开启新一轮的接收时,需要再次设置 :cpp:type:`rmt_receive_config_t` 选项。接收器会将传入信号以 :cpp:type:`rmt_symbol_word_t` 的格式保存在内部内存块或 DMA 缓冲区中。 From 9ada0b029a2d01e134721a889261c06d27799f24 Mon Sep 17 00:00:00 2001 From: morris Date: Tue, 2 Apr 2024 18:21:45 +0800 Subject: [PATCH 2/2] fix(rmt): fix the counting clock used by rx filter on esp32/s2 is always APB, independent to the channel clock selection Closes https://github.com/espressif/esp-idf/issues/13510 --- components/esp_driver_rmt/src/rmt_common.c | 1 - components/esp_driver_rmt/src/rmt_private.h | 2 + components/esp_driver_rmt/src/rmt_rx.c | 9 +- .../test_apps/rmt/main/test_rmt_rx.c | 141 ++++++++++++++++-- 4 files changed, 142 insertions(+), 11 deletions(-) diff --git a/components/esp_driver_rmt/src/rmt_common.c b/components/esp_driver_rmt/src/rmt_common.c index 5e404ee59a..9da1a246f6 100644 --- a/components/esp_driver_rmt/src/rmt_common.c +++ b/components/esp_driver_rmt/src/rmt_common.c @@ -19,7 +19,6 @@ #include "soc/rmt_periph.h" #include "hal/rmt_ll.h" #include "driver/gpio.h" -#include "esp_clk_tree.h" #include "esp_private/periph_ctrl.h" static const char *TAG = "rmt"; diff --git a/components/esp_driver_rmt/src/rmt_private.h b/components/esp_driver_rmt/src/rmt_private.h index 35298e6e25..cf56d52c6b 100644 --- a/components/esp_driver_rmt/src/rmt_private.h +++ b/components/esp_driver_rmt/src/rmt_private.h @@ -21,6 +21,7 @@ #include "hal/cache_ll.h" #include "esp_intr_alloc.h" #include "esp_heap_caps.h" +#include "esp_clk_tree.h" #include "esp_pm.h" #include "esp_attr.h" #include "esp_private/gdma.h" @@ -190,6 +191,7 @@ typedef struct { struct rmt_rx_channel_t { rmt_channel_t base; // channel base class + uint32_t filter_clock_resolution_hz; // filter clock resolution, in Hz size_t mem_off; // starting offset to fetch the symbols in RMT-MEM size_t ping_pong_symbols; // ping-pong size (half of the RMT channel memory) rmt_rx_done_callback_t on_recv_done; // callback, invoked on receive done diff --git a/components/esp_driver_rmt/src/rmt_rx.c b/components/esp_driver_rmt/src/rmt_rx.c index b7ee610859..23385ffb70 100644 --- a/components/esp_driver_rmt/src/rmt_rx.c +++ b/components/esp_driver_rmt/src/rmt_rx.c @@ -265,6 +265,13 @@ esp_err_t rmt_new_rx_channel(const rmt_rx_channel_config_t *config, rmt_channel_ ESP_LOGW(TAG, "channel resolution loss, real=%"PRIu32, rx_channel->base.resolution_hz); } + rx_channel->filter_clock_resolution_hz = group->resolution_hz; + // On esp32 and esp32s2, the counting clock used by the RX filter always comes from APB clock + // no matter what the clock source is used by the RMT channel as the "core" clock +#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 + esp_clk_tree_src_get_freq_hz(SOC_MOD_CLK_APB, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &rx_channel->filter_clock_resolution_hz); +#endif + rmt_ll_rx_set_mem_blocks(hal->regs, channel_id, rx_channel->base.mem_block_num); rmt_ll_rx_set_mem_owner(hal->regs, channel_id, RMT_LL_MEM_OWNER_HW); #if SOC_RMT_SUPPORT_RX_PINGPONG @@ -383,7 +390,7 @@ esp_err_t rmt_receive(rmt_channel_handle_t channel, void *buffer, size_t buffer_ rmt_hal_context_t *hal = &group->hal; int channel_id = channel->channel_id; - uint32_t filter_reg_value = ((uint64_t)group->resolution_hz * config->signal_range_min_ns) / 1000000000UL; + uint32_t filter_reg_value = ((uint64_t)rx_chan->filter_clock_resolution_hz * config->signal_range_min_ns) / 1000000000UL; uint32_t idle_reg_value = ((uint64_t)channel->resolution_hz * config->signal_range_max_ns) / 1000000000UL; ESP_RETURN_ON_FALSE_ISR(filter_reg_value <= RMT_LL_MAX_FILTER_VALUE, ESP_ERR_INVALID_ARG, TAG, "signal_range_min_ns too big"); ESP_RETURN_ON_FALSE_ISR(idle_reg_value <= RMT_LL_MAX_IDLE_VALUE, ESP_ERR_INVALID_ARG, TAG, "signal_range_max_ns too big"); diff --git a/components/esp_driver_rmt/test_apps/rmt/main/test_rmt_rx.c b/components/esp_driver_rmt/test_apps/rmt/main/test_rmt_rx.c index 9ba314983b..ee9fd6ccf7 100644 --- a/components/esp_driver_rmt/test_apps/rmt/main/test_rmt_rx.c +++ b/components/esp_driver_rmt/test_apps/rmt/main/test_rmt_rx.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -25,13 +25,13 @@ typedef struct { TaskHandle_t task_to_notify; size_t received_symbol_num; -} test_nec_rx_user_data_t; +} test_rx_user_data_t; TEST_RMT_CALLBACK_ATTR static bool test_rmt_rx_done_callback(rmt_channel_handle_t channel, const rmt_rx_done_event_data_t *edata, void *user_data) { BaseType_t high_task_wakeup = pdFALSE; - test_nec_rx_user_data_t *test_user_data = (test_nec_rx_user_data_t *)user_data; + test_rx_user_data_t *test_user_data = (test_rx_user_data_t *)user_data; rmt_symbol_word_t *remote_codes = edata->received_symbols; esp_rom_printf("%u symbols decoded:\r\n", edata->num_symbols); for (size_t i = 0; i < edata->num_symbols; i++) { @@ -64,7 +64,7 @@ static void test_rmt_rx_nec_carrier(size_t mem_block_symbols, bool with_dma, rmt rmt_rx_event_callbacks_t cbs = { .on_recv_done = test_rmt_rx_done_callback, }; - test_nec_rx_user_data_t test_user_data = { + test_rx_user_data_t test_user_data = { .task_to_notify = xTaskGetCurrentTaskHandle(), }; TEST_ESP_OK(rmt_rx_register_event_callbacks(rx_channel, &cbs, &test_user_data)); @@ -216,11 +216,6 @@ static void pwm_bit_bang(int gpio_num) } } -typedef struct { - TaskHandle_t task_to_notify; - size_t received_symbol_num; -} test_rx_user_data_t; - TEST_RMT_CALLBACK_ATTR static bool test_rmt_partial_receive_done(rmt_channel_handle_t channel, const rmt_rx_done_event_data_t *edata, void *user_data) { @@ -307,3 +302,131 @@ TEST_CASE("rmt rx long frame partially", "[rmt]") #endif } #endif // SOC_RMT_SUPPORT_RX_PINGPONG + +TEST_RMT_CALLBACK_ATTR +static bool test_rmt_received_done(rmt_channel_handle_t channel, const rmt_rx_done_event_data_t *edata, void *user_data) +{ + BaseType_t high_task_wakeup = pdFALSE; + test_rx_user_data_t *test_user_data = (test_rx_user_data_t *)user_data; + test_user_data->received_symbol_num += edata->num_symbols; + // when receive done, notify the task to check the received data + if (edata->flags.is_last) { + vTaskNotifyGiveFromISR(test_user_data->task_to_notify, &high_task_wakeup); + } + return high_task_wakeup == pdTRUE; +} + +static void test_rmt_receive_filter(rmt_clock_source_t clk_src) +{ + uint32_t const test_rx_buffer_symbols = 32; + rmt_symbol_word_t *receive_user_buf = heap_caps_aligned_calloc(64, test_rx_buffer_symbols, sizeof(rmt_symbol_word_t), + MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA); + TEST_ASSERT_NOT_NULL(receive_user_buf); + + rmt_rx_channel_config_t rx_channel_cfg = { + .clk_src = clk_src, + .resolution_hz = 1000000, // 1MHz, 1 tick = 1us + .mem_block_symbols = SOC_RMT_MEM_WORDS_PER_CHANNEL, + .gpio_num = TEST_RMT_GPIO_NUM_A, + .flags.io_loop_back = true, // the GPIO will act like a loopback + }; + printf("install rx channel\r\n"); + rmt_channel_handle_t rx_channel = NULL; + TEST_ESP_OK(rmt_new_rx_channel(&rx_channel_cfg, &rx_channel)); + + // initialize the GPIO level to low + TEST_ESP_OK(gpio_set_level(TEST_RMT_GPIO_NUM_A, 0)); + + printf("register rx event callbacks\r\n"); + rmt_rx_event_callbacks_t cbs = { + .on_recv_done = test_rmt_received_done, + }; + test_rx_user_data_t test_user_data = { + .task_to_notify = xTaskGetCurrentTaskHandle(), + .received_symbol_num = 0, + }; + TEST_ESP_OK(rmt_rx_register_event_callbacks(rx_channel, &cbs, &test_user_data)); + + // use TX channel to simulate the input signal + rmt_tx_channel_config_t tx_channel_cfg = { + .clk_src = clk_src, + .resolution_hz = 1000000, // 1MHz, 1 tick = 1us + .mem_block_symbols = SOC_RMT_MEM_WORDS_PER_CHANNEL, + .trans_queue_depth = 4, + .gpio_num = TEST_RMT_GPIO_NUM_A, + .flags.io_loop_back = true, // TX channel and RX channel will bounded to the same GPIO + }; + printf("install tx channel\r\n"); + rmt_channel_handle_t tx_channel = NULL; + TEST_ESP_OK(rmt_new_tx_channel(&tx_channel_cfg, &tx_channel)); + + printf("install a simple copy encoder\r\n"); + rmt_encoder_handle_t copy_encoder = NULL; + rmt_copy_encoder_config_t encoder_cfg = {}; + TEST_ESP_OK(rmt_new_copy_encoder(&encoder_cfg, ©_encoder)); + rmt_transmit_config_t transmit_config = { + .loop_count = 0, // no loop + }; + + printf("enable tx channel\r\n"); + TEST_ESP_OK(rmt_enable(tx_channel)); + printf("enable rx channel\r\n"); + TEST_ESP_OK(rmt_enable(rx_channel)); + + rmt_receive_config_t rx_config = { + .signal_range_min_ns = 3000, // filter out the pulses shorter than 3us + .signal_range_max_ns = 12000000, + }; + // ready to receive + TEST_ESP_OK(rmt_receive(rx_channel, receive_user_buf, test_rx_buffer_symbols * sizeof(rmt_symbol_word_t), &rx_config)); + + // generate short pulse of width 2us, should be filtered out + printf("send a short pulse\r\n"); + rmt_symbol_word_t short_pulse = { + .level0 = 1, + .duration0 = 2, // pulse width 2us + .level1 = 0, + .duration1 = 1, + }; + TEST_ESP_OK(rmt_transmit(tx_channel, copy_encoder, &short_pulse, sizeof(short_pulse), &transmit_config)); + TEST_ASSERT_EQUAL(0, ulTaskNotifyTake(pdFALSE, pdMS_TO_TICKS(1000))); + printf("received %zu symbols\r\n", test_user_data.received_symbol_num); + TEST_ASSERT_EQUAL(0, test_user_data.received_symbol_num); + + printf("send a long pulse\r\n"); + // generate long pulse of width 10us, should be received + rmt_symbol_word_t long_pulse = { + .level0 = 1, + .duration0 = 10, // pulse width 10us + .level1 = 0, + .duration1 = 1, + }; + TEST_ESP_OK(rmt_transmit(tx_channel, copy_encoder, &long_pulse, sizeof(long_pulse), &transmit_config)); + TEST_ASSERT_NOT_EQUAL(0, ulTaskNotifyTake(pdFALSE, pdMS_TO_TICKS(1000))); + printf("received %zu symbols\r\n", test_user_data.received_symbol_num); + TEST_ASSERT_EQUAL(1, test_user_data.received_symbol_num); + + // verify the received data + printf("{%d:%d},{%d:%d}\r\n", receive_user_buf[0].level0, receive_user_buf[0].duration0, receive_user_buf[0].level1, receive_user_buf[0].duration1); + TEST_ASSERT_EQUAL(1, receive_user_buf[0].level0); + TEST_ASSERT_INT_WITHIN(2, 10, receive_user_buf[0].duration0); + TEST_ASSERT_EQUAL(0, receive_user_buf[0].level1); + + printf("disable tx and rx channels\r\n"); + TEST_ESP_OK(rmt_disable(tx_channel)); + TEST_ESP_OK(rmt_disable(rx_channel)); + printf("delete channels and encoder\r\n"); + TEST_ESP_OK(rmt_del_channel(rx_channel)); + TEST_ESP_OK(rmt_del_channel(tx_channel)); + TEST_ESP_OK(rmt_del_encoder(copy_encoder)); + free(receive_user_buf); +} + +TEST_CASE("rmt rx filter functionality", "[rmt]") +{ + // test width different clock sources + rmt_clock_source_t clk_srcs[] = SOC_RMT_CLKS; + for (size_t i = 0; i < sizeof(clk_srcs) / sizeof(clk_srcs[0]); i++) { + test_rmt_receive_filter(clk_srcs[i]); + } +}