- Freescale Semiconductor

Download Report

Transcript - Freescale Semiconductor

Rapid Prototyping Solutions
Module 2: Introduction to Real-Time
Operating Systems
TM
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
Module Objectives
►This
session describes basic RTOS concepts using
the MQX RTOS
►It
will cover tasks, scheduling, semaphores and more
with hands-on examples
►You’ll
learn how to use a RTOS and the advantages
that using one provides
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
2
Module Agenda
►What
is an RTOS
►MQX
Basics: Tasks
•
Hands-on
►MQX
•
Hands-on
►MQX
•
•
Basics: Scheduling
Basics: Task Synchronization
Semaphores
Hands-on
►Additional
Resources
►Review
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
3
Module Agenda
►What
is an RTOS?
►MQX
Basics: Tasks
•
Hands-on
►MQX
•
Hands-on
►MQX
•
•
Basics: Scheduling
Basics: Task Synchronization
Semaphores
Hands-on
►Additional
Resources
►Review
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
4
Operating Systems
► The
term “operating system” can
be used to describe the collection
of software that manages a
system’s hardware resources
User
► This
software might include a file
system module, a GUI and other
components
Application
► Often
times, a “kernel” is understood
to be a subset of such a collection
Operating System
► Characteristics



HARDWARE
Resource management
Interface between application and hardware
Library of functions for the application
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
5
Embedded Operating Systems
►
Fusion of the application and the OS to one unit
►
Characteristics

Resource management



Primary internal resources
Less overhead
Code of the OS and the
application mostly reside in
ROM
User
Operating System + Application
HARDWARE
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
6
Real Time Operating Systems
►A
real-time operating system (RTOS) manages
the time of a microprocessor or microcontroller
►Features
of an RTOS:
 Allows
multi-tasking
 Scheduling
of the tasks with priorities
 Synchronization
 Inter-task
 Time
of the resource access
communication
predictable
 Interrupt
handling
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
7
Why use an RTOS?
►
Plan to use drivers that are available with an RTOS
►
Would like to spend your time developing application
code and not creating or maintaining a scheduling
system
►
Multi-thread support with synchronization
►
Portability of application code to other CPUs
►
►
Resource handling
Add new features without affecting higher priority
functions
►
Support for upper layer protocols such as:

TCP/IP, USB, Flash Systems, Web Servers,

CAN protocols, Embedded GUI, SSL, SNMP
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
8
Freescale MQX
►We
will be using Freescale MQX to demonstrate
these RTOS concepts
►Freescale

http://www.freescale.com/mqx
►Default

MQX Software can be downloaded:
Freescale MQX folder:
C:\Program Files\Freescale\Freescale MQX 3.5
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
9
MQX Directory Structure
►Described
►Folders







in the MQX Release Notes
are:
config
demo
doc
lib
mqx
tools
And then the RTCS, USB, and MFS stacks
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
10
MQX Directory Structure (Cont.)
►The
“mqx” directory is heart of
MQX
►Folders
are:
build
examples
source







bsp
io
psp
MQX API source
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
11
Changing Configuration Options
►User
configuration options are set in
<mqx_install>/config/<board>/user_config.h
►Change
the default serial port to UART1:
#define BSP_DEFAULT_IO_CHANNEL
"ttyb:"
#define BSP_DEFAULT_IO_CHANNEL_DEFINED
#define BSPCFG_ENABLE_TTYB
1
►Always
need to re-build all the libraries with this
new configuration
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
12
Module Agenda
►What
is an RTOS?
►MQX
Basics: Tasks
•
Hands-on
►MQX
•
Hands-on
►MQX
•
•
Basics: Scheduling
Basics: Task Synchronization
Semaphores
Hands-on
►Additional
Resources
►Review
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
13
MQX RTOS Tasks
► A system
► Tasks
► Only
consists of multiple tasks
take turns running
one task is active (has the processor) at any given time
► MQX
manages how the tasks share the processor (context
switching)
► Task
Context
• Data structure stored for each task, including registers and a
list of owned resources
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
14
Typical Task Coding Structure
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
15
Module Agenda
►What
is an RTOS?
►MQX
Basics: Tasks
•
Hands-on
►MQX
•
Hands-on
►MQX
•
•
Basics: Scheduling
Basics: Task Synchronization
Semaphores
Hands-on
►Additional
Resources
►Review
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
16
Write Your First RTOS Application
Exercise:
Run your first application
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
17
Hands On: Run your first application
► The
application has only one task – the Init Task
 Prints out welcome
 Loops, incrementing x
