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

Activate  solenoid
                   Start solenoid timer by  setting SOLENOID to 20
                   Poll for  19ms, checking to see if  SOLENOID went to  0
                   Polling loop Ands that it is time to start an ADC  conversion.
                   Call ADC  routine
                        Start ADC conversion
                        Wait for ADC to complete
                          *** Right here, the timer  interrupt occurs, so the ISR decrements
                          SOLENOID. SOLENOID decrements to 0.
                          Read/store ADC  result
                          Return to polljng loop
                        Polling loop checks SOLENOID, fhds it has rolled to 0, turns off
                            solenoid.

                 Now, in an RTOS environment, it might work like this:

                   Solenoid/pump  driver task turns on solenoid and suspends for 20ms.
                   19ms go by, during which other tasks are executed
                   Some event tells RTOS that it is time to start an ADC  conversion
                   RTOS  starts ADC  conversion task
                        ADC  conversion is  started
                          *** Again, the timer  interrupt occurs, and the RTOS finds that
                            20ms has gone by.
                   RTOS  reactivates  solenoid/pump driver task
                        Solenoid is turned off
                        Pump is turned on
                        Other processing  goes on until solenoid/pump drive task  suspends
                            again.
                   RTOS  reactivates ADC  task. but now  it is too late. ADC  result is bad.

                   The result here is that, sometimes, the analog-todigital conversion will be bad.
                 There  are  a  number  of  ways  to  fix  this. The ADC  task  could  be  given  higher
                 priority than the solenoid/pump driver task. Or, before starting the conversion, the
                ADC task could tell the RTOS that it is about to begin a noninterruptible function
                 (if the RTOS supports that). Or, the ADC  task could ask temporarily to have  its
                 priority set higher than the solenoid task until the conversion is complete (again,
                 assuming the RTOS supports it). The point is that, in an RTOS environment, any
                 event that results in an RTOS function being executed can result in a task switch.
                 An ISR does not necessarily return to the task that was executing when the inter-
                 rupt occurred-at  least not  right  away.  You  must  take  this into  account  in  the
                 software. You do not know when interrupts will occur, so you must assume they will
                 occur at the worst possible time.


                 ReabTime Operating Systems                                           249
   263   264   265   266   267   268   269   270   271   272   273