/linux-kernel-module-template

Template for Linux kernel module

Primary LanguageCGNU General Public License v3.0GPL-3.0

linux-kernel-module-template

  • master:
    Build Status

  • develop:
    Build Status

Overview

00.hello

Hello World of linux kernel module. Print a message to kernel's message buffer on the module loaded/unloaded, open()/close(), write()/read().

01.ktimer

Kernel Timer Template. Timer handler using kernel timer is periodically executed, and it increments counter every 100ms.

02.hrtimer

High-resolution Timer Template. Timer handler using high-resolution timer is periodically executed, and it increments counter every 100ms.

03.i2c

I2C Client Driver Template. Example of L3GD20 3-axis gyro sensor.

04.SPI

SPI Client Driver Template. Example of L3GD20 3-axis gyro sensor.

05.iomemmap

I/O memory remap/unmap Template. Monitor memory of specified physical address and display these values.

Install

Self Compiling (for Ubuntu, etc)

Download linux-headers:

$ sudo apt-get install linux-headers-`uname -r`

Make a kernel module:

$ make

Cross Compiling (for Raspberry Pi)

Build cross compilation environment (refer to the official documentation for details).

On 32bit Linux:

$ git clone --depth=1 https://github.com/raspberrypi/linux
$ git clone --depth=1 https://github.com/raspberrypi/tools
$ cd linux
$ KERNEL=kernel7
$ make ARCH=arm CROSS_COMPILE=../tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf- bcm2709_defconfig
$ make ARCH=arm CROSS_COMPILE=../tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf- zImage modules dtbs

On 64bit Linux:

$ git clone --depth=1 https://github.com/raspberrypi/linux
$ git clone --depth=1 https://github.com/raspberrypi/tools
$ cd linux
$ KERNEL=kernel7
$ make ARCH=arm CROSS_COMPILE=../tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf- bcm2709_defconfig
$ make ARCH=arm CROSS_COMPILE=../tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf- zImage modules dtbs

Change first and second lines of Makefile.raspberrypi (KPATH, TPATH) according to download path on the previous step.

Make a kernel module:

$ make -f Makefile.raspberrypi

Cross Compiling (for Digilent Board)

Download and Install Vivado Design Suite HLx Edition.

Add bellow line to your .bashrc in order to add cross-compiler path (change version according to your Vivado):

export PATH=$PATH:/opt/Xilinx/SDK/2017.1/gnu/arm/lin/bin

Build cross compilation environment.

$ git clone -b master-next https://github.com/DigilentInc/Linux-Digilent-Dev.git
$ cd Linux-Digilent-Dev
$ make ARCH=arm CROSS_COMPILE=arm-xilinx-linux-gnueabi- xilinx_zynq_defconfig
$ make ARCH=arm CROSS_COMPILE=arm-xilinx-linux-gnueabi-

Change first line of Makefile.digilent (KPATH) according to download path on the previous step.

Make a kernel module:

$ make -f Makefile.digilent

Usage

00.hello

Load the kernel module:

$ sudo insmod hello.ko

Can see the loaded kernel module:

$ lsmod | grep hello

Can see exported valiable:

$ cat /sys/module/hello/parameters/g_dev_major
$ cat /sys/module/hello/parameters/g_dev_minor

Unload the kernel module:

$ sudo rmmod hello

01.ktimer

Load the kernel module:

$ sudo insmod ktimer.ko

Can see the loaded kernel module:

$ lsmod | grep ktimer

Can see exported valiable:

$ cat /sys/module/ktimer/parameters/g_counter

Unload the kernel module:

$ sudo rmmod ktimer

02.hrtimer

Load the kernel module:

$ sudo insmod hrtimer.ko

Can see the loaded kernel module:

$ lsmod | grep hrtimer

Can see exported valiable:

$ cat /sys/module/hrtimer/parameters/g_counter

Unload the kernel module:

$ sudo rmmod hrtimer

03.i2c & 04.spi

Move the kernel module into kernel directory, and update module information:

$ sudo make install

Connect I2C/SPI device and notify it to I2C/SPI core driver:

$ sudo make connect

Can read raw values of 3-axis gyro sensor:

$ sudo make read

(comma-delimited, X-axis, Y-axis, and Z-axis)

Disconnect I2C/SPI device:

$ sudo make disconnect

Remove the kernel module from the kernel directory, and update module information:

$ sudo make uninstall

05.iomemmap

Load the kernel module:

$ sudo insmod iomemmap.ko

Specify monitoring physical address and size with HEX-format (addr=0x41200000, size=0x8):

$ sudo echo 41200000,8 > /dev/05.iomemmap0

Can monitor these values:

$ sudo cat /dev/05.iomemmap0

Unload the kernel module:

$ sudo rmmod iomemmap

License

MIT and GPL

Author

T. Ngtk