In this part, we will be configuring the BMP280 sensor module through the probe function, and reading the raw pressure and temperature value.
BMP280 Datasheet
Before jumping to the code, we should know what kind of operating modes are there, what sort of register we need to play with, to get the job done.
Mode: It can be operated in three modes:
sleep mode: As the name suggest, this mode is when sensor is sleeping.
normal mode: It comprises of automated perpetual cycling between active measurement period and a inactive standby period.
force mode: In this mode, a single measurement is performed, then it returns to sleep mode.
Register table:
Register
Description
id(0xD0)
This register contains the chip identification number which is 0x58.
reset(0xE0)
writing 0xB6 resets the chip,any other value have no effect.
ctrl_meas(0xF4)
cotrol oversampling rate of temperature and pressure, and sets operating mode.
config(0xF5)
control inactive duration, iir filter coefficient, and interface option.
press(0xF7-0xF9)
contains the raw pressure data bits output up [19:0] , in total 20 bits.
temp (0xFA-0xFC)
contains the raw temperature data bits output up to [19:0],in total 20 bits.
I2C SMBus API
To talk to the I2C-based BMP280 sensor, we will use Linux SMBus API, it's a subset of I2C that works seamlessly over the I2C bus and wraps up the lower level details like start condition, acknowledgments and register addressing into single clean function call.
s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command) read a single byte at command register address passed from the client and return the data read or error in case of failure.
s32 i2c_smbus_write_byte_data(const struct i2c_client *client,u8 command, u8 value); writes the value byte at the command register address on client device and returns 0 for success or error on failure.
s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client,u8 command, u8 length, u8 *values); allow us to read multiple bytes at once at once in the buffer pointed by the values.
code
#define ID_REG 0xD0#define RESET_REG 0xE0#define CTRL_MEAS_REG 0xF4#define CONFIG_REG 0xF5staticintbmp280_hw_init(structi2c_client*client){intret=i2c_smbus_read_byte_data(client,ID_REG);if(ret<0)returnret;elseif(ret!=0x58){pr_info("bmp2780_driver: Found, but the chip id is 0x%2x is invalid\n",ret);return-ENODEV;}ret=i2c_smbus_write_byte_data(client,RESET_REG,0xB6);usleep_range(2000,3000);if(ret<0)returnret;ret=i2c_smbus_write_byte_data(client,CONFIG_REG,0x10);ret|=i2c_smbus_write_byte_data(client,CTRL_MEAS_REG,0x57);returnret;}
In bmp280_hw_init function, we initialize the BMP280 sensor chip, where we do the following things:
Read the chip id stored at the ID_REG, verify as per the datasheet it should read 0x58.
Reset the chip before we configure it, writing 0xB6 at RESET_REG performs power-on-reset, and usleep(2000,3000) gives the sensor the 2ms startup time as per the datasheet. After the successful reset the chip enters the sleep mode.
Below table is for selection of the value of the ctrl_meas register to be 0x57. This register sets the data acquistion of the device.
Field
Bit
Value
What it means
osrs_t[2:0]
7,6,5
010
This sets oversampling for temperature to 2.
osrs_p[2:0]
4,3,2
101
This sets oversampling for pressure to 16.
mode[1:0]
1,0
11
This sets the operating mode to normal.
Below table shows the selection of the value of the config register to 0x10. This register sets the standby period for the normal mode,time constant of iir filter and interface option.
Field
Bit
Value
What it means
t_sb[2:0]
7,6,5
000
This sets the standby time to 0.5ms.
filter[2:0]
4,3,2
100
This sets the iir filter value to 16.
spi3w_en[0]
0
0
Disable 3 wire spi interface when set to 0.
Tip
The reason i selected those particular values, is
based on the BMP280 datasheet. In the section 3.8.2, Table 15, Bosch provides recommended setting for indoor naviagtion,which specify the exact oversampling, standby time, and IIR values to use for these registers.
The config register must be set before the control_meas register due to if the sensor is operating in normal mode, writing to config register have no effect.
statics32get_bmp280_tempertaure(structi2c_client*client){u8buffer[6]={0};intret=i2c_smbus_read_i2c_block_data(client,0xF7,6,buffer);if(ret<0){pr_err("bmp280_driver: Failed to read I2C bus: %d\n",ret);returnret;}return((u32)buffer[3]<<12)|((u32)buffer[4]<<4)|((u32)buffer[5]>>4);}
This function is very similar to the static s32 get_bmp280_pressure(struct i2c_client *client) , it reads the raw value of the temperature. We do the burst read 0xF7 to 0xFC in one go and not each register individually. This is done to ensure the pressure and temperature stay synchronized.
The data are 20 bit wide across three bytes(msb,lsb and top 4 bits of xlsb), so we shift and OR them in one 32 bit integer value.
staticintmy_bmp280_probe(structi2c_client*client){intret=bmp280_hw_init(client);if(ret<0){pr_err("bmp280_driver: Hardware initialization failed: %d\n",ret);returnret;}s32temp=get_bmp280_tempertaure(client);s32press=get_bmp280_pressure(client);pr_info("bmp280_driver:Press raw data read: %d\n",press);pr_info("bmp280_driver:Temp raw data read: %d\n",temp);pr_info("bmp280_driver:Driver initialized sucessfully\n");return0;}staticvoidmy_bmp280_remove(structi2c_client*client){i2c_smbus_write_byte_data(client,RESET_REG,0xB6);usleep_range(2000,3000);pr_info("Calling the bmp280 driver remove successful\n");}
Now probe() actually talks to the chip, waking it up, configuring it, and printing the raw ADC value to dmesg.
When we do rmmod the driver, remove() writes 0xB6 to RESET_REG to reset the sensor back into low power sleep mode.
#include <linux/init.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/i2c.h>#include <linux/delay.h>#include <linux/sysfs.h>MODULE_LICENSE("GPL");MODULE_AUTHOR("Aadarsh Mandal");MODULE_DESCRIPTION("A simple bmp280 i2c device driver");MODULE_VERSION("0.1");#define ID_REG 0xD0#define RESET_REG 0xE0#define CTRL_MEAS_REG 0xF4#define CONFIG_REG 0xF5#define CALIB_REG 0x88/**
* This function reads the raw temperature value from the bmp280 data register.
* */statics32get_bmp280_tempertaure(structi2c_client*client){u8buffer[6]={0};intret=i2c_smbus_read_i2c_block_data(client,0xF7,6,buffer);if(ret<0){pr_err("bmp280_driver: Failed to read from I2C bus! Error code: %d\n",ret);returnret;}return((u32)buffer[3]<<12)|((u32)buffer[4]<<4)|((u32)buffer[5]>>4);}/**
* This function reads the raw pressure value from the bmp280 data register.
* */statics32get_bmp280_pressure(structi2c_client*client){u8buffer[6]={0};intret=i2c_smbus_read_i2c_block_data(client,0xF7,6,buffer);if(ret<0){pr_err("bmp280_driver: Failed to read from I2C bus! Error code: %d\n",ret);returnret;}return((u32)buffer[0]<<12)|((u32)buffer[1]<<4)|((u32)buffer[2]>>4);}/**
* Bmp280 sensor initialisation process:
* 1. Read the chip id and verify.
* 2. Reset the chip before configuring.
* 3. Configure control register (0xF4).
* 4. Configure status register (0xF5).
* 5. Verfiying the configurations.
*/staticintbmp280_hw_init(structi2c_client*client){intret=i2c_smbus_read_byte_data(client,ID_REG);if(ret<0)returnret;elseif(ret!=0x58){pr_info("bmp2780_driver: Found, but the chip id is 0x%2x is invalid\n",ret);return-ENODEV;}pr_info("bmp280_driver: Chip id: 0x%2x is valid\n",ret);ret=i2c_smbus_write_byte_data(client,RESET_REG,0xB6);usleep_range(2000,3000);if(ret<0)returnret;pr_info("bmp280_driver: Reset status sucessfull");/**
* Selecting the standby period = 0.5ms for the normal mode, and also setting
* the IIR filter coefficinet value=16,
*/ret=i2c_smbus_write_byte_data(client,CONFIG_REG,0x10);if(ret<0)returnret;pr_info("bmp280_driver: Configured the standby time and iir coefficient sucessfully\n");/**
* configuring the control register to choose
* the indoor navigation mode
* 1. mode : Normal mode
* 2. temp oversampling : 2
* 3. pressure oversampling: 16
* so the hex value for this register we have to enter will
* be 0x57.
* */ret=i2c_smbus_write_byte_data(client,CTRL_MEAS_REG,0x57);if(ret<0)returnret;pr_info("bmp280_driver: Configured the control register sucessfully\n");return0;}staticintmy_bmp280_probe(structi2c_client*client){intret=bmp280_hw_init(client);if(ret<0){pr_err("bmp280_driver: Hardware initialization failed: %d\n",ret);returnret;}/* give sensor some time to complete its measurement cycle */msleep(1000);s32temp=get_bmp280_tempertaure(client);s32press=get_bmp280_pressure(client);pr_info("bmp280_driver:Press raw data read: %d\n",press);pr_info("bmp280_driver:Temp raw data read: %d\n",temp);pr_info("bmp280_driver:Driver initialized sucessfully\n");return0;}/* This should put the sensor in the sleep mode */staticvoidmy_bmp280_remove(structi2c_client*client){i2c_smbus_write_byte_data(client,RESET_REG,0xB6);usleep_range(2000,3000);pr_info("Calling the bmp280 driver remove sucessful\n");}staticconststructof_device_idbmp280_of_match[]={{.compatible="custom,my-bmp280"},{}};MODULE_DEVICE_TABLE(of,bmp280_of_match);staticconststructi2c_device_idbmp280_i2c_id[]={{"my-bmp280",0},{}};MODULE_DEVICE_TABLE(i2c,bmp280_i2c_id);staticstructi2c_drivermy_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);
Testing
Run make, build the module and transfer it to on your raspberry pi board.
# echo my-bmp280 0x76 > /sys/bus/i2c/devices/i2c-1/new_device[ 2405.451421] i2c i2c-1: new_device: Instantiated device my-bmp280 at 0x76
# insmod bmp280.ko[ 2409.948698] bmp280_driver: Chip id: 0x58 is valid
[ 2409.955977] bmp280_driver: Reset status sucessfull
[ 2409.956288] bmp280_driver: Configured the standby time and iir coefficient sucessfully
[ 2409.969339] bmp280_driver: Configured the control register sucessfully
[ 2411.008968] bmp280_driver:Press raw data read: 416923
[ 2411.014056] bmp280_driver:Temp raw data read: 514848
[ 2411.019088] bmp280_driver:Driver initialized sucessfully
# echo 0x76 > /sys/bus/i2c/devices/i2c-1/delete_device[ 2416.967212] i2c i2c-1: delete_device: Deleting device my-bmp280 at 0x76
[ 2416.977283] Calling the bmp280 driver remove sucessful
What's next?
In the final part 4, we will write a function that converts the raw ADC values to real world scale and expose it to user space via sysfs.
Top comments (0)