Build a Linux Kernel Module for the Itron Riva Platform

PDF - Build a Linux Kernel Module



Using these step by step instructcions, you can build Linux kernel modules for any of the following Itron Riva devices:.
  • Itron Riva Dev Edge board
  • Itron Riva Dev Mini board
  • Itron Riva Smart Network Interface Card (NIC)
  • Itron Riva Electric Meter
  • Itron Riva Connected Grid Router (CGR) adaptive communications module, also known as the CAM

The instructions presented here assume you have the following development environment setup:

  • You have at least one of the Itron Riva Dev boards listed above
  • You have installed the Itron Riva SDK (Software Developers Kit)
  • You are using Ubuntu 16.04.  Other Linux versions should work, but these steps were performed on an Ubuntu 16.04 system
  • You are familiar with the Linux command-line prompt


By following these steps, you will create a Linux kernel module named hello.ko. You will load and unload the kernel module, observing messages from the module by using the dmesg command.

Creating the Project Directory

The first step is to open a command-line prompt

$



The next step is to change to a directory where you want to create this project. In this case, we will put this project in the Documents/Projects directory, so we change to that.

$ cd Documents/Projects
$

 


Now create a directory named hello-km. This is the directory where you will create the kernel module.

$ mkir hello-km


Finally change into the hello-km directory:

$ cd hello-km


Creating the Makefile

Next you will create a Makefile with the contents shown below. Using the text editor of your choice (e.g. vim or nano), create a file named Makefile that has the following contents:

#

# Makefile for building Itron Riva Kernel Modules

#

# For questions/support: norm.mcentire@itron.com

#

# 2017-09-17

#

obj-m := km-module.o

#km-module-objs := main.o load.o unload.o

km-module-objs := main.o

KERNELDIR := /home/nmcentire/Documents/p4client/OpenWayInternational/RIVA_OPENWAY_ARCHIMEDES_SR_4-1/Workspace/ItronRiva-Dev/build_armv7l-timesys-linux-uclibcgnueabi/linux-4.4

PWD := $(shell pwd)

all:

$(MAKE) -C $(KERNELDIR) ARCH=arm CROSS_COMPILE=armv7l-timesys-linux-uclibcgnueabi- M=$(PWD) modules

clean:

rm -rf *.ko *.o *.mod.c core Module.symvers Module.markers modules.order .*.cmd .tmp_versions



You can test your Makefile out from the command-line by typing make:

$ make

make -C /home/nmcentire/Documents/p4client/OpenWayInternational/RIVA_OPENWAY_ARCHIMEDES_SR_4-1/Workspace/SmartNic-Dev/build_armv7l-timesys-linux-uclibcgnueabi/linux-4.4 ARCH=arm CROSS_COMPILE=armv7l-timesys-linux-uclibcgnueabi- M=/home/nmcentire/Documents/Projects/hello-km modules

make[1]: Entering directory '/home/nmcentire/Documents/p4client/OpenWayInternational/RIVA_OPENWAY_ARCHIMEDES_SR_4-1/Workspace/SmartNic-Dev/build_armv7l-timesys-linux-uclibcgnueabi/linux-4.4'

Makefile:669: Cannot use CONFIG_CC_STACKPROTECTOR_REGULAR: -fstack-protector not supported by compiler

make[1]: armv7l-timesys-linux-uclibcgnueabi-gcc: Command not found

make[2]: *** No rule to make target '/home/nmcentire/Documents/Projects/hello-km/main.o', needed by '/home/nmcentire/Documents/Projects/hello-km/km-module.o'. Stop.

Makefile:1404: recipe for target '_module_/home/nmcentire/Documents/Projects/hello-km' failed

make[1]: *** [_module_/home/nmcentire/Documents/Projects/hello-km] Error 2

make[1]: Leaving directory '/home/nmcentire/Documents/p4client/OpenWayInternational/RIVA_OPENWAY_ARCHIMEDES_SR_4-1/Workspace/SmartNic-Dev/build_armv7l-timesys-linux-uclibcgnueabi/linux-4.4' Makefile:10: recipe for target 'all' failed

make: *** [all] Error 2



From the above, notice that you will receive an error. This is normal, because you have not yet created main.c

Creating main.c

Next you will create a main.c source file with the contents shown below. Using the text editor of your choice (e.g. vim or nano), create a file named main.c that has the following contents:

/*

* main.c

*

* Main file for hello-km kernel module

*

* For questions/support: norm.mcentire@itron.com

*

* 2017-09-17

*

*/

#include


//#include "load.h"

//#include "unload.h"



static int __init km_hello_module_init(void) { printk(KERN_INFO "km_hello_module_init\n"); return 0; }

module_init(km_hello_module_init); static void __exit km_hello_module_exit(void) { printk(KERN_INFO "km_hello_module_exit\n"); }

module_exit(km_hello_module_exit);

MODULE_LICENSE("GPL");

MODULE_AUTHOR("Norm McEntire
<norm.mcentire@itron.com>");

MODULE_DESCRIPTION("Itron Riva Dev Hello Kerenel Module");


You are now ready to build the Kernel Module

</norm.mcentire@itron.com>

Building the Kernel Module

You should now have two files in your project: Makefile and main.c:

$ ls

Makefile

main.c


To build the project, make sure the cross-compile toolchain is in your path:

$ echo $PATH

/home/nmcentire/itron-isom-4-1-

80/toolchain/bin:/home/nmcentire/bin:/home/nmcentire/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin


If your toolchain is not in your PATH, you can set it like this:

$ export PATH=/home/nmcentire/itron-isom-4-1-80/toolchain/bin:$PATH

After making sure your PATH is setup correctly, you can now make your project by typing make:

$ make

make -C /home/nmcentire/Documents/p4client/OpenWayInternational/RIVA_OPENWAY_ARCHIMEDES_SR_4-1/Workspace/SmartNic-Dev/build_armv7l-timesys-linux-uclibcgnueabi/linux-4.4 ARCH=arm CROSS_COMPILE=armv7l-timesys-linux-uclibcgnueabi- M=/home/nmcentire/Documents/Projects/hello-km modules

make[1]: Entering directory '/home/nmcentire/Documents/p4client/OpenWayInternational/RIVA_OPENWAY_ARCHIMEDES_SR_4-1/Workspace/SmartNic-Dev/build_armv7l-timesys-linux-uclibcgnueabi/linux-4.4'

CC [M] /home/nmcentire/Documents/Projects/hello-km/main.o

LD [M] /home/nmcentire/Documents/Projects/hello-km/km-module.o

Building modules, stage 2.

MODPOST 1 modules

CC /home/nmcentire/Documents/Projects/hello-km/km-module.mod.o

LD [M] /home/nmcentire/Documents/Projects/hello-km/km-module.ko

make[1]: Leaving directory '/home/nmcentire/Documents/p4client/OpenWayInternational/RIVA_OPENWAY_ARCHIMEDES_SR_4-1/Workspace/SmartNic-Dev/build_armv7l-timesys-linux-uclibcgnueabi/linux-4.4'