A kernel module for controlling a simple LED (or any signal) that is connected to a GPIO. It is threaded in order that it can flash the LED. The sysfs entry appears at /sys/ebb/led49.
More...
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/gpio.h>
#include <linux/kobject.h>
#include <linux/kthread.h>
#include <linux/delay.h>
|
| MODULE_LICENSE ("GPL") |
|
| MODULE_AUTHOR ("Derek Molloy") |
|
| MODULE_DESCRIPTION ("A simple Linux LED driver LKM for the BBB") |
|
| MODULE_VERSION ("0.1") |
|
| module_param (gpioLED, uint, S_IRUGO) |
| Param desc. S_IRUGO can be read/not changed. More...
|
|
| MODULE_PARM_DESC (gpioLED," GPIO LED number (default=49)") |
| parameter description More...
|
|
| module_param (blinkPeriod, uint, S_IRUGO) |
| Param desc. S_IRUGO can be read/not changed. More...
|
|
| MODULE_PARM_DESC (blinkPeriod," LED blink period in ms (min=1, default=1000, max=10000)") |
|
static ssize_t | mode_show (struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
| A callback function to display the LED mode. More...
|
|
static ssize_t | mode_store (struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) |
| A callback function to store the LED mode using the enum above. More...
|
|
static ssize_t | period_show (struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
| A callback function to display the LED period. More...
|
|
static ssize_t | period_store (struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) |
| A callback function to store the LED period value. More...
|
|
static int | flash (void *arg) |
| The pointer to the thread task. More...
|
|
static int __init | ebbLED_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 | ebbLED_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. More...
|
|
| module_init (ebbLED_init) |
|
| module_exit (ebbLED_exit) |
|
A kernel module for controlling a simple LED (or any signal) that is connected to a GPIO. It is threaded in order that it can flash the LED. The sysfs entry appears at /sys/ebb/led49.
- Author
- Derek Molloy
- Date
- 19 April 2015
- See also
- http://www.derekmolloy.ie/
static void __exit ebbLED_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.
174 printk(KERN_INFO
"EBB LED: Goodbye from the EBB LED LKM!\n");
static struct task_struct * task
The pointer to the kobject.
Definition: led.c:103
static struct kobject * ebb_kobj
Definition: led.c:102
static unsigned int gpioLED
Default GPIO for the LED is 49.
Definition: led.c:24
static int __init ebbLED_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
135 printk(KERN_INFO
"EBB LED: Initializing the EBB LED LKM\n");
138 ebb_kobj = kobject_create_and_add(
"ebb", kernel_kobj->parent);
140 printk(KERN_ALERT
"EBB LED: failed to create kobject\n");
146 printk(KERN_ALERT
"EBB LED: failed to create sysfs group\n");
151 gpio_request(
gpioLED,
"sysfs");
156 task = kthread_run(
flash, NULL,
"LED_flash_thread");
158 printk(KERN_ALERT
"EBB LED: failed to create the task\n");
159 return PTR_ERR(
task);
static char ledName[7]
Null terminated default string – just in case.
Definition: led.c:32
static struct attribute_group attr_group
Definition: led.c:97
static struct task_struct * task
The pointer to the kobject.
Definition: led.c:103
static struct kobject * ebb_kobj
Definition: led.c:102
static unsigned int gpioLED
Default GPIO for the LED is 49.
Definition: led.c:24
static bool ledOn
Is the LED on or off? Used for flashing.
Definition: led.c:33
static int flash(void *arg)
The pointer to the thread task.
Definition: led.c:110
static int flash |
( |
void * |
arg | ) |
|
|
static |
The pointer to the thread task.
The LED Flasher main kthread loop
- Parameters
-
arg | A void pointer used in order to pass data to the thread |
- Returns
- returns 0 if successful
111 printk(KERN_INFO
"EBB LED: Thread has started running \n");
112 while(!kthread_should_stop()){
113 set_current_state(TASK_RUNNING);
118 set_current_state(TASK_INTERRUPTIBLE);
121 printk(KERN_INFO
"EBB LED: Thread has run to completion \n");
static unsigned int gpioLED
Default GPIO for the LED is 49.
Definition: led.c:24
static bool ledOn
Is the LED on or off? Used for flashing.
Definition: led.c:33
static enum modes mode
Default mode is flashing.
Definition: led.c:35
static unsigned int blinkPeriod
The blink period in ms.
Definition: led.c:28
static ssize_t mode_show |
( |
struct kobject * |
kobj, |
|
|
struct kobj_attribute * |
attr, |
|
|
char * |
buf |
|
) |
| |
|
static |
A callback function to display the LED mode.
- Parameters
-
kobj | represents a kernel object device that appears in the sysfs filesystem |
attr | the pointer to the kobj_attribute struct |
buf | the buffer to which to write the number of presses |
- Returns
- return the number of characters of the mode string successfully displayed
45 case OFF:
return sprintf(buf,
"off\n");
46 case ON:
return sprintf(buf,
"on\n");
47 case FLASH:
return sprintf(buf,
"flash\n");
48 default:
return sprintf(buf,
"LKM Error\n");
static enum modes mode
Default mode is flashing.
Definition: led.c:35
static ssize_t mode_store |
( |
struct kobject * |
kobj, |
|
|
struct kobj_attribute * |
attr, |
|
|
const char * |
buf, |
|
|
size_t |
count |
|
) |
| |
|
static |
A callback function to store the LED mode using the enum above.
55 if (strncmp(buf,
"on",count-1)==0) {
mode =
ON; }
56 else if (strncmp(buf,
"off",count-1)==0) {
mode =
OFF; }
57 else if (strncmp(buf,
"flash",count-1)==0) {
mode =
FLASH; }
static enum modes mode
Default mode is flashing.
Definition: led.c:35
MODULE_AUTHOR |
( |
"Derek Molloy" |
| ) |
|
MODULE_DESCRIPTION |
( |
"A simple Linux LED driver LKM for the BBB" |
| ) |
|
This next calls are mandatory – they identify the initialization function and the cleanup function (as above).
module_param |
( |
gpioLED |
, |
|
|
uint |
, |
|
|
S_IRUGO |
|
|
) |
| |
Param desc. S_IRUGO can be read/not changed.
Param desc. S_IRUGO can be read/not changed.
MODULE_PARM_DESC |
( |
gpioLED |
, |
|
|
" GPIO LED number (default=49)" |
|
|
) |
| |
MODULE_PARM_DESC |
( |
blinkPeriod |
, |
|
|
" LED blink period in ms (min=1, default=1000, max=10000)" |
|
|
) |
| |
static ssize_t period_show |
( |
struct kobject * |
kobj, |
|
|
struct kobj_attribute * |
attr, |
|
|
char * |
buf |
|
) |
| |
|
static |
A callback function to display the LED period.
static unsigned int blinkPeriod
The blink period in ms.
Definition: led.c:28
static ssize_t period_store |
( |
struct kobject * |
kobj, |
|
|
struct kobj_attribute * |
attr, |
|
|
const char * |
buf, |
|
|
size_t |
count |
|
) |
| |
|
static |
A callback function to store the LED period value.
69 sscanf(buf,
"%du", &period);
70 if ((period>1)&&(period<=10000)){
static unsigned int blinkPeriod
The blink period in ms.
Definition: led.c:28
struct attribute_group attr_group |
|
static |
Initial value:= {
}
static char ledName[7]
Null terminated default string – just in case.
Definition: led.c:32
static struct attribute * ebb_attrs[]
Definition: led.c:87
The attribute group uses the attribute array and a name, which is exposed on sysfs – in this case it is gpio49, which is automatically defined in the ebbLED_init() function below using the custom kernel parameter that can be passed when the module is loaded.
unsigned int blinkPeriod = 1000 |
|
static |
struct attribute* ebb_attrs[] |
|
static |
Initial value:= {
NULL,
}
static struct kobj_attribute period_attr
Definition: led.c:81
static struct kobj_attribute mode_attr
Definition: led.c:82
The ebb_attrs[] is an array of attributes that is used to create the attribute group below. The attr property of the kobj_attribute is used to extract the attribute struct
unsigned int gpioLED = 49 |
|
static |
Default GPIO for the LED is 49.
char ledName[7] = "ledXXX" |
|
static |
Null terminated default string – just in case.
Is the LED on or off? Used for flashing.
Default mode is flashing.
Use these helper macros to define the name and access levels of the kobj_attributes The kobj_attribute has an attribute attr (name and mode), show and store function pointers The period variable is associated with the blinkPeriod variable and it is to be exposed with mode 0666 using the period_show and period_store functions above
The pointer to the kobject.