The right and wrong ways for constructing tasks for the Labs and Assignment.

Slides:



Advertisements
Similar presentations
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Advertisements

Thermal arm-wrestling Design of a video game using two programmable flags (PF) interrupts Tutorial on handling 2 Hardware interrupts from an external device.
Lab. 2 – More details – Tasks 4 to 6 1. What concepts are you expected to understand after the Lab. 2 is finished? 2. How do you demonstrate that you have.
Core Timer Code Development How you could have done the Take- Home Quiz using a test driven development (TDD) approach.
Lab. 2 Overview 1. What concepts are you expected to understand after the Lab. 2 is finished? 2. How do you demonstrate that you have that knowledge?
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Timers and Timing Signals Tutorial. 6/18/2015 Timer Control Copyright M. Smith, ECE, University of Calgary, Canada 2 / 31 Temperature Sensor -- Lab 3.
Developing Tasks to use with a co-operative scheduler Ideas for Assignment 2 Lab. 2, 3 and 4 Review for Midterm on Lab. 2 Ideas of defects, errors and.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
Peripherals and their Control An overview of industrially available “peripheral devices” that use “pulse-width modulation” for information passing. Review.
Nachos Phase 1 Code -Hints and Comments
Humboldt University Berlin, University of Novi Sad, University of Plovdiv, University of Skopje, University of Belgrade, University of Niš, University.
Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University.
CE Operating Systems Lecture 11 Windows – Object manager and process management.
Pointers OVERVIEW.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Lab. 2 Overview Move the tasks you developed in Lab. 1 into the more controllable TTCOS operating system Manual control of RC car.
ECS642U Embedded Systems Cyclic Execution and Polling William Marsh.
Wall Encounter By Made easy by Dwayne Abuel.
Moving Arrays -- 1 Completion of ideas needed for a general and complete program Final concepts needed for Final Review for Final – Loop efficiency.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
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.
Mistakes, Errors and Defects. 12/7/2015Mistakes, Errors, Defects, Copyright M. Smith, ECE, University of Calgary, Canada 2 Basic Concepts  You are building.
Handling multiple input signals Version #2 – co-operative scheduler Version #3 – pre-emptive scheduler.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Written by: Dr. JJ Shepherd
Building a simple loop using Blackfin assembly code If you can handle the while-loop correctly in assembly code on any processor, then most of the other.
1 Run-to-Completion Non-Preemptive Scheduler. 2 In These Notes... What is Scheduling? What is non-preemptive scheduling? Examples Run to completion (cooperative)
Slides created by: Professor Ian G. Harris Operating Systems  Allow the processor to perform several tasks at virtually the same time Ex. Web Controlled.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
“Lab. 5” – Updating Lab. 3 to use DMA Test we understand DMA by using some simple memory to memory DMA Make life more interesting, since hardware is involved,
Developing Tasks to use with a co-operative scheduler Ideas for Assignment 2 Lab. 2, 3 and 4 Review for Midterm on Lab. 2 Ideas of defects, errors and.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Introduction In this lab , we will learn
Chapter 5: Process Synchronization – Part 3
Concurrency.
Getting Started with the µC/OS-III Real Time Kernel
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Moving Arrays -- 1 Completion of ideas needed for a general and complete program Final concepts needed for Final Review for Final – Loop efficiency.
Lab. 2 Overview – Earlier Tasks Prelaboratory T1, T3, T4 and T5
Lab. 2 Overview.
Thermal arm-wrestling
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Lab. 2 – More details – Later tasks
Moving Arrays -- 1 Completion of ideas needed for a general and complete program Final concepts needed for Final Review for Final – Loop efficiency.
Moving Arrays -- 2 Completion of ideas needed for a general and complete program Final concepts needed for Final DMA.
Thermal arm-wrestling
CPU scheduling decisions may take place when a process:
Moving Arrays -- 2 Completion of ideas needed for a general and complete program Final concepts needed for Final DMA.
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Getting serious about “going fast” on the TigerSHARC
Thermal arm-wrestling
Explaining issues with DCremoval( )
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Lab. 2 Overview Move the tasks you developed in Lab. 1 into the more controllable TTCOS operating system.
Thermal arm-wrestling
Mistakes, Errors and Defects
Why Threads Are A Bad Idea (for most purposes)
Working with the Compute Block
Why Threads Are A Bad Idea (for most purposes)
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
Chapter 1: Creating a Program.
Presentation transcript:

