sths34pf80.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-01-29 Rick the first version
  9. */
  10. #include "stdint.h"
  11. #include "sths34pf80.h"
  12. /** @defgroup STHS34PF80_Private_Function_Prototypes STHS34PF80 Private Function Prototypes
  13. * @{
  14. */
  15. static int32_t ReadRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length);
  16. static int32_t WriteRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length);
  17. static int32_t STHS34PF80_Initialize(STHS34PF80_Object_t *pObj);
  18. /**
  19. * @brief Wrap Read register component function to Bus IO function
  20. * @param Handle the device handler
  21. * @param Reg the register address
  22. * @param pData the stored data pointer
  23. * @param Length the length
  24. * @retval 0 in case of success, an error code otherwise
  25. */
  26. static int32_t ReadRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length)
  27. {
  28. uint16_t i;
  29. int32_t ret = STHS34PF80_OK;
  30. STHS34PF80_Object_t *pObj = (STHS34PF80_Object_t *)Handle;
  31. if (pObj->IO.BusType == STHS34PF80_I2C_BUS) /* I2C */
  32. {
  33. for (i = 0; i < Length; i++)
  34. {
  35. ret = pObj->IO.ReadReg(pObj->IO.Address, (Reg + i), &pData[i], 1);
  36. if (ret != STHS34PF80_OK)
  37. {
  38. return STHS34PF80_ERROR;
  39. }
  40. }
  41. return ret;
  42. }
  43. else /* SPI 4-Wires or SPI 3-Wires */
  44. {
  45. return pObj->IO.ReadReg(pObj->IO.Address, Reg, pData, Length);
  46. }
  47. }
  48. /**
  49. * @brief Wrap Write register component function to Bus IO function
  50. * @param Handle the device handler
  51. * @param Reg the register address
  52. * @param pData the stored data pointer
  53. * @param Length the length
  54. * @retval 0 in case of success, an error code otherwise
  55. */
  56. static int32_t WriteRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length)
  57. {
  58. uint16_t i;
  59. int32_t ret = STHS34PF80_OK;
  60. STHS34PF80_Object_t *pObj = (STHS34PF80_Object_t *)Handle;
  61. if (pObj->IO.BusType == STHS34PF80_I2C_BUS) /* I2C */
  62. {
  63. for (i = 0; i < Length; i++)
  64. {
  65. ret = pObj->IO.WriteReg(pObj->IO.Address, (Reg + i), &pData[i], 1);
  66. if (ret != STHS34PF80_OK)
  67. {
  68. return STHS34PF80_ERROR;
  69. }
  70. }
  71. return ret;
  72. }
  73. else /* SPI 4-Wires or SPI 3-Wires */
  74. {
  75. return pObj->IO.WriteReg(pObj->IO.Address, Reg, pData, Length);
  76. }
  77. }
  78. /**
  79. * @brief Register Component Bus IO operations
  80. * @param pObj the device pObj
  81. * @retval 0 in case of success, an error code otherwise
  82. */
  83. int32_t STHS34PF80_RegisterBusIO(STHS34PF80_Object_t *pObj, STHS34PF80_IO_t *pIO)
  84. {
  85. int32_t ret = STHS34PF80_OK;
  86. if (pObj == NULL)
  87. {
  88. ret = STHS34PF80_ERROR;
  89. }
  90. else
  91. {
  92. pObj->IO.Init = pIO->Init;
  93. pObj->IO.DeInit = pIO->DeInit;
  94. pObj->IO.BusType = pIO->BusType;
  95. pObj->IO.Address = pIO->Address;
  96. pObj->IO.WriteReg = pIO->WriteReg;
  97. pObj->IO.ReadReg = pIO->ReadReg;
  98. pObj->IO.GetTick = pIO->GetTick;
  99. pObj->Ctx.read_reg = ReadRegWrap;
  100. pObj->Ctx.write_reg = WriteRegWrap;
  101. pObj->Ctx.handle = pObj;
  102. }
  103. return ret;
  104. }
  105. /**
  106. * @brief Initialize the STHS34PF80 sensor
  107. * @param pObj the device pObj
  108. * @retval 0 in case of success, an error code otherwise
  109. */
  110. int32_t STHS34PF80_Init(STHS34PF80_Object_t *pObj)
  111. {
  112. if (pObj->is_initialized == 0U)
  113. {
  114. if (STHS34PF80_Initialize(pObj) != STHS34PF80_OK)
  115. {
  116. return STHS34PF80_ERROR;
  117. }
  118. }
  119. pObj->is_initialized = 1U;
  120. return STHS34PF80_OK;
  121. }
  122. /**
  123. * @brief Deinitialize the STHS34PF80 sensor
  124. * @param pObj the device pObj
  125. * @retval 0 in case of success, an error code otherwise
  126. */
  127. int32_t STHS34PF80_DeInit(STHS34PF80_Object_t *pObj)
  128. {
  129. if (sths34pf80_ctrl1_odr_set(&(pObj->Ctx), 0) != STHS34PF80_OK)
  130. {
  131. return STHS34PF80_ERROR;
  132. }
  133. pObj->is_initialized = 0;
  134. return STHS34PF80_OK;
  135. }
  136. /**
  137. * @brief Get WHO_AM_I value
  138. * @param pObj the device pObj
  139. * @param Id the WHO_AM_I value
  140. * @retval 0 in case of success, an error code otherwise
  141. */
  142. int32_t STHS34PF80_ReadID(STHS34PF80_Object_t *pObj, uint8_t *Id)
  143. {
  144. if (sths34pf80_who_am_i_get(&(pObj->Ctx), Id) != STHS34PF80_OK)
  145. {
  146. return STHS34PF80_ERROR;
  147. }
  148. return STHS34PF80_OK;
  149. }
  150. /**
  151. * @brief Initialize the STHS34PF80 sensor
  152. * @param pObj the device pObj
  153. * @retval 0 in case of success, an error code otherwise
  154. */
  155. static int32_t STHS34PF80_Initialize(STHS34PF80_Object_t *pObj)
  156. {
  157. if (sths34pf80_lpf_motion_set(&(pObj->Ctx), pObj->Config.LPF_Motion) != STHS34PF80_OK)
  158. {
  159. return STHS34PF80_ERROR;
  160. }
  161. if (sths34pf80_lpf_presence_set(&(pObj->Ctx), pObj->Config.LPF_Presence) != STHS34PF80_OK)
  162. {
  163. return STHS34PF80_ERROR;
  164. }
  165. if (sths34pf80_lpf_temperature_set(&(pObj->Ctx), pObj->Config.LPF_Temperature) != STHS34PF80_OK)
  166. {
  167. return STHS34PF80_ERROR;
  168. }
  169. if (sths34pf80_avg_trim_avg_tmos_set(&(pObj->Ctx), pObj->Config.AVG_TMOS) != STHS34PF80_OK)
  170. {
  171. return STHS34PF80_ERROR;
  172. }
  173. if (sths34pf80_ctrl1_odr_set(&(pObj->Ctx), pObj->Config.ODR) != STHS34PF80_OK)
  174. {
  175. return STHS34PF80_ERROR;
  176. }
  177. if (sths34pf80_threshold_set(&(pObj->Ctx),STHS34PF80_PRESENCE_THS_L,pObj->Config.THS_Presence & 0xFF) != STHS34PF80_OK)
  178. {
  179. return STHS34PF80_ERROR;
  180. }
  181. if (sths34pf80_threshold_set(&(pObj->Ctx),STHS34PF80_PRESENCE_THS_H,(pObj->Config.THS_Presence & 0xFF00)>>8) != STHS34PF80_OK)
  182. {
  183. return STHS34PF80_ERROR;
  184. }
  185. if (sths34pf80_threshold_set(&(pObj->Ctx),STHS34PF80_MOTION_THS_L,pObj->Config.THS_Motion & 0xFF) != STHS34PF80_OK)
  186. {
  187. return STHS34PF80_ERROR;
  188. }
  189. if (sths34pf80_threshold_set(&(pObj->Ctx),STHS34PF80_MOTION_THS_H,(pObj->Config.THS_Motion & 0xFF00)>>8) != STHS34PF80_OK)
  190. {
  191. return STHS34PF80_ERROR;
  192. }
  193. if (sths34pf80_threshold_set(&(pObj->Ctx),STHS34PF80_TAMBSHOCK_THS_L,pObj->Config.THS_Temp_Shock & 0xFF) != STHS34PF80_OK)
  194. {
  195. return STHS34PF80_ERROR;
  196. }
  197. if (sths34pf80_threshold_set(&(pObj->Ctx),STHS34PF80_TAMBSHOCK_THS_H,(pObj->Config.THS_Temp_Shock & 0xFF00)>>8) != STHS34PF80_OK)
  198. {
  199. return STHS34PF80_ERROR;
  200. }
  201. return STHS34PF80_OK;
  202. }
  203. /**
  204. * @brief Get the STHS34PF80 Presence value
  205. * @param pObj the device pObj
  206. * @param Value pointer where the Presence value is written
  207. * @retval 0 in case of success, an error code otherwise
  208. */
  209. int32_t STHS34PF80_ReadPresence(STHS34PF80_Object_t *pObj, uint16_t *value)
  210. {
  211. uint32_t data_raw_presence = 0;
  212. if (sths34pf80_tpresence_get(&(pObj->Ctx), &data_raw_presence) != STHS34PF80_OK)
  213. {
  214. return STHS34PF80_ERROR;
  215. }
  216. *value = data_raw_presence;
  217. return STHS34PF80_OK;
  218. }
  219. /**
  220. * @brief Get the STHS34PF80 Presence flag
  221. * @param pObj the device pObj
  222. * @param Value pointer where the Presence value is written
  223. * @retval 0 in case of success, an error code otherwise
  224. */
  225. int32_t STHS34PF80_ReadPresenceFlag(STHS34PF80_Object_t *pObj, uint16_t *value)
  226. {
  227. uint32_t data_raw_flag = 0;
  228. if (sths34pf80_pres_flag_get(&(pObj->Ctx), &data_raw_flag) != STHS34PF80_OK)
  229. {
  230. return STHS34PF80_ERROR;
  231. }
  232. *value = data_raw_flag;
  233. return STHS34PF80_OK;
  234. }
  235. /**
  236. * @brief Get the STHS34PF80 temp value
  237. * @param pObj the device pObj
  238. * @param Value pointer where the temp value is written
  239. * @retval 0 in case of success, an error code otherwise
  240. */
  241. int32_t STHS34PF80_ReadTemperature(STHS34PF80_Object_t *pObj, uint16_t *value)
  242. {
  243. uint32_t data_raw_temp = 0;
  244. if (sths34pf80_tambient_get(&(pObj->Ctx), &data_raw_temp) != STHS34PF80_OK)
  245. {
  246. return STHS34PF80_ERROR;
  247. }
  248. *value = data_raw_temp;
  249. return STHS34PF80_OK;
  250. }
  251. /**
  252. * @brief Get the STHS34PF80 Temp_Shock flag
  253. * @param pObj the device pObj
  254. * @param Value pointer where the temp value is written
  255. * @retval 0 in case of success, an error code otherwise
  256. */
  257. int32_t STHS34PF80_ReadTempShockFlag(STHS34PF80_Object_t *pObj, uint16_t *value)
  258. {
  259. uint32_t data_raw_flag = 0;
  260. if (sths34pf80_tamb_shock_flag_get(&(pObj->Ctx), &data_raw_flag) != STHS34PF80_OK)
  261. {
  262. return STHS34PF80_ERROR;
  263. }
  264. *value = data_raw_flag;
  265. return STHS34PF80_OK;
  266. }
  267. /**
  268. * @brief Get the STHS34PF80 Motion value
  269. * @param pObj the device pObj
  270. * @param Value pointer where the pressure value is written
  271. * @retval 0 in case of success, an error code otherwise
  272. */
  273. int32_t STHS34PF80_ReadMotion(STHS34PF80_Object_t *pObj, uint16_t *value)
  274. {
  275. uint32_t data_raw_motion = 0;
  276. if (sths34pf80_tmotion_get(&(pObj->Ctx), &data_raw_motion) != STHS34PF80_OK)
  277. {
  278. return STHS34PF80_ERROR;
  279. }
  280. *value = data_raw_motion;
  281. return STHS34PF80_OK;
  282. }
  283. /**
  284. * @brief Get the STHS34PF80 Motion flag
  285. * @param pObj the device pObj
  286. * @param Value pointer where the temp value is written
  287. * @retval 0 in case of success, an error code otherwise
  288. */
  289. int32_t STHS34PF80_ReadMotionFlag(STHS34PF80_Object_t *pObj, uint16_t *value)
  290. {
  291. uint32_t data_raw_flag = 0;
  292. if (sths34pf80_mot_flag_get(&(pObj->Ctx), &data_raw_flag) != STHS34PF80_OK)
  293. {
  294. return STHS34PF80_ERROR;
  295. }
  296. *value = data_raw_flag;
  297. return STHS34PF80_OK;
  298. }
  299. /**
  300. * @brief STHS34PF80_ControlINT
  301. * @param pObj the device pObj
  302. * @param msk_id is INT mask select(presence-motion-tamb_shock--->000),state is control value
  303. * @retval 0 in case of success, an error code otherwise
  304. */
  305. int32_t STHS34PF80_ControlINT(STHS34PF80_Object_t *pObj, uint8_t msk_id, uint8_t state)
  306. {
  307. sths34pf80_ctrl3_ien_set(&(pObj->Ctx), 0x02);
  308. switch(msk_id)
  309. {
  310. case 0:
  311. sths34pf80_ctrl3_int_msk0_set(&(pObj->Ctx), state);
  312. break;
  313. case 1:
  314. sths34pf80_ctrl3_int_msk1_set(&(pObj->Ctx), state);
  315. break;
  316. case 2:
  317. sths34pf80_ctrl3_int_msk2_set(&(pObj->Ctx), state);
  318. break;
  319. default:
  320. break;
  321. }
  322. return STHS34PF80_OK;
  323. }