hts221.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /**
  2. ******************************************************************************
  3. * @file hts221.c
  4. * @author MEMS Software Solutions Team
  5. * @brief HTS221 driver file
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. /* Includes ------------------------------------------------------------------*/
  36. #include "hts221.h"
  37. /** @addtogroup BSP BSP
  38. * @{
  39. */
  40. /** @addtogroup Component Component
  41. * @{
  42. */
  43. /** @defgroup HTS221 HTS221
  44. * @{
  45. */
  46. /** @defgroup HTS221_Exported_Variables HTS221 Exported Variables
  47. * @{
  48. */
  49. HTS221_CommonDrv_t HTS221_COMMON_Driver =
  50. {
  51. HTS221_Init,
  52. HTS221_DeInit,
  53. HTS221_ReadID,
  54. HTS221_GetCapabilities,
  55. };
  56. HTS221_HUM_Drv_t HTS221_HUM_Driver =
  57. {
  58. HTS221_HUM_Enable,
  59. HTS221_HUM_Disable,
  60. HTS221_HUM_GetOutputDataRate,
  61. HTS221_HUM_SetOutputDataRate,
  62. HTS221_HUM_GetHumidity,
  63. };
  64. HTS221_TEMP_Drv_t HTS221_TEMP_Driver =
  65. {
  66. HTS221_TEMP_Enable,
  67. HTS221_TEMP_Disable,
  68. HTS221_TEMP_GetOutputDataRate,
  69. HTS221_TEMP_SetOutputDataRate,
  70. HTS221_TEMP_GetTemperature,
  71. };
  72. /**
  73. * @}
  74. */
  75. /** @defgroup HTS221_Private_Function_Prototypes HTS221 Private Function Prototypes
  76. * @{
  77. */
  78. static int32_t ReadRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length);
  79. static int32_t WriteRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length);
  80. static int32_t HTS221_GetOutputDataRate(HTS221_Object_t *pObj, float *Odr);
  81. static int32_t HTS221_SetOutputDataRate(HTS221_Object_t *pObj, float Odr);
  82. static int32_t HTS221_Initialize(HTS221_Object_t *pObj);
  83. static float Linear_Interpolation(lin_t *Lin, float Coeff);
  84. /**
  85. * @}
  86. */
  87. /** @defgroup HTS221_Exported_Functions HTS221 Exported Functions
  88. * @{
  89. */
  90. /**
  91. * @brief Register Component Bus IO operations
  92. * @param pObj the device pObj
  93. * @retval 0 in case of success, an error code otherwise
  94. */
  95. int32_t HTS221_RegisterBusIO(HTS221_Object_t *pObj, HTS221_IO_t *pIO)
  96. {
  97. int32_t ret;
  98. if (pObj == NULL)
  99. {
  100. ret = HTS221_ERROR;
  101. }
  102. else
  103. {
  104. pObj->IO.Init = pIO->Init;
  105. pObj->IO.DeInit = pIO->DeInit;
  106. pObj->IO.BusType = pIO->BusType;
  107. pObj->IO.Address = pIO->Address;
  108. pObj->IO.WriteReg = pIO->WriteReg;
  109. pObj->IO.ReadReg = pIO->ReadReg;
  110. pObj->IO.GetTick = pIO->GetTick;
  111. pObj->Ctx.read_reg = ReadRegWrap;
  112. pObj->Ctx.write_reg = WriteRegWrap;
  113. pObj->Ctx.handle = pObj;
  114. if (pObj->IO.Init != NULL)
  115. {
  116. ret = pObj->IO.Init();
  117. }
  118. else
  119. {
  120. ret = HTS221_ERROR;
  121. }
  122. }
  123. return ret;
  124. }
  125. /**
  126. * @brief Initialize the HTS221 sensor
  127. * @param pObj the device pObj
  128. * @retval 0 in case of success, an error code otherwise
  129. */
  130. int32_t HTS221_Init(HTS221_Object_t *pObj)
  131. {
  132. if (pObj->is_initialized == 0U)
  133. {
  134. if (HTS221_Initialize(pObj) != HTS221_OK)
  135. {
  136. return HTS221_ERROR;
  137. }
  138. }
  139. pObj->is_initialized = 1;
  140. return HTS221_OK;
  141. }
  142. /**
  143. * @brief Deinitialize the HTS221 sensor
  144. * @param pObj the device pObj
  145. * @retval 0 in case of success, an error code otherwise
  146. */
  147. int32_t HTS221_DeInit(HTS221_Object_t *pObj)
  148. {
  149. if (pObj->is_initialized == 1U)
  150. {
  151. if (HTS221_HUM_Disable(pObj) != HTS221_OK)
  152. {
  153. return HTS221_ERROR;
  154. }
  155. if (HTS221_TEMP_Disable(pObj) != HTS221_OK)
  156. {
  157. return HTS221_ERROR;
  158. }
  159. }
  160. pObj->is_initialized = 0;
  161. return HTS221_OK;
  162. }
  163. /**
  164. * @brief Get WHO_AM_I value
  165. * @param pObj the device pObj
  166. * @param Id the WHO_AM_I value
  167. * @retval 0 in case of success, an error code otherwise
  168. */
  169. int32_t HTS221_ReadID(HTS221_Object_t *pObj, uint8_t *Id)
  170. {
  171. if (hts221_device_id_get(&(pObj->Ctx), Id) != HTS221_OK)
  172. {
  173. return HTS221_ERROR;
  174. }
  175. return HTS221_OK;
  176. }
  177. /**
  178. * @brief Get HTS221 sensor capabilities
  179. * @param pObj Component object pointer
  180. * @param Capabilities pointer to HTS221 sensor capabilities
  181. * @retval 0 in case of success, an error code otherwise
  182. */
  183. int32_t HTS221_GetCapabilities(HTS221_Object_t *pObj, HTS221_Capabilities_t *Capabilities)
  184. {
  185. /* Prevent unused argument(s) compilation warning */
  186. (void)(pObj);
  187. Capabilities->Humidity = 1;
  188. Capabilities->Pressure = 0;
  189. Capabilities->Temperature = 1;
  190. Capabilities->LowPower = 0;
  191. Capabilities->HumMaxOdr = 12.5f;
  192. Capabilities->TempMaxOdr = 12.5f;
  193. Capabilities->PressMaxOdr = 0.0f;
  194. return HTS221_OK;
  195. }
  196. /**
  197. * @brief Get the HTS221 initialization status
  198. * @param pObj the device pObj
  199. * @param Status 1 if initialized, 0 otherwise
  200. * @retval 0 in case of success, an error code otherwise
  201. */
  202. int32_t HTS221_Get_Init_Status(HTS221_Object_t *pObj, uint8_t *Status)
  203. {
  204. if (pObj == NULL)
  205. {
  206. return HTS221_ERROR;
  207. }
  208. *Status = pObj->is_initialized;
  209. return HTS221_OK;
  210. }
  211. /**
  212. * @brief Enable the HTS221 humidity sensor
  213. * @param pObj the device pObj
  214. * @retval 0 in case of success, an error code otherwise
  215. */
  216. int32_t HTS221_HUM_Enable(HTS221_Object_t *pObj)
  217. {
  218. /* Check if the component is already enabled */
  219. if (pObj->hum_is_enabled == 1U)
  220. {
  221. return HTS221_OK;
  222. }
  223. /* Check if the HTS221 temperature sensor is already enabled. */
  224. /* If yes, skip the enable function, if not call enable function */
  225. if (pObj->temp_is_enabled == 0U)
  226. {
  227. /* Power on the component. */
  228. if (hts221_power_on_set(&(pObj->Ctx), PROPERTY_ENABLE) != HTS221_OK)
  229. {
  230. return HTS221_ERROR;
  231. }
  232. }
  233. pObj->hum_is_enabled = 1;
  234. return HTS221_OK;
  235. }
  236. /**
  237. * @brief Disable the HTS221 humidity sensor
  238. * @param pObj the device pObj
  239. * @retval 0 in case of success, an error code otherwise
  240. */
  241. int32_t HTS221_HUM_Disable(HTS221_Object_t *pObj)
  242. {
  243. /* Check if the component is already disabled */
  244. if (pObj->hum_is_enabled == 0U)
  245. {
  246. return HTS221_OK;
  247. }
  248. /* Check if the HTS221 temperature sensor is still enable. */
  249. /* If yes, skip the disable function, if not call disable function */
  250. if (pObj->temp_is_enabled == 0U)
  251. {
  252. /* Power off the component. */
  253. if (hts221_power_on_set(&(pObj->Ctx), PROPERTY_DISABLE) != HTS221_OK)
  254. {
  255. return HTS221_ERROR;
  256. }
  257. }
  258. pObj->hum_is_enabled = 0;
  259. return HTS221_OK;
  260. }
  261. /**
  262. * @brief Get the HTS221 humidity sensor output data rate
  263. * @param pObj the device pObj
  264. * @param Odr pointer where the output data rate is written
  265. * @retval 0 in case of success, an error code otherwise
  266. */
  267. int32_t HTS221_HUM_GetOutputDataRate(HTS221_Object_t *pObj, float *Odr)
  268. {
  269. return HTS221_GetOutputDataRate(pObj, Odr);
  270. }
  271. /**
  272. * @brief Set the HTS221 humidity sensor output data rate
  273. * @param pObj the device pObj
  274. * @param Odr the output data rate value to be set
  275. * @retval 0 in case of success, an error code otherwise
  276. */
  277. int32_t HTS221_HUM_SetOutputDataRate(HTS221_Object_t *pObj, float Odr)
  278. {
  279. return HTS221_SetOutputDataRate(pObj, Odr);
  280. }
  281. /**
  282. * @brief Get the HTS221 humidity value
  283. * @param pObj the device pObj
  284. * @param Value pointer where the humidity value is written
  285. * @retval 0 in case of success, an error code otherwise
  286. */
  287. int32_t HTS221_HUM_GetHumidity(HTS221_Object_t *pObj, float *Value)
  288. {
  289. axis1bit16_t data_raw_humidity;
  290. axis1bit16_t coeff;
  291. lin_t lin_hum;
  292. if (hts221_hum_adc_point_0_get(&(pObj->Ctx), coeff.u8bit) != HTS221_OK)
  293. {
  294. return HTS221_ERROR;
  295. }
  296. lin_hum.x0 = (float)coeff.i16bit;
  297. if (hts221_hum_rh_point_0_get(&(pObj->Ctx), coeff.u8bit) != HTS221_OK)
  298. {
  299. return HTS221_ERROR;
  300. }
  301. lin_hum.y0 = (float)coeff.u8bit[0];
  302. if (hts221_hum_adc_point_1_get(&(pObj->Ctx), coeff.u8bit) != HTS221_OK)
  303. {
  304. return HTS221_ERROR;
  305. }
  306. lin_hum.x1 = (float)coeff.i16bit;
  307. if (hts221_hum_rh_point_1_get(&(pObj->Ctx), coeff.u8bit) != HTS221_OK)
  308. {
  309. return HTS221_ERROR;
  310. }
  311. lin_hum.y1 = (float)coeff.u8bit[0];
  312. (void)memset(data_raw_humidity.u8bit, 0x00, sizeof(int16_t));
  313. if (hts221_humidity_raw_get(&(pObj->Ctx), data_raw_humidity.u8bit) != HTS221_OK)
  314. {
  315. return HTS221_ERROR;
  316. }
  317. *Value = Linear_Interpolation(&lin_hum, (float)data_raw_humidity.i16bit);
  318. if (*Value < 0.0f)
  319. {
  320. *Value = 0.0f;
  321. }
  322. if (*Value > 100.0f)
  323. {
  324. *Value = 100.0f;
  325. }
  326. return HTS221_OK;
  327. }
  328. /**
  329. * @brief Get the HTS221 humidity data ready bit value
  330. * @param pObj the device pObj
  331. * @param Status the status of data ready bit
  332. * @retval 0 in case of success, an error code otherwise
  333. */
  334. int32_t HTS221_HUM_Get_DRDY_Status(HTS221_Object_t *pObj, uint8_t *Status)
  335. {
  336. if (hts221_hum_data_ready_get(&(pObj->Ctx), Status) != HTS221_OK)
  337. {
  338. return HTS221_ERROR;
  339. }
  340. return HTS221_OK;
  341. }
  342. /**
  343. * @brief Enable the HTS221 temperature sensor
  344. * @param pObj the device pObj
  345. * @retval 0 in case of success, an error code otherwise
  346. */
  347. int32_t HTS221_TEMP_Enable(HTS221_Object_t *pObj)
  348. {
  349. /* Check if the component is already enabled */
  350. if (pObj->temp_is_enabled == 1U)
  351. {
  352. return HTS221_OK;
  353. }
  354. /* Check if the HTS221 humidity sensor is already enabled. */
  355. /* If yes, skip the enable function, if not call enable function */
  356. if (pObj->hum_is_enabled == 0U)
  357. {
  358. /* Power on the component. */
  359. if (hts221_power_on_set(&(pObj->Ctx), PROPERTY_ENABLE) != HTS221_OK)
  360. {
  361. return HTS221_ERROR;
  362. }
  363. }
  364. pObj->temp_is_enabled = 1;
  365. return HTS221_OK;
  366. }
  367. /**
  368. * @brief Disable the HTS221 temperature sensor
  369. * @param pObj the device pObj
  370. * @retval 0 in case of success, an error code otherwise
  371. */
  372. int32_t HTS221_TEMP_Disable(HTS221_Object_t *pObj)
  373. {
  374. /* Check if the component is already disabled */
  375. if (pObj->temp_is_enabled == 0U)
  376. {
  377. return HTS221_OK;
  378. }
  379. /* Check if the HTS221 humidity sensor is still enable. */
  380. /* If yes, skip the disable function, if not call disable function */
  381. if (pObj->hum_is_enabled == 0U)
  382. {
  383. /* Power off the component. */
  384. if (hts221_power_on_set(&(pObj->Ctx), PROPERTY_DISABLE) != HTS221_OK)
  385. {
  386. return HTS221_ERROR;
  387. }
  388. }
  389. pObj->temp_is_enabled = 0;
  390. return HTS221_OK;
  391. }
  392. /**
  393. * @brief Get the HTS221 temperature sensor output data rate
  394. * @param pObj the device pObj
  395. * @param Odr pointer where the output data rate is written
  396. * @retval 0 in case of success, an error code otherwise
  397. */
  398. int32_t HTS221_TEMP_GetOutputDataRate(HTS221_Object_t *pObj, float *Odr)
  399. {
  400. return HTS221_GetOutputDataRate(pObj, Odr);
  401. }
  402. /**
  403. * @brief Set the HTS221 temperature sensor output data rate
  404. * @param pObj the device pObj
  405. * @param Odr the output data rate value to be set
  406. * @retval 0 in case of success, an error code otherwise
  407. */
  408. int32_t HTS221_TEMP_SetOutputDataRate(HTS221_Object_t *pObj, float Odr)
  409. {
  410. return HTS221_SetOutputDataRate(pObj, Odr);
  411. }
  412. /**
  413. * @brief Get the HTS221 temperature value
  414. * @param pObj the device pObj
  415. * @param Value pointer where the temperature value is written
  416. * @retval 0 in case of success, an error code otherwise
  417. */
  418. int32_t HTS221_TEMP_GetTemperature(HTS221_Object_t *pObj, float *Value)
  419. {
  420. axis1bit16_t data_raw_temperature;
  421. axis1bit16_t coeff;
  422. lin_t lin_temp;
  423. if (hts221_temp_adc_point_0_get(&(pObj->Ctx), coeff.u8bit) != HTS221_OK)
  424. {
  425. return HTS221_ERROR;
  426. }
  427. lin_temp.x0 = (float)coeff.i16bit;
  428. if (hts221_temp_deg_point_0_get(&(pObj->Ctx), coeff.u8bit) != HTS221_OK)
  429. {
  430. return HTS221_ERROR;
  431. }
  432. lin_temp.y0 = (float)coeff.u8bit[0];
  433. if (hts221_temp_adc_point_1_get(&(pObj->Ctx), coeff.u8bit) != HTS221_OK)
  434. {
  435. return HTS221_ERROR;
  436. }
  437. lin_temp.x1 = (float)coeff.i16bit;
  438. if (hts221_temp_deg_point_1_get(&(pObj->Ctx), coeff.u8bit) != HTS221_OK)
  439. {
  440. return HTS221_ERROR;
  441. }
  442. lin_temp.y1 = (float)coeff.u8bit[0];
  443. (void)memset(data_raw_temperature.u8bit, 0x00, sizeof(int16_t));
  444. if (hts221_temperature_raw_get(&(pObj->Ctx), data_raw_temperature.u8bit) != HTS221_OK)
  445. {
  446. return HTS221_ERROR;
  447. }
  448. *Value = Linear_Interpolation(&lin_temp, (float)data_raw_temperature.i16bit);
  449. return HTS221_OK;
  450. }
  451. /**
  452. * @brief Get the HTS221 temperature data ready bit value
  453. * @param pObj the device pObj
  454. * @param Status the status of data ready bit
  455. * @retval 0 in case of success, an error code otherwise
  456. */
  457. int32_t HTS221_TEMP_Get_DRDY_Status(HTS221_Object_t *pObj, uint8_t *Status)
  458. {
  459. if (hts221_temp_data_ready_get(&(pObj->Ctx), Status) != HTS221_OK)
  460. {
  461. return HTS221_ERROR;
  462. }
  463. return HTS221_OK;
  464. }
  465. /**
  466. * @brief Get the HTS221 register value
  467. * @param pObj the device pObj
  468. * @param Reg address to be read
  469. * @param Data pointer where the value is written
  470. * @retval 0 in case of success, an error code otherwise
  471. */
  472. int32_t HTS221_Read_Reg(HTS221_Object_t *pObj, uint8_t Reg, uint8_t *Data)
  473. {
  474. if (hts221_read_reg(&(pObj->Ctx), Reg, Data, 1) != HTS221_OK)
  475. {
  476. return HTS221_ERROR;
  477. }
  478. return HTS221_OK;
  479. }
  480. /**
  481. * @brief Set the HTS221 register value
  482. * @param pObj the device pObj
  483. * @param Reg address to be written
  484. * @param Data value to be written
  485. * @retval 0 in case of success, an error code otherwise
  486. */
  487. int32_t HTS221_Write_Reg(HTS221_Object_t *pObj, uint8_t Reg, uint8_t Data)
  488. {
  489. if (hts221_write_reg(&(pObj->Ctx), Reg, &Data, 1) != HTS221_OK)
  490. {
  491. return HTS221_ERROR;
  492. }
  493. return HTS221_OK;
  494. }
  495. /**
  496. * @}
  497. */
  498. /** @defgroup HTS221_Private_Functions HTS221 Private Functions
  499. * @{
  500. */
  501. /**
  502. * @brief Get output data rate
  503. * @param pObj the device pObj
  504. * @param Odr the output data rate value
  505. * @retval 0 in case of success, an error code otherwise
  506. */
  507. static int32_t HTS221_GetOutputDataRate(HTS221_Object_t *pObj, float *Odr)
  508. {
  509. int32_t ret = HTS221_OK;
  510. hts221_odr_t odr_low_level;
  511. if (hts221_data_rate_get(&(pObj->Ctx), &odr_low_level) != HTS221_OK)
  512. {
  513. return HTS221_ERROR;
  514. }
  515. switch (odr_low_level)
  516. {
  517. case HTS221_ONE_SHOT:
  518. *Odr = 0.0f;
  519. break;
  520. case HTS221_ODR_1Hz:
  521. *Odr = 1.0f;
  522. break;
  523. case HTS221_ODR_7Hz:
  524. *Odr = 7.0f;
  525. break;
  526. case HTS221_ODR_12Hz5:
  527. *Odr = 12.5f;
  528. break;
  529. default:
  530. ret = HTS221_ERROR;
  531. break;
  532. }
  533. return ret;
  534. }
  535. /**
  536. * @brief Set output data rate
  537. * @param pObj the device pObj
  538. * @param Odr the output data rate value to be set
  539. * @retval 0 in case of success, an error code otherwise
  540. */
  541. static int32_t HTS221_SetOutputDataRate(HTS221_Object_t *pObj, float Odr)
  542. {
  543. hts221_odr_t new_odr;
  544. new_odr = (Odr <= 1.0f) ? HTS221_ODR_1Hz
  545. : (Odr <= 7.0f) ? HTS221_ODR_7Hz
  546. : HTS221_ODR_12Hz5;
  547. if (hts221_data_rate_set(&(pObj->Ctx), new_odr) != HTS221_OK)
  548. {
  549. return HTS221_ERROR;
  550. }
  551. return HTS221_OK;
  552. }
  553. /**
  554. * @brief Initialize the HTS221 sensor
  555. * @param pObj the device pObj
  556. * @retval 0 in case of success, an error code otherwise
  557. */
  558. static int32_t HTS221_Initialize(HTS221_Object_t *pObj)
  559. {
  560. /* Power off the component. */
  561. if (hts221_power_on_set(&(pObj->Ctx), PROPERTY_DISABLE) != HTS221_OK)
  562. {
  563. return HTS221_ERROR;
  564. }
  565. /* Enable BDU */
  566. if (hts221_block_data_update_set(&(pObj->Ctx), PROPERTY_ENABLE) != HTS221_OK)
  567. {
  568. return HTS221_ERROR;
  569. }
  570. /* Set default ODR */
  571. if (HTS221_SetOutputDataRate(pObj, 1.0f) != HTS221_OK)
  572. {
  573. return HTS221_ERROR;
  574. }
  575. return HTS221_OK;
  576. }
  577. /**
  578. * @brief Function used to apply coefficient
  579. * @param Lin the line
  580. * @param Coeff the coefficient
  581. * @retval Calculation result
  582. */
  583. static float Linear_Interpolation(lin_t *Lin, float Coeff)
  584. {
  585. return (((Lin->y1 - Lin->y0) * Coeff) + ((Lin->x1 * Lin->y0) - (Lin->x0 * Lin->y1))) / (Lin->x1 - Lin->x0);
  586. }
  587. /**
  588. * @brief Wrap Read register component function to Bus IO function
  589. * @param Handle the device handler
  590. * @param Reg the register address
  591. * @param pData the stored data pointer
  592. * @param Length the length
  593. * @retval 0 in case of success, an error code otherwise
  594. */
  595. static int32_t ReadRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length)
  596. {
  597. HTS221_Object_t *pObj = (HTS221_Object_t *)Handle;
  598. if (pObj->IO.BusType == (uint32_t)HTS221_I2C_BUS) /* I2C */
  599. {
  600. /* Enable Multi-byte read */
  601. return pObj->IO.ReadReg(pObj->IO.Address, (Reg | 0x80U), pData, Length);
  602. }
  603. else /* SPI 3-Wires */
  604. {
  605. /* Enable Multi-byte read */
  606. return pObj->IO.ReadReg(pObj->IO.Address, (Reg | 0x40U), pData, Length);
  607. }
  608. }
  609. /**
  610. * @brief Wrap Write register component function to Bus IO function
  611. * @param Handle the device handler
  612. * @param Reg the register address
  613. * @param pData the stored data pointer
  614. * @param Length the length
  615. * @retval 0 in case of success, an error code otherwise
  616. */
  617. static int32_t WriteRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length)
  618. {
  619. HTS221_Object_t *pObj = (HTS221_Object_t *)Handle;
  620. if (pObj->IO.BusType == (uint32_t)HTS221_I2C_BUS) /* I2C */
  621. {
  622. /* Enable Multi-byte write */
  623. return pObj->IO.WriteReg(pObj->IO.Address, (Reg | 0x80U), pData, Length);
  624. }
  625. else /* SPI 3-Wires */
  626. {
  627. /* Enable Multi-byte write */
  628. return pObj->IO.WriteReg(pObj->IO.Address, (Reg | 0x40U), pData, Length);
  629. }
  630. }
  631. /**
  632. * @}
  633. */
  634. /**
  635. * @}
  636. */
  637. /**
  638. * @}
  639. */
  640. /**
  641. * @}
  642. */
  643. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/