Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile a kernel module for Raspberry pi?

I'm having trouble compiling a kernel module for a raspberry pi. I want to compile a "hello world" kernel module using the raspberry pi itself.

I am using raspbian wheezy 3.6.11+.

I tried following the directions at http://elinux.org/RPi_Kernel_Compilation.

Here is the Makefile I am using:

obj-m += hello-1.o  all:     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules  clean:     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean 

Here is the source code for hello-1.c:

/*    *  hello-1.c - The simplest kernel module.  */ #include <linux/module.h>   /* Needed by all modules */ #include <linux/kernel.h>   /* Needed for KERN_INFO */  int init_module(void) {     printk(KERN_INFO "Hello world 1.\n");      /*       * A non 0 return means init_module failed; module can't be loaded.       */     return 0; }  void cleanup_module(void) {     printk(KERN_INFO "Goodbye world 1.\n"); } 

Here's what I get when I try to make the project:

root@raspberrypi:/home/pi/hello-module# make make -C /lib/modules/3.6.11+/build M=/home/pi/hello-module modules make: *** /lib/modules/3.6.11+/build: No such file or directory.  Stop. make: *** [all] Error 2 

I tried creating the build directory at /lib/modules/3.6.11+

make -C /lib/modules/3.6.11+/build M=/home/pi/hello-module modules make[1]: Entering directory `/lib/modules/3.6.11+/build' make[1]: *** No rule to make target `modules'.  Stop. make[1]: Leaving directory `/lib/modules/3.6.11+/build' make: *** [all] Error 2 

I have GNU Make 3.81 and gcc (Debian 4.6.3-14+rpi1) 4.6.3 installed. I also installed the linux source using

sudo apt-get install linux-source

Any ideas on what I might do to get this to compile?

like image 438
user3025582 Avatar asked Nov 23 '13 20:11

user3025582


People also ask

How long does it take to compile kernel on Raspberry Pi?

The low processing power of the Raspberry Pi means that a local compile will take many hours. A compilation of the latest kernel and modules took about 752 minutes (12h30m)!


1 Answers

Here are the steps I used to build the Hello World kernel module on Raspbian.

  1. Perform sudo rpi-update

    See https://github.com/Hexxeh/rpi-update for details on rpi-update. You have to be on the latest firmware and associated kernel to be able to perform the next step.

  2. Install and run rpi-source to install the source code that built the latest kernel that you are running. This will create the correct entry in /lib/modules for the kernel that you are running. Note: you don't need to be root to run this, however the script will perform certain tasks using sudo and the root password will be requested during the script execution.

    Instructions to install rpi-source can be found at https://github.com/notro/rpi-source/wiki

Once those steps are performed you should be able to make the Hello World kernel module.

johnma@raspberrypi ~/HelloWorld $ make make -C /lib/modules/3.12.19+/build M=/home/johnma/HelloWorld modules make[1]: Entering directory `/home/johnma/linux-c3db7205bcd8988cf7c185e50c8849542554b1f5'   CC [M]  /home/johnma/HelloWorld/hello.o   Building modules, stage 2.   MODPOST 1 modules   CC      /home/johnma/HelloWorld/hello.mod.o   LD [M]  /home/johnma/HelloWorld/hello.ko make[1]: Leaving directory `/home/johnma/linux-c3db7205bcd8988cf7c185e50c8849542554b1f5'  johnma@raspberrypi ~/HelloWorld $ sudo insmod hello.ko johnma@raspberrypi ~/HelloWorld $ tail -1 /var/log/syslog May 15 13:45:39 raspberrypi kernel: [59789.169461] Hello World :)  johnma@raspberrypi ~/HelloWorld $ sudo rmmod hello.ko johnma@raspberrypi ~/HelloWorld $ tail -1 /var/log/syslog May 15 13:46:10 raspberrypi kernel: [59819.503503] Goodbye World! 
like image 90
HeatfanJohn Avatar answered Oct 06 '22 10:10

HeatfanJohn