hts221.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #include "hts221.h"
  41. #include <string.h>
  42. #define HTS221_WRITE(p_instance, msg) \
  43. nrf_twi_sensor_write(p_instance->p_sensor_data, \
  44. p_instance->sensor_addr, \
  45. msg, \
  46. ARRAY_SIZE(msg), \
  47. true)
  48. static void hts221_init_cb(ret_code_t result, void * p_register_data)
  49. {
  50. hts221_calib_t * calib_info = (hts221_calib_t *) p_register_data;
  51. uint8_t calib_raw[HTS221_REG_CALIBRATION_NUM];
  52. memcpy(calib_raw, calib_info, HTS221_REG_CALIBRATION_NUM);
  53. calib_info->H0_rH_x2 = calib_raw[0];
  54. calib_info->H1_rH_x2 = calib_raw[1];
  55. calib_info->T0_degC_x8 = (uint16_t)calib_raw[2]
  56. + ((uint16_t)(calib_raw[5] & 0x03) << 8);
  57. calib_info->T1_degC_x8 = (uint16_t)calib_raw[3]
  58. + ((uint16_t)((calib_raw[5] >> 2) & 0x03) << 8);
  59. calib_info->H0_T0_OUT = (int16_t)calib_raw[6] + ((int16_t)calib_raw[7] << 8);
  60. calib_info->H1_T0_OUT = (int16_t)calib_raw[10] + ((int16_t)calib_raw[11] << 8);
  61. calib_info->T0_OUT = (int16_t)calib_raw[12] + ((int16_t)calib_raw[13] << 8);
  62. calib_info->T1_OUT = (int16_t)calib_raw[14] + ((int16_t)calib_raw[15] << 8);
  63. }
  64. ret_code_t hts221_init(hts221_instance_t * p_instance)
  65. {
  66. ASSERT(p_instance != NULL);
  67. if (p_instance->p_sensor_data->p_twi_mngr->p_queue->size < HTS221_MIN_QUEUE_SIZE)
  68. {
  69. return NRF_ERROR_INVALID_LENGTH;
  70. }
  71. p_instance->ctrl_reg1 = 0;
  72. uint8_t send_msg[] = {
  73. HTS221_REG_AV_CONF,
  74. HTS221_DEF_AV_CONF,
  75. 0,
  76. 0,
  77. 0
  78. };
  79. ret_code_t err = HTS221_WRITE(p_instance, send_msg);
  80. if (err != NRF_SUCCESS)
  81. {
  82. return err;
  83. }
  84. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  85. p_instance->sensor_addr,
  86. HTS221_REG_CALIBRATION | HTS221_INCR_REG_MASK,
  87. hts221_init_cb,
  88. (uint8_t *) &p_instance->calib_info,
  89. HTS221_REG_CALIBRATION_NUM);
  90. }
  91. ret_code_t hts221_avg_cfg(hts221_instance_t * p_instance,
  92. hts221_temp_avg_samples_t temp_avg,
  93. hts221_hum_avg_samples_t hum_avg)
  94. {
  95. ASSERT(p_instance != NULL);
  96. uint8_t reg_val = 0;
  97. NRF_TWI_SENSOR_REG_SET(reg_val, HTS221_AVGT_MASK, HTS221_AVGT_POS, temp_avg);
  98. NRF_TWI_SENSOR_REG_SET(reg_val, HTS221_AVGH_MASK, HTS221_AVGH_POS, hum_avg);
  99. uint8_t send_msg[] = {
  100. HTS221_REG_AV_CONF,
  101. reg_val
  102. };
  103. return HTS221_WRITE(p_instance, send_msg);
  104. }
  105. ret_code_t hts221_data_rate_cfg(hts221_instance_t * p_instance, hts221_odr_t odr)
  106. {
  107. ASSERT(p_instance != NULL);
  108. NRF_TWI_SENSOR_REG_SET(p_instance->ctrl_reg1, HTS221_ODR_MASK, HTS221_ODR_POS, odr);
  109. uint8_t send_msg[] = {
  110. HTS221_REG_CTRL_REG1,
  111. p_instance->ctrl_reg1
  112. };
  113. return HTS221_WRITE(p_instance, send_msg);
  114. }
  115. ret_code_t hts221_pd_enable(hts221_instance_t * p_instance, bool enable)
  116. {
  117. ASSERT(p_instance != NULL);
  118. NRF_TWI_SENSOR_REG_SET(p_instance->ctrl_reg1, HTS221_PD_MASK, HTS221_PD_POS, enable);
  119. uint8_t send_msg[] = {
  120. HTS221_REG_CTRL_REG1,
  121. p_instance->ctrl_reg1
  122. };
  123. return HTS221_WRITE(p_instance, send_msg);
  124. }
  125. ret_code_t hts221_boot(hts221_instance_t * p_instance)
  126. {
  127. ASSERT(p_instance != NULL);
  128. uint8_t reg_val = p_instance->ctrl_reg2;
  129. NRF_TWI_SENSOR_REG_SET(reg_val, HTS221_BOOT_MASK, HTS221_BOOT_POS, 1);
  130. uint8_t send_msg[] = {
  131. HTS221_REG_CTRL_REG2,
  132. reg_val
  133. };
  134. return HTS221_WRITE(p_instance, send_msg);
  135. }
  136. ret_code_t hts221_heater_enable(hts221_instance_t * p_instance, bool enable)
  137. {
  138. ASSERT(p_instance != NULL);
  139. NRF_TWI_SENSOR_REG_SET(p_instance->ctrl_reg2, HTS221_HEATER_MASK, HTS221_HEATER_POS, enable);
  140. uint8_t send_msg[] = {
  141. HTS221_REG_CTRL_REG2,
  142. p_instance->ctrl_reg2
  143. };
  144. return HTS221_WRITE(p_instance, send_msg);
  145. }
  146. ret_code_t hts221_oneshot(hts221_instance_t * p_instance)
  147. {
  148. ASSERT(p_instance != NULL);
  149. uint8_t reg_val = p_instance->ctrl_reg2;
  150. NRF_TWI_SENSOR_REG_SET(reg_val, HTS221_ONE_SHOT_MASK, HTS221_ONE_SHOT_POS, true);
  151. uint8_t send_msg[] = {
  152. HTS221_REG_CTRL_REG2,
  153. reg_val
  154. };
  155. return HTS221_WRITE(p_instance, send_msg);
  156. }
  157. ret_code_t hts221_drdy_pin_cfg(hts221_instance_t * p_instance,
  158. bool active_low,
  159. bool operation,
  160. bool drdy_enable)
  161. {
  162. ASSERT(p_instance != NULL);
  163. uint8_t reg_val = 0;
  164. NRF_TWI_SENSOR_REG_SET(reg_val, HTS221_DRDY_H_L_MASK, HTS221_DRDY_H_L_POS, active_low);
  165. NRF_TWI_SENSOR_REG_SET(reg_val, HTS221_PP_OD_MASK, HTS221_PP_OD_POS, operation);
  166. NRF_TWI_SENSOR_REG_SET(reg_val, HTS221_DRDY_EN_MASK, HTS221_DRDY_EN_POS, drdy_enable);
  167. uint8_t send_msg[] = {
  168. HTS221_REG_CTRL_REG3,
  169. reg_val
  170. };
  171. return HTS221_WRITE(p_instance, send_msg);
  172. }
  173. ret_code_t hts221_temp_read(hts221_instance_t * p_instance,
  174. hts221_data_callback_t user_callback,
  175. int16_t * p_temp)
  176. {
  177. ASSERT(p_instance != NULL);
  178. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  179. p_instance->sensor_addr,
  180. HTS221_REG_TEMP_OUT_L | HTS221_INCR_REG_MASK,
  181. (nrf_twi_sensor_reg_cb_t) user_callback,
  182. (uint8_t *) p_temp,
  183. 2);
  184. }
  185. int16_t hts221_temp_process(hts221_instance_t * p_instance, int16_t raw_temp)
  186. {
  187. ASSERT(p_instance != NULL);
  188. int32_t y;
  189. int32_t x0 = p_instance->calib_info.T0_OUT;
  190. int32_t x1 = p_instance->calib_info.T1_OUT;
  191. int32_t y0 = p_instance->calib_info.T0_degC_x8;
  192. int32_t y1 = p_instance->calib_info.T1_degC_x8;
  193. y = ((y0 * (x1 - raw_temp)) + (y1 * (raw_temp - x0))) / (x1 - x0);
  194. return y;
  195. }
  196. ret_code_t hts221_hum_read(hts221_instance_t * p_instance,
  197. hts221_data_callback_t user_callback,
  198. int16_t * p_hum)
  199. {
  200. ASSERT(p_instance != NULL);
  201. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  202. p_instance->sensor_addr,
  203. HTS221_REG_HUM_OUT_L | HTS221_INCR_REG_MASK,
  204. (nrf_twi_sensor_reg_cb_t) user_callback,
  205. (uint8_t *) p_hum,
  206. 2);
  207. }
  208. int16_t hts221_hum_process(hts221_instance_t * p_instance, int16_t raw_hum)
  209. {
  210. ASSERT(p_instance != NULL);
  211. int32_t y;
  212. int32_t x0 = p_instance->calib_info.H0_T0_OUT;
  213. int32_t x1 = p_instance->calib_info.H1_T0_OUT;
  214. int32_t y0 = p_instance->calib_info.H0_rH_x2;
  215. int32_t y1 = p_instance->calib_info.H1_rH_x2;
  216. y = ((y0 * (x1 - raw_hum)) + (y1 * (raw_hum - x0))) / (x1 - x0);
  217. return y;
  218. }