ccs811_internal.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #ifndef CCS811_INTERNAL_H
  41. #define CCS811_INTERNAL_H
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /**
  46. * @brief CCS811 sensor registers.
  47. */
  48. #define CCS811_REG_STATUS 0x00
  49. #define CCS811_REG_MEAS_MODE 0x01
  50. #define CCS811_REG_ALG_RESULT_DATA 0x02
  51. #define CCS811_REG_RAW_DATA 0x03
  52. #define CCS811_REG_ENV_DATA 0x05
  53. #define CCS811_REG_NTC 0x06
  54. #define CCS811_REG_THRESHOLDS 0x10
  55. #define CCS811_REG_BASELINE 0x11
  56. #define CCS811_REG_HW_ID 0x20
  57. #define CCS811_REG_HW_VER 0x21
  58. #define CCS811_REG_FW_BOOT_VER 0x23
  59. #define CCS811_REG_FW_APP_VER 0x24
  60. #define CCS811_REG_ERROR_ID 0xE0
  61. #define CCS811_REG_SW_RESET 0xFF
  62. #define CCS811_REG_APP_START 0xF4
  63. /**
  64. * @brief CCS811 Default configuration values.
  65. */
  66. #define CCS811_DEFAULT_HUMIDITY 50
  67. #define CCS811_DEFAULT_TEMPERATURE 25
  68. #define CCS811_DEFAULT_LOW_THR 1500
  69. #define CCS811_DEFAULT_HIGH_THR 2500
  70. #define CCS811_DEFAULT_HYSTERESIS 50
  71. /**
  72. * @brief CCS811 Environment temperature offset.
  73. */
  74. #define CCS811_TEMPERATURE_OFFSET 25
  75. /**
  76. * @brief Status register bitmasks.
  77. */
  78. // Bitmasks for FW_MODE.
  79. #define CCS811_FW_MODE_POS 7
  80. #define CCS811_FW_MODE_MASK (1 << CCS811_FW_MODE_POS)
  81. // Bitmasks for APP_VALID.
  82. #define CCS811_APP_VALID_POS 4
  83. #define CCS811_APP_VALID_MASK (1 << CCS811_APP_VALID_POS)
  84. // Bitmasks for DATA_READY.
  85. #define CCS811_DATA_READY_POS 3
  86. #define CCS811_DATA_READY_MASK (1 << CCS811_DATA_READY_POS)
  87. // Bitmasks for ERROR.
  88. #define CCS811_ERROR_POS 0
  89. #define CCS811_ERROR_MASK (1 << CCS811_ERROR_POS)
  90. /**
  91. * @brief Meas mode register bitmasks.
  92. */
  93. // Register validity mask.
  94. #define CCS811_MEAS_MODE_VALID_MASK 0x83U
  95. // Bitmasks for DRIVE_MODE.
  96. #define CCS811_DRIVE_MODE_POS 4
  97. #define CCS811_DRIVE_MODE_MASK (7 << CCS811_DRIVE_MODE_POS)
  98. // Bitmasks for INTERRUPT.
  99. #define CCS811_INTERRUPT_POS 3
  100. #define CCS811_INTERRUPT_MASK (1 << CCS811_INTERRUPT_MASK)
  101. // Bitmasks for THRESH.
  102. #define CCS811_THRESH_POS 2
  103. #define CCS811_THRESH_MASK (1 << CCS811_THRESH_POS)
  104. /**
  105. * @brief Algorithm results data bytes.
  106. */
  107. #define CCS811_ALG_RESULT_BYTE_NUM 8
  108. #define CCS811_ALG_ECO2_H_BYTE 0
  109. #define CCS811_ALG_ECO2_L_BYTE 1
  110. #define CCS811_ALG_TVOC_H_BYTE 2
  111. #define CCS811_ALG_TVOC_L_BYTE 3
  112. #define CCS811_ALG_STATUS_BYTE 4
  113. #define CCS811_ALG_ERROR_BYTE 5
  114. #define CCS811_ALG_RAW_DATA1_BYTE 6
  115. #define CCS811_ALG_RAW_DATA2_BYTE 7
  116. /**
  117. * @brief Environment data register.
  118. */
  119. #define CCS811_ENV_HUMIDITY_H_BYTE 0
  120. // Humidity percent position.
  121. #define CCS811_ENV_HUM_PERCENT_POS 9
  122. // Bitmasks for humidity fraction.
  123. #define CCS811_ENV_HUM_FRACTION_MASK (0x01FF)
  124. // Temperature value position.
  125. #define CCS811_ENV_TEMP_VALUE_POS 9
  126. // Bitmasks for temperature fraction.
  127. #define CCS811_ENV_TEMP_FRACTION_MASK (0x01FF)
  128. /**
  129. * @brief Error register.
  130. */
  131. // Bitmasks for HEATER_SUPPLY
  132. #define CCS811_ERROR_HEATER_SUPPLY_POS 5
  133. #define CCS811_ERROR_HEATER_SUPPLY_MASK (1 << CCS811_ERROR_HEATER_SUPPLY_POS)
  134. // Bitmasks for HEATER_FAULT
  135. #define CCS811_ERROR_HEATER_FAULT_POS 4
  136. #define CCS811_ERROR_HEATER_FAULT_MASK (1 << CCS811_ERROR_HEATER_FAULT_POS)
  137. // Bitmasks for MAX_RESISTANCE
  138. #define CCS811_ERROR_MAX_RESISTANCE_POS 3
  139. #define CCS811_ERROR_MAX_RESISTANCE_MASK (1 << CCS811_ERROR_MAX_RESISTANCE_POS)
  140. // Bitmasks for MEASMODE_INVALID
  141. #define CCS811_ERROR_MEAS_MODE_POS 2
  142. #define CCS811_ERROR_MEAS_MODE_MASK (1 CCS811_ERROR_MEAS_MODE_POS)
  143. // Bitmasks for READ_REG
  144. #define CCS811_ERROR_READ_REG_POS 1
  145. #define CCS811_ERROR_READ_REG_MASK (1 << CCS811_ERROR_READ_REG_POS)
  146. // Bitmasks for WRITE_REG
  147. #define CCS811_ERROR_WRITE_REG_POS 0
  148. #define CCS811_ERROR_WRITE_REG_MASK (1 << CCS811_ERROR_WRITE_REG_POS)
  149. /**
  150. * @brief Software reset register.
  151. */
  152. #define CCS811_SW_RESET_BYTE0 0x11
  153. #define CCS811_SW_RESET_BYTE1 0xE5
  154. #define CCS811_SW_RESET_BYTE2 0x72
  155. #define CCS811_SW_RESET_BYTE3 0x8A
  156. /**
  157. * @brief Structure holding sensor instance
  158. */
  159. typedef struct
  160. {
  161. nrf_twi_sensor_t * const p_sensor_data;
  162. uint8_t const sensor_addr;
  163. } ccs811_instance_t;
  164. /**
  165. * @brief Macro that creates sensor instance.
  166. */
  167. #define CCS811_INTERNAL_INSTANCE_DEF(_ccs811_inst_name, _p_twi_sensor, _sensor_address) \
  168. static ccs811_instance_t _ccs811_inst_name = \
  169. { \
  170. .p_sensor_data = _p_twi_sensor, \
  171. .sensor_addr = _sensor_address, \
  172. }
  173. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  174. __STATIC_INLINE ret_code_t ccs811_status_read(ccs811_instance_t const * p_instance,
  175. nrf_twi_sensor_reg_cb_t user_cb,
  176. uint8_t * p_reg_val)
  177. {
  178. ASSERT(p_instance != NULL);
  179. ASSERT(p_reg_val != NULL);
  180. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  181. p_instance->sensor_addr,
  182. CCS811_REG_STATUS,
  183. user_cb,
  184. p_reg_val,
  185. 1);
  186. }
  187. __STATIC_INLINE ret_code_t ccs811_hw_id_read(ccs811_instance_t const * p_instance,
  188. nrf_twi_sensor_reg_cb_t user_cb,
  189. uint8_t * p_reg_val)
  190. {
  191. ASSERT(p_instance != NULL);
  192. ASSERT(p_reg_val != NULL);
  193. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  194. p_instance->sensor_addr,
  195. CCS811_REG_HW_ID,
  196. user_cb,
  197. p_reg_val,
  198. 1);
  199. }
  200. __STATIC_INLINE ret_code_t ccs811_error_read(ccs811_instance_t const * p_instance,
  201. nrf_twi_sensor_reg_cb_t user_cb,
  202. uint8_t * p_reg_val)
  203. {
  204. ASSERT(p_instance != NULL);
  205. ASSERT(p_reg_val != NULL);
  206. return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
  207. p_instance->sensor_addr,
  208. CCS811_REG_ERROR_ID,
  209. user_cb,
  210. p_reg_val,
  211. 1);
  212. }
  213. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  214. #ifdef __cplusplus
  215. }
  216. #endif
  217. #endif // CCS811_INTERNAL_H