I have been developing a bare metal control application on dsPIC33EP256MC506. My application consists of infinite loop in the background and three foreground "application" interrupt service routines (isr). Besides those application isrs there is also let's say system isr which services the SPI end of transaction interrupt requests. In each SPI interrupt new status of the remote digital inputs (state of contactors) is being read. This information is then used for the calculations (logic expressions determining when to close or open individual contactors) done in the background loop.
My problem is that I am not sure how to ensure that during one pass through the background loop the state of the remote digital inputs will be consistent. Better saying I have been looking for a mechanism how to avoid following situation
Can anybody recommend me a simple and robust solution for this kind of problem?.
EDIT:
Below given answers inspired me to following possible solution. I will define SPI driver:
The update
function will be executed in the background loop and it will do following
if(new_data_ready){
new_data_ready = false;
transferDataToMirror();
}
startTransaction();
The SPI end of transaction interrupt will be serviced in following simple manner:
new_data_ready = true;
The client's code from the SPI driver point of view will access to the digital_ inputs_mirror
via getInputsState
function call. The digital_ inputs_mirror
will be updated in synchronous manner in the background loop via transferDataToMirror()
which will retrieve data from the SPI peripheral registers.