training.micrium.ucos-ii.rtos
TRANSCRIPT
Industrial Reference Design PlatformMicrium µC/OS-II RTOS
Release 1.0
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
2
Products without KernelsForeground/Background Systems
Background
Foreground #1
Foreground #2
Time
Task #1 Task #2 Task #3
Infinite loop
ISR #1 ISR #1
ISR #2
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
3
Foreground/BackgroundDisadvantages – Response Time
Background response time is the background execution time– Non-deterministic
• Affected by if, for, while ...
– May not be responsive enough– Changes as you change your code
Task #1 Task #2 Task #3
Infinite loop
ISR
Task #4
Poll to see if ISR occurred
Affected by if, for, while
Task #1 Task #2 Task #3
Infinite loop
ISR
Task #4
Poll to see if ISR occurred
Affected by if, for, while
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
4
Foreground?/Background TasksDisadvantages - Priority
All ‘tasks’/functions() have the same priority– Code executes in sequence– If an important event occurs, it’s handled at the same priority as everything
else– May need to execute the same code often to avoid missing an event
Task #1 Task #2 Task #3
Infinite loop
Task #4Task #1 Task #2 Task #3
Infinite loop
Task #4
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
5
What is an RTOS?
Real-Time Operating System– Software that manages the time of a microprocessor,
microcontroller, or a digital signal processor– Prioritizes the work to be done– Provides ‘multitasking’– Provides ‘services’ to the application
• Semaphores• Message mailboxes and queues• Event flags• Time delays, timers, and timeouts• Task management• Memory management• Bandwidth assessment ‘idle time’
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
6
Types of RTOSs
1. Pre-emptive:Always runs the highest available task. Tasks of identical priority share CPU time (fully pre-emptive with round robin time slicing)
2 . Cooperative:Context switches only occur if a task blocks, or explicitly relinquishes CPU control
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
7
RTOS Advantages
Software that manages the time of a microprocessor or microcontroller
– Ensures that the most important code runs first
Allows Multitasking– Do more than one thing at the same time– Application is broken down into multiple tasks, each handling one aspect of
your application– It’s like having multiple CPUs
Provides valuable services to your application– Time delays– Resource sharing– Inter-task communication and synchronization
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
8
Available RTOSs for LPC2300/2400A few of those available
Keil RTX Real-Time Kernel
– http://www.keil.com/arm/rl-arm/kernel.asp
Micrium µC/OS-II
– http://www.micrium.com/products/rtos/kernel/rtos.html
CMX-RTX
– http://www.cmx.com/rtx.htm
Express Logic ThreadX
– http://www.rtos.com/
Segger ebmOS
– http://www.segger.com/embos_general.html
uClinux
– As distributed with the EA LPC2468 board
FreeRTOS.org
– http://www.freertos.org/
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
9
Why NXP chose Micrium µC/OS-IIFor the LPC2468 Industrial Reference Design (IRD) Platform
Written in ANSI C– Source code provided– Consistent coding style
Pre-emptive
Deterministic
Royalty-free– Licensed on a per-end-product basis
Extensive Documentation and Support
Meets requirements of Safety-Critical Systems
Large user base– Used in hundreds of products all over the world
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
10
Micrium µC/OS-IIFeature Set
Full suite of modules in addition to the Operating System (RTOS)– µC/FS: File System– µC/Probe: Run-Time Monitoring– µC/TCP-IP: Networking Protocol– uC/HTTPs: HTTP Server– µC/GUI: Graphics User Interface– µC/USB: Universal Serial Bus
• Host: Mass Storage, HID, Communication Device Class (CDC)• Device: Bulk, Mass Storage, HID, CDC
– µC/CAN: Controller Area Network– µC/FL: Flash Loader– µC/LCD: VFD
Small Footprint
Scaleable
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
11
Micrium µC/OS-II and Related Files
CPUSpecific(Port)
CPUIndependent
BoardSupportPackage
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
12
Micrium µC/OS-II on the LPC2468 IRDModules we use
TCP-IP Protocol StackIncludes: IPv4, ARP, ICMP, UDP, TCP, BSD 4.4, and HTTPs
Real-Time Operating System (RTOS)Manage up to 255 tasks
Host USB Stack (Host Controller Driver for OHCI)Mass Storage Host Class driver enabled
USB Core( Core Device module needed for all applications)HID Class driver enabled, LPC24xx Target Device
CAN framework API ( abstracts CAN messages and signals)RX/TX CAN communication, LPC24xx Target Device
Run-Time data monitor Supports RS-232, TCP/IP, USB, JTAG
µC/Shell and µC/LCD usedLCD/VFD character based display driver
Microsoft compatible FAT File SystemBasic Package, MCU RAM disk driver included
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
13
Module Flash size RAM
BSP 8503 32
uC/LCD 384 6
App tasks 8697 4133
uC/USB Host 26565 10669
uC/USB Device 7410 513
uC/LIB 19744 228
uC/OS 8898 7584
uC/HTTP 4236 6696
uC/TCP-IP 77634 24531
uC/CAN 5892 2428
uC/Probe 1392 1070
uC/FS 17124 565
uC/Shell 10996 4240
Total 197475 62695
Micrium µC/OS-II on the LPC2468 IRDMicrocontroller Resource Allocation
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
14
Micrium µC/OS-II on the LPC2468 IRDTask Priorities
Task Name Priority Function
AppTaskStart()"Start Task" 1 Starts the Application Tasks
AppTaskKbd()"Keyboard" 2Reads status of push buttons; passes IDs of pressed buttons to AppTaskStart() in a queue.
HC_AsyncIOHandler "USBH AsynC" 4 USB Host Controller Asynchornous IO handler
OSProbe_Task "Probe TCP/IP" 5 µC/Probe Task
Terminal_OS_Task ""Terminal" 6 Terminal Task
"Net IF Rx Task" 7The task in which µC/TCP-IP receives data from a NIC or EMAC driver.
"Net Timer Task" 8 The task used by µC/TCP-IP to manage its timers.
"HTTP Server" 9 µC/HTTPs (Web Server)
"USBH MSC Demo" 10 Mass Storage Device Task
"USD device HID" 17 USB Device HID
"Echo CAN" 25 µC/CAN Echo task
AppTaskHeartbeat "HeartBeat" 28 Generates a 1Hz signal in the heartbeat pin
"uC/OS-II Tmr" 29 Manages µC/OS-II timers
"uC/OS-II Stat" 30 Collect stack usage statistics
"uC/OS-II Idle" 31 Executes when no other task is executing
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
15
Micrium µC/OS-II on the LPC2468 IRDPricing
TCP-IP Protocol StackIncludes: IPv4, ARP, ICMP, UDP, TCP, BSD 4.4, and HTTPs $11,500.00
Real-Time Operating System (RTOS)Manage up to 254 application tasks $4,995.00
Host USB Stack (Host Controller Driver for OHCI)Mass Storage Host Class driver enabled $9,750.00
USB Core( Core Device module needed for all applications)HID Class driver enabled, LPC24xx Target Device $4,700.00
CAN framework API ( abstracts CAN messages and signals)RX/TX CAN communication, LPC24xx Target Device $2,350.00
Run-Time data monitor Supports RS-232, TCP/IP, USB, JTAG $995.00
µC/Shell and µC/LCD usedLCD/VFD character based display driver $2,000.00
Microsoft compatible FAT File SystemBasic Package, MCU RAM disk driver included $3,750.00
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
16
Micrium µC/OS-II on the LPC2468 IRD Built upon the µC/OS-II Foundation
uC/LCD was ported for the Noritake VFD
uC/CAN was ported for the LPC23xx/24xx CAN controller
I2C Keypad Decoding was created
I2CTemperature Driver was created
I2C LED Blinker and Buttons driver was created
Piezo driver was created
Heartbeat task was created
Demonstration web pages for IRD components
BSP was ported from the Embedded Artists - LPC2468 base board to the IRD base board
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
17
RTOS Tasks
A task is a simple program that thinks it has the CPU all to itself
Each Task has– Its own stack space– A priority based on its importance
A task contains YOUR application code
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
18
What is a Task?
A task is an infinite loop
void Task (void *p_arg){ Do something with ‘argument’ p_arg; Task initialization; for (;;) { /* Processing (Your Code) */ Wait for event; /* Time to expire ... */ /* Signal from ISR ... */ /* Signal from task ... */ /* Processing (Your Code) */ }}
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
19
Designing with µC/OS-IISplitting an application into Tasks
High Priority Task
Low Priority Task
Task
Task
Task
Task
Task
Task
Event Event
Each Task
Infinite Loop
Importance
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
20
µC/OS-II Task States
TasksReside
InROM
Task Waiting For Event
TaskReady-To-Run
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
21
Why ‘Create’ a Task?
To make it ready for multitasking
The kernel needs to have information about your task– Its starting address– Its top-of-stack (TOS)– Its priority– Arguments passed to the task– Other information about your task
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
22
Creating a task with µC/OS-II
OSTaskCreateExt(void (*task)(void *parg), void *parg, OS_STK *pstk, INT8U prio, INT16U id, OS_STK *pbos, INT32U stk_size, void *pext, INT16U opt);
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
23
Initializing µC/OS-IIExecution Path
main
Disable Interrupts
Start the execution of the RTOS
Create the First Application Task (AppTaskStart)
Initialize internal OS structures
Disable Interrupts
Initialize:interrupt nesting countertask countercontext switch counterstatistics TCB and Events lists
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
24
Initializing µC/OS-IIFirst Application Task
No
Initialize the HW (BSP_Init)
Initialize TCP/IP
Initialize CAN
Initialize USB Host
Initialize USB Device
Start
Create the Application Tasks
Execute the AppTaskStart infinite task code
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
25
IRD Micrium µC/OS-II Exercise
Create a new task called “AppTaskHeartbeat”
This task will generate a 0.5 Hz square wave signal– Displayed by the Heartbeat LED (P2.30)
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
26
IRD Micrium µC/OS-II ExerciseSteps
1. Open the IAR project workspace
2. Insert the initialization of the Heartbeat pin (P2.30) in the bsp.c file
3. Insert the body of the “AppTaskHeartbeat” task in the app_heartbeat.c file
4. Insert the AppTaskHeartbeat protoytpe in the file app.c
5. Insert the function call “OSTaskCreateExt” to create the task in the “AppTaskStart” function in the app.c file
6. Define the AppTaskHeartbeat stack in the app.c file
7. Define the AppTaskHeartbeat priority and stack size in the app_cfg.h file
8. Compile and run the projectThese steps are covered in detail in
the upcoming slides
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
27
IRD Micrium µC/OS-II Exercise Project files
Please make sure that IAR version 4.42A4.42A is installed in your system– The project has only been tested in this version
• Newer IAR releases changed the linker configuration– the code won’t compile in the latest IAR version available on their web site
Uncompress the IRD project file in a directory close to the root directory
– Note: IAR won’t find some files if the PATH is too big
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
28
Exercise – Step 1Open IAR Micrium µC/OS-II project workspace
Project Name– LPC2468-IRD-OS-Probe-TCPIP-VFD.ewwLPC2468-IRD-OS-Probe-TCPIP-VFD.eww
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
29
Exercise – Step 2Prepare to configure P2.30 as an output
Open the bsp.c file and find the function GPIO_Init()
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
30
Exercise - Step 2Configure the P2.30 as an output
Add the following code at the end of the function
pinsel = PINSEL5;
pinsel &= 0xCFFFFFFF;
PINSEL5 = pinsel;
FIO2DIR |= DEF_BIT_30;
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
31
Exercise - Step 3Create the AppTaskHeartbeat function
Open file app_heartbeat.c and find the comment
/*Insert the AppTaskHeartBeat here */
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
32
Exercise - Step 3Create the AppTaskHeartbeat function
• An infinite loop is needed for all the tasks
• P2.30 is being toggled
• A 1 second delay is executed after modifying the pin voltage level
• Function OSTimeDlyHMSM generates the delay
void AppTaskHeartbeat(void *p_arg){
(void)p_arg;
while (DEF_TRUE) {
if( FIO2PIN & DEF_BIT_30){
FIO2CLR = DEF_BIT_30;
}else{
FIO2SET = DEF_BIT_30;
}
OSTimeDlyHMSM(0, 0, 1, 0);
}
}
Please insert the following code after the comment
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
33
Open file app.c and find the comment
/*Insert the AppTaskHeartbeat prototype here */
Exercise - Step 4Prepare to Insert the AppTaskHeartbeat Prototype
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
34
Exercise - Step 4Insert the AppTaskHeartbeat Prototype
Copy the following code line
extern void AppTaskHeartbeat (void *p_arg);
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
35
Open file app.c and find the comment
/*Insert the OSTaskCreateExt call here */
Exercise - Step 5Prepare to create the AppTaskHeartbeat task
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
36
Exercise - Step 5AppTaskHeartbeat creation
The function AppTaskCreateAppTaskCreate creates the application tasks
Add the following code at the end of the function AppTaskCreateAppTaskCreate (app.c file)
OSTaskCreateExt(AppTaskHeartbeat,
(void *)0,
(OS_STK *) &AppTaskHeartbeatStk[APP_TASK_HEARTBEAT_STK_SIZE - 1],
APP_TASK_HEARTBEAT_PRIO,
APP_TASK_HEARTBEAT_PRIO,
(OS_STK *)&AppTaskHeartbeatStk[0],
APP_TASK_HEARTBEAT_STK_SIZE,
(void *)0,
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
#if (OS_TASK_NAME_SIZE > 9)
OSTaskNameSet(APP_TASK_HEARTBEAT_PRIO, (CPU_CHAR *)"Heartbeat", &err);
#endif
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
37
Exercise - Step 6Prepare to define the AppTaskHeartbeat Stack
Find the following comment in the app.c file
/*Insert the AppTaskHeartbeat stack definition*/
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
38
Exercise - Step 6AppTaskHeartbeat Stack definition
Copy the following code line
static OS_STK AppTaskHeartbeatStk[APP_TASK_HEARTBEAT_STK_SIZE];
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
39
Exercise - Step 7Prepare to define stack size and priority
• Find the following comments in the app_cfg.h file
/*Insert the AppHeartbeat Task Priority*/
/*Insert the AppHeartbeat Stack Size*/
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
40
Exercise - Step 7Define Stack size and priority• The following constants need to be defined at this point:
#define APP_TASK_HEARTBEAT_STK_SIZE 96
#define APP_TASK_HEARTBEAT_PRIO (OS_LOWEST_PRIO - 3)
Note:The OS_LOWEST_PRIO is 31 in µC/OS-II
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
41
Exercise – Step 8Compile the project
Compile as per screenshot
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
42
Exercise – Step 8Run the project
Start debugger– Run project
You should see a the Heartbeat LED blinking at a 0.5 Hz rate
TSC Americas – IRD Platform – Micrium uC/OC-II RTOS
43