Serial Communication
Serial Communication
I/O:
Serial Communication
Protocols
Serial Communication
• Transfer a single bit each time
• UART
• I2C
• SPI
1
2/6/25
UART
UART: Connections
• Data transmitted from Tx pin and received at Rx pin in the receiver
• The protocol is point-to-point
- No addressing is needed
2
2/6/25
UART: Connections
UART: Frame
• Data to be transmitted is divided into frames
• Frame:
- Start bit: Represented by a low-level voltage
- Data length (7, 8 or 9 bits)
- Parity bit (even, odd, none):
• High-level voltage - logic 0/low-level voltage – logic 1
- Stop bit(s) (0.5, 1, 1.5, or 2 bits):
• Represented by a high voltage
• The voltage of the transmission line remains high if no further data is transmitted
3
2/6/25
UART
• E.g., 8-N-1. Transmission of two data bytes: 0x32 and 0x3C. LSB is
transmitted first
4
2/6/25
- Transmission rate of data is not 9600 bits per second / - 1200 bytes
- Since each frame has 10 bits (Start + 8D + Stop), 960 frames will be
transferred per second
- Since each frame has 1 byte, 960 bytes will be transferred per second
COEN 421/6341: Embedded Systems Design 10
10
5
2/6/25
UART Communication
• Check pinout of the MCU to see how the connection will be made
between the UART ports of the communicating entities
- Dev. 1 Tx connects with Dev. 2 Rx
- Dev. 1 Rx connects with Dev. 2 Rx
- GND to GND
• Check the pin functions
• Example:
11
UART Communication
12
6
2/6/25
Registers
• GPIO port mode register (GPIOx_MODER) (x =A to I) – AO: 0x00
• GPIO port output type register (GPIOx_OTYPER) (x = A to I) - AO: 0x04
• GPIO port output speed register (GPIOx_OSPEEDR) (x = A to I) - AO: 0x08
• GPIO port pull-up/pull-down register (GPIOx_PUPDR) (x = A to I) - AO: 0x0C
• GPIO port input data register (GPIOx_IDR) - AO: 0x10
• GPIO port output data register (GPIOx_ODR) (x = A to I) - AO: 0x14
• GPIO port bit set/reset register (GPIOx_BSRR) (x = A to I) - AO: 0x18
• GPIO port configuration lock register (GPIOx_LCKR) (x = A to I) – AO: 0x1C
• GPIO alternate function low register (GPIOx_AFRL) (x = A to I) – AO: 0x20
• GPIO alternate function high register (GPIOx_AFRH) (x = A to I) – AO: 0x24
• GPIO port bit reset register (GPIOx_BRR) (x = A to I) – AO: 0x24
• GPIO port analog switch control register (GPIOx_ASCR)(x = A to H(a)) – AO: 0x2C
13
Registers Map
Not all of them use the 32 bits
14
7
2/6/25
Registers Map
15
UART
1. Define the pointer for the port you will use
- Port A
#define GPIOA ((GPIO_TypeDef *) 0x48000000
16
8
2/6/25
UART
2. Clear MODE register for the pins to be used and set the alternate
function mode in the MODE register for the pins to be used
GPIOA->MODER &= ~(0b11 << (2*pin)); //zero the bits for the pin ``pin’’
GPIOA->MODER |= 0b10 << (2*pin); //10 -> alternate function
17
UART 2
4
GPIOA->AFR[1] |= 1UL << 4; 3
18
9
2/6/25
UART
19
UART
5. Find the memory base address for the UART port to be used
20
10
2/6/25
UART
6. UART/USART registers
- Data frame 7, 8, or 9 bits
21
UART
6. UART/USART registers
- Data frame 7, 8, or 9 bits
- USART_CR1 (address offset: 0x00)
22
11
2/6/25
UART
23
UART
24
12
2/6/25
UART
25
UART
6. UART/USART registers
- Data frame 7, 8, or 9 bits
- USART_CR1 (address offset: 0x00)
- Set data length (8 bits in this case)
- Select the stop bit configuration (1 in this case)
- Set parity control (no parity in this case)
UARTx->CR1 &= ~USART_CR1_PCE;
26
13
2/6/25
UART
6. UART/USART registers
- Data frame 7, 8, or 9 bits
- USART_CR1 (address offset: 0x00)
- Set data length (8 bits in this case)
- Select the stop bit configuration (1 in this case)
- Set parity control (no parity in this case)
- Set oversampling
UARTx->CR1 &= ~USART_CR1_OVER8;
27
UART
6. UART/USART registers
- Data frame 7, 8, or 9 bits
- USART_CR1 (address offset: 0x00)
- Set data length (8 bits in this case)
- Select the stop bit configuration (1 in this case)
- Set parity control (no parity in this case)
- Set oversampling
- Set baud rate BRR register stores the USARTDIV
UARTx->BRR = 0x208D; //For a targeted 9600 baud rate when clock is 80MHz
COEN 421/6341: Embedded Systems Design 28
28
14
2/6/25
UART
29
UART
6. UART/USART registers
- Data frame 7, 8, or 9 bits
- USART_CR1 (address offset: 0x00)
- Set data length (8 bits in this case)
- Select the stop bit configuration (1 in this case)
- Set parity control (no parity in this case)
- Set oversampling
- Enable transmission and reception
30
15
2/6/25
UART
6. UART/USART registers
- Data frame 7, 8, or 9 bits
- USART_CR1 (address offset: 0x00)
- Set data length (8 bits in this case)
- Select the stop bit configuration (1 in this case)
- Set parity control (no parity in this case)
- Set oversampling
- Enable transmission and reception
- Enable USART
USARTx->CR1 |= USART_CR1_UE;
31
UART
6. UART/USART registers
- Data frame 7, 8, or 9 bits
- USART_CR1 (address offset: 0x00)
- Set data length (8 bits in this case)
- Select the stop bit configuration (1 in this case)
- Set parity control (no parity in this case)
- Set oversampling
- Enable transmission and reception
- Enable USART
- Verify if the configured port is ready for transmission and reception
32
16
2/6/25
UART
6. Read function
33
UART
6. Write function
34
17
2/6/25
UART
35
36
18
2/6/25
37
38
19
2/6/25
39
40
20
2/6/25
41
42
21
2/6/25
43
44
22
2/6/25
45
46
23
2/6/25
47
48
24
2/6/25
• The slave whose address matches the address sent by the master will answer with
an ACK bit
• After the slave acknowledges the addressing successfully, data transfer takes place
in the direction specified by the R/W bit
• Data are transferred byte by byte
• Each byte is followed by an ACK or NACK bit
• It takes 9 cycles to transfer one byte
• The master completes the communication by sending a STOP bit to the slave
COEN 421/6341: Embedded Systems Design 49
49
50
25
2/6/25
(1)
• After the master sends out the first byte, all slave devices compare it to their own address. It is possible that
more than one device finds a match between the two leading bits of the 10-bit address. Therefore, multiple
ACK bits might be generated during the A l clock period
• After the master sends out the lower 8 bits of the slave address, at most one device finds an address match,
and thus no multiple ACK bits are generated during the A2 clock period
After sending two address bytes (R/W = 0), the master repeats the start bit and
sends again the first address byte, with R/W being 1 to indicate reading
COEN 421/6341: Embedded Systems Design 51
51
52
26
2/6/25
53
54
27
2/6/25
55
56
28
2/6/25
57
58
29
2/6/25
59
60
30
2/6/25
61
62
31
2/6/25
63
64
32
2/6/25
• Data is sent and received based on the clock provided by the master
• One device writes a bit to the data line at the rising or falling edge of the clock,
the other device then reads the bit at the opposite edge of the same clock period
• Master can change the clock speed by programming the clock prescaler register
65
66
33
2/6/25
67
68
34
2/6/25
• https://www.parlezvoustech.com/en/comparaison-protocoles-
communication-i2c-spi-uart/
69
35