Raspberry pi 3 kernel module compilation:



 

Raspberry pi 3 kernel module compile :

Step 1:
Update current kernel

$sudo apt-get update

$sudo apt-get upgrade

$sudo apt-get install bc

$sudo apt-get install gcc

Step 2:
Download linux headers for current kernel version

$uname -r
>4.4.26-v7+

copy kernel version

and goto url https://www.niksula.hut.fi/~mhiienka/Rpi/linux-headers-rpi/ and find your kernel version
here in my case
 
linux-headers-4.4.26-v7+_4.4.26-v7+-2_armhf.deb    20-Oct-2016 21:07             6891064

Download it

Step3:
Now install the downloaded inux headers.

$sudo dpkg -i linux-headers-4.4.26-v7+_4.4.26-v7+-2_armhf.deb

it takes while let it be complete without interrupt process.

Step 4 :
$cd

$mkdir kmod

$cd kmod

$sudo nano hello-1.c

copy and past following programe in file then save it [^X]

/*hello−1.c − The simplest kernel module.*/
#include < linux/init.h >             //linux/init.h Macros used to mark up functions e.g., __init __exit
#include < linux/module.h >           //linux/module.h Core header for loading LKMs into the kernel
#include < linux/kernel.h >           //linux/kernel.h Contains types, macros, functions for the kernel


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");


Now for compile this module we need make file
$sudo nano Makefile

copy and past following code in make file

obj-m += hello-1.o

all:
    make -C /lib/modules/4.4.26-v7+/build M=/home/pi/kmod modules
clean:
    make -C /lib/modules/4.4.26-v7+/build M=/home/pi/kmod clean


remember the there is Tab before make.

Save it

Step 5:

compile our hello world module

$sudo make
>
make -C /lib/modules/4.4.26-v7+/build M=/home/pi/kmod modules

make[1]: Entering directory '/usr/src/linux-headers-4.4.26-v7+'

CC [M] /home/pi/kmod/hello-1.o

Building modules, stage 2.

MODPOST 1 modules

CC /home/pi/kmod/hello-1.mod.o

LD [M] /home/pi/kmod/hello-1.ko

make[1]: Leaving directory '/usr/src/linux-headers-4.4.26-v7+'

Step 6:
insert module into kernel
$sudo insmod hello-1.ko

if there is noerror then your module get successfully installed

Step 7:
to verify it
$sudo dmesg

You get following result 
 

now to remove module 

$sudo rmmod hello-1.ko

to verify

$sudo dmesg

you see the kernel log as follows

 


Thank You . if you have any question comment below or email me.

Comments

Popular posts from this blog