Proc Embarqué - Ch5
Proc Embarqué - Ch5
1
Chapter Syllabus
▪ GPIO
▪ Basic Concepts & Port Circuitry
▪ Driver Layer
▪ Circuit Interfacing
▪ Serial Communication
▪ Serial Communication Definition
▪ Software Structure for Handling Asynchronous Communication
▪ UART Communication
▪ Serial Communication Protocols Comparison
2
GPIO BASIC CONCEPTS
& PORT CIRCUITRY
3
GPIO Basic Concepts
4
GPIO Alternative Functions
▪ Advantages:
▪ Saves space on the package
▪ Improves flexibility
5
Pull-Up & Pull-Down Resistors
Pull-down Pull-up
▪ Ensure a known value on the output if a pin is left
floating
6
GPIO DRIVER LAYER
7
Code Structure
▪ Main code talks to the drivers, producing easy to read and
understand code High Level main.c
▪ gpio_set_mode(P2_5, Output)
drivers
▪ Drivers utilise CMSIS library and group relevant actions
▪ port_struct->direction_reg = output
CMSIS
▪ CMSIS transforms memory mapped registers into C structs
▪ #define PORT0 ((struct PORT*)0x2000030)
Registers
▪ Registers directly control hardware Low Level
Input Pin
Output Hardware
direction_reg
8
GPIO Drivers Layer: How It Works
9
GPIO Drivers Layer: How It Works
10
C Interface: GPIO Configuration
11
C Interface: GPIO Reading and Writing
12
Pseudocode for Program
13
C Code
while (1) {
if (gpio_get(P_SW)) {
// Switch is not pressed (active low), turn LED1 off and LED2 on.
gpio_set(P_LED1, 0);
gpio_set(P_LED2, 1);
} else {
// Switch is pressed, turn LED2 off and LED1 on.
gpio_set(P_LED2, 0);
gpio_set(P_LED1, 1);
}
}
14
Inputs and Outputs, Ones and Zeros,Voltages and Currents
INTERFACING
15
Inputs: What’s a One? A Zero?
16
Outputs: What’s a One? A Zero?
Vout
(18 mA for high-drive pads) and VDD > 2.7 V
Logic 0 out
Iout
17
Output Example: Driving LEDs
18
Output Example: Driving a Speaker
void beep(void) {
unsigned int period = 20;
while (1) {
gpio_toggle(P_SPEAKER);
delay_ms(period/2);
}
}
19
Serial Communications
20
SERIAL COMMUNICATION
DEFINITION
21
Why Communicate Serially?
▪ Often it’s not feasible to support sending all the word’s bits at the same time
▪ Cost and weight: more wires needed, larger connectors needed
▪ Mechanical reliability: more wires => more connector contacts to fail
▪ Timing Complexity: some bits may arrive later than others due to variations in capacitance and
resistance across conductors
▪ Circuit complexity and power: may not want to have 16 different radio transmitters + receivers in the
system
22
Synchronous Serial Data Transmission
Parallel Data In
D3 D2 D1 D0
Serial Serial
D Q D Q D Q D Q Data In D Q D Q D Q D Q
Data Out
Clk Clk
D3 D2 D1 D0
Parallel Data Out
▪ Use shift registers and a clock signal to convert between serial and parallel formats
▪ Synchronous: an explicit clock signal is along with the data signal
23
Synchronous Serial Data Bus Configurations
▪ Use of two serial data lines - one for reading, one for writing. Allows simultaneous send and
receive full-duplex communication
▪ Share the serial data line. Doesn’t allow simultaneous send and receive
24
Asynchronous Serial Communication
Data bits
Start bit Stop bit
Time Zero
Tbit*1.5
Tbit*2.5
Tbit*3.5
Tbit*4.5
Tbit*5.5
Tbit*6.5
Tbit*7.5
Tbit*8.5
Tbit*9.5
Data Sampling
Time at Receiver
▪ Single parity bit detects if bits are corrupted, but doesn’t detect an even number of
corrupted bits
▪ Stronger error detection codes (e.g. Cyclic Redundancy Check) exist and use multiple bits
(e.g. 8, 16), and can detect many more corruptions.
▪ Used for CAN, USB, Ethernet, Bluetooth, etc.
27
SOFTWARE STRUCTURE – HANDLING
ASYNCHRONOUS COMMUNICATION
28
Software Structure
▪ Options
▪ Polling
◦ Wait until data is available
◦ Simple but inefficient of processor time
▪ Interrupt
◦ CPU interrupts program when data is available
◦ Efficient, but more complex
29
Serial Communications and Interrupts
Main Program or
other threads
▪ Want to provide multiple threads of control in the
program
send_string get_string
▪ Main program (and subroutines it calls)
▪ Transmit ISR – executes when serial interface is ready to
send another character
▪ Receive ISR – executes when serial interface receives a
character
▪ Error ISR(s) – execute if an error occurs
tx_isr rx_isr
30
Code to Implement Queues
older newer
data
▪ Enqueue at tail: tail is the index of the next free entry data
▪ Dequeue from head: head is the index of the item to remove
▪ Queue size is initialised and stored in size
▪ One queue per direction read data write data
▪ tx ISR unloads tx_q from head to tail
▪ rx ISR loads rx_q
▪ Other threads (e.g. main) load tx_q and unload rx_q send_string get_string
31
ASYNCHRONOUS SERIAL (UART)
COMMUNICATIONS
32
Transmitter Basics
Data
bits
Data Sampling
Time Zero
Time at Receiver
Tbit
Tbit
Tbit
Tbit
Tbit
Tbit
Tbit
Tbit
Tbit
Tbit
Tbit
▪ If no data to send, keep sending 1 (stop bit) – idle line
▪ When there is a data word to send
▪ Send a 0 (start bit) to indicate the start of a word
▪ Send each data bit in the word (use a shift register for the transmit buffer)
▪ Send a 1 (stop bit) to indicate the end of the word
33
Receiver Basics
Data
bits
Data Sampling
Zero
Time
Tbit*10.5
Tbit*1.5
Time at
Tbit*2.5
Tbit*3.5
Tbit*4.5
Tbit*5.5
Tbit*6.5
Tbit*7.5
Tbit*8.5
Tbit*9.5
Receiver
35
Input Data Oversampling
36
Baud Rate
▪ Need to divide high frequency clock down to desired baud rate * oversampling factor
▪ Example
▪ 24 MHz -> 4800 baud with 16x oversampling
▪ Division factor = 24E6/(4800*16) = 312.5. Must round to closest integer value ( 312 or 313), will
have a slight frequency error.
37
Software for Polled Serial Comm.
void test_polled() {
uart_init(9600);
uart_enable();
while(1) {
uart_tx(uart_rx()); // echos the received character back
}
}
38
SPI COMMUNICATIONS
39
SPI Communication
▪ All chips share bus signals
▪ Clock SCK
▪ Data lines MOSI (master out, slave in) and MISO
(master in, slave out)
▪ Each peripheral has its own chip select line (CS)
▪ Master (MCU) asserts the CS line of only the
peripheral it’s communicating with
▪ Use shift registers and a clock signal to convert between serial and parallel formats
▪ Synchronous: an explicit clock signal is along with the data signal
40
SPI Example: Secure Digital Card Access
CMD / CLK /
GND VDD
▪ SD cards have two communication modes DAT3 / DI SCLK GND DAT0/
▪ Native 4-bit DAT2 / CS DO
▪ Legacy SPI 1-bit X DAT1/
X
▪ VDD from 2.7 to 3.6 V
▪ CS: Chip Select (active low)
41
I2C COMMUNICATIONS
42
I2C Communication Architecture
43
I2C Addressing
44
SERIAL COMMUNICATION PROTOCOLS
COMPARISON
45
Factors to Consider
▪ How fast can the data get through?
▪ Depends on raw bit rate, protocol overhead in packet
UART Fast – Tens of Mbit/s 2*N (TxD, RxD) None Point-to-point full
(Point to Point) duplex
SPI Fast – Tens of Mbit/s 3+N for SCLK, MOSI, Hardware chip Multi-point full-
MISO, and one SS per select signal per duplex, multi-drop
device device half-duplex buses
I2C Moderate – 100 kbit/s, 400 kbit/s, 1 2 (SCL, SDA) In packet Multi-point half-
Mbit/s, 3.4 Mbit/s. Packet overhead. duplex bus
47