► Follow
the directions in Lab 1 to compile and run the lab
using CodeWarrior 10
► Pause
the execution to see the init task running
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
18
Task States
►
A task is in one of these logical states:

blocked



active


the task is ready and is running because it’s the highestpriority ready task
ready


the task is blocked and therefore not ready
it’s waiting for a condition to be true
the task is ready, but it’s not running because it isn’t the
highest-priority ready task
terminated

the task has finished all its work, or was explicitly
destroyed
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
19
Task States
Task Finishes
Explicit
Termination
Terminated
Task
Starts
Active
Blocking
Call
Higher-priority Task
becomes Ready
Time Slice Expires
Interrupt comes in
Blocked
Context Switch
Object
Available
Timeout Expires
Ready
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
20
Module Agenda
►What
is an RTOS?
►MQX
Basics: Tasks
•
Hands-on
►MQX
•
Hands-on
►MQX
•
•
Basics: Scheduling
Basics: Task Synchronization
Semaphores
Hands-on
►Additional
Resources
►Review
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
21
Priorities
►Each
•
•
task is assigned a priority
Higher number means lower priority
0 is highest priority
►Used
by scheduler to determine which task to run next
►User
tasks should run at priority 8 or higher.
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
22
T
Scheduler
►Common
Scheduling Configurations:
FIFO (also called priority-based preemptive)


The active task is the highest-priority task that has been
ready the longest
Round Robin


The active task is the highest-priority task that has been
ready the longest without consuming its time slice
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
23
T
Priority Based FIFO Scheduling
high
priority
low
FIFO
list of
ready
tasks
Ready
CPU
Scheduler
active
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
processor time
TM
T
Priority Based FIFO Scheduling
high
priority
low
FIFO
list of
ready
tasks
Ready
CPU
Scheduler
active
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
processor time
TM
T
Priority Based FIFO Scheduling
high
priority
low
FIFO
list of
ready
tasks
Ready
CPU
Scheduler
active
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
processor time
TM
T
Round-Robin Scheduling
Task 1
75ms
Same
Priority
Task 2
50ms
Time Slice = 50ms
Task 3
60ms
time
Ready
Task1
T0
Task2
50ms
Task3
100ms
Task1 Task3
150ms
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
200ms
time
TM
T
MQX Tasks
►Tasks
can be automatically
created when MQX Starts; also,
any task can create another task
by calling _task_create() or
_task_create_blocked()
function _task_create()
puts the child task in the ready
state and the scheduler puts the
higher priority task to run
Active
_task_abort
_task_block
_task_destroy
►The
_task_create_blocked is
used the task is not ready until
_task_ready() is called
►If
Blocked
Terminated
_task_ready
_task_create
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
Ready
TM
28
Creating a Task
►
When creating a task you have to:
 Make the task prototype and index definition
#define INIT_TASK 5
extern void init_task(uint_32);

Add the task in the Task Template List
TASK_TEMPLATE_STRUCT MQX_template_list[] =
{
{ TASK_INDEX, TASK, STACK, TASK_PRIORITY,
TASK_NAME, TASK_ATTRIBUTES, CREATION_PARAMETER,
TIME_SLICE}
}
Using the init_task example:
TASK_TEMPLATE_STRUCT MQX_template_list[] =
{
{INIT_TASK, init_task, 1500, 9, "init",
MQX_AUTO_START_TASK, 0, 0},
}
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
29
Creating a Task
►
When creating a task you have to:

