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

386                             15. Mbed RTOS PROJECTS

                 and no2. Main program starts two threads called producer and consumer. A memory pool and
                 a queue are defined at the beginning of the program. Thread producer allocates a memory pool
                 and sets variables no1 and no2 to 10 and 20, respectively, and puts them into the queue. Thread
                 consumer reads from the queue and displays the following text on the PC screen:

                   no1¼10 no2¼20
                   Here, structure osEvent points to the actual data



                                                15.9 Mbed MAIL


                   Mail is a very useful feature similar to a queue that is used to send and receive messages
                 between threads, with the added advantage that it can allocate memory pool. Mbed Mail
                 function alloc() allocates memory, put() puts a mail in the queue, get() gets a mail from
                 the queue, and free() returns the allocated block.
                   An example program (program: mail) using Mail is shown in Fig. 15.20. In this program a
                 structure is created as in the previous example with two integer numbers. A mail is created
                 with the name mailbox. Thread producer allocates a memory pool, stores 5 and 20 in vari-
                 ables no1 and no2, respectively. The data are then put into the mail using statement mail-
                 box.put(mail). The producer thread then waits for 2s before freeing the memory pool.
                 Thread consumer gets the mail and then displays the contents of no1 and no2 on the PC
                 screen. Note that osEvent is a pointer to the data.



                                           15.10 Mbed RTOS TIMER

                   The RTOS timer can be used to create and control one-shot as well as periodic timer func-
                 tions. A timer can be started, restarted, or stopped. Function stop() stops a timer, function
                 start(ms) starts (or restarts) a timer where the argument is the timer period in milliseconds.
                   An example use of an RTOS timer program (program: rtostmr) is shown in Fig. 15.21.In
                 this example an RTOS Timer is created with the name timer. This timer is then started with a
                 period of 500ms. After 5s the timer is stopped.



                                                15.11 SUMMARY

                   Multitasking is an important concept in the development of complex real-time systems. In
                 this chapter we have learned the following:
                 • Multitasking scheduling algorithms
                 • Mbed RTOS
                 • Mbed thread, queue, signal, mutex, semaphore, mail, and RTOS timer
                 • Example projects using multitasking features of Mbed
   395   396   397   398   399   400   401   402   403   404   405