Programmed I/O - College of Information & Computer Sciences

30
Programmed I/O Getting the right bits in the right place at the right time

Transcript of Programmed I/O - College of Information & Computer Sciences

Page 1: Programmed I/O - College of Information & Computer Sciences

Programmed I/OGetting the right bits in the right place at the right time

Page 2: Programmed I/O - College of Information & Computer Sciences

I/O Registers• Early machines had op-codes to access a “bank” of I/O registers

• Locations in the bank only responded if the corresponding I/O device was installed

• Sometimes locations were hard-coded

• Other cases, I/O devices had switches that were set to indicate an ID number

• Some systems assigned them by bus position

Page 3: Programmed I/O - College of Information & Computer Sciences

I/O Registers

• Made sense when there were few I/O devices in existence

• In retrospect, clearly shortsighted

• Devices become obsolete, but still in ISA

• Can run out of room for new devices

Page 4: Programmed I/O - College of Information & Computer Sciences

Memory Mapped I/O

• Reserve memory space for the “registers”

• OS can map physical locations to virtual ones in user space (gives more flexibility)

• No special I/O instructions: Load/Store

• Locations have side effects (e.g., changing one might cause a change in others, reading may cause a change in value)

Page 5: Programmed I/O - College of Information & Computer Sciences

mbed Programmed I/O

• DigitalOut, DigitalIn, DigitalInOut

• BusIn, BusOut, BusInOut

• PortIn, PortOut, PortInOut

• PwmOut

• AnalogIn, AnalogOut

Ones we’ve already seen

Page 6: Programmed I/O - College of Information & Computer Sciences

DigitalIn• Constructor: DigitalIn(pinName)

• int read() returns 0 or 1

• () overloaded for read

• void mode (PinMode) sets mode of pin to

• PullUp, PullDown, PullNone, OpenDrain

• Default is PullDown

Page 7: Programmed I/O - College of Information & Computer Sciences

Digital Pins in Blue

Page 8: Programmed I/O - College of Information & Computer Sciences

BusIn/Out, PortIn/Out

• Bus I/O lets you specify up to 16 pins to be read or written at once

• Port I/O lets you directly read or write the LPC1768 GPIO ports

• Faster, but limited to a contiguous set of logical port pins

Page 9: Programmed I/O - College of Information & Computer Sciences

Wiring

• Connect wire from p8 to top right lead of one of the push buttons

• Wire from top left lead of button to VDD

• Wire from p21 (lower left corner) to an LED in the lower board

Page 10: Programmed I/O - College of Information & Computer Sciences

New Project

Page 11: Programmed I/O - College of Information & Computer Sciences

ORAND NAND⊕

Connect p21 to an LED

Page 12: Programmed I/O - College of Information & Computer Sciences

ORAND NAND⊕

Connect p8 to a button

Page 13: Programmed I/O - College of Information & Computer Sciences

ORAND NAND⊕

Connect other leg of button to + rail

Page 14: Programmed I/O - College of Information & Computer Sciences

Code

• In mbed, open a new program, ButtonDemo

• Use the Blinky Template

• Go to course home page

• Open BUTTONDEMO.TXT

• Copy and paste contents into main.cpp

Page 15: Programmed I/O - College of Information & Computer Sciences

Declarations

Page 16: Programmed I/O - College of Information & Computer Sciences

main

Page 17: Programmed I/O - College of Information & Computer Sciences

Helper Functions

Page 18: Programmed I/O - College of Information & Computer Sciences

Compile and Run• Lights light while button is pressed

• Each press advances to next on-board LED

• External LED grows dimmer

• Processor is continually checking the status of the button pin as fast as it can

• Called polling — loop looks repeatedly for button to change

Page 19: Programmed I/O - College of Information & Computer Sciences

Debouncing• Note use of wait(0.1) in code

• Try commenting out and see what happens

Page 20: Programmed I/O - College of Information & Computer Sciences

Debouncing• Note use of wait(0.1) in code

• Try commenting out and see what happens

• Random behavior is because switch doesn’t close instantly

• Contact is made and broken multiple times before solid connection

• May only be milliseconds between, but for a 100MHz processor, that’s 100K cycles

Page 21: Programmed I/O - College of Information & Computer Sciences

Interrupts, Traps, ExceptionsCousins to Subroutines

Page 22: Programmed I/O - College of Information & Computer Sciences

Interrupt Driven I/O• Polling is inefficient, doesn’t multitask

• Device signals CPU when it is ready or done

• CPU asynchronously jumps to interrupt service routine to handle the device request

• Device ID is used to BL via an address table

• Returns when done

Page 23: Programmed I/O - College of Information & Computer Sciences

LPC1768 Interrupt Table

Page 24: Programmed I/O - College of Information & Computer Sciences

Classes of Interrupts• In general, different devices have different priorities, often

dependent on data rate or timing

• High priority interrupts mask low priority

• Typically disable own priority, at least for a critical section at start of service routine

• Non-maskable interrupt is always handled

• Generally assumes supervisor state and OS

Page 25: Programmed I/O - College of Information & Computer Sciences

Interrupt Enable

• Beyond priority, an individual device may have a status bit to enable/disable

• If it has been disabled, its requests are ignored

• If enabled, then some combination of one or more status bits will trigger an interrupt, perhaps specifying a priority

Page 26: Programmed I/O - College of Information & Computer Sciences

When to Interrupt?

• In a simple processor, between Execute and Fetch phases -- effectively, inserting instructions into the stream

• With pipelining, not as clear. Some interrupts may squash current instructions and require restart

• Usually handled by hardware, but OS can choose to do something else (not return, call user exception, etc.)

Page 27: Programmed I/O - College of Information & Computer Sciences

mbed InterruptIn• Constructor: InterruptIn(PinName)

• void mode (PinMode) sets PullUp, PullDown

• void rise (void(*fptr)(void)) connects a function to the pin as an interrupt service

• void fall (void(*fptr)(void)) same as rise, but responds to signal falling to 0

• void enable_irq(), void disable_irq()

• All numbered pins except p19, p20

Page 28: Programmed I/O - College of Information & Computer Sciences

Demo

• Go to mbed.org/handbook/InterruptIn

• Find InterruptIn_HelloWorld

• Click on Import program

• In compiler change InterruptIn constructor to use p8 instead of p5

• Compile and run

Page 29: Programmed I/O - College of Information & Computer Sciences

Code

Interrupt service routine

Point handler table to routine

Page 30: Programmed I/O - College of Information & Computer Sciences

What’s Happening• LED4 is flashing in the loop in main

• When you press the button, the flip routine is called immediately

• flip returns to whatever instruction was interrupted when you pressed the button

• There is no polling of the button

• main doesn’t have to wait for the signal