-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbme280.c
394 lines (325 loc) · 14 KB
/
bme280.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright (C) 2024 Aleksei Rogov <alekzzzr@gmail.com>. All rights reserved.
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include "bme280.h"
#define DRIVER_NAME "bme280"
#define DRIVER_CLASS "bme280_class"
#define I2C_BUS_AVAILABLE 1
#define SLAVE_DEVICE_NAME "bme280"
#define BME280_SLAVE_ADDRESS 0x76
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Aleksei Rogov");
MODULE_DESCRIPTION("BME280 driver");
static dev_t dev_num;
static struct class *bme280_class;
static struct cdev bme280_device;
static struct i2c_adapter *bme_i2c_adapter = NULL;
static struct i2c_client *bme280_i2c_client = NULL;
static int i2c_addr = BME280_SLAVE_ADDRESS;
module_param(i2c_addr, int, 0660);
MODULE_PARM_DESC(i2c_addr, "bme280 i2c address (default is 0x76)");
static int i2c_bus = I2C_BUS_AVAILABLE;
module_param(i2c_bus, int, 0660);
MODULE_PARM_DESC(i2c_bus, "i2c bus to use (default is 1)");
static const struct i2c_device_id bme_id[] = {
{ SLAVE_DEVICE_NAME, 0},
{}
};
static struct i2c_driver bme_driver = {
.driver = {
.name = SLAVE_DEVICE_NAME,
.owner = THIS_MODULE
}
};
static struct i2c_board_info bme_board_info = {
I2C_BOARD_INFO(SLAVE_DEVICE_NAME, BME280_SLAVE_ADDRESS)
};
struct bme280_calibration_t bme280_calibration_t;
int32_t t_fine;
// Returns temperature in DegC, resolution is 0.01 DegC.
// Output value of “5123” equals 51.23 DegC.
static int32_t bme280_read_compensate_temperature_int32(void) {
int32_t adc_T, var1, var2, t;
int32_t d1, d2, d3;
// Read temperature register
if((d1 = i2c_smbus_read_byte_data(bme280_i2c_client, BME280_REGISTER_TEMP_MSB)) < 0
|| (d2 = i2c_smbus_read_byte_data(bme280_i2c_client, BME280_REGISTER_TEMP_LSB)) < 0
|| (d3 = i2c_smbus_read_byte_data(bme280_i2c_client, BME280_REGISTER_TEMP_XLSB)) < 0) {
return BUS_ERROR;
}
// Temperature register contains reset values
if(d1 == 0x80 && d2 == 0x0 && d3 == 0x0) {
return INCORRECT_TEMPERATURE;
}
adc_T = (d1 << 12) | (d2 << 4) | (d3 >> 4);
// Calculate temperature according the "4.2.3 Compensation formulas"
var1 = ((((adc_T >> 3) - ((int32_t)bme280_calibration_t.dig_T1 << 1))) * ((int32_t)bme280_calibration_t.dig_T2)) >> 11;
var2 = (((((adc_T >> 4) - ((int32_t)bme280_calibration_t.dig_T1)) * ((adc_T >> 4)
- ((int32_t)bme280_calibration_t.dig_T1))) >> 12) * ((int32_t)bme280_calibration_t.dig_T3)) >> 14;
t_fine = var1 + var2;
t = (t_fine * 5 + 128) >> 8;
return t;
}
// Returns pressure in Pa as unsigned 32 bit integer.
// Output value of “96386” equals 96386 Pa = 963.86 hPa
static int32_t bme280_read_compensate_pressure_int32(void) {
uint32_t p;
int32_t adc_P, d1, d2, d3, var1, var2;
// Read pressure register
if((d1 = i2c_smbus_read_byte_data(bme280_i2c_client, BME280_REGISTER_PRESS_MSB)) < 0
|| (d2 = i2c_smbus_read_byte_data(bme280_i2c_client, BME280_REGISTER_PRESS_LSB)) < 0
|| (d3 = i2c_smbus_read_byte_data(bme280_i2c_client, BME280_REGISTER_PRESS_XLSB)) < 0) {
return BUS_ERROR;
}
// Pressure register contains reset values
if(d1 == 0x80 && d2 == 0x0 && d3 == 0x0) {
return INCORRECT_PRESSURE;
}
adc_P = (d1 << 12) | (d2 << 4) | (d3 >> 4);
var1 = (((int32_t)t_fine) >> 1) - (int32_t)64000;
var2 = (((var1 >> 2) * (var1 >> 2)) >> 11 ) * ((int32_t)bme280_calibration_t.dig_P6);
var2 = var2 + ((var1 * ((int32_t)bme280_calibration_t.dig_P5)) << 1);
var2 = (var2 >> 2) + (((int32_t)bme280_calibration_t.dig_P4) << 16);
var1 = (((bme280_calibration_t.dig_P3 * (((var1 >> 2) * (var1 >> 2)) >> 13 )) >> 3) + ((((int32_t)bme280_calibration_t.dig_P2) * var1) >> 1)) >> 18;
var1 =((((32768 + var1)) * ((int32_t)bme280_calibration_t.dig_P1)) >> 15);
if (var1 == 0) {
return 0;
}
p = (((uint32_t)(((int32_t)1048576) - adc_P) - (var2 >> 12))) * 3125;
if (p < 0x80000000) {
p = (p << 1) / ((uint32_t)var1);
}
else {
p = (p / (uint32_t)var1) * 2;
}
var1 = (((int32_t)bme280_calibration_t.dig_P9) * ((int32_t)(((p >> 3) * (p >> 3)) >> 13))) >> 12;
var2 = (((int32_t)(p >> 2)) * ((int32_t)bme280_calibration_t.dig_P8)) >> 13;
p = (uint32_t)((int32_t)p + ((var1 + var2 + bme280_calibration_t.dig_P7) >> 4));
return p;
}
// Returns humidity in %RH as unsigned 32 bit integer in Q22.10 format (22 integer and 10fractional bits).
// Output value of “47445” represents 47445/1024 = 46.333 %RH
static int32_t bme280_read_compensate_humidity_int32(void) {
int32_t v_x1_u32r, adc_H;
int32_t d1, d2;
// Read humidity register
if((d1 = i2c_smbus_read_byte_data(bme280_i2c_client, BME280_REGISTER_HUM_MSB)) < 0
|| (d2 = i2c_smbus_read_byte_data(bme280_i2c_client, BME280_REGISTER_HUM_LSB)) < 0) {
return BUS_ERROR;
}
// Humidity register contains reset values
if(d1 == 0x80 && d2 == 0x0) {
return INCORRECT_HUMIDITY;
}
adc_H = (d1 << 8) | d2;
v_x1_u32r = (t_fine - ((int32_t)76800));
v_x1_u32r = (((((adc_H << 14) - (((int32_t)bme280_calibration_t.dig_H4) << 20) - (((int32_t)bme280_calibration_t.dig_H5) * v_x1_u32r))
+ ((int32_t)16384)) >> 15) * (((((((v_x1_u32r * ((int32_t)bme280_calibration_t.dig_H6)) >> 10)
* (((v_x1_u32r * ((int32_t)bme280_calibration_t.dig_H3)) >> 11) + ((int32_t)32768))) >> 10) + ((int32_t)2097152))
* ((int32_t)bme280_calibration_t.dig_H2) + 8192) >> 14));
v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * ((int32_t)bme280_calibration_t.dig_H1)) >> 4));
v_x1_u32r = (v_x1_u32r < 0 ? 0 : v_x1_u32r);
v_x1_u32r = (v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r);
return (uint32_t)(v_x1_u32r >> 12);
}
static int init_sensor(void) {
int8_t id;
if((id = i2c_smbus_read_byte_data(bme280_i2c_client, BME280_REGISTER_ID)) < 0) {
return BUS_ERROR;
}
if(id != BME280_CHIP_ID) {
printk(KERN_ERR "bme280: chip id should be %d not %d\n", BME280_CHIP_ID, id);
return CHIP_ID_ERROR;
}
printk(KERN_DEFAULT "bme280: sensor detected on i2c-%d at address 0x%x\n", i2c_bus, i2c_addr);
uint32_t ret;
bme280_calibration_t.dig_T1 = ret = i2c_smbus_read_word_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_T1);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_T2 = ret = i2c_smbus_read_word_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_T2);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_T3 = ret = i2c_smbus_read_word_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_T3);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_P1 = ret = i2c_smbus_read_word_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_P1);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_P2 = ret = i2c_smbus_read_word_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_P2);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_P3 = ret = i2c_smbus_read_word_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_P3);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_P4 = ret = i2c_smbus_read_word_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_P4);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_P5 = ret = i2c_smbus_read_word_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_P5);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_P6 = ret = i2c_smbus_read_word_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_P6);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_P7 = ret = i2c_smbus_read_word_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_P7);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_P8 = ret = i2c_smbus_read_word_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_P8);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_P9 = ret = i2c_smbus_read_word_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_P9);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_H1 = ret = i2c_smbus_read_byte_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_H1);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_H2 = ret = i2c_smbus_read_word_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_H2);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_H3 = ret = i2c_smbus_read_byte_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_H3);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_H4 = ret = i2c_smbus_read_byte_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_H4);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_H5 = ret = i2c_smbus_read_byte_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_H5);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_H6 = ret = i2c_smbus_read_byte_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_H5 + 1);
if(ret < 0) {
return BUS_ERROR;
}
bme280_calibration_t.dig_H4 = (bme280_calibration_t.dig_H4 << 4) | (bme280_calibration_t.dig_H5 & 0xF);
bme280_calibration_t.dig_H5 = (bme280_calibration_t.dig_H6 << 4) | ((bme280_calibration_t.dig_H5 >> 4) & 0xF);
bme280_calibration_t.dig_H6 = ret = i2c_smbus_read_byte_data(bme280_i2c_client, CALIBRATION_REGISTER_DIG_H6);
if(ret < 0) {
return BUS_ERROR;
}
// Initialize device
if(i2c_smbus_write_byte_data(bme280_i2c_client, BME280_REGISTER_CTRL_HUM, BME280_OSRS_H_1) != 0) {
return BUS_ERROR;
}
if(i2c_smbus_write_byte_data(bme280_i2c_client, BME280_REGISTER_CTRL_MEAS, BME280_OSRS_MODE_NORMAL | BME280_OSRS_P_1 | BME280_OSRS_T_1) != 0) {
return BUS_ERROR;
}
if(i2c_smbus_write_byte_data(bme280_i2c_client, BME280_REGISTER_CONFIG, BME280_T_SB_1000) != 0) {
return BUS_ERROR;
}
printk(KERN_DEFAULT "bme280: id: 0x%X, callibrations: {H1: %d, H2: %d, H3: %d, H4: %d, H5: %d, H6: %d}\n", id, bme280_calibration_t.dig_H1, bme280_calibration_t.dig_H2,\
bme280_calibration_t.dig_H3, bme280_calibration_t.dig_H4,\
bme280_calibration_t.dig_H5, bme280_calibration_t.dig_H6);
return 0;
}
static ssize_t driver_read(struct file *fd, char *user_buffer, size_t count, loff_t *offs) {
int to_copy, not_copied;
int32_t temperature, pressure, humidity, bytes;
char buff[128];
to_copy = min(sizeof(buff), count);
if((temperature = bme280_read_compensate_temperature_int32()) == BUS_ERROR
|| (pressure = bme280_read_compensate_pressure_int32()) == BUS_ERROR
|| (humidity = bme280_read_compensate_humidity_int32()) == BUS_ERROR ) {
temperature = pressure = humidity = 0;
printk(KERN_ERR "bme280: bus error occured\n");
}
if(temperature == INCORRECT_TEMPERATURE && pressure == INCORRECT_PRESSURE && humidity == INCORRECT_HUMIDITY) {
printk(KERN_WARNING "bme280: reinitialize sensor\n");
if(init_sensor() != 0) {
printk(KERN_ERR "bme280: can't reinitialize sensor\n");
}
temperature = pressure = humidity = 0;
}
bytes = snprintf(buff, sizeof(buff), "{\"temperature\": %d.%d, \"pressure\": %d, \"humidity\": %d.%d}\n",\
temperature / 100, temperature % 100, pressure, humidity >> 10, ((humidity % 1024) * 100) >> 10 );
not_copied = copy_to_user(user_buffer, buff, to_copy);
return to_copy - not_copied;
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.read = driver_read,
};
static int __init bme280_init(void) {
printk(KERN_DEFAULT "bme280: module loaded\n");
bme_board_info.addr = i2c_addr;
if(alloc_chrdev_region(&dev_num, 0, 1, DRIVER_NAME) < 0){
printk(KERN_ERR "bme280: device number can't be allocated\n");
return -1;
}
printk(KERN_DEFAULT "bme280: allocated device (major: %d, minor: %d)\n", dev_num >> 20, dev_num && 0xfffff);
if((bme280_class = class_create(DRIVER_CLASS)) == NULL) {
printk(KERN_ERR "bme280: device class can't be crated\n");
goto ClassError;
}
if(device_create(bme280_class, NULL, dev_num, NULL, DRIVER_NAME) == NULL) {
printk(KERN_ERR "bme280: can't create device file\n");
goto DeviceError;
}
cdev_init(&bme280_device, &fops);
if(cdev_add(&bme280_device, dev_num, 1) == -1) {
printk(KERN_ERR "bme280: registration of device to kernel failed\n");
goto AddError;
}
if((bme_i2c_adapter = i2c_get_adapter(i2c_bus)) == NULL) {
printk(KERN_ERR "bme280: can't get adapter i2c-%d\n", i2c_bus);
goto AdapterError;
}
if((bme280_i2c_client = i2c_new_client_device(bme_i2c_adapter, &bme_board_info)) == NULL) {
goto AdapterError;
}
if(i2c_add_driver(&bme_driver) == -1) {
printk(KERN_ERR "bme280: can't add driver\n");
goto I2CDriverError;
}
i2c_put_adapter(bme_i2c_adapter);
printk(KERN_DEFAULT "bme280: driver added\n");
if(init_sensor() != 0) {
goto SensorError;
}
return 0;
SensorError:
printk(KERN_ERR "bme280: can't read data from the sensor\n");
I2CDriverError:
i2c_unregister_device(bme280_i2c_client);
AdapterError:
cdev_del(&bme280_device);
AddError:
device_destroy(bme280_class, dev_num);
DeviceError:
class_destroy(bme280_class);
ClassError:
unregister_chrdev(dev_num, DRIVER_NAME);
return -1;
}
static void __exit bme280_exit(void) {
i2c_del_driver(&bme_driver);
i2c_unregister_device(bme280_i2c_client);
cdev_del(&bme280_device);
device_destroy(bme280_class, dev_num);
class_destroy(bme280_class);
unregister_chrdev(dev_num, DRIVER_NAME);
printk(KERN_DEFAULT "bme280: module unloaded\n");
}
module_init(bme280_init);
module_exit(bme280_exit);