RMT data buffer and race condition
Posted: Sat Jun 22, 2019 5:57 pm
I'm following the code in the example to control some RGB LED pixels.
https://github.com/espressif/arduino-es ... oPixel.ino
I have a global buffer that I fill with data to send out the pixels.
I initialize RMT with
When an HTTP request arrives to the ESP32, I can send out data to the pixels like follows
My concern is about race conditions since RMT is asynchronous. What if a second HTTP request arrives while the rmtWrite for the first HTTP request is still in progress? The second request handler will start overwriting data in the led_data buffer. This could possibly break the data from the first HTTP request still to be sent to the lights. How to make the code more robust?
(I am using the Ardunio IDE but I don't think this topic of race condition is specific to the IDE.)
https://github.com/espressif/arduino-es ... oPixel.ino
I have a global buffer that I fill with data to send out the pixels.
Code: Select all
#define NR_OF_LEDS 8*4
#define NR_OF_ALL_BITS 24*NR_OF_LEDS
rmt_data_t led_data[NR_OF_ALL_BITS];
Code: Select all
if ((rmt_send = rmtInit(17, true, RMT_MEM_64)) == NULL) {
Serial.println("init sender failed\n");
}
float realTick = rmtSetTick(rmt_send, 100);
Code: Select all
int led, col, bit;
int i = 0;
for (led = 0; led < NR_OF_PIXELS; led++) {
for (col = 0; col < 3; col++) {
for (bit = 0; bit < 8; bit++) {
led_data[i].level0 = 1;
led_data[i].duration0 = 8; // T1H
led_data[i].level1 = 0;
led_data[i].duration1 = 5; // T1L
i++;
}
}
}
rmtWrite(rmt_send, led_data, NR_OF_ALL_BITS);
(I am using the Ardunio IDE but I don't think this topic of race condition is specific to the IDE.)