Lecture07_GeneralPurposeDigitalInterfacing
Lecture07_GeneralPurposeDigitalInterfacing
© 2021 Arm
Learning Objectives
At the end of this lecture, you should be able to:
• Explain the concept of GPIO alternative functions and outline its advantages.
• Explain the functions and relevance of pull-up and pull-down resistors used in IO pins.
• Describe a simple input synchronisation circuitry consisting of two D-flipflops and
explain its importance.
• Describe the GPIO code structure and outline its layers.
• Write C program to set the GPIO mode and turn on the on-board LED.
2 © 2021 Arm
Overview
• How do we make a program light up LEDs in response to a switch?
• GPIO
• Basic Concepts
• Port Circuitry
• Alternate Functions
• Peripheral Access In C
• Circuit Interfacing
• Inputs
• Outputs
• Additional Port Configuration
3 © 2021 Arm
Basic Concepts
4 © 2021 Arm
GPIO Alternative Functions
• Pins may have different features
• Advantages:
• Saves space on the package
• Improves flexibility
5 © 2021 Arm
Pull-Up & Pull-Down Resistors
• Ensure a known value on the output if a pin is left
floating
• In our example, we want the switch SW1 to pull the Pull-down Pull-up
pin to ground, so we enable the pull-up
6 © 2021 Arm
Input Synchronization
• External signals are asynchronous to internal
clock
• Data
• Output (different ways to access it)
• Input
8 © 2021 Arm
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 utilize 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
• Hardware drives I/O pins physically
9 © 2021 Arm
Drivers Layer: How It Works
void gpio_set(Pin pin, int value)
1) mask = 1 << pin index
2) tmp = port_struct->data_reg & ~mask
3) tmp |= value << pin index
4) port_struct->data_reg = tmp
10 © 2021 Arm
Drivers Layer: How It Works
int gpio_get(Pin pin)
1) mask = 1 << pin index
2) tmp = port_struct->data_reg & mask
3) tmp >>= pin index
4) return tmp
11 © 2021 Arm
C Interface: GPIO Configuration
/*! This enum describes the directional setup of a GPIO pin. */
typedef enum {
Reset, //!< Resets the pin-mode to the default value.
Input, //!< Sets the pin as an input with no pull-up or pull-down.
Output, //!< Sets the pin as a low impedance output.
PullUp, //!< Enables the internal pull-up resistor and sets as input.
PullDown //!< Enables the internal pull-down resistor and sets as input.
} PinMode;
13 © 2021 Arm
Pseudocode for Program
Make LED1 and LED2 outputs
Make switch an input with a pull-up resistor
do forever {
if switch is not pressed {
Turn off LED1
Turn on LED2
} else {
Turn off LED2
Turn on LED1
}
}
14 © 2021 Arm
C Code
gpio_set_mode(P_LED1, Output); // Set LED pins to outputs
gpio_set_mode(P_LED2, Output);
gpio_set_mode(P_SW, Pullup); // Switch pin to resistive pull-up
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 off LED2 and LED1 on.
gpio_set(P_LED2, 0);
gpio_set(P_LED1, 1);
}
}
15 © 2021 Arm
Interfacing
© 2021 Arm
Inputs: What’s a One? A Zero?
• Input signal’s value is determined by voltage
17 © 2021 Arm
Outputs: What’s a One? A Zero?
• Nominal output voltages
• 1: VDD-0.5 V to VDD
• 0: 0 V to 0.5 V
Vout
Logic 0 out
Iout
18 © 2021 Arm
Output Example: Driving LEDs
• Need to limit current to a value which is safe for
both LED and MCU port driver
• Use current-limiting resistor
• R = (VDD – VLED)/ILED
• Set ILED = 4mA
• VLED depends on type of LED (mainly color)
• Red: ~1.8V
• Blue: ~2.7V
• Solve for R given VDD = ~3.0V
• Red: 300W
• Blue: 75W
• Demonstration code in Basic Light Switching Example
19 © 2021 Arm
Output Example: Driving a Speaker
• Create a square wave with a GPIO output
• Use capacitor to block DC value
• Use resistor to reduce volume if needed
void beep(void) {
unsigned int period = 20;
while (1) {
gpio_toggle(P_SPEAKER);
delay_ms(period/2);
}
}
20 © 2021 Arm
The Arm trademarks featured in this presentation are registered
trademarks or trademarks of Arm Limited (or its subsidiaries) in
the US and/or elsewhere. All rights reserved. All other marks
featured may be trademarks of their respective owners.
www.arm.com/company/policies/trademarks
© 2021 Arm