-1

I have a project with Arduino or Teensy where I need to read constantly bytes from a binary file "byte" buf=file.read() from an SD card and make an output with port manipulation (PORTD = buf, ... or PORTD = file.read() inside a loop) at some stable frequency.

I have read that because of the SD card latency the file.read() command will not execute at the same time every time, so the output frequency will not be stable. Does this latency happen when I open the file once read until the end with a loop and then close the file?

If so, the other solution is to read from the SD card 512 bytes each and store them into a buf array with the help of a struct according to that. The problem now is that each time I read 512 bytes I have to wait for those to come plus the SD card read latency. Is it possible to make two buffer arrays of 512 bytes each -> fill the first one -> start port manipulation with the first array inside a timer interrupt with a frequency of 10 or 20 Khz or more, so the output always has a stable frequency while in another loop I try to fill the other array? I mean is it possible to interrupt the "file.read(buf, size of(512))" command without issues?

If so, how can I deal with smooth data rate and have a stable frequency?

I believe that the same problem occurs with microcontroller audio players. where they play wav files from the SD card. Please help me. Thanks in advance!

dda
  • 6,030
  • 2
  • 25
  • 34
  • 1
    You don't mention which exact part you're using, so no one here knows the capabilities (e.g. SRAM, interrupts, I/O) available to do your task. The Arduino family alone spans a great range of capabilities. Get out the datasheets and be specific, please, or nobody will be able to help. – TomServo Jun 13 '17 at 12:58

1 Answers1

0

I didn't write the code yet, so to be clear, let's say I have a teensy 3.6 with an on-board SD card module with available 204800 bytes of SRAM for buffers, which means that I can use 400 buffers of 512 bytes for each block read from the SD card.

Then I make a timer interrupt, which is running (e.g at 50KHz) and push out simultaneously to 8 pins (any one of the PORTS can be used with PORT manipulation) byte by byte. Meanwhile, is it possible to read new blocks from the SD card in the available time remaining outside the interrupt that does port manipulation, so that there are always bytes available and not wait for the SD card?

In a few words, when a read command starts at some time it will be interrupted by a timer interrupt function to push out the byte and then return where it was. Is this possible? I think that mic audio players use that logic with two buffers, one is pushed out (DAC values) while the other one is filled again before the first one comes to the end. That's why 44.4 Khz is the maximum 8-bit wav playing so none of the buffer underflows with the result of disturbing output frequency. Thanks!

dda
  • 6,030
  • 2
  • 25
  • 34