Make the task definition
void init_task(void)
{
/* Put the Task Code here */
}

During execution time, create the task using
task_create()
(if it is not an autostart task)
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
30
MQX_Template_List
{ WORLD_ID, world_task, 0x3000, 9,
"world_task",
MQX_AUTO_START_TASK, 0L, 0},
{ HELLO_ID, hello_task, 0x1000, 8,
“hello_task",
MQX_TIME_SLICE_TASK, 0L, 100},
{ LED_ID, led_task, 0x2000, 10,
“LED Task",
MQX_AUTO_START_TASK |
MQX_TIME_SLICE_TASK, 0L, 50},
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
31
T
{INIT_TASK,
init_task, 1500, 11,
"init",
MQX_AUTO_START_TASK,
0, 0},
{TASK_A, Task_A,
1500, 10, “Task A",
0, 0, 0},
MQX - Task Management Example
void init_task(void)
{
_task_create(0,TASK_A,0);
...
_task_ready(Task_B);
...
}
init_task is
created when
MQX starts
void Task_A(void)
{
...
_task_create_blocked(0,TASK_B,0);
...
_task_abort(TASK_A);
}
{TASK_B, Task_B,
1500, 9, “Task B",
0, 0, 0},
void Task_B(void)
{
...
_task_abort(TASK_B);
CPU Time
}
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
Module Agenda
►What
is an RTOS?
►MQX
Basics: Tasks
•
Hands-on
►MQX
•
Hands-on
►MQX
•
•
Basics: Scheduling
Basics: Task Synchronization
Semaphores
Hands-on
►Additional
Resources
►Review
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
33
Write Your First RTOS Application
Exercise:
Scheduling Lab
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
34
Hands On: Scheduling
► Add
the print task to our application
•
Init Task:
 AUTOSTART, priority 9
 Prints out welcome
 Loops, incrementing x
• Print Task:
 AUTOSTART, priority 10
 Loops, printing the value of x
►Add
a _time_delay() to block the init task after incrementing x
►Follow
the directions in Lab 2 to compile, download and run the
code
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
35
Task Aware Debugging
►Task Aware
Debugging is available to analyze tasks
running on MQX
►Look
at the ready queues and task state
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
36
Module Agenda
►What
is an RTOS?
►MQX
Basics: Tasks
•
Hands-on
►MQX
•
Hands-on
►MQX
•
•
Basics: Scheduling
Basics: Task Synchronization
Semaphores
Hands-on
►Additional
Resources
►Review
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
37
Why Synchronization?
►
Synchronization may be used to solve:








►
Mutual Exclusion
Control Flow
Data Flow
Synchronization Mechanisms include:
Semaphores
Events
Mutexs
Message Queues
EF
M
The correct synchronization mechanism
depends on the synchronization issue being
addressed
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
38
Control Flow with Synchronization
►One
task affects another’s execution, so that tasks execute in an
application-controlled order
Active Task
Post
Wait
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
Blocked
Task
TM
39
Mutual Exclusion
►
Allowing only one task at a time to access a shared resource
►
Resource may be devices, files, memory, drivers, code…
►
Mutual exclusion locks the resource
TASK 1
Lock
do work
Unlock
Protected Resource
M
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TASK 2
Lock
do work
Unlock
TASK 3
Lock
do work
Unlock
TASK 4
Lock
do work
Unlock
TM
40
Competence Condition
►What
happens if two tasks access the same resource
at the same time?

We call this “competence condition”. When two or
more tasks read or write on share a resource at a
certain moment
►Why
the “competence condition” can be a
problem?




Memory corruption
Wrong results
Unstable application
Device conflicts
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
41
Module Agenda
►What
is an RTOS?
►MQX
Basics: Tasks
•
Hands-on
►MQX
•
Hands-on
►MQX
•
•
Basics: Scheduling
Basics: Task Synchronization
Semaphores
Hands-on
►Additional
Resources
►Review
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
42
Semaphores
►A semaphore
is a protocol mechanism offered by most
multitasking kernels. Semaphores are used to:

