]> git.itanic.dy.fi Git - BME280_driver/blob - examples/linux_userspace.c
Hide excess debug messages by default
[BME280_driver] / examples / linux_userspace.c
1 /**\\r
2  * Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.\r
3  *\r
4  * SPDX-License-Identifier: BSD-3-Clause\r
5  **/\r
6 \r
7 /**\r
8  * \ingroup bme280\r
9  * \defgroup bme280Examples Examples\r
10  * @brief Reference Examples\r
11  */\r
12 \r
13 /*!\r
14  * @ingroup bme280Examples\r
15  * @defgroup bme280GroupExampleLU linux_userspace\r
16  * @brief Linux userspace test code, simple and mose code directly from the doco.\r
17  * compile like this: gcc linux_userspace.c ../bme280.c -I ../ -o bme280\r
18  * tested: Raspberry Pi.\r
19  * Use like: ./bme280 /dev/i2c-0\r
20  * \include linux_userspace.c\r
21  */\r
22 \r
23 #ifdef __KERNEL__\r
24 #include <linux/i2c-dev.h>\r
25 #include <sys/ioctl.h>\r
26 #endif\r
27 \r
28 /******************************************************************************/\r
29 /*!                         System header files                               */\r
30 #include <string.h>\r
31 #include <stdio.h>\r
32 #include <stdlib.h>\r
33 #include <unistd.h>\r
34 #include <sys/types.h>\r
35 #include <fcntl.h>\r
36 \r
37 /******************************************************************************/\r
38 /*!                         Own header files                                  */\r
39 #include "bme280.h"\r
40 \r
41 /******************************************************************************/\r
42 /*!                               Structures                                  */\r
43 \r
44 /* Structure that contains identifier details used in example */\r
45 struct identifier\r
46 {\r
47     /* Variable to hold device address */\r
48     uint8_t dev_addr;\r
49 \r
50     /* Variable that contains file descriptor */\r
51     int8_t fd;\r
52 };\r
53 \r
54 /****************************************************************************/\r
55 /*!                         Functions                                       */\r
56 \r
57 /*!\r
58  *  @brief Function that creates a mandatory delay required in some of the APIs.\r
59  *\r
60  * @param[in] period              : Delay in microseconds.\r
61  * @param[in, out] intf_ptr       : Void pointer that can enable the linking of descriptors\r
62  *                                  for interface related call backs\r
63  *  @return void.\r
64  *\r
65  */\r
66 void user_delay_us(uint32_t period, void *intf_ptr);\r
67 \r
68 /*!\r
69  * @brief Function for print the temperature, humidity and pressure data.\r
70  *\r
71  * @param[out] comp_data    :   Structure instance of bme280_data\r
72  *\r
73  * @note Sensor data whose can be read\r
74  *\r
75  * sens_list\r
76  * --------------\r
77  * Pressure\r
78  * Temperature\r
79  * Humidity\r
80  *\r
81  */\r
82 void print_sensor_data(struct bme280_data *comp_data);\r
83 \r
84 /*!\r
85  *  @brief Function for reading the sensor's registers through I2C bus.\r
86  *\r
87  *  @param[in] reg_addr       : Register address.\r
88  *  @param[out] data          : Pointer to the data buffer to store the read data.\r
89  *  @param[in] len            : No of bytes to read.\r
90  *  @param[in, out] intf_ptr  : Void pointer that can enable the linking of descriptors\r
91  *                                  for interface related call backs.\r
92  *\r
93  *  @return Status of execution\r
94  *\r
95  *  @retval 0 -> Success\r
96  *  @retval > 0 -> Failure Info\r
97  *\r
98  */\r
99 int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr);\r
100 \r
101 /*!\r
102  *  @brief Function for writing the sensor's registers through I2C bus.\r
103  *\r
104  *  @param[in] reg_addr       : Register address.\r
105  *  @param[in] data           : Pointer to the data buffer whose value is to be written.\r
106  *  @param[in] len            : No of bytes to write.\r
107  *  @param[in, out] intf_ptr  : Void pointer that can enable the linking of descriptors\r
108  *                                  for interface related call backs\r
109  *\r
110  *  @return Status of execution\r
111  *\r
112  *  @retval BME280_OK -> Success\r
113  *  @retval BME280_E_COMM_FAIL -> Communication failure.\r
114  *\r
115  */\r
116 int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr);\r
117 \r
118 /*!\r
119  * @brief Function reads temperature, humidity and pressure data in forced mode.\r
120  *\r
121  * @param[in] dev   :   Structure instance of bme280_dev.\r
122  *\r
123  * @return Result of API execution status\r
124  *\r
125  * @retval BME280_OK - Success.\r
126  * @retval BME280_E_NULL_PTR - Error: Null pointer error\r
127  * @retval BME280_E_COMM_FAIL - Error: Communication fail error\r
128  * @retval BME280_E_NVM_COPY_FAILED - Error: NVM copy failed\r
129  *\r
130  */\r
131 int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev);\r
132 \r
133 /*!\r
134  * @brief This function starts execution of the program.\r
135  */\r
136 int main(int argc, char* argv[])\r
137 {\r
138     struct bme280_dev dev;\r
139 \r
140     struct identifier id;\r
141 \r
142     /* Variable to define the result */\r
143     int8_t rslt = BME280_OK;\r
144 \r
145     if (argc < 2)\r
146     {\r
147         fprintf(stderr, "Missing argument for i2c bus.\n");\r
148         exit(1);\r
149     }\r
150 \r
151     if ((id.fd = open(argv[1], O_RDWR)) < 0)\r
152     {\r
153         fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);\r
154         exit(1);\r
155     }\r
156 \r
157 #ifdef __KERNEL__\r
158     if (ioctl(id.fd, I2C_SLAVE, id.dev_addr) < 0)\r
159     {\r
160         fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");\r
161         exit(1);\r
162     }\r
163 \r
164 #endif\r
165 \r
166     /* Make sure to select BME280_I2C_ADDR_PRIM or BME280_I2C_ADDR_SEC as needed */\r
167     id.dev_addr = BME280_I2C_ADDR_PRIM;\r
168 \r
169     dev.intf = BME280_I2C_INTF;\r
170     dev.read = user_i2c_read;\r
171     dev.write = user_i2c_write;\r
172     dev.delay_us = user_delay_us;\r
173 \r
174     /* Update interface pointer with the structure that contains both device address and file descriptor */\r
175     dev.intf_ptr = &id;\r
176 \r
177     /* Initialize the bme280 */\r
178     rslt = bme280_init(&dev);\r
179     if (rslt != BME280_OK)\r
180     {\r
181         fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt);\r
182         exit(1);\r
183     }\r
184 \r
185     rslt = stream_sensor_data_forced_mode(&dev);\r
186     if (rslt != BME280_OK)\r
187     {\r
188         fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);\r
189         exit(1);\r
190     }\r
191 \r
192     return 0;\r
193 }\r
194 \r
195 /*!\r
196  * @brief This function reading the sensor's registers through I2C bus.\r
197  */\r
198 int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr)\r
199 {\r
200     struct identifier id;\r
201 \r
202     id = *((struct identifier *)intf_ptr);\r
203 \r
204     write(id.fd, &reg_addr, 1);\r
205     read(id.fd, data, len);\r
206 \r
207     return 0;\r
208 }\r
209 \r
210 /*!\r
211  * @brief This function provides the delay for required time (Microseconds) as per the input provided in some of the\r
212  * APIs\r
213  */\r
214 void user_delay_us(uint32_t period, void *intf_ptr)\r
215 {\r
216     usleep(period);\r
217 }\r
218 \r
219 /*!\r
220  * @brief This function for writing the sensor's registers through I2C bus.\r
221  */\r
222 int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr)\r
223 {\r
224     uint8_t *buf;\r
225     struct identifier id;\r
226 \r
227     id = *((struct identifier *)intf_ptr);\r
228 \r
229     buf = malloc(len + 1);\r
230     buf[0] = reg_addr;\r
231     memcpy(buf + 1, data, len);\r
232     if (write(id.fd, buf, len + 1) < (uint16_t)len)\r
233     {\r
234         return BME280_E_COMM_FAIL;\r
235     }\r
236 \r
237     free(buf);\r
238 \r
239     return BME280_OK;\r
240 }\r
241 \r
242 /*!\r
243  * @brief This API used to print the sensor temperature, pressure and humidity data.\r
244  */\r
245 void print_sensor_data(struct bme280_data *comp_data)\r
246 {\r
247     float temp, press, hum;\r
248 \r
249 #ifdef BME280_FLOAT_ENABLE\r
250     temp = comp_data->temperature;\r
251     press = 0.01 * comp_data->pressure;\r
252     hum = comp_data->humidity;\r
253 #else\r
254 #ifdef BME280_64BIT_ENABLE\r
255     temp = 0.01f * comp_data->temperature;\r
256     press = 0.0001f * comp_data->pressure;\r
257     hum = 1.0f / 1024.0f * comp_data->humidity;\r
258 #else\r
259     temp = 0.01f * comp_data->temperature;\r
260     press = 0.01f * comp_data->pressure;\r
261     hum = 1.0f / 1024.0f * comp_data->humidity;\r
262 #endif\r
263 #endif\r
264     printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);\r
265 }\r
266 \r
267 /*!\r
268  * @brief This API reads the sensor temperature, pressure and humidity data in forced mode.\r
269  */\r
270 int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)\r
271 {\r
272     /* Variable to define the result */\r
273     int8_t rslt = BME280_OK;\r
274 \r
275     /* Variable to define the selecting sensors */\r
276     uint8_t settings_sel = 0;\r
277 \r
278     /* Variable to store minimum wait time between consecutive measurement in force mode */\r
279     uint32_t req_delay;\r
280 \r
281     /* Structure to get the pressure, temperature and humidity values */\r
282     struct bme280_data comp_data;\r
283 \r
284     /* Recommended mode of operation: Indoor navigation */\r
285     dev->settings.osr_h = BME280_OVERSAMPLING_1X;\r
286     dev->settings.osr_p = BME280_OVERSAMPLING_16X;\r
287     dev->settings.osr_t = BME280_OVERSAMPLING_2X;\r
288     dev->settings.filter = BME280_FILTER_COEFF_16;\r
289 \r
290     settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;\r
291 \r
292     /* Set the sensor settings */\r
293     rslt = bme280_set_sensor_settings(settings_sel, dev);\r
294     if (rslt != BME280_OK)\r
295     {\r
296         fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);\r
297 \r
298         return rslt;\r
299     }\r
300 \r
301     printf("Temperature, Pressure, Humidity\n");\r
302 \r
303     /*Calculate the minimum delay required between consecutive measurement based upon the sensor enabled\r
304      *  and the oversampling configuration. */\r
305     req_delay = bme280_cal_meas_delay(&dev->settings);\r
306 \r
307     /* Continuously stream sensor data */\r
308     while (1)\r
309     {\r
310         /* Set the sensor to forced mode */\r
311         rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);\r
312         if (rslt != BME280_OK)\r
313         {\r
314             fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt);\r
315             break;\r
316         }\r
317 \r
318         /* Wait for the measurement to complete and print data */\r
319         dev->delay_us(req_delay, dev->intf_ptr);\r
320         rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);\r
321         if (rslt != BME280_OK)\r
322         {\r
323             fprintf(stderr, "Failed to get sensor data (code %+d).", rslt);\r
324             break;\r
325         }\r
326 \r
327         print_sensor_data(&comp_data);\r
328     }\r
329 \r
330     return rslt;\r
331 }\r