Page 1 of 1

RMT data buffer and race condition

Posted: Sat Jun 22, 2019 5:57 pm
by tedhenry100
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.

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];
I initialize RMT with

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);
When an HTTP request arrives to the ESP32, I can send out data to the pixels like follows

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); 
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.)

Re: RMT data buffer and race condition

Posted: Sun Jun 23, 2019 5:08 pm
by WiFive
Use a flag/semaphore but is there a way to check if tx is done in the Arduino rmt driver?

Re: RMT data buffer and race condition

Posted: Wed Jun 26, 2019 4:03 am
by tedhenry100
I've stopped using the Arduino wrappers for the RMT functionality. I've switched to rmt_write_sample and can set the last parameter, wait_tx_done, to true.

rmt_write_sample is nice as a gigantic led_data array does not need to be created. I exceeded the limit and the led_data array was so large it wouldn't fit in a memory segment.