Professional Documents
Culture Documents
EEE415 Week09 GPIO
EEE415 Week09 GPIO
415 Week 09
1
DRAFT 8/11/2023
• The Cortex-M family includes Cortex-MO, Cortex-MO+, Cortex-Ml, Cortex-M3, CortexM4, and Cortex-M7. The former
three are Von Neumann architecture, and the latter three are Harvard architecture. Moreover, Cortex-MO/MO+/Ml
are ARMv6-M, and CortexM3/M4/M7 are ARMv7-M.
• Cortex-M processors are backward compatible, and Figure 3-2 compares the instructions supported by each
processor group. For example, a binary program compiled for CortexM3 can run on Cortex-M4 without any
modification
EEE 415 - Department of EEE, BUET Dr. Sajid Muhaimin Choudhury 4
source: from referred textbook Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
2
DRAFT 8/11/2023
Computer Architectures
Von-Neumann Harvard
Instructions and data are stored in Data and instructions are stored into
the same memory. separate memories.
N.B. multi cycle processor we discussed earlier N.B. single cycle and pipelined processors we
was based on this architecture discussed earlier were based on this architecture
EEE 415 - Department of EEE, BUET Dr. Sajid Muhaimin Choudhury 5 5
source: from slides prepared by Dr. Yifeng Zhu Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
3
DRAFT 8/11/2023
Arm Cortex M4
4
DRAFT 8/11/2023
An in-circuit debugger is a
hardware device, connected
between a PC and the target
microcontroller test system,
and is used to debug real-
time applications.
10
5
DRAFT 8/11/2023
EEE 415 - Department of EEE, BUET from st.com Dr. Sajid Muhaimin Choudhury 11 11
source: from slides prepared by Dr. Yifeng Zhu Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
11
12
6
DRAFT 8/11/2023
Word of Caution
• For the theory course, we will just talk about STM32L4
architecture
13
14
7
DRAFT 8/11/2023
STM32L4
• An ARM Cortex-M processor chip consists of a Cortex-M core licensed by ARM, on-chip peripheral devices
implemented by chip manufacturers, and buses and bridges for the communication between the core and
peripheral devices.
15
• Fundamental components of a Cortex-M processor core include the arithmetic logic unit
(ALU), the processor control unit, the interrupt controller (NVIC), the instruction fetching
and decoding unit, and the interfaces for memory and debug.
• The interrupt controller (NVIC) allows the processor core to stop the execution of
the current task and immediately respond to special events or signals generated by
software or by peripheral devices.
• The instruction fetching and decoding unit reads one machine instruction from the
instruction memory address pointed by the program counter and decodes the
instruction to figure out what operations the processor core should perform.
• The processor control unit then generates corresponding control signals based on
the decoding result.
• The memory interface supports the access to memory devices (SRAM, flash).
• The debug interface allows a programmer to use a host computer to start or stop a
software program on a Cortex-M processor, and monitor or modify processor
registers, peripheral registers, and memory in real-time.
• Cortex-M4 supports digital signal processing (DSP) and can optionally have a single-
precision floating processing unit (FPU).
• Cortex-MO/MO+/Ml/M3 has no support to DSP and FPU. Compared with Cortex-M4, the
optional FPU on CortexM7 can support both single-precision and double-precision
operations.
Dr. Sajid Muhaimin Choudhury 16
EEE 415 - Department of EEE, BUET
source: from referred textbook Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
16
8
DRAFT 8/11/2023
17
• The core processor communicates with the flash memory (typically used as instruction memory), SRAM
(generally used as data memory), Direct Memory Access (DMA) controller, and general-purpose input/output
(GPIO) ports via a bus matrix (also called crossbar switch)
• Peripheral devices are connected to the bus matrix via the bus bridges that links the advanced high-performance
bus (AHB) and the advanced peripheral bus (APB) Dr. Sajid Muhaimin Choudhury 18
EEE 415 - Department of EEE, BUET
source: from referred textbook Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
18
9
DRAFT 8/11/2023
• Each GPIO pin has multiple functions usually. Software can change its function, even at runtime.
• We can use a GPIO pin simply for digital input or digital output, or we can use it for more advanced functions
such as analog-to-digital conversion (ADC), serial communication, timer functions, and so on.
• Different SoC chips may have different GPIO functions, depending on the chip manufacturers
Dr. Sajid Muhaimin Choudhury 19
EEE 415 - Department of EEE, BUET
source: from referred textbook Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
19
General Purpose
Input/Output (GPIO) of
ARM Microcontroller
20
10
DRAFT 8/11/2023
21
Functionality of GPIO
• A GPIO port consists of a group of GPIO pins, typically 8 or 16, which share the
same data and control registers.
• All GPIO pins in a GPIO port can be configured as input or output independently
• When a GPIO pin i is set as a digital input, the binary data read from this pin of
this GPIO group is saved at bit i in the input data register (IDR). Each bit in IDR
holds the digital input of the corresponding pin.
• When a GPIO pin i is configured as a digital output, bit i in the output data register
(ODR) holds the output of this pin. Therefore, when changing the output of a
GPIO pin, the programmer should only alter the value of the corresponding bit of
ODR, without affecting the other bits in ODR.
22
11
DRAFT 8/11/2023
23
24
12
DRAFT 8/11/2023
25
1 GB External Device
0x60000000
0xA0000000
…
1 GB External RAM
0x48001000
GPIO D (1 KB)
0x60000000 0x48000C00
because 1024 bytes= 400 in
0.5 GB Peripheral GPIO C (1 KB) hexa, each byte in each memory
0x40000000 0x48000800 address
GPIO B (1 KB)
0.5 GB SRAM 0x48000400
0x20000000 GPIO A (1 KB)
0.5 GB Code 0x48000000
0x40000000
…
0x00000000
26
13
DRAFT 8/11/2023
27
• 8 GPIO Ports:
A, B, C, D, E, F, G, H
28
14
DRAFT 8/11/2023
0x4800002C ASCR
0x48000028 BRR
0x48000024 AFR[1]
0x48000020 AFR[0]
0x48000400 0x4800001C LCKR
GPIO A (1 KB) 0x48000018 BSRR
0x48000000 48 bytes
0x48000014 ODR
0x48000010 IDR
0x4800000C PUPDR
0x48000008 OSPEEDR
0x48000004 OTYPER
0x48000000 MODER
• A peripheral register usually takes four bytes in memory. For example, the output
data register (ODR) of Port B on STM32L4 is mapped to memory addresses Each register has 4 bytes.
0x48000414 to 0x48000417, with the upper half-word being reserved. because after 16 bits are used by
EEE 415 - Department of EEE, BUET Dr.bits
16 registers, there remain 16 left Muhaimin Choudhury 29
Sajid
source: from referred textbook Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
29
0x48000017
ODR 1 word (i.e. 32 bits)
0x48000014
0x48000017
0x48000016
4 bytes
0x48000015
0x48000014
Little Endian
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Note that values stored in peripheral registers are in the format of little-endian
30
15
DRAFT 8/11/2023
31
0x48000017
0x48000016
4 bytes
0x48000015
0x48000014
Little Endian
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1
32
16
DRAFT 8/11/2023
33
34
17
DRAFT 8/11/2023
35
36
18
DRAFT 8/11/2023
37
38
19
DRAFT 8/11/2023
0x60000000
…
0x48001000
GPIO D (1 KB)
0x48000C00
GPIO C (1 KB)
0x48000800
GPIO B (1 KB)
0x48000400
GPIO A (1 KB)
0x48000000
0x40000000
…
39
D Bus
FLASH
ARM Cortex-M4 I Bus
AHB Bus
Matrix
SRAM
FPU S Bus
GPIO A
GPIO B APB Bus
GPIO C Matrix
GPIO D
GPIO E UART
GPIO F
GPIO G SPI
GPIO H
Timer
…
STM32L4
40
20
DRAFT 8/11/2023
41
a |= (1 << k)
or
a = a | (1 << k)
Example: k = 5
a a7 a6 a5 a4 a3 a2 a1 a0
1 << k 0 0 1 0 0 0 0 0
a | (1 << k) a7 a6 1 a4 a3 a2 a1 a0
42
21
DRAFT 8/11/2023
a |= (3 << k)
or
a = a | (3 << k)
Example: k = 5
a a7 a6 a5 a4 a3 a2 a1 a0
1 << k 0 1 1 0 0 0 0 0
a | (1 << k) a7 1 1 a4 a3 a2 a1 a0
43
a |= (7 << k)
or
a = a | (7 << k)
Example: k = 4
a a7 a6 a5 a4 a3 a2 a1 a0
1 << k 0 1 1 1 0 0 0 0
a | (1 << k) a7 1 1 1 a3 a2 a1 a0
44
22
DRAFT 8/11/2023
a &= ~(1<<k)
Example: k = 5
a a7 a6 a5 a4 a3 a2 a1 a0
~(1 << k) 1 1 0 1 1 1 1 1
a & ~(1<<k) a7 a6 0 a4 a3 a2 a1 a0
45
a &= ~(3<<k)
Example: k = 5
a a7 a6 a5 a4 a3 a2 a1 a0
~(1 << k) 1 0 0 1 1 1 1 1
a & ~(1<<k) a7 0 0 a4 a3 a2 a1 a0
46
23
DRAFT 8/11/2023
a &= ~(7<<k)
Example: k = 4
a a7 a6 a5 a4 a3 a2 a1 a0
~(1 << k) 1 0 0 0 1 1 1 1
a & ~(1<<k) a7 0 0 0 a3 a2 a1 a0
47
a ^= 1<<k
Example: k = 5
a a7 a6 a5 a4 a3 a2 a1 a0
1 << k 0 0 1 0 0 0 0 0
a ^ (1<<k) a7 a6 NOT(a5) a4 a3 a2 a1 a0
m n m⊕n
Truth table of 0 0 0
Exclusive OR 0 1 1
1 0 1
1 1 0
Dr. Sajid Muhaimin Choudhury 48
EEE 415 - Department of EEE, BUET Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
48
24
DRAFT 8/11/2023
a ^= 7<<k
Example: k = 4
a a7 a6 a5 a4 a3 a2 a1 a0
1 << k 0 0 1 1 1 0 0 0
a ^ (1<<k) a7 a6 NOT(a5) NOT(a5) NOT(a5) a2 a1 a0
m n m⊕n
Truth table of 0 0 0
Exclusive OR 0 1 1
1 0 1
1 1 0
Dr. Sajid Muhaimin Choudhury 49
EEE 415 - Department of EEE, BUET Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
49
50
25
DRAFT 8/11/2023
Schmitt
trigger
51
Output
Data Schmitt
trigger
Register GPIO Output Type Register (OTYPER)
0 = Output push-pull (default)
1 = Output open-drain
52
26
DRAFT 8/11/2023
Flowchart for
GPIO Initialization Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Step 6:
53
54
27
DRAFT 8/11/2023
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
55
56
28
DRAFT 8/11/2023
57
• Output Speed:
• Speed of rising and falling Speed of Rising
• Four speeds: Low, Medium,
Low
Fast, High
• Tradeoff
• Higher GPIO speed increases Medium
EMI noise and power
consumption
Fast
• Configure based on peripheral
speed
– Low speed for toggling LEDs High
– High speed for SPI
58
29
DRAFT 8/11/2023
GPIO Output = 1
Source current to external circuit
59
GPIO Output = 0
Drain current from external circuit
60
30
DRAFT 8/11/2023
GPIO
Output Pin
GPIO
Output Bit
D
G
0/1 Controller NMOS
S
GPIO Output = 0
Drain current from external circuit
61
GPIO
Output Pin GPIO
Output Pin
Output is
GPIO GPIO Floating
Output Bit Output Bit
D 0
G
0/1 Controller NMOS 1 Controller NMOS
Output = 1
GPIO Pin has high-impedance to external circuit
62
31
DRAFT 8/11/2023
Slew Rate
Slew Rate
Maximum rate of change of the
output voltage
∆𝑉
𝑆𝑙𝑒𝑤 𝑅𝑎𝑡𝑒 = 𝑚𝑎𝑥
∆𝑡
63
64
32
DRAFT 8/11/2023
Pin 2
65
66
33
DRAFT 8/11/2023
Input Data
Register (IDR)
Input is sampled into IDR
every AHB clock cycle!
GPIO Pull-up/Pull-down Register (PUPDR)
00 = No pull-up, pull-down 01 = Pull-up
10 = Pull-down 11 = Reserved
67
Schmitt Trigger
V in
Threshold
Analog signals
• Noisy
• Rise and fall slowly (small slew rate)
68
34
DRAFT 8/11/2023
Schmitt Trigger
V in
Threshold
69
Schmitt Trigger
Vout Vin
VTH
Vout
VTL
V in
Vout
VDD
V in
Vout
VDD
V in
70
35
DRAFT 8/11/2023
Flowchart for
GPIO Initialization Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Step 6:
71
72
36
DRAFT 8/11/2023
73
Pull-Up Pull-Down
If external input is HiZ, the If external input is HiZ, the
input is read as a valid HIGH. input is read as a valid LOW.
Dr. Sajid Muhaimin Choudhury 74
EEE 415 - Department of EEE, BUET Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
74
37
DRAFT 8/11/2023
// No pull-up, pull-down
GPIOA->PUPDR &= ~3UL;
75
or
76
38
DRAFT 8/11/2023
// Read pin 0
input = (GPIOA->IDR & 1UL);
if (input == 0) {
// Center of joystick is not pressed
...
} else {
// Center of joystick is pressed
...
}
Dr. Sajid Muhaimin Choudhury 77
EEE 415 - Department of EEE, BUET Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
77
I/O Debouncing
• Example signal when a button is pressed
3
2.5
Voltage across push button
1.5
0.5
-0.5
-0.05 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4
Time (microseconds)
78
39
DRAFT 8/11/2023
De-Bouncing
• Hardware De-bouncers
• Simple RC circuit as a low-pass filter
• Software De-bouncing
• Solution A: Read the switch after a sufficient delay to allow the
bounces to settle down
• Solution B: Reading periodically and use a counter as a filter
– Reset the counter when the signal is “unpressed”
– Counts up if “pressed”
– If counter > threshold, the contacts has stopped bouncing
79
Practice Problems
aC
aC
3. Write a C program that would light up an LED when a button is pressed in the STM32 board, and
turn off the LED when the button is released
80
40