Hands-on Workshop: Freescale MQX and Tower System RTOS Getting Started (Part 2)

Similar documents
The components in the middle are core and the components on the outside are optional.

Real-Time Programming

Freescale MQX TM Real-Time Operating System. User s Guide

Freescale MQX Lite RTOS

Introduction to Real-Time Operating Systems

Freescale MQX TM Real-Time Operating System. User s Guide

Zilog Real-Time Kernel

Freescale MQX RTOS for Kinetis SDK Release Notes version beta

Tasks. Task Implementation and management

Embedded Systems. 5. Operating Systems. Lothar Thiele. Computer Engineering and Networks Laboratory

EE458 - Embedded Systems Lecture 8 Semaphores

Micrium µc/os II RTOS Introduction EE J. E. Lumpp

ZiLOG Real-Time Kernel Version 1.2.0

Micriμm. Getting Started with Micriμm s. Matt Gordon, Sr. Applications Engineer. 9L05I Renesas Electronics America Inc.

FreeRTOS. A Brief Overview. Christopher Kenna. October 1, Avionics. FreeRTOS 1 / 34

What s An OS? Cyclic Executive. Interrupts. Advantages Simple implementation Low overhead Very predictable

AUTOBEST: A United AUTOSAR-OS And ARINC 653 Kernel. Alexander Züpke, Marc Bommert, Daniel Lohmann

GLOSSARY. VisualDSP++ Kernel (VDK) User s Guide B-1

RTOS Real T i Time me Operating System System Concepts Part 2

Embedded Systems: OS. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

AC OB S. Multi-threaded FW framework (OS) for embedded ARM systems Torsten Jaekel, June 2014

Embedded Systems: OS

Reference Model and Scheduling Policies for Real-Time Systems

REAL TIME OPERATING SYSTEMS: A COMPLETE OVERVIEW

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Virtual Machine Design

Micrium OS Kernel Labs

Exam TI2720-C/TI2725-C Embedded Software

M52259DEMOKIT One-stop-shop connectivity MCU with USB, Ethernet and CAN, featuring Freescale MQX software solutions

real-time kernel documentation

Efficiency and memory footprint of Xilkernel for the Microblaze soft processor

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Lecture 3: Concurrency & Tasking

Lecture 4: Real Time Semaphores

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

SMD149 - Operating Systems

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

FreeRTOS. A Brief Overview. Christopher Kenna. October 1, Avionics. FreeRTOS 1 / 34

Software Development with an Open Source RTOS

Introduction to the ThreadX Debugger Plugin for the IAR Embedded Workbench C-SPYDebugger

CS A320 Operating Systems for Engineers

Component validity and internal error checking functionality to ensure reliable operation

Introduction to OS Synchronization MOS 2.3

Lecture 3. Introduction to Real-Time kernels. Real-Time Systems

Semaphores. Chapter. verview inary Semaphores for Task Synchronization utex Semaphores to Solve Mutual Exclusion Problems

Module 1. Introduction:

Multiprocessor and Real- Time Scheduling. Chapter 10

UNIT:2. Process Management

Preemptive Scheduling

CS370 Operating Systems Midterm Review

CODE TIME TECHNOLOGIES. Abassi RTOS. CMSIS Version 3.0 RTOS API

Chapter 6: Process Synchronization

