2 * Copyright (C) 2020 Bosch Sensortec GmbH
\r
4 * The license is available at root folder
\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
19 #include <linux/i2c-dev.h>
\r
20 #include <sys/ioctl.h>
\r
23 /******************************************************************************/
\r
24 /*! System header files */
\r
29 #include <sys/types.h>
\r
32 /******************************************************************************/
\r
33 /*! Own header files */
\r
36 /*****************************************************************************/
\r
37 /*! Global variables */
\r
40 /****************************************************************************/
\r
44 * @brief Function that creates a mandatory delay required in some of the APIs.
\r
46 * @param[in] period : The required wait time in microseconds.
\r
50 void user_delay_ms(uint32_t period);
\r
53 * @brief Function for print the temperature, humidity and pressure data.
\r
55 * @param[out] comp_data : Structure instance of bme280_data
\r
57 * @note Sensor data whose can be read
\r
66 void print_sensor_data(struct bme280_data *comp_data);
\r
69 * @brief Function for reading the sensor's registers through I2C bus.
\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
76 * @return Status of execution
\r
78 * @retval 0 -> Success
\r
79 * @retval > 0 -> Failure Info
\r
82 int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
\r
85 * @brief Function for writing the sensor's registers through I2C bus.
\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
92 * @return Status of execution
\r
94 * @retval BME280_OK -> Success
\r
95 * @retval BME280_E_COMM_FAIL -> Communication failure.
\r
98 int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
\r
101 * @brief Function reads temperature, humidity and pressure data in forced mode.
\r
103 * @param[in] dev : Structure instance of bme280_dev.
\r
105 * @return Result of API execution status
\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
113 int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev);
\r
116 * @brief This function starts execution of the program.
\r
118 int main(int argc, char* argv[])
\r
120 struct bme280_dev dev;
\r
122 /* Variable to define the result */
\r
123 int8_t rslt = BME280_OK;
\r
127 fprintf(stderr, "Missing argument for i2c bus.\n");
\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
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
140 if ((fd = open(argv[1], O_RDWR)) < 0)
\r
142 fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);
\r
147 if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0)
\r
149 fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");
\r
154 /* Initialize the bme280 */
\r
155 rslt = bme280_init(&dev);
\r
156 if (rslt != BME280_OK)
\r
158 fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt);
\r
162 rslt = stream_sensor_data_forced_mode(&dev);
\r
163 if (rslt != BME280_OK)
\r
165 fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);
\r
173 * @brief This function reading the sensor's registers through I2C bus.
\r
175 int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
\r
177 write(fd, ®_addr, 1);
\r
178 read(fd, data, len);
\r
184 * @brief This function provides the delay for required time (Microseconds) as per the input provided in some of the
\r
187 void user_delay_ms(uint32_t period)
\r
189 /* Milliseconds convert to microseconds */
\r
190 usleep(period * 1000);
\r
194 * @brief This function for writing the sensor's registers through I2C bus.
\r
196 int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
\r
200 buf = malloc(len + 1);
\r
202 memcpy(buf + 1, data, len);
\r
203 if (write(fd, buf, len + 1) < len)
\r
205 return BME280_E_COMM_FAIL;
\r
214 * @brief This API used to print the sensor temperature, pressure and humidity data.
\r
216 void print_sensor_data(struct bme280_data *comp_data)
\r
218 float temp, press, hum;
\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
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
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
235 printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);
\r
239 * @brief This API reads the sensor temperature, pressure and humidity data in forced mode.
\r
241 int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
\r
243 /* Variable to define the result */
\r
244 int8_t rslt = BME280_OK;
\r
246 /* Variable to define the selecting sensors */
\r
247 uint8_t settings_sel = 0;
\r
249 /* Variable to store minimum wait time between consecutive measurement in force mode */
\r
250 uint32_t req_delay;
\r
252 /* Structure to get the pressure, temperature and humidity values */
\r
253 struct bme280_data comp_data;
\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
261 settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;
\r
263 /* Set the sensor settings */
\r
264 rslt = bme280_set_sensor_settings(settings_sel, dev);
\r
265 if (rslt != BME280_OK)
\r
267 fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);
\r
272 printf("Temperature, Pressure, Humidity\n");
\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
278 /* Continuously stream sensor data */
\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
285 fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt);
\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
294 fprintf(stderr, "Failed to get sensor data (code %+d).", rslt);
\r
298 print_sensor_data(&comp_data);
\r