I would like to save all the CAN bus data to an SPI connected SD card.
Using RTOS, what is the best method to do this?
I am thinking of saving the CAN bus data to a RTOS queue, then in a different task, writing the queue data to the SD card.
The CAN bus runs at 500 Kbs and the average data rate is under 25 KBs. Testing of multiple SD cards (with a 2KB buffer) averages more thank 800 KB/second, so the SD card should't be a bottle neck.
Would a ring buffer be better to use?
Any other suggestions?
I think I will first try coping the CAN bus data to a normal RTOS queue. Then copy that data to a much larger buffer (2.5 - 4 KB) and when that buffer is at 2K, write the buffer to the SD card.
OR should I just use a larger RTOS queue of 2.5KB and write it to the SD card when it has 2KB of data?
Here is what we went with:
ESP32 CAN bus frame triggers an interrupt and the CAN frame data is copied to an RTOS queue and returns.
Task #1, copies the CAN frame data from the queue, does some processing/formatting and saves it to a larger queue.
Task #2, copies the formatted data and writes it to the SD card. Every 150 CAN frames, flush() is called to write the data to the SD card.
My reason for using 2 queues is to be able to handle the variable CAN frame rate (500 - 2100 frames/sec) and to handle the delay in SD card writing when it is doing its housekeeping.
It is still being field tested, but so far it is working very well.