Complete Chapter 03 and keep demo_events.c and poll_events.py. See Chapter 14 for Yocto packaging.
1. Wire the button
Shut down and unplug the Raspberry Pi 3. Verify that GPIO17 is available. Use a normally open button and a 10 kΩ pull-up resistor.
| Component | Wiring |
|---|---|
| Normally open button | GPIO17 (physical pin 11) to ground (physical pin 9) |
| 10 kΩ resistor | GPIO17 (physical pin 11) to 3.3 V (physical pin 1) |
The resistor holds GPIO17 high when released. Pressing the button pulls GPIO17 low and produces a falling edge.
2. Describe the button in Device Tree
Create demo-button-overlay.dts.
/dts-v1/;
/plugin/;
/ {
compatible = "brcm,bcm2837";
fragment@0 {
target-path = "/";
__overlay__ {
demo-button {
compatible = "example,demo-button";
button-gpios = <&gpio 17 1>;
status = "okay";
};
};
};
};
The GPIO number is 17. The flag 1 means GPIO_ACTIVE_LOW, so the driver reads logical 1 when the pin is pulled low.
Compile on a development host with dtc installed.
dtc -@ -I dts -O dtb -o demo-button.dtbo demo-button-overlay.dts
Copy the overlay to the overlays/ directory on the boot partition used by the Pi firmware. Add the line below to that partition’s config.txt and reboot.
dtoverlay=demo-button
3. Replace the software event source with a GPIO interrupt
Keep the FIFO, event_read(), event_poll(), and the Makefile. Remove MAX_WRITE_SIZE, event_write(), and its .write entry in event_fops.
Add these headers.
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/interrupt.h>
#include <linux/jiffies.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
Before the existing event_init(), add the following.
#define DEBOUNCE_MS 25
struct demo_button {
struct gpio_desc *gpio;
struct device *dev;
unsigned long last_press;
bool has_press;
};
static irqreturn_t button_irq_thread(int irq, void *arg)
{
static const unsigned char message[] = "BUTTON_PRESS\n";
struct demo_button *button = arg;
const unsigned int len = sizeof(message) - 1;
int value;
/* Give the mechanical contacts time to settle. */
msleep(DEBOUNCE_MS);
value = gpiod_get_value_cansleep(button->gpio);
if (value < 0) {
dev_warn_ratelimited(button->dev, "GPIO read failed: %d\n", value);
return IRQ_HANDLED;
}
if (!value)
return IRQ_HANDLED;
if (button->has_press &&
time_before(jiffies,
button->last_press + msecs_to_jiffies(DEBOUNCE_MS)))
return IRQ_HANDLED;
button->last_press = jiffies;
button->has_press = true;
mutex_lock(&event_lock);
if (kfifo_avail(&event_fifo) < len) {
mutex_unlock(&event_lock);
dev_warn_ratelimited(button->dev, "event FIFO full, press dropped\n");
return IRQ_HANDLED;
}
kfifo_in(&event_fifo, message, len);
WRITE_ONCE(data_ready, true);
mutex_unlock(&event_lock);
wake_up_interruptible(&event_wait);
return IRQ_HANDLED;
}
static int button_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct demo_button *button;
int irq;
int ret;
button = devm_kzalloc(dev, sizeof(*button), GFP_KERNEL);
if (!button)
return -ENOMEM;
button->dev = dev;
button->gpio = devm_gpiod_get(dev, "button", GPIOD_IN);
if (IS_ERR(button->gpio))
return dev_err_probe(dev, PTR_ERR(button->gpio),
"cannot get button GPIO\n");
irq = gpiod_to_irq(button->gpio);
if (irq < 0)
return dev_err_probe(dev, irq, "cannot map button IRQ\n");
ret = devm_request_threaded_irq(dev, irq, NULL, button_irq_thread,
IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
dev_name(dev), button);
if (ret)
return dev_err_probe(dev, ret, "cannot request button IRQ\n");
dev_info(dev, "button ready on IRQ %d\n", irq);
return 0;
}
static const struct of_device_id button_of_match[] = {
{ .compatible = "example,demo-button" },
{ }
};
MODULE_DEVICE_TABLE(of, button_of_match);
static struct platform_driver button_driver = {
.probe = button_probe,
.driver = {
.name = "demo-button",
.of_match_table = button_of_match,
},
};
Replace event_init() and event_exit() with these versions. Keep the original module_init() and module_exit() declarations.
static int __init event_init(void)
{
int ret;
ret = misc_register(&event_device);
if (ret)
return ret;
ret = platform_driver_register(&button_driver);
if (ret)
misc_deregister(&event_device);
return ret;
}
static void __exit event_exit(void)
{
platform_driver_unregister(&button_driver);
misc_deregister(&event_device);
}
The consumer name button maps to button-gpios. The threaded handler can sleep while contacts settle and then acquire the mutex before enqueuing the event.
4. Verify on the Raspberry Pi
Build against the Pi’s target kernel. If matching headers are installed on the Pi, build there.
make -C "/lib/modules/$(uname -r)/build" M="$PWD" modules
For Yocto, adapt Chapter 14 to use demo_events.c and obj-m := demo_events.o. Before loading the new build, close any active readers and unload the old module.
sudo insmod ./demo_events.ko
sudo dmesg | grep 'button ready'
sudo dd if=/dev/demo-events bs=128 count=1 status=none
Press the button while dd waits. Expect this output.
BUTTON_PRESS
Rerun sudo python3 poll_events.py and press the button again. Look for button ready in the kernel log before starting a reader. The device node shows that the character device registered, while the probe log confirms that the GPIO driver bound.
This lab reports presses only. Start with a 25 ms debounce delay, then test several presses and check for one line per press.