32
Lab. 2 Overview Move the tasks you developed in Lab. 1 into the more controllable TTCOS operating system Manual control of RC car

Lab. 2 Overview Move the tasks you developed in Lab. 1 into the more controllable TTCOS operating system Manual control of RC car

Embed Size (px)

Citation preview

Lab. 2 Overview

Move the tasks you developed in Lab. 1 into the more controllable TTCOS operating system

Manual control of RC car

Lab 1 Task 4AUncontrolled switch copy

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

How would you handle a more complex set of embedded tests

Echo switches 1 and 2 in LED’s 1 and 2Flash LED 3 every 2 secondsFlash LED 5 every ½ secondFlash LED 6 every ¼ secondDo something else for 4 seconds, then

something else for 12 and then something else for 5 – Assignment 1

Etc

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 3 / 28

If we used the “super-loop idea”

int main( ) {

InitLED( );

while (1) {

if CorrectTIme(time1) then TaskFlashLED3( );

I if CorrectTIme(time 2) then TaskFlashLED3( ); value = ReadSwitches( ); WriteLED(value >> 8);

}

Actual code on next slide

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 4 / 28

QUESTONS TO ASNWER

1.We need the processor to switch LED every 2 seconds

2.How do you handle the timing?

3.How can you get the processor to go to sleep to save battery power at other times?

Super loop – Lab 1 Task 4B

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 5 / 28

From Pont’s bookControl of an aeroplane

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 6 / 28

Need a systematic way of programming such a system

These ideas and the following slides are based on the book by M. J. Pont – “Patterns for Time-Triggered Embedded Systems”Down load a free copy from

www.tte-systems.com/downloads/pttes_0408a.pdf

The code in the book was for the 8051 processor and I have modified the code to run on Blackfin Processor

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 7 / 28

Scheduler The idea is to make a list of all the tasks that you

need to make the embedded system to work Then you use a “scheduler” to cause the tasks to

run

Pre-emptive scheduler -- discussed FridayTasks run as random events

Co-operative Scheduler -- discussed FridayTasks run at predictable intervals

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 8 / 28

Lab. 2 Task 2 – Download TTCOS and validate the install

Check the web pages for install detailsTTCOS511.h file

all the prototypes for the TTCOS functionsTTCOS511.dlb

the TTCOS library for the BlackfinExample_TTCOSmain.c

Use this as a template for all TTCOS programs in Labs, assignment and quizzes and exams)

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 9 / 28

Example_TTCOS main.cpp

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 10 / 28

The only lines of codethat ever change

FOR OTHER LAB 2 partsreplace these lines

The total code for Lab. 2 Task 2

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 11 / 28

First task – name -- Task_SayGreetingRuns with no delay

Runs only once

Second task – name -- Task_SayHiOccasionallyRuns with no delay

Runs every 5 seconds, and then allows processor to sleep

Lab. 2 – Task 3: Reuse Lab. 1 Functions within TTCOS

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 12 / 28

Step 1 in processRecognize the tasks that are needed

3 RUN_ONCE2 PERIODIC

Step 1 – Add TTCOS task to initialize system

One TTCOS task activates many others

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 13 / 28

So this is the code we need to replace – Lab 1 Task 4B

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 14 / 28

Lets add the flashing LED tasks

For predictability – each task runs in its own time slot 1 / 48000 apart

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 15 / 28

Uncoupling actions -- Use two task to do copy of switches to LEDs

Later will have a task that copies switch values to RC car

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 16 / 28

Final task is more difficult

In Lab 1 Task 4B - Controlled echo (copy) of switches to LED we simply had DO CheckSwitch4PressedAndReleased( ) stop the loop.

 

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 17 / 28

We can't stop the operating system -- so we much use a KILL  task based on the old CheckSwitch4 task

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 18 / 28

New switch 4 task – calls a kill task

Possible error messages from ‘task race’

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 19 / 28

Halloween – so we bring the killed tasks back to life

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 20 / 28

Lab 2 Task 4 -- Make PF lines INPUT and OUPUT at same time

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 21 / 28

ChristineTask -- Step 1

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 22 / 28

Christine Task – step 2

Control the wheels to turn the car left and right

Use AND and OR instructions, otherwise you will destroy the input switch values

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 23 / 28

ChristineTask( ) – Step 3

Run these task every 1/ 4 second – no more often – the car will not respond any faster

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 24 / 28

Major milestone (B- on Lab)

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 25 / 28

Task 5 -- totally automated

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 26 / 28

What does code look like on pre-emptive scheduler?

int main( ) {Setup LED, SwitchesSet Up interrupts( ) and start them

while (1 ) { // Lab. 1 Task 7 Read Switches and Write to LED;

}

}

Timer1_ISR( ) { // Only happens when Timer1 alarm rings

Flash LED 3( );}

Timer2_ISR( ) { // Only happens when Timer1 alarm rings

Flash LED 4( );}

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 27 / 28

Problem is “what happens ifboth alarms ring at same time

(with larger ISR code)

InterruptServiceRoutine

“Subroutine activated by hardware”not by “code” -- unpredictable

ENCM Real time course Pre-emptive scheduler

Lab. 2 problemsTask to Flash LED 3 every 2 secondsTask to Flash LED 4 every one second

Both tasks want to use same resourceWhat happens if one task starts to save

something and gets interrupted?

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 28 / 28

We think we can solve this problem using “locks” and “semaphores”

#define UNLOCKED 0

#define LOCKED 1

Bit lock = UNLOCKED ; // Global Lock

void LED3_Task( )

while (lock == LOCKED) /* Wait for lock to be released */ ;

lock = LOCKED; // Indicate using LEDs

WriteLEDASM(LED3_ON);

lock = UNLOCKED;

}

void LED4_Task( )

while (lock == LOCKED) /* Wait for lock to be released */ ;

lock = LOCKED; // Indicate using LEDs

WriteLEDASM(LED4_ON);

lock = UNLOCKED;

}04/20/23

TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 29 / 28

Solve this problem using “lock”#define UNLOCKED 0

#define LOCKED 1

Bit lock = UNLOCKED ; // Global Lock

void LED3_Task( )

while (lock == LOCKED) /* Wait for lock to be released */ ;

lock = LOCKED; // Indicate using LEDs

WriteLEDASM(LED3_ON);

lock = UNLOCKED;

}

void LED4_Task( )

while (lock == LOCKED) /* Wait for lock to be released */ ;

lock = LOCKED; // Indicate using LEDs

WriteLEDASM(LED4_ON);

lock = UNLOCKED;

}04/20/23

TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 30 / 28

Suppose LED3_Task

gets switched out hereand LED4_Task runs

LED4_Task thinks the resource

is free to use

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 31 / 28

Need to know in future lectures

Need to understand “interrupts” on the Blackfin

Need to understand how the “core timer” and “watch dog timer” work and are programmed.

Useful to understand how the scheduler works – but not necessary unless we plan to change it

04/20/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 32 / 28