ECEC 201 · Week 9
Modern Computer: Von Neumann
A GPIO line is requested from the Linux character device, then set or read.
Week 9 of 10 · Das Frite
ECEC 201 · Week 9 · In class
A GPIO line is requested from the Linux character device, then set or read.
- A button bounces, so the program waits a debounce interval before it counts another press.
- Use only the pins the station sheet lists.
ECEC 201 · Week 9 · In class
A Makefile rebuilds only what changed.
- Change one source file and show that only it recompiles.
- This week is the project checkpoint: the button program, built with
make. - No overlay.
- No driver.
ECEC 201 · Week 9 · Lecture
Every signal pin is also a GPIO line
- 5 V
- 3.3 V
- Ground
- GPIO
- I2C
- SPI
- UART
- PWM
- I2S
- S/PDIF
- Clock
- SDIO
Colours as on the Das Frite pinout, where each pin's name is. The square pad is pin 1.
ECEC 201 · Week 9 · Lecture
A GPIO line is requested, then used
- Each GPIO controller is a file,
/dev/gpiochipN; a line is an offset on that chip. gpiodetectlists the chips andgpioinfolists their lines and who holds them; both are on the board's image.- A program asks the chip for the line as an input or an output and gets back a new file descriptor for it.
- The line stays yours until that descriptor is closed or the program exits.
ECEC 201 · Week 9 · Lecture
Requesting one input line
/* needs linux/gpio.h, string.h, sys/ioctl.h */
int request_input(int chip_fd, unsigned offset) /* -1: error */
{
struct gpio_v2_line_request req;
memset(&req, 0, sizeof req);
req.offsets[0] = offset;
req.num_lines = 1;
req.config.flags = GPIO_V2_LINE_FLAG_INPUT;
strcpy(req.consumer, "button");
if (ioctl(chip_fd, GPIO_V2_GET_LINE_IOCTL, &req) < 0)
return -1;
return req.fd;
}
ECEC 201 · Week 9 · Lecture
Reading the line, and why buttons bounce
struct gpio_v2_line_values v = { .mask = 1 };
if (ioctl(line_fd, GPIO_V2_LINE_GET_VALUES_IOCTL, &v) == 0)
pressed = v.bits & 1;
- A mechanical contact chatters for a few milliseconds when pressed or released.
- Read fast enough and one press looks like several.
- Debounce: count a change only after the value has stayed the same for an interval you choose and justify.
ECEC 201 · Week 9 · Lecture
make rebuilds only what changed
CFLAGS = -Wall -Wextra -g
button: main.o gpio.o
$(CC) $(CFLAGS) -o $@ $^
- A rule is a target, a colon, its prerequisites, then recipe lines.
- Each recipe line starts with a tab, not spaces.
$@is the target;$^is every prerequisite.
ECEC 201 · Week 9 · Lab
Lab
- With the GPIO character-device interface, light one LED and read one button on the pins the station sheet lists.
- Debounce the button, count 20 presses, and build the program with a `Makefile`. Second pass: change one source file, run `make`, and show that only that file recompiles.
- Assemble the lab hand-in: The program, the `Makefile`, the pin numbers, and the count from 20 presses.
ECEC 201 · Week 9 · Lab
Hand in
The program, the `Makefile`, the pin numbers, and the count from 20 presses.
ECEC 201 · Week 9 · Homework
Homework
- Write the `Makefile` rule for one object file and say what makes it rebuild.
ECEC 201 · Week 9 · Homework
Turn in
The rule and what triggers it.
ECEC 201 · Week 9 · Read
Read
On the lab site: Module 9, Device tree and pin multiplexing, reaches GPIO through an overlay. This week grades a C program on the lines the image already exposes. Stop before an overlay and before a driver.
ECEC 201 · Week 9 · Next
Week 10
The demo is one C program: each button press logs one decoded register reading to a file.