V. 2. ( Copyright 2004, 2006 Yuri Tiomkin

Operating Systems Design Fall 2010 Exam 1 Review. Paul Krzyzanowski

BASICS OF THE RENESAS SYNERGY PLATFORM

CSCI-GA Operating Systems Lecture 3: Processes and Threads -Part 2 Scheduling Hubertus Franke

BASICS OF THE RENESAS SYNERGY TM

REAL-TIME MULTITASKING KERNEL FOR IBM-BASED MICROCOMPUTERS


Real Time Operating System: Inter-Process Communication (IPC)

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

CODE TIME TECHNOLOGIES. Abassi RTOS. Porting Document. ARM Cortex-M3 CCS

Lesson 6: Process Synchronization

Operating Systems Comprehensive Exam. Spring Student ID # 3/20/2013

Real-time operating systems and scheduling

Concurrent Programming. Implementation Alternatives. Content. Real-Time Systems, Lecture 2. Historical Implementation Alternatives.

Concurrent Programming

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

ArdOS The Arduino Operating System Reference Guide Contents

Intertask Communication

Resource management. Real-Time Systems. Resource management. Resource management

Systemy RT i embedded Wykład 11 Systemy RTOS

Nios II. uc/os-ii porting with Nios II Altera Corporation

Processes. Dr. Yingwu Zhu

Quick Start Guide for TWR-MCF5225X TOWER SYSTEM. MCF5225X ColdFire connectivity MCUs

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors.

Deadlock. Lecture 4: Synchronization & Communication - Part 2. Necessary conditions. Deadlock handling. Hierarchical resource allocation

Hands-On Workshop: Freescale MQX Drivers and BSP s

Short Term Courses (Including Project Work)

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Embedding OS in AVR microcontrollers. Prof. Prabhat Ranjan DA-IICT, Gandhinagar

Multitasking / Multithreading system Supports multiple tasks

Concurrency: Deadlock and Starvation

RT3 - FreeRTOS Real Time Programming

Lecture Topics. Announcements. Today: Uniprocessor Scheduling (Stallings, chapter ) Next: Advanced Scheduling (Stallings, chapter

OPERATING SYSTEMS CS3502 Spring Processor Scheduling. Chapter 5

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Midterm Exam. October 20th, Thursday NSC

The University of Texas at Arlington

Last Class: Deadlocks. Today

Operating Systems Comprehensive Exam. Spring Student ID # 3/16/2006

Quadros. RTXC Kernel Services Reference, Volume 1. Levels, Threads, Exceptions, Pipes, Event Sources, Counters, and Alarms. Systems Inc.

CS2506 Quick Revision

CHAPTER 6: PROCESS SYNCHRONIZATION

Dealing with Issues for Interprocess Communication

An Automatic Temperature Control System Using RZK

MQX -celeration RTOS-integrated solutions

Concurrency: Deadlock and Starvation. Chapter 6

Transcription:

August, 2010 Hands-on Workshop: Freescale MQX and Tower System RTOS Getting Started (Part 2) ENT-F0720 Shen Li and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc.

This course covers the fundamentals of the MQX RTOS including: Task Task Prioritization Scheduling Synchronization Interrupt handling Introduction With the concepts presented in this session you will have an understanding of what MQX is and how you can use it to create tasks, use interrupts, and create synchronization between the tasks. Presenters: Doug Bruce, Embedded Access Inc Doug.Bruce@Embedded-Access.com Craig Honegger, Embedded Access Inc Craig.Honegger@Embedded-Access.com Session duration is 3 hours. and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 2

After completing this session you will be able to: Understand what an RTOS is and how they can be used Create MQX Tasks Understand MQX scheduling concepts Use various MQX Synchronization mechanisms for Mutual Exclusion Control Flow Data Flow Build a simple MQX application Session Objectives and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 3

Module Agenda 1. What is an RTOS 2. MQX Basics Tasks Priorities and Scheduling Hands-on 3. Task Synchronization Mutexes Events Semaphores Hands-on Messages Hands-on 4. Additional Resources 5. Review and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 4

Real-Time Operating Systems and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 5

Operating Systems The term operating system can be used to describe the collection of software that manages a system s hardware resources This software might include a file system module, a GUI and other components Often times, a kernel is understood to be a subset of such a collection Characteristics Resource management Interface between application and hardware Library of functions for the application User Application Operating System HARDWARE and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 6

Fusion of the application and the OS to one unit Embedded Operating Systems Characteristics Resource management Primary internal resources Less overhead Code of the OS and the application mostly reside in ROM User Operating System + Application HARDWARE and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 7

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 of the resource access Inter-task communication Time predictable Interrupt handling Real Time Operating Systems and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 8

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 and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 9

RTOS RTOS Terminology Real Time Operating System The first layer of software that runs on top of the CPU Task or Thread A portion of a program that, from its perspective, has the CPU to itself Multitasking A system consists of multiple tasks that take turns running Only one task per CPU is active (has the CPU) at any given time MQX manages how the tasks share the CPU (context switching) and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 10

Scheduler RTOS Terminology (cont d) Portion of the RTOS that decides which task to execute next, based on certain rules Scheduling Policy Determines how the task is handled (in what order they run) by the scheduler Task Context Data structure stored for each task, including registers and a list of owned resources Context Switch Switch between running tasks by saving/restoring tasks contexts. Blocking vs. Non-blocking Blocking causes running task to be removed from Read Q, Non-blocking does not. Non-blocking calls may still cause a context switch! and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 11

Synchronization RTOS Terminology (cont d) One task affects another s execution, so that tasks execute in an application-controlled order Active Task Post Wait Blocked Task and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 12

Mutual Exclusion RTOS Terminology (cont d) Allowing only one task at a time to access a shared resource Protected Resource TASK 1 Lock do work Unlock TASK 3 Lock do work Unlock M TASK 2 Lock do work Unlock TASK 4 Lock do work Unlock and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 13

We will be using Freescale MQX to demonstrate these RTOS concepts Freescale MQX Software can be downloaded: http://www.freescale.com/mqx Default Freescale MQX folder: C:\Program Files\Freescale\Freescale MQX 3.5 Freescale MQX and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 14

MQX Directory Structure Described in the MQX Release Notes Folders are: config demo doc lib mqx tools And then the RTCS, USB, and MFS stacks and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 15

MQX Directory Structure (Cont.) The mqx directory is heart of MQX Folders are: build examples source bsp io psp MQX API source and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 16

Tasks and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 17

MQX Tasks A system consists of multiple tasks Tasks take turns running Only one task is active (has the processor) at any given time MQX manages how the tasks share the processor (context switching) and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 18

Tasks Each task should have a specific action that it takes Can have multiple tasks from same template, and each is its own instance Each task has a unique 32-bit task ID used by MQX Number of tasks only limited by memory Automatic cleanup of resources when task terminates Can also specify An exit function, which MQX calls when it terminates the task. An exception handler, which MQX calls if an exception occurs while the task is active. and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 19

Typical Task Coding Structure and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 20

A task is a unique instance of a task template Overview of Task Management A task is defined by its task descriptor, which includes: Context program counter stack registers Priority Resources Task ID Task-specific parameters TD Task1 Registers PC SP State TaskCode1() {... /* Task Init */ while(1) { /* Task Body */... MyFunc();... } } and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 21

Overview of Task Environment TD Task1 TD Task2 TD Task3 TD Task4 Context Switch Registers PC SP CPU Registers PC SP State Registers PC SP State Registers PC SP State Registers PC SP State TaskCode1() {... /* Task Init */ while(1) { /* Task Body */... MyFunc();... } } OtherTaskCode() {... /* Task Init */ while(1) { /* Task Body */... ReEntrantFunc();... } } ReEntrantFunc() {... } and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 22

A task is in one of these logical states: Blocked the task is blocked and therefore not ready it s waiting for a condition to be true Active the task is ready and is running because it s the highest-priority ready task Ready the task is ready, but it s not running because it isn t the highest-priority ready task Ready Active Task States Blocking call Context Switch Object available Blocked and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 23

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() _task_block _task_create_blocked active _task_abort _task_destroy The function _task_create() puts the new task in the ready state and the scheduler runs the highest priority task blocked _task_ready terminated If _task_create_blocked is used the task is not ready until _task_ready() is called _task_create ready and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 24

MQX_template_list TASK_TEMPLATE_STRUCT defined in \mqx\source\include\mqx.h _mqx_uint TASK_TEMPLATE_INDEX void (_CODE_PTR_)(uint_32) TASK_ADDRESS _mem_size TASK_STACKSIZE _mqx_uint TASK_PRIORITY char _PTR_ TASK_NAME _mqx_uint TASK_ATTRIBUTES uint_32 CREATION_PARAMETER _mqx_uint DEFAULT_TIME_SLICE Possible Task Attributes MQX_AUTO_START_TASK MQX_TIME_SLICE_TASK And more and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 25

MQX_Template_list AKA Task Template Structure { MAIN_TASK, world_task, 0x3000, 9, "world_task", MQX_AUTO_START_TASK, 0L, 0}, { HELLO, hello_task, 0x1000, 8, "hello_task", MQX_TIME_SLICE_TASK, 0L, 100}, { LED, float_task, 0x2000, 10, "Float_task", MQX_AUTO_START_TASK MQX_FLOATING_POINT_TASK, 0L, 0}, and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 26

Creating a Task When creating a task you have to: Create task prototype and index definition #define INIT_TASK 5 extern void init_task(uint_32); Add the task in the Task Template List Using the init_task example: TASK_TEMPLATE_STRUCT MQX_template_list[] = { {INIT_TASK, init_task, 1500, 9, "init", MQX_AUTO_START_TASK, 0, 0}, } Define the task void init_task(void) { /* Put the Task Code here */ } During execution time, create the task (if it is not an autostart Task) _task_create() and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 27

Task Prioritization and Scheduling and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 28

Priorities Priorities run from 0 to N Priority 0 means interrupts disabled, 1 is most important task N is set by the highest priority number in the MQX_Template_List Idle task runs at N+1 MQX creates one ready queue for each priority up to the lowest priority (highest number) So must make sure priorities are consecutive Able to change priority of a task during runtime _task_set_priority() Any tasks at priority below 6 means it masks certain levels of interrupts. So user tasks should start at 7 or above. and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 29

Context Switching If Task A is running, it will stop running and call the scheduler if: Calls a blocking function Timeslice expires (Round Robin) A higher priority task is made ready Interrupt Occurs Then: Context of Task A is stored Context of highest priority task in the ready queue is restored Task A gets put at the end of either the ready or wait queue, depending on reason it stopped running If called a blocking function, gets put in wait queue Else put back in ready queue and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 30

Preemption Preemption occurs when a higher-priority task becomes ready, and thus becomes the active task. The previously active task is still ready, but is no longer the active task. Occurs when: An interrupt handler causes a higher-priority task to become ready, Active task makes a higher-priority task ready. and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 31

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 and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 32

T Priority Based FIFO Scheduling high priority low FIFO list of ready tasks CPU Scheduler active processor time and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc.

T Round-Robin Scheduling Task 1 Same Priority Time Slice = 50ms 75ms Task 2 50ms Task 3 60ms time Ready Task1 Task2 Task3 Task1 Task3 T0 50ms 100ms 150ms 200ms time and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc.

Idle Task Lowest priority task Simply increments a counter, which can be used to determine how long nothing is happening in the system Optional MQX_USE_IDLE_TASK Located in \mqx\source\kernel\idletask.c in the PSP project, under the kernel directory and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 35

T MQX - Task Management Example {INIT_TASK, init_task, 1500, 11, "init", MQX_AUTO_START_TASK, 0, 0}, {TASK_A, Task_A, 1500, 10, Task A", 0, 0, 0}, {TASK_B, Task_B, 1500, 9, Task B", 0, 0, 0}, void init_task(void) { _task_create(0,task_a,0);... _task_ready(task_b);... } void Task_A(void) {... _task_create_blocked(0,task_b,0);... _task_abort(task_a); } void Task_B(void) { init_task is created when MQX starts }... _task_abort(task_b); CPU Time and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc.

Write Your First RTOS Application Exercise: Run your first application and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 37

Synchronization and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 38

Why Synchronization? Synchronization may be used to solve: Mutual Exclusion Control Flow Data Flow EF Synchronization Mechanisms include: Semaphores Events Mutexes Message Queues M The correct Synchronization Mechanism depends on the synchronization issue being addressed. and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 39

Mutual Exclusion Mutual Exclusion prevents multiple tasks from simultaneously accessing the same resource Resource may be Code, Data, Peripheral, etc. Mutual Exclusion locks the Resource and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 40

Control Flow Control Flow provides a mechanism for a task or ISR to resume execution of one or more other tasks Mutual exclusion is used to prevent another task from running Control Flow is used to allow another task to run Not all synchronization objects are suitable for control flow: Good: Non-strict Semaphores, Events, Messages, Task Qs Bad: Mutexes, Strict Semaphores and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 41

Data Flow provides a mechanism for a task or ISR to resume execution of one or more other tasks with data Data Flow Mutual exclusion is not necessary Messages are the primary means of data flow synchronization and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 42

Attributes of MQX Synchronization Mechanisms Reference by name vs. by address Strict One-to-One vs. One-to-Many Convey information Priority queuing Priority inheritance Multi-processor capable Ownership tracking and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 43

MQX Mutexes and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 44

Mutex Mutexes are used for mutual exclusion, so that only one task at a time uses a shared resource such as data or a device. To access the shared resource, a task locks the mutex associated with the resource. The task owns the mutex, until it unlocks the mutex. and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 45

Mutex example There task are 12 33 then tries unlocks tasks. to tries lock Task the to the Mutex lock 2 locks Mutex the and Mutex the but the Mutex. it task but is already it 31 is locks already locked it. locked so task so 1 task waits 3 in waits the Mutex s in the Mutex s queue. queue. Task 1 (H) Task 2 (M) Task 3 (L) Mutex Task 2 Mutex Queue Task 3 Task 1 Resource and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 46

Overview of MQX Mutexes MQX Optional Component Attributes control operation Identified by address No limit to the number of mutexes M Init Lock Unlock Try Lock Set Priority Ceiling Destroy and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 47

Using mutexes To declare a MUTEX: MUTEX_STRUCT mutex; To initialize the mutex with default attributes: result = _mutex_init(mutex_ptr, NULL) To lock a mutex: _mqx_uint _mutex_lock(mutex_ptr) To unlock a mutex: _mqx_uint _mutex_unlock(mutex_ptr) and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 48

Semaphores and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 49

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. and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 50

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 How semaphores work and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 51

How Semaphores Work (n) Resources protected by semaphore A semaphore has: Counter maximum number of concurrent accesses (n) Queue for tasks that wait for access If a task waits for a semaphore If n>0, n is decremented and task runs If n is 0, task is put on the waiting queue When a task releases (posts to) a semaphore If a task is in the queue, task is readied per the queuing policy No queued tasks, n is incremented (n) TASK 1 Wait do work Post TASK 2 Wait do work Post TASK 3 Wait do work Post TASK 4 Wait do work Post and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 52

Strictness If a semaphore is strict, a task can t release (post) it unless the task first waited for and got the semaphore the counter is bounded by its initial value If a semaphore isn t strict, a task can release (post) it without first waiting for it the counter is unbounded Uses for non-strict semaphores include: if tasks always wait before posting, faster and lower-overhead non-strict semaphores are safe to use if the semaphore is associated with a dynamically allocated resource, it may need to have its counter increased past its initial count and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 53

Semaphore example Most A Depending There task kernels are desiring releases generally on allow the a semaphore only kernel, you three to the specify operations task by will performing a which timeout; that will can if a receive WAIT be the SIGNAL performed semaphore the operation. semaphore on a is If semaphore: the not If no semaphore available is either: task is within is a available certain waiting amount for a) (the INITIALIZE the semaphore, highest of time, (also priority the called value the requesting task semaphore CREATE) is greater waiting task than value for is made the 0), is semaphore, the simply ready to incremented. run orand value an error is decremented code and indicating If any the task b) that WAIT the is continues a waiting first (also timeout task called for execution. has that the PEND) occurred requested semaphore, is the returned however, semaphore to one the (First caller. of the In tasks First is Out, made or FIFO). ready SIGNAL (also called POST). to If run the and semaphore's the semaphore value value is 0, the is not task incremented; performing a the WAIT key on is given the semaphore to one of the is placed The initial value of the semaphore must be provided when the semaphore is initialized. The in tasks a waiting list. for it. waiting list of tasks is always initially empty. Task 1 (H) Task 2 (M) Driver Resource Task 3 (L) 0 Value: 1 Queue Task 3 (L) Task 1 (H) Task Driver 13 2 initializes releases requests the resource. resources; semaphore Task Because 31 gets (i.e. queue value on the priority) resource. is empty >0, drivers gets value the puts assign is resource. Task1 Task3 the in resource the waiting to list. incremented The Task2 queue and decrement strategy can the be semaphore based on priority or FIFO and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 54

