0% found this document useful (0 votes)
3 views

Lecture07_GeneralPurposeDigitalInterfacing

This document provides an overview of General Purpose Input/Output (GPIO) concepts, including alternative functions, pull-up and pull-down resistors, and input synchronization. It details the GPIO code structure, including the drivers and C interface for configuring GPIO pins, as well as examples of interfacing with LEDs and switches. The document also includes pseudocode and C code for implementing a simple LED control based on switch input.

Uploaded by

faris.banihani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture07_GeneralPurposeDigitalInterfacing

This document provides an overview of General Purpose Input/Output (GPIO) concepts, including alternative functions, pull-up and pull-down resistors, and input synchronization. It details the GPIO code structure, including the drivers and C interface for configuring GPIO pins, as well as examples of interfacing with LEDs and switches. The document also includes pseudocode and C code for implementing a simple LED control based on switch input.

Uploaded by

faris.banihani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

General Purpose I/O

© 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

• Goal: light either LED1 or LED2 based on switch SW1 position


• GPIO = General-purpose input and output (digital)
• Input: program can determine if input signal is a 1 or a 0
• Output: program can set output to 1 or 0

• Can use this to interface with external devices


• Input: switch
• Output: LEDs

4 © 2021 Arm
GPIO Alternative Functions
• Pins may have different features

• To enable an alternative function, set up the


appropriate register Memory Mapped I/O
Alternate Function 1
Alternate Function 2 Pin

• May also have analogue paths for ADC / DAC Alternate Function n
etc.
Function Select

• 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

• The pin value is:


• High when SW1 is not pressed
• Low when SW1 is pressed

6 © 2021 Arm
Input Synchronization
• External signals are asynchronous to internal
clock

• If an external signal changes at the same time Pin D Q D Q Input


as the clock, a flip-flop can enter a metastable
state indefinitely
CLK

• Solution – synchronize the input signals with


the clock

• This is done for us by hardware, no need to


worry!
7 © 2021 Arm
GPIO Port Bit Circuitry in MCU
• Control
• Direction
• MUX

• 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

• e.g. gpio_set(P2_5, 1) with PORT_DATA_REGISTER = 0b01010101


1. Create a mask for the bit we want to set (0b00100000)
2. Invert the mask (0b11011111) to select all the other bits in the port data register, and save the status of the
other bits (tmp = 0b01010101)
3. Move the new value of the bit into position, and or it with the new register value (tmp = 0b01110101)
4. Write the new data register value out to the port (PORT_DATA_REGISTER = 0b01110101)

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

• e.g. gpio_get(P2_5) with PORT_DATA_REGISTER = 0b01110101


1. Create a mask for the bit we want to get (0b00100000)
2. Select the bit in the port data register based on the mask (tmp = 0b00100000)
3. Bitshift the value to produce a one or zero (tmp = 0b00000001)
4. Return the value of the pin back to the user

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;

/*! \brief Configures the output mode of a GPIO pin.


* Used to set the GPIO as an input, output, and configure the
* possible pull-up or pull-down resistors.
* \param pin Pin to set.
* \param mode New output mode of the pin.
*/
void gpio_set_mode(Pin pin, PinMode mode);
12 © 2021 Arm
C Interface: Reading and Writing
/*! \brief Sets a pin to the specified logic level.
* \param pin Pin to set.
* \param value New logic level of the pin (0 is low, otherwise high).
*/
void gpio_set(Pin pin, int value);

/*! \brief Get the current logic level of a GPIO pin.


* \param pin Pin to read.
* \return The logic level of the GPIO pin (0 if low, 1 if high).
*/
int gpio_get(Pin pin);

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

Inputs and Outputs, Ones and Zeros, Voltages and


Currents

© 2021 Arm
Inputs: What’s a One? A Zero?
• Input signal’s value is determined by voltage

• Input threshold voltages depend on supply voltage VDD

• Exceeding VDD or GND may damage chip

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

• Note: Output voltage depends on current


drawn by load on pin
• Need to consider source-to-drain resistance in the
transistor Logic 1 out
• Above values only specified when current < 5mA (18
mA for high-drive pads) and VDD > 2.7 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

You might also like