Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab. 2 Overview.

Similar presentations


Presentation on theme: "Lab. 2 Overview."— Presentation transcript:

1 Lab. 2 Overview

2 Echo Switches to LED Lab1 Task 7
11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

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 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

4 If we used the “super-loop idea”
int main( ) { InitLED( ); while (1) { TaskFlashLED3( ); TaskFlashLED2( ); value = ReadSwitches( ); WriteLED(value >> 8); } 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? 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

5 Auto control of an aeroplane
Lists the tasks 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

6 From Pont’s book 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

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 The code in the book was for the 8051 processor and I have modified the code to run on Blackfin Processor 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

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 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

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

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 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

11 Solve this problem using “lock”
#define UNLOCKED 0 #define LOCKED 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( ) WriteLEDASM(LED4_ON); 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

12 Pre-emptive scheduler
11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

13 Solve this problem using “lock”
#define UNLOCKED 0 #define LOCKED 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( ) WriteLEDASM(LED4_ON); Suppose LED3_Task gets switched out here and LED4_Task runs LED4_Task thinks the resource is free to use 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

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

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( ); } 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

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” 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( ); } 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

17 Lab. 2 I have found a “co-operative scheduler” that works for the 8051 using the 8051 timer interrupts. The scheduler is easy to use and the tasks are easy to write (using existing Lab. 1 code) 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

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( ) if LED4 is on turn it off if LED4 is off turn it on 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

19 Scheduler functions SCH_Add_Task(TaskName, Delay, Period)
Thus SCH_Add_Task(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 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

20 Scheduler functions SCH_Add_Task(TaskName, Delay, Period)
Thus SCH_Add_Task(Led3, 0, 1000); SCH_Add_Task(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 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

21 Other Scheduler Functions
#define MICROSECS_BETWEEN_INTERRUPTS 1000 static unsigned int tickTime = MICROSECS_BETWEEN_INTERRUPTS; SCH_Init_Scheduler(tickTime); -- Initializes the timer so that the tick time is 1 ms SCH_Start_Scheduler( ); -- Start the scheduler SCH_Dispatch_Tasks( ); -- Causes the tasks to run at the required times 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

22 Lab. 2 code looks like this
int main(int) { SCH_Init_Scheduler(tickTime); SCH_Add_Task(InitFlash_Task, NO_DELAY, RUN_ONCE); SCH_Add_Task(InitPF_Task, NO_DELAY, RUN_ONCE); SCH_Add_Task(LED3_Flash, NO_DELAY, EVERY_2000_TICKS); SCH_Add_Task(LED4_Flash, NO_DELAY, EVERY_2000_TICKS / 2); SCH_Start_Scheduler( ); while (1) { // Super loop // Wait, in low power mode, for an interrupt SCH_EnterLowPowerMode( ); // Run all the tasks in the system according to timing information SCH_Dispatch_Tasks( ); } return 0; 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

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 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada

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 11/15/2018 TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada


Download ppt "Lab. 2 Overview."

Similar presentations


Ads by Google