From 9ada0b029a2d01e134721a889261c06d27799f24 Mon Sep 17 00:00:00 2001 From: morris Date: Tue, 2 Apr 2024 18:21:45 +0800 Subject: [PATCH] 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 5e404ee59af..9da1a246f6d 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 35298e6e256..cf56d52c6b2 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 b7ee6108599..23385ffb708 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 9ba314983ba..ee9fd6ccf7d 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]); + } +}