ECS642U Embedded Systems
Cyclic Execution and Polling
William Marsh
2ARM University ProgramCopyright © ARM Ltd 2013
Acknowledgement
•Some slides from ARM University Program lab-in-a-box•Copyright acknowledged
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
Recap: Memory Mapped I/O and GPIO
Principles, Configuration and Use
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
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
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
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
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
Alternative Pin Configuration
• Each pin has multiple uses
Headers
Pin Control Register (PCR)• Each pin has a PCR (32 per port)
• MUX = 1 for alternative use 1 (GPIO)• Also controls interrupts – next week
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;
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) ;}
Using GPIO for Input
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
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
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
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 ;
}
Configure Inputs
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
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);}
Design of Reactive Systems
Reactive
• System respond to change in environment
• Temperature rises• Pressure falls• …• Switch closed / button pressed
Example Problem
• When the button is pressed, illuminate the light for 5 sec
System
Design 1
• Wait for button
• Wait for 5 sec
Is Presse
d
Start
5 sec?
Light On
Light Off
Evaluation
• Design is very inflexible
• Extend to 2 buttons and lights?
System
Try it
Cyclic Execution
• Repeatedly– Test input– Calculate– Set outputs
• Repeat fast enough – Appears continuous– ’Cycle time‘
Initialise
Write outputs
Read inputs
Calculate
How long? WAIT
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
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
Problem: Long Button Press
• Distinguish between– Button is pressed – event– Button is down – state
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
Timing Jitter
• Time for ‘Calculate’ varies
• … Cycle time varies
• E.g. waveform generation
• E.g. motor control
Initialise
Write outputs
Read inputs
Calculate
WAIT
Modelling Reactive Systems
Introducing State Transition Models
(more in week 4)
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 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
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
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