operating systems and concurrency b04 - hesabu.nethesabu.net/en0572/assets/ra/b04.pdf · operating...

26
Operating systems and concurrency B04 David Kendall Northumbria University David Kendall (Northumbria University) Operating systems and concurrency B04 1 / 21

Upload: dangdang

Post on 26-Mar-2018

220 views

Category:

Documents


2 download

TRANSCRIPT

Operating systems and concurrency B04

David Kendall

Northumbria University

David Kendall (Northumbria University) Operating systems and concurrency B04 1 / 21

Introduction

Towards an operating system: CMSIS and MbedMulti-tasking operating system servicesµC/OS-II (uC/OS-II)Task management in uC/OS-II

David Kendall (Northumbria University) Operating systems and concurrency B04 2 / 21

Standard names for the microcontroller registers

#include <stdboo l . h>#include < s t d i n t . h>#include <LPC407x_8x_177x_8x . h>

#define LED1PIN (1UL << 18)

void delay ( u i n t32_ t ms ) ;

i n t main ( ) {LPC_IOCON−>P1_18 &= ~0x1FUL ;LPC_GPIO1−>DIR |= LED1PIN ;while ( t r ue ) {

LPC_GPIO1−>SET = LED1PIN ;delay (1000) ;LPC_GPIO1−>CLR = LED1PIN ;delay (1000) ;

}}

Header fileLPC407x_8x_177x_8x.hprovided by themanufacturer of themicrocontroller (NXP) togive a set of standardnames for themicrocontroller registersNo need to write#defines after looking upthe addresses yourself.Header file written instandard ANSI C -CMSIS-complaint -portable between toolproviders.

David Kendall (Northumbria University) Operating systems and concurrency B04 3 / 21

CMSIS

Cortex MicrocontrollerSoftware Interface StandardCMSIS-Core

Standard set of namesprovided by ARM foraccessing themicroprocessorStandard startup files forbringing up the CPU out ofreset:

configure system clocksdefine the vector tablerun the main function

Improves software portabilityand reusability across differentmicrocontrollers andtoolchains

Martin, T. The Designer’s Guide to the Cortex-M Processor

Family: A Tutorial Approach, Newnes, 2013

David Kendall (Northumbria University) Operating systems and concurrency B04 4 / 21

Foreground/background tasks

Simple multitaskingSuper loop callsfunctions forcomputation(background)Interrupt serviceroutines (ISRs) handleasynchronous events(foreground)

ProblemModel with only one computation task is not flexible enough

David Kendall (Northumbria University) Operating systems and concurrency B04 5 / 21

Foreground/background tasks

Simple multitaskingSuper loop callsfunctions forcomputation(background)Interrupt serviceroutines (ISRs) handleasynchronous events(foreground)

ProblemModel with only one computation task is not flexible enough

David Kendall (Northumbria University) Operating systems and concurrency B04 5 / 21

Multitasking

Easier to structure the application as a set of concurrent tasksrather than as a single program or as foreground/backgroundtasks: each task is responsible for some well-defined part of thesystem’s overall behaviourBut only the illusion of concurrency - the OS switches quicklybetween tasks, executing some instructions from one task beforemoving on to another taskswitching from one text to another requires a context switchdeciding which task to switch to requires a scheduling algorithm

David Kendall (Northumbria University) Operating systems and concurrency B04 6 / 21

Scheduling

Deciding when one task should stop executing and which oneshould begin next is a scheduling problemTwo main approaches to scheduling:

Preemptive schedulingTask is forced to yield the CPURound robinPriority-based

Non-preemptive (cooperative) schedulingTask voluntarily yields the CPU and signals the next task to begin

David Kendall (Northumbria University) Operating systems and concurrency B04 7 / 21

Fixed-priority preemptive scheduling

We focus on fixed-priority premptive scheduling

David Kendall (Northumbria University) Operating systems and concurrency B04 8 / 21

A task and its execution states

David Kendall (Northumbria University) Operating systems and concurrency B04 9 / 21

uC/OS-II: A small operating system

Main features:Multi-taskingPreemptive

Other features:PredictableRobust and reliableStandards-compliantPortableScalableSource code available

David Kendall (Northumbria University) Operating systems and concurrency B04 10 / 21

uC/OS-II Services

Task managementDelay management

SemaphoresMutual exclusion semaphoresEvent flagsMessage mailboxesMessage queuesMemory managementTimersMiscellaneous

See uC/OS-II Quick Reference

David Kendall (Northumbria University) Operating systems and concurrency B04 11 / 21

