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!