diff --git a/components/driver/mcpwm/include/driver/mcpwm_cap.h b/components/driver/mcpwm/include/driver/mcpwm_cap.h index 4fa50c657f..4ea851a961 100644 --- a/components/driver/mcpwm/include/driver/mcpwm_cap.h +++ b/components/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/driver/rmt/rmt_common.c b/components/driver/rmt/rmt_common.c index b280fa93fa..bbc4592e98 100644 --- a/components/driver/rmt/rmt_common.c +++ b/components/driver/rmt/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/driver/rmt/rmt_private.h b/components/driver/rmt/rmt_private.h index ac9e38e4ea..93aa67fc02 100644 --- a/components/driver/rmt/rmt_private.h +++ b/components/driver/rmt/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" @@ -180,6 +181,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/driver/rmt/rmt_rx.c b/components/driver/rmt/rmt_rx.c index a0c259345d..ac82162a4b 100644 --- a/components/driver/rmt/rmt_rx.c +++ b/components/driver/rmt/rmt_rx.c @@ -256,6 +256,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 @@ -278,11 +285,11 @@ esp_err_t rmt_new_rx_channel(const rmt_rx_channel_config_t *config, rmt_channel_ .pull_up_en = true, .pin_bit_mask = 1ULL << 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"); 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_hal_iomux_func_sel(GPIO_PIN_MUX_REG[config->gpio_num], PIN_FUNC_GPIO); // initialize other members of rx channel portMUX_INITIALIZE(&rx_channel->base.spinlock); @@ -365,7 +372,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/driver/rmt/rmt_tx.c b/components/driver/rmt/rmt_tx.c index b4d3a24692..5aeb32f3f7 100644 --- a/components/driver/rmt/rmt_tx.c +++ b/components/driver/rmt/rmt_tx.c @@ -314,7 +314,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_hal_iomux_func_sel(GPIO_PIN_MUX_REG[config->gpio_num], PIN_FUNC_GPIO); portMUX_INITIALIZE(&tx_channel->base.spinlock); atomic_init(&tx_channel->base.fsm, RMT_FSM_INIT); diff --git a/components/driver/test_apps/rmt/main/test_rmt_rx.c b/components/driver/test_apps/rmt/main/test_rmt_rx.c index 22958a3c71..0fb2414ab4 100644 --- a/components/driver/test_apps/rmt/main/test_rmt_rx.c +++ b/components/driver/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 */ @@ -24,13 +24,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++) { @@ -45,7 +45,7 @@ static void test_rmt_rx_nec_carrier(size_t mem_block_symbols, bool with_dma, rmt { uint32_t const test_rx_buffer_symbols = 128; rmt_symbol_word_t *remote_codes = heap_caps_aligned_calloc(64, test_rx_buffer_symbols, sizeof(rmt_symbol_word_t), - MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA); + MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA); TEST_ASSERT_NOT_NULL(remote_codes); rmt_rx_channel_config_t rx_channel_cfg = { @@ -63,7 +63,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)); @@ -201,3 +201,126 @@ TEST_CASE("rmt rx nec with carrier", "[rmt]") test_rmt_rx_nec_carrier(128, true, RMT_CLK_SRC_DEFAULT); #endif } + +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 + 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)); + + 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]); + } +}