We have a Bounded buffer: size N The access entry is 0 N-1, then wrap around to 0 again Producer Task writes data to buffer Counting semaphores example Must not write more than N items more than consumer ate Consumer Task reads data from buffer Should not try to consume if there is no data A semaphore is used to have the control of the available space in the buffer. 0 1 N-1 Semaphore = 43 Producer Consumer and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 55

Lightweight Semaphore Overview MQX Core Component Useful for: Mutual Exclusion Task Synchronization Count of available resources Not strict count is un-bounded Simple & Small Create Wait Post Poll Wait for Wait ticks Wait until Destroy and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 56

To declare a lightweight semaphore: LWSEM_STRUCT sem; Using lightweight semaphores 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) and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 57

MQX Events and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 58

Events Tasks can wait for a combination of event bits to become set. A task can set or clear a combination of event bits. Events can be used to synchronize a task with another task or with an ISR. The event component consists of event groups, which are groupings of event bits. 32 event bits per group (mqx_unit) Tasks can wait for all or any set of event bits in an event group (with an optional timeout) Event groups can be identified by name or by index (fast event groups) and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 59

Events Any task can wait for event bits in an event group. If the event bits are not set, the task blocks. When the event bits are set, MQX puts all waiting tasks, whose waiting condition is met, into the task s ready queue. If the event group has autoclearing event bits, MQX clears the event bits as soon as they are set. Can use events across processors (not possible with lightweight events.) and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 60

