Page 383 - ARM Based Microcontroller Projects Using MBED
P. 383

15.4 PROJECT 1—DIFFERENT FLASHING A PAIR OF LEDs—USING Mbed THREAD  369
            assigned to PC_0 and PC_1, respectively. Two functions named LEDAControl and
            LEDBControl are created to flash the two LEDs. LEDAControl flashes LEDA every second,
            while LEDBControl flashes LEDB every 0.5s. Inside the main program threads LEDAControl
            and LEDBControl are started. The main program loops forever, not doing anything useful. In
            actualfact,mainisanotherthreadandoneoftheLEDscouldhavebeenflashedinsidethemain.
            Because main is another thread, in this program there are actually three threads, two of them
            flashing the two LEDs and the third one just repeating itself in a loop and wasting CPU re-
            sources. We can place the main thread in a wait state so that it does not consume any CPU re-
            sources. This can be done by declaring a semaphore at the beginning of the program, such as:
              Semaphore sema;
              And then place the main thread in a forever wait state with the following statement:

              sema.wait(osWaitForever);
              We can also set the priority of the main to idle (see the following section) so that it does not
            consume any CPU resources. Another option is to set the thread to wait forever by the fol-
            lowing statement:
              Thread::wait(osWaitForever);


            15.4.6 Mbed Thread Priorities

              The default thread priority is osPriorityNormal. A thread can be set to one of the following
            priorities (Mbed OS2 only):


                        osPriotityIdle                     lowest priority ( 3)
                        osPriorityLow                      low priority ( 2)
                        osPriorityBelowNormal              below normal priority ( 1)
                        osPriorityNormal                   normal (default) priority (0)
                        osPriorityAboveNormal              above normal priority (+1)
                        osPriorityHigh                     high priority (+2)
                        osPriorityRealtime                 real-time (highest) priority (+3)


              Table 15.1 gives a list of the important thread related functions with example
            usage (further details and complete list can be obtained from the web site:
            https://os.mbed.com/handbook/RTOS):


            15.4.7 Thread States
              A thread can be in any one of the following four states at any time:

              RUNNING: this is the currently running thread (only one thread can be in this state
              at any time).
   378   379   380   381   382   383   384   385   386   387   388