CPSC 359
Raspberry PI GPIO

PhD Student
Fall 2018
Today
What is GPIO?
Pins on your Raspberry PI (or more generally, a microcontroller) that have no specific function, but are programmable as either input or output

Today
GPIO
You can either configure them as outputs HIGH (3.3v) or LOW (0v).
You can either configure them as inputs (reads in voltage, returns true if ~3.3v on the pin, false if ~0v on the pin.
How do we interface with GPIO?
Memory Mapped IO!
GPIO
// From GPIO.h
#define MMIO_BASE 0x3F000000
#define GPFSEL0 ((volatile unsigned int *)(MMIO_BASE + 0x00200000))
#define GPFSEL1 ((volatile unsigned int *)(MMIO_BASE + 0x00200004))
#define GPFSEL2 ((volatile unsigned int *)(MMIO_BASE + 0x00200008))
#define GPFSEL3 ((volatile unsigned int *)(MMIO_BASE + 0x0020000C))
#define GPFSEL4 ((volatile unsigned int *)(MMIO_BASE + 0x00200010))
#define GPFSEL5 ((volatile unsigned int *)(MMIO_BASE + 0x00200014))
#define GPSET0 ((volatile unsigned int *)(MMIO_BASE + 0x0020001C))
#define GPSET1 ((volatile unsigned int *)(MMIO_BASE + 0x00200020))
#define GPCLR0 ((volatile unsigned int *)(MMIO_BASE + 0x00200028))
#define GPCLR1 ((volatile unsigned int *)(MMIO_BASE + 0x0020002C))
#define GPLEV0 ((volatile unsigned int *)(MMIO_BASE + 0x00200034))
#define GPLEV1 ((volatile unsigned int *)(MMIO_BASE + 0x00200038))
#define GPEDS0 ((volatile unsigned int *)(MMIO_BASE + 0x00200040))
#define GPEDS1 ((volatile unsigned int *)(MMIO_BASE + 0x00200044))
#define GPREN0 ((volatile unsigned int *)(MMIO_BASE + 0x0020004C))
#define GPREN1 ((volatile unsigned int *)(MMIO_BASE + 0x00200050))
#define GPFEN0 ((volatile unsigned int *)(MMIO_BASE + 0x00200058))
#define GPFEN1 ((volatile unsigned int *)(MMIO_BASE + 0x0020005C))
#define GPHEN0 ((volatile unsigned int *)(MMIO_BASE + 0x00200064))
#define GPHEN1 ((volatile unsigned int *)(MMIO_BASE + 0x00200068))
#define GPLEN0 ((volatile unsigned int *)(MMIO_BASE + 0x00200070))
#define GPLEN1 ((volatile unsigned int *)(MMIO_BASE + 0x00200074))
#define GPAREN0 ((volatile unsigned int *)(MMIO_BASE + 0x0020007C))
#define GPAREN1 ((volatile unsigned int *)(MMIO_BASE + 0x00200080))
#define GPAFEN0 ((volatile unsigned int *)(MMIO_BASE + 0x00200088))
#define GPAFEN1 ((volatile unsigned int *)(MMIO_BASE + 0x0020008C))
#define GPPUD ((volatile unsigned int *)(MMIO_BASE + 0x00200094))
#define GPPUDCLK0 ((volatile unsigned int *)(MMIO_BASE + 0x00200098))
#define GPPUDCLK1 ((volatile unsigned int *)(MMIO_BASE + 0x0020009C))
These are all registers that control certain aspects of the GPIO pins. You modify them by reading and writing to them.
GPIO
A few important registers:
- GPFSEL0-GPFSEL5: Specifies the functions of a GPIO pin.
- GPSET0-GPSET1: Sets a high value on a GPIO pin
- GPCLR0-GPCLR1: Sets a low value on a GPIO pin
- GPLEV0-GPLEV1: Reads a value from a GPIO pin
GPFSEL0 specifies the function of pins 0-9, GPFSEL1 specifies the function of pins 10-19,

GPFSEL0:
GPIO
GPSET0-GPSET1: Sets a high value on a GPIO pin GPSET0 controls GPIO pins 0-31 , GPSET1 controls 32 and above
GPSET0: Bit 0 sets pin 0, Bit 1 sets pin 1
GPSET1: Bit 0 sets pin 32, Bit 1 sets pin 33
GPIO

GPIO
*GPFSEL0 &= ~(0x07 << 21); // Clear out bits 21-23
*GPFSEL0 |= (0x01 << 21); // set bits 21-23 to 0b001
For example, set GPIO 7 as output
*GPCLR0 = (0x1<<7);
For example, set GPIO 7 as low
*GPSET0 = (0x1<<7);
For example, set GPIO 7 as high
Demo
Demo

How to use a breadboard
Demo

How to use a breadboard


CPSC 359: Pi GPIO
By Joshua Horacsek
CPSC 359: Pi GPIO
- 1,672