uC/OS-II Services

Task managementDelay managementSemaphoresMutual exclusion semaphoresEvent flagsMessage mailboxesMessage queuesMemory managementTimersMiscellaneous

See uC/OS-II Quick Reference

David Kendall (Northumbria University) Operating systems and concurrency B04 11 / 21

uC/OS-II Services

Task managementDelay managementSemaphoresMutual exclusion semaphoresEvent flagsMessage mailboxesMessage queuesMemory managementTimersMiscellaneous

See uC/OS-II Quick Reference

David Kendall (Northumbria University) Operating systems and concurrency B04 11 / 21

Tasks behaviour

The behaviour of a task is defined by a C function that:1 never terminates2 blocks repeatedly

Example of task behaviour definition

s t a t i c void appTaskLED2 ( void ∗pdata ) {while ( t r ue ) {

OSTimeDlyHMSM(0 ,0 ,0 ,500 ) ;gpioPinToggle (& p in [ LED2 ] ) ;

}}

David Kendall (Northumbria University) Operating systems and concurrency B04 12 / 21

Tasks: other requirements

PriorityUsed for fixed-priority pre-emptive schedulinga number between 0 and OS_LOWEST_PRIO

low number ⇒ high priorityhigh number ⇒ low priorityOS reserves priorities 0 to 3 and OS_LOWEST_PRIO - 3 toOS_LOWEST_PRIO

Advice: define an enumeration of task priority constants, startingat priority level 4.Example

enum {APP_TASK_BUTTONS_PRIO = 4 ,APP_TASK_LED1_PRIO,APP_TASK_LED2_PRIO

} ;

David Kendall (Northumbria University) Operating systems and concurrency B04 13 / 21

Tasks: other requirements

StackEach task needs its own data area (stack) for storing

contextlocal variables

Example stack definition

enum {APP_TASK_LED2_STK_SIZE = 256} ;s t a t i c OS_STK appTaskLED2Stk [ APP_TASK_LED2_STK_SIZE ] ;

User dataOptionally tasks can be given access to user data when they arecreatedWe will not use this feature in this moduleAdvice: always specify this as (void *)0 when creating a task

David Kendall (Northumbria University) Operating systems and concurrency B04 14 / 21

Tasks: other requirements

StackEach task needs its own data area (stack) for storing

contextlocal variables

Example stack definition

enum {APP_TASK_LED2_STK_SIZE = 256} ;s t a t i c OS_STK appTaskLED2Stk [ APP_TASK_LED2_STK_SIZE ] ;

User dataOptionally tasks can be given access to user data when they arecreatedWe will not use this feature in this moduleAdvice: always specify this as (void *)0 when creating a task

David Kendall (Northumbria University) Operating systems and concurrency B04 14 / 21

Task creation

A task is created using the OS functionINT8U OSTaskCreate (

void (∗ task ) ( void ∗pdata ) , /∗ f u n c t i o n f o r the task ∗ /void ∗pdata , /∗ user data f o r f u n c t i o n ∗ /OS_STK ∗ptos , /∗ p o i n t e r to top o f s tack ∗ /INT8U p r i o r i t y /∗ task p r i o r i t y ∗ /

) ;

Example

enum {APP_TASK_LED2_PRIO = 4 } ;enum {APP_TASK_LED2_STK_SIZE = 256} ;

s t a t i c OS_STK appTaskLED2Stk [ APP_TASK_LED2_STK_SIZE ] ;

OSTaskCreate ( appTaskLED2 ,( void ∗ )0 ,(OS_STK ∗)&appTaskLED2Stk [ APP_TASK_LED2_STK_SIZE − 1 ] ,APP_TASK_LED2_PRIO ) ;

David Kendall (Northumbria University) Operating systems and concurrency B04 15 / 21

Task creation

A task is created using the OS functionINT8U OSTaskCreate (

void (∗ task ) ( void ∗pdata ) , /∗ f u n c t i o n f o r the task ∗ /void ∗pdata , /∗ user data f o r f u n c t i o n ∗ /OS_STK ∗ptos , /∗ p o i n t e r to top o f s tack ∗ /INT8U p r i o r i t y /∗ task p r i o r i t y ∗ /

) ;

Example

enum {APP_TASK_LED2_PRIO = 4 } ;enum {APP_TASK_LED2_STK_SIZE = 256} ;

s t a t i c OS_STK appTaskLED2Stk [ APP_TASK_LED2_STK_SIZE ] ;

