Skip to content

Commit ac0fc7f

Browse files
authored
feat(rmt): Solve neopixel issue
if neopixelWrite() is used from different tasks/isr_callbacks, it may result in a concurrency problem and many detach/attach calls in sequence, but not in synch. This commit avoids initializing the neopixel GPIO again.
1 parent d708438 commit ac0fc7f

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

Diff for: cores/esp32/esp32-hal-rmt.c

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Espressif Systems (Shanghai) PTE LTD
1+
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -61,6 +61,7 @@ struct rmt_obj_s {
6161
EventGroupHandle_t rmt_events; // read/write done event RMT callback handle
6262
bool rmt_ch_is_looping; // Is this RMT TX Channel in LOOPING MODE?
6363
size_t *num_symbols_read; // Pointer to the number of RMT symbol read by IDF RMT RX Done
64+
rmt_reserve_memsize_t mem_size; // RMT Memory size
6465
uint32_t frequency_Hz; // RMT Frequency
6566
uint8_t rmt_EOT_Level; // RMT End of Transmission Level - default is LOW
6667

@@ -464,6 +465,17 @@ bool rmtInit(int pin, rmt_ch_dir_t channel_direction, rmt_reserve_memsize_t mem_
464465
}
465466
}
466467

468+
// check if the RMT peripheral is already initialized with the same parameters
469+
rmt_bus_handle_t bus = NULL;
470+
peripheral_bus_type_t rmt_bus_type = perimanGetPinBusType(pin);
471+
if (rmt_bus_type == ESP32_BUS_TYPE_RMT_TX || rmt_bus_type == ESP32_BUS_TYPE_RMT_RX) {
472+
rmt_ch_dir_t bus_rmt_dir = rmt_bus_type == ESP32_BUS_TYPE_RMT_TX ? RMT_TX_MODE : RMT_RX_MODE;
473+
bus = (rmt_bus_handle_t) perimanGetPinBus(pin, rmt_bus_type);
474+
if (bus->frequency_Hz == frequency_Hz && bus_rmt_dir == channel_direction && bus->mem_size == mem_size) {
475+
return true; // already initialized with the same parameters
476+
}
477+
}
478+
467479
// set Peripheral Manager deInit Callback
468480
perimanSetBusDeinit(ESP32_BUS_TYPE_RMT_TX, _rmtDetachBus);
469481
perimanSetBusDeinit(ESP32_BUS_TYPE_RMT_RX, _rmtDetachBus);
@@ -491,14 +503,15 @@ bool rmtInit(int pin, rmt_ch_dir_t channel_direction, rmt_reserve_memsize_t mem_
491503
while (xSemaphoreTake(g_rmt_block_lock, portMAX_DELAY) != pdPASS) {}
492504

493505
// allocate the rmt bus object and sets all fields to NULL
494-
rmt_bus_handle_t bus = (rmt_bus_handle_t)heap_caps_calloc(1, sizeof(struct rmt_obj_s), MALLOC_CAP_DEFAULT);
506+
bus = (rmt_bus_handle_t)heap_caps_calloc(1, sizeof(struct rmt_obj_s), MALLOC_CAP_DEFAULT);
495507
if (bus == NULL) {
496508
log_e("GPIO %d - Bus Memory allocation fault.", pin);
497509
goto Err;
498510
}
499511

500-
// store the RMT Freq to check Filter and Idle valid values in the RMT API
512+
// store the RMT Freq and mem_size to check Initialization, Filter and Idle valid values in the RMT API
501513
bus->frequency_Hz = frequency_Hz;
514+
bus->mem_size = mem_size;
502515
// pulses with width smaller than min_ns will be ignored (as a glitch)
503516
//bus->signal_range_min_ns = 0; // disabled --> not necessary CALLOC set all to ZERO.
504517
// RMT stops reading if the input stays idle for longer than max_ns

0 commit comments

Comments
 (0)