A kernel module for controlling a GPIO LED/button pair. The device mounts devices via sysfs /sys/class/gpio/gpio115 and gpio49. Therefore, this test LKM circuit assumes that an LED is attached to GPIO 49 which is on P9_23 and the button is attached to GPIO 115 on P9_27. There is no requirement for a custom overlay, as the pins are in their default mux mode states.
More...
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
|
| MODULE_LICENSE ("GPL") |
|
| MODULE_AUTHOR ("Derek Molloy") |
|
| MODULE_DESCRIPTION ("A Button/LED test driver for the BBB") |
|
| MODULE_VERSION ("0.1") |
|
static irq_handler_t | ebbgpio_irq_handler (unsigned int irq, void *dev_id, struct pt_regs *regs) |
| Function prototype for the custom IRQ handler function – see below for the implementation. More...
|
|
static int __init | ebbgpio_init (void) |
| The LKM initialization function The static keyword restricts the visibility of the function to within this C file. The __init macro means that for a built-in driver (not a LKM) the function is only used at initialization time and that it can be discarded and its memory freed up after that point. In this example this function sets up the GPIOs and the IRQ. More...
|
|
static void __exit | ebbgpio_exit (void) |
| The LKM cleanup function Similar to the initialization function, it is static. The __exit macro notifies that if this code is used for a built-in driver (not a LKM) that this function is not required. Used to release the GPIOs and display cleanup messages. More...
|
|
| module_init (ebbgpio_init) |
|
| module_exit (ebbgpio_exit) |
|
|
static unsigned int | gpioLED = 49 |
| hard coding the LED gpio for this example to P9_23 (GPIO49) More...
|
|
static unsigned int | gpioButton = 115 |
| hard coding the button gpio for this example to P9_27 (GPIO115) More...
|
|
static unsigned int | irqNumber |
| Used to share the IRQ number within this file. More...
|
|
static unsigned int | numberPresses = 0 |
| For information, store the number of button presses. More...
|
|
static bool | ledOn = 0 |
| Is the LED on or off? Used to invert its state (off by default) More...
|
|
A kernel module for controlling a GPIO LED/button pair. The device mounts devices via sysfs /sys/class/gpio/gpio115 and gpio49. Therefore, this test LKM circuit assumes that an LED is attached to GPIO 49 which is on P9_23 and the button is attached to GPIO 115 on P9_27. There is no requirement for a custom overlay, as the pins are in their default mux mode states.
- Author
- Derek Molloy
- Date
- 19 April 2015
- See also
- http://www.derekmolloy.ie/
static void __exit ebbgpio_exit |
( |
void |
| ) |
|
|
static |
The LKM cleanup function Similar to the initialization function, it is static. The __exit macro notifies that if this code is used for a built-in driver (not a LKM) that this function is not required. Used to release the GPIOs and display cleanup messages.
83 printk(KERN_INFO
"GPIO_TEST: The button state is currently: %d\n", gpio_get_value(
gpioButton));
84 printk(KERN_INFO
"GPIO_TEST: The button was pressed %d times\n",
numberPresses);
91 printk(KERN_INFO
"GPIO_TEST: Goodbye from the LKM!\n");
static unsigned int gpioLED
hard coding the LED gpio for this example to P9_23 (GPIO49)
Definition: gpio_test.c:23
static unsigned int numberPresses
For information, store the number of button presses.
Definition: gpio_test.c:26
static unsigned int gpioButton
hard coding the button gpio for this example to P9_27 (GPIO115)
Definition: gpio_test.c:24
static unsigned int irqNumber
Used to share the IRQ number within this file.
Definition: gpio_test.c:25
static int __init ebbgpio_init |
( |
void |
| ) |
|
|
static |
The LKM initialization function The static keyword restricts the visibility of the function to within this C file. The __init macro means that for a built-in driver (not a LKM) the function is only used at initialization time and that it can be discarded and its memory freed up after that point. In this example this function sets up the GPIOs and the IRQ.
- Returns
- returns 0 if successful
41 printk(KERN_INFO
"GPIO_TEST: Initializing the GPIO_TEST LKM\n");
44 printk(KERN_INFO
"GPIO_TEST: invalid LED GPIO\n");
60 printk(KERN_INFO
"GPIO_TEST: The button state is currently: %d\n", gpio_get_value(
gpioButton));
64 printk(KERN_INFO
"GPIO_TEST: The button is mapped to IRQ: %d\n",
irqNumber);
73 printk(KERN_INFO
"GPIO_TEST: The interrupt request result is: %d\n", result);
static unsigned int gpioLED
hard coding the LED gpio for this example to P9_23 (GPIO49)
Definition: gpio_test.c:23
static irq_handler_t ebbgpio_irq_handler(unsigned int irq, void *dev_id, struct pt_regs *regs)
Function prototype for the custom IRQ handler function – see below for the implementation.
Definition: gpio_test.c:104
static bool ledOn
Is the LED on or off? Used to invert its state (off by default)
Definition: gpio_test.c:27
static unsigned int gpioButton
hard coding the button gpio for this example to P9_27 (GPIO115)
Definition: gpio_test.c:24
static unsigned int irqNumber
Used to share the IRQ number within this file.
Definition: gpio_test.c:25
static irq_handler_t ebbgpio_irq_handler |
( |
unsigned int |
irq, |
|
|
void * |
dev_id, |
|
|
struct pt_regs * |
regs |
|
) |
| |
|
static |
Function prototype for the custom IRQ handler function – see below for the implementation.
The GPIO IRQ Handler function This function is a custom interrupt handler that is attached to the GPIO above. The same interrupt handler cannot be invoked concurrently as the interrupt line is masked out until the function is complete. This function is static as it should not be invoked directly from outside of this file.
- Parameters
-
irq | the IRQ number that is associated with the GPIO – useful for logging. |
dev_id | the *dev_id that is provided – can be used to identify which device caused the interrupt Not used in this example as NULL is passed. |
regs | h/w specific register values – only really ever used for debugging. return returns IRQ_HANDLED if successful – should return IRQ_NONE otherwise. |
107 printk(KERN_INFO
"GPIO_TEST: Interrupt! (button state is %d)\n", gpio_get_value(
gpioButton));
109 return (irq_handler_t) IRQ_HANDLED;
static unsigned int gpioLED
hard coding the LED gpio for this example to P9_23 (GPIO49)
Definition: gpio_test.c:23
static unsigned int numberPresses
For information, store the number of button presses.
Definition: gpio_test.c:26
static bool ledOn
Is the LED on or off? Used to invert its state (off by default)
Definition: gpio_test.c:27
static unsigned int gpioButton
hard coding the button gpio for this example to P9_27 (GPIO115)
Definition: gpio_test.c:24
MODULE_AUTHOR |
( |
"Derek Molloy" |
| ) |
|
MODULE_DESCRIPTION |
( |
"A Button/LED test driver for the BBB" |
| ) |
|
This next calls are mandatory – they identify the initialization function and the cleanup function (as above).
unsigned int gpioButton = 115 |
|
static |
hard coding the button gpio for this example to P9_27 (GPIO115)
unsigned int gpioLED = 49 |
|
static |
hard coding the LED gpio for this example to P9_23 (GPIO49)
Used to share the IRQ number within this file.
Is the LED on or off? Used to invert its state (off by default)
unsigned int numberPresses = 0 |
|
static |
For information, store the number of button presses.