]> git.itanic.dy.fi Git - BME280_driver/blob - examples/bsd_userspace.c
Updated interface design and examples.
[BME280_driver] / examples / bsd_userspace.c
1 /*\r
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
6  */\r
7 \r
8 #ifdef __KERNEL__\r
9 #include <sys/ioctl.h>\r
10 #include <dev/iicbus/iic.h>\r
11 #endif\r
12 \r
13 /******************************************************************************/\r
14 /*!                         System header files                               */\r
15 \r
16 #include <string.h>\r
17 #include <stdio.h>\r
18 #include <stdlib.h>\r
19 #include <unistd.h>\r
20 #include <sys/types.h>\r
21 #include <fcntl.h>\r
22 \r
23 /******************************************************************************/\r
24 /*!                         Own header files                                  */\r
25 #include <iic.h>\r
26 #include "bme280.h"\r
27 \r
28 /******************************************************************************/\r
29 /*!                               Structures                                  */\r
30 \r
31 /* Structure that contains identifier details used in example */\r
32 struct identifier\r
33 {\r
34     /* Variable to hold device address */\r
35     uint8_t dev_addr;\r
36 \r
37     /* Variable that contains file descriptor */\r
38     int8_t fd;\r
39 };\r
40 \r
41 /******************************************************************************/\r
42 /*!                           Functions                                       */\r
43 \r
44 /*!\r
45  *  @brief Function for reading the sensor's registers through I2C bus.\r
46  *\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
52  *\r
53  *  @return Status of execution\r
54  *\r
55  *  @retval 0 -> Success\r
56  *  @retval > 0 -> Failure Info\r
57  *\r
58  */\r
59 int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr);\r
60 \r
61 /*!\r
62  *  @brief Function that creates a mandatory delay required in some of the APIs.\r
63  *\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
67  *  @return void.\r
68  *\r
69  */\r
70 void user_delay_us(uint32_t period, void *intf_ptr);\r
71 \r
72 /*!\r
73  *  @brief Function for writing the sensor's registers through I2C bus.\r
74  *\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
80  *\r
81  *  @return Status of execution\r
82  *\r
83  *  @retval BME280_OK -> Success\r
84  *  @retval BME280_E_COMM_FAIL -> Communication failure.\r
85  *\r
86  */\r
87 int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr);\r
88 \r
89 /*!\r
90  * @brief Function for print the temperature, humidity and pressure data.\r
91  *\r
92  * @param[out] comp_data    :   Structure instance of bme280_data\r
93  *\r
94  * @note Sensor data whose can be read\r
95  *\r
96  * sens_list\r
97  * --------------\r
98  * Pressure\r
99  * Temperature\r
100  * Humidity\r
101  *\r
102  */\r
103 static void print_sensor_data(struct bme280_data *comp_data);\r
104 \r
105 /*!\r
106  * @brief Function reads temperature, humidity and pressure data in forced mode.\r
107  *\r
108  * @param[in] dev   :   Structure instance of bme280_dev.\r
109  *\r
110  * @return Result of API execution status\r
111  *\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
116  *\r
117  */\r
118 static int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev);\r
119 \r
120 /*!\r
121  * @brief This function reading the sensor's registers through I2C bus.\r
122  */\r
123 int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr)\r
124 {\r
125     struct identifier id;\r
126 \r
127     id = *((struct identifier *)intf_ptr);\r
128 \r
129     struct iic_msg msgs[2] = {\r
130         { id.dev_addr << 1 | IIC_M_WR, IIC_M_WR, 1, &reg_addr }, { id.dev_addr << 1 | IIC_M_RD, IIC_M_RD, len, data },\r
131     };\r
132 \r
133     struct iic_rdwr_data rdwr_data = { msgs, 2 };\r
134 \r
135     int error = ioctl(id.fd, I2CRDWR, &rdwr_data);\r
136 \r
137     if (error)\r
138     {\r
139         return BME280_E_COMM_FAIL;\r
140     }\r
141 \r
142     return BME280_OK;\r
143 }\r
144 \r
145 /*!\r
146  * @brief This function provides the delay for required time (Microseconds) as per the input provided in some of the\r
147  * APIs\r
148  */\r
149 void user_delay_us(uint32_t period, void *intf_ptr)\r
150 {\r
151     usleep(period);\r
152 }\r
153 \r
154 /*!\r
155  * @brief This function for writing the sensor's registers through I2C bus.\r
156  */\r
157 int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr)\r
158 {\r
159     struct identifier id;\r
160 \r
161     id = *((struct identifier *)intf_ptr);\r
162 \r
163     uint8_t *buf = malloc((1 + len) * sizeof(uint8_t));\r
164 \r
165     if (buf == NULL)\r
166     {\r
167         return BME280_E_COMM_FAIL;\r
168     }\r
169 \r
170     buf[0] = reg_addr;\r
171 \r
172     for (uint8_t i = 0; i < len; i++)\r
173     {\r
174         buf[i + 1] = data[i];\r
175     }\r
176 \r
177     struct iic_msg msg;\r
178 \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
182     msg.buf = buf;\r
183 \r
184     struct iic_rdwr_data rdwr_data = { &msg, 1 };\r
185 \r
186     int error = ioctl(id.fd, I2CRDWR, &rdwr_data);\r
187 \r
188     if (error)\r
189     {\r
190         free(buf);\r
191 \r
192         return BME280_E_COMM_FAIL;\r
193     }\r
194 \r
195     free(buf);\r
196 \r
197     return BME280_OK;\r
198 }\r
199 \r
200 /*!\r
201  * @brief This API used to print the sensor temperature, pressure and humidity data.\r
202  */\r
203 void print_sensor_data(struct bme280_data *comp_data)\r
204 {\r
205     float temp, press, hum;\r
206 \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
211 #else\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
216 #else\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
220 #endif\r
221 #endif\r
222     printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);\r
223 }\r
224 \r
225 /*!\r
226  * @brief This API reads the sensor temperature, pressure and humidity data in forced mode.\r
227  */\r
228 int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)\r
229 {\r
230     int8_t rslt;\r
231     uint8_t settings_sel;\r
232     struct bme280_data comp_data;\r
233 \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
239 \r
240     settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;\r
241 \r
242     rslt = bme280_set_sensor_settings(settings_sel, dev);\r
243     if (rslt != BME280_OK)\r
244     {\r
245         fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);\r
246 \r
247         return rslt;\r
248     }\r
249 \r
250     printf("Temperature, Pressure, Humidity\n");\r
251 \r
252     /* Continuously stream sensor data */\r
253     while (1)\r
254     {\r
255         rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);\r
256         if (rslt != BME280_OK)\r
257         {\r
258             fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt);\r
259             break;\r
260         }\r
261 \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
266         {\r
267             fprintf(stderr, "Failed to get sensor data (code %+d).", rslt);\r
268             break;\r
269         }\r
270 \r
271         print_sensor_data(&comp_data);\r
272         dev->delay_us(1000000, dev->intf_ptr);\r
273     }\r
274 \r
275     return rslt;\r
276 }\r
277 \r
278 /*!\r
279  * @brief This function starts execution of the program.\r
280  */\r
281 int main(int argc, char* argv[])\r
282 {\r
283     struct bme280_dev dev;\r
284     int8_t rslt = BME280_OK;\r
285 \r
286     struct identifier id;\r
287 \r
288     if (argc < 2)\r
289     {\r
290         fprintf(stderr, "Missing argument for i2c bus.\n");\r
291         exit(1);\r
292     }\r
293 \r
294     if ((id.fd = open(argv[1], O_RDWR)) < 0)\r
295     {\r
296         fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);\r
297         exit(1);\r
298     }\r
299 \r
300     /*\r
301      * make sure to select BME280_I2C_ADDR_PRIM\r
302      * or BME280_I2C_ADDR_SEC as needed\r
303      */\r
304     id.dev_addr = BME280_I2C_ADDR_PRIM;\r
305 \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
310 \r
311     /* Update interface pointer with the structure that contains both device address and file descriptor */\r
312     dev.intf_ptr = &id;\r
313 \r
314     rslt = bme280_init(&dev);\r
315     if (rslt != BME280_OK)\r
316     {\r
317         fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt);\r
318         exit(1);\r
319     }\r
320 \r
321     rslt = stream_sensor_data_forced_mode(&dev);\r
322     if (rslt != BME280_OK)\r
323     {\r
324         fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);\r
325         exit(1);\r
326     }\r
327 \r
328     return 0;\r
329 }\r