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