Page 48 - Designing Autonomous Mobile Robots : Inside the Mindo f an Intellegent Machine
P. 48

The Basics of Real-time Software (For Mere Mortals)

               so that it will be available for the first “return” into the thread. Only the main
               thread, which starts at reset, does not require its stack to be initialized in this way.
               Finally, notice that even though this process is stack oriented, such interrupts are not
               reentrant. For this reason, the interrupt that causes the context switch must remain
               masked (disabled) until the context has been restored. In most systems, this is the
               default and the programmer would have to reenable the interrupt intentionally to
               cause a problem.

               If you are going to write programs for dedicated microprocessors that do not have operating
               systems, you need to master this technique. If you are using a system that has context
               switching, it is important to know how it works in order to use it most efficiently.


               Task switching

               A real-time kernel can be extremely simple or quite complex and if you are working
               with small microcomputers you can readily write your own to fit your needs. There
               are many ways in which the kernel can interrupt and resume execution of various
               tasks. The most obvious way is in response to interrupt events.


               Interrupt events

               The serial communications task interrupt described above is an event. Events can
               cause the kernel to divert control to a specific task. The task must then relinquish
               the CPU by calling for another event (going back through the looking glass). Timers
               can also create events. In more complex kernels the task can relinquish the CPU
               while asking for one or more events to restart it.
               For example, a communications task might ask for a character from the serial port
               OR a timeout of 1 second, whichever comes first. If the timer expired first, the inter-
               rupt would return a special flag to the calling thread to indicate that the event was a
               time out and not a valid character being received. The beauty of event-driven task
               switching is that it is very efficient. Only threads with work to do get the CPU’s
               resources.

               The kernel thus stands between the interrupts and the threads. Program flow thus
               goes from the interrupt to the kernel, and then through a context switch to a con-
               suming thread if and when it is expecting the data. If your system is very simple and
               resources like serial ports are not shared between threads, then a true kernel may be
               unnecessary.





                                                        31
   43   44   45   46   47   48   49   50   51   52   53