• No results found

F28HS Hardware-Software Interface: Systems Programming

N/A
N/A
Protected

Academic year: 2022

Share "F28HS Hardware-Software Interface: Systems Programming"

Copied!
23
0
0

Loading.... (view fulltext now)

Full text

(1)

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

(2)

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

(3)

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

(4)

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

(5)

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

(6)

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

(7)

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

(8)

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

(9)

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

(10)

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

(11)

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

(12)

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

(13)

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

(14)

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

(15)

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

(16)

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

(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

(18)

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

(19)

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

(20)

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

(21)

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

(22)

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

(23)

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

References

Related documents

Global Anglicanism Project (GAP) Episcopal Church Foundation Originator and Member of the Management Team, 2002-2005 Anglican Contextual Theologians Network. Founder and Host of

If you are installing a dashboard environment with monitoring dashboard applications such as the IBM Infrastructure Management Dashboards for Servers, IBM Infrastructure

practices on lead poisoning and utilization of personal protective equipment by battery technicians at the workplace controlling for the covariates (age, education level, marital

7 in respect of an amount equal to the gross profits of the employer  for such preceding accounting year after deducting there from the amount of bonus which the employer has paid or

(Centre for Cardiovascular Genetics, Depart- ment of Medicine, British Heart Foundation Laboratories, Rayne Build- ing, Royal Free and University College Medical School, 5

River health attributes included the length of native riverside vegetation and the number of rare species in the George catchment.. The area of healthy seagrass beds in the

The objectives include minimizing the storage size of new infrastructures, maximizing firm monthly and average annual energy generation from the proposed dams and maximizing

Both samples were parents of 4th grade youth, similar in age; sex; frequency of eating dinner with their child; ecSI 2.0 TM and subscale scores; and the primary outcomes of