19
19
#include "driver/gpio.h"
20
20
#include "driver/rmt_tx.h"
21
21
#include "driver/rmt_rx.h"
22
+ #include "hal/rmt_ll.h"
22
23
23
24
#include "esp32-hal-rmt.h"
24
25
#include "esp32-hal-periman.h"
@@ -58,6 +59,7 @@ struct rmt_obj_s {
58
59
EventGroupHandle_t rmt_events ; // read/write done event RMT callback handle
59
60
bool rmt_ch_is_looping ; // Is this RMT TX Channel in LOOPING MODE?
60
61
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
61
63
62
64
#if !CONFIG_DISABLE_HAL_LOCKS
63
65
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
210
212
return retCode ;
211
213
}
212
214
213
- bool rmtSetFilter (int pin , uint8_t filter_pulse_ns )
215
+ bool rmtSetRxMinThreshold (int pin , uint8_t filter_pulse_ticks )
214
216
{
215
217
rmt_bus_handle_t bus = _rmtGetBus (pin , __FUNCTION__ );
216
218
if (bus == NULL ) {
@@ -221,13 +223,23 @@ bool rmtSetFilter(int pin, uint8_t filter_pulse_ns)
221
223
return false;
222
224
}
223
225
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
+
224
236
RMT_MUTEX_LOCK (bus );
225
237
bus -> signal_range_min_ns = filter_pulse_ns ; // set zero to disable it
226
238
RMT_MUTEX_UNLOCK (bus );
227
239
return true;
228
240
}
229
241
230
- bool rmtSetRxThreshold (int pin , uint16_t value )
242
+ bool rmtSetRxMaxThreshold (int pin , uint16_t idle_thres_ticks )
231
243
{
232
244
rmt_bus_handle_t bus = _rmtGetBus (pin , __FUNCTION__ );
233
245
if (bus == NULL ) {
@@ -238,8 +250,17 @@ bool rmtSetRxThreshold(int pin, uint16_t value)
238
250
return false;
239
251
}
240
252
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
+
241
262
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 ;
243
264
RMT_MUTEX_UNLOCK (bus );
244
265
return true;
245
266
}
@@ -462,10 +483,12 @@ bool rmtInit(int pin, rmt_ch_dir_t channel_direction, rmt_reserve_memsize_t mem_
462
483
goto Err ;
463
484
}
464
485
486
+ // store the RMT Freq to check Filter and Idle valid values in the RMT API
487
+ bus -> frequency_Hz = frequency_Hz ;
465
488
// 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
467
490
// 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
469
492
// creates the event group to control read_done and write_done
470
493
bus -> rmt_events = xEventGroupCreate ();
471
494
if (bus -> rmt_events == NULL ) {
0 commit comments