bh1745.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 BH1745_H
  41. #define BH1745_H
  42. #include "nrf_twi_sensor.h"
  43. #include "bh1745_internal.h"
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @brief Possible sensor addresses.
  49. */
  50. #define BH1745_BASE_ADDRESS_LOW 0x38U
  51. #define BH1745_BASE_ADDRESS_HIGH 0x39U
  52. // Minimum nrf_twi_sensor message buffer size and nrf_twi_mngr queue length.
  53. #define BH1745_MIN_QUEUE_SIZE 5
  54. /**
  55. * @brief Sensor driver usage.
  56. *
  57. * Sensor instance has to be defined first in global context using @ref BH1745_INSTANCE DEF.
  58. * After that it has to be initialized using @ref bh1745_init.
  59. * At this point sensor instance is ready and all other functions can be used.
  60. *
  61. * Configuration functions schedule TWI operation using @ref nrf_twi_sensor module.
  62. * After calling function, setting will be automatically send to sensor when TWI bus is free.
  63. *
  64. * There are designated functions to read status sensor registers e.g. @ref bh1745_sys_ctrl_read
  65. * As parameters they receive function to be called after register is read, and pointer where
  66. * register value should be stored. From that value specific parameters can be extracted
  67. * using @ref NRF_TWI_SENSOR_REG_VAL_GET macro.
  68. * Example:
  69. * uint8_t part_id = NRF_TWI_SENSOR_REG_VAL_GET(sys_ctrl_reg,
  70. * BH1745_PART_ID_MASK,
  71. * BH1745_PART_ID_POS);
  72. *
  73. * Other functions are self-explanatory or have description on their usage.
  74. */
  75. /**
  76. * @brief Measurement time.
  77. */
  78. typedef enum
  79. {
  80. BH1745_MEAS_TIME_160MS,
  81. BH1745_MEAS_TIME_320MS,
  82. BH1745_MEAS_TIME_640MS,
  83. BH1745_MEAS_TIME_1280MS,
  84. BH1745_MEAS_TIME_2560MS,
  85. BH1745_MEAS_TIME_5120MS
  86. } bh1745_meas_time_t;
  87. /**
  88. * @brief RGBC (red, green, blue, clear) gain setting.
  89. */
  90. typedef enum
  91. {
  92. BH1745_GAIN_1X,
  93. BH1745_GAIN_2X,
  94. BH1745_GAIN_16X
  95. } bh1745_gain_t;
  96. /**
  97. * @brief Persistence settings.
  98. */
  99. typedef enum
  100. {
  101. BH1745_TOGGLE_EACH_MEASURE,
  102. BH1745_UPDATE_EACH_MEASURE,
  103. BH1745_UPDATE_EVERY_FOURTH,
  104. BH1745_UPDATE_EVERY_EIGHTH
  105. } bh1745_persistence_t;
  106. /**
  107. * @brief Interrupt source settings.
  108. */
  109. typedef enum
  110. {
  111. BH1745_RED_CHANNEL,
  112. BH1745_GREEN_CHANNEL,
  113. BH1745_BLUE_CHANNEL,
  114. BH1745_CLEAR_CHANNEL
  115. } bh1745_int_source_t;
  116. /**
  117. * @brief Measurement result.
  118. */
  119. typedef struct
  120. {
  121. uint16_t red; //!< Raw red color value.
  122. uint16_t green; //!< Raw green color value.
  123. uint16_t blue; //!< Raw blue color value.
  124. uint16_t clear; //!< Raw clear color pass value.
  125. uint8_t valid; //!< Mode control 2 register value, used for checking data validity.
  126. } bh1745_data_t;
  127. /**
  128. * @brief RGBC data callback prototype.
  129. *
  130. * @param result Result of operation (NRF_SUCCESS on success,
  131. * otherwise a relevant error code).
  132. * @param[in] p_user_data Pointer to color data structure.
  133. */
  134. typedef void (* bh1745_data_callback_t)(ret_code_t result, bh1745_data_t * p_user_data);
  135. /**
  136. * @brief Macro that creates sensor instance.
  137. *
  138. * @param[in] _bh1745_inst_name Sensor instance name.
  139. * @param[in] _p_twi_sensor Pointer to common TWI sensor instance. @ref NRF_TWI_SENSOR_DEF
  140. * @param[in] _sensor_address Sensor base address.
  141. */
  142. #define BH1745_INSTANCE_DEF(_bh1745_inst_name, _p_twi_sensor, _sensor_address) \
  143. BH1745_INTERNAL_INSTANCE_DEF(_bh1745_inst_name, _p_twi_sensor, _sensor_address)
  144. /**
  145. * @brief Function for initializing the BH1745 sensor instance.
  146. *
  147. * TWI manager queue length has to be at least BH1745_MIN_QUEUE_SIZE.
  148. *
  149. * @param[in] p_instance Pointer to the sensor instance.
  150. *
  151. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  152. */
  153. ret_code_t bh1745_init(bh1745_instance_t * p_instance);
  154. /**
  155. * @brief Function for resetting the BH1745 registers.
  156. *
  157. * @param[in] p_instance Pointer to the sensor instance.
  158. *
  159. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  160. */
  161. ret_code_t bh1745_sw_reset(bh1745_instance_t * p_instance);
  162. /**
  163. * @brief Function for resetting the interrupt.
  164. *
  165. * @param[in] p_instance Pointer to the sensor instance.
  166. *
  167. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  168. */
  169. ret_code_t bh1745_int_reset(bh1745_instance_t * p_instance);
  170. /**
  171. * @brief Function for setting measurement configuration.
  172. *
  173. * @param[in] p_instance Pointer to the sensor instance.
  174. * @param[in] meas_time Measurement time.
  175. * @param[in] enable Enable RGBC measurements.
  176. * @param[in] gain Measurement gain.
  177. *
  178. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  179. */
  180. ret_code_t bh1745_meas_cfg(bh1745_instance_t * p_instance,
  181. bh1745_meas_time_t meas_time,
  182. bool enable,
  183. bh1745_gain_t gain);
  184. /**
  185. * @brief Function for setting interrupt configuration.
  186. *
  187. * @param p_instance Pointer to sensor instance.
  188. * @param latch INT pin latch.
  189. * @arg false INT pin is latched until INTERRUPT register is read or initialized.
  190. * @arg true INT pin is updated after each measurement.
  191. * @param source Interrupt source.
  192. * @param enable Enable INT pin.
  193. * @param persistence Set persistence.
  194. *
  195. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  196. */
  197. ret_code_t bh1745_int_cfg(bh1745_instance_t * p_instance,
  198. bool latch,
  199. bh1745_int_source_t source,
  200. bool enable,
  201. bh1745_persistence_t persistance);
  202. /**
  203. * @brief Function for setting high interrupt threshold.
  204. *
  205. * @param[in] p_instance Pointer to sensor instance.
  206. * @param[in] threshold Threshold value.
  207. *
  208. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  209. */
  210. ret_code_t bh1745_high_thr_set(bh1745_instance_t * p_instance,
  211. uint16_t threshold);
  212. /**
  213. * @brief Function for setting low interrupt threshold.
  214. *
  215. * @param[in] p_instance Pointer to sensor instance.
  216. * @param[in] threshold Threshold value.
  217. *
  218. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  219. */
  220. ret_code_t bh1745_low_thr_set(bh1745_instance_t * p_instance,
  221. uint16_t threshold);
  222. /**
  223. * @brief Function for getting the BH1745 data.
  224. *
  225. * @param[in] p_instance Pointer to the sensor instance.
  226. * @param[in] user_callback Callback function created by user.
  227. * @param[out] p_data The measurement results.
  228. *
  229. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  230. */
  231. ret_code_t bh1745_data_read(bh1745_instance_t * p_instance,
  232. bh1745_data_callback_t user_callback,
  233. bh1745_data_t * p_data);
  234. /**
  235. * @brief Function for reading system control register.
  236. *
  237. * @param[in] p_instance Pointer to sensor instance.
  238. * @param[in] user_cb Function to be called after register is read.
  239. * @param[out] reg_val Register value, single uint8_t.
  240. *
  241. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  242. */
  243. __STATIC_INLINE ret_code_t bh1745_sys_ctrl_read(bh1745_instance_t * p_instance,
  244. nrf_twi_sensor_reg_cb_t user_cb,
  245. uint8_t * reg_val);
  246. /**
  247. * @brief Function for reading interrupt register.
  248. *
  249. * @param[in] p_instance Pointer to sensor instance.
  250. * @param[in] user_cb Function to be called after register is read.
  251. * @param[out] reg_val Register value, single uint8_t.
  252. *
  253. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  254. */
  255. __STATIC_INLINE ret_code_t bh1745_interrupt_read(bh1745_instance_t * p_instance,
  256. nrf_twi_sensor_reg_cb_t user_cb,
  257. uint8_t * reg_val);
  258. /**
  259. * @brief Function for reading manufacturer id register.
  260. *
  261. * @param[in] p_instance Pointer to sensor instance.
  262. * @param[in] user_cb Function to be called after register is read.
  263. * @param[out] reg_val Register value, single uint8_t.
  264. *
  265. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  266. */
  267. __STATIC_INLINE ret_code_t bh1745_manu_id_read(bh1745_instance_t * p_instance,
  268. nrf_twi_sensor_reg_cb_t user_cb,
  269. uint8_t * reg_val);
  270. /**
  271. * @brief Function for checking if RGBC data has been updated after last configuration change.
  272. *
  273. * This function should be used in data callback.
  274. *
  275. * @param[in] p_instance Pointer to the sensor instance.
  276. *
  277. * @return True if data is valid.
  278. */
  279. __STATIC_INLINE bool bh1745_is_data_valid(bh1745_data_t * p_data);
  280. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  281. __STATIC_INLINE ret_code_t bh1745_sys_ctrl_read(bh1745_instance_t * p_instance,
  282. nrf_twi_sensor_reg_cb_t user_cb,
  283. uint8_t * reg_val)
  284. {
  285. ASSERT(p_instance != NULL);
  286. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  287. p_instance->sensor_addr,
  288. BH1745_REG_SYSTEM_CONTROL,
  289. user_cb,
  290. reg_val,
  291. 1);
  292. }
  293. __STATIC_INLINE ret_code_t bh1745_interrupt_read(bh1745_instance_t * p_instance,
  294. nrf_twi_sensor_reg_cb_t user_cb,
  295. uint8_t * reg_val)
  296. {
  297. ASSERT(p_instance != NULL);
  298. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  299. p_instance->sensor_addr,
  300. BH1745_REG_INTERRUPT,
  301. user_cb,
  302. reg_val,
  303. 1);
  304. }
  305. __STATIC_INLINE ret_code_t bh1745_manu_id_read(bh1745_instance_t * p_instance,
  306. nrf_twi_sensor_reg_cb_t user_cb,
  307. uint8_t * reg_val)
  308. {
  309. ASSERT(p_instance != NULL);
  310. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  311. p_instance->sensor_addr,
  312. BH1745_REG_MANUFACTURER_ID,
  313. user_cb,
  314. reg_val,
  315. 1);
  316. }
  317. __STATIC_INLINE bool bh1745_is_data_valid(bh1745_data_t * p_data)
  318. {
  319. return NRF_TWI_SENSOR_REG_VAL_GET(p_data->valid,
  320. BH1745_VALID_MASK,
  321. BH1745_VALID_POS);
  322. }
  323. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  324. #ifdef __cplusplus
  325. }
  326. #endif
  327. #endif // BH1745_H