hts221_internal.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 HTS221_INTERNAL_H
  41. #define HTS221_INTERNAL_H
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /**
  46. * @brief HTS221 sensor registers.
  47. */
  48. #define HTS221_REG_WHO_AM_I 0x0F
  49. #define HTS221_REG_AV_CONF 0x10
  50. #define HTS221_REG_CTRL_REG1 0x20
  51. #define HTS221_REG_CTRL_REG2 0x21
  52. #define HTS221_REG_CTRL_REG3 0x22
  53. #define HTS221_REG_STATUS_REG 0x27
  54. #define HTS221_REG_HUM_OUT_L 0x28
  55. #define HTS221_REG_HUM_OUT_H 0x29
  56. #define HTS221_REG_TEMP_OUT_L 0x2A
  57. #define HTS221_REG_TEMP_OUT_H 0x2B
  58. // Calibration registers
  59. #define HTS221_REG_CALIBRATION 0x30
  60. #define HTS221_REG_CALIBRATION_NUM 16
  61. #define HTS221_REG_CTRL_NUM 3
  62. // For auto incrementing address, msb in register address must be set to 1.
  63. #define HTS221_INCR_REG_MASK 0x80
  64. /**
  65. * @brief AV_CONF register bitmasks.
  66. */
  67. #define HTS221_DEF_AV_CONF 0x1B
  68. // Register validity bitmask.
  69. #define HTS221_AV_CONF_VALID_MASK 0xC0
  70. // Bitmasks for AVGT.
  71. #define HTS221_AVGT_POS 3
  72. #define HTS221_AVGT_MASK (7 << HTS221_AVGT_POS)
  73. // Bitmasks for AVGH.
  74. #define HTS221_AVGH_POS 0
  75. #define HTS221_AVGH_MASK (7 << HTS221_AVGH_POS)
  76. /**
  77. * @brief Control register 1 bitmasks.
  78. */
  79. // Register validity bitmask.
  80. #define HTS221_CTRL1_VALID_MASK 0x78
  81. // Bitmasks for PD.
  82. #define HTS221_PD_POS 7
  83. #define HTS221_PD_MASK (1 << HTS221_PD_POS)
  84. // Bitmasks for BDU.
  85. #define HTS221_BDU_POS 2
  86. #define HTS221_BDU_MASK (1 << HTS221_BDU_POS)
  87. // Bitmasks for ODR.
  88. #define HTS221_ODR_POS 0
  89. #define HTS221_ODR_MASK (3 << HTS221_ODR_POS)
  90. /**
  91. * @brief Control register 2 bitmasks.
  92. */
  93. // Register validity bitmask.
  94. #define HTS221_CTRL2_VALID_MASK 0x7C
  95. // Bitmasks for BOOT.
  96. #define HTS221_BOOT_POS 7
  97. #define HTS221_BOOT_MASK (1 << HTS221_BOOT_POS)
  98. // Bitmasks for Heater.
  99. #define HTS221_HEATER_POS 1
  100. #define HTS221_HEATER_MASK (1 << HTS221_HEATER_POS)
  101. // Bitmasks for ONE_SHOT.
  102. #define HTS221_ONE_SHOT_POS 0
  103. #define HTS221_ONE_SHOT_MASK (1 << HTS221_ONE_SHOT_POS)
  104. /**
  105. * @brief Control register 3 bitmasks.
  106. */
  107. // Register validity bitmask.
  108. #define HTS221_CTRL3_VALID_MASK 0x3B
  109. // Bitmasks for DRDY_H_L.
  110. #define HTS221_DRDY_H_L_POS 7
  111. #define HTS221_DRDY_H_L_MASK (1 << HTS221_DRDY_H_L_POS)
  112. // Bitmasks for PP_OD
  113. #define HTS221_PP_OD_POS 6
  114. #define HTS221_PP_OD_MASK (1 << HTS221_PP_OD_POS)
  115. // Bitmasks for DRDY_EN.
  116. #define HTS221_DRDY_EN_POS 2
  117. #define HTS221_DRDY_EN_MASK (1 << HTS221_DRDY_EN_POS)
  118. /**
  119. * @brief Status register bitmasks.
  120. */
  121. // Bitmasks for H_DA.
  122. #define HTS221_H_DA_POS 1
  123. #define HTS221_H_DA_MASK (1 << HTS221_H_DA_POS)
  124. // Bitmasks for T_DA
  125. #define HTS221_T_DA_POS 0
  126. #define HTS221_T_DA_MASK (1 << HTS221_T_DA_POS)
  127. /**
  128. * @brief Structure holding calibration information.
  129. */
  130. typedef struct
  131. {
  132. uint8_t H0_rH_x2;
  133. uint8_t H1_rH_x2;
  134. uint16_t T0_degC_x8;
  135. uint16_t T1_degC_x8;
  136. int16_t H0_T0_OUT;
  137. int16_t H1_T0_OUT;
  138. int16_t T0_OUT;
  139. int16_t T1_OUT;
  140. uint16_t padding; //<- Additional memory needed to store all calibration registers.
  141. } hts221_calib_t;
  142. /**
  143. * @brief Structure holding sensor instance
  144. */
  145. typedef struct
  146. {
  147. nrf_twi_sensor_t * const p_sensor_data;
  148. uint8_t const sensor_addr;
  149. hts221_calib_t calib_info;
  150. uint8_t ctrl_reg1;
  151. uint8_t ctrl_reg2;
  152. } hts221_instance_t;
  153. /**
  154. * @brief Macro creating hts221 sensor instance.
  155. */
  156. #define HTS221_INTERNAL_INSTANCE_DEF(_hts221_inst_name, _p_twi_sensor, _sensor_address) \
  157. static hts221_instance_t _hts221_inst_name = \
  158. { \
  159. .p_sensor_data = _p_twi_sensor, \
  160. .sensor_addr = _sensor_address, \
  161. }
  162. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  163. __STATIC_INLINE ret_code_t hts221_who_am_i_read(hts221_instance_t * p_instance,
  164. nrf_twi_sensor_reg_cb_t user_cb,
  165. uint8_t * reg_val)
  166. {
  167. ASSERT(p_instance != NULL);
  168. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  169. p_instance->sensor_addr,
  170. HTS221_REG_WHO_AM_I,
  171. user_cb,
  172. reg_val,
  173. 1);
  174. }
  175. __STATIC_INLINE ret_code_t hts221_status_read(hts221_instance_t * p_instance,
  176. nrf_twi_sensor_reg_cb_t user_cb,
  177. uint8_t * reg_val)
  178. {
  179. ASSERT(p_instance != NULL);
  180. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  181. p_instance->sensor_addr,
  182. HTS221_REG_STATUS_REG,
  183. user_cb,
  184. reg_val,
  185. 1);
  186. }
  187. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  188. #ifdef __cplusplus
  189. }
  190. #endif
  191. #endif // HTS221_INTERNAL_H