embassy icon indicating copy to clipboard operation
embassy copied to clipboard

Fix ringbuffer hardware race condition bug.

Open liarokapisv opened this issue 6 months ago • 5 comments

When attempting to get a snapshot of the completion count and position, the current ringbuffer implementation uses a critical section. This does indeed prevent the completion_count from updating but does not prevent the position from updating since that is fetched directly from the DMA register.

When the code is called right at the end of the DMA cycle, the position that is read may be that of the next cycle, while the completion_count is still that of the previous cycle. As a result it looks to the code as if we went backwards and an overrun error is emitted.

In order to fix this we remove the critical section and get two readings, comparing the two in order to deduce that a wrap-around occured.

liarokapisv avatar Aug 08 '24 13:08 liarokapisv