lab. 2 overview. echo switches to led lab1 task 7 12/4/2015 tdd-core timer library, copyright m....

24
Lab. 2 Overview

Upload: audrey-lang

Post on 14-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

Lab. 2 Overview

Page 2: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

Echo Switches to LED Lab1 Task 7

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

Page 3: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-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 4 every 1 secondFlash LED 5 every ½ secondFlash LED 6 every ¼ secondEtcEtcetc

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

Page 4: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

If we used the “super-loop idea”

int main( ) {

InitLED( );

while (1) {

TaskFlashLED3( );

TaskFlashLED2( ); value = ReadSwitches( ); WriteLED(value >> 8);

}

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

Questions

We need the processor to switch

LED every 2 seconds

How do you handle the timing?

How can you get the processor to go to

sleep to save battery power at other

times?

Page 5: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

Auto control of an aeroplane

Lists the tasks

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

Page 6: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

From Pont’s book

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

Page 7: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 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/21/23TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 7 / 28

Page 8: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 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 schedulerCo-operative Scheduler

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

Page 9: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

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

Page 10: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

ENCM Real time course

Previous classes have used Analog Devices VDK Kernel which has a pre-emptive scheduler.

Lab. 2 problems Task to Flash LED 3 every 2 seconds Task to Flash LED 4 every one second

Both tasks want to use same resource Memory that controls LED Represents something ‘important’ Can’t have 2 things writing to same device Operating systems class

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

Page 11: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 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/21/23

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

Page 12: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

Pre-emptive scheduler

What if task gets switched out at ‘critical time’?

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

Page 13: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 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/21/23

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

Suppose LED3_Task

gets switched out hereand LED4_Task runs

LED4_Task thinks the resource

is free to use

Page 14: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

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

Page 15: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

Scheduler code looks like thisint main( ) {

Init_Scheduler( );

Add_Task(LED3_Task, 0, 2000);

Add_Task(LED4_Task, 0, 1000);

StartScheduler( );

while (1) { time = ReadCycleCounter( ); Dispatch_Tasks(time); }

}

Dispatch_Tasks(time) {

if time to run LED3_Task then run LED3_Task( );

if time to run LED4_Task then run LED4_Task( );}

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

Page 16: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

Power saving codeint main( ) {

Init_Scheduler( );

Add_Task(LED3_Task, 0, 2000);

Add_Task(LED4_Task, 0, 1000);

StartScheduler( ); -- this activates a “timer” which causes an interrupt

while (1) { GoToSleepUntil_InterruptOccurs( );

time = ReadCycleCounter( ); Dispatch_Tasks(time); }

}

Dispatch_Tasks(time) {

if time to run LED3_Task then run LED3_Task( );

if time to run LED4_Task then run LED4_Task( );}

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

Page 17: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

Lab. 2

I have found a “co-operative scheduler” that works for the 8051 using the 8051 timer interrupts. Lets suppose it could be made to work for Blackfin

The scheduler is easy to use and the tasks are easy to write (using your existing Lab. 1 code and assignment code)

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

Page 18: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

Task are easy to develop

void LED3_Task( )

Uses Read LED( ) and WriteLED( ) from Lab. 1

if LED3 is on turn it off

if LED3 is off turn it on

}

void LED4_Task( )

Uses Read LED( ) and WriteLED( ) from Lab. 1

if LED4 is on turn it off

if LED4 is off turn it on

}

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

Page 19: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

Scheduler functions

TTCOS_AddTask(TaskName, Delay, Period)

Thus TTCOS_AddTask (Led3, 0, 1000);

Means “run” LED3 task with “no delay” the first time it is run, and then run it again after “1000 clock-ticks”. This will cause LED3 to turn on for 1000 ticks and off for 1000 ticks

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

Page 20: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

Scheduler functions

TTCOS_AddTask(TaskName, Delay, Period)

Thus TTCOS_AddTask Led3, 0, 1000); TTCOS_AddTask (Led4, 0, 2000);

Now Led3 task runs for a short time every 1000 ticks, and Led4 task runs for a short time every 2000 ticks

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

Page 21: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

Other Scheduler Functions

TTCOS_511Init( ); -- Initialize the scheduler

TTCOS_Start(15); -- Start the scheduler

TTCOS_GoToSleep( ); -- Go to low power mode

TTCOS_DispatchTasks( ) -- Causes the tasks to run at

the required times

Details all hidden – if you want the details – read Pont’s book

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

Page 22: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

Lab. 2 code looks like thisint main(int) {

TTCOS_511Init( );

TTCOS_AddTask(InitFlash_Task, NO_DELAY, RUN_ONCE);

TTCOS_AddTask (InitPF_Task, NO_DELAY, RUN_ONCE);

TTCOS_AddTask(LED3_Flash, NO_DELAY, EVERY_2000_TICKS);

TTCOS_AddTask(LED4_Flash, NO_DELAY, EVERY_2000_TICKS / 2);

TTCOS_Start( 10 + 5 ); // Allow 10 user (max 15) and 5 system threads

while (1) { // Super loop

// Wait, in low power mode, for an interrupt

TTCOS_GoToSleep( );

// Run all the tasks in the system according to timing information

TTCOS_DispatchTasks( );

}

return 0;

}

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

Page 23: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

Lab. 2 – what’s missing

I have found a “co-operative scheduler” that works for the 8051 using the 8051 timer interrupts.

We need to change this to work with the Blackfin interrupts – 5 routines needed

InitializeTimer( ); StartTimer( ); StopTimer( );EnterLowPowerMode( ); -- Enter low power mode until woken up by interrupt TimerISR( ); -- What to do when the timer interrupt occurs

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

Page 24: Lab. 2 Overview. Echo Switches to LED Lab1 Task 7 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 2 / 28

Need to know

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 or move to a different processor

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