bh1745.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "bh1745.h"
  41. #define BH1745_SENSOR_WRITE(p_instance, msg) \
  42. nrf_twi_sensor_write(p_instance->p_sensor_data, \
  43. p_instance->sensor_addr, \
  44. msg, \
  45. ARRAY_SIZE(msg), \
  46. true)
  47. ret_code_t bh1745_init(bh1745_instance_t * p_instance)
  48. {
  49. ASSERT(p_instance != NULL);
  50. if (p_instance->p_sensor_data->p_twi_mngr->p_queue->size < BH1745_MIN_QUEUE_SIZE)
  51. {
  52. return NRF_ERROR_INVALID_LENGTH;
  53. }
  54. static const uint8_t msg1[] = {
  55. BH1745_REG_MODE_CONTROL1,
  56. 0x00,
  57. 0x00,
  58. 0x00
  59. };
  60. ret_code_t err = BH1745_SENSOR_WRITE(p_instance, msg1);
  61. if (err != NRF_SUCCESS)
  62. {
  63. return err;
  64. }
  65. static const uint8_t msg2[] = {
  66. BH1745_REG_INTERRUPT,
  67. 0x00,
  68. 0x01,
  69. 0xFF,
  70. 0xFF,
  71. 0x00,
  72. 0x00
  73. };
  74. return BH1745_SENSOR_WRITE(p_instance, msg2);
  75. }
  76. ret_code_t bh1745_sw_reset(bh1745_instance_t * p_instance)
  77. {
  78. ASSERT(p_instance != NULL);
  79. static const uint8_t send_msg[] = {
  80. BH1745_REG_SYSTEM_CONTROL,
  81. BH1745_SW_RESET_MASK
  82. };
  83. return BH1745_SENSOR_WRITE(p_instance, send_msg);
  84. }
  85. ret_code_t bh1745_int_reset(bh1745_instance_t * p_instance)
  86. {
  87. ASSERT(p_instance != NULL);
  88. static const uint8_t send_msg[] = {
  89. BH1745_REG_SYSTEM_CONTROL,
  90. BH1745_INT_RESET_MASK
  91. };
  92. return BH1745_SENSOR_WRITE(p_instance, send_msg);
  93. }
  94. ret_code_t bh1745_meas_cfg(bh1745_instance_t * p_instance,
  95. bh1745_meas_time_t meas_time,
  96. bool enable,
  97. bh1745_gain_t gain)
  98. {
  99. ASSERT(p_instance != NULL);
  100. if (meas_time > BH1745_MEAS_TIME_5120MS)
  101. {
  102. meas_time = BH1745_MEAS_TIME_5120MS;
  103. }
  104. if (gain > BH1745_GAIN_16X)
  105. {
  106. gain = BH1745_GAIN_16X;
  107. }
  108. uint8_t send_msg[] = {
  109. BH1745_REG_MODE_CONTROL1,
  110. 0,
  111. 0,
  112. 0x02
  113. };
  114. NRF_TWI_SENSOR_REG_SET(send_msg[1], BH1745_MEAS_TIME_MASK, BH1745_MEAS_TIME_POS, meas_time);
  115. NRF_TWI_SENSOR_REG_SET(send_msg[2], BH1745_RGBC_EN_MASK, BH1745_RGBC_EN_POS, enable);
  116. NRF_TWI_SENSOR_REG_SET(send_msg[2], BH1745_ADC_GAIN_MASK, BH1745_ADC_GAIN_POS, gain);
  117. return BH1745_SENSOR_WRITE(p_instance, send_msg);
  118. }
  119. ret_code_t bh1745_data_read(bh1745_instance_t * p_instance,
  120. bh1745_data_callback_t user_callback,
  121. bh1745_data_t * p_data)
  122. {
  123. ASSERT(p_instance != NULL);
  124. ASSERT(p_data != NULL);
  125. ret_code_t err_code;
  126. err_code = nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  127. p_instance->sensor_addr,
  128. BH1745_REG_MODE_CONTROL2,
  129. NULL,
  130. &p_data->valid,
  131. 1);
  132. if (err_code != NRF_SUCCESS)
  133. {
  134. return err_code;
  135. }
  136. err_code = nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  137. p_instance->sensor_addr,
  138. BH1745_REG_RED_DATA_LSB,
  139. (nrf_twi_sensor_reg_cb_t) user_callback,
  140. (uint8_t *) &p_data->red,
  141. BH1745_DATA_REG_NUM);
  142. return err_code;
  143. }
  144. ret_code_t bh1745_int_cfg(bh1745_instance_t * p_instance,
  145. bool latch,
  146. bh1745_int_source_t source,
  147. bool enable,
  148. bh1745_persistence_t persistance)
  149. {
  150. ASSERT(p_instance != NULL);
  151. uint8_t int_reg = 0;
  152. NRF_TWI_SENSOR_REG_SET(int_reg, BH1745_INT_ENABLE_MASK, BH1745_INT_ENABLE_POS, enable);
  153. NRF_TWI_SENSOR_REG_SET(int_reg, BH1745_INT_SOURCE_MASK, BH1745_INT_SOURCE_POS, source);
  154. NRF_TWI_SENSOR_REG_SET(int_reg, BH1745_INT_LATCH_MASK, BH1745_INT_LATCH_POS, latch);
  155. uint8_t send_msg[] = {
  156. BH1745_REG_INTERRUPT,
  157. int_reg,
  158. persistance
  159. };
  160. return BH1745_SENSOR_WRITE(p_instance, send_msg);
  161. }
  162. ret_code_t bh1745_high_thr_set(bh1745_instance_t * p_instance,
  163. uint16_t threshold)
  164. {
  165. ASSERT(p_instance != NULL);
  166. uint8_t send_msg[] = {
  167. BH1745_REG_TH_LSB,
  168. LSB_16(threshold),
  169. MSB_16(threshold)
  170. };
  171. return BH1745_SENSOR_WRITE(p_instance, send_msg);
  172. }
  173. ret_code_t bh1745_low_thr_set(bh1745_instance_t * p_instance,
  174. uint16_t threshold)
  175. {
  176. ASSERT(p_instance != NULL);
  177. uint8_t send_msg[] = {
  178. BH1745_REG_TL_LSB,
  179. LSB_16(threshold),
  180. MSB_16(threshold)
  181. };
  182. return BH1745_SENSOR_WRITE(p_instance, send_msg);
  183. }