Chapter3 Presentation
Chapter3 Presentation
Chapter3 Presentation
CPU-2-Device Device-2-”Mechanism”
status
register xmit/
CPU rcv
CPU Bus
serial
data port
register UART
control Baud
register Rate gen
Example: UART for serial communication
Universal asynchronous receiver transmitter (UART) : provides serial
communication – one “character” at a time.
UARTs are integrated into most microcontrollers
Allows several communication parameters to be programmed.
Bits/character, Parity even/odd/none, Baud rate, # Stop bits
Example:
no optional
char n data bits parity bit
time
UART Registers
Data (read received data, write data to be transmitted)
Control register(s) to set:
Bits per character (5,6,7,8 bits).
Enable/disable parity generation/checking.
Type of parity bit: Even, Odd, Stuck-0, Stuck-1.
Length of stop bit (1, 2 bits).
Enable interrupt on received byte/end of transmitted byte
Status register(s) to determine:
Receiver Data Ready (Newly-received data in received buffer register)
Transmitter Holding Empty (transmit holding register ready to accept new data)
Transmitter Empty (All data has been transmitted
FE, OE, PE – framing/overrun/parity error in received data
Programming I/O
0000 Data 00
Memory
Program 7F
Memory Special 80
Function
0FFF Registers FF
ARM system memory map (Cortex-M)
Single address space shared by memory and I/O registers
Memory-mapped I/O
Program memory addresses On-chip 0x0….0
(ROM, Flash) ROM
On-chip 0x2….0
Data memory addresses (RAM) RAM
I/O register addresses On-chip 0x4....0
(manufacturer peripherals) Peripherals
0x6.…0
Off-chip (external) memory External
Memory
Off-chip (external) memory
Cortex-M peripheral reg’s External 0xA….0
(NVIC, SYSTICK, …) Devices
Cortex CPU 0xE.…0
peripherals
ARM memory-mapped I/O
Define location(address) for device:
DEV1 EQU 0x40010000
Read/write assembly code:
LDR r1,=DEV1 ; set up device address
LDRB r0,[r1] ; read byte from DEV1
MOV r0,#8 ; set up value to write
STRB r0,[r1] ; write value to device
Equivalent C code:
Var1 = DEV1; //read from DEV1 to variable
DEV1 = Var1; //write variable to DEV1
Addressing I/O device registers
Example: STM32Lxx general-purpose I/O port D
; GPIOD module address definitions
GPIOD EQU 0x48000C00 ; GPIOD base address
MODE EQU 0x00 ; MODE register offset
IDR EQU 0x10 ; Input data reg offset
ODR EQU 0x14 ; Output data reg offset
Check status
Busy?
Yes
No (wait)
(data ready) Busy?
Access data
No
(device ready)
Busy/wait output example
Simplest way to program device.
Instructions test device ready status.
OUT_CHAR and OUT_STATUS are device addresses
Normally defined in a “header file” for the microcontroller
mechanism
intr ack reg
PC
IR
CPU
data/address data
reg
Global variables
main() {
while (TRUE) {
if (gotchar) { // set by intr routine
OUT_DATA = achar; //write char
OUT_STATUS = 1; //set status
gotchar = FALSE; //reset flag
}
}
other processing….
}
void input_handler() {
achar = IN_DATA; //global variable
gotchar = TRUE; //signal main prog
IN_STATUS = 0; //reset status
}
void output_handler() {
} //interrupt signals char done
Example: interrupt I/O with buffers
Queue for characters:
Queue
Queue
head tail tail Class
add_char()
remove_char()
Buffer-based input handler
void input_handler() {
char achar;
if (full_buffer()) error = 1;
else {
achar = IN_DATA; //read char
add_char(achar); //add to queue
}
IN_STATUS = 0; //reset status
if (nchars >= 1) { //buffer empty?
OUT_DATA = remove_char();
OUT_STATUS = 1; }
} //above needed to initiate output
Interrupts vs. Subroutines
CPU checks interrupt signals between instructions
Interrupt handler starting address:
fixed in some microcontrollers
usually provided as a pointer (“vector”)
CPU saves its “state”, to be restored by the interrupt handler
when it is finished
Push items on a stack
and/or: Save items in special registers
Handler should preserve any other registers that it may use
Prioritized interrupts
interrupt interrupt
acknowledge requests
interrupt
D1 D2 .. Dn controller
CPU Priorities determine what interrupt
gets CPU first.
Vectors determine what code is called
for each type of interrupt.
Mechanisms are orthogonal: most
CPUs provide both.
Interrupt prioritization
Masking: interrupt with priority lower than current priority
is not recognized until pending interrupt is complete.
Non-maskable interrupt (NMI): highest-priority, never
masked.
Often used for power-down.
Handler may choose to “enable” other interrupts (allows
handler to be preempted)
receive a empty
a
send a
empty
receive b
b
receive c bc
send b
c
Example: Prioritized I/O
high priority low priority
:interrupts :foreground :A :B :C
A,B
Interrupt vectors
Allow different devices to be handled by different code.
Interrupt vector table:
Directly supported by CPU architecture and/or
Supported by a separate interrupt-support device/function
Interrupt handler 0
Vector Table
handler 1
Head
handler 2
handler 3
Interrupt vector acquisition
:CPU :device
receive
request
receive
ack
receive
vector Synchronous msg
Asynchronous msg
Generic interrupt mechanism
Assume priority selection is
continue intr? handled before this point.
N
execution Y CPU detects interrupt request
N intr priority >
ignore current
priority?
CPU acknowledges
Y the request
ack
N
Device sends vector
Y N
bus error timeout? vector?
Y
CPU calls handler &
call table[vector]
Software processes the request
Sources of interrupt overhead
Handler execution time.
Interrupt mechanism overhead.
Register save/restore.
Pipeline-related penalties.
Cache-related penalties.
Interrupt “latency” = time from activation of interrupt signal
until event serviced.
ARM worst-case latency to respond to interrupt is 27 cycles:
2 cycles to synchronize external request.
Up to 20 cycles to complete current instruction.
3 cycles for data abort.
2 cycles to enter interrupt handling state.
Exception
Exception: internally detected error.
Example: divide by 0
ARM: undefined opcode, data abort, memory error
Exceptions are synchronous with instructions but unpredictable.
Build exception mechanism on top of interrupt mechanism.
Exceptions are usually prioritized and vectorized.
Trap/Software Interrupt
Trap (software interrupt): an exception generated by an
instruction.
Ex: Enter supervisor mode.
Often used to enter operating system (RTOS)