Hello everybody,
I noticed that on the forum, it is considered common practice to use two DMA channels to acquire data stereo from the i2s, and place it in two separate ping-pong buffers into memory (left & right), waiting to be processed. Both DMA channels are hardware-triggered by the right-receive event of the I2S but, if they are under control of the same DMA controller, they won't start at the same time. One is going to be served first, and another later. I understood that, since there is no priority setting for the channels (they are served in round robin) if the two channels are triggered by the same event, it is basically random, which one is going to be served first (am I wrong?)
And here comes my question:
let's assume that the elaboration task. at a certain point wants to read the data from the PING_PONG. It will read the status register of one or both the DMA channels to decide which half is free (I am not generating software interrupts with the DMAs). There is the very unlikely, but still realistic possibility that, reading the LAST_XFER bits will give a different result for the two different channels. One channel, the first to be served, could have just written the last slot of its PING, and have updated its LAST_XFER bit to 0, while the second channel might still be writing data in the last slot of its PING, and therefore have still the LAST_XFER bit set to 1 when the read comes.
Using transfer-complete interrupts by the DMA channels wouldn't help, because if the first channel triggers an interrupt when it completes the PING and, say, wakes the elaboration task, it could be that, when the task reads the LAST_XFER bits, the second channel is still finishing its last burst in the PING, if its burst is long.
Could this happen? And is there a way to be sure about which half of the buffer is actually not being written by any channel?
For example, knowing that one channel is ALWAYS served second would help, because even if the second-served channel is writing the last slot of its PING (and so the first-served channel just entered its PONG) the first-served channel would not write its PONG until the next i2s event, which would leave a generous margin to read the PONG sections, which are not, in fact, still being accessed by anyone.
I hope I was clear, thank you in advance for your support.
V.