However, to have a continuous data stream to the PWM output, I had to configure two 512 byte buffer arrays in SRAM in addition to the ~ 1000 bytes of buffer that the SDFat library is already using. This means that for all other variables only 500 bytes are left. This is a severe limitation for all other functions I want to implement, i.e. defining playlists etc. It also means that this method doesn't work on an Atmega 328 , that has only 2048 bytes SRAM. Trying to reduce the buffer size always leads to audible interruptions of the data stream.
Has somebody managed to directly read from the SDFat buffer , w/o using the file.read() function
which is far too slow for single byte transfer at the needed 88 kByte/s ?
// This example assumes you're using a double bufferunsigned int bytesRead = file.read(buffer[whichBuffer], sizeof(buffer));// Saving the amount of bytes obtained might be a good idea to realize on time when the end of the file has been reached
Or a 1284P, with 16K of SRAM.
You have convinced me that I should not interfere with the inner workings of the SDFat library.
- Yes, I use the file.read(512) instruction in the main loop to fill two alternating buffers of 512 bytes each.
The ISR reads 4 bytes ( 16 bit L/R audio ) 22050 times / second and sets the OCRs of Timer1/4 accordingly.
I tried smaller buffer sizes, which always led to audible interruptions of the audio, because then thetime to empty the buffers was shorter than the time needed to refill them from the SD card. The ISR then reads invalid data , which can be heard as noise and clicks at irregular intervals.
The raw physical speed of the SPI connection to the SD card isn't the actual bottleneck ( at 8 MHz clock the theoretical limit would be 1000 kbytes / second ).
It seems that the transfer between the library's buffer and the two buffer arrays only work w/o substantial delay, when one entire 512 byte block can be transferred with each file.read() call. Kind of synchronisation of the data transfer inside of the MCU with the one between SD card and MCU.