Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Lab. 2 Overview

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

3 How would you handle a more complex set of embedded tests Echo switches 1 and 2 in LED’s 1 and 2 Flash LED 3 every 2 seconds Flash LED 4 every 1 second Flash LED 5 every ½ second Flash LED 6 every ¼ second Etc etc 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 3 / 28

4 If we used the “super-loop idea” int main( ) { InitLED( ); while (1) { TaskFlashLED3( ); TaskFlashLED2( ); value = ReadSwitches( ); WriteLED(value >> 8); } 12/4/2015 TDD-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?

5 Auto control of an aeroplane Lists the tasks 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 5 / 28

6 From Pont’s book 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 6 / 28

7 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 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 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 7 / 28

8 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 Co-operative Scheduler 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 8 / 28

9 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 9 / 28

10 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 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 10 / 28

11 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; } 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 11 / 28

12 Pre-emptive scheduler What if task gets switched out at ‘critical time’? 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 12 / 28

13 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; } 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 13 / 28 Suppose LED3_Task gets switched out here and LED4_Task runs LED4_Task thinks the resource is free to use

14 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 14 / 28

15 Scheduler code looks like this int 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 ( ); } 12/4/2015TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 15 / 28

16 Power saving code int 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 ( ); } 12/4/2015TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 16 / 28

17 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) 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 17 / 28

18 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 } 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 18 / 28

19 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 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 19 / 28

20 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 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 20 / 28

21 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 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 21 / 28

22 Lab. 2 code looks like this int 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; } 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 22 / 28

23 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 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 23 / 28

24 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 12/4/2015 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 24 / 28


Download ppt "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."

Similar presentations


Ads by Google