Arc I · Linux Driver Lab
Chapter 07

BBB GPIO Driver — First Real Hardware

Moving from simulated events to actual electrons on a BeagleBone Black.

BeagleBone Black AM335x gpio_request gpio_export real hardware

Why this driver?

Every driver up to this point ran on x86 and produced events in software. Camera pipelines eventually touch real hardware — sensors, GPIO lines, physical interfaces. This driver is the first step into that: controlling the USR0 LED on a BeagleBone Black via a character device, cross-compiled from an x86 host.

The hardware

ParameterValue
Target boardBeagleBone Black (AM335x)
Kernel5.10.168-ti-r76
GPIOGPIO1_21 → global number 53
LEDUSR0 onboard

GPIO numbering on AM335x: global = bank × 32 + pin. GPIO1_21 = 1×32+21 = 53.

LEDBankPinGlobal
USR012153
USR112254
USR212355
USR312456
The onboard LEDs are claimed by the leds-gpio driver at boot. You must release them first: echo none > /sys/class/leds/beaglebone:green:usr0/trigger

GPIO lifecycle

/* Init */
gpio_is_valid(GPIO_53);
gpio_request(GPIO_53, "GPIO_53");
gpio_direction_output(GPIO_53, 0);  /* output, start LOW */
gpio_export(GPIO_53, false);        /* visible in /sys/class/gpio/ */

/* Exit */
gpio_unexport(GPIO_53);
gpio_free(GPIO_53);

Write handler

static ssize_t etx_write(struct file *filp,
                         const char __user *buf, size_t len, loff_t *off)
{
    uint8_t rec_buf[10] = {0};
    copy_from_user(rec_buf, buf, len);
    if (rec_buf[0] == '1')      gpio_set_value(GPIO_53, 1);
    else if (rec_buf[0] == '0') gpio_set_value(GPIO_53, 0);
    return len;
}
# From userspace
echo 1 > /dev/etx_device   # LED on
echo 0 > /dev/etx_device   # LED off

Cross-compilation

Built on a Dell Latitude 5320 (Ubuntu 22.04) targeting the BBB's ARM Cortex-A8, deployed via SCP.

CROSS_COMPILE = arm-linux-gnueabihf-
ARCH          = arm
KDIR          = /home/sanath/bbb-kernel/build