This is the chapter web page to support the content in Chapter 16 of the book: Exploring Raspberry Pi – Interfacing to the Real World with Embedded Linux. The summary introduction to the chapter is as follows:

In this chapter, you are introduced to Linux kernel programming on an embedded device such as the Raspberry Pi (RPi). Kernel programming is an advanced topic that requires in-depth study of the source code for the Linux kernel; however, this chapter is structured as a practical step-by-step guide to the focused task of writing Linux loadable kernel modules (LKMs) that interface to general-purpose inputs/outputs (GPIOs). The first example is a straightforward “Hello World” module that can be used to establish a configuration for LKM development on the RPi. The second LKM example introduces interrupt service routines (ISRs), and interfaces a simple GPIO button and LED circuit to Linux kernel space. Two further examples are provided that introduce the kobject interface and the use of kernel threads to build kernel-space sysfs devices for the RPi. By the end of this chapter, you should be familiar with the steps required to write kernel code, and appreciate the programming constraints that such development entails.

After completing this chapter, you should hopefully be able to do the following:

  • Write a basic Linux loadable kernel module (LKM) that can receive a kernel argument.
  • Build, load, and unload a custom LKM on a desktop machine and/or the RPi.
  • Undertake the steps required to build a module for embedded devices that can control GPIOs.
  • Appreciate some of the concepts required to build LKMs on an embedded Linux device, such as interrupts, kobjects, and kernel threads.

Digital Media Resources

Video Demonstration

A short YouTube video is provided here that presents an overview of the functionality of the type of LKMs that are developed in this chapter.

Video: A video of the type of functionality of the LKMs that are described in this article.

The LKM examples in Chapter 16 focus on interfacing to GPIOs and the use of sysfs. However, you might also be interested in a blog discussion “Writing a Linux Kernel Module — Part 2: A Character Device,” on writing a straightforward character driver that can be used to pass information between a Linux user-space program and a loadable kernel module (LKM), which is running in Linux kernel space. In this example, a C user-space application sends a string to the LKM. The LKM then responds with the message that was sent along with the number of letters that the sent message contains. Later in the article I describe why we need to solve synchronization problems that arise with this approach, and I provide a version of the program that uses mutexes to provide a solution.

External Resources


None for the moment

Recommended Books on the Content in this Chapter