lps22hb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * Copyright (c) 2017 - 2019, 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 "lps22hb.h"
  41. ret_code_t lps22hb_init(lps22hb_instance_t * p_instance)
  42. {
  43. ASSERT(p_instance != NULL);
  44. p_instance->interrupt_cfg = 0;
  45. p_instance->ctrl_reg[0] = 0;
  46. p_instance->ctrl_reg[1] = LPS22HB_CTRL_REG2_DEFAULT;
  47. p_instance->ctrl_reg[2] = 0;
  48. p_instance->fifo_ctrl = 0;
  49. ret_code_t err_code;
  50. if (p_instance->p_sensor_data->p_twi_mngr->p_queue->size < LPS22HB_MIN_QUEUE_SIZE)
  51. {
  52. return NRF_ERROR_INVALID_LENGTH;
  53. }
  54. err_code = lps22hb_cfg_commit(p_instance);
  55. return err_code;
  56. }
  57. ret_code_t lps22hb_autorifp_enable(lps22hb_instance_t * p_instance, bool enable)
  58. {
  59. ASSERT(p_instance != NULL);
  60. uint8_t reg = p_instance->interrupt_cfg;
  61. if (enable == true)
  62. {
  63. NRF_TWI_SENSOR_REG_SET(reg, LPS22HB_AUTORIFP_MASK, LPS22HB_AUTORIFP_POS, 1);
  64. }
  65. else
  66. {
  67. NRF_TWI_SENSOR_REG_SET(reg, LPS22HB_RESET_ARP_MASK, LPS22HB_RESET_ARP_POS, 1);
  68. }
  69. uint8_t send_msg[] = {
  70. LPS22HB_REG_INTERRUPT_CONFIG,
  71. reg
  72. };
  73. return nrf_twi_sensor_write(p_instance->p_sensor_data,
  74. p_instance->sensor_addr,
  75. send_msg,
  76. ARRAY_SIZE(send_msg),
  77. true);
  78. }
  79. ret_code_t lps22hb_autozero_enable(lps22hb_instance_t * p_instance, bool enable)
  80. {
  81. ASSERT(p_instance != NULL);
  82. uint8_t reg = p_instance->interrupt_cfg;
  83. if (enable == true)
  84. {
  85. NRF_TWI_SENSOR_REG_SET(reg, LPS22HB_AUTOZERO_MASK, LPS22HB_AUTOZERO_POS, 1);
  86. }
  87. else
  88. {
  89. NRF_TWI_SENSOR_REG_SET(reg, LPS22HB_RESET_AZ_MASK, LPS22HB_RESET_AZ_POS, 1);
  90. }
  91. uint8_t send_msg[] = {
  92. LPS22HB_REG_INTERRUPT_CONFIG,
  93. reg
  94. };
  95. return nrf_twi_sensor_write(p_instance->p_sensor_data,
  96. p_instance->sensor_addr,
  97. send_msg,
  98. ARRAY_SIZE(send_msg),
  99. true);
  100. }
  101. void lps22hb_data_rate_set(lps22hb_instance_t * p_instance, lps22hb_odr_t odr)
  102. {
  103. ASSERT(p_instance != NULL);
  104. NRF_TWI_SENSOR_REG_SET(p_instance->ctrl_reg[0], LPS22HB_ODR_MASK, LPS22HB_ODR_POS, odr);
  105. }
  106. ret_code_t lps22hb_data_read(lps22hb_instance_t * p_instance,
  107. lps22hb_data_callback_t user_callback,
  108. lps22hb_data_t * p_out_data,
  109. uint8_t samples)
  110. {
  111. ASSERT(p_instance != NULL);
  112. ret_code_t err_code;
  113. err_code = nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  114. p_instance->sensor_addr,
  115. LPS22HB_REG_PRESS_OUT_XL,
  116. (nrf_twi_sensor_reg_cb_t) user_callback,
  117. (uint8_t *) p_out_data,
  118. samples * LPS22HB_BYTES_PER_SAMPLE);
  119. return err_code;
  120. }
  121. void lps22hb_data_decode(lps22hb_data_t * p_data, uint8_t samples)
  122. {
  123. ASSERT(p_data != NULL);
  124. lps22hb_raw_data_t * p_in_data = (lps22hb_raw_data_t *) p_data;
  125. uint32_t pres;
  126. uint16_t temp;
  127. for (int i = samples-1; i >= 0; i--)
  128. {
  129. pres = ((uint32_t) p_in_data[i].press_out_xl) |
  130. (((uint32_t) p_in_data[i].press_out_l) << 8) |
  131. (((uint32_t) p_in_data[i].press_out_h) << 16);
  132. pres <<= 8;
  133. temp = ((uint16_t) p_in_data[i].temp_out_l) |
  134. (((uint16_t) p_in_data[i].temp_out_h) << 8);
  135. // Dividing by 256 because signed integer can't be shifted by 8
  136. p_data[i].pressure = *((int32_t *) &pres) / 256;
  137. p_data[i].temperature = *((int16_t *) &temp);
  138. }
  139. }
  140. ret_code_t lps22hb_threshold_set(lps22hb_instance_t * p_instance, uint16_t thr)
  141. {
  142. ASSERT(p_instance != NULL);
  143. thr *= 16;
  144. uint8_t send_msg[] = {
  145. LPS22HB_REG_THS_P_L,
  146. thr & 0x00FFU,
  147. thr >> 8
  148. };
  149. ret_code_t err_code;
  150. err_code = nrf_twi_sensor_write(p_instance->p_sensor_data,
  151. p_instance->sensor_addr,
  152. send_msg,
  153. ARRAY_SIZE(send_msg),
  154. true);
  155. return err_code;
  156. }
  157. ret_code_t lps22hb_ref_pressure_set(lps22hb_instance_t * p_instance, int32_t pressure)
  158. {
  159. ASSERT(p_instance != NULL);
  160. // Multiplying by 256 because signed integer can't be shifted by 8
  161. pressure *= 256;
  162. uint32_t pres = *((uint32_t *) &pressure);
  163. pres >>= 8;
  164. uint8_t send_msg[] = {
  165. LPS22HB_REG_REF_P_XL,
  166. pres & 0x00FFU,
  167. (pres >> 8) & 0x00FFU,
  168. (pres >> 16) & 0x00FFU
  169. };
  170. ret_code_t err_code;
  171. err_code = nrf_twi_sensor_write(p_instance->p_sensor_data,
  172. p_instance->sensor_addr,
  173. send_msg,
  174. ARRAY_SIZE(send_msg),
  175. true);
  176. return err_code;
  177. }
  178. ret_code_t lps22hb_offset_set(lps22hb_instance_t * p_instance, int16_t offset)
  179. {
  180. ASSERT(p_instance != NULL);
  181. offset *= 16;
  182. uint16_t off = *((uint16_t *) &offset);
  183. uint8_t send_msg[] = {
  184. LPS22HB_REG_RPDS_L,
  185. off & 0x00FFU,
  186. off >> 8
  187. };
  188. ret_code_t err_code;
  189. err_code = nrf_twi_sensor_write(p_instance->p_sensor_data,
  190. p_instance->sensor_addr,
  191. send_msg,
  192. ARRAY_SIZE(send_msg),
  193. true);
  194. return err_code;
  195. }
  196. ret_code_t lps22hb_cfg_commit(lps22hb_instance_t * p_instance)
  197. {
  198. ASSERT(p_instance != NULL);
  199. p_instance->ctrl_reg[1] |= LPS22HB_CTRL_REG2_DEFAULT;
  200. p_instance->ctrl_reg[0] &= ~LPS22HB_CTRL1_VALID_MASK;
  201. p_instance->ctrl_reg[1] &= ~LPS22HB_CTRL2_VALID_MASK;
  202. ret_code_t err_code;
  203. err_code = nrf_twi_sensor_reg_write(p_instance->p_sensor_data,
  204. p_instance->sensor_addr,
  205. LPS22HB_REG_INTERRUPT_CONFIG,
  206. &p_instance->interrupt_cfg,
  207. 1);
  208. if (err_code != NRF_SUCCESS)
  209. {
  210. return err_code;
  211. }
  212. err_code = nrf_twi_sensor_reg_write(p_instance->p_sensor_data,
  213. p_instance->sensor_addr,
  214. LPS22HB_REG_CTRL1,
  215. p_instance->ctrl_reg,
  216. 3);
  217. if (err_code != NRF_SUCCESS)
  218. {
  219. return err_code;
  220. }
  221. err_code = nrf_twi_sensor_reg_write(p_instance->p_sensor_data,
  222. p_instance->sensor_addr,
  223. LPS22HB_REG_FIFO_CTRL,
  224. &p_instance->fifo_ctrl,
  225. 1);
  226. return err_code;
  227. }
  228. ret_code_t lps22hb_sw_reset(lps22hb_instance_t * p_instance)
  229. {
  230. ASSERT(p_instance != NULL);
  231. uint8_t reg_val = p_instance->ctrl_reg[1];
  232. NRF_TWI_SENSOR_REG_SET(reg_val, LPS22HB_SWRESET_MASK, LPS22HB_SWRESET_POS, 1);
  233. uint8_t send_msg[] = {
  234. LPS22HB_REG_CTRL2,
  235. reg_val
  236. };
  237. ret_code_t err_code;
  238. err_code = nrf_twi_sensor_write(p_instance->p_sensor_data,
  239. p_instance->sensor_addr,
  240. send_msg,
  241. ARRAY_SIZE(send_msg),
  242. true);
  243. return err_code;
  244. }
  245. ret_code_t lps22hb_boot(lps22hb_instance_t * p_instance)
  246. {
  247. ASSERT(p_instance != NULL);
  248. uint8_t reg_val = p_instance->ctrl_reg[1];
  249. NRF_TWI_SENSOR_REG_SET(reg_val, LPS22HB_BOOT_MASK, LPS22HB_BOOT_POS, 1);
  250. uint8_t send_msg[] = {
  251. LPS22HB_REG_CTRL2,
  252. reg_val
  253. };
  254. ret_code_t err_code;
  255. err_code = nrf_twi_sensor_write(p_instance->p_sensor_data,
  256. p_instance->sensor_addr,
  257. send_msg,
  258. ARRAY_SIZE(send_msg),
  259. true);
  260. return err_code;
  261. }
  262. ret_code_t lps22hb_oneshot(lps22hb_instance_t * p_instance)
  263. {
  264. ASSERT(p_instance != NULL);
  265. uint8_t reg_val = p_instance->ctrl_reg[1];
  266. NRF_TWI_SENSOR_REG_SET(reg_val, LPS22HB_ONE_SHOT_MASK, LPS22HB_ONE_SHOT_POS, 1);
  267. uint8_t send_msg[] = {
  268. LPS22HB_REG_CTRL2,
  269. reg_val
  270. };
  271. ret_code_t err_code;
  272. err_code = nrf_twi_sensor_write(p_instance->p_sensor_data,
  273. p_instance->sensor_addr,
  274. send_msg,
  275. ARRAY_SIZE(send_msg),
  276. true);
  277. return err_code;
  278. }
  279. ret_code_t lps22hb_low_power_enable(lps22hb_instance_t * p_instance, bool enable)
  280. {
  281. ASSERT(p_instance != NULL);
  282. uint8_t send_msg[] = {
  283. LPS22HB_REG_RES_CONF,
  284. enable
  285. };
  286. ret_code_t err_code;
  287. err_code = nrf_twi_sensor_write(p_instance->p_sensor_data,
  288. p_instance->sensor_addr,
  289. send_msg,
  290. ARRAY_SIZE(send_msg),
  291. true);
  292. return err_code;
  293. }