]> git.itanic.dy.fi Git - BME280_driver/blob - README.md
Cleaned macros and defaulted to using floating point compensation
[BME280_driver] / README.md
1 # BME280 sensor API\r
2 ## Introduction\r
3 This package contains the Bosch Sensortec's BME280 pressure sensor driver (sensor API)\r
4 \r
5 The sensor driver package includes bme280.c, bme280.h and bme280_defs.h files.\r
6 \r
7 \r
8 ## Integration details\r
9 * Integrate bme280.h, bme280_defs.h and bme280.c file in to the project.\r
10 * Include the bme280.h file in your code like below.\r
11 ``` c\r
12 #include "bme280.h"\r
13 ```\r
14 \r
15 ## File information\r
16 * bme280_defs.h : This header file has the constants, macros and datatype declarations.\r
17 * bme280.h : This header file contains the declarations of the sensor driver APIs.\r
18 * bme280.c : This source file contains the definitions of the sensor driver APIs.\r
19 \r
20 ## Supported sensor interfaces\r
21 * SPI 4-wire\r
22 * I2C\r
23 \r
24 SPI 3-wire is currently not supported in the API.\r
25 ## Usage guide\r
26 ### Initializing the sensor\r
27 To initialize the sensor, user need to create a device structure. User can do this by \r
28 creating an instance of the structure bme280_dev. After creating the device strcuture, user \r
29 need to fill in the various parameters as shown below.\r
30 \r
31 #### Example for SPI 4-Wire\r
32 ``` c\r
33 struct bme280_dev dev;\r
34 int8_t rslt = BME280_OK;\r
35 \r
36 /* Sensor_0 interface over SPI with native chip select line */\r
37 dev.dev_id = 0;\r
38 dev.intf = BME280_SPI_INTF;\r
39 dev.read = user_spi_read;\r
40 dev.write = user_spi_write;\r
41 dev.delay_ms = user_delay_ms;\r
42 \r
43 rslt = bme280_init(&dev);\r
44 ```\r
45 #### Example for I2C\r
46 ``` c\r
47 struct bme280_dev dev;\r
48 int8_t rslt = BME280_OK;\r
49 \r
50 dev.dev_id = BME280_I2C_ADDR_PRIM;\r
51 dev.intf = BME280_I2C_INTF;\r
52 dev.read = user_i2c_read;\r
53 dev.write = user_i2c_write;\r
54 dev.delay_ms = user_delay_ms;\r
55 \r
56 rslt = bme280_init(&dev);\r
57 ```\r
58 Regarding compensation functions for temperature,pressure and humidity we have two implementations.\r
59 1) Double precision floating point version\r
60 2) Integer version\r
61 \r
62 By default, integer version is used in the API. If the user needs the floating point version, the user has to uncomment BME280_FLOAT_ENABLE macro in bme280_defs.h file or add that to the compiler flags.\r
63 \r
64 In integer compensation functions, we also have below two implementations for pressure.\r
65 1) For 32 bit machine.\r
66 2) For 64 bit machine.\r
67 \r
68 By default, 64 bit variant is used in the API. If the user wants 32 bit variant, the user can disable the\r
69 macro BME280_64BIT_ENABLE in bme280_defs.h file.\r
70 \r
71 ### Sensor data units\r
72 > The sensor data units depends on the following macros being enabled or not, \r
73 > (in bme280_defs.h file or as compiler macros)\r
74 >   * BME280_FLOAT_ENABLE\r
75 >   * BME280_64BIT_ENABLE\r
76 \r
77 In case of the macro "BME280_FLOAT_ENABLE" enabled,\r
78 The outputs are in double and the units are\r
79 \r
80     - °C for temperature\r
81     - % relative humidity\r
82     - Pascal for pressure\r
83 \r
84 In case if "BME280_FLOAT_ENABLE" is not enabled, then it is\r
85 \r
86     - int32_t for temperature with the units 100 * °C\r
87     - uint32_t for humidity with the units 1024 * % relative humidity\r
88     - uint32_t for pressure\r
89          If macro "BME280_64BIT_ENABLE" is enabled, which it is by default, the unit is 100 * Pascal\r
90          If this macro is disabled, Then the unit is in Pascal\r
91 \r
92 ### Stream sensor data\r
93 #### Stream sensor data in forced mode\r
94 \r
95 ``` c\r
96 int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)\r
97 {\r
98     int8_t rslt;\r
99     uint8_t settings_sel;\r
100         uint32_t req_delay;\r
101     struct bme280_data comp_data;\r
102 \r
103     /* Recommended mode of operation: Indoor navigation */\r
104     dev->settings.osr_h = BME280_OVERSAMPLING_1X;\r
105     dev->settings.osr_p = BME280_OVERSAMPLING_16X;\r
106     dev->settings.osr_t = BME280_OVERSAMPLING_2X;\r
107     dev->settings.filter = BME280_FILTER_COEFF_16;\r
108 \r
109     settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;\r
110 \r
111     rslt = bme280_set_sensor_settings(settings_sel, dev);\r
112         \r
113         /*Calculate the minimum delay required between consecutive measurement based upon the sensor enabled\r
114      *  and the oversampling configuration. */\r
115     req_delay = bme280_cal_meas_delay(&dev->settings);\r
116 \r
117     printf("Temperature, Pressure, Humidity\r\n");\r
118     /* Continuously stream sensor data */\r
119     while (1) {\r
120         rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);\r
121         /* Wait for the measurement to complete and print data @25Hz */\r
122         dev->delay_ms(req_delay);\r
123         rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);\r
124         print_sensor_data(&comp_data);\r
125     }\r
126     return rslt;\r
127 }\r
128 \r
129 void print_sensor_data(struct bme280_data *comp_data)\r
130 {\r
131 #ifdef BME280_FLOAT_ENABLE\r
132         printf("%0.2f, %0.2f, %0.2f\r\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);\r
133 #else\r
134         printf("%ld, %ld, %ld\r\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);\r
135 #endif\r
136 }\r
137 ```\r
138 ##### Stream sensor data in normal mode\r
139 ``` c\r
140 int8_t stream_sensor_data_normal_mode(struct bme280_dev *dev)\r
141 {\r
142         int8_t rslt;\r
143         uint8_t settings_sel;\r
144         struct bme280_data comp_data;\r
145 \r
146         /* Recommended mode of operation: Indoor navigation */\r
147         dev->settings.osr_h = BME280_OVERSAMPLING_1X;\r
148         dev->settings.osr_p = BME280_OVERSAMPLING_16X;\r
149         dev->settings.osr_t = BME280_OVERSAMPLING_2X;\r
150         dev->settings.filter = BME280_FILTER_COEFF_16;\r
151         dev->settings.standby_time = BME280_STANDBY_TIME_62_5_MS;\r
152 \r
153         settings_sel = BME280_OSR_PRESS_SEL;\r
154         settings_sel |= BME280_OSR_TEMP_SEL;\r
155         settings_sel |= BME280_OSR_HUM_SEL;\r
156         settings_sel |= BME280_STANDBY_SEL;\r
157         settings_sel |= BME280_FILTER_SEL;\r
158         rslt = bme280_set_sensor_settings(settings_sel, dev);\r
159         rslt = bme280_set_sensor_mode(BME280_NORMAL_MODE, dev);\r
160 \r
161         printf("Temperature, Pressure, Humidity\r\n");\r
162         while (1) {\r
163                 /* Delay while the sensor completes a measurement */\r
164                 dev->delay_ms(70);\r
165                 rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);\r
166                 print_sensor_data(&comp_data);\r
167         }\r
168 \r
169         return rslt;\r
170 }\r
171 \r
172 void print_sensor_data(struct bme280_data *comp_data)\r
173 {\r
174 #ifdef BME280_FLOAT_ENABLE\r
175         printf("%0.2f, %0.2f, %0.2f\r\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);\r
176 #else\r
177         printf("%ld, %ld, %ld\r\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);\r
178 #endif\r
179 }\r
180 ```\r
181 \r
182 ### Templates for function pointers\r
183 ``` c\r
184 \r
185 void user_delay_ms(uint32_t period)\r
186 {\r
187     /*\r
188      * Return control or wait,\r
189      * for a period amount of milliseconds\r
190      */\r
191 }\r
192 \r
193 int8_t user_spi_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)\r
194 {\r
195     int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */\r
196 \r
197     /*\r
198      * The parameter dev_id can be used as a variable to select which Chip Select pin has\r
199      * to be set low to activate the relevant device on the SPI bus\r
200      */\r
201 \r
202     /*\r
203      * Data on the bus should be like\r
204      * |----------------+---------------------+-------------|\r
205      * | MOSI           | MISO                | Chip Select |\r
206      * |----------------+---------------------|-------------|\r
207      * | (don't care)   | (don't care)        | HIGH        |\r
208      * | (reg_addr)     | (don't care)        | LOW         |\r
209      * | (don't care)   | (reg_data[0])       | LOW         |\r
210      * | (....)         | (....)              | LOW         |\r
211      * | (don't care)   | (reg_data[len - 1]) | LOW         |\r
212      * | (don't care)   | (don't care)        | HIGH        |\r
213      * |----------------+---------------------|-------------|\r
214      */\r
215 \r
216     return rslt;\r
217 }\r
218 \r
219 int8_t user_spi_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)\r
220 {\r
221     int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */\r
222 \r
223     /*\r
224      * The parameter dev_id can be used as a variable to select which Chip Select pin has\r
225      * to be set low to activate the relevant device on the SPI bus\r
226      */\r
227 \r
228     /*\r
229      * Data on the bus should be like\r
230      * |---------------------+--------------+-------------|\r
231      * | MOSI                | MISO         | Chip Select |\r
232      * |---------------------+--------------|-------------|\r
233      * | (don't care)        | (don't care) | HIGH        |\r
234      * | (reg_addr)          | (don't care) | LOW         |\r
235      * | (reg_data[0])       | (don't care) | LOW         |\r
236      * | (....)              | (....)       | LOW         |\r
237      * | (reg_data[len - 1]) | (don't care) | LOW         |\r
238      * | (don't care)        | (don't care) | HIGH        |\r
239      * |---------------------+--------------|-------------|\r
240      */\r
241 \r
242     return rslt;\r
243 }\r
244 \r
245 int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)\r
246 {\r
247     int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */\r
248 \r
249     /*\r
250      * The parameter dev_id can be used as a variable to store the I2C address of the device\r
251      */\r
252 \r
253     /*\r
254      * Data on the bus should be like\r
255      * |------------+---------------------|\r
256      * | I2C action | Data                |\r
257      * |------------+---------------------|\r
258      * | Start      | -                   |\r
259      * | Write      | (reg_addr)          |\r
260      * | Stop       | -                   |\r
261      * | Start      | -                   |\r
262      * | Read       | (reg_data[0])       |\r
263      * | Read       | (....)              |\r
264      * | Read       | (reg_data[len - 1]) |\r
265      * | Stop       | -                   |\r
266      * |------------+---------------------|\r
267      */\r
268 \r
269     return rslt;\r
270 }\r
271 \r
272 int8_t user_i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)\r
273 {\r
274     int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */\r
275 \r
276     /*\r
277      * The parameter dev_id can be used as a variable to store the I2C address of the device\r
278      */\r
279 \r
280     /*\r
281      * Data on the bus should be like\r
282      * |------------+---------------------|\r
283      * | I2C action | Data                |\r
284      * |------------+---------------------|\r
285      * | Start      | -                   |\r
286      * | Write      | (reg_addr)          |\r
287      * | Write      | (reg_data[0])       |\r
288      * | Write      | (....)              |\r
289      * | Write      | (reg_data[len - 1]) |\r
290      * | Stop       | -                   |\r
291      * |------------+---------------------|\r
292      */\r
293 \r
294     return rslt;\r
295 }\r
296 \r
297 ```