1919#include "driver/gpio.h"
2020#include "driver/rmt_tx.h"
2121#include "driver/rmt_rx.h"
22+ #include "hal/rmt_ll.h"
2223
2324#include "esp32-hal-rmt.h"
2425#include "esp32-hal-periman.h"
@@ -58,6 +59,7 @@ struct rmt_obj_s {
5859 EventGroupHandle_t rmt_events ; // read/write done event RMT callback handle
5960 bool rmt_ch_is_looping ; // Is this RMT TX Channel in LOOPING MODE?
6061 size_t * num_symbols_read ; // Pointer to the number of RMT symbol read by IDF RMT RX Done
62+ uint32_t frequency_Hz ; // RMT Frequency
6163
6264#if !CONFIG_DISABLE_HAL_LOCKS
6365 SemaphoreHandle_t g_rmt_objlocks ; // Channel Semaphore Lock
@@ -210,7 +212,7 @@ bool rmtSetCarrier(int pin, bool carrier_en, bool carrier_level, uint32_t freque
210212 return retCode ;
211213}
212214
213- bool rmtSetFilter (int pin , uint8_t filter_pulse_ns )
215+ bool rmtSetRxMinThreshold (int pin , uint8_t filter_pulse_ticks )
214216{
215217 rmt_bus_handle_t bus = _rmtGetBus (pin , __FUNCTION__ );
216218 if (bus == NULL ) {
@@ -221,13 +223,23 @@ bool rmtSetFilter(int pin, uint8_t filter_pulse_ns)
221223 return false;
222224 }
223225
226+ uint32_t filter_pulse_ns = (1000000000 / bus -> frequency_Hz ) * filter_pulse_ticks ;
227+ // RMT_LL_MAX_FILTER_VALUE is 255 for ESP32, S2, S3, C3, C6 and H2;
228+ // filter_pulse_ticks is 8 bits, thus it will not exceed 255
229+ #if 0 // for the future, in case some other SoC has different limit
230+ if (filter_pulse_ticks > RMT_LL_MAX_FILTER_VALUE ) {
231+ log_e ("filter_pulse_ticks is too big. Max = %d" , RMT_LL_MAX_FILTER_VALUE );
232+ return false;
233+ }
234+ #endif
235+
224236 RMT_MUTEX_LOCK (bus );
225237 bus -> signal_range_min_ns = filter_pulse_ns ; // set zero to disable it
226238 RMT_MUTEX_UNLOCK (bus );
227239 return true;
228240}
229241
230- bool rmtSetRxThreshold (int pin , uint16_t value )
242+ bool rmtSetRxMaxThreshold (int pin , uint16_t idle_thres_ticks )
231243{
232244 rmt_bus_handle_t bus = _rmtGetBus (pin , __FUNCTION__ );
233245 if (bus == NULL ) {
@@ -238,8 +250,17 @@ bool rmtSetRxThreshold(int pin, uint16_t value)
238250 return false;
239251 }
240252
253+ uint32_t idle_thres_ns = (1000000000 / bus -> frequency_Hz ) * idle_thres_ticks ;
254+ // RMT_LL_MAX_IDLE_VALUE is 65535 for ESP32,S2 and 32767 for S3, C3, C6 and H2
255+ #if RMT_LL_MAX_IDLE_VALUE < 65535 // idle_thres_ticks is 16 bits anyway - save some bytes
256+ if (idle_thres_ticks > RMT_LL_MAX_IDLE_VALUE ) {
257+ log_e ("idle_thres_ticks is too big. Max = %ld" , RMT_LL_MAX_IDLE_VALUE );
258+ return false;
259+ }
260+ #endif
261+
241262 RMT_MUTEX_LOCK (bus );
242- bus -> signal_range_max_ns = value ; // set as zero to disable it
263+ bus -> signal_range_max_ns = idle_thres_ns ;
243264 RMT_MUTEX_UNLOCK (bus );
244265 return true;
245266}
@@ -462,10 +483,12 @@ bool rmtInit(int pin, rmt_ch_dir_t channel_direction, rmt_reserve_memsize_t mem_
462483 goto Err ;
463484 }
464485
486+ // store the RMT Freq to check Filter and Idle valid values in the RMT API
487+ bus -> frequency_Hz = frequency_Hz ;
465488 // pulses with width smaller than min_ns will be ignored (as a glitch)
466- bus -> signal_range_min_ns = 1000000000 / ( frequency_Hz * 2 ) ; // 1/2 pulse width
489+ bus -> signal_range_min_ns = 0 ; // disabled
467490 // RMT stops reading if the input stays idle for longer than max_ns
468- bus -> signal_range_max_ns = (1000000000 / frequency_Hz ) * 10 ; // 10 pulses width
491+ bus -> signal_range_max_ns = (1000000000 / frequency_Hz ) * RMT_LL_MAX_IDLE_VALUE ; // maximum possible
469492 // creates the event group to control read_done and write_done
470493 bus -> rmt_events = xEventGroupCreate ();
471494 if (bus -> rmt_events == NULL ) {
0 commit comments