-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsd_userspace.c
329 lines (272 loc) · 8.72 KB
/
bsd_userspace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* BSD userspace test code, simple and mose code directy from the doco.
* compile like this: cc bsd_userspace.c ../bme280.c -I ../ -o bme280
* tested: NanoPi NEO.
* Use like: ./bme280 /dev/iic0
*/
#ifdef __KERNEL__
#include <sys/ioctl.h>
#include <dev/iicbus/iic.h>
#endif
/******************************************************************************/
/*! System header files */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
/******************************************************************************/
/*! Own header files */
#include <iic.h>
#include "bme280.h"
/******************************************************************************/
/*! Structures */
/* Structure that contains identifier details used in example */
struct identifier
{
/* Variable to hold device address */
uint8_t dev_addr;
/* Variable that contains file descriptor */
int8_t fd;
};
/******************************************************************************/
/*! Functions */
/*!
* @brief Function for reading the sensor's registers through I2C bus.
*
* @param[in] reg_addr : Register address.
* @param[out] data : Pointer to the data buffer to store the read data.
* @param[in] len : No of bytes to read.
* @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related call backs.
*
* @return Status of execution
*
* @retval 0 -> Success
* @retval > 0 -> Failure Info
*
*/
int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr);
/*!
* @brief Function that creates a mandatory delay required in some of the APIs.
*
* @param[in] period : Delay in microseconds.
* @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related call backs
* @return void.
*
*/
void user_delay_us(uint32_t period, void *intf_ptr);
/*!
* @brief Function for writing the sensor's registers through I2C bus.
*
* @param[in] reg_addr : Register address.
* @param[in] data : Pointer to the data buffer whose value is to be written.
* @param[in] len : No of bytes to write.
* @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related call backs
*
* @return Status of execution
*
* @retval BME280_OK -> Success
* @retval BME280_E_COMM_FAIL -> Communication failure.
*
*/
int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr);
/*!
* @brief Function for print the temperature, humidity and pressure data.
*
* @param[out] comp_data : Structure instance of bme280_data
*
* @note Sensor data whose can be read
*
* sens_list
* --------------
* Pressure
* Temperature
* Humidity
*
*/
static void print_sensor_data(struct bme280_data *comp_data);
/*!
* @brief Function reads temperature, humidity and pressure data in forced mode.
*
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
*
* @retval BME280_OK - Success.
* @retval BME280_E_NULL_PTR - Error: Null pointer error
* @retval BME280_E_COMM_FAIL - Error: Communication fail error
* @retval BME280_E_NVM_COPY_FAILED - Error: NVM copy failed
*
*/
static int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev);
/*!
* @brief This function reading the sensor's registers through I2C bus.
*/
int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr)
{
struct identifier id;
id = *((struct identifier *)intf_ptr);
struct iic_msg msgs[2] = {
{ id.dev_addr << 1 | IIC_M_WR, IIC_M_WR, 1, ®_addr }, { id.dev_addr << 1 | IIC_M_RD, IIC_M_RD, len, data },
};
struct iic_rdwr_data rdwr_data = { msgs, 2 };
int error = ioctl(id.fd, I2CRDWR, &rdwr_data);
if (error)
{
return BME280_E_COMM_FAIL;
}
return BME280_OK;
}
/*!
* @brief This function provides the delay for required time (Microseconds) as per the input provided in some of the
* APIs
*/
void user_delay_us(uint32_t period, void *intf_ptr)
{
usleep(period);
}
/*!
* @brief This function for writing the sensor's registers through I2C bus.
*/
int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr)
{
struct identifier id;
id = *((struct identifier *)intf_ptr);
uint8_t *buf = malloc((1 + len) * sizeof(uint8_t));
if (buf == NULL)
{
return BME280_E_COMM_FAIL;
}
buf[0] = reg_addr;
for (uint8_t i = 0; i < len; i++)
{
buf[i + 1] = data[i];
}
struct iic_msg msg;
msg.slave = id.dev_addr << 1 | !IIC_M_RD;
msg.flags = !IIC_M_RD;
msg.len = (1 + len) * sizeof(uint8_t);
msg.buf = buf;
struct iic_rdwr_data rdwr_data = { &msg, 1 };
int error = ioctl(id.fd, I2CRDWR, &rdwr_data);
if (error)
{
free(buf);
return BME280_E_COMM_FAIL;
}
free(buf);
return BME280_OK;
}
/*!
* @brief This API used to print the sensor temperature, pressure and humidity data.
*/
void print_sensor_data(struct bme280_data *comp_data)
{
float temp, press, hum;
#ifdef BME280_FLOAT_ENABLE
temp = comp_data->temperature;
press = 0.01 * comp_data->pressure;
hum = comp_data->humidity;
#else
#ifdef BME280_64BIT_ENABLE
temp = 0.01f * comp_data->temperature;
press = 0.0001f * comp_data->pressure;
hum = 1.0f / 1024.0f * comp_data->humidity;
#else
temp = 0.01f * comp_data->temperature;
press = 0.01f * comp_data->pressure;
hum = 1.0f / 1024.0f * comp_data->humidity;
#endif
#endif
printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);
}
/*!
* @brief This API reads the sensor temperature, pressure and humidity data in forced mode.
*/
int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
{
int8_t rslt;
uint8_t settings_sel;
struct bme280_data comp_data;
/* Recommended mode of operation: Indoor navigation */
dev->settings.osr_h = BME280_OVERSAMPLING_1X;
dev->settings.osr_p = BME280_OVERSAMPLING_16X;
dev->settings.osr_t = BME280_OVERSAMPLING_2X;
dev->settings.filter = BME280_FILTER_COEFF_16;
settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;
rslt = bme280_set_sensor_settings(settings_sel, dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);
return rslt;
}
printf("Temperature, Pressure, Humidity\n");
/* Continuously stream sensor data */
while (1)
{
rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt);
break;
}
/* Wait for the measurement to complete and print data @25Hz */
dev->delay_us(40000, dev->intf_ptr);
rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to get sensor data (code %+d).", rslt);
break;
}
print_sensor_data(&comp_data);
dev->delay_us(1000000, dev->intf_ptr);
}
return rslt;
}
/*!
* @brief This function starts execution of the program.
*/
int main(int argc, char* argv[])
{
struct bme280_dev dev;
int8_t rslt = BME280_OK;
struct identifier id;
if (argc < 2)
{
fprintf(stderr, "Missing argument for i2c bus.\n");
exit(1);
}
if ((id.fd = open(argv[1], O_RDWR)) < 0)
{
fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);
exit(1);
}
/*
* make sure to select BME280_I2C_ADDR_PRIM
* or BME280_I2C_ADDR_SEC as needed
*/
id.dev_addr = BME280_I2C_ADDR_PRIM;
dev.intf = BME280_I2C_INTF;
dev.read = user_i2c_read;
dev.write = user_i2c_write;
dev.delay_us = user_delay_us;
/* Update interface pointer with the structure that contains both device address and file descriptor */
dev.intf_ptr = &id;
rslt = bme280_init(&dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt);
exit(1);
}
rslt = stream_sensor_data_forced_mode(&dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);
exit(1);
}
return 0;
}