lps22hb.c 21 KB

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