Event Bits A task waits for a pattern of event bits (a mask) in an event group with _event_wait_all() or _event_wait_any(). Wait for all the bits in the mask provided to be set or Wait for any of the bits When a bit is set, MQX makes ready the tasks that are waiting for the bit. A task can set a pattern of event bits (a mask) in an event group with Task or ISR code _event_set(). _lwevent_set _lwevent_create 32 bits TaskA _lwevent_wait_for (any) _lwevent_clear OR AND TaskB _lwevent_wait_for (all) _lwevent_clear and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 61

Lightweight event groups To declare a lightweight event group: LWEVENT_STRUCT lwevent; To create a lightweight event group: _mqx_uint _lwevent_create(lwevent_ptr, flags) flags NULL or LWEVENT_AUTO_CLEAR To set the specified event bit or bits in a lightweight event group: _mqx_uint _lwevent_set(lwevent_ptr, mask) if this is an auto-clearing event group, only one task is made ready To wait for a number of ticks (in ticks): _mqx_uint _lwevent_wait_ticks(lwevent_ptr,mask,all,ticks) If ticks = 0, the wait is unlimited To clear the specified event bit or bits in a lightweight event group: _mqx_uint _lwevent_clear(lwevent_ptr, mask) and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 62

LightWeight Events Lightweight events are simpler, low-overhead event implementation. Lightweight event groups are created from static-data structures and are not multi-processor capable. To create a lightweight event group, an application declares a variable of type LWEVENT_STRUCT, and initializes it by calling _lwevent_create() with a pointer to the variable and a flag indicating whether the event group has autoclearing event bits. and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 63

