EMBEDDED SYSTEM DESIGN
INPUT AND OUTPUT
Doan Duy, Ph. D.
Email: duyd@[Link]
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 1
Objective and Content
◼ Overview of IO interface
◼ Hardware and Software IO
◼ Interrupt
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 2
Content
◼ Overview of IO interface
◼ Hardware and Software IO
◼ Interrupt
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 3
Connecting the Analog and Digital Worlds
Cyber: Physical:
• Digital • Continuum
• Discrete in time • Continuous in time
• Sequential • Concurrent
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 4
Practical Issues
• Analog vs. digital
• Wired vs. wireless
• Serial vs. parallel
• Sampled or event triggered
• Bit rates
• Access control, security, authentication
• Physical connectors
• Electrical requirements (voltages and currents)
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 5
A Typical Microcomputer Board
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 6
◼One of eight configurations
with SPI buses, analog I/O, etc.
Many GPIO
pins can be
reconfigured to
be PWM
drivers, timers,
etc.
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 7
Memory-Mapped
Peripherals
DIO158_OUT is a C preprocessor
macro defined in a header file in
your IDE project. It defines the
memory address of this register.
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 8
Content
◼ Overview of IO interface
◼ Hardware and Software IO
◼ Interrupt
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 9
Typical GPIO
◼ Open collector circuits GPIO pins configured for
bus output. Any one
are often used on GPIO controller can pull the bus
(general-purpose I/O) pins voltage down.
of a microcontroller.
◼ The same pin can be
used for input and output.
And multiple users can
connect to the same bus.
◼ The current is limited!?
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 10
Example of LED connection
◼ Assume GPIO pins can
3V VDD
sink up to 18 mA. Assume
the LED, when forward
biased (turned on), has a
voltage drop of 2 volts.
◼ What resistor should you
use?
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 11
Example of LED connection
Ohm’s law:
3V VDD
V = IR
When LED is on, V = 1 volt.
To limit to 18mA,
R ≥ 1/0.018 ≈ 56 ohms
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 12
PCI
Wired Connections
Parallel vs. Serial Digital Interfaces
Parallel (one wire per bit)
ATA: Advanced Technology Attachment SCSI
PCI: Peripheral Component Interface
SCSI: Small Computer System Interface
…
Serial (one wire per direction) USB
RS-232
SPI: Serial Peripheral Interface bus
I2C: Inter-Integrated Circuit
USB: Universal Serial Bus
SATA: Serial ATA RS-232
…
Mixed (one or more “lanes”)
PCIe: PCI Express
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 13
Serial Interfaces
◼ The old but persistent RS-232
standard supports asynchronous serial
connections (no common clock).
◼ How does it work?
Many uses of RS-232 are being
replaced by USB, which is
electrically simpler but with a
more complex protocol, or
Bluetooth, which is wireless.
Uppercase ASCII "K" character (0x4b)
with 1 start bit, 8 data bits, 1 stop bit.
Image license: Creative Commons
ShareAlike 1.0 License
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 14
UART: Universal Asynchronous
Receiver-Transmitter
• Convert serial data to
parallel data, and vice
versa.
• Uses shift registers to
load store data
• Can raise interrupt
when data is ready
• Commonly used with
RS-232 interface
Variant: USART: Universal Synchronous/Asynchronous
Receiver-Transmitter
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 15
Input/Output Mechanisms in Software
Polling
⚫ Main loop uses each I/O device periodically.
⚫ If output is to be produced, produce it.
⚫ If input is ready, read it.
Interrupts
⚫ External hardware alerts the processor that input is ready.
⚫ Processor suspends what it is doing.
⚫ Processor invokes an interrupt service routine (ISR).
⚫ ISR interacts with the application concurrently.
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 16
Polling
Processor Setup Code
Processor checks I/O control register Ready
Processor services I/O 1
for status of peripheral 1
Not Ready
Processor checks I/O control register Ready Processor services I/O 2
for status of peripheral 2
Not Ready
Processor checks I/O control register
Ready Processor services I/O 3
for status of peripheral 3
Not Ready
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 17
Using a Serial Interface
In an Atmel AVR 8-bit microcontroller, to send a byte over a
serial port, the following C code will do:
while(!(UCSR0A & 0x20));
UDR0 = x;
• x is a variable of type uint8.
• UCSR0A and UDR0 are variables defined in a header.
• They refer to memory-mapped registers in the UART (Universal
Asynchronous Receiver-Transmitter)
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 18
Send a Sequence of Bytes
for(i = 0; i < 8; i++) {
while(!(UCSR0A & 0x20));
UDR0 = x[i];
}
How long will this take to execute? Assume:
• 57600 baud serial speed.
• 8/57600 =139 microseconds.
• Processor operates at 18 MHz.
Each for loop iteration will consume about 2502 cycles.
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 19
Receiving via UART
Again, on an Atmel AVR:
while(!(UCSR0A & 0x80));
return UDR0;
• Wait until the UART has received an incoming byte.
• The programmer must ensure there will be one!
• If reading a sequence of bytes, how long will this take?
Under the same assumptions as before, it will take about 2502
cycles to receive each byte.
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 20
Content
◼ Overview of IO interface
◼ Hardware and Software IO
◼ Interrupt
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 21
Input/Output Mechanisms in Software
Polling
⚫ Main loop uses each I/O device periodically.
⚫ If output is to be produced, produce it.
⚫ If input is ready, read it.
Interrupts
⚫ External hardware alerts the processor that input is ready.
⚫ Processor suspends what it is doing.
⚫ Processor invokes an interrupt service routine (ISR).
⚫ ISR interacts with the application concurrently.
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 22
Interrupt
Interrupt Service Routine
Short subroutine that handles the interrupt
Processor Setup Code
Register the Interrupt Service Routine
Interrupt!
Context switch
Processor executes task code Run Interrupt Service Routine
Resume
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 23
Interrupts
Program memory addresses,
not data memory addresses.
◼ Triggers: Source: ATmega168 Reference Manual
A level change on an interrupt request pin
Writing to an interrupt pin configured as an output ( “ software
interrupt”) or executing special instruction
◼ Responses:
Disable interrupts.
Push the current program counter onto the stack.
Execute the instruction at a designated address in program memory.
◼ Design of interrupt service routine:
Save and restore any registers it uses.
Re-enable interrupts before returning from interrupt.
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 24
0xFFFFFFFF
Memory Map Unmapped Area
0xC220FFFF
MicroBlaze
50MHz
MEMORY
DRAM
ADC subsystem
0xC2200000
Unmapped Area
0x8440FFFF
UART0
Debugger
UART1 0x84400000
Unmapped Area
0x8402FFFF
ADC
Subsystem
UARTs
0x84000000
Unmapped Area
0x83C0FFFF
TIMER Timer
0x83C00000
Unmapped Area
0x8180FFFF
Debugger
Interrupt controller
0x81800000
Interrupt
controller
Unmapped Area
0x0000FFFF
Memory for
Instructions and Data 0x0000004F
Reset, interrupt, … 0x00000000
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 25
Interrupts are Evil
In one or two respects modern machinery is basically more
difficult to handle than the old machinery. Firstly, we have got
the interrupts, occurring at unpredictable and irreproducible
moments; compared with the old sequential machine that
pretended to be a fully deterministic automaton, this has been a
dramatic change, and many a systems programmer’s grey hair
bears witness to the fact that we should not talk lightly about
the logical problems created by that feature.
(Dijkstra, “The humble programmer” 1972)
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 26
Timed Interrupt
Processor Setup
Reset timer
Timer
Register Interrupt Service Routine
When timer expires,
interrupt processor
Initialize Timer
Processor jumps to ISR
Execute Task Code Update Tick / Sample
Resumes
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 27
Concurrency
volatile uint timer_count;
void ISR(void) {
timer_count--;
} concurrent code:
logically runs at the
same time. In this case,
int main(void) { between any two
// initialization code machine instructions in
SysTickIntRegister(&ISR); main() an interrupt can
... // other init occur and the upper
timer_count = 2000; code can execute.
while(timer_count != 0) {
... code to run for 2 seconds
}
}
What could go wrong?
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 28
Concurrency
volatile uint timer_count;
void ISR(void) {
timer_count--;
}
int main(void) {
// initialization code
SysTickIntRegister(&ISR);
... // other init what if the interrupt
timer_count = 2000; occurs twice during
while(timer_count != 0) { the execution of this
... code to run for 2 seconds code?
}
}
What could go wrong?
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 29
What’s the difference between
Concurrency
and
Parallelism
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 30
Concurrency and Parallelism
◼ A program is said to be concurrent if different
parts of the program conceptually execute
simultaneously.
◼ A program is said to be parallel if different parts of
the program physically execute simultaneously on
distinct hardware.
◼
A parallel program is concurrent, but a concurrent
program need not be parallel.
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 31
Concurrency and Parallelism
Interrupt Handling
Reacting to external events (interrupts)
Exception handling (software interrupts)
Processes
Creating the illusion of simultaneously running
different programs (multitasking)
Threads
How is a thread different from a process?
Multiple processors (multi-cores)
◼ ...
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 32
Q&A
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 33
Example: Set up a timer on an ATmega168
to trigger an interrupt every 1ms
TCCR: Timer/Counter Control Register
OCR: output compare register
TIMSK: Timer Interrupt Mask
◼ The “prescaler” value divides the
system clock to drive the timer.
◼ Setting a non-zero bit in the timer
interrupt mask causes an interrupt to
occur when the timer resets.
Source: iRobot Command Module Reference Manual v6
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 34
Setting up the timer interrupt hardware in C
#include <avr/io.h>
int main (void) { memory-
TCCR1A = 0x00; mapped
TCCR1B = 0x0C; register.
OCR1A = 71; But how is this
TIMSK1 = 0x02; proper C code?
...
}
This code sets the hardware up
to trigger an interrupt every 1ms.
Source: ATmega168 Reference Manual
How do we handle the interrupt?
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 35
#define _MMIO_BYTE(mem_addr) (*(volatile uint8_t *)(mem_addr))
#define _SFR_IO8(io_addr) _MMIO_BYTE((io_addr) + 0x20)
#define _SFR_MEM8(mem_addr) _MMIO_BYTE(mem_addr)
#define _BV(bit) (1 << (bit)) //Enable interrupts (interrupt.h)
# define sei() __asm__ __volatile__ ("sei" ::)
//Disable interrupts (interrupt.h)
//Timer defines (iomx8.h) # define cli() __asm__ __volatile__ ("cli" ::)
#define TCCR1A _SFR_MEM8 (0x80) #define SIGNAL(signame) \
#define TCCR1B _SFR_MEM8 (0x81) void signame (void) __attribute__ ((signal)); \
/* TCCR1B */ void signame (void)
#define WGM12 3
#define CS12 2
void initialize(void) {
cli(); // Global variables
volatile uint16_t timer_cnt = 0;
// Set I/O pins volatile uint8_t timer_on = 0;
DDRB = 0x10;
PORTB = 0xCF; // Timer 1 interrupt to time delays in ms
……. SIGNAL(SIG_OUTPUT_COMPARE1A) {
if(timer_cnt) {
// Set up timer 1 to generate an interrupt every 1 ms timer_cnt--;
TCCR1A = 0x00; } else {
TCCR1B = (_BV(WGM12) | _BV(CS12)); timer_on = 0;
OCR1A = 71; }
TIMSK1 = _BV(OCIE1A); }
void delayMs(uint16_t time_ms) {
// Set up the serial port with rx interrupt
timer_on = 1;
…….
timer_cnt = time_ms;
while(timer_on) ;
// Turn on interrupts
}
sei();
}
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 36
#define _MMIO_BYTE(mem_addr) (*(volatile uint8_t *)(mem_addr))
#define _SFR_IO8(io_addr) _MMIO_BYTE((io_addr) + 0x20)
#define _SFR_MEM8(mem_addr) _MMIO_BYTE(mem_addr)
#define _BV(bit) (1 << (bit)) //Enable interrupts (interrupt.h)
# define sei() __asm__ __volatile__ ("sei" ::)
//Disable interrupts (interrupt.h)
//Timer defines (iomx8.h) # define cli() __asm__ __volatile__ ("cli" ::)
#define TCCR1A _SFR_MEM8 (0x80) #define SIGNAL(signame) \
#define TCCR1B _SFR_MEM8 (0x81) void signame (void) __attribute__ ((signal)); \
/* TCCR1B */ void signame (void)
#define WGM12 3
#define CS12 2
void initialize(void) {
cli(); // Global variables
volatile uint16_t timer_cnt = 0;
// Set I/O pins volatile uint8_t timer_on = 0;
DDRB = 0x10;
PORTB = 0xCF; // Timer 1 interrupt to time delays in ms
……. SIGNAL(SIG_OUTPUT_COMPARE1A) {
if(timer_cnt) {
// Set up timer 1 to generate an interrupt every 1 ms timer_cnt--;
TCCR1A = 0x00; } else {
TCCR1B = (_BV(WGM12) | _BV(CS12)); timer_on = 0;
OCR1A = 71; }
TIMSK1 = _BV(OCIE1A); }
void delayMs(uint16_t time_ms) {
// Set up the serial port with rx interrupt
timer_on = 1;
…….
timer_cnt = time_ms;
while(timer_on) ;
// Turn on interrupts
}
sei();
}
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 37
Setting up the timer interrupt hardware in C
#include <avr/io.h>
int main (void) {
TCCR1A = 0x00;
TCCR1B = 0x0C;
OCR1A = 71;
TIMSK1 = 0x02;
...
}
(*(volatile uint8_t *) (0x80)) = 0x00;
Source: ATmega168 Reference Manual
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 38
Example 2: Set up a timer on a Luminary
Micro board to trigger an interrupt every 1ms
◼ // Setup and enable SysTick with interrupt every
1ms
◼ void initTimer(void) {
◼ SysTickPeriodSet(SysCtlClockGet() / 1000);
Number of cycles per sec.
◼ SysTickEnable();
◼ SysTickIntEnable(); Start SysTick counter
◼}
Enable SysTick timer interrupt
◼ // Disable SysTick
◼ void disableTimer(void) {
◼ SysTickIntDisable();
◼ SysTickDisable();
◼}
Source: Stellaris Peripheral Driver Library User’s Guide
8/23/2023 Copyrights 2020 CE-UIT. All Rights Reserved. 39