Uploaded by ghattas.akkad

Lab 6 ARM RTOS.pdf

advertisement
ARM RTOS
Due Date: Today +1 Week
In this lab, you will integrate the functions developed in the previous modules, and run them
concurrently in the ARM RTOS.
Objectives
 Understand the Cortex M4 RTOS.
 Understand the Keil RTX.
 Understand the concept of threads.
Cortex M4 RTOS
Cortex M4 RTOS
This chapter is an introduction to using a small footprint RTOS on a Cortex-M microcontroller. The use of
an RTOS represents a more sophisticated design approach, inherently fostering structured code
development that is enforced by the RTOS API. The RTOS structure allows you to take a more object
orientated design approach, while still programming in C. The RTOS also provides you with
multithreaded support on a small microcontroller. These two features actually create quite a shift in
design philosophy, moving us away from thinking about procedural C code and flowcharts. Instead we
consider the fundamental program threads and the flow of data between them. Since an RTOS-based
project is composed of well-defined threads it helps to improve project management, code reuse, and
software testing.
Figure 1 – RTOS Active Threads
The trade-off for this is that an RTOS has additional memory requirements and increased interrupt
latency. Typically, the Keil RTX RTOS will require 500 bytes of RAM and 5 KB of code, but remember that
some of the RTOS code would be replicated in your program anyway. We now have a generation of
small low-cost microcontrollers that have enough on-chip memory and processing power to support the
use of an RTOS. Developing using this approach is therefore much more accessible.
This study source was downloaded by 100000870726887 from CourseHero.com on 10-13-2023 09:01:01 GMT -05:00
https://www.coursehero.com/file/210681906/Lab-6-ARM-RTOSpdf/
The RTOS itself consists of a scheduler, which supports round-robin, preemptive and cooperative
multitasking of program threads, as well as time and memory management services. Inter-thread
communication is supported by additional RTOS objects, including signal triggering, semaphores, Mutex,
and a mailbox system. As we will see, interrupt handling can also be accomplished by prioritized threads
that are scheduled by the RTOS kernel.
Figure 2 – RTOS Kernel
Project Overview
In this project we will first look at setting up an introductory RTOS project for a Cortex-M-based
microcontroller. Here we will look at the main differences between a standard C program and an RTOSbased program.
Lab Procedure
Task


