diff options
author | Akshay <[email protected]> | 2022-04-10 12:13:40 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2022-04-10 12:13:40 +0100 |
commit | dc90387ce7d8ba7b607d9c48540bf6d8b560f14d (patch) | |
tree | 4ccb8fa5886b66fa9d480edef74236c27f035e16 /lib/chibios/os/ex/devices/ST/lps22hb.c |
Diffstat (limited to 'lib/chibios/os/ex/devices/ST/lps22hb.c')
-rw-r--r-- | lib/chibios/os/ex/devices/ST/lps22hb.c | 686 |
1 files changed, 686 insertions, 0 deletions
diff --git a/lib/chibios/os/ex/devices/ST/lps22hb.c b/lib/chibios/os/ex/devices/ST/lps22hb.c new file mode 100644 index 000000000..d153a0d67 --- /dev/null +++ b/lib/chibios/os/ex/devices/ST/lps22hb.c | |||
@@ -0,0 +1,686 @@ | |||
1 | /* | ||
2 | ChibiOS - Copyright (C) 2016..2019 Rocco Marco Guglielmi | ||
3 | |||
4 | This file is part of ChibiOS. | ||
5 | |||
6 | ChibiOS is free software; you can redistribute it and/or modify | ||
7 | it under the terms of the GNU General Public License as published by | ||
8 | the Free Software Foundation; either version 3 of the License, or | ||
9 | (at your option) any later version. | ||
10 | |||
11 | ChibiOS is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | GNU General Public License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU General Public License | ||
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
18 | |||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file lps22hb.c | ||
23 | * @brief LPS22HB MEMS interface module code. | ||
24 | * | ||
25 | * @addtogroup LPS22HB | ||
26 | * @ingroup EX_ST | ||
27 | * @{ | ||
28 | */ | ||
29 | |||
30 | #include "hal.h" | ||
31 | #include "lps22hb.h" | ||
32 | |||
33 | /*===========================================================================*/ | ||
34 | /* Driver local definitions. */ | ||
35 | /*===========================================================================*/ | ||
36 | |||
37 | /*===========================================================================*/ | ||
38 | /* Driver exported variables. */ | ||
39 | /*===========================================================================*/ | ||
40 | |||
41 | /*===========================================================================*/ | ||
42 | /* Driver local variables and types. */ | ||
43 | /*===========================================================================*/ | ||
44 | |||
45 | /*===========================================================================*/ | ||
46 | /* Driver local functions. */ | ||
47 | /*===========================================================================*/ | ||
48 | |||
49 | #if (LPS22HB_USE_I2C) || defined(__DOXYGEN__) | ||
50 | /** | ||
51 | * @brief Reads registers value using I2C. | ||
52 | * @pre The I2C interface must be initialized and the driver started. | ||
53 | * | ||
54 | * @param[in] i2cp pointer to the I2C interface | ||
55 | * @param[in] sad slave address without R bit | ||
56 | * @param[in] reg first sub-register address | ||
57 | * @param[out] rxbuf pointer to an output buffer | ||
58 | * @param[in] n number of consecutive register to read | ||
59 | * @return the operation status. | ||
60 | * | ||
61 | * @notapi | ||
62 | */ | ||
63 | static msg_t lps22hbI2CReadRegister(I2CDriver *i2cp, lps22hb_sad_t sad, | ||
64 | uint8_t reg, uint8_t* rxbuf, size_t n) { | ||
65 | |||
66 | return i2cMasterTransmitTimeout(i2cp, sad, ®, 1, rxbuf, n, | ||
67 | TIME_INFINITE); | ||
68 | } | ||
69 | |||
70 | /** | ||
71 | * @brief Writes a value into a register using I2C. | ||
72 | * @pre The I2C interface must be initialized and the driver started. | ||
73 | * | ||
74 | * @param[in] i2cp pointer to the I2C interface | ||
75 | * @param[in] sad slave address without R bit | ||
76 | * @param[in] txbuf buffer containing sub-address value in first position | ||
77 | * and values to write | ||
78 | * @param[in] n size of txbuf less one (not considering the first | ||
79 | * element) | ||
80 | * @return the operation status. | ||
81 | * | ||
82 | * @notapi | ||
83 | */ | ||
84 | #define lps22hbI2CWriteRegister(i2cp, sad, txbuf, n) \ | ||
85 | i2cMasterTransmitTimeout(i2cp, sad, txbuf, n + 1, NULL, 0, \ | ||
86 | TIME_INFINITE) | ||
87 | #endif /* LPS22HB_USE_I2C */ | ||
88 | |||
89 | /** | ||
90 | * @brief Return the number of axes of the BaseBarometer. | ||
91 | * | ||
92 | * @param[in] ip pointer to @p BaseBarometer interface. | ||
93 | * | ||
94 | * @return the number of axes. | ||
95 | */ | ||
96 | static size_t baro_get_axes_number(void *ip) { | ||
97 | (void)ip; | ||
98 | |||
99 | return LPS22HB_BARO_NUMBER_OF_AXES; | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * @brief Retrieves raw data from the BaseBarometer. | ||
104 | * @note This data is retrieved from MEMS register without any algebraical | ||
105 | * manipulation. | ||
106 | * @note The axes array must be at least the same size of the | ||
107 | * BaseBarometer axes number. | ||
108 | * | ||
109 | * @param[in] ip pointer to @p BaseBarometer interface. | ||
110 | * @param[out] axes a buffer which would be filled with raw data. | ||
111 | * | ||
112 | * @return The operation status. | ||
113 | * @retval MSG_OK if the function succeeded. | ||
114 | * @retval MSG_RESET if one or more I2C errors occurred, the errors can | ||
115 | * be retrieved using @p i2cGetErrors(). | ||
116 | * @retval MSG_TIMEOUT if a timeout occurred before operation end. | ||
117 | */ | ||
118 | static msg_t baro_read_raw(void *ip, int32_t axes[]) { | ||
119 | LPS22HBDriver* devp; | ||
120 | uint8_t buff[3]; | ||
121 | msg_t msg; | ||
122 | |||
123 | osalDbgCheck((ip != NULL) && (axes != NULL)); | ||
124 | |||
125 | /* Getting parent instance pointer.*/ | ||
126 | devp = objGetInstance(LPS22HBDriver*, (BaseBarometer*)ip); | ||
127 | |||
128 | osalDbgAssert((devp->state == LPS22HB_READY), | ||
129 | "baro_read_raw(), invalid state"); | ||
130 | |||
131 | osalDbgAssert((devp->config->i2cp->state == I2C_READY), | ||
132 | "baro_read_raw(), channel not ready"); | ||
133 | |||
134 | #if LPS22HB_SHARED_I2C | ||
135 | i2cAcquireBus(devp->config->i2cp); | ||
136 | i2cStart(devp->config->i2cp, | ||
137 | devp->config->i2ccfg); | ||
138 | #endif /* LPS22HB_SHARED_I2C */ | ||
139 | |||
140 | msg = lps22hbI2CReadRegister(devp->config->i2cp, devp->config->slaveaddress, | ||
141 | LPS22HB_AD_PRESS_OUT_XL, buff, 3); | ||
142 | |||
143 | #if LPS22HB_SHARED_I2C | ||
144 | i2cReleaseBus(devp->config->i2cp); | ||
145 | #endif /* LPS22HB_SHARED_I2C */ | ||
146 | |||
147 | if(msg == MSG_OK) { | ||
148 | *axes = buff[0] + (buff[1] << 8) + (buff[2] << 16); | ||
149 | } | ||
150 | return msg; | ||
151 | } | ||
152 | |||
153 | /** | ||
154 | * @brief Retrieves cooked data from the BaseBarometer. | ||
155 | * @note This data is manipulated according to the formula | ||
156 | * cooked = (raw * sensitivity) - bias. | ||
157 | * @note Final data is expressed as hPa. | ||
158 | * @note The axes array must be at least the same size of the | ||
159 | * BaseBarometer axes number. | ||
160 | * | ||
161 | * @param[in] ip pointer to @p BaseBarometer interface. | ||
162 | * @param[out] axes a buffer which would be filled with cooked data. | ||
163 | * | ||
164 | * @return The operation status. | ||
165 | * @retval MSG_OK if the function succeeded. | ||
166 | * @retval MSG_RESET if one or more I2C errors occurred, the errors can | ||
167 | * be retrieved using @p i2cGetErrors(). | ||
168 | * @retval MSG_TIMEOUT if a timeout occurred before operation end. | ||
169 | */ | ||
170 | static msg_t baro_read_cooked(void *ip, float axes[]) { | ||
171 | LPS22HBDriver* devp; | ||
172 | int32_t raw; | ||
173 | msg_t msg; | ||
174 | |||
175 | osalDbgCheck((ip != NULL) && (axes != NULL)); | ||
176 | |||
177 | /* Getting parent instance pointer.*/ | ||
178 | devp = objGetInstance(LPS22HBDriver*, (BaseBarometer*)ip); | ||
179 | |||
180 | osalDbgAssert((devp->state == LPS22HB_READY), | ||
181 | "baro_read_cooked(), invalid state"); | ||
182 | |||
183 | msg = baro_read_raw(ip, &raw); | ||
184 | |||
185 | *axes = (raw * devp->barosensitivity) - devp->barobias; | ||
186 | |||
187 | return msg; | ||
188 | } | ||
189 | |||
190 | /** | ||
191 | * @brief Set bias values for the BaseBarometer. | ||
192 | * @note Bias must be expressed as hPa. | ||
193 | * @note The bias buffer must be at least the same size of the | ||
194 | * BaseBarometer axes number. | ||
195 | * | ||
196 | * @param[in] ip pointer to @p BaseBarometer interface. | ||
197 | * @param[in] bp a buffer which contains biases. | ||
198 | * | ||
199 | * @return The operation status. | ||
200 | * @retval MSG_OK if the function succeeded. | ||
201 | * @retval MSG_RESET if one or more I2C errors occurred, the errors can | ||
202 | * be retrieved using @p i2cGetErrors(). | ||
203 | * @retval MSG_TIMEOUT if a timeout occurred before operation end. | ||
204 | */ | ||
205 | static msg_t baro_set_bias(void *ip, float *bp) { | ||
206 | LPS22HBDriver* devp; | ||
207 | msg_t msg = MSG_OK; | ||
208 | |||
209 | osalDbgCheck((ip != NULL) && (bp != NULL)); | ||
210 | |||
211 | /* Getting parent instance pointer.*/ | ||
212 | devp = objGetInstance(LPS22HBDriver*, (BaseBarometer*)ip); | ||
213 | |||
214 | osalDbgAssert((devp->state == LPS22HB_READY), | ||
215 | "baro_set_bias(), invalid state"); | ||
216 | |||
217 | devp->barobias = *bp; | ||
218 | return msg; | ||
219 | } | ||
220 | |||
221 | /** | ||
222 | * @brief Reset bias values for the BaseBarometer. | ||
223 | * @note Default biases value are obtained from device datasheet when | ||
224 | * available otherwise they are considered zero. | ||
225 | * | ||
226 | * @param[in] ip pointer to @p BaseBarometer interface. | ||
227 | * | ||
228 | * @return The operation status. | ||
229 | * @retval MSG_OK if the function succeeded. | ||
230 | */ | ||
231 | static msg_t baro_reset_bias(void *ip) { | ||
232 | LPS22HBDriver* devp; | ||
233 | msg_t msg = MSG_OK; | ||
234 | |||
235 | osalDbgCheck(ip != NULL); | ||
236 | |||
237 | /* Getting parent instance pointer.*/ | ||
238 | devp = objGetInstance(LPS22HBDriver*, (BaseBarometer*)ip); | ||
239 | |||
240 | osalDbgAssert((devp->state == LPS22HB_READY), | ||
241 | "baro_reset_bias(), invalid state"); | ||
242 | |||
243 | devp->barobias = LPS22HB_BARO_SENS; | ||
244 | return msg; | ||
245 | } | ||
246 | |||
247 | /** | ||
248 | * @brief Set sensitivity values for the BaseBarometer. | ||
249 | * @note Sensitivity must be expressed as hPa/LSB. | ||
250 | * @note The sensitivity buffer must be at least the same size of the | ||
251 | * BaseBarometer axes number. | ||
252 | * | ||
253 | * @param[in] ip pointer to @p BaseBarometer interface. | ||
254 | * @param[in] sp a buffer which contains sensitivities. | ||
255 | * | ||
256 | * @return The operation status. | ||
257 | * @retval MSG_OK if the function succeeded. | ||
258 | */ | ||
259 | static msg_t baro_set_sensitivity(void *ip, float *sp) { | ||
260 | LPS22HBDriver* devp; | ||
261 | msg_t msg = MSG_OK; | ||
262 | |||
263 | osalDbgCheck((ip != NULL) && (sp != NULL)); | ||
264 | |||
265 | /* Getting parent instance pointer.*/ | ||
266 | devp = objGetInstance(LPS22HBDriver*, (BaseBarometer*)ip); | ||
267 | |||
268 | osalDbgAssert((devp->state == LPS22HB_READY), | ||
269 | "baro_set_sensitivity(), invalid state"); | ||
270 | |||
271 | devp->barosensitivity = *sp; | ||
272 | return msg; | ||
273 | } | ||
274 | |||
275 | /** | ||
276 | * @brief Reset sensitivity values for the BaseBarometer. | ||
277 | * @note Default sensitivities value are obtained from device datasheet. | ||
278 | * | ||
279 | * @param[in] ip pointer to @p BaseBarometer interface. | ||
280 | * | ||
281 | * @return The operation status. | ||
282 | * @retval MSG_OK if the function succeeded. | ||
283 | */ | ||
284 | static msg_t baro_reset_sensitivity(void *ip) { | ||
285 | LPS22HBDriver* devp; | ||
286 | msg_t msg = MSG_OK; | ||
287 | |||
288 | osalDbgCheck(ip != NULL); | ||
289 | |||
290 | /* Getting parent instance pointer.*/ | ||
291 | devp = objGetInstance(LPS22HBDriver*, (BaseBarometer*)ip); | ||
292 | |||
293 | osalDbgAssert((devp->state == LPS22HB_READY), | ||
294 | "baro_reset_sensitivity(), invalid state"); | ||
295 | |||
296 | devp->barosensitivity = LPS22HB_BARO_SENS; | ||
297 | return msg; | ||
298 | } | ||
299 | |||
300 | /** | ||
301 | * @brief Return the number of axes of the BaseThermometer. | ||
302 | * | ||
303 | * @param[in] ip pointer to @p BaseThermometer interface. | ||
304 | * | ||
305 | * @return the number of axes. | ||
306 | */ | ||
307 | static size_t thermo_get_axes_number(void *ip) { | ||
308 | (void)ip; | ||
309 | |||
310 | return LPS22HB_THERMO_NUMBER_OF_AXES; | ||
311 | } | ||
312 | |||
313 | /** | ||
314 | * @brief Retrieves raw data from the BaseThermometer. | ||
315 | * @note This data is retrieved from MEMS register without any algebraical | ||
316 | * manipulation. | ||
317 | * @note The axes array must be at least the same size of the | ||
318 | * BaseThermometer axes number. | ||
319 | * | ||
320 | * @param[in] ip pointer to @p BaseThermometer interface. | ||
321 | * @param[out] axes a buffer which would be filled with raw data. | ||
322 | * | ||
323 | * @return The operation status. | ||
324 | * @retval MSG_OK if the function succeeded. | ||
325 | * @retval MSG_RESET if one or more I2C errors occurred, the errors can | ||
326 | * be retrieved using @p i2cGetErrors(). | ||
327 | * @retval MSG_TIMEOUT if a timeout occurred before operation end. | ||
328 | */ | ||
329 | static msg_t thermo_read_raw(void *ip, int32_t axes[]) { | ||
330 | LPS22HBDriver* devp; | ||
331 | int16_t tmp; | ||
332 | uint8_t buff[2]; | ||
333 | msg_t msg; | ||
334 | |||
335 | osalDbgCheck((ip != NULL) && (axes != NULL)); | ||
336 | |||
337 | /* Getting parent instance pointer.*/ | ||
338 | devp = objGetInstance(LPS22HBDriver*, (BaseThermometer*)ip); | ||
339 | |||
340 | osalDbgAssert((devp->state == LPS22HB_READY), | ||
341 | "thermo_read_raw(), invalid state"); | ||
342 | |||
343 | osalDbgAssert((devp->config->i2cp->state == I2C_READY), | ||
344 | "thermo_read_raw(), channel not ready"); | ||
345 | |||
346 | #if LPS22HB_SHARED_I2C | ||
347 | i2cAcquireBus(devp->config->i2cp); | ||
348 | i2cStart(devp->config->i2cp, | ||
349 | devp->config->i2ccfg); | ||
350 | #endif /* LPS22HB_SHARED_I2C */ | ||
351 | |||
352 | msg = lps22hbI2CReadRegister(devp->config->i2cp, devp->config->slaveaddress, | ||
353 | LPS22HB_AD_TEMP_OUT_L, buff, 2); | ||
354 | |||
355 | #if LPS22HB_SHARED_I2C | ||
356 | i2cReleaseBus(devp->config->i2cp); | ||
357 | #endif /* LPS22HB_SHARED_I2C */ | ||
358 | |||
359 | if (msg == MSG_OK) { | ||
360 | tmp = buff[0] + (buff[1] << 8); | ||
361 | *axes = (int32_t)tmp; | ||
362 | } | ||
363 | return msg; | ||
364 | } | ||
365 | |||
366 | /** | ||
367 | * @brief Retrieves cooked data from the BaseThermometer. | ||
368 | * @note This data is manipulated according to the formula | ||
369 | * cooked = (raw * sensitivity) - bias. | ||
370 | * @note Final data is expressed as °C. | ||
371 | * @note The axes array must be at least the same size of the | ||
372 | * BaseThermometer axes number. | ||
373 | * | ||
374 | * @param[in] ip pointer to @p BaseThermometer interface. | ||
375 | * @param[out] axis a buffer which would be filled with cooked data. | ||
376 | * | ||
377 | * @return The operation status. | ||
378 | * @retval MSG_OK if the function succeeded. | ||
379 | * @retval MSG_RESET if one or more I2C errors occurred, the errors can | ||
380 | * be retrieved using @p i2cGetErrors(). | ||
381 | * @retval MSG_TIMEOUT if a timeout occurred before operation end. | ||
382 | */ | ||
383 | static msg_t thermo_read_cooked(void *ip, float* axis) { | ||
384 | LPS22HBDriver* devp; | ||
385 | int32_t raw; | ||
386 | msg_t msg; | ||
387 | |||
388 | osalDbgCheck((ip != NULL) && (axis != NULL)); | ||
389 | |||
390 | /* Getting parent instance pointer.*/ | ||
391 | devp = objGetInstance(LPS22HBDriver*, (BaseThermometer*)ip); | ||
392 | |||
393 | osalDbgAssert((devp->state == LPS22HB_READY), | ||
394 | "thermo_read_cooked(), invalid state"); | ||
395 | |||
396 | msg = thermo_read_raw(devp, &raw); | ||
397 | |||
398 | *axis = (raw * devp->thermosensitivity) - devp->thermobias; | ||
399 | |||
400 | return msg; | ||
401 | } | ||
402 | |||
403 | /** | ||
404 | * @brief Set bias values for the BaseThermometer. | ||
405 | * @note Bias must be expressed as °C. | ||
406 | * @note The bias buffer must be at least the same size of the | ||
407 | * BaseThermometer axes number. | ||
408 | * | ||
409 | * @param[in] ip pointer to @p BaseThermometer interface. | ||
410 | * @param[in] bp a buffer which contains biases. | ||
411 | * | ||
412 | * @return The operation status. | ||
413 | * @retval MSG_OK if the function succeeded. | ||
414 | */ | ||
415 | static msg_t thermo_set_bias(void *ip, float *bp) { | ||
416 | LPS22HBDriver* devp; | ||
417 | msg_t msg = MSG_OK; | ||
418 | |||
419 | osalDbgCheck((ip != NULL) && (bp != NULL)); | ||
420 | |||
421 | /* Getting parent instance pointer.*/ | ||
422 | devp = objGetInstance(LPS22HBDriver*, (BaseThermometer*)ip); | ||
423 | |||
424 | osalDbgAssert((devp->state == LPS22HB_READY), | ||
425 | "thermo_set_bias(), invalid state"); | ||
426 | |||
427 | devp->thermobias = *bp; | ||
428 | |||
429 | return msg; | ||
430 | } | ||
431 | |||
432 | /** | ||
433 | * @brief Reset bias values for the BaseThermometer. | ||
434 | * @note Default biases value are obtained from device datasheet when | ||
435 | * available otherwise they are considered zero. | ||
436 | * | ||
437 | * @param[in] ip pointer to @p BaseThermometer interface. | ||
438 | * | ||
439 | * @return The operation status. | ||
440 | * @retval MSG_OK if the function succeeded. | ||
441 | */ | ||
442 | static msg_t thermo_reset_bias(void *ip) { | ||
443 | LPS22HBDriver* devp; | ||
444 | msg_t msg = MSG_OK; | ||
445 | |||
446 | osalDbgCheck(ip != NULL); | ||
447 | |||
448 | /* Getting parent instance pointer.*/ | ||
449 | devp = objGetInstance(LPS22HBDriver*, (BaseThermometer*)ip); | ||
450 | |||
451 | osalDbgAssert((devp->state == LPS22HB_READY), | ||
452 | "thermo_reset_bias(), invalid state"); | ||
453 | |||
454 | devp->thermobias = LPS22HB_THERMO_BIAS; | ||
455 | |||
456 | return msg; | ||
457 | } | ||
458 | |||
459 | /** | ||
460 | * @brief Set sensitivity values for the BaseThermometer. | ||
461 | * @note Sensitivity must be expressed as °C/LSB. | ||
462 | * @note The sensitivity buffer must be at least the same size of the | ||
463 | * BaseThermometer axes number. | ||
464 | * | ||
465 | * @param[in] ip pointer to @p BaseThermometer interface. | ||
466 | * @param[in] sp a buffer which contains sensitivities. | ||
467 | * | ||
468 | * @return The operation status. | ||
469 | * @retval MSG_OK if the function succeeded. | ||
470 | */ | ||
471 | static msg_t thermo_set_sensitivity(void *ip, float *sp) { | ||
472 | LPS22HBDriver* devp; | ||
473 | msg_t msg = MSG_OK; | ||
474 | |||
475 | osalDbgCheck((ip != NULL) && (sp != NULL)); | ||
476 | |||
477 | /* Getting parent instance pointer.*/ | ||
478 | devp = objGetInstance(LPS22HBDriver*, (BaseThermometer*)ip); | ||
479 | |||
480 | osalDbgAssert((devp->state == LPS22HB_READY), | ||
481 | "thermo_set_sensitivity(), invalid state"); | ||
482 | |||
483 | devp->thermosensitivity = *sp; | ||
484 | |||
485 | return msg; | ||
486 | } | ||
487 | |||
488 | /** | ||
489 | * @brief Reset sensitivity values for the BaseThermometer. | ||
490 | * @note Default sensitivities value are obtained from device datasheet. | ||
491 | * | ||
492 | * @param[in] ip pointer to @p BaseThermometer interface. | ||
493 | * | ||
494 | * @return The operation status. | ||
495 | * @retval MSG_OK if the function succeeded. | ||
496 | */ | ||
497 | static msg_t thermo_reset_sensitivity(void *ip) { | ||
498 | LPS22HBDriver* devp; | ||
499 | msg_t msg = MSG_OK; | ||
500 | |||
501 | osalDbgCheck(ip != NULL); | ||
502 | |||
503 | /* Getting parent instance pointer.*/ | ||
504 | devp = objGetInstance(LPS22HBDriver*, (BaseThermometer*)ip); | ||
505 | |||
506 | osalDbgAssert((devp->state == LPS22HB_READY), | ||
507 | "thermo_reset_sensitivity(), invalid state"); | ||
508 | |||
509 | devp->thermosensitivity = LPS22HB_THERMO_SENS; | ||
510 | |||
511 | return msg; | ||
512 | } | ||
513 | |||
514 | static const struct LPS22HBVMT vmt_device = { | ||
515 | (size_t)0 | ||
516 | }; | ||
517 | |||
518 | static const struct BaseBarometerVMT vmt_barometer = { | ||
519 | sizeof(struct LPS22HBVMT*), | ||
520 | baro_get_axes_number, baro_read_raw, baro_read_cooked, | ||
521 | baro_set_bias, baro_reset_bias, baro_set_sensitivity, | ||
522 | baro_reset_sensitivity | ||
523 | }; | ||
524 | |||
525 | static const struct BaseThermometerVMT vmt_thermometer = { | ||
526 | sizeof(struct LPS22HBVMT*) + sizeof(BaseBarometer), | ||
527 | thermo_get_axes_number, thermo_read_raw, thermo_read_cooked, | ||
528 | thermo_set_bias, thermo_reset_bias, thermo_set_sensitivity, | ||
529 | thermo_reset_sensitivity | ||
530 | }; | ||
531 | |||
532 | /*===========================================================================*/ | ||
533 | /* Driver exported functions. */ | ||
534 | /*===========================================================================*/ | ||
535 | |||
536 | /** | ||
537 | * @brief Initializes an instance. | ||
538 | * | ||
539 | * @param[out] devp pointer to the @p LPS22HBDriver object | ||
540 | * | ||
541 | * @init | ||
542 | */ | ||
543 | void lps22hbObjectInit(LPS22HBDriver *devp) { | ||
544 | |||
545 | devp->vmt = &vmt_device; | ||
546 | devp->baro_if.vmt = &vmt_barometer; | ||
547 | devp->thermo_if.vmt = &vmt_thermometer; | ||
548 | |||
549 | devp->config = NULL; | ||
550 | |||
551 | devp->baroaxes = LPS22HB_BARO_NUMBER_OF_AXES; | ||
552 | devp->thermoaxes = LPS22HB_THERMO_NUMBER_OF_AXES; | ||
553 | |||
554 | devp->state = LPS22HB_STOP; | ||
555 | } | ||
556 | |||
557 | /** | ||
558 | * @brief Configures and activates LPS22HB Complex Driver peripheral. | ||
559 | * | ||
560 | * @param[in] devp pointer to the @p LPS22HBDriver object | ||
561 | * @param[in] config pointer to the @p LPS22HBConfig object | ||
562 | * | ||
563 | * @api | ||
564 | */ | ||
565 | void lps22hbStart(LPS22HBDriver *devp, const LPS22HBConfig *config) { | ||
566 | uint8_t cr[2]; | ||
567 | osalDbgCheck((devp != NULL) && (config != NULL)); | ||
568 | |||
569 | osalDbgAssert((devp->state == LPS22HB_STOP) || (devp->state == LPS22HB_READY), | ||
570 | "lps22hbStart(), invalid state"); | ||
571 | |||
572 | devp->config = config; | ||
573 | |||
574 | /* Enabling register auto-increment.*/ | ||
575 | /* Control register 1 configuration block.*/ | ||
576 | { | ||
577 | cr[0] = LPS22HB_AD_CTRL_REG2; | ||
578 | cr[1] = LPS22HB_CTRL_REG2_IF_ADD_INC; | ||
579 | } | ||
580 | #if LPS22HB_SHARED_I2C | ||
581 | i2cAcquireBus(devp->config->i2cp); | ||
582 | #endif /* LPS22HB_SHARED_I2C */ | ||
583 | |||
584 | i2cStart(devp->config->i2cp, devp->config->i2ccfg); | ||
585 | lps22hbI2CWriteRegister(devp->config->i2cp, devp->config->slaveaddress, | ||
586 | cr, 1); | ||
587 | |||
588 | #if LPS22HB_SHARED_I2C | ||
589 | i2cReleaseBus((devp)->config->i2cp); | ||
590 | #endif /* LPS22HB_SHARED_I2C */ | ||
591 | |||
592 | /* Control register 1 configuration block.*/ | ||
593 | { | ||
594 | cr[0] = LPS22HB_AD_CTRL_REG1; | ||
595 | cr[1] = devp->config->outputdatarate; | ||
596 | #if LPS22HB_USE_ADVANCED || defined(__DOXYGEN__) | ||
597 | cr[1] |= devp->config->blockdataupdate; | ||
598 | cr[1] |= devp->config->lowpass_filter; | ||
599 | #endif | ||
600 | } | ||
601 | |||
602 | #if LPS22HB_SHARED_I2C | ||
603 | i2cAcquireBus((devp)->config->i2cp); | ||
604 | i2cStart((devp)->config->i2cp, | ||
605 | (devp)->config->i2ccfg); | ||
606 | #endif /* LPS22HB_SHARED_I2C */ | ||
607 | |||
608 | lps22hbI2CWriteRegister(devp->config->i2cp, devp->config->slaveaddress, cr, 1); | ||
609 | |||
610 | #if LPS22HB_SHARED_I2C | ||
611 | i2cReleaseBus((devp)->config->i2cp); | ||
612 | #endif /* LPS22HB_SHARED_I2C */ | ||
613 | |||
614 | if(devp->config->barosensitivity == NULL) { | ||
615 | devp->barosensitivity = LPS22HB_BARO_SENS; | ||
616 | } | ||
617 | else{ | ||
618 | /* Taking barometer sensitivity from user configurations */ | ||
619 | devp->barosensitivity = *(devp->config->barosensitivity); | ||
620 | } | ||
621 | |||
622 | if(devp->config->barobias == NULL) { | ||
623 | devp->barobias = LPS22HB_BARO_BIAS; | ||
624 | } | ||
625 | else{ | ||
626 | /* Taking barometer bias from user configurations */ | ||
627 | devp->barobias = *(devp->config->barobias); | ||
628 | } | ||
629 | |||
630 | if(devp->config->thermosensitivity == NULL) { | ||
631 | devp->thermosensitivity = LPS22HB_THERMO_SENS; | ||
632 | } | ||
633 | else{ | ||
634 | /* Taking thermometer sensitivity from user configurations */ | ||
635 | devp->thermosensitivity = *(devp->config->thermosensitivity); | ||
636 | } | ||
637 | |||
638 | if(devp->config->thermobias == NULL) { | ||
639 | devp->thermobias = LPS22HB_THERMO_BIAS; | ||
640 | } | ||
641 | else{ | ||
642 | /* Taking thermometer bias from user configurations */ | ||
643 | devp->thermobias = *(devp->config->thermobias); | ||
644 | } | ||
645 | |||
646 | /* This is the Barometer transient recovery time */ | ||
647 | osalThreadSleepMilliseconds(5); | ||
648 | |||
649 | devp->state = LPS22HB_READY; | ||
650 | } | ||
651 | |||
652 | /** | ||
653 | * @brief Deactivates the LPS22HB Complex Driver peripheral. | ||
654 | * | ||
655 | * @param[in] devp pointer to the @p LPS22HBDriver object | ||
656 | * | ||
657 | * @api | ||
658 | */ | ||
659 | void lps22hbStop(LPS22HBDriver *devp) { | ||
660 | uint8_t cr[2]; | ||
661 | |||
662 | osalDbgCheck(devp != NULL); | ||
663 | |||
664 | osalDbgAssert((devp->state == LPS22HB_STOP) || (devp->state == LPS22HB_READY), | ||
665 | "lps22hbStop(), invalid state"); | ||
666 | |||
667 | if (devp->state == LPS22HB_READY) { | ||
668 | #if LPS22HB_SHARED_I2C | ||
669 | i2cAcquireBus((devp)->config->i2cp); | ||
670 | i2cStart((devp)->config->i2cp, | ||
671 | (devp)->config->i2ccfg); | ||
672 | #endif /* LPS22HB_SHARED_I2C */ | ||
673 | |||
674 | cr[0] = LPS22HB_AD_CTRL_REG1; | ||
675 | cr[1] = 0; | ||
676 | lps22hbI2CWriteRegister(devp->config->i2cp, devp->config->slaveaddress, | ||
677 | cr, 1); | ||
678 | |||
679 | i2cStop((devp)->config->i2cp); | ||
680 | #if LPS22HB_SHARED_I2C | ||
681 | i2cReleaseBus((devp)->config->i2cp); | ||
682 | #endif /* LPS22HB_SHARED_I2C */ | ||
683 | } | ||
684 | devp->state = LPS22HB_STOP; | ||
685 | } | ||
686 | /** @} */ | ||