Creating a Basic LED Driver for Raspberry Pi

This tutorial demonstrates how to develop and debug a basic hardware driver for Raspberry PI. It will demonstrate the following techniques:

  • Controlling the BCM2708/BCM2835 peripherals by accessing their hardware registers
  • Creating a sysfs device object to provide user-mode control interface
  • Using software timers provided by Linux Kernel

We will create a kernel module that will make an LED connected to the Raspberry Pi blink with a specified period. The user-mode applications will be able to modify the period via a sysfs interface. Note that the Raspberry Pi kernel already comes with a GPIO driver that allows user-mode applications to control the GPIO pins (and LEDs connected to them) directly, however we will not reuse it and will build our driver from scratch to demonstrate direct hardware access.

  1. First of all, create a basic kernel module project for Raspberry Pi by following this tutorial. Ensure that you can build and debug your module.
  2. Connect an LED to one of the GPIO pins not involved in JTAG debugging. In this example we will use the GPIO_GEN1 signal that corresponds to pin 12 on P1 and GPIO18:
  3. According to the BCM2835 peripheral description, the GPIO pins can be controlled by first configuring them as output by writing to one of GPFSELx registers and then writing to GPFSETx/GPFCLRx registers. We will define a structure describing the GPIO registers and provide functions for controlling GPIO pins using it:
  4. Now we need to set s_pGpioRegisters to the address of the GPIO registers in memory. Include the <asm/io.h> and <mach/platform.h> files and add the following line to your init() function:

    The GPIO_BASE is the base address of the GPIO registers defined in platform.h.
  5. Now we will make the LED blink with a given period (1 second in this example). If we were writing a user-mode application, we could just make an infinite loop with a call to sleep() inside it. However if we do that in our init() function, we will lock up the kernel and prevent other modules from being loaded until init() returns. Thankfully, the kernel provides an easy mechanism to work around this – timers. All we need to do is create a timer and set it to fire one second from ‘now’. Once it fires, we toggle the state of our LED and set it to fire in a second again. The timer will consist of a timer object and a timer callback that will be invoked every second:
  6. Finally we need to add code for setting up the timer in our init() function and code for deleting it in the
    exit() function. We also need to configure the GPIO pin in the output mode before we start toggling it:
  7. Build your module and start debugging it. You can step through the initialization function to double-check that all necessary initialization succeeds:
  8. Once you hit F5 to resume the Raspberry Pi kernel the LED will start blinking:
  9. Now we will create a sysfs-based user-mode interface for changing the LED blinking frequency. This is accomplished in 3 steps:
    • Register a device class (e.g. /sys/class/LedBlink)
    • Register a device object within the class (e.g. /sys/class/LedBlink/LedBlink)
    • Create a sysfs file for the device object (e.g. /sys/class/LedBlink/LedBlink/period)

    First of all we will define a function that will receive a string value, convert it to an integer and update the LED blinking period accordingly:

  10. Create the class, device and file objects from the init() function:

  11. Finally add the cleanup code to the exit() function:

    You will need to switch the license of your module to GPL to be able to call device_destroy().

  12. Build your module and start debugging it. You can step through the initialization to re-check everything:
  13. Connect to your Raspberry Pi over SSH and write the new period value to the /sys/class/LedBlink/LedBlink/period file using the root terminal:
    You will see how the LED blinking period will change. Note that if you write a value that is not a number, you will get an error (because set_period_callback() will return -EINVAL).

You can download the source code of the LED driver described in this tutorial here.