Shared SPI ADC resource across task and deferred task

Hello, I am reading a SPi 8 channel ADC in a deferred task. A 1ms timed ISR sync with a task that reads a single channel of the ADC and stores it in a cache. I also want t be able to read the other less important channels and access the data via a command parser / comms terminal. I have tackled this by having another task read the other channels. The ADC reads fine and there are no issues with the driver. The issue is when I try and read the other channels in another task. It all goes horribly wrong with corrupted ADC data. I started with the deferred task being higher priority and used mutexes to protect the shared resource to no avail. I then made both tasks the same priorty and added a task yield to avoid task starvation. Still no luck Am I going about tackling this the wrong way or am I overlooking something very obivous?

Shared SPI ADC resource across task and deferred task

Hardware shared between tasks should have a syrncronizing object, like a mutex to guard it, which should work if implemented correctly. You should check the return value of the take operation to make sure you did get the mutex so it does protect the resource. Also, make sure that ALL the accesses to the hardware are under the protection of the mutex, for the full operation, and nothing makes an assumption about prior configuration that might have been assumed for the operation. One other possible issue is that many SPI ADCs, the channel number sent out doesn’t affect the reading you get back for that transaction, but for the next, so continually reading the same channel can be done with single transactions, but a ‘random’ read takes two transactions. (The driver could remember the last channel number sent, and if wrong, send the new channel and ignore the first result, and the get the data with a second read).

Shared SPI ADC resource across task and deferred task

Thanks Richard. Sorted now. It was indeed the ADC driver. Was happy reading in sequence but asking for two completley different channels upset it. Had to do what you said and send the request twice for it to accept the new channel request. Like your idea about the driver remembering last channel. Thanks for taking time to reply.