Page 260 - Embedded Microprocessor Systems Real World Design
P. 260

is set. When a byte is received, the receive interrupt service routine  (ISR) sets this
                 semaphore, and the RTOS activates the receive task. The receive task reads the byte
                 from the universal asynchronous receiver/transmitter  (UART) register, processes
                 the  data, and places it in  a buffer for  the output task. The receive task has the
                 highest priority because the system cannot afford to miss a serial input byte. Once
                 the receive task has finished processing the received byte, it becomes not ready until
                 the  next  receive  interrupt  occurs, by  asking  the  RTOS  to  suspend  it  until  the
                 semaphore is set again.
                    When the buffer is near full, the XOFF task is activated (becomes ready) by the
                 receive  processing. XOFF  could  be  “created” by  receive  processing, where  the
                 receive processing requests that XOFF processing be activated, or XOFF could have
                 previously been set up to be activated by  a semaphore like receive processing was.
                 XOFF runs until it has successfully sent the XOFJ? signal to the host or until it is
                 preempted by the receive task. If receive preempts XOFF, it gets control (from the
                 RTOS), processes the receive data, and then control is returned  to XOFF. Again,
                 the highest-priority ready task is the one executed.
                    XON is next in priority. If  output processing empties the buffer past a certain
                 point, it activates XON. Output processing has the lowest priority, which is possi-
                 ble because the XOFF task prevents the buffer from overflowing, so  no data ever
                 are missed. Of  course, if  the receive data flow cannot be suspended with XOFF,
                 then output processing would have to have a higher priority so the buffer does not
                 overflow.
                    None  of  this happens by  magic. The RTOS can  activate a task, when  a sema-
                 phore is set or a message is received, only if it was previously told to do so. Also, in
                 an RTOSbased  system, the interrupt service routines usually get control via  the
                 RTOS, so an ISR may not need to set a semaphore to start a task. Instead, the RTOS
                 can schedule the task upon activation of the ISR itself.
                    A  final  note  about  scheduling:  Both  sequential  and  preemptive  sched-
                 uling  systems  allow  a  task  to  run  until  finished.  The  difference  is  that,  in  a
                 preemptive  system, a task  runs  until  finished  or  until  preempted. Between two
                 ready  tasks  of  different  priorities,  the  higher-priority task  always  preempts  the
                 lower-priority task  and  finishes first. If  two  tasks  of  equal  priority  are  ready  at
                 the same time, a sophisticated RTOS usually activates the one that has been idle
                 the longest.
                    A note about terminology:  A task is considered active when it actually is running,
                 when it has been given control of the CPU by the RTOS. A ready task is in the list
                 of tasks waiting to run. A task can be not ready, such as the receive processing on
                 the protocol converter while waiting for the start semaphore.
                    The RTOS knows about only those tasks it has been told about (those that have
                 been  created).  The  code  for  other  tasks  may  reside  in  memory, but  they  are
                 invisible to the RTOS until they are created.




                 Real-Time Operating Systems                                          241
   255   256   257   258   259   260   261   262   263   264   265