Write Your First RTOS Application Exercise: Scheduling Lab and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 64

Messages and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 65

Messages passing Tasks can communicate with each other by exchanging messages. Tasks send messages to message queues, and receive messages from message queues. Messages can be assigned a priority or marked urgent. Tasks can send broadcast messages. and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 66

Message format SIZE Header Target QID Source QID Control MESSAGE_HEADER_ STRUCT Application data Messages are areas of memory divided into a header and a data area Application data is user-defined and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 67

Message Pools Message Pools are allocated from core memory A message pool is defined by: Message size Initial size Grow amount Max size They contain several equal-sized messages Benefit is fast allocation/de-allocation and no fragmentation Each pool can contain different size messages System can automatically add new messages to a pool if it fills up (i.e. all messages are in use) Message and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 68

System Message Pools MSG Size = a MSG Size = b MSG Size = c where a <= b <= c System pools are linked together Messages are allocated on a first-fit basis Easiest type of pool to use Having different sized pools allows for more efficient use of memory, but longer allocation times Created with _msgpool_create_system() and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 69

Private Message Pools Pool_A Pool_B Pool_C Private pools are accessed individually via pool ids Allows each part of application to use a different pool Allows more control over how many messages are allocated Create with _msgpool_create() and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 70

Message Queues Task A TaskB Each task can have one (or more) messages queues associated with it Messages are always addressed to queues, not tasks Queues are identified by _queue_id This is a combination of queue number and CPU number Create a queue using _msgq_open() and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 71

