2 * Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.
\r
4 * SPDX-License-Identifier: BSD-3-Clause
\r
9 * \defgroup bme280Examples Examples
\r
10 * @brief Reference Examples
\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
24 #include <linux/i2c-dev.h>
\r
25 #include <sys/ioctl.h>
\r
28 /******************************************************************************/
\r
29 /*! System header files */
\r
34 #include <sys/types.h>
\r
37 /******************************************************************************/
\r
38 /*! Own header files */
\r
41 /******************************************************************************/
\r
44 /* Structure that contains identifier details used in example */
\r
47 /* Variable to hold device address */
\r
50 /* Variable that contains file descriptor */
\r
54 /****************************************************************************/
\r
58 * @brief Function that creates a mandatory delay required in some of the APIs.
\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
66 void user_delay_us(uint32_t period, void *intf_ptr);
\r
69 * @brief Function for print the temperature, humidity and pressure data.
\r
71 * @param[out] comp_data : Structure instance of bme280_data
\r
73 * @note Sensor data whose can be read
\r
82 void print_sensor_data(struct bme280_data *comp_data);
\r
85 * @brief Function for reading the sensor's registers through I2C bus.
\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
93 * @return Status of execution
\r
95 * @retval 0 -> Success
\r
96 * @retval > 0 -> Failure Info
\r
99 int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr);
\r
102 * @brief Function for writing the sensor's registers through I2C bus.
\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
110 * @return Status of execution
\r
112 * @retval BME280_OK -> Success
\r
113 * @retval BME280_E_COMM_FAIL -> Communication failure.
\r
116 int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr);
\r
119 * @brief Function reads temperature, humidity and pressure data in forced mode.
\r
121 * @param[in] dev : Structure instance of bme280_dev.
\r
123 * @return Result of API execution status
\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
131 int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev);
\r
134 * @brief This function starts execution of the program.
\r
136 int main(int argc, char* argv[])
\r
138 struct bme280_dev dev;
\r
140 struct identifier id;
\r
142 /* Variable to define the result */
\r
143 int8_t rslt = BME280_OK;
\r
147 fprintf(stderr, "Missing argument for i2c bus.\n");
\r
151 if ((id.fd = open(argv[1], O_RDWR)) < 0)
\r
153 fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);
\r
158 if (ioctl(id.fd, I2C_SLAVE, id.dev_addr) < 0)
\r
160 fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");
\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
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
174 /* Update interface pointer with the structure that contains both device address and file descriptor */
\r
175 dev.intf_ptr = &id;
\r
177 /* Initialize the bme280 */
\r
178 rslt = bme280_init(&dev);
\r
179 if (rslt != BME280_OK)
\r
181 fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt);
\r
185 rslt = stream_sensor_data_forced_mode(&dev);
\r
186 if (rslt != BME280_OK)
\r
188 fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);
\r
196 * @brief This function reading the sensor's registers through I2C bus.
\r
198 int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr)
\r
200 struct identifier id;
\r
202 id = *((struct identifier *)intf_ptr);
\r
204 write(id.fd, ®_addr, 1);
\r
205 read(id.fd, data, len);
\r
211 * @brief This function provides the delay for required time (Microseconds) as per the input provided in some of the
\r
214 void user_delay_us(uint32_t period, void *intf_ptr)
\r
220 * @brief This function for writing the sensor's registers through I2C bus.
\r
222 int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr)
\r
225 struct identifier id;
\r
227 id = *((struct identifier *)intf_ptr);
\r
229 buf = malloc(len + 1);
\r
231 memcpy(buf + 1, data, len);
\r
232 if (write(id.fd, buf, len + 1) < (uint16_t)len)
\r
234 return BME280_E_COMM_FAIL;
\r
243 * @brief This API used to print the sensor temperature, pressure and humidity data.
\r
245 void print_sensor_data(struct bme280_data *comp_data)
\r
247 float temp, press, hum;
\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
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
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
264 printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);
\r
268 * @brief This API reads the sensor temperature, pressure and humidity data in forced mode.
\r
270 int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
\r
272 /* Variable to define the result */
\r
273 int8_t rslt = BME280_OK;
\r
275 /* Variable to define the selecting sensors */
\r
276 uint8_t settings_sel = 0;
\r
278 /* Variable to store minimum wait time between consecutive measurement in force mode */
\r
279 uint32_t req_delay;
\r
281 /* Structure to get the pressure, temperature and humidity values */
\r
282 struct bme280_data comp_data;
\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
290 settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;
\r
292 /* Set the sensor settings */
\r
293 rslt = bme280_set_sensor_settings(settings_sel, dev);
\r
294 if (rslt != BME280_OK)
\r
296 fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);
\r
301 printf("Temperature, Pressure, Humidity\n");
\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
307 /* Continuously stream sensor data */
\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
314 fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt);
\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
323 fprintf(stderr, "Failed to get sensor data (code %+d).", rslt);
\r
327 print_sensor_data(&comp_data);
\r