DEV Community

Aadarsh Mandal
Aadarsh Mandal

Posted on

My First Linux I2C Driver: BMP280 on Raspberry Pi 4, Part 2

Overview

overview of this article

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-gcc because your laptop is based on x86 processor while the soc is based on ARM, so the native gcc compiler output will not run. You have to use the scp to 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");
}
Enter fullscreen mode Exit fullscreen mode
  • This function my_bmp280_probe runs 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 sdie my_bmp280_remove is the remove function when you call rmmod or 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);
Enter fullscreen mode Exit fullscreen mode
  • Since I2C devices aren't self-discoverable. We give the Linux kernel two ways to match our driver:
  1. This struct bmp280_of_match contains the list of compatible strings ("custom,my-bmp280"), and is used by the device tree.
  2. This struct bmp280_i2c_id also contains the list of plain device names ("my-bmp280"), and is used when we manually instantiate device by sysfs.
  • The MODULE_DEVICE_TABLE this 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 .dtb file 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);
Enter fullscreen mode Exit fullscreen mode
  • The my_bmp280_driver struct 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_driver is 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 with i2c_add_driver() when the module loads and unregisters it with i2c_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);
Enter fullscreen mode Exit fullscreen mode

Compiling the code :

  • Replace KDIR with the path to your compiled Raspberry Pi kernel headers/build tree (for Buildroot, it lives inside output/build/linux-<version>/; on Raspberry Pi OS, it's /lib/modules/$(uname -r)/build).
  • Replace CROSS_COMPILE with the prefix of your AArch64 toolchain (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
Enter fullscreen mode Exit fullscreen mode
  • Run make, if everything builds cleanly you will get bmp280.ko in your project folder.
make
# Or,To override on command line, pass appropriate path
make KDIR=/path/kernel/dir CROSS_COMPILE=/path/to/compiler
Enter fullscreen mode Exit fullscreen mode
  • 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/
Enter fullscreen mode Exit fullscreen mode

Warning

The module bmp280.ko module can cause conflict with kernel intree BMP280 module. You need to change the name of the module to something else like my-bmp280.ko module.

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • If you get the same logs while loading and unloading the driver module , means your driver got bind with your device successfully.

Important link

Top comments (2)

Collapse
 
userxnom profile image
Abdullah Naseer •

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?

Collapse
 
codedbyaadarsh profile image
Aadarsh Mandal •

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.