Message passing example Task A Task B Owner _msgpool_create() _msg_alloc() Address the message and add data _msgq_send() _msgq_open() _msgq_receive() Blocking wait Read the message Owner Sending changes ownership _msg_free()or _msgq_send() (forward to any task, including Task A) Message must travel in a loop: Allocate it from a pool Use it Return it to pool (i.e. free it) and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 72

To create a private message pool: _pool_id _msgpool_create(message_size,num_msg,grow,limit) To get a (fixed-size) message from a private message pool: pointer _msg_alloc(pool_id) To open a private message queue (a resource of the opening task): _queue_id _msgq_open(queue_number,max_queue_size) To send a message (of lowest priority, 0): boolean _msgq_send(msg_ptr) To receive a messages: pointer _msgq_receive(queue_id, ms_timeout) To free a message: _msg_free(msg_ptr) and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 73

Message passing: Things to remember If the application forwards or returns the message to the sender, the normal rules which apply to memory allocation also apply to message passing: If you continuously allocate messages without freeing them, you will eventually run out of messages!! However, it is perfectly ok to allocate a message and use it over and over by sending it between several tasks The task which owns the message is the only one that can free it and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 74

Synchronization Comparison and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 75

Attributes of MQX Synchronization Mechanisms LW Sem LW Events LW Msg Sems Events Mutex Msg Reference by Name or ID vs. by Address address address address name name address id Strict Yes Yes One-to-One vs. One-to-Many 1:1 1:many 1:1 1:1 1:1 or 1:many 1:1 1:many Convey Information Yes Yes Yes Yes Priority Queuing Yes Yes Yes Priority Inheritance Yes Yes Multi-processor Capable Yes Yes Yes Ownership tracking Yes Yes Yes Control Flow Yes Yes Yes Yes Yes Yes Data Flow Yes Yes Yes Yes Priority Protection Mutual Exclusion Yes Yes Yes Footprint low low low high high medium high Yes and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 76

Write Your First RTOS Application Exercise: Message Passing Lab and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 77

Freescale MQX Documentation MQXUG User Guide MQXRM Reference Manual MQXUSBHOSTAPIRM USB Host API Reference Manual MQXUSBDEVAPI USB Device API Reference MQXUSBHOSTUG USB Host User Guide MQXRTCSUG RTCS User Guide MQXMFSUG File System User Guide MQXIOUG I/O Drivers User Guide MQXFS Software Solutions Fact Sheets and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 78

TWR-MCF51CN-KIT Lab Document MCF5225x Lab Document MQX Release Notes MQX User s Guide Writing First MQX Application (AN3905) Further Reading and Training Using MQX: RTCS, USB, and MFS (AN3907) How to Develop I/O Drivers for MQX (AN3902) and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 79

Videos: www.freescale.com/mqx Further Reading and Training (Cont.) MCF5225x & Freescale MQX introduction Getting started with MCF5225x and Freescale MQX Lab Demos And more vftf 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 and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 80

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 and VortiQa are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. 2010 Freescale Semiconductor, Inc. 81