ecs642u embedded systems cyclic execution and polling william marsh
TRANSCRIPT
![Page 1: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/1.jpg)
ECS642U Embedded Systems
Cyclic Execution and Polling
William Marsh
![Page 2: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/2.jpg)
2ARM University ProgramCopyright © ARM Ltd 2013
Acknowledgement
•Some slides from ARM University Program lab-in-a-box•Copyright acknowledged
![Page 3: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/3.jpg)
Outline
• Recap– Memory mapped I/O– Configuration of GPIO
• Configuration of GPIO input• Design of reactive systems
– How fast?– Cyclic system design– Polling input
• Modelling reactive systems – introduction
![Page 4: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/4.jpg)
Recap: Memory Mapped I/O and GPIO
Principles, Configuration and Use
![Page 5: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/5.jpg)
Key Concept
• Output by writing to memory• Input by reading from memory• No special I/O instructions
• I/O registers mapped into memory
I/O register
Pins world
Address bus
Data bus
![Page 6: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/6.jpg)
GPIO – Digital I/O
• GPIO– Only two signal levels distinguished– Input: is input signal is a 1 or a 0?– Output: set output to 1 or 0
• Use with external devices– Input: switch– Output: LEDs
![Page 7: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/7.jpg)
Digital Output
• GPIO– Output: set output to 1 or 0
• Light an LED
Electronics notes•Resister size depends on LED colour•How much current can the micro-controller deliver?
On board
External
![Page 8: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/8.jpg)
KL25Z GPIO Ports
• Port A (PTA) to Port E (PTE)– 32 bits
• Not all port bits are available– Depends on
package pin count
Software note•Write word to set bit
![Page 9: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/9.jpg)
GPIO Configuration & Use
1. Enable clock to GPIO port2. Set pin to GPIO function
– MUX = 1
3. Set direction of pin(s) on GPIO port– Some pins input, some pins output
4. Use GPIO memory-mapped registers
Register Use
GPIOx_PDDR Data direction
GPIOx_PSOR Set
GPIOx_PCOR Clear
GPIOx_PTOR Toggle
GPIOx_PDOR Data out
GPIOx_PDIR Data input
![Page 10: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/10.jpg)
Alternative Pin Configuration
• Each pin has multiple uses
![Page 11: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/11.jpg)
Headers
![Page 12: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/12.jpg)
Pin Control Register (PCR)• Each pin has a PCR (32 per port)
• MUX = 1 for alternative use 1 (GPIO)• Also controls interrupts – next week
![Page 13: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/13.jpg)
Enable the Clock
• Enable clock to GPIO module– GPIO modules Disabled to save power– Using an unclocked module fault
• Control register SIM_SCGC5 – Clocks to GPIO ports– Enable clock to Port A
• Header file MKL25Z4.h has definitions
Bit Port13 PORTE12 PORTD11 PORTC10 PORTB9 PORTA
SIM->SCGC5 |= (1UL << 9);
SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK;
![Page 14: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/14.jpg)
Example Use
• Code to control on board red LED
void redOn(void){ // set red on without changing anything else // LED is active low – clear to light PTB->PCOR |= MASK(RED_LED_POS) ;}
void redOff(void){ // set red off with changing anything else // LED is active low – set to turn off PTB->PSOR |= MASK(RED_LED_POS) ;}
![Page 15: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/15.jpg)
Using GPIO for Input
![Page 16: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/16.jpg)
Example System with Interrupt
• Goal: react when button is pressed• External switch / button• Note logic:
– input high switch open– input low switch pressed
Electronics note•Pull-up resistor•… internal to micro-controller
![Page 17: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/17.jpg)
How to Detect Switch is Pressed?
• Polling - use software to check regularly– Works but wasteful of CPU time
• Esp. of fast response
– Scales badly – many switches?
• Interrupt – hardware– Signal to MCU– Next week
![Page 18: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/18.jpg)
How Fast?
• Ideal switch
• Timing– How soon to notice?– How short a pulse to notice?– What happens with a long
pulse (hold button)?
high
low
press
time
release
![Page 19: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/19.jpg)
Code Example
/*---------------------- isPressed: test the switch
Operating the switch connects the input to ground. A non-zero value shows the switch is not pressed. *-----------------------*/bool isPressed(void) {
if (PTD->PDIR & MASK(BUTTON_POS)) {return false ;
}return true ;
}
![Page 20: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/20.jpg)
Configure Inputs
![Page 21: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/21.jpg)
Pin Control Register (PORTx_PCRn)
• Pull-up / pull-down resistors
• PE: set to 1 to enable pull up /down• PS: 0 pull down, 1 for pull-up• IRQC controls interrupts – 0 for disable
![Page 22: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/22.jpg)
Input Configuration Code
void configureGPIOinput(void) { SIM->SCGC5 |= SIM_SCGC5_PORTD_MASK; /* Enable clock for port D */
/* Select GPIO and enable pull-up resistors and no interrupts */ PORTD->PCR[BUTTON_POS] |= PORT_PCR_MUX(1) | PORT_PCR_PS_MASK | PORT_PCR_PE_MASK | PORT_PCR_IRQC(0x0);
/* Set port D switch bit to inputs */ PTD->PDDR &= ~MASK(BUTTON_POS);}
![Page 23: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/23.jpg)
Design of Reactive Systems
![Page 24: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/24.jpg)
Reactive
• System respond to change in environment
• Temperature rises• Pressure falls• …• Switch closed / button pressed
![Page 25: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/25.jpg)
Example Problem
• When the button is pressed, illuminate the light for 5 sec
System
![Page 26: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/26.jpg)
Design 1
• Wait for button
• Wait for 5 sec
Is Presse
d
Start
5 sec?
Light On
Light Off
![Page 27: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/27.jpg)
Evaluation
• Design is very inflexible
• Extend to 2 buttons and lights?
System
Try it
![Page 28: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/28.jpg)
Cyclic Execution
• Repeatedly– Test input– Calculate– Set outputs
• Repeat fast enough – Appears continuous– ’Cycle time‘
Initialise
Write outputs
Read inputs
Calculate
How long? WAIT
![Page 29: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/29.jpg)
Design 2 – Cyclic (One Light)
• Cycle quickly
• Very easily extended to two buttons & lights
L == Off
StartL=off
T< 5 sec?
Light OnL=on
T=0 sec
Button presse
d
Light Off
L=off
Inc TOff
On
Yes
No Yes
No
![Page 30: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/30.jpg)
Cyclic Execution
• Multiple ‘tasks’– Test input– Calculate– Set outputs
• Tasks can interact with ‘global’ variables
Initialise
Write outputs
Read inputs
Calculate
Task 1
Task 2
![Page 31: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/31.jpg)
Problem: Long Button Press
• Distinguish between– Button is pressed – event– Button is down – state
![Page 32: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/32.jpg)
Polling & Latency
• Both designs 1 & 2 use polling• Design 1 blocks
– Wait until button pressed
• Design 2 does not block– Check if button pressed– ‘skip’ polling
• Latency– Latency – delay to process button press– In design 2
• Average latency = 50% cycle time• Max latency = cycle time
![Page 33: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/33.jpg)
Timing Jitter
• Time for ‘Calculate’ varies
• … Cycle time varies
• E.g. waveform generation
• E.g. motor control
Initialise
Write outputs
Read inputs
Calculate
WAIT
![Page 34: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/34.jpg)
Modelling Reactive Systems
Introducing State Transition Models
(more in week 4)
![Page 35: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/35.jpg)
State Transition Example
Light offinitialise()
when [timer expires] / turn off
Light on each cycle [timer not expired] / Increment timer
Button pressed / turn on / start timer
![Page 36: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/36.jpg)
State Transition Example
Light offinitialise()
when [timer expires] / turn off
Light on each cycle [timer not expired] / Increment timer
Button pressed / turn on / start timer
State with name
TransitionEvent
Guard
Action
![Page 37: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/37.jpg)
Light and Button
Light off
Light on
Open
Closed
Button pressed
Button released
Software issue•Read button state (cf event)•Use variable to show event
![Page 38: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh](https://reader031.vdocuments.us/reader031/viewer/2022020920/56649f1e5503460f94c361d6/html5/thumbnails/38.jpg)
Summary
• Memory mapped I/O– Read / write instructions– Output: address write pin device– Input: device pin address read
• Polling– Read input for device … repeatedly – Do not block
• Cyclic systems can be reactive– Cycle fast enough to respond to events