Electronic – High Speed Data Acquisition with SD Card Write

interruptsisrsdspi

I am collecting data from several dozen sensors over an 8 MHz SPI bus at 400 Hz. It is critical that the sensors are sampled at this rate with no lost packets. In order to do this, I have a timer (which I will refer to as TimerA) which raises an interrupt every 2.5 milliseconds. To store the large amount of data, I am using an SD card.

Here is my current program flow

  • Within the TimerA ISR, data is collected from all the sensors. This takes approximately 1 millisecond.
  • This data sample is stored in a FIFO buffer. The buffer can store up to 400 samples (1 second of data).
  • Each time a sample is written to the buffer, an index k is incremented. k keeps track of the number of samples in the FIFO buffer.
  • The main() loop watches the index k. If k > 0, it pulls an entry from the FIFO buffer, processes it, then stores the result in the SD card write buffer. k is decremented.
  • Once the SD card write buffer reaches 512 bytes, the SD card write buffer is written to the SD card.

I know that having the data acquisition within the TimerA ISR is a no-no as the ISR code should be short and sweet. I originally had the TimerA ISR raise a flag that would be handle in main(), but during long SD card writes I would lose several data samples.

My question is: is my current program flow acceptable? Or is having the data acquisition code within the TimerA ISR going to cause issues?

Best Answer

Within the TimerA ISR, data is collected from all the sensors. This takes approximately 1 millisecond

This could work fine, but why don't just start an SPI DMA Transfer in your timer? These beefy Cortex M4Fs usually have multiple DMA channels, the core is free to do other things (like finishing the SD card transfer one ms sooner) or to sleep, saving power.