Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECS642U Embedded Systems Cyclic Execution and Polling William Marsh.

Similar presentations


Presentation on theme: "ECS642U Embedded Systems Cyclic Execution and Polling William Marsh."— Presentation transcript:

1 ECS642U Embedded Systems Cyclic Execution and Polling William Marsh

2 2 ARM University Program Copyright © ARM Ltd 2013 Acknowledgement Some slides from ARM University Program lab-in-a- box Copyright acknowledged

3 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

4 Recap: Memory Mapped I/O and GPIO Principles, Configuration and Use

5 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

6 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

7 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

8 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

9 GPIO Configuration & Use 1.Enable clock to GPIO port 2.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 RegisterUse GPIOx_PDDRData direction GPIOx_PSORSet GPIOx_PCORClear GPIOx_PTORToggle GPIOx_PDORData out GPIOx_PDIRData input

10 Alternative Pin Configuration Each pin has multiple uses

11 Headers

12 Pin Control Register (PCR) Each pin has a PCR (32 per port) MUX = 1 for alternative use 1 (GPIO) Also controls interrupts – next week

13 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 BitPort 13PORTE 12PORTD 11PORTC 10PORTB 9PORTA SIM->SCGC5 |= (1UL << 9); SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK;

14 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) ; }

15 Using GPIO for Input

16 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

17 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

18 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

19 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 ; }

20 CONFIGURE INPUTS

21 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

22 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); }

23 Design of Reactive Systems

24 Reactive System respond to change in environment Temperature rises Pressure falls … Switch closed / button pressed

25 Example Problem When the button is pressed, illuminate the light for 5 sec System

26 Design 1 Wait for button Wait for 5 sec Is Pressed Start 5 sec? Light On Light Off

27 Evaluation Design is very inflexible Extend to 2 buttons and lights? System Try it

28 Cyclic Execution Repeatedly –Test input –Calculate –Set outputs Repeat fast enough –Appears continuous –’Cycle time‘ Initialise Write outputs Read inputs Calculate How long? WAIT

29 Design 2 – Cyclic (One Light) Cycle quickly Very easily extended to two buttons & lights L == Off Start L=off T< 5 sec? Light On L=on T=0 sec Button pressed Light Off L=off Inc T Off On Yes NoYes No

30 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

31 Problem: Long Button Press Distinguish between –Button is pressed – event –Button is down – state

32 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

33 Timing Jitter Time for ‘Calculate’ varies … Cycle time varies E.g. waveform generation E.g. motor control Initialise Write outputs Read inputs Calculate WAIT

34 Modelling Reactive Systems Introducing State Transition Models (more in week 4)

35 State Transition Example Light off initialise() when [timer expires] / turn off Light on each cycle [timer not expired] / Increment timer Button pressed / turn on / start timer

36 State Transition Example Light off initialise() when [timer expires] / turn off Light on each cycle [timer not expired] / Increment timer Button pressed / turn on / start timer State with name Transition Event Guard Action

37 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

38 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


Download ppt "ECS642U Embedded Systems Cyclic Execution and Polling William Marsh."

Similar presentations


Ads by Google