hts221.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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 hts221.c
  17. * @brief HTS221 MEMS interface module code.
  18. *
  19. * @addtogroup HTS221
  20. * @ingroup EX_ST
  21. * @{
  22. */
  23. #include "hal.h"
  24. #include "hts221.h"
  25. /*===========================================================================*/
  26. /* Driver local definitions. */
  27. /*===========================================================================*/
  28. #define HTS221_SEL(mask, offset) (int16_t)(mask << offset)
  29. #define HTS221_FLAG_HYGRO_BIAS 0x01
  30. #define HTS221_FLAG_HYGRO_SENS 0x02
  31. #define HTS221_FLAG_THERMO_BIAS 0x04
  32. #define HTS221_FLAG_THERMO_SENS 0x08
  33. /*===========================================================================*/
  34. /* Driver exported variables. */
  35. /*===========================================================================*/
  36. /*===========================================================================*/
  37. /* Driver local variables and types. */
  38. /*===========================================================================*/
  39. /*===========================================================================*/
  40. /* Driver local functions. */
  41. /*===========================================================================*/
  42. #if (HTS221_USE_I2C) || defined(__DOXYGEN__)
  43. /**
  44. * @brief Reads registers value using I2C.
  45. * @pre The I2C interface must be initialized and the driver started.
  46. *
  47. * @param[in] i2cp pointer to the I2C interface
  48. * @param[in] reg first sub-register address
  49. * @param[out] rxbuf pointer to an output buffer
  50. * @param[in] n number of consecutive register to read
  51. * @return the operation status.
  52. *
  53. * @notapi
  54. */
  55. static msg_t hts221I2CReadRegister(I2CDriver *i2cp, uint8_t reg, uint8_t* rxbuf,
  56. size_t n) {
  57. uint8_t txbuf = reg;
  58. if (n > 1)
  59. txbuf |= HTS221_SUB_MS;
  60. return i2cMasterTransmitTimeout(i2cp, HTS221_SAD, &txbuf, 1, rxbuf, n,
  61. TIME_INFINITE);
  62. }
  63. /**
  64. * @brief Writes a value into a register using I2C.
  65. * @pre The I2C interface must be initialized and the driver started.
  66. *
  67. * @param[in] i2cp pointer to the I2C interface
  68. * @param[in] txbuf buffer containing sub-address value in first position
  69. * and values to write
  70. * @param[in] n size of txbuf less one (not considering the first
  71. * element)
  72. * @return the operation status.
  73. *
  74. * @notapi
  75. */
  76. static msg_t hts221I2CWriteRegister(I2CDriver *i2cp, uint8_t* txbuf, size_t n) {
  77. if (n > 1)
  78. (*txbuf) |= HTS221_SUB_MS;
  79. return i2cMasterTransmitTimeout(i2cp, HTS221_SAD, txbuf, n + 1, NULL, 0,
  80. TIME_INFINITE);
  81. }
  82. #endif /* HTS221_USE_I2C */
  83. /**
  84. * @brief Computes biases and sensitivities starting from data stored in
  85. * calibration registers.
  86. * @note Factory bias and sensitivity values are stored into the driver
  87. * structure.
  88. *
  89. * @param[in] devp pointer to the HTS221 interface
  90. * @return the operation status.
  91. *
  92. * @notapi
  93. */
  94. static msg_t hts221Calibrate(HTS221Driver *devp) {
  95. msg_t msg;
  96. uint8_t calib[16], H0_rH_x2, H1_rH_x2, msb;
  97. int16_t H0_T0_OUT, H1_T0_OUT, T0_degC_x8, T1_degC_x8, T0_OUT, T1_OUT;
  98. /* Retrieving rH values from Calibration registers */
  99. msg = hts221I2CReadRegister(devp->config->i2cp,
  100. HTS221_AD_CALIB_0, calib, 16);
  101. H0_rH_x2 = calib[0];
  102. H1_rH_x2 = calib[1];
  103. H0_T0_OUT = calib[6];
  104. H0_T0_OUT += calib[7] << 8;
  105. H1_T0_OUT = calib[10];
  106. H1_T0_OUT += calib[11] << 8;
  107. T0_degC_x8 = calib[2];
  108. /* Completing T0_degC_x8 value */
  109. msb = (calib[5] & HTS221_SEL(0x03, 0));
  110. if (msb & HTS221_SEL(0x01, 1)) {
  111. msb |= HTS221_SEL(0x3F, 2);
  112. }
  113. T0_degC_x8 += msb << 8;
  114. T1_degC_x8 = calib[3];
  115. /* Completing T1_degC_x8 value */
  116. msb = ((calib[5] & HTS221_SEL(0x03, 2)) >> 2);
  117. if (msb & HTS221_SEL(0x01, 1)) {
  118. msb |= HTS221_SEL(0x3F, 2);
  119. }
  120. T1_degC_x8 += msb << 8;
  121. T0_OUT = calib[12];
  122. T0_OUT += calib[13] << 8;
  123. T1_OUT = calib[14];
  124. T1_OUT += calib[15] << 8;
  125. devp->hygrofactorysensitivity = ((float)H1_rH_x2 - (float)H0_rH_x2) /
  126. (((float)H1_T0_OUT - (float)H0_T0_OUT) * 2.0f);
  127. devp->hygrofactorybias = (devp->hygrofactorysensitivity * (float)H0_T0_OUT) -
  128. ((float)H0_rH_x2 / 2.0f);
  129. devp->thermofactorysensitivity = ((float)T1_degC_x8 - (float)T0_degC_x8) /
  130. (((float)T1_OUT - (float)T0_OUT) * 8.0f);
  131. devp->thermofactorybias = (devp->thermofactorysensitivity * (float)T0_OUT) -
  132. ((float)T0_degC_x8 / 8.0f);
  133. return msg;
  134. }
  135. /**
  136. * @brief Return the number of axes of the BaseHygrometer.
  137. *
  138. * @param[in] ip pointer to @p BaseHygrometer interface.
  139. *
  140. * @return the number of axes.
  141. */
  142. static size_t hygro_get_axes_number(void *ip) {
  143. (void)ip;
  144. return HTS221_HYGRO_NUMBER_OF_AXES;
  145. }
  146. /**
  147. * @brief Retrieves raw data from the BaseHygrometer.
  148. * @note This data is retrieved from MEMS register without any algebraical
  149. * manipulation.
  150. * @note The axes array must be at least the same size of the
  151. * BaseHygrometer axes number.
  152. *
  153. * @param[in] ip pointer to @p BaseHygrometer interface.
  154. * @param[out] axes a buffer which would be filled with raw data.
  155. *
  156. * @return The operation status.
  157. * @retval MSG_OK if the function succeeded.
  158. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  159. * be retrieved using @p i2cGetErrors().
  160. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  161. */
  162. static msg_t hygro_read_raw(void *ip, int32_t axes[]) {
  163. HTS221Driver* devp;
  164. uint8_t buff[2];
  165. int16_t tmp;
  166. msg_t msg;
  167. osalDbgCheck((ip != NULL) && (axes != NULL));
  168. /* Getting parent instance pointer.*/
  169. devp = objGetInstance(HTS221Driver*, (BaseHygrometer*)ip);
  170. osalDbgAssert((devp->state == HTS221_READY),
  171. "hygro_read_raw(), invalid state");
  172. osalDbgAssert((devp->config->i2cp->state == I2C_READY),
  173. "hygro_read_raw(), channel not ready");
  174. #if HTS221_SHARED_I2C
  175. i2cAcquireBus(devp->config->i2cp);
  176. i2cStart(devp->config->i2cp,
  177. devp->config->i2ccfg);
  178. #endif /* HTS221_SHARED_I2C */
  179. msg = hts221I2CReadRegister(devp->config->i2cp, HTS221_AD_HUMIDITY_OUT_L,
  180. buff, 2);
  181. #if HTS221_SHARED_I2C
  182. i2cReleaseBus(devp->config->i2cp);
  183. #endif /* HTS221_SHARED_I2C */
  184. if (msg == MSG_OK) {
  185. tmp = buff[0] + (buff[1] << 8);
  186. *axes = (int32_t)tmp;
  187. }
  188. return msg;
  189. }
  190. /**
  191. * @brief Retrieves cooked data from the BaseHygrometer.
  192. * @note This data is manipulated according to the formula
  193. * cooked = (raw * sensitivity) - bias.
  194. * @note Final data is expressed as %rH.
  195. * @note The axes array must be at least the same size of the
  196. * BaseHygrometer axes number.
  197. *
  198. * @param[in] ip pointer to @p BaseHygrometer interface.
  199. * @param[out] axes a buffer which would be filled with cooked data.
  200. *
  201. * @return The operation status.
  202. * @retval MSG_OK if the function succeeded.
  203. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  204. * be retrieved using @p i2cGetErrors().
  205. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  206. */
  207. static msg_t hygro_read_cooked(void *ip, float axes[]) {
  208. HTS221Driver* devp;
  209. int32_t raw;
  210. msg_t msg;
  211. osalDbgCheck((ip != NULL) && (axes != NULL));
  212. /* Getting parent instance pointer.*/
  213. devp = objGetInstance(HTS221Driver*, (BaseHygrometer*)ip);
  214. osalDbgAssert((devp->state == HTS221_READY),
  215. "hygro_read_cooked(), invalid state");
  216. msg = hygro_read_raw(ip, &raw);
  217. *axes = (raw * devp->hygrosensitivity) - devp->hygrobias;
  218. return msg;
  219. }
  220. /**
  221. * @brief Set bias values for the BaseHygrometer.
  222. * @note Bias must be expressed as %rH.
  223. * @note The bias buffer must be at least the same size of the
  224. * BaseHygrometer axes number.
  225. *
  226. * @param[in] ip pointer to @p BaseHygrometer interface.
  227. * @param[in] bp a buffer which contains biases.
  228. *
  229. * @return The operation status.
  230. * @retval MSG_OK if the function succeeded.
  231. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  232. * be retrieved using @p i2cGetErrors().
  233. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  234. */
  235. static msg_t hygro_set_bias(void *ip, float *bp) {
  236. HTS221Driver* devp;
  237. msg_t msg = MSG_OK;
  238. osalDbgCheck((ip != NULL) && (bp != NULL));
  239. /* Getting parent instance pointer.*/
  240. devp = objGetInstance(HTS221Driver*, (BaseHygrometer*)ip);
  241. osalDbgAssert((devp->state == HTS221_READY),
  242. "hygro_set_bias(), invalid state");
  243. devp->hygrobias = *bp;
  244. return msg;
  245. }
  246. /**
  247. * @brief Reset bias values for the BaseHygrometer.
  248. * @note Default biases value are obtained from device datasheet when
  249. * available otherwise they are considered zero.
  250. *
  251. * @param[in] ip pointer to @p BaseHygrometer interface.
  252. *
  253. * @return The operation status.
  254. * @retval MSG_OK if the function succeeded.
  255. */
  256. static msg_t hygro_reset_bias(void *ip) {
  257. HTS221Driver* devp;
  258. msg_t msg = MSG_OK;
  259. osalDbgCheck(ip != NULL);
  260. /* Getting parent instance pointer.*/
  261. devp = objGetInstance(HTS221Driver*, (BaseHygrometer*)ip);
  262. osalDbgAssert((devp->state == HTS221_READY),
  263. "hygro_reset_bias(), invalid state");
  264. devp->hygrobias = devp->hygrofactorybias;
  265. return msg;
  266. }
  267. /**
  268. * @brief Set sensitivity values for the BaseHygrometer.
  269. * @note Sensitivity must be expressed as %rH/LSB.
  270. * @note The sensitivity buffer must be at least the same size of the
  271. * BaseHygrometer axes number.
  272. *
  273. * @param[in] ip pointer to @p BaseHygrometer interface.
  274. * @param[in] sp a buffer which contains sensitivities.
  275. *
  276. * @return The operation status.
  277. * @retval MSG_OK if the function succeeded.
  278. */
  279. static msg_t hygro_set_sensitivity(void *ip, float *sp) {
  280. HTS221Driver* devp;
  281. msg_t msg = MSG_OK;
  282. osalDbgCheck((ip != NULL) && (sp != NULL));
  283. /* Getting parent instance pointer.*/
  284. devp = objGetInstance(HTS221Driver*, (BaseHygrometer*)ip);
  285. osalDbgAssert((devp->state == HTS221_READY),
  286. "hygro_set_sensitivity(), invalid state");
  287. devp->hygrosensitivity = *sp;
  288. return msg;
  289. }
  290. /**
  291. * @brief Reset sensitivity values for the BaseHygrometer.
  292. * @note Default sensitivities value are obtained from device datasheet.
  293. *
  294. * @param[in] ip pointer to @p BaseHygrometer interface.
  295. *
  296. * @return The operation status.
  297. * @retval MSG_OK if the function succeeded.
  298. */
  299. static msg_t hygro_reset_sensitivity(void *ip) {
  300. HTS221Driver* devp;
  301. msg_t msg = MSG_OK;
  302. osalDbgCheck(ip != NULL);
  303. /* Getting parent instance pointer.*/
  304. devp = objGetInstance(HTS221Driver*, (BaseHygrometer*)ip);
  305. osalDbgAssert((devp->state == HTS221_READY),
  306. "hygro_reset_sensitivity(), invalid state");
  307. devp->hygrosensitivity = devp->hygrofactorysensitivity;
  308. return msg;
  309. }
  310. /**
  311. * @brief Return the number of axes of the BaseThermometer.
  312. *
  313. * @param[in] ip pointer to @p BaseThermometer interface.
  314. *
  315. * @return the number of axes.
  316. */
  317. static size_t thermo_get_axes_number(void *ip) {
  318. (void)ip;
  319. return HTS221_THERMO_NUMBER_OF_AXES;
  320. }
  321. /**
  322. * @brief Retrieves raw data from the BaseThermometer.
  323. * @note This data is retrieved from MEMS register without any algebraical
  324. * manipulation.
  325. * @note The axes array must be at least the same size of the
  326. * BaseThermometer axes number.
  327. *
  328. * @param[in] ip pointer to @p BaseThermometer interface.
  329. * @param[out] axes a buffer which would be filled with raw data.
  330. *
  331. * @return The operation status.
  332. * @retval MSG_OK if the function succeeded.
  333. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  334. * be retrieved using @p i2cGetErrors().
  335. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  336. */
  337. static msg_t thermo_read_raw(void *ip, int32_t axes[]) {
  338. HTS221Driver* devp;
  339. int16_t tmp;
  340. uint8_t buff[2];
  341. msg_t msg;
  342. osalDbgCheck((ip != NULL) && (axes != NULL));
  343. /* Getting parent instance pointer.*/
  344. devp = objGetInstance(HTS221Driver*, (BaseThermometer*)ip);
  345. osalDbgAssert((devp->state == HTS221_READY),
  346. "thermo_read_raw(), invalid state");
  347. osalDbgAssert((devp->config->i2cp->state == I2C_READY),
  348. "thermo_read_raw(), channel not ready");
  349. #if HTS221_SHARED_I2C
  350. i2cAcquireBus(devp->config->i2cp);
  351. i2cStart(devp->config->i2cp,
  352. devp->config->i2ccfg);
  353. #endif /* HTS221_SHARED_I2C */
  354. msg = hts221I2CReadRegister(devp->config->i2cp, HTS221_AD_TEMP_OUT_L,
  355. buff, 2);
  356. #if HTS221_SHARED_I2C
  357. i2cReleaseBus(devp->config->i2cp);
  358. #endif /* HTS221_SHARED_I2C */
  359. if (msg == MSG_OK) {
  360. tmp = buff[0] + (buff[1] << 8);
  361. *axes = (int32_t)tmp;
  362. }
  363. return msg;
  364. }
  365. /**
  366. * @brief Retrieves cooked data from the BaseThermometer.
  367. * @note This data is manipulated according to the formula
  368. * cooked = (raw * sensitivity) - bias.
  369. * @note Final data is expressed as °C.
  370. * @note The axes array must be at least the same size of the
  371. * BaseThermometer axes number.
  372. *
  373. * @param[in] ip pointer to @p BaseThermometer interface.
  374. * @param[out] axis a buffer which would be filled with cooked data.
  375. *
  376. * @return The operation status.
  377. * @retval MSG_OK if the function succeeded.
  378. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  379. * be retrieved using @p i2cGetErrors().
  380. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  381. */
  382. static msg_t thermo_read_cooked(void *ip, float* axis) {
  383. HTS221Driver* devp;
  384. int32_t raw;
  385. msg_t msg;
  386. osalDbgCheck((ip != NULL) && (axis != NULL));
  387. /* Getting parent instance pointer.*/
  388. devp = objGetInstance(HTS221Driver*, (BaseThermometer*)ip);
  389. osalDbgAssert((devp->state == HTS221_READY),
  390. "thermo_read_cooked(), invalid state");
  391. msg = thermo_read_raw(devp, &raw);
  392. *axis = (raw * devp->thermosensitivity) - devp->thermobias;
  393. return msg;
  394. }
  395. /**
  396. * @brief Set bias values for the BaseThermometer.
  397. * @note Bias must be expressed as °C.
  398. * @note The bias buffer must be at least the same size of the
  399. * BaseThermometer axes number.
  400. *
  401. * @param[in] ip pointer to @p BaseThermometer interface.
  402. * @param[in] bp a buffer which contains biases.
  403. *
  404. * @return The operation status.
  405. * @retval MSG_OK if the function succeeded.
  406. */
  407. static msg_t thermo_set_bias(void *ip, float *bp) {
  408. HTS221Driver* devp;
  409. msg_t msg = MSG_OK;
  410. osalDbgCheck((ip != NULL) && (bp != NULL));
  411. /* Getting parent instance pointer.*/
  412. devp = objGetInstance(HTS221Driver*, (BaseThermometer*)ip);
  413. osalDbgAssert((devp->state == HTS221_READY),
  414. "thermo_set_bias(), invalid state");
  415. devp->thermobias = *bp;
  416. return msg;
  417. }
  418. /**
  419. * @brief Reset bias values for the BaseThermometer.
  420. * @note Default biases value are obtained from device datasheet when
  421. * available otherwise they are considered zero.
  422. *
  423. * @param[in] ip pointer to @p BaseThermometer interface.
  424. *
  425. * @return The operation status.
  426. * @retval MSG_OK if the function succeeded.
  427. */
  428. static msg_t thermo_reset_bias(void *ip) {
  429. HTS221Driver* devp;
  430. msg_t msg = MSG_OK;
  431. osalDbgCheck(ip != NULL);
  432. /* Getting parent instance pointer.*/
  433. devp = objGetInstance(HTS221Driver*, (BaseThermometer*)ip);
  434. osalDbgAssert((devp->state == HTS221_READY),
  435. "thermo_reset_bias(), invalid state");
  436. devp->thermobias = devp->thermofactorybias;
  437. return msg;
  438. }
  439. /**
  440. * @brief Set sensitivity values for the BaseThermometer.
  441. * @note Sensitivity must be expressed as °C/LSB.
  442. * @note The sensitivity buffer must be at least the same size of the
  443. * BaseThermometer axes number.
  444. *
  445. * @param[in] ip pointer to @p BaseThermometer interface.
  446. * @param[in] sp a buffer which contains sensitivities.
  447. *
  448. * @return The operation status.
  449. * @retval MSG_OK if the function succeeded.
  450. */
  451. static msg_t thermo_set_sensitivity(void *ip, float *sp) {
  452. HTS221Driver* devp;
  453. msg_t msg = MSG_OK;
  454. osalDbgCheck((ip != NULL) && (sp != NULL));
  455. /* Getting parent instance pointer.*/
  456. devp = objGetInstance(HTS221Driver*, (BaseThermometer*)ip);
  457. osalDbgAssert((devp->state == HTS221_READY),
  458. "thermo_set_sensitivity(), invalid state");
  459. devp->thermosensitivity = *sp;
  460. return msg;
  461. }
  462. /**
  463. * @brief Reset sensitivity values for the BaseThermometer.
  464. * @note Default sensitivities value are obtained from device datasheet.
  465. *
  466. * @param[in] ip pointer to @p BaseThermometer interface.
  467. *
  468. * @return The operation status.
  469. * @retval MSG_OK if the function succeeded.
  470. */
  471. static msg_t thermo_reset_sensitivity(void *ip) {
  472. HTS221Driver* devp;
  473. msg_t msg = MSG_OK;
  474. osalDbgCheck(ip != NULL);
  475. /* Getting parent instance pointer.*/
  476. devp = objGetInstance(HTS221Driver*, (BaseThermometer*)ip);
  477. osalDbgAssert((devp->state == HTS221_READY),
  478. "thermo_reset_sensitivity(), invalid state");
  479. devp->thermosensitivity = devp->thermofactorysensitivity;
  480. return msg;
  481. }
  482. static const struct HTS221VMT vmt_device = {
  483. (size_t)0
  484. };
  485. static const struct BaseHygrometerVMT vmt_hygrometer = {
  486. sizeof(struct HTS221VMT*),
  487. hygro_get_axes_number, hygro_read_raw, hygro_read_cooked,
  488. hygro_set_bias, hygro_reset_bias, hygro_set_sensitivity,
  489. hygro_reset_sensitivity
  490. };
  491. static const struct BaseThermometerVMT vmt_thermometer = {
  492. sizeof(struct HTS221VMT*) + sizeof(BaseHygrometer),
  493. thermo_get_axes_number, thermo_read_raw, thermo_read_cooked,
  494. thermo_set_bias, thermo_reset_bias, thermo_set_sensitivity,
  495. thermo_reset_sensitivity
  496. };
  497. /*===========================================================================*/
  498. /* Driver exported functions. */
  499. /*===========================================================================*/
  500. /**
  501. * @brief Initializes an instance.
  502. *
  503. * @param[out] devp pointer to the @p HTS221Driver object
  504. *
  505. * @init
  506. */
  507. void hts221ObjectInit(HTS221Driver *devp) {
  508. devp->vmt = &vmt_device;
  509. devp->hygro_if.vmt = &vmt_hygrometer;
  510. devp->thermo_if.vmt = &vmt_thermometer;
  511. devp->config = NULL;
  512. devp->hygroaxes = HTS221_HYGRO_NUMBER_OF_AXES;
  513. devp->thermoaxes = HTS221_THERMO_NUMBER_OF_AXES;
  514. devp->hygrobias = 0.0f;
  515. devp->thermobias = 0.0f;
  516. devp->state = HTS221_STOP;
  517. }
  518. /**
  519. * @brief Configures and activates HTS221 Complex Driver peripheral.
  520. *
  521. * @param[in] devp pointer to the @p HTS221Driver object
  522. * @param[in] config pointer to the @p HTS221Config object
  523. *
  524. * @api
  525. */
  526. void hts221Start(HTS221Driver *devp, const HTS221Config *config) {
  527. uint8_t cr[2];
  528. osalDbgCheck((devp != NULL) && (config != NULL));
  529. osalDbgAssert((devp->state == HTS221_STOP) || (devp->state == HTS221_READY),
  530. "hts221Start(), invalid state");
  531. devp->config = config;
  532. #if HTS221_SHARED_I2C
  533. i2cAcquireBus(devp->config->i2cp);
  534. #endif /* HTS221_SHARED_I2C */
  535. /* Intializing the I2C. */
  536. i2cStart(devp->config->i2cp, devp->config->i2ccfg);
  537. hts221Calibrate(devp);
  538. #if HTS221_SHARED_I2C
  539. i2cReleaseBus(devp->config->i2cp);
  540. #endif /* HTS221_SHARED_I2C */
  541. if(devp->config->hygrosensitivity == NULL) {
  542. devp->hygrosensitivity = devp->hygrofactorysensitivity;
  543. }
  544. else{
  545. /* Taking hygrometer sensitivity from user configurations */
  546. devp->hygrosensitivity = *(devp->config->hygrosensitivity);
  547. }
  548. if(devp->config->hygrobias == NULL) {
  549. devp->hygrobias = devp->hygrofactorybias;
  550. }
  551. else{
  552. /* Taking hygrometer bias from user configurations */
  553. devp->hygrobias = *(devp->config->hygrobias);
  554. }
  555. if(devp->config->thermosensitivity == NULL) {
  556. devp->thermosensitivity = devp->thermofactorysensitivity;
  557. }
  558. else{
  559. /* Taking thermometer sensitivity from user configurations */
  560. devp->thermosensitivity = *(devp->config->thermosensitivity);
  561. }
  562. if(devp->config->thermobias == NULL) {
  563. devp->thermobias = devp->thermofactorybias;
  564. }
  565. else{
  566. /* Taking thermometer bias from user configurations */
  567. devp->thermobias = *(devp->config->thermobias);
  568. }
  569. /* Control register 1 configuration block.*/
  570. {
  571. cr[0] = HTS221_AD_CTRL_REG1;
  572. cr[1] = devp->config->outputdatarate | HTS221_CTRL_REG1_PD;
  573. #if HTS221_USE_ADVANCED || defined(__DOXYGEN__)
  574. cr[1] |= devp->config->blockdataupdate;
  575. #endif
  576. #if HTS221_SHARED_I2C
  577. i2cAcquireBus(devp->config->i2cp);
  578. i2cStart(devp->config->i2cp, devp->config->i2ccfg);
  579. #endif /* HTS221_SHARED_I2C */
  580. hts221I2CWriteRegister(devp->config->i2cp, cr, 1);
  581. #if HTS221_SHARED_I2C
  582. i2cReleaseBus(devp->config->i2cp);
  583. #endif /* HTS221_SHARED_I2C */
  584. }
  585. /* Average register configuration block.*/
  586. {
  587. cr[0] = HTS221_AD_AV_CONF;
  588. cr[1] = 0x05;
  589. #if HTS221_USE_ADVANCED || defined(__DOXYGEN__)
  590. cr[1] = devp->config->hygroresolution | devp->config->thermoresolution;
  591. #endif
  592. #if HTS221_SHARED_I2C
  593. i2cAcquireBus(devp->config->i2cp);
  594. i2cStart(devp->config->i2cp, devp->config->i2ccfg);
  595. #endif /* HTS221_SHARED_I2C */
  596. hts221I2CWriteRegister(devp->config->i2cp, cr, 1);
  597. #if HTS221_SHARED_I2C
  598. i2cReleaseBus(devp->config->i2cp);
  599. #endif /* HTS221_SHARED_I2C */
  600. }
  601. /* This is the MEMS transient recovery time */
  602. osalThreadSleepMilliseconds(5);
  603. devp->state = HTS221_READY;
  604. }
  605. /**
  606. * @brief Deactivates the HTS221 Complex Driver peripheral.
  607. *
  608. * @param[in] devp pointer to the @p HTS221Driver object
  609. *
  610. * @api
  611. */
  612. void hts221Stop(HTS221Driver *devp) {
  613. uint8_t cr[2];
  614. osalDbgCheck(devp != NULL);
  615. osalDbgAssert((devp->state == HTS221_STOP) || (devp->state == HTS221_READY),
  616. "hts221Stop(), invalid state");
  617. if (devp->state == HTS221_READY) {
  618. #if HTS221_SHARED_I2C
  619. i2cAcquireBus(devp->config->i2cp);
  620. i2cStart(devp->config->i2cp, devp->config->i2ccfg);
  621. #endif /* HTS221_SHARED_I2C */
  622. cr[0] = HTS221_AD_CTRL_REG1;
  623. cr[1] = 0;
  624. hts221I2CWriteRegister(devp->config->i2cp, cr, 1);
  625. i2cStop(devp->config->i2cp);
  626. #if HTS221_SHARED_I2C
  627. i2cReleaseBus(devp->config->i2cp);
  628. #endif /* HTS221_SHARED_I2C */
  629. }
  630. devp->state = HTS221_STOP;
  631. }
  632. /** @} */