Skip to content

Instantly share code, notes, and snippets.

@colerd24
Last active November 24, 2019 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colerd24/01eb5d459a7b527dbff22d3ad7685b88 to your computer and use it in GitHub Desktop.
Save colerd24/01eb5d459a7b527dbff22d3ad7685b88 to your computer and use it in GitHub Desktop.
1) Install necessary dependencies:
Raspberry Pi: raspberrypi-kernel-headers
Ubuntu: build-essential linux-headers-`uname -r`
2) Create build directory:
mkdir ~/lkm_test
cd ~/lkm_test
3) Create example lkm_test.c file:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE(“MIT”);
MODULE_AUTHOR(“Your name here”);
MODULE_DESCRIPTION(“Test Linux kernel module”);
MODULE_VERSION(“0.01”);
static int __init lkm_test_init(void) {
printk(KERN_INFO “Hello world!\n”);
return 0;
}
static void __exit lkm_test_exit(void) {
printk(KERN_INFO “Goodbye cruel World!\n”);
}
module_init(lkm_example_init);
module_exit(lkm_example_exit);
4) Create makefile (Make sure tabs are correct):
obj-m += lkm_test.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
test:
sudo dmesg -C
sudo insmod lkm_test.ko
sudo rmmod lkm_test.ko
dmesg
5) Run make
6) Load: sudo insmod lkm_test.ko
7) Check kernel log for output: sudo dmesg
8) Check to see if module is loaded: lsmod | grep “lkm_test”
9) Remove module: sudo rmmod lkm_test
Board ID Example:
http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/board-info.html
https://github.com/ev3dev/lego-linux-drivers/blob/ev3dev-buster/linux/board_info/board_info.h
https://github.com/ev3dev/lego-linux-drivers/blob/ev3dev-buster/ev3/ev3_board.c
Kernel Debugging:
https://sysprogs.com/VisualKernel/tutorials/raspberry/leddriver/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment