Do you use RTOS? Typically, this type of thing will be handled by a high priority thread that receives an alarm to do some interrupt work.
If you do not use RTOS, you only have a few tasks, and the work performed by the interrupt is not too resource intensive, it can be easier if your high-priority work is performed in the context of an interrupt handler. If these conditions are not met, then the implementation of what you are talking about will be the beginning of the basic multi-tasking OS. It may be an interesting project in its own right, but if you just want to get the job done, you may need a simple RTOS.
Since you mentioned some of the features of the work you do, here is an overview of how I dealt with a similar problem in the past:
To process the received data via UART, one method that I used when working with a simpler system that does not have full support for the task (i.e. tasks are round, I na simple while loop) should have a common queue for data received from UART . When a UART interrupt occurs, data is read from the UDR RDR (Receive Data Register) and queued. The trick to dealing with this so that the queue pointers are not corrupted is to carefully make the queue pointers unstable and make sure that only the interrupt handler changes the pointer to the tail and that only the “foreground” task that reads the data in Queue has changed the pointer to the head. High Level Review:
Make sure that queue.head and queue.tail are volatile (or write these bits to the assembly) to make sure there are no problems with sequencing.
Now just make sure that your data queue received by UART is large enough to keep all the bytes that could be received before the foreground task gets a chance to run. The foreground task should print the data from the queue to its own buffers in order to create messages for the task "message processor".
Michael burr
source share