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