Overview
Coding and cross compiling driver
- The setup is based like this, write code on our laptop, use the cross compiler to compile our code and produce the final binary that runs on target board. To compile our code you need a raspberry pi compatible compiler which is
aarch64-linux-gccbecause your laptop is based onx86processor while the soc is based on ARM, so the nativegcccompiler output will not run. You have to use thescpto transfer the compiled binary to the board once it boots up.
static int my_bmp280_probe(struct i2c_client *client){
pr_info("Using bmp280 driver probing\n");
return 0;
}
static void my_bmp280_remove(struct i2c_client *client){
pr_info("Calling the bmp280 driver remove sucessful\n");
}
- This function
my_bmp280_proberuns as soon as kernel matches our driver to the hardware. The purpose of this function is to initialize the chip,or configure its working mode before we start reading data. On the flip sdiemy_bmp280_removeis the remove function when you callrmmodor device goes aways this function is called, this function job is to put the chip or sensor to sleep or free any allocated memory.
static const struct of_device_id bmp280_of_match[]={
{.compatible = "custom,my-bmp280"},
{}
};
MODULE_DEVICE_TABLE(of,bmp280_of_match);
static const struct i2c_device_id bmp280_i2c_id[] = {
{"my-bmp280",0},
{}
};
MODULE_DEVICE_TABLE(i2c,bmp280_i2c_id);
- Since I2C devices aren't self-discoverable. We give the Linux kernel two ways to match our driver:
- This struct
bmp280_of_matchcontains the list of compatible strings ("custom,my-bmp280"), and is used by the device tree. - This struct
bmp280_i2c_idalso contains the list of plain device names ("my-bmp280"), and is used when we manually instantiate device bysysfs.
- The
MODULE_DEVICE_TABLEthis macro helps embeds those id table into the compiled module metadata at build time, allowing Linux to auto load your driver when a matching hardware device appears. - In a device tree file, we describe our hardware and its properties,for example if we were describing this BMP280 I2C sensor module, we will be describing where its attached, its address, and its compatible string,clock speed etc. When your kernel is booting this
.dtbfile is passed, to tell our kernel what sort of hardware this device contains rather than hardcoding such info in the kernel itself. This is the idea the kernel looks for the compatible string from this device tree file, and if it finds a driver with the same compatible string it then proceeds to bind the device with this driver , and then calls your probe function.
static struct i2c_driver my_bmp280_driver = {
.driver = {
.name = "bmp280_driver",
.of_match_table= bmp280_of_match
},
.probe = my_bmp280_probe,
.remove = my_bmp280_remove,
.id_table = bmp280_i2c_id,
};
module_i2c_driver(my_bmp280_driver);
- The
my_bmp280_driverstruct represent an I2C device driver, ties everything together and pointing the kernel to its probe function,remove function, its id table, name so on. - This
module_i2c_driveris helper macro that replaces the boilerplate module_init() and module_exit(), these two function are the entry and exit point of driver and runs once. Under the hood, it automatically registers our driver withi2c_add_driver()when the module loads and unregisters it withi2c_del_driver()when module unloaded. - Here is full code:
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/i2c.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Aadarsh Mandal");
MODULE_DESCRIPTION("A simple bmp280 i2c device driver");
MODULE_VERSION("0.1");
static int my_bmp280_probe(struct i2c_client *client){
pr_info("Using bmp280 driver probing\n");
return 0;
}
static void my_bmp280_remove(struct i2c_client *client){
pr_info("Calling the bmp280 driver remove sucessful\n");
}
static const struct of_device_id bmp280_of_match[]={
{.compatible = "custom,my-bmp280"},
{}
};
MODULE_DEVICE_TABLE(of,bmp280_of_match);
static const struct i2c_device_id bmp280_i2c_id[] = {
{"my-bmp280",0},
{}
};
MODULE_DEVICE_TABLE(i2c,bmp280_i2c_id);
static struct i2c_driver my_bmp280_driver = {
.driver = {
.name = "bmp280_driver",
.of_match_table= bmp280_of_match
},
.probe = my_bmp280_probe,
.remove = my_bmp280_remove,
.id_table = bmp280_i2c_id,
};
module_i2c_driver(my_bmp280_driver);
Compiling the code :
- Replace
KDIRwith the path to your compiled Raspberry Pi kernel headers/build tree (for Buildroot, it lives insideoutput/build/linux-<version>/;on Raspberry Pi OS, it's/lib/modules/$(uname -r)/build). - Replace
CROSS_COMPILEwith the prefix of yourAArch64toolchain (for example,aarch64-linux-gnu-if you installed the distro package on Ubuntu, or the Buildroot host toolchain path).
# 1. Tell the Linux kernel build system what object file to create.
# The kernel looks for 'obj-m' to know what needs to be compiled as a module.
obj-m += bmp280.o
# 2. Define where the compiled kernel source tree lives inside Buildroot.
KDIR ?= /mnt/data/buildroot/output/build/linux-custom
# 3. Define the cross-compiler toolchain built by Buildroot.
ARCH ?= arm64
CROSS_COMPILE ?= /mnt/data/buildroot/output/host/bin/aarch64-linux-
all:
$(MAKE) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KDIR) M=$(PWD) modules
clean:
$(MAKE) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KDIR) M=$(PWD) clean
- Run make, if everything builds cleanly you will get
bmp280.koin your project folder.
make
# Or,To override on command line, pass appropriate path
make KDIR=/path/kernel/dir CROSS_COMPILE=/path/to/compiler
- Run the following command, to transfer your compiled binary to the board. where the root is the username of ssh,and ip address seperated by colon to the path where you want it.
scp /path/to/bmp280.ko <ssh_user>@<your_ip>:/path/
Warning
The module
bmp280.komodule can cause conflict with kernel intree BMP280 module. You need to change the name of the module to something else likemy-bmp280.komodule.
Loading and unloading driver module
# To load the driver module run the following command
insmod bmp280.ko
# To unload the driver module
rmmod bmp280.ko
# To view the loaded modules, following command list all loaded modules
lsmod
# command to manually trigger the device probe function from the command line
echo my-bmp280 0x76 > /sys/bus/i2c/devices/i2c-1/new_device
## to remove
echo 0x76 > /sys/bus/i2c/devices/i2c-1/delete_device
Reading kernel logs
# echo my-bmp280 0x76 > /sys/bus/i2c/devices/i2c-1/new_device
# insmod bmp280.ko
# dmesg | tail
[ 368.302404] i2c i2c-1: new_device: Instantiated device my-bmp280 at 0x76
[ 368.325831] IPv6: MLD: clamping QRV from 1 to 2!
[ 476.338000] IPv6: MLD: clamping QRV from 1 to 2!
[ 476.342996] IPv6: MLD: clamping QRV from 1 to 2!
[ 524.354602] IPv6: MLD: clamping QRV from 1 to 2!
[ 524.359394] IPv6: MLD: clamping QRV from 1 to 2!
[ 530.355631] IPv6: MLD: clamping QRV from 1 to 2!
[ 530.360413] IPv6: MLD: clamping QRV from 1 to 2!
[ 534.274593] Using bmp280 driver probing
# rmmod bmp280.ko
# dmesg | tail
[ 674.400300] IPv6: MLD: clamping QRV from 1 to 2!
[ 674.405455] IPv6: MLD: clamping QRV from 1 to 2!
[ 680.239479] Calling the bmp280 driver remove sucessful
- If you get the same logs while loading and unloading the driver module , means your driver got bind with your device successfully.

Top comments (2)
This is a clean walkthrough, and the most useful line in it is the warning about the in-tree BMP280 module — renaming to my-bmp280.ko is exactly the kind of thing that costs an evening if nobody tells you. One thing I'd flag: your probe prints 'Using bmp280 driver probing' and returns 0 without ever reading the chip, so the dmesg test proves the driver bound, not that a BMP280 is actually at 0x76. A wrong address or a different chip on the bus would pass the same test. The natural next step is reading the ID register in probe and failing the bind when it doesn't match — then the log line actually means something. I see Part 3 is already up — does it add the ID check and the first real data read, or is that still to come?
yes it does, actually i dont want it t overload to the people who don't have much idea ,so doing it in the parts so its easy to understand, and in the next part 3 i do the id check, configuring the sensor its operating mode etc before reading a value, Thanks for pointing out.