Programmed I/O - College of Information & Computer Sciences

Post on 27-Apr-2022

3 views 0 download

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

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

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

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

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)

mbed Programmed I/O

• DigitalOut, DigitalIn, DigitalInOut

• BusIn, BusOut, BusInOut

• PortIn, PortOut, PortInOut

• PwmOut

• AnalogIn, AnalogOut

Ones we’ve already seen

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

Digital Pins in Blue

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

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

New Project

ORAND NAND⊕

Connect p21 to an LED

ORAND NAND⊕

Connect p8 to a button

ORAND NAND⊕

Connect other leg of button to + rail

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

Declarations

main

Helper Functions

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

Debouncing• Note use of wait(0.1) in code

• Try commenting out and see what happens

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

Interrupts, Traps, ExceptionsCousins to Subroutines

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

LPC1768 Interrupt Table

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

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

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.)

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

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

Code

Interrupt service routine

Point handler table to routine

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