38
ECS642U Embedded Systems Cyclic Execution and Polling William Marsh

ECS642U Embedded Systems Cyclic Execution and Polling William Marsh

Embed Size (px)

Citation preview

Page 1: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh

ECS642U Embedded Systems

Cyclic Execution and Polling

William Marsh

Page 2: 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

Page 3: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh

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

Recap: Memory Mapped I/O and GPIO

Principles, Configuration and Use

Page 5: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh

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

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

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

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

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

Alternative Pin Configuration

• Each pin has multiple uses

Page 11: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh

Headers

Page 12: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh

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

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

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

Using GPIO for Input

Page 16: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh

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

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

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

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

Configure Inputs

Page 21: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh

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

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

Design of Reactive Systems

Page 24: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh

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

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

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

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

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

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

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

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

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

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

Modelling Reactive Systems

Introducing State Transition Models

(more in week 4)

Page 35: ECS642U Embedded Systems Cyclic Execution and Polling William Marsh

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

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

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

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