lps22hb.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 LPS22HB_H
  41. #define LPS22HB_H
  42. #include "nrf_twi_sensor.h"
  43. #include "lps22hb_internal.h"
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @brief Possible sensor addresses.
  49. */
  50. #define LPS22HB_BASE_ADDRESS_LOW 0x5CU
  51. #define LPS22HB_BASE_ADDRESS_HIGH 0x5DU
  52. // WHO_AM_I register value
  53. #define LPS22HB_WHO_AM_I 0xB1
  54. // Minimum nrf_twi_sensor message buffer size and nrf_twi_mngr queue length.
  55. #define LPS22HB_MIN_QUEUE_SIZE 4
  56. /**
  57. * @brief Sensor driver usage.
  58. *
  59. * Sensor instance has to be defined first in global context using @ref LPS22HB_INSTANCE DEF.
  60. * After that it has to be initialized using @ref lps22hb_init.
  61. * At this point sensor instance is ready and all other functions can be used.
  62. *
  63. * There are two ways in which sensor settings are set:
  64. *
  65. * First one are asynchronous macros, using them does not change real sensor settings
  66. * until @ref lps22hb_cfg_commit is called.
  67. * Example:
  68. * LPS22HB_DATA_CFG(m_sensor1, LPS22HB_ODR_POWERDOWN, false, false);
  69. * LPS22HB_FIFO_CFG(m_sensor1, LPS22HB_STREAM, true, false, 15);
  70. * lps22hb_cfg_commit(&m_sensor1);
  71. *
  72. * Second way are functions, functions schedule TWI operation using @ref nrf_twi_sensor module.
  73. * After calling function, setting will be automatically send to sensor when TWI bus is free.
  74. * Example:
  75. * lps22hb_low_power_enable(&m_sensor1, true);
  76. * lps22hb_offset_set(&m_sensor1, -27);
  77. *
  78. * There are designated functions to read status sensor registers e.g. @ref lps22hb_int_source_read
  79. * As parameters they receive function to be called after register is read, and pointer where
  80. * register value should be stored. From that value specific parameters can be extracted
  81. * using @ref NRF_TWI_SENSOR_REG_VAL_GET macro.
  82. * Example:
  83. * uint8_t ia = NRF_TWI_SENSOR_REG_VAL_GET(int_source_reg, LPS22HB_IA_MASK, LPS22HB_IA_POS);
  84. *
  85. * Other functions are self-explanatory or have description on their usage.
  86. */
  87. /**
  88. * @brief Output data rate settings.
  89. */
  90. typedef enum
  91. {
  92. LPS22HB_ODR_POWERDOWN,
  93. LPS22HB_ODR_1HZ,
  94. LPS22HB_ODR_10HZ,
  95. LPS22HB_ODR_25HZ,
  96. LPS22HB_ODR_50HZ,
  97. LPS22HB_ODR_75HZ
  98. } lps22hb_odr_t;
  99. /**
  100. * @brief Fifo mode settings.
  101. */
  102. typedef enum
  103. {
  104. LPS22HB_BYPASS,
  105. LPS22HB_FIFO,
  106. LPS22HB_STREAM,
  107. LPS22HB_STREAM_TO_FIFO,
  108. LPS22HB_BYPASS_TO_STREAM,
  109. LPS22HB_RESERVED_FIFO,
  110. LPS22HB_DYNAMIC_STREAM,
  111. LPS22HB_BYPASS_TO_FIFO
  112. } lps22hb_fifo_mode_t;
  113. /**
  114. * @brief Low pass filter configuration.
  115. */
  116. typedef enum
  117. {
  118. LPS22HB_LPFP_DISABLE = 1,
  119. LPS22HB_LPFP_ODR_DIV_9,
  120. LPS22HB_LPFP_ODR_DIV_20
  121. } lps22hb_lpfp_t;
  122. /**
  123. * @brief Pressure and temperature output data.
  124. *
  125. * @note To get pressure in hPa it has to be divided by 4096.
  126. * To get temperature in degrees it has to be divided by 100.
  127. */
  128. typedef struct
  129. {
  130. int32_t pressure;
  131. int16_t temperature;
  132. } lps22hb_data_t;
  133. /**
  134. * @brief Data callback prototype.
  135. *
  136. * @param[in] result Result of operation (NRF_SUCCESS on success,
  137. * otherwise a relevant error code).
  138. * @param[in] p_raw_data Pointer to raw sensor data structure.
  139. */
  140. typedef void (* lps22hb_data_callback_t)(ret_code_t result, lps22hb_data_t * p_raw_data);
  141. /**
  142. * @brief Macro creating lps22hb sensor instance.
  143. *
  144. * @param[in] _lps22hb_inst_name Sensor instance name.
  145. * @param[in] _p_twi_sensor Pointer to common TWI sensor instance.
  146. * @param[in] _sensor_address Sensor base address.
  147. */
  148. #define LPS22HB_INSTANCE_DEF(_lps22hb_inst_name, _p_twi_sensor, _sensor_address) \
  149. LPS22HB_INTERNAL_INSTANCE_DEF(_lps22hb_inst_name, _p_twi_sensor, _sensor_address)
  150. /**
  151. * ===============================================================================================
  152. * @brief Sensor configuration macros.
  153. *
  154. * @note After setting configuration using these macros, it has to be committed to sensor
  155. * using @ref lps22hb_cfg_commit
  156. */
  157. /**
  158. * @brief Macro for interrupt configuration.
  159. *
  160. * @param[in] _s Sensor instance.
  161. * @param[in] _diff_en Enable interrupt generation. True if enabled.
  162. * @param[in] _lir Latch interrupt request to INT_SOURCE register. True if enabled.
  163. * @param[in] _ple Enable interrupt generation on pressure low event. True if enabled.
  164. * @param[in] _phe Enable interrupt generation on pressure high event. True if enabled.
  165. */
  166. #define LPS22HB_INT_CFG(_s, _diff_en, _lir, _ple, _phe)\
  167. LPS22HB_INTERNAL_INT_CFG(_s, _diff_en, _lir, _ple, _phe)
  168. /**
  169. * @brief Macro for data acquisition configuration.
  170. *
  171. * @param[in] _s Sensor instance.
  172. * @param[in] _odr Desired output data rate. @ref lps22hb_odr_t
  173. * @param[in] _f_en Enables filter. True if enabled.
  174. * @param[in] _f_cfg Filter configuration.
  175. * @arg true Filter bandwidth is ODR/20
  176. * @arg false Filter bandwidth is ODR/9
  177. */
  178. #define LPS22HB_DATA_CFG(_s, _odr, _f_en, _f_cfg)\
  179. LPS22HB_INTERNAL_DATA_CFG(_s, _odr, _f_en, _f_cfg)
  180. /**
  181. * @brief Macro for FIFO configuration.
  182. *
  183. * @param[in] _s Sensor instance.
  184. * @param[in] _f_mode FIFO mode. @ref lps22hb_fifo_mode_t
  185. * @param[in] _f_en Enable FIFO. True if enabled.
  186. * @param[in] _f_stop Stop on FIFO watermark. True if enabled.
  187. * @param[in] _f_wtm FIFO watermark value. Between 0 and 31.
  188. *
  189. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  190. */
  191. #define LPS22HB_FIFO_CFG(_s, _f_mode, _f_en, _f_stop, _f_wtm)\
  192. LPS22HB_INTERNAL_FIFO_CFG(_s, _f_mode, _f_en, _f_stop, _f_wtm)
  193. /**
  194. * @brief Macro for INT_DRDY pin configuration.
  195. * @param[in] _s Sensor instance.
  196. * @param[in] _activ Active state.
  197. * @arg true Active low.
  198. * @arg false Active high.
  199. * @param[in] _pp_od Pin operation.
  200. * @arg true Open drain.
  201. * @arg false Push-pull.
  202. * @param[in] _fss FIFO full flag. True if enabled.
  203. * @param[in] _fth FIFO watermark status. True if enabled.
  204. * @param[in] _ovr FIFO overrun interrupt. True if enabled.
  205. * @param[in] _drdy Data Ready signal. True if enabled.
  206. * @param[in] _high Pressure higher than interrupt threshold. True if enabled.
  207. * @param[in] _low Pressure lower than interrupt threshold. True if enabled.
  208. */
  209. #define LPS22HB_DRDY_CFG(_s, _activ, _pp_od, _fss, _fth, _ovr, _drdy, _high, _low)\
  210. LPS22HB_INTERNAL_DRDY_CFG(_s, _activ, _pp_od, _fss, _fth, _ovr, _drdy, _high, _low)
  211. /**
  212. * ===============================================================================================
  213. */
  214. /**
  215. * @brief Function for initializing lps22hb sensor.
  216. *
  217. * Writes configuration data in sensor instance to sensor.
  218. *
  219. * @param[in] p_instance Pointer to sensor instance created by macro
  220. *
  221. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  222. */
  223. ret_code_t lps22hb_init(lps22hb_instance_t * p_instance);
  224. /**
  225. * @brief Function for enabling autorifp.
  226. *
  227. * @param[in] p_instance Pointer to sensor instance
  228. * @param[in] enable Autorifp setting.
  229. * @arg true Autorifp is enabled.
  230. * @arg false Autorifp is disabled and reset.
  231. *
  232. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  233. */
  234. ret_code_t lps22hb_autorifp_enable(lps22hb_instance_t * p_instance, bool enable);
  235. /**
  236. * @brief Function for enabling autozero.
  237. *
  238. * @param[in] p_instance Pointer to sensor instance
  239. * @param[in] enable Autozero setting.
  240. * @arg true Autozero is enabled.
  241. * @arg false Autozero is disabled and reset.
  242. *
  243. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  244. */
  245. ret_code_t lps22hb_autozero_enable(lps22hb_instance_t * p_instance, bool enable);
  246. /**
  247. * @brief Function performing software reset.
  248. *
  249. * @param[in] p_instance Pointer to sensor instance.
  250. *
  251. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  252. */
  253. ret_code_t lps22hb_sw_reset(lps22hb_instance_t * p_instance);
  254. /**
  255. * @brief Function performing boot.
  256. *
  257. * @param[in] p_instance Pointer to sensor instance.
  258. *
  259. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  260. */
  261. ret_code_t lps22hb_boot(lps22hb_instance_t * p_instance);
  262. /**
  263. * @brief Function setting oneshot.
  264. *
  265. * @param[in] p_instance Pointer to sensor instance.
  266. *
  267. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  268. */
  269. ret_code_t lps22hb_oneshot(lps22hb_instance_t * p_instance);
  270. /**
  271. * @brief Function for reading pressure and temperature data.
  272. *
  273. * @param[in] p_instance Pointer to sensor instance.
  274. * @param[in] user_callback Function to be called when data is gathered.
  275. * @param[out] p_out_data Pointer to raw data buffer.
  276. * @param[in] samples Number of data samples to read.
  277. *
  278. * @note Data can be read in two ways. With or without sensors FIFO.
  279. * FIFO mode depends on FIFO mode set using lps22hb_fifo_mode_set function.
  280. * FIFO is enabled using lps22hb_fifo_enable function.
  281. * Without FIFO only one sample can be acquired, p_out_data can be pointer to single variable.
  282. * With FIFO enabled, data can be read in burst mode, p_out_data table has to be same
  283. * or bigger than number of samples to read.
  284. *
  285. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  286. */
  287. ret_code_t lps22hb_data_read(lps22hb_instance_t * p_instance,
  288. lps22hb_data_callback_t user_callback,
  289. lps22hb_data_t * p_out_data,
  290. uint8_t samples);
  291. /**
  292. * @brief Function for converting raw sensor data to real.
  293. *
  294. * @param[in/out] p_data Pointer to data to be processed.
  295. * @param[in] samples Number of samples to be processed.
  296. *
  297. * @note After data is processed, structure contains pressure in hPa*4096
  298. * and temperature in Celsius degrees*100
  299. */
  300. void lps22hb_data_decode(lps22hb_data_t * p_data, uint8_t samples);
  301. /**
  302. * @brief Function for setting reference pressure.
  303. *
  304. * @param[in] p_instance Pointer to sensor instance.
  305. * @param[in] pressure Reference pressure in hPa*4096
  306. *
  307. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  308. */
  309. ret_code_t lps22hb_ref_pressure_set(lps22hb_instance_t * p_instance, int32_t pressure);
  310. /**
  311. * @brief Function for setting pressure offset.
  312. *
  313. * @param[in] p_instance Pointer to sensor instance.
  314. * @param[in] offset Pressure offset in hPa.
  315. *
  316. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  317. */
  318. ret_code_t lps22hb_offset_set(lps22hb_instance_t * p_instance, int16_t offset);
  319. /**
  320. * @brief Function for setting interrupt threshold.
  321. *
  322. * @param[in] p_instance Pointer to sensor instance.
  323. * @param[in] threshold Interrupt threshold in hPa.
  324. *
  325. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  326. */
  327. ret_code_t lps22hb_threshold_set(lps22hb_instance_t * p_instance, uint16_t threshold);
  328. /**
  329. * @brief Function for enabling low power mode.
  330. *
  331. * @param[in] p_instance Pointer to sensor instance.
  332. * @param[in] enable Enable low power mode. True if enabled.
  333. *
  334. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  335. */
  336. ret_code_t lps22hb_low_power_enable(lps22hb_instance_t * p_instance, bool enable);
  337. /**
  338. * @brief Function for setting sensor configuration.
  339. *
  340. * @param[in] p_instance Pointer to sensor instance.
  341. *
  342. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  343. */
  344. ret_code_t lps22hb_cfg_commit(lps22hb_instance_t * p_instance);
  345. /**
  346. * @brief Function for resetting filter.
  347. *
  348. * @param[in] p_instance Pointer to sensor instance.
  349. *
  350. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  351. */
  352. __STATIC_INLINE ret_code_t lps22hb_reset_filter(lps22hb_instance_t * p_instance);
  353. /**
  354. * @brief Function for reading who am i register.
  355. *
  356. * @param[in] p_instance Pointer to sensor instance.
  357. * @param[in] user_cb Function to be called after register is read.
  358. * @param[out] reg_val Register value, single uint8_t.
  359. *
  360. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  361. */
  362. __STATIC_INLINE ret_code_t lps22hb_who_am_i_read(lps22hb_instance_t * p_instance,
  363. nrf_twi_sensor_reg_cb_t user_cb,
  364. uint8_t * reg_val);
  365. /**
  366. * @brief Function for reading interrupt source register.
  367. *
  368. * @param[in] p_instance Pointer to sensor instance.
  369. * @param[in] user_cb Function to be called after register is read.
  370. * @param[out] reg_val Register value, single uint8_t.
  371. *
  372. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  373. */
  374. __STATIC_INLINE ret_code_t lps22hb_int_source_read(lps22hb_instance_t * p_instance,
  375. nrf_twi_sensor_reg_cb_t user_cb,
  376. uint8_t * reg_val);
  377. /**
  378. * @brief Function for reading fifo status register.
  379. *
  380. * @param[in] p_instance Pointer to sensor instance.
  381. * @param[in] user_cb Function to be called after register is read.
  382. * @param[out] reg_val Register value, single uint8_t.
  383. *
  384. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  385. */
  386. __STATIC_INLINE ret_code_t lps22hb_fifo_status_read(lps22hb_instance_t * p_instance,
  387. nrf_twi_sensor_reg_cb_t user_cb,
  388. uint8_t * reg_val);
  389. /**
  390. * @brief Function for reading status register.
  391. *
  392. * @param[in] p_instance Pointer to sensor instance.
  393. * @param[in] user_cb Function to be called after register is read.
  394. * @param[out] reg_val Register value, single uint8_t.
  395. *
  396. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  397. */
  398. __STATIC_INLINE ret_code_t lps22hb_status_read(lps22hb_instance_t * p_instance,
  399. nrf_twi_sensor_reg_cb_t user_cb,
  400. uint8_t * reg_val);
  401. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  402. __STATIC_INLINE ret_code_t lps22hb_reset_filter(lps22hb_instance_t * p_instance)
  403. {
  404. ASSERT(p_instance != NULL);
  405. static uint8_t temp;
  406. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  407. p_instance->sensor_addr,
  408. LPS22HB_REG_LPFP_RES,
  409. NULL,
  410. &temp,
  411. 1);
  412. }
  413. __STATIC_INLINE ret_code_t lps22hb_who_am_i_read(lps22hb_instance_t * p_instance,
  414. nrf_twi_sensor_reg_cb_t user_cb,
  415. uint8_t * reg_val)
  416. {
  417. ASSERT(p_instance != NULL);
  418. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  419. p_instance->sensor_addr,
  420. LPS22HB_REG_WHO_AM_I,
  421. user_cb,
  422. reg_val,
  423. 1);
  424. }
  425. __STATIC_INLINE ret_code_t lps22hb_int_source_read(lps22hb_instance_t * p_instance,
  426. nrf_twi_sensor_reg_cb_t user_cb,
  427. uint8_t * reg_val)
  428. {
  429. ASSERT(p_instance != NULL);
  430. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  431. p_instance->sensor_addr,
  432. LPS22HB_REG_INT_SOURCE,
  433. user_cb,
  434. reg_val,
  435. 1);
  436. }
  437. __STATIC_INLINE ret_code_t lps22hb_fifo_status_read(lps22hb_instance_t * p_instance,
  438. nrf_twi_sensor_reg_cb_t user_cb,
  439. uint8_t * reg_val)
  440. {
  441. ASSERT(p_instance != NULL);
  442. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  443. p_instance->sensor_addr,
  444. LPS22HB_REG_FIFO_STATUS,
  445. user_cb,
  446. reg_val,
  447. 1);
  448. }
  449. __STATIC_INLINE ret_code_t lps22hb_status_read(lps22hb_instance_t * p_instance,
  450. nrf_twi_sensor_reg_cb_t user_cb,
  451. uint8_t * reg_val)
  452. {
  453. ASSERT(p_instance != NULL);
  454. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  455. p_instance->sensor_addr,
  456. LPS22HB_REG_STATUS,
  457. user_cb,
  458. reg_val,
  459. 1);
  460. }
  461. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  462. #ifdef __cplusplus
  463. }
  464. #endif
  465. #endif // LPS22HB_H