OSTaskCreate ( appTaskLED2 ,( void ∗ )0 ,(OS_STK ∗)&appTaskLED2Stk [ APP_TASK_LED2_STK_SIZE − 1 ] ,APP_TASK_LED2_PRIO ) ;

David Kendall (Northumbria University) Operating systems and concurrency B04 15 / 21

Task delay

Often, a task will block itself by explicitly asking the OS to delay itfor some period of timevoid OSTimeDly(INT16U ticks);

Causes a context switch if ticks is between 1 and 65535If ticks is 0, OSTimeDly() returns immediately to callerOn context switch uC/OS-II executes the next highest priority taskTask that called OSTimeDly() will be made ready to run whenthe specified number of ticks elapses - actually runs when itbecomes the highest priority ready taskResolution of the delay is between 0 and 1 tickAnother task can cancel the delay by callingOSTimeDlyResume()

David Kendall (Northumbria University) Operating systems and concurrency B04 16 / 21

Task delay

OSTimeDly() specifies delay in terms of a number of ticksUse OSTimeDlyHMSM() to specify delay in terms of Hours,Minutes, Seconds and MillisecondsOtherwise OSTimeDlyHMSM() behaves as OSTimeDly()

David Kendall (Northumbria University) Operating systems and concurrency B04 17 / 21

Complete example

#include <stdboo l . h>#include < ucos_ i i . h>#include " gpioPin . h "

typedef enum {APP_TASK_LED1_PRIO = 4 ,APP_TASK_LED2_PRIO

} t a s k P r i o r i t i e s _ t ;

typedef enum {APP_TASK_LED1_STK_SIZE = 256 ,APP_TASK_LED2_STK_SIZE = 256

} s tackS izes_ t ;

s t a t i c OS_STK appTaskLED1Stk [ APP_TASK_LED1_STK_SIZE ] ;s t a t i c OS_STK appTaskLED2Stk [ APP_TASK_LED2_STK_SIZE ] ;

s t a t i c void appTaskLED1 ( void ∗pdata ) ;s t a t i c void appTaskLED2 ( void ∗pdata ) ;

typedef enum {LED1 = 0 ,LED2} deviceNames_t ;

gp ioP in_ t p in [ 2 ] ;

David Kendall (Northumbria University) Operating systems and concurrency B04 18 / 21

Complete example

i n t main ( ) {

/∗ I n i t i a l i s e the GPIO pins ∗ /g p i o P i n I n i t (& p in [ LED1 ] , 1 , 18 , OUTPUT_PIN ) ;g p i o P i n I n i t (& p in [ LED2 ] , 0 , 13 , OUTPUT_PIN ) ;

/∗ I n i t i a l i s e the OS ∗ /OSIn i t ( ) ;

/∗ Create the tasks ∗ /OSTaskCreate ( appTaskLED1 ,

( void ∗)0 ,(OS_STK ∗)&appTaskLED1Stk [ APP_TASK_LED1_STK_SIZE − 1 ] ,APP_TASK_LED1_PRIO ) ;

OSTaskCreate ( appTaskLED2 ,( void ∗)0 ,(OS_STK ∗)&appTaskLED2Stk [ APP_TASK_LED2_STK_SIZE − 1 ] ,APP_TASK_LED2_PRIO ) ;

/∗ S t a r t the OS ∗ /OSStart ( ) ;

/∗ Should never a r r i v e here ∗ /return 0;

}

David Kendall (Northumbria University) Operating systems and concurrency B04 19 / 21

Complete example

s t a t i c void appTaskLED1 ( void ∗pdata ) {/∗ S t a r t the OS t i c k e r −− must be done i n the h ighes t p r i o r i t y task ∗ /SysTick_Config ( SystemCoreClock / OS_TICKS_PER_SEC ) ;

/∗ Task main loop ∗ /while ( t r ue ) {

gpioPinToggle (& p in [ LED1 ] ) ;OSTimeDlyHMSM(0 ,0 ,0 ,500 ) ;

}}

s t a t i c void appTaskLED2 ( void ∗pdata ) {while ( t r ue ) {

gpioPinToggle (& p in [ LED2 ] ) ;OSTimeDlyHMSM(0 ,0 ,0 ,500 ) ;

}}

David Kendall (Northumbria University) Operating systems and concurrency B04 20 / 21

Acknowledgements

Qing Li and Caroline Yao, Real-time concepts for embeddedsystems, CMP, 2003Jean Labrosse, MicroC/OS-II: The Real-Time Kernel, CMP, 2002

David Kendall (Northumbria University) Operating systems and concurrency B04 21 / 21