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.

ECEC 201 · Week 9 · In class

A Makefile rebuilds only what changed.

ECEC 201 · Week 9 · Lecture

Every signal pin is also a GPIO line

139240
  • 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

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;

ECEC 201 · Week 9 · Lecture

make rebuilds only what changed

CFLAGS = -Wall -Wextra -g

button: main.o gpio.o
	$(CC) $(CFLAGS) -o $@ $^

ECEC 201 · Week 9 · Lab

Lab

  1. With the GPIO character-device interface, light one LED and read one button on the pins the station sheet lists.
  2. 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.
  3. 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

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.

Arrow keys or space to move, F for full screen, N for speaker notes, Home and End for the first and last slide. Print gives one slide per page.