lps22hb_internal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /**
  2. * Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef LPS22HB_INTERNAL_H
  41. #define LPS22HB_INTERNAL_H
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. #define LPS22HB_BYTES_PER_SAMPLE 5
  46. /**
  47. * @brief LPS22HB sensor registers.
  48. */
  49. #define LPS22HB_REG_INTERRUPT_CONFIG 0x0B
  50. #define LPS22HB_REG_THS_P_L 0x0C
  51. #define LPS22HB_REG_THS_P_H 0x0D
  52. #define LPS22HB_REG_WHO_AM_I 0x0F
  53. #define LPS22HB_REG_CTRL1 0x10
  54. #define LPS22HB_REG_CTRL2 0x11
  55. #define LPS22HB_REG_CTRL3 0x12
  56. #define LPS22HB_REG_FIFO_CTRL 0x14
  57. #define LPS22HB_REG_REF_P_XL 0x15
  58. #define LPS22HB_REG_REF_P_L 0x16
  59. #define LPS22HB_REG_REF_P_H 0x17
  60. #define LPS22HB_REG_RPDS_L 0x18
  61. #define LPS22HB_REG_RPDS_H 0x19
  62. #define LPS22HB_REG_RES_CONF 0x1A
  63. #define LPS22HB_REG_INT_SOURCE 0x25
  64. #define LPS22HB_REG_FIFO_STATUS 0x26
  65. #define LPS22HB_REG_STATUS 0x27
  66. #define LPS22HB_REG_PRESS_OUT_XL 0x28
  67. #define LPS22HB_REG_PRESS_OUT_L 0x29
  68. #define LPS22HB_REG_PRESS_OUT_H 0x2A
  69. #define LPS22HB_REG_TEMP_OUT_L 0x2B
  70. #define LPS22HB_REG_TEMP_OUT_H 0x2C
  71. #define LPS22HB_REG_LPFP_RES 0x33
  72. /**
  73. * @brief Interrupt config register bitmasks.
  74. */
  75. // Bitmasks for AUTORIFP.
  76. #define LPS22HB_AUTORIFP_POS 7
  77. #define LPS22HB_AUTORIFP_MASK (1 << LPS22HB_AUTORIFP_POS)
  78. // Bitmasks for RESET_ARP.
  79. #define LPS22HB_RESET_ARP_POS 6
  80. #define LPS22HB_RESET_ARP_MASK (1 << LPS22HB_RESET_ARP_POS)
  81. // Bitmasks for AUTOZERO.
  82. #define LPS22HB_AUTOZERO_POS 5
  83. #define LPS22HB_AUTOZERO_MASK (1 << LPS22HB_AUTOZERO_POS)
  84. // Bitmasks for RESET_AZ.
  85. #define LPS22HB_RESET_AZ_POS 4
  86. #define LPS22HB_RESET_AZ_MASK (1 << LPS22HB_RESET_AZ_POS)
  87. // Bitmasks for DIFF_EN.
  88. #define LPS22HB_DIFF_EN_POS 3
  89. #define LPS22HB_DIFF_EN_MASK (1 << LPS22HB_DIFF_EN_POS)
  90. // Bitmasks for LIR.
  91. #define LPS22HB_LIR_POS 2
  92. #define LPS22HB_LIR_MASK (1 << LPS22HB_LIR_POS)
  93. // Bitmasks for PLE.
  94. #define LPS22HB_PLE_POS 1
  95. #define LPS22HB_PLE_MASK (1 << LPS22HB_PLE_POS)
  96. // Bitmasks for PHE.
  97. #define LPS22HB_PHE_POS 0
  98. #define LPS22HB_PHE_MASK (1 << LPS22HB_PHE_POS)
  99. /**
  100. * @brief Control register 1 bitmasks.
  101. */
  102. // Register validity bitmask.
  103. #define LPS22HB_CTRL1_VALID_MASK 0x80
  104. // Bitmasks for ODR.
  105. #define LPS22HB_ODR_POS 4
  106. #define LPS22HB_ODR_MASK (7 << LPS22HB_ODR_POS)
  107. // Bitmasks for EN_LPFP.
  108. #define LPS22HB_EN_LPFP_POS 3
  109. #define LPS22HB_EN_LPFP_MASK (1 << LPS22HB_EN_LPFP_POS)
  110. // Bitmasks for LPFP_CFG.
  111. #define LPS22HB_LPFP_CFG_POS 2
  112. #define LPS22HB_LPFP_CFG_MASK (1 << LPS22HB_LPFP_CFG_POS)
  113. // Bitmasks for BDU.
  114. #define LPS22HB_BDU_POS 1
  115. #define LPS22HB_BDU_MASK (1 << LPS22HB_BDU_POS)
  116. // Bitmasks for SIM.
  117. #define LPS22HB_SIM_POS 0
  118. #define LPS22HB_SIM_MASK (1 << LPS22HB_SIM_POS)
  119. /**
  120. * @brief Control register 2 bitmasks.
  121. */
  122. // Register validity bitmask.
  123. #define LPS22HB_CTRL2_VALID_MASK 0x02
  124. // Bitmasks for BOOT.
  125. #define LPS22HB_BOOT_POS 7
  126. #define LPS22HB_BOOT_MASK (1 << LPS22HB_BOOT_POS)
  127. // Bitmasks for FIFO_EN.
  128. #define LPS22HB_FIFO_EN_POS 6
  129. #define LPS22HB_FIFO_EN_MASK (1 << LPS22HB_FIFO_EN_POS)
  130. // Bitmasks for STOP_ON_FTH.
  131. #define LPS22HB_STOP_ON_FTH_POS 5
  132. #define LPS22HB_STOP_ON_FTH_MASK (1 << LPS22HB_STOP_ON_FTH_POS)
  133. // Bitmasks for IF_ADD_INC.
  134. #define LPS22HB_IF_ADD_INC_POS 4
  135. #define LPS22HB_IF_ADD_INC_MASK (1 << LPS22HB_IF_ADD_INC_POS)
  136. // Bitmasks for I2C_DIS.
  137. #define LPS22HB_I2C_DIS_POS 3
  138. #define LPS22HB_I2C_DIS_MASK (1 << LPS22HB_I2C_DIS_POS)
  139. // Bitmasks for SWRESET.
  140. #define LPS22HB_SWRESET_POS 2
  141. #define LPS22HB_SWRESET_MASK (1 << LPS22HB_SWRESET_POS)
  142. // Bitmasks for ONE_SHOT.
  143. #define LPS22HB_ONE_SHOT_POS 0
  144. #define LPS22HB_ONE_SHOT_MASK (1 << LPS22HB_ONE_SHOT_POS)
  145. /**
  146. * @brief Control register 3 bitmasks.
  147. */
  148. // Bitmasks for INT_H_L.
  149. #define LPS22HB_INT_H_L_POS 7
  150. #define LPS22HB_INT_H_L_MASK (1 << LPS22HB_INT_H_L_POS)
  151. // Bitmasks for PP_OD.
  152. #define LPS22HB_PP_OD_POS 6
  153. #define LPS22HB_PP_OD_MASK (1 << LPS22HB_PP_OD_POS)
  154. // Bitmasks for F_FSS5.
  155. #define LPS22HB_F_FSS5_POS 5
  156. #define LPS22HB_F_FSS5_MASK (1 << LPS22HB_F_FSS5_POS)
  157. // Bitmasks for F_FTH.
  158. #define LPS22HB_F_FTH_POS 4
  159. #define LPS22HB_F_FTH_MASK (1 << LPS22HB_F_FTH_POS)
  160. // Bitmasks for F_OVR.
  161. #define LPS22HB_F_OVR_POS 3
  162. #define LPS22HB_F_OVR_MASK (1 << LPS22HB_F_OVR_POS)
  163. // Bitmasks for DRDY.
  164. #define LPS22HB_DRDY_POS 2
  165. #define LPS22HB_DRDY_MASK (1 << LPS22HB_DRDY_POS)
  166. // Bitmasks for INT_S.
  167. #define LPS22HB_INT_S_POS 0
  168. #define LPS22HB_INT_S_MASK (3 << LPS22HB_INT_S_POS)
  169. /**
  170. * @brief Fifo control register bitmasks.
  171. */
  172. // Bitmasks for F_MODE.
  173. #define LPS22HB_F_MODE_POS 5
  174. #define LPS22HB_F_MODE_MASK (7 << LPS22HB_F_MODE_POS)
  175. // Bitmasks for WTM
  176. #define LPS22HB_WTM_POS 0
  177. #define LPS22HB_WTM_MASK (0x1F << LPS22HB_WTM_POS)
  178. /**
  179. * @brief Low power mode register bitmasks.
  180. */
  181. // Register validity bitmask.
  182. #define LPS22HB_RES_CONF_VALID_MASK 0xFE
  183. // Bitmasks for LC_EN
  184. #define LPS22HB_LC_EN_POS 0
  185. #define LPS22HB_LC_EN_MASK (1 << LPS22HB_LC_EN_POS)
  186. /**
  187. * @brief INT source register bitmasks.
  188. */
  189. // Bitmasks for IA
  190. #define LPS22HB_IA_POS 2
  191. #define LPS22HB_IA_MASK (1 << LPS22HB_IA_POS)
  192. // Bitmasks for PL
  193. #define LPS22HB_PL_POS 1
  194. #define LPS22HB_PL_MASK (1 << LPS22HB_PL_POS)
  195. // Bitmasks for PH
  196. #define LPS22HB_PH_POS 0
  197. #define LPS22HB_PH_MASK (1 << LPS22HB_PH_POS)
  198. /**
  199. * @brief FIFO status register bitmasks.
  200. */
  201. // Bitmasks for FTH_FIFO
  202. #define LPS22HB_FTH_FIFO_POS 7
  203. #define LPS22HB_FTH_FIFO_MASK (1 << LPS22HB_FTH_FIFO_POS)
  204. // Bitmasks for OVR
  205. #define LPS22HB_OVR_POS 6
  206. #define LPS22HB_OVR_MASK (1 << LPS22HB_OVR_POS)
  207. // Bitmasks for stored data level
  208. #define LPS22HB_FSS_POS 0
  209. #define LPS22HB_FSS_MASK (0x3F << LPS22HB_FSS_POS)
  210. /**
  211. * @brief Status register bitmasks.
  212. */
  213. // Bitmasks for T_OR.
  214. #define LPS22HB_T_OR_POS 5
  215. #define LPS22HB_T_OR_MASK (1 << LPS22HB_T_OR_POS)
  216. // Bitmasks for P_OR.
  217. #define LPS22HB_P_OR_POS 4
  218. #define LPS22HB_P_OR_MASK (1 << LPS22HB_P_OR_POS)
  219. // Bitmasks for T_DA.
  220. #define LPS22HB_T_DA_POS 1
  221. #define LPS22HB_T_DA_MASK (1 << LPS22HB_T_DA_POS)
  222. // Bitmasks for P_DA.
  223. #define LPS22HB_P_DA_POS 0
  224. #define LPS22HB_P_DA_MASK (1 << LPS22HB_P_DA_POS)
  225. /**
  226. * @brief Config registers defaults.
  227. */
  228. #define LPS22HB_CTRL_REG2_DEFAULT 0x10
  229. /**
  230. * @brief Raw pressure and temperature data.
  231. *
  232. * @note For internal use only.
  233. */
  234. typedef struct
  235. {
  236. uint8_t press_out_xl;
  237. uint8_t press_out_l;
  238. uint8_t press_out_h;
  239. uint8_t temp_out_l;
  240. uint8_t temp_out_h;
  241. } lps22hb_raw_data_t;
  242. /**
  243. * @brief Structure holding sensor instance
  244. *
  245. * @note For internal use only.
  246. */
  247. typedef struct
  248. {
  249. nrf_twi_sensor_t * const p_sensor_data;
  250. uint8_t const sensor_addr;
  251. uint8_t interrupt_cfg;
  252. uint8_t ctrl_reg[3];
  253. uint8_t fifo_ctrl;
  254. } lps22hb_instance_t;
  255. #define LPS22HB_INTERNAL_INSTANCE_DEF(_lps22hb_inst_name, _p_twi_sensor, _sensor_address) \
  256. static lps22hb_instance_t _lps22hb_inst_name = \
  257. { \
  258. .p_sensor_data = _p_twi_sensor, \
  259. .sensor_addr = _sensor_address, \
  260. }
  261. #define LPS22HB_INTERNAL_INT_CFG(_s, _diff_en, _lir, _ple, _phe) \
  262. NRF_TWI_SENSOR_REG_SET(_s.interrupt_cfg, LPS22HB_DIFF_EN_MASK, LPS22HB_DIFF_EN_POS, _diff_en); \
  263. NRF_TWI_SENSOR_REG_SET(_s.interrupt_cfg, LPS22HB_LIR_MASK, LPS22HB_LIR_POS, _lir); \
  264. NRF_TWI_SENSOR_REG_SET(_s.interrupt_cfg, LPS22HB_PLE_MASK, LPS22HB_PLE_POS, _ple); \
  265. NRF_TWI_SENSOR_REG_SET(_s.interrupt_cfg, LPS22HB_PHE_MASK, LPS22HB_PHE_POS, _phe);
  266. #define LPS22HB_INTERNAL_DATA_CFG(_s, _odr, _f_en, _f_cfg) \
  267. NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[0], LPS22HB_ODR_MASK, LPS22HB_ODR_POS, _odr); \
  268. NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[0], LPS22HB_EN_LPFP_MASK, LPS22HB_EN_LPFP_POS, _f_en); \
  269. NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[0], LPS22HB_LPFP_CFG_MASK, LPS22HB_LPFP_CFG_POS, _f_cfg);
  270. #define LPS22HB_INTERNAL_FIFO_CFG(_s, _f_mode, _f_en, _f_stop, _f_wtm) \
  271. NRF_TWI_SENSOR_REG_SET(_s.fifo_ctrl, LPS22HB_F_MODE_MASK, LPS22HB_F_MODE_POS, _f_mode); \
  272. NRF_TWI_SENSOR_REG_SET(_s.fifo_ctrl, LPS22HB_WTM_MASK, LPS22HB_WTM_POS, _f_wtm); \
  273. NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[1], LPS22HB_FIFO_EN_MASK, LPS22HB_FIFO_EN_POS, _f_en); \
  274. NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[1], \
  275. LPS22HB_STOP_ON_FTH_MASK, \
  276. LPS22HB_STOP_ON_FTH_POS, \
  277. _f_stop)
  278. #define LPS22HB_INTERNAL_DRDY_CFG(_s, _activ, _pp_od, _fss, _fth, _ovr, _drdy, _high, _low) \
  279. NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[2], LPS22HB_INT_H_L_MASK, LPS22HB_INT_H_L_POS, _activ); \
  280. NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[2], LPS22HB_PP_OD_MASK, LPS22HB_PP_OD_POS, _pp_od); \
  281. NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[2], LPS22HB_F_FSS5_MASK, LPS22HB_F_FSS5_POS, _fss); \
  282. NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[2], LPS22HB_F_FTH_MASK, LPS22HB_F_FTH_POS, _fth); \
  283. NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[2], LPS22HB_F_OVR_MASK, LPS22HB_F_OVR_POS, _ovr); \
  284. NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[2], \
  285. LPS22HB_INT_S_MASK, \
  286. LPS22HB_INT_S_POS, \
  287. (_low << 1) + _high);
  288. #ifdef __cplusplus
  289. }
  290. #endif
  291. #endif // LPS22HB_INTERNAL_H