Fix UART overflow and timing issues #42
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously, we have seen some timing issues resulting from time spent in FatFS functions. Namely:
f_sync
would block the main loop, preventing the handling of data from the UART. This meant that if the call took too long to return, the UART buffer would overflow and data would be lost.Tone_Task
from loading new waveform data for spoken indications, which would result in a buffer overflow there and "clipped" speech.Looking into the problem, I determined that the
f_sync
function was taking up to 140 ms to return. To put this into perspective, the tone buffer is only 131 ms long. Looking more closely, I determined that these long return times resulted fromFatFS
waiting for the microSD card to be ready. In particular, when a write to the microSD card crosses a cluster boundary, the card must clear the next cluster before it can be written. Thef_sync
operation cannot complete until this is done.Looking more closely yet, I determined that there are two places in particular where the
f_sync
function tends to block. The three newf_sync_x
functions break the original function into three parts around these two blocking points.After examining the operations in each block, I believe that it is safe to access the card in a "read only" capacity between the three new
f_sync
operations. This means we can, e.g., fill the audio buffer in this time. Since a single blocking operation always takes less than 70 ms (whenf_sync
blocks for 140 ms, it is actually blocking twice internally), our current tone buffer is sufficient to provide audio during this time.In testing, I have found that even with the present changes, the audio buffer is not sufficient if we double the audio rate (i.e., going from 7812 Hz to 15625 Hz). Therefore, I don't think we will be able to increase the audio sample rate without increasing the size of the audio buffer. It is doubtful we will be able to free up enough memory to do so.
Testing the new firmware for 3 continuous hours, I could detect no audio buffer overflows or UART buffer overflows.