2 * BSD userspace test code, simple and mose code directy from the doco.
\r
3 * compile like this: cc bsd_userspace.c ../bme280.c -I ../ -o bme280
\r
4 * tested: NanoPi NEO.
\r
5 * Use like: ./bme280 /dev/iic0
\r
9 #include <sys/ioctl.h>
\r
10 #include <dev/iicbus/iic.h>
\r
13 /******************************************************************************/
\r
14 /*! System header files */
\r
20 #include <sys/types.h>
\r
23 /******************************************************************************/
\r
24 /*! Own header files */
\r
28 /******************************************************************************/
\r
31 /* Structure that contains identifier details used in example */
\r
34 /* Variable to hold device address */
\r
37 /* Variable that contains file descriptor */
\r
41 /******************************************************************************/
\r
45 * @brief Function for reading the sensor's registers through I2C bus.
\r
47 * @param[in] reg_addr : Register address.
\r
48 * @param[out] data : Pointer to the data buffer to store the read data.
\r
49 * @param[in] len : No of bytes to read.
\r
50 * @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
\r
51 * for interface related call backs.
\r
53 * @return Status of execution
\r
55 * @retval 0 -> Success
\r
56 * @retval > 0 -> Failure Info
\r
59 int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr);
\r
62 * @brief Function that creates a mandatory delay required in some of the APIs.
\r
64 * @param[in] period : Delay in microseconds.
\r
65 * @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
\r
66 * for interface related call backs
\r
70 void user_delay_us(uint32_t period, void *intf_ptr);
\r
73 * @brief Function for writing the sensor's registers through I2C bus.
\r
75 * @param[in] reg_addr : Register address.
\r
76 * @param[in] data : Pointer to the data buffer whose value is to be written.
\r
77 * @param[in] len : No of bytes to write.
\r
78 * @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
\r
79 * for interface related call backs
\r
81 * @return Status of execution
\r
83 * @retval BME280_OK -> Success
\r
84 * @retval BME280_E_COMM_FAIL -> Communication failure.
\r
87 int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr);
\r
90 * @brief Function for print the temperature, humidity and pressure data.
\r
92 * @param[out] comp_data : Structure instance of bme280_data
\r
94 * @note Sensor data whose can be read
\r
103 static void print_sensor_data(struct bme280_data *comp_data);
\r
106 * @brief Function reads temperature, humidity and pressure data in forced mode.
\r
108 * @param[in] dev : Structure instance of bme280_dev.
\r
110 * @return Result of API execution status
\r
112 * @retval BME280_OK - Success.
\r
113 * @retval BME280_E_NULL_PTR - Error: Null pointer error
\r
114 * @retval BME280_E_COMM_FAIL - Error: Communication fail error
\r
115 * @retval BME280_E_NVM_COPY_FAILED - Error: NVM copy failed
\r
118 static int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev);
\r
121 * @brief This function reading the sensor's registers through I2C bus.
\r
123 int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr)
\r
125 struct identifier id;
\r
127 id = *((struct identifier *)intf_ptr);
\r
129 struct iic_msg msgs[2] = {
\r
130 { id.dev_addr << 1 | IIC_M_WR, IIC_M_WR, 1, ®_addr }, { id.dev_addr << 1 | IIC_M_RD, IIC_M_RD, len, data },
\r
133 struct iic_rdwr_data rdwr_data = { msgs, 2 };
\r
135 int error = ioctl(id.fd, I2CRDWR, &rdwr_data);
\r
139 return BME280_E_COMM_FAIL;
\r
146 * @brief This function provides the delay for required time (Microseconds) as per the input provided in some of the
\r
149 void user_delay_us(uint32_t period, void *intf_ptr)
\r
155 * @brief This function for writing the sensor's registers through I2C bus.
\r
157 int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr)
\r
159 struct identifier id;
\r
161 id = *((struct identifier *)intf_ptr);
\r
163 uint8_t *buf = malloc((1 + len) * sizeof(uint8_t));
\r
167 return BME280_E_COMM_FAIL;
\r
172 for (uint8_t i = 0; i < len; i++)
\r
174 buf[i + 1] = data[i];
\r
177 struct iic_msg msg;
\r
179 msg.slave = id.dev_addr << 1 | !IIC_M_RD;
\r
180 msg.flags = !IIC_M_RD;
\r
181 msg.len = (1 + len) * sizeof(uint8_t);
\r
184 struct iic_rdwr_data rdwr_data = { &msg, 1 };
\r
186 int error = ioctl(id.fd, I2CRDWR, &rdwr_data);
\r
192 return BME280_E_COMM_FAIL;
\r
201 * @brief This API used to print the sensor temperature, pressure and humidity data.
\r
203 void print_sensor_data(struct bme280_data *comp_data)
\r
205 float temp, press, hum;
\r
207 #ifdef BME280_FLOAT_ENABLE
\r
208 temp = comp_data->temperature;
\r
209 press = 0.01 * comp_data->pressure;
\r
210 hum = comp_data->humidity;
\r
212 #ifdef BME280_64BIT_ENABLE
\r
213 temp = 0.01f * comp_data->temperature;
\r
214 press = 0.0001f * comp_data->pressure;
\r
215 hum = 1.0f / 1024.0f * comp_data->humidity;
\r
217 temp = 0.01f * comp_data->temperature;
\r
218 press = 0.01f * comp_data->pressure;
\r
219 hum = 1.0f / 1024.0f * comp_data->humidity;
\r
222 printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);
\r
226 * @brief This API reads the sensor temperature, pressure and humidity data in forced mode.
\r
228 int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
\r
231 uint8_t settings_sel;
\r
232 struct bme280_data comp_data;
\r
234 /* Recommended mode of operation: Indoor navigation */
\r
235 dev->settings.osr_h = BME280_OVERSAMPLING_1X;
\r
236 dev->settings.osr_p = BME280_OVERSAMPLING_16X;
\r
237 dev->settings.osr_t = BME280_OVERSAMPLING_2X;
\r
238 dev->settings.filter = BME280_FILTER_COEFF_16;
\r
240 settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;
\r
242 rslt = bme280_set_sensor_settings(settings_sel, dev);
\r
243 if (rslt != BME280_OK)
\r
245 fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);
\r
250 printf("Temperature, Pressure, Humidity\n");
\r
252 /* Continuously stream sensor data */
\r
255 rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);
\r
256 if (rslt != BME280_OK)
\r
258 fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt);
\r
262 /* Wait for the measurement to complete and print data @25Hz */
\r
263 dev->delay_us(40000, dev->intf_ptr);
\r
264 rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);
\r
265 if (rslt != BME280_OK)
\r
267 fprintf(stderr, "Failed to get sensor data (code %+d).", rslt);
\r
271 print_sensor_data(&comp_data);
\r
272 dev->delay_us(1000000, dev->intf_ptr);
\r
279 * @brief This function starts execution of the program.
\r
281 int main(int argc, char* argv[])
\r
283 struct bme280_dev dev;
\r
284 int8_t rslt = BME280_OK;
\r
286 struct identifier id;
\r
290 fprintf(stderr, "Missing argument for i2c bus.\n");
\r
294 if ((id.fd = open(argv[1], O_RDWR)) < 0)
\r
296 fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);
\r
301 * make sure to select BME280_I2C_ADDR_PRIM
\r
302 * or BME280_I2C_ADDR_SEC as needed
\r
304 id.dev_addr = BME280_I2C_ADDR_PRIM;
\r
306 dev.intf = BME280_I2C_INTF;
\r
307 dev.read = user_i2c_read;
\r
308 dev.write = user_i2c_write;
\r
309 dev.delay_us = user_delay_us;
\r
311 /* Update interface pointer with the structure that contains both device address and file descriptor */
\r
312 dev.intf_ptr = &id;
\r
314 rslt = bme280_init(&dev);
\r
315 if (rslt != BME280_OK)
\r
317 fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt);
\r
321 rslt = stream_sensor_data_forced_mode(&dev);
\r
322 if (rslt != BME280_OK)
\r
324 fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);
\r