hts221.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. * Copyright (c) 2017 - 2018, 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_H
  41. #define HTS221_H
  42. #include "nrf_twi_sensor.h"
  43. #include "hts221_internal.h"
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @brief Possible sensor addresses.
  49. */
  50. #define HTS221_BASE_ADDRESS 0x5FU
  51. // Who am I default value.
  52. #define HTS221_WHO_AM_I 0xBC
  53. // Minimal TWI Manager queue size needed for sensor.
  54. #define HTS221_MIN_QUEUE_SIZE 4
  55. /**
  56. * @brief Sensor driver usage.
  57. *
  58. * Sensor instance has to be defined first in global context using @ref HTS221_INSTANCE_DEF.
  59. * After that it has to be initialized using @ref hts221_init.
  60. * At this point sensor instance is ready and all other functions can be used.
  61. *
  62. * Configuration functions schedule TWI operation using @ref nrf_twi_sensor module.
  63. * After calling function, setting will be automatically send to sensor when TWI bus is free.
  64. *
  65. * There are designated functions to read status sensor registers e.g. @ref hts221_status_read
  66. * As parameters they receive function to be called after register is read, and pointer where
  67. * register value should be stored. From that value specific parameters can be extracted
  68. * using @ref NRF_TWI_SENSOR_REG_VAL_GET macro.
  69. * Example:
  70. * uint8_t h_da = NRF_TWI_SENSOR_REG_VAL_GET(status, HTS221_H_DA_MASK, HTS221_H_DA_POS);
  71. *
  72. * Other functions are self-explanatory or have description on their usage.
  73. */
  74. /**
  75. * @brief Temperature average setting.
  76. */
  77. typedef enum
  78. {
  79. HTS221_TEMP_SAMPLES_2,
  80. HTS221_TEMP_SAMPLES_4,
  81. HTS221_TEMP_SAMPLES_8,
  82. HTS221_TEMP_SAMPLES_16,
  83. HTS221_TEMP_SAMPLES_32,
  84. HTS221_TEMP_SAMPLES_64,
  85. HTS221_TEMP_SAMPLES_128,
  86. HTS221_TEMP_SAMPLES_256
  87. } hts221_temp_avg_samples_t;
  88. /**
  89. * @brief Humidity average setting.
  90. */
  91. typedef enum
  92. {
  93. HTS221_HUMIDITY_SAMPLES_4,
  94. HTS221_HUMIDITY_SAMPLES_8,
  95. HTS221_HUMIDITY_SAMPLES_16,
  96. HTS221_HUMIDITY_SAMPLES_32,
  97. HTS221_HUMIDITY_SAMPLES_64,
  98. HTS221_HUMIDITY_SAMPLES_128,
  99. HTS221_HUMIDITY_SAMPLES_256,
  100. HTS221_HUMIDITY_SAMPLES_512
  101. } hts221_hum_avg_samples_t;
  102. /**
  103. * @brief Output data rate settings.
  104. */
  105. typedef enum
  106. {
  107. HTS221_ODR_ONESHOT,
  108. HTS221_ODR_1HZ,
  109. HTS221_ODR_7HZ,
  110. HTS221_ODR_12_5HZ,
  111. } hts221_odr_t;
  112. /**
  113. * @brief Data callback prototype.
  114. *
  115. * @param[in] result Return error code from TWI manager and underlying drivers.
  116. * @param[in] p_data Pointer to sensor data.
  117. */
  118. typedef void (* hts221_data_callback_t)(ret_code_t result, int16_t * p_data);
  119. /**
  120. * @brief Macro creating hts221 sensor instance.
  121. *
  122. * @param[in] _hts221_inst_name Sensor instance name.
  123. * @param[in] _p_twi_sensor Pointer to common TWI sensor instance. @ref NRF_TWI_SENSOR_DEF
  124. * @param[in] _sensor_address Sensor base address.
  125. */
  126. #define HTS221_INSTANCE_DEF(_hts221_inst_name, _p_twi_sensor, _sensor_address) \
  127. HTS221_INTERNAL_INSTANCE_DEF(_hts221_inst_name, _p_twi_sensor, _sensor_address)
  128. /**
  129. * @brief Function initializing hts221 sensor
  130. *
  131. * Writes configuration from sensor instance into sensor.
  132. *
  133. * @param[in] p_instance Pointer to sensor instance created by macro
  134. *
  135. * @note TWI manager queue size has to be at least
  136. * HTS221_MIN_QUEUE_SIZE element long.
  137. *
  138. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  139. */
  140. ret_code_t hts221_init(hts221_instance_t * p_instance);
  141. /**
  142. * @brief Function for setting average configuration.
  143. *
  144. * @param[in] p_instance Pointer to sensor instance.
  145. * @param[in] temp_avg Number of temperature average samples.
  146. * @param[in] hum_avg Number of humidity average samples.
  147. *
  148. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  149. */
  150. ret_code_t hts221_avg_cfg(hts221_instance_t * p_instance,
  151. hts221_temp_avg_samples_t temp_avg,
  152. hts221_hum_avg_samples_t hum_avg);
  153. /**
  154. * @brief Function for setting power down mode
  155. *
  156. * @param[in] p_instance Pointer to sensor instance.
  157. * @param[in] enable True if device is powered, false if power down.
  158. *
  159. * @note Changes made by this function don't take effect before @ref hts221_cfg_commit
  160. */
  161. ret_code_t hts221_pd_enable(hts221_instance_t * p_instance, bool enable);
  162. /**
  163. * @brief Function for rebooting sensor.
  164. *
  165. * @param[in] p_instance Pointer to sensor instance.
  166. *
  167. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  168. */
  169. ret_code_t hts221_boot(hts221_instance_t * p_instance);
  170. /**
  171. * @brief Function for setting heater.
  172. *
  173. * @param[in] p_instance Pointer to sensor instance.
  174. * @param[in] enable True if heater is on.
  175. *
  176. * @note Changes made by this function don't take effect before @ref hts221_cfg_commit
  177. */
  178. ret_code_t hts221_heater_enable(hts221_instance_t * p_instance, bool enable);
  179. /**
  180. * @brief Function for setting one shot mode
  181. *
  182. * @param[in] p_instance Pointer to sensor instance.
  183. * @param[in] enable True if one shot mode is on.
  184. *
  185. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  186. */
  187. ret_code_t hts221_oneshot(hts221_instance_t * p_instance);
  188. /**
  189. * @brief Function for setting sensor_data ready output signal.
  190. *
  191. * @param[in] p_instance Pointer to sensor instance.
  192. * @param[in] active_low True if active low, false if active high.
  193. * @param[in] open_drain True if open drain, false if push-pull.
  194. * @param[in] drdy_enable True if pin is enabled.
  195. *
  196. * @note Changes made by this function don't take effect before @ref hts221_cfg_commit
  197. */
  198. ret_code_t hts221_drdy_pin_cfg(hts221_instance_t * p_instance,
  199. bool active_low,
  200. bool operation,
  201. bool drdy_enable);
  202. /**
  203. * @brief Function for setting output data rate.
  204. *
  205. * @param[in] p_instance Pointer to sensor instance.
  206. * @param[in] odr Desired output data rate.
  207. *
  208. * @note Changes made by this function don't take effect before @ref hts221_cfg_commit
  209. */
  210. ret_code_t hts221_data_rate_cfg(hts221_instance_t * p_instance, hts221_odr_t odr);
  211. /**
  212. * @brief Function for reading sensors temperature.
  213. *
  214. * @param[in] p_instance Pointer to sensor instance.
  215. * @param[in] user_callback Function to be called when sensor data is gathered.
  216. * @param[out] p_temp Pointer for raw temperature value, single int16_t.
  217. *
  218. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  219. */
  220. ret_code_t hts221_temp_read(hts221_instance_t * p_instance,
  221. hts221_data_callback_t user_callback,
  222. int16_t * p_temp);
  223. /**
  224. * @brief Function for calculating temperature based on sensors calibration data.
  225. *
  226. * @param[in] p_instance Pointer to sensor instance.
  227. * @param[in] raw_temp Raw temperature in.
  228. *
  229. * @return Temperature * 8
  230. */
  231. int16_t hts221_temp_process(hts221_instance_t * p_instance, int16_t raw_temp);
  232. /**
  233. * @brief Function for reading sensors humidity.
  234. *
  235. * @param[in] p_instance Pointer to sensor instance.
  236. * @param[in] user_callback Function to be called when data is gathered.
  237. * @param[out] p_hum Pointer for raw humidity value, single int16_t.
  238. *
  239. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  240. */
  241. ret_code_t hts221_hum_read(hts221_instance_t * p_instance,
  242. hts221_data_callback_t user_callback,
  243. int16_t * p_hum);
  244. /**
  245. * @brief Function for calculating humidity based on sensors calibration data.
  246. *
  247. * @param[in] p_instance Pointer to sensor instance.
  248. * @param[in] raw_hum Raw humidity in.
  249. *
  250. * @return Humidity * 2
  251. */
  252. int16_t hts221_hum_process(hts221_instance_t * p_instance, int16_t raw_hum);
  253. /**
  254. * @brief Function for reading WHO_AM_I register.
  255. *
  256. * @param[in] p_instance Pointer to sensor instance.
  257. * @param[in] user_cb Function to be called after register is read.
  258. * @param[out] reg_val Register value, single uint8_t.
  259. *
  260. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  261. */
  262. __STATIC_INLINE ret_code_t hts221_who_am_i_read(hts221_instance_t * p_instance,
  263. nrf_twi_sensor_reg_cb_t user_cb,
  264. uint8_t * reg_val);
  265. /**
  266. * @brief Function for reading status register.
  267. *
  268. * @param[in] p_instance Pointer to sensor instance.
  269. * @param[in] user_cb Function to be called after register is read.
  270. * @param[out] reg_val Register value, single uint8_t.
  271. *
  272. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  273. */
  274. __STATIC_INLINE ret_code_t hts221_status_read(hts221_instance_t * p_instance,
  275. nrf_twi_sensor_reg_cb_t user_cb,
  276. uint8_t * reg_val);
  277. #ifdef __cplusplus
  278. }
  279. #endif
  280. #endif // HTS221_H