Hi everyone,
I have a fairly simple use for ADC: sample 4 channels (scan mode) every 10ms and store in RAM.
Every now and then (asynchronously) there will be a function or a task (not in ISR context) that will want to read the latest values of the 4 channels.
I've been struggling to implement that without a potential for getting a race condition (where the task might read the values as they are written), and I wanted to ask for your advice on that.
My (simplified) code for configuring the ADC is as follows:
In a nutshell it's identical to the example ADC code, where we have a double buffer, and PPI+Timer to sample every 10ms.
As for the asynchronous function run from a task, it will occasionally need to read the latest values stored by the ADC. I was thinking about this code:
My idea is that the ADC will update the buffer index m_buffer_index (0 or 1) and the task will read from m_buffer_index^1 (1 or 0).
My concern here is that there is a potential for a race condition, where m_buffer_index would get updated by the ISR while the function above is being run.
Your advice would be greatly appreciated!