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