Control access to a shared resource
(mutual exclusion)

Signal the occurrence of an event

Allow two tasks to synchronize their activities
►A semaphore
is a token that your code acquires in
order to continue execution
►If
the semaphore is already in use, the requesting task
is suspended until the semaphore is released by its
current owner
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
43
How Semaphores Work
►
A semaphore has:
counter — maximum number of concurrent accesses
 queue — for tasks that wait for access

►
If a task waits for a semaphore

►
if counter > 0
counter is decremented by 1
task gets the semaphore and can do work
else
task is put in the queue
If a task releases (post) a semaphore

if at least one task is in the semaphore queue
appropriate task is readied, according to the queuing
policy
else
counter is incremented by 1
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
44
Using Lightweight Semaphores
►
To declare a lightweight semaphore:
LWSEM_STRUCT sem;
►
To create a lightweight semaphore:
_mqx_uint _lwsem_create(lwsem_ptr,initial_count)
►
To wait for a lightweight semaphore
_mqx_uint _lwsem_wait(lwsem_ptr)
►
To post a lightweight semaphore:
_mqx_uint _lwsem_post(lwsem_ptr)
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
45
Module Agenda
►What
is an RTOS?
►MQX
Basics: Tasks
•
Hands-on
►MQX
•
Hands-on
►MQX
•
•
Basics: Scheduling
Basics: Task Synchronization
Semaphores
Hands-on
►Additional
Resources
►Review
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
46
Write Your First RTOS Application
Exercise:
Semaphore Lab
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
47
Mutual Exclusion Hands-on
►
The application uses the two tasks from the previous
labs, but adds semaphore control for accessing the
variable
►
Follow the directions in Lab 3 to compile, download,
and run the code
 Declare a global lightweight semaphore
 Create a lightweight semaphore, count = 1
 In both tasks:


►
Before accessing x, wait on the lightweight semaphore
After accessing x, post the lightweight semaphore
Look at TAD to analyze the semaphores
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
48
Module Agenda
►MQX
RTOS Basics
►Write
Your MQX Application
►MQX
Tasks
•
•
Task scheduling
Hands-on
►Task
•
•
Synchronization
Semaphores
Hands-on
►Additional
Resources
►Review
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
49
Freescale MQX™ Documentation
►MQXUG
User Guide
►MQXRM
Reference Manual
►MQXUSBHOSTAPIRM
►MQXUSBDEVAPI
USB Device API Reference
►MQXUSBHOSTUG
►MQXRTCSUG
►MQXMFSUG
►MQXIOUG
►MQXFS
USB Host API Reference Manual
USB Host User Guide
RTCS User Guide
File System User Guide
I/O Drivers User Guide
Software Solutions Fact Sheets
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
50
Further Reading and Training
►TWR-MCF51CN-KIT
►MCF5225x
– Lab Document
►MQX
Release Notes
►MQX
User’s Guide
►Writing
►Using
►How
Lab Document
First MQX Application (AN3905)
MQX: RTCS, USB, and MFS (AN3907)
to Develop I/O Drivers for MQX (AN3902)
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
51
Further Reading and Training (Cont.)
►Videos:



MCF5225x & Freescale MQX introduction
Getting started with MCF5225x and Freescale MQX Lab Demos
And more
►vFTF



www.freescale.com/mqx
technical session videos www.freescale.com/vftf
Introducing a modular system, Serial-to-Ethernet V1 ColdFire® MCU
and Complimentary MQX™ RTOS
Writing First MQX Application
Implementing Ethernet Connectivity with the complimentary Freescale
MQX™ RTOS
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
52
In Summary
By now, you should be able to:
►Understand
what an RTOS is and how they can be
used
►Create
tasks, schedule them and add
synchronization using MQX
►Create
your own MQX applications
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of Freescale Semiconductor, Inc.,
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions Logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SmartMOS, TurboLink
and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2010.
TM
53
TM