F28HS Hardware-Software Interface:
Systems Programming
Hans-Wolfgang Loidl
School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh
Semester 2 — 2021/22
0No proprietary software has been used in producing these slides
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface 2021/22 1 / 17
Outline
1 Tutorial 1: Using Python and the Linux FS for GPIO Control
2 Tutorial 2: Programming an LED
3
Tutorial 3: Programming a Button input device
4 Tutorial 4: Inline Assembler with gcc
5 Tutorial 5: Programming an LCD Display
6 Tutorial 6: Performance Counters on the RPi 2
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface 2021/22 2 / 17
Tutorial 3: Programming a Button input device
In this tutorial we want to use a button, connected through a breadboard as an input device.
This is the simplest input device that we will cover.
The code needed to control is typical for such devices.
This tutorial deals withprogramming a button as input device.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 3 / 17
Core Techniques
In the LED tutorial, we have seen that we first need toidentify the registersthat give control to the device.
For that we will again look into theBCM Peripherals documentation.
We will then go through a simple example of
I reading button input data,
I blinking an LED on button press.
We want to connect the button withpin 24, using a breadboard.
These simple activities, will also be at the core of CW2.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 4 / 17
GPIO Register Assignment
0SeeBCM Peripherals Manual, Chapter 6, Table 6.1
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 5 / 17
Registers 13 14
GPIO Register Assignment
0SeeBCM Peripherals Manual, Chapter 6, Table 6.1
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 5 / 17
Registers 13 14
BCM2835 GPIO Peripherals
The main registers that we need in this case are (see p90ff of BCM2835 ARM peripherals):
GPFSEL: function select registers (3 bits per pin); set it to 0 for input, 1 for output; 6 more alternate functions available
GPSET:setthe corresponding pin GPCLR:clearthe corresponding pin
GPLEV: return thevalueof the corresponding pin
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 6 / 17
Define the button pin as an INPUT device
12:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 11:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 10:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 9: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 8: 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: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 6: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 5: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 4: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 3: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 2: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 1: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 0: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8
pin 24 Write into these bits (12–14) to set thefunctionforpin 24
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 7 / 17
Define the button pin as an INPUT device
12:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 11:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 10:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 9: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 8: 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: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 6: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 5: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 4: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 3: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 2: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 1: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 0: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8
2 14 13 12
Write into these bits (12–14) to set thefunctionforpin 24
NB:Recall, each GPFSEL register controls 10 pins (1–10, 11–20, etc);
for each pin, 3-bits control the behaviour.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 7 / 17
Define the button pin as an INPUT device
12:
11:
10:
9:
8:
7:
6:
5:
4:
3:
2:
1:
0:
2 0 0 0
NB:To set a pin toinputwe need to write the value0into it.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 7 / 17
Reading from the button input
GPIO registers (Base address: 0x3F200000)
15:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 14:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 13:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 12:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 11:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 10:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 9: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 8: 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: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 6: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 5: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 4: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8
GPLEV0 pin 24
Read this bit (24)
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 8 / 17
Contents:
Bit positions
Reading from the button input
GPIO registers (Base address: 0x3F200000)
15:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 14:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 13:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 12:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 11:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 10:31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 9: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 8: 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: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 6: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 5: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 4: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8
13 24
Read from this bit (24)
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 8 / 17
Contents:
Bit positions
Reading from the button input
GPIO registers (Base address: 0x3F200000)
15 14 13 12 11 10 9 8 7 6 5 4
1
Input:HIGH
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 8 / 17
Contents:
Bit values
Reading from the button input
GPIO registers (Base address: 0x3F200000)
15 14 13 12 11 10 9 8 7 6 5 4
0
Input: LOW
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 8 / 17
Contents:
Bit values
Sample C code: Button input
First we define some constants that we will need.
// Tunables:
// PINs (based on BCM numbering)
#define LED 23
#define BUTTON 24
// delay for loop iterations (mainly), in ms
#define DELAY 200
#define INPUT 0
#define OUTPUT 1
#define LOW 0
#define HIGH 1
This assumes that we have wired-up the button with GPIO pin 24 and the LED with GPIO pin 23.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 9 / 17
Using a breadboard
To control an external LED, you could directly connect GPIO pins with the LED and a resistor using jumper cables.
However, a breadboard is a more flexible way of wiring peripherals, such as LEDs or buttons, to the RPi.
You need to understand how the columns and the rows on a breadboard are connected, though.
For a good basic intro on how to use a breadboardfollow this link
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 10 / 17
The wiring as a Fritzing diagram
To describe a specific wiring, we useFritzing diagramslike this:
AnLED, as output device, is connected to the RPi2 using GPIO pin 23.
Abutton, as input device, is connected to GPIO pin 24.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 11 / 17
Sample C code: Button input
We memory-map the addresses for the GPIO registers (as before).
gpiobase = 0x3F200000;
// memory mapping
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
return failure (FALSE, "setup: Unable to open / dev/mem: %s\n", strerror (errno)) ;
// GPIO:
gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|
PROT_WRITE, MAP_SHARED, fd, gpiobase) ; if ((int32_t)gpio == -1)
return failure (FALSE, "setup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 12 / 17
Sample C code: Button input
We set the modes for the LED pin (OUTPUT) and the button pin (INPUT).
// setting the mode
fSel = 2; // register 2 (GPFSEL2) shift = 9; // slot 3 (shift 3*3) // set the above pin to output mode
*(gpio + fSel) = (*(gpio + fSel) & ˜(7 << shift))
| (1 << shift) ; // Sets bits to one = output
fSel = 2; // register 2 (GPFSEL2) shift = 12; // slot 4 (shift 4*3) // set the above pin to input mode
*(gpio + fSel) = (*(gpio + fSel) & ˜(7 << shift))
; // Sets bits to zero = input
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 13 / 17
Sample C code: Button input
Inside the main loop, we first read from the bit associated with the button input in theGPLEV0register.
for (j=0; j<1000; j++) {
if ((*(gpio + 13 /* GPLEV0 */) & (1 << (BUTTON &
31))) != 0) theValue = HIGH ; else
theValue = LOW ;
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 14 / 17
Sample C code: Button input
Further down the loop, we write to the bit associated with the LED output in theGPLCR0orGPSET0register.
if (theValue == LOW) {
clrOff = 10; // GPCLR0 for pin 23
*(gpio + clrOff) = 1 << (LED & 31); // 23-rd bit in the register
} else {
setOff = 7; // GPSET0 for pin 23
*(gpio + setOff) = 1 << (LED & 31); // 23-rd bit in the register
}
// delay ...
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 15 / 17
Sample C code: Button input
Finally, we want to clean-up by setting the LED to LOW. Which kind of code do we need here?
// clean-up by setting the LED pin to LOW
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 16 / 17
Summary
Reading input from a button works in the same way as writing to the LED:
I We need to identify the relevant registers and bits for our pin
I We declare the pin an INPUT device in theGPFSELregister
I We read from the associated bit in theGPLEVregister to get the input
With the button you have a basic input device to communicate with the system
In the CW we will combine a button (for input), an LED (for output) and an LCD display (for nicer output) and implement a small app for this configuration.
See sample source: tut button.c
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 17 / 17