Salvo Rtos Lch

32
SALVO 02/11/2010 LOTFI CHARAABI THE SALVO RTOS

Transcript of Salvo Rtos Lch

Page 1: Salvo Rtos Lch

SALVO

02/11/2010

LOTFI CHARAABI

THE SALVO

RTOS

Page 2: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 2 -

Pre-emptive vs cooperative sheduling

Pre-emptive sheduling for classic RTOS

Stak for each task

Contex switching is time-consuming

adapted to tight deadlines

Cooperative sheduling: each task must relinquish of its own accord

No central overhead

This may blocking out the OS

Page 3: Salvo Rtos Lch

SALVO

LOTFI CHARAABI

overview

Task States

- 3 -

Suplied by Pumpkin Real Time Software Inc.

cooperative RTOS for small embedded system

Page 4: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 4 -

Salvo build Process

Page 5: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 5 -

Configuration (build with library)

Building with library build (salvo.h, mem.c, salvocfgh.h)

Library sets for each compiler

Page 6: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 6 -

Writing Salvo tasks

Main()

Page 7: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 7 -

Writing Salvo tasks

Task is written as C function

All Tasks are initially “destroyed”. Tasks must be created using the Salvo

OScreateTask() function in the main function

Tasks are generally made up optional initialization, followed by an infinite loop

which must contain at least one context switch

The context switch can be provided by a call to the function OS_Yield()

Tasks cannot take any parameters

Tasks use static variables

The operating characteristics of a task are contained within its (TCB). This is a

block of memory, which contains the task‟s start address, state and priority.

Page 8: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 8 -

Initialisation and sheduling(1/2)

Function /service Action and parameters

OSInit() Initialises the operating system, including data structures, pointers

and counters. Must be called before any other Salvo function.

No parameters.

OSSched( ) The Salvo scheduler. On every call it chooses – from those which

are eligible – the next task to run. Multi-tasking can occur when this

is called repeatedly. No parameters.

OS_Yield( ) Unconditional return to scheduler(1) Context switch label, often defined through use of _OSLabel( ).

Core Salvo services

Page 9: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 9 -

Initialisation and sheduling(2/2)

Function /service Action and parameters

OSCreateTask( , , ) Creates a task and starts it (i.e. makes it eligible).

(1) Pointer to task starting address – usually the task‟s name.

(2) Pointer to task TCB (task control block).

(3) Priority – a number from 0 (highest) to 15 (lowest).

OSStartTask( ) Makes a stopped task eligible.

(1) Pointer to the task TCB (task control block).

_OSLabel( ) Defines a unique label required for each context switch.(1) Label name.

OSTCBP( ) Defines a pointer to specified control block, in this case to the task control block.(1) An integer from 1 to OSTASKS, where OSTASKS appears in the salvocfg.h file and specifies the number of tasks.

Page 10: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 10 -

First Salvo example (ex1)

Tow tasks of equal priority. One count_Task() increments a counter. The

other, Display_Task, displays two bits of the counter value on two bits of

Port C.

tasks are of equal priority, they are being executed in round robin

scheduling. Running the program causes execution to alternate between

tasks, visiting OSSched( ) in between each task call.

Every time Count_Task runs, the value of counter in the Watch window

increments, and every time Display_Task runs, the value of Port C is

updated.

Program is running at effectively uncontrolled speed.

Page 11: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 11 -

First Salvo example (ex1)

//function prototypes. These functions are tasks.

void Count_Task( void );

void Display_Task( void );

//Define labels for context switches

_OSLabel(Count_Task1)

_OSLabel(Display_Task1)

//Define and initialise variable

unsigned char counter = 0;

Page 12: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 12 -

First Salvo example (ex1)

//Task Definitions (configured as functions)