Open the project in “CMSIS RTOS first project” “CMSIS RTOS Threads” folder provided in the
email.
Mbed RTOS
Procedure – CMSIS RTOS + Threads
Here we have a simple program that runs to main and sits in a loop forever.
Right click on the Source Group 1 folder and add the RTX_Conf_CM.c file and the RTX library. You will
need to change the “types of file” filter to see the library file.
In main.c add the CMSIS RTOS header file.
#include “cmsis_os.h”
In the Options for Target\Target tab set RTX Kernel as the OS. Build the project and start the debugger.
The code will reach the for loop in main() and halt.
Open the Debug\OS Support\RTX Tasks and System window. This is a new debug window that shows us
the status of the RTOS and status of the running threads.
This study source was downloaded by 100000870726887 from CourseHero.com on 10-13-2023 09:01:01 GMT -05:00
https://www.coursehero.com/file/210681906/Lab-6-ARM-RTOSpdf/
While this project does not actually do anything, it demonstrates the few steps necessary to start using
CMSIS RTOS.
Figure 3 – Debugger Window
In this project, we will create and manage some additional threads.
Open the project in c:\exercises\CMSIS RTOS Threads.
When the RTOS starts main() runs as a thread and in addition we will create two additional threads.
First, we will create handles for each of the threads and then define the parameters of each thread.
These include the priority the thread will run at, the number of instances of each thread we will create,
and its stack size (the amount of memory allocated to it); zero indicates it will have the default stack
size.
Then in the main() function the two threads are created.
When the thread is created we can pass it a parameter in place of the NULL defines. Build the project
and start the debugger.
Start running the code and open the Debug\OS Support\RTX Tasks and System window. Now we have
four active threads with one running and the others ready.
This study source was downloaded by 100000870726887 from CourseHero.com on 10-13-2023 09:01:01 GMT -05:00
https://www.coursehero.com/file/210681906/Lab-6-ARM-RTOSpdf/
Figure 4 – Thread Debugging
Open the Debug\OS Support\Event Viewer window.
The event viewer shows the execution of each thread as a trace against time. This allows you to visualize
the activity of each thread and get a feel for the amount of CPU time consumed by each thread.
Figure 5 – Event Viewer
The event viewer shows the execution of each thread as a trace against time. This allows you to visualize
the activity of each thread and get a feel for the amount of CPU time consumed by each thread.
Now open the Peripherals\General Purpose IO\GPIOB window.
Our two LED threads are each toggling a GPIO port pin. Leave the code running and watch the pins
toggle for a few seconds.
Figure 6 – GPIOB Monitor Window
Before we can build this project, we need to make sure that the compiler will build code to use the FPU.
Open the Options for Target window and select the Target menu.
This study source was downloaded by 100000870726887 from CourseHero.com on 10-13-2023 09:01:01 GMT -05:00
https://www.coursehero.com/file/210681906/Lab-6-ARM-RTOSpdf/
Each thread calls functions to switch an LED on and off and uses a delay function between each on and
off. Several important things are happening here. First, the delay function can be safely called by each
thread. Each thread keeps local variables in its stack so they cannot be corrupted by any other thread.
Second, none of the threads blocks; each one runs for its full allocated time slice, mostly sitting in the
delay loop wasting cycles. Finally, there is no synchronization between the threads. They are running as
separate “programs” on the CPU and as we can see from the GPIO debug window the toggled pins
appear random.
Procedure – Mbed RTOS
In this lab, you will integrate the functions developed in the previous modules, and run them
concurrently in the mbed RTOS. Each of the previous tasks will be executed in one thread that is
scheduled by the RTOS. There are four threads:
1.
2.
3.
4.
Display the temperature on the LCD
Adjust the brightness of the LED using a potentiometer
Display an incrementing counter on the LCD
Blink an LED
Before starting the exercise, you need to add two files to the Project tree.
When you open the .uvproj file you will see the project tree in on the left.
Double click on the src folder to open the Add Files to Group ‘src’ menu.
Then navigate to: mbed-rtos/rtx/TARGET_XX/TOOLCHAIN_ARM/ (where XX stands for the Cortex core
name).
Add HAL_CMx.c and SVC_Table.s files to the project.
If you skip this step, your program won’t compile properly.
Process:

Create 4 threads to:
1.
2.
3.
4.
Display the temperature on the LCD
Adjust the brightness of the LED using a potentiometer
Display an incrementing counter on the LCD
Blink an LED
This study source was downloaded by 100000870726887 from CourseHero.com on 10-13-2023 09:01:01 GMT -05:00
https://www.coursehero.com/file/210681906/Lab-6-ARM-RTOSpdf/

Note that:
o
Every thread contains an infinite loop
o
Tasks are scheduled in a cooperative manner, namely, threads can go to waiting state after
each update, to let the next thread be scheduled
Since the LCD has to be accessed exclusively, we need a mutex to protect its single-access.



In the main program:
o
Clear the LCD display
o
Start all threads
mbed RTOS API:
o Provides easy-to-use API for programming mbed enabled devices
o Based on Keil RTX RTOS kernel implementation
o Uses the CMSIS-RTOS API open standard
CMSIS-RTOS API
o Common API for Real-Time operating systems
o Foundation of the official mbed RTOS
o Provides a standardized programming interface that is portable to many RTOS’s
o Hence enables software templates, middleware, libraries, and other components that
can work across various supported RTOS systems
Figure 7 – Blink Led Thread Example
This study source was downloaded by 100000870726887 from CourseHero.com on 10-13-2023 09:01:01 GMT -05:00
https://www.coursehero.com/file/210681906/Lab-6-ARM-RTOSpdf/
Powered by TCPDF (www.tcpdf.org)
Download