From 335affea044c3ca8a7d6bd550699990ea1465f20 Mon Sep 17 00:00:00 2001 From: Sola85 Date: Fri, 30 May 2025 16:37:18 +0200 Subject: [PATCH 1/4] espressif: Try RMT with DMA --- ports/espressif/common-hal/pulseio/PulseIn.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/espressif/common-hal/pulseio/PulseIn.c b/ports/espressif/common-hal/pulseio/PulseIn.c index f7e500790562a..a8010ed42b4a5 100644 --- a/ports/espressif/common-hal/pulseio/PulseIn.c +++ b/ports/espressif/common-hal/pulseio/PulseIn.c @@ -80,7 +80,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu } // We add one to the maxlen version to ensure that two symbols at lease are // captured because we may skip the first portion of a symbol. - self->raw_symbols_size = MIN(64, maxlen / 2 + 1) * sizeof(rmt_symbol_word_t); + self->raw_symbols_size = (maxlen / 2 + 1) * sizeof(rmt_symbol_word_t); self->raw_symbols = (rmt_symbol_word_t *)m_malloc_without_collect(self->raw_symbols_size); if (self->raw_symbols == NULL) { m_free(self->buffer); @@ -109,7 +109,8 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu .clk_src = RMT_CLK_SRC_DEFAULT, // 2 us resolution so we can capture 65ms pulses. The RMT period is only 15 bits. .resolution_hz = 1000000 / 2, - .mem_block_symbols = SOC_RMT_MEM_WORDS_PER_CHANNEL, + .mem_block_symbols = self->raw_symbols_size, + .with_dma = 1 }; // If we fail here, the buffers allocated above will be garbage collected. CHECK_ESP_RESULT(rmt_new_rx_channel(&config, &self->channel)); From 7b05a011a34e8a27683d48831ed1f5172009ad8a Mon Sep 17 00:00:00 2001 From: Sola85 Date: Sat, 31 May 2025 11:26:17 +0200 Subject: [PATCH 2/4] fix struct --- ports/espressif/common-hal/pulseio/PulseIn.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/espressif/common-hal/pulseio/PulseIn.c b/ports/espressif/common-hal/pulseio/PulseIn.c index a8010ed42b4a5..d64dbf89e495d 100644 --- a/ports/espressif/common-hal/pulseio/PulseIn.c +++ b/ports/espressif/common-hal/pulseio/PulseIn.c @@ -110,7 +110,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu // 2 us resolution so we can capture 65ms pulses. The RMT period is only 15 bits. .resolution_hz = 1000000 / 2, .mem_block_symbols = self->raw_symbols_size, - .with_dma = 1 + .flags.with_dma = 1 }; // If we fail here, the buffers allocated above will be garbage collected. CHECK_ESP_RESULT(rmt_new_rx_channel(&config, &self->channel)); From a33aa9f35f867d30158004a91e6327b10bbec123 Mon Sep 17 00:00:00 2001 From: Sola85 Date: Sat, 31 May 2025 21:41:20 +0200 Subject: [PATCH 3/4] add error handling --- ports/espressif/common-hal/pulseio/PulseIn.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ports/espressif/common-hal/pulseio/PulseIn.c b/ports/espressif/common-hal/pulseio/PulseIn.c index d64dbf89e495d..04070fa588727 100644 --- a/ports/espressif/common-hal/pulseio/PulseIn.c +++ b/ports/espressif/common-hal/pulseio/PulseIn.c @@ -67,7 +67,7 @@ static bool _done_callback(rmt_channel_handle_t rx_chan, } if (!self->paused) { - rmt_receive(self->channel, self->raw_symbols, self->raw_symbols_size, &rx_config); + CHECK_ESP_RESULT(rmt_receive(self->channel, self->raw_symbols, self->raw_symbols_size, &rx_config)); } return false; } @@ -118,9 +118,9 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu rmt_rx_event_callbacks_t rx_callback = { .on_recv_done = _done_callback }; - rmt_rx_register_event_callbacks(self->channel, &rx_callback, self); - rmt_enable(self->channel); - rmt_receive(self->channel, self->raw_symbols, self->raw_symbols_size, &rx_config); + CHECK_ESP_RESULT(rmt_rx_register_event_callbacks(self->channel, &rx_callback, self)); + CHECK_ESP_RESULT(rmt_enable(self->channel)); + CHECK_ESP_RESULT(rmt_receive(self->channel, self->raw_symbols, self->raw_symbols_size, &rx_config)); } bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) { @@ -139,7 +139,7 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) { void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self) { self->paused = true; - rmt_disable(self->channel); + CHECK_ESP_RESULT(rmt_disable(self->channel)); } void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self, uint16_t trigger_duration) { @@ -158,8 +158,8 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self, uint16_t tri self->find_first = true; self->paused = false; - rmt_enable(self->channel); - rmt_receive(self->channel, self->raw_symbols, self->raw_symbols_size, &rx_config); + CHECK_ESP_RESULT(rmt_enable(self->channel)); + CHECK_ESP_RESULT(rmt_receive(self->channel, self->raw_symbols, self->raw_symbols_size, &rx_config)); } void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t *self) { From 3313f0e2ecb90bfee8a474b241edbf08b4f27b14 Mon Sep 17 00:00:00 2001 From: Sola85 Date: Sun, 1 Jun 2025 11:42:11 +0200 Subject: [PATCH 4/4] debugging --- ports/espressif/common-hal/pulseio/PulseIn.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ports/espressif/common-hal/pulseio/PulseIn.c b/ports/espressif/common-hal/pulseio/PulseIn.c index 04070fa588727..edfaf6b5c1756 100644 --- a/ports/espressif/common-hal/pulseio/PulseIn.c +++ b/ports/espressif/common-hal/pulseio/PulseIn.c @@ -67,7 +67,9 @@ static bool _done_callback(rmt_channel_handle_t rx_chan, } if (!self->paused) { + mp_printf(&mp_plat_print, "check 6"); CHECK_ESP_RESULT(rmt_receive(self->channel, self->raw_symbols, self->raw_symbols_size, &rx_config)); + mp_printf(&mp_plat_print, "check 7"); } return false; } @@ -113,14 +115,18 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu .flags.with_dma = 1 }; // If we fail here, the buffers allocated above will be garbage collected. + mp_printf(&mp_plat_print, "check 1"); CHECK_ESP_RESULT(rmt_new_rx_channel(&config, &self->channel)); - + mp_printf(&mp_plat_print, "check 2"); rmt_rx_event_callbacks_t rx_callback = { .on_recv_done = _done_callback }; CHECK_ESP_RESULT(rmt_rx_register_event_callbacks(self->channel, &rx_callback, self)); + mp_printf(&mp_plat_print, "check 3"); CHECK_ESP_RESULT(rmt_enable(self->channel)); + mp_printf(&mp_plat_print, "check 4"); CHECK_ESP_RESULT(rmt_receive(self->channel, self->raw_symbols, self->raw_symbols_size, &rx_config)); + mp_printf(&mp_plat_print, "check 5"); } bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) { @@ -139,7 +145,9 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) { void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self) { self->paused = true; + mp_printf(&mp_plat_print, "check 8"); CHECK_ESP_RESULT(rmt_disable(self->channel)); + mp_printf(&mp_plat_print, "check 9"); } void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self, uint16_t trigger_duration) { @@ -158,8 +166,11 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self, uint16_t tri self->find_first = true; self->paused = false; + mp_printf(&mp_plat_print, "check 10"); CHECK_ESP_RESULT(rmt_enable(self->channel)); + mp_printf(&mp_plat_print, "check 11"); CHECK_ESP_RESULT(rmt_receive(self->channel, self->raw_symbols, self->raw_symbols_size, &rx_config)); + mp_printf(&mp_plat_print, "check 12"); } void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t *self) {