void Count_Task( void ){for (;;) //infinite loop{counter++;OS_Yield(Count_Task1); //context switch}}void Display_Task( void ){for (;;){PORTC = counter<<5; //Shift Counter left, and move to PORT COS_Yield(Display_Task1);}}

Page 13: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 13 -

First Salvo example (ex1)

// Main

void main( void )

{

//Initialise

TRISC = 0b10000000; //Set all Port C bits to output, except bit 7.

PORTC = 0; //Set all Port C outputs low

//Initialise the RTOS

OSInit();

//Create Tasks

OSCreateTask(Count_Task, OSTCBP(1), 10);

OSCreateTask(Display_Task, OSTCBP(2), 10);

//Set up continuous loop, within which scheduling will take place.

for (;;)

OSSched();

}

Page 14: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 14 -

Using interrupts, delays and semaphores (ex2)

Function /service Action and parameters

OSTimer() Checks to see if any delayed or waiting tasks have timed out. If yes,

they are rendered eligible. Must be called at the desired tick rate

if delay, time-out or elapsed time services are required. Often

placed within timer interrupt ISR. No parameters.

OS_Delay( , ) Causes current task to return to scheduler and delay by amount

specified. Requires OSTimer( ) to be in use.

(1) Integer giving desired delay in system ticks, 8-bit value only.

(2) Context switch label, often defined through use of _OSLabel( ).

OSEi( ) Enables interrupts (sets GEI and PEIE in INTCON)

OSDi( ) Disables interrupts.

Salvo functions and services used with interrupts, timers and delays

Page 15: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 15 -

Configuration (ex2)

A new salvocfg.h file is needed for this project:

# define OSEVENTS 1 /* multitasking only */

#define OSTASKS 2

#define OSBYTES_OF_DELAYS 1

#define OSENABLE_BINARY_SEMAPHORES TRUE

#define OSENABLE_DELAYS TRUE

In the ex2 example, the MAKE_WITH_SOURCE method compilation was used.

The application includes the following files: ex2.c, isr.c, binsem.c, delay.c,

event.c, init.c, inittask.c, mem.c, portpic.c, qins.c, sched.c, timer.c

Page 16: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 16 -

Using interrupts, delays and semaphores (ex2)

Same Tow tasks of equal priority. but introduces an interrupt-driven

clock tick, a delay and a binary semaphore.

The main function initialises the microcontroller through a call to the

function Micro_Init( ). This is followed by the RTOS initialisation

OSInit( ). After creating tasks and semaphore, the program enters the

expected scheduling loop.

The program contains an ISR for Timer 0 interrupt overflow to flash the

LEDs. The ISR includes OSTimer( ) function. It allows the time-based

features of Salvo to work. A variable tick_counter is also incremented on

every ISR iteration.

Page 17: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 17 -

The function OSTimer( ) allows the time-based features of Salvo to work.

A clock tick period of 10 ms was chosen for this example program. This is enabled

and configured through the OpenTimer0( ) call.

The Global Interrupt Enable is set within the call to OSEI( ) in the main()

The RTOS does routinely disable interrupts, for example during OSSched( ).

This worsens the interrupt latency and will cause delay in the response to the

timer ISR

Interrupts can be introduced to a Salvo-based program by adding

USE_INTERRUPTS in the Macro Definitions window. This identifies to Salvo

that a file called isr.c acts as the system ISR.

Using interrupts and establishing the clock tick (ex2)

Page 18: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 18 -

OS_Delay (20,Count_Task1); //Task switch, delay for 20x10ms, (200ms)

The OS_Delay( ) function forces a context switch when called and can

thus replace OS_Yield( ). It also introduces a delay before the next

time the task can run, determined by the setting of the first parameter.

Using delays (ex2)

use of this delay causes the Count_Task function to occur periodically,

every 200 ms.

Page 19: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 19 -

Using a binary semaphore (ex2)

Function /service Action and parameters

OSCreateBinSem( , ) Creates binary semaphore.(1) Pointer to ECB (event control block).(2) Initial value (0 or 1).

OSSignalBinSem( ) Signals a binary semaphore. If no task is waiting it increments. Ifone or more tasks are waiting, then the one with highest priorityis made eligible.(1) Pointer to semaphore ECB.

OS_WaitBinSem( , , ) Task waits until binary semaphore is signalled. Wait state isexited when semaphore signals and task is highest priority, or iftime-out expires. Semaphore is then automatically cleared. Time-out must be specified.(1) Pointer to ECB.(2) Time-out value (in system ticks).(3) Context switch label.

OSECB( ) Defines pointer to the event control block.

Page 20: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 20 -

#define BINSEM_Display OSECBP(1)

The semaphores, like tasks, must first be created in the program. This

allocates them an event control block (ECB) in memory. Semaphores can then be

written to by one task and waited on by another.

The single semaphore is allocated to the ECB pointer OSECB(1)

Using a binary semaphore (ex2)

The semaphore is created in the main( ) function

OSCreateBinSem(BINSEM_Display, 0);

Count_Task, when it runs, signals to the semaphore through its call toOSSignalBinSem( ). This action sets its value high. If a task is waiting for it, thenthat task becomes ready to run and the semaphore is cleared. Display_Task iswaiting for the semaphore to go high

OS_WaitBinSem(BINSEM_Display, 100, Display_Task1);

Page 21: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 21 -

Simulating the program (ex2)

the program will reach the breakpoint within Count_Task. This is the

task created first, and the delay does not take effect until the first

iteration of OSDelay( ). The value of counter is incremented to 1. The

semaphore is then set and a further run will reach the breakpoint in

Display_Task. The next run will find the breakpoint in the timer ISR.

Page 22: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 22 -

Using messages (ex3)

Function /service Action and parameters

OSCreateMsg( , ) Creates message, with ECB pointer and initial value.(1) Pointer to message ECB.(2) Pointer to message.

OSSignalMsg( , ) Signals a message, i.e. attaches a data element to it. If one ormore tasks are waiting, then the one with highest priority is madeeligible.(1) Pointer to message ECB.(2) Pointer to message.

OS_WaitMsg( , , , ) Task waits until message is signalled. Then makes message pointerpoint to it, waiting task then continues with execution. Task alsocontinues if timed out.(1) Pointer to message ECB.(2) Pointer for message.(3) Time-out value (in system ticks).(4) Label.

OStypeMsgP Define data type as message pointer

Page 23: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 23 -

Random number generator (ex3)

A random number between 0 and 255 is generated. Eight LEDs are

connected to PORTB of a PIC18F452 microcontroller. In addition, a

push-button switch is connected to bit 0 of PORTD (RD0), and a LED is

connected to bit 7 of PORTD (RD7).

Page 24: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 24 -

Random number generator (ex3)

Three tasks are used in this project: Live, Generator, and Display.

Task Live runs every 200ms and flashes the LED on port pin RD7 toindicate that the system is working.

Task Generator increments a variable from 0 to 255 continuously andchecks the status of the push-button switch. When the push-buttonswitch is pressed, the value of the current count is sent to task Displayusing a messaging queue.

Task Display reads the number from the message queue and sends thereceived byte to the LEDs connected to PORTB. Thus, the LEDs display arandom pattern every time the push button is pressed.

Page 25: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 25 -

The task Display opens by declaring the variable msge, where the incoming

message will be stored. It then defines the message pointer msgP using a special

Salvo data type OSTypeMsgP

Only one Salvo message, Msg_to_display, is created, but it can be used to

carry different messages from different places in the program.

Using messages (ex3)

The message is created in main using OSCreateMsg( ). Very early in the first

call to display_Task( ), program execution reaches an OS_WaitMsg( ) call. This

causes a context switch and forces the task to wait until the message identified,

Msg_to_display, is signalled. Time-out is explicitly not applied, through use of the

OSNO_TIMEOUT. The pointer for the incoming message signal is specified as

msgP, which is declared at the beginning of the function.

Page 26: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 26 -

Home Project

The prototype's hardware includes a 20 MHz crystal, a potentiometer

push-btton, a serial port, EEPROM, live LED and a piezo beeper.

The time-base is a 2 ms periodic interrupt derived from Timer0

EEPROM

beeper

Page 27: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 27 -

Home Project

The potentiometer voltage will be monitored to check if the voltage isbetween the low and high threshold voltage range (set by default between1 and 5 volts)

A zone voltage that is not within these parameters will set on the Piezoalarm, simultaneously storing the voltage alarm value into the eeprom.

The USART is used for displaying the current voltage on a PC monitor;this is executed by pressing the push-button switch. The USART isconfigured for Master Asynchronous mode with a 9600 baud rate.

I2C communication between the microcontroller and EEPROM.

The potentiometer is connected to the PIC18f452 ADC

Page 28: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 28 -

Task configuraton

Task_Convert()

Priority: 1

Task has a priority of „1‟ because we must determine potentiometer voltage

to decide whether an alarm condition exists.

Status: Runs every 40 milliseconds.

Responsibilities:

1. Converts the analog voltage into a digital value

2. This value is compared against the low and high threshold voltages to

determine if an alarm is necessary.

Page 29: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 29 -

Task configuraton

Task_Alarm_On()

Priority: 1

This task also has a priority of „1‟, but runs after Task_Convert() in a

round-robin fashion. After determining temperature, checking for alarms

is most important.

Status: Waits for an event.

Responsibilities:

1. Has the same priority as, and runs immediately after, Task_Convert()

at start-up.

2. Turns the piezo beeper on and off.

Page 30: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 30 -

Task configuraton

Task_Usart()

Priority: 4

Remote PC monitoring is only performed occasionally

because usage is low.

Status: Waits for an event.

Responsibilities:

1. Scans for a push button press.

2. Writes the “voltage” string out to the HyperTerminal via the USART.

Page 31: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 31 -

Task configuraton

Task_Live()

Priority: 2

Enables flashing led

Status: Runs every 800 milliseconds.

Responsibilities:

1. Flashing a led

Page 32: Salvo Rtos Lch

SALVO

LOTFI CHARAABI - 32 -

Task configuraton

Task_Weeprom()

Priority: 5

This task is only active when an alarm has occurred; therefore, it is used

very little.

Status: Waits for an event.

Responsibilities:

1. Receives the alarm.

2. Writes voltage alarm to EEPROM

3. I2C communication between the microcontroller and EEPROM