Skip to content

Latest commit

 

History

History
108 lines (89 loc) · 4.52 KB

L07.md

File metadata and controls

108 lines (89 loc) · 4.52 KB
layout title license
default
uC/OS-II - semaphores

Introduction

This lab requires you to make use of semaphores in uC/OS-II to solve a variety of synchronisation problems.

FRDM-K64F image

Exercises

  1. Make sure that you are up to date with the exercises so far. When you are ready to check your work, you can import the repositories and checkout the solution branches. Build, run, and observe the solution programs. Study the code carefully to make sure that you understand it. Ask your lab tutor for help with anything that you don't understand.

    $ cd ~/kf6010
    $ mbed import https://github.com/davidkendall/blinky-mbed-tt
    $ cd blinky-mbed-tt
    $ git checkout P03

    and

    $ cd ~/kf6010
    $ mbed import https://github.com/davidkendall/blinky-mbed-ucos-ii
    $ git checkout P03
  2. Now import the repository that is the starting point for your use of semaphores with uC/OS-II.

    $ cd ~/kf6010
    $ mbed import https://github.com/davidkendall/semaphore-mbed-ucos-ii
    $ cd semaphore-mbed-ucos-ii

    Build and run the program. Observe its behaviour carefully. Notice that the values of the count1 and count2 variables are shown on the LCD display. The LED on the FRDM-K64F board is green and is not flashing (you can ignore the RGB LED on the application board for the moment).

  3. Look carefully at the code in src/app/main.cpp. Notice the use of semaphores in appTaskCount1 and appTaskCount2. Now comment out the lines in these tasks that pend and post the lcdSem. You should comment out 4 lines in total. Build and run the program again. Ignore the warnings about unused variables. Observe its behaviour. What happens and why?

  4. Restore the lines that make use of the lcdSem. Build and run the program again. Observe its behaviour once more. Notice that the value of the count1 variable is incrementing much more quickly than the value of the count2 variable. How can that be happening? It is possible to replace the lcdSem with a pair of semaphores, lcdSem1 and lcdSem2, that can be used by appTaskCount1 and appTaskCount2 to preserve mutual exclusion and to ensure that both tasks make progress at the same rate. The appTaskCount1 task should pend on lcdSem1 and post lcdSem2. The apptaskCount2 task should pend on lcdSem2 and post lcdSem1. Create these semaphores (how should they be initialised to preserve mutual exclusion and avoid deadlock?) and implement the functionality described above. Build and run the program again. Both counts should now increment at the same rate.

  5. Study the initialisation of the semaphores in the previous exercise methodically. Try the following combinations of initialisation values:

           lcdSem1    lcdSem2
           ------------------
               0         0
               0         1
               1         0
               1         1
    

    In each case, build and run the program. Observe its behaviour and explain the results.

    Now add a 1 ms delay at the beginning of appTaskCount1, just immediately before the while loop. Try all of the combinations of initialisation value again. Build and run the program each time. Observe its behaviour and explain the results again.

  6. Do the task priorities of this system conform to the requirements of the rate monotonic (RM) scheduling method for fixed priority allocation? If so, explain why. If not, rewrite the program to comply with RM requirements. Build and run your new program.

Working at home

You'll need to spend about 5 or 6 hours per week, outside of scheduled classroom time, working on the exercises and doing further reading. The most important part of this will be the time that you spend programming. You can configure your own computer to enable you to do any of the work that you would normally do in the lab. If your own computer has a Linux OS installed, then you can simply follow the instructions for installing the GNU Arm Embedded Toolchain and pyOCD. If you have a Windows or Mac machine, then you should install Virtualbox and create an Ubuntu 16.04 Desktop guest OS to run on your machine. See [Using Virtualbox]({{ url }}{{ baseurl }}kf4005/L01.html#using-virtualbox) for guidance. You can then install the tools, as explained earlier.