The right and wrong ways for constructing tasks for the Labs and Assignment

Assignment 1 – What I suggested #define ULL unsigned long long int // Place in include / header file void main(void) { ULL currentCycleCount = ReadCycleCounter( ); ULL nextLED1ExecutionTime = currentCycleCount + ONE_SECOND ULL nextLED2ExecutionTime = currentCycleCount + TWO_SECONDS while (1) { currentCycleCount = ReadCycleCounter( ); if (nextLED1ExecutionTime < currentCycleCount) { nextLED1ExecutionTime = nextLED1ExecutionTime + ONE_SECOND LED1Task( ); } if (nextLED2ExecutionTime < currentCycleCount) { nextLED2ExecutionTime = nextLED2ExecutionTime + TWO_SECONDS LED2Task( ); } } 2 /18

Assignment 1 – What many did! Why is this bad programming? ULL currentCycleCount = ReadCycleCounter( ); ULL nextLED1ExecutionTime = currentCycleCount + ONE_SECOND ULL nextLED2ExecutionTime = currentCycleCount + TWO_SECONDS int led1 = 0; int led2 = 0; while (1) { currentCycleCount = ReadCycleCounter( ); if (nextLED1ExecutionTime < currentCycleCount) { nextLED1ExecutionTime = nextLED1ExecutionTime + ONE_SECOND if (led1 == 0) { My_LEDWrite(1, 1); // If off, turn on led1 = 1; } else { My_LedWrite(1, 0); // If on, turn off led1 = 0; } } 3 /18

Bad programming because Imagine what this code would look like with 15 tasks Too complicated – all the detail is in one piece of code Somebody reading / maintaining main( ) does not need all this detail.  That person could be you in your internship position SOLUTION Profound Procrastination Programming Proper words – Abstraction – Hide details 4 /18

Define CODE ERROR and CODE DEFECT Everybody is likely to make mistakes  Keep track of mistakes you make  Identify the two most common  Do code review looking for those particular errors Every project is made up of phases  If you make a mistake and find it BEFORE moving onto the next phase – CODE ERROR and EASY to find as the mistake is in last twenty lines or so  If you make a mistake and DON’T find it BEFORE moving onto the next phase – CODE DEFECT and HARD to find and fix 5 /18

Assignment 1 – Why does this code contain a CODE DEFECT #define ULL unsigned long long int // Place in include / header file void main(void) { ULL currentCycleCount = ReadCycleCounter( ); ULL nextLED1ExecutionTime = currentCycleCount + ONE_SECOND ULL nextLED2ExecutionTime = currentCycleCount + TWO_SECONDS while (1) { currentCycleCount = ReadCycleCounter( ); if (nextLED1ExecutionTime < currentCycleCount) { nextLED1ExecutionTime = nextLED1ExecutionTime + ONE_SECOND LED1Task( ); } if (nextLED2ExecutionTime < currentCycleCount) { nextLED2ExecutionTime = currentCycleCount + ONE_SECOND LED2Task( ); } } 6 /18

Define LIFE ERROR and LIFE DEFECT Everybody is likely to make mistakes  Keep track of mistakes you make  Identify the two most common  Do life review looking for those particular errors Every project is made up of phases  If you make a mistake and find it BEFORE moving onto the next phase – LIFE ERROR and EASY to find as the mistake is in last twenty minutes or so  If you make a mistake and DON’T find it BEFORE moving onto the next phase – LIFE DEFECT and HARD to fix 7 /18

Define DESIGN ERROR and DESIGN DEFECT Everybody is likely to make mistakes  Keep track of mistakes you make  Identify the two most common  Do design review looking for those particular errors Every project is made up of phases  If you make a mistake and find it BEFORE moving onto the next phase – DESIGN ERROR and EASY to find as the mistake is in last twenty lines or so  If you make a mistake and DON’T find it BEFORE moving onto the next phase – DESIGN DEFECT and HARD to find and fix 8 /18

Design and implementation of code void LED3_Task( ) // DESIGN Uses Read LED( ) and WriteLED( ) from Lab. 1 if LED3 is on turn it off if LED3 is off turn it on } WHY DOES THIS IMPLEMENTATION NOT WORK – DEFECT QUICKLY FOUND void LED3_Task( ) { bool LED3_on = true; currentLED = ReadLED_ASM( ); if (LED3_on == true) // turn off LED 3 WriteLED_ASM(currentLED & 0x4); // 0x4 is magic number for LED 3 %0100 else WriteLED_ASM(currentLED | 0x4); // 0x4 is magic number for LED 3 %0100 LED3_on = ! LED3_on } 12/19/2015TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 9 / 28

Design and implementation of code void LED3_Task( ) // DESIGN Uses Read LED( ) and WriteLED( ) from Lab. 1 if LED3 is on turn it off if LED3 is off turn it on } FIXED void LED3_Task( ) { static bool LED3_on = true; // Variable is a private permaneant variable (not on stack) currentLED = ReadLED_ASM( ); if (LED3_on == true) // turn off LED 3 WriteLED_ASM(currentLED & 0x4); // 0x4 is magic number for LED 3 %0100 else WriteLED_ASM(currentLED | 0x4); // 0x4 is magic number for LED 3 %0100 LED3_on = ! LED3_on } 12/19/2015TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 10 / 28

Design and implementation of code void LED3_Task( ) // DESIGN Uses Read LED( ) and WriteLED( ) from Lab. 1 if LED3 is on turn it off if LED3 is off turn it on } WHY DOES THIS IMPLEMENTATION MOST OF THE TIME void LED3_Task( ) { currentLED = ReadLED_ASM( ); int LED3_on = currentLED & 0x4; // Check if current LED is on if (LED3_on == 1) // turn off LED 3 WriteLED_ASM(currentLED & 0x4); // 0x4 is magic number for LED 3 %0100 else WriteLED_ASM(currentLED | 0x4); // 0x4 is magic number for LED 3 %0100 } 12/19/2015TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 11 / 28

Why I don’t like this sort of coding void LED3_Task( ) { static bool LED3_on = true; // Variable is a private permanent variable (not on stack) currentLED = ReadLED_ASM( ); if (LED3_on == true) // turn off LED 3 WriteLED_ASM(currentLED & 0x4); // 0x4 is magic number for LED 3 %0100 else WriteLED_ASM(currentLED | 0x4); // 0x4 is magic number for LED 3 %0100 LED3_on = ! LED3_on } // Main answer – not easy to generalize when the coding gets more complicated // I want to see ‘STATES’ 12/19/2015TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 12 / 28

Rewritten as States enum {UNKNOWN, LED_ON, LED_OFF} void LED3_Task( ) { static unsigned int LED3_state = LED_OFF; // Assume InitLED( ) turns LED3 off unsigned int LED3_nextState = UNKNOWN; // NOT static – very important unsigned int currentLED = ReadLEDASM( ); switch (LED3_state ) { case LED_ON: // Move from state ON to state OFF WriteLED_ASM(currentLED & 0x4); // 0x4 is magic number for LED 3 %0100 LED3_nextState = LED_OFF; break; case LED_OFF: // Move from state ON to state OFF WriteLED_ASM(currentLED | 0x4); // 0x4 is magic number for LED 3 %0100 LED3_nextState = LED_ON; break; default: SendErrorMessage(‘Should not get here\n”); } LED3_state = LED3_nextState; // Update the state } 12/19/2015TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 13 / 28

Is this – a state of being ‘too clever’? #define BEGIN_STATE case #define END_STATE break #define ERROR_STATE default void LED3_Task( ) { static unsigned int LED3_state = LED_OFF; // Assume InitLED( ) turns LED3 off unsigned int LED3_nextState = UNKNOWN; // NOT static – very important unsigned int currentLED = ReadLEDASM( ); switch (LED3_state ) { BEGIN_STATE: LED_ON: // Move from state ON to state OFF WriteLED_ASM(currentLED & 0x4); // 0x4 is magic number for LED 3 %0100 LED3_nextState = LED_OFF; END_STATE; BEGIN_STATE: LED_OFF: // Move from state ON to state OFF WriteLED_ASM(currentLED | 0x4); // 0x4 is magic number for LED 3 %0100 LED3_nextState = LED_ON; END_STATE; ERROR_STATE: SendErrorMessage(‘Should not get here\n”); } LED3_state = LED3_nextState; // Update the state } 12/19/2015TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 14 / 28

Design defect – too much detail Hide the detail behind an operating system #define ULL unsigned long long int // Place in include / header file void main(void) { ULL currentCycleCount = ReadCycleCounter( ); ULL nextLED1ExecutionTime = currentCycleCount + ONE_SECOND ULL nextLED2ExecutionTime = currentCycleCount + TWO_SECONDS while (1) { currentCycleCount = ReadCycleCounter( ); if (nextLED1ExecutionTime < currentCycleCount) { nextLED1ExecutionTime = nextLED1ExecutionTime + ONE_SECOND LED1Task( ); } if (nextLED2ExecutionTime < currentCycleCount) { nextLED2ExecutionTime = nextLED2ExecutionTime + TWO_SECONDS LED2Task( ); } } 15 /18

Using a co-operative scheduler which is responsible for handling the timing #define ONE_SECOND (44100) // Audio interrupts / sec #define TWO_SECONDS (ONE_SECOND) #define NO_DELAY (0) int main(int) { TTCOS_511Init( ); TTCOS_AddTask(InitFlash_Task, NO_DELAY, RUN_ONCE); TTCOS_AddTask(LED1Task, NO_DELAY, EVERY_SECOND); TTCOS_AddTask(LED2Task, NO_DELAY, TWO_SECONDS); TTCOS_Start( ); // Allow 10 user (max 15) and 5 system threads while (1) {// Super loop TTCOS_GoToSleep( ); TTCOS_DispatchTasks( ); // Run tasks if time delay } return 0; } 12/19/2015TDD-Core Timer Library, Copyright M. Smith, ECE, University of Calgary, Canada 16 / 28

Rules for task Two types of tasks  Pre-emptive –special, very, very time critical E.g. Store audio sample every 1 / Only pre-emptive task in our labs is ProcessData  Normal – not time critical E.g. read switch presses – every 1/ 30 is fast enough NO TASK CAN INCLUDE A WAIT All tasks must have this format void Task(void) 17 /18

Write tasks that change LEDs with switch presses void BadSwitchTask( ) { unsigned int switch1 = ReadSwitchASM(1); while (switch1 == 0) { switch1 = ReadSwitchASM(1); } WriteLED(1, 1); // Switch is pressed – LED on while (switch1 == 1) { switch1 = ReadSwitchASM(1); } WriteLED(1, 0); // Switch is released – LED off } // This task is said to BLOCK other tasks from running 18 /18

Write tasks that change LEDs with switch presses TTCOS_AddTask(BadSwitchTask, NO_DELAY; EVERY_SECOND / 30) void BadSwitchTask(void ) { unsigned int switch1 = ReadSwitchASM(1); while (switch1 == 0) { switch1 = ReadSwitchASM(1); } WriteLED(1, 1); // Switch is pressed – LED on while (switch1 == 1) { switch1 = ReadSwitchASM(1); } WriteLED(1, 0); // Switch is released – LED off } // This task is said to BLOCK other tasks from running 19 /18

Write tasks that change LEDs with switch presses – this code version does not block enum {PRESSED, NOT_PRESSED} void SwitchTask_NoWait(void ) { static unsigned int currentSwitch1 = NOT_PRESSED; unsigned int switch1 = ReadSwitchASM(1); // Nothing has changed – so don’t wait if ((switch1 == 0) && (currentSwitch1 = NOT_PRESSED) ) return; if ((switch1 ==1) && (currentSwitch1 = PRESSED) ) return; switch (currentSwitch1) { case NOT_PRESSED: WriteLED(1, 1); // Switch is pressed – LED on nextState = PRESSED; break; case PRESSED: WriteLED(1, 0); // Switch is released – LED off nextState = NOT_PRESSED; break; } currentSwitch1 = nextState ; } 20 /18