ccs811.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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_H
  41. #define CCS811_H
  42. #include "nrf_twi_sensor.h"
  43. #include "ccs811_internal.h"
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @brief Possible sensor addresses.
  49. */
  50. #define CCS811_BASE_ADDRESS_LOW 0x5AU
  51. #define CCS811_BASE_ADDRESS_HIGH 0x5BU
  52. // Minimum nrf_twi_sensor message buffer size and nrf_twi_mngr queue length.
  53. #define CCS811_MIN_QUEUE_SIZE 6
  54. // Hardware ID register value
  55. #define CCS811_HARDWARE_ID 0x81
  56. /**
  57. * @brief Sensor driver usage.
  58. *
  59. * Sensor instance has to be defined first in global context using @ref CCS811_INSTANCE_DEF.
  60. * After that it has to be initialized using @ref ccs811_init.
  61. * At this point sensor instance is ready and all other functions can be used.
  62. *
  63. * Configuration functions schedule TWI operation using @ref nrf_twi_sensor module.
  64. * After calling function, setting will be automatically send to sensor when TWI bus is free.
  65. *
  66. * There are designated functions to read status sensor registers e.g. @ref ccs811_status_read
  67. * As parameters they receive function to be called after register is read, and pointer where
  68. * register value should be stored. From that value specific parameters can be extracted
  69. * using @ref NRF_TWI_SENSOR_REG_VAL_GET macro.
  70. * Example:
  71. * bool drdy = NRF_TWI_SENSOR_REG_VAL_GET(status, CCS811_DATA_READY_MASK, CCS811_DATA_READY_POS);
  72. *
  73. * Other functions are self-explanatory or have description on their usage.
  74. */
  75. /**
  76. * @brief Drive mode setting.
  77. */
  78. typedef enum
  79. {
  80. CCS811_MODE_0,//!< CCS811_MODE_0 - Idle
  81. CCS811_MODE_1,//!< CCS811_MODE_1 - Constant power, measure every second.
  82. CCS811_MODE_2,//!< CCS811_MODE_2 - Pulse heating mode, measure every 10 seconds.
  83. CCS811_MODE_3,//!< CCS811_MODE_3 - Low power pulse heating mode, measure every 60 seconds.
  84. CCS811_MODE_4 //!< CCS811_MODE_4 - Constant power, measure every 250ms, only raw data.
  85. } ccs811_drive_mode_t;
  86. /**
  87. * @brief Last byte read from algorithm data.
  88. *
  89. * Used with @ref ccs811_alg_data_read function, defines to which byte data should be read.
  90. */
  91. typedef enum
  92. {
  93. CCS811_LAST_ECO2 = 2,
  94. CCS811_LAST_TVOC = 4,
  95. CCS811_LAST_STATUS,
  96. CCS811_LAST_ERROR_ID,
  97. CCS811_LAST_RAW = 8
  98. } ccs811_last_data_byte_t;
  99. /**
  100. * @brief Structure for holding algorithm data.
  101. */
  102. typedef struct
  103. {
  104. uint16_t eco2; //!< eC02 value in ppm.
  105. uint16_t tvoc; //!< TVOC value in ppb.
  106. uint8_t status; //!< Status register data.
  107. uint8_t error_id; //!< Error register data.
  108. uint16_t raw; //!< Raw data.
  109. } ccs811_alg_data_t;
  110. /**
  111. * @brief Data callback prototype.
  112. *
  113. * @param[in] result Return code from TWI manager and underlying drivers.
  114. * @param[in] p_data Pointer to sensor data.
  115. */
  116. typedef void (* ccs811_data_callback_t)(ret_code_t result, ccs811_alg_data_t * p_data);
  117. /**
  118. * @brief Macro that creates sensor instance.
  119. *
  120. * @param[in] _ccs811_inst_name Sensor instance name.
  121. * @param[in] _p_twi_sensor Pointer to common TWI sensor instance.
  122. * @param[in] _sensor_address Sensor base address.
  123. */
  124. #define CCS811_INSTANCE_DEF(_ccs811_inst_name, _p_twi_sensor, _sensor_address) \
  125. CCS811_INTERNAL_INSTANCE_DEF(_ccs811_inst_name, _p_twi_sensor, _sensor_address)
  126. /**
  127. * @brief Function initializing ccs811 sensor
  128. *
  129. * TWI manager queue length has to be at least CCS811_MIN_QUEUE_SIZE
  130. *
  131. * @param[in] p_instance Pointer to sensor instance created by macro
  132. *
  133. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  134. */
  135. ret_code_t ccs811_init(ccs811_instance_t const * p_instance);
  136. /**
  137. * @brief Function for setting drive mode.
  138. *
  139. * @param[in] p_instance Pointer to sensor instance.
  140. * @param[in] mode Drive mode.
  141. * @param[in] drdy_en Enable data ready pin.
  142. * @param[in] thr_en Enable threshold.
  143. *
  144. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  145. */
  146. ret_code_t ccs811_drive_mode_set(ccs811_instance_t const * p_instance,
  147. ccs811_drive_mode_t mode,
  148. bool drdy_en,
  149. bool thr_en);
  150. /**
  151. * @brief Function for reading sensor data.
  152. *
  153. * @param[in] p_instance Pointer to sensor instance.
  154. * @param[in] user_cb Function to be called after data read.
  155. * @param[out] p_alg_data Pointer to structure holding sensor algorithm data.
  156. * @param[in] last Last byte to read.
  157. *
  158. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  159. */
  160. ret_code_t ccs811_alg_data_read(ccs811_instance_t const * p_instance,
  161. ccs811_data_callback_t user_cb,
  162. ccs811_alg_data_t * p_alg_data,
  163. ccs811_last_data_byte_t last);
  164. /**
  165. * @brief Function for processing algorithm data
  166. *
  167. * @param[in/out] p_alg_data Pointer to read data to be processed.
  168. */
  169. void ccs811_alg_data_process(ccs811_alg_data_t * p_alg_data);
  170. /**
  171. * @brief Function for setting environment temperature.
  172. *
  173. * @param[in] p_instance Pointer to sensor instance.
  174. * @param[in] temp_value Temperature value (-25 to 100 degree celsius).
  175. * @param[in] temp_fraction Temperature fraction.
  176. * @param[in] hum_percent Humidity percent.
  177. * @param[in] hum_fraction Humidity fraction.
  178. *
  179. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  180. */
  181. ret_code_t ccs811_env_set(ccs811_instance_t const * p_instance,
  182. int8_t temp_value,
  183. uint16_t temp_fraction,
  184. uint8_t hum_percent,
  185. uint16_t hum_fraction);
  186. /**
  187. * @brief Function for threshold configuration
  188. *
  189. * @param[in] p_instance Pointer to sensor instance.
  190. * @param[in] l_to_m Low to medium threshold.
  191. * @param[in] m_to_h Medium to high threshold.
  192. * @param[in] hysteresis Hysteresis.
  193. *
  194. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  195. */
  196. ret_code_t ccs811_thr_cfg(ccs811_instance_t const * p_instance,
  197. uint16_t l_to_m,
  198. uint16_t m_to_h,
  199. uint8_t hysteresis);
  200. /**
  201. * @brief Function for reading baseline.
  202. *
  203. * @param[in] p_instance Pointer to sensor instance.
  204. * @param[in] user_cb Function to be called after baseline read.
  205. * @param[out] baseline Baseline value, single uint16_t.
  206. *
  207. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  208. */
  209. ret_code_t ccs811_baseline_read(ccs811_instance_t const * p_instance,
  210. nrf_twi_sensor_reg_cb_t user_cb,
  211. uint16_t * p_baseline);
  212. /**
  213. * @brief Function for setting baseline.
  214. *
  215. * @param[in] p_instance Pointer to sensor instance.
  216. * @param[in] baseline Baseline value.
  217. *
  218. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  219. */
  220. ret_code_t ccs811_baseline_set(ccs811_instance_t const * p_instance,
  221. uint16_t baseline);
  222. /**
  223. * @brief Function commencing software reset.
  224. *
  225. * @param[in] p_instance Pointer to sensor instance.
  226. *
  227. * @note To use sensor after reset, it has to be initialized again using @ref ccs811_init function.
  228. *
  229. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  230. */
  231. ret_code_t ccs811_sw_reset(ccs811_instance_t const * p_instance);
  232. /**
  233. * @brief Function for reading status register.
  234. *
  235. * @param[in] p_instance Pointer to sensor instance.
  236. * @param[in] user_cb Function to be called after register is read.
  237. * @param[out] p_reg_val Pointer to register value, single uint8_t.
  238. *
  239. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  240. */
  241. __STATIC_INLINE ret_code_t ccs811_status_read(ccs811_instance_t const * p_instance,
  242. nrf_twi_sensor_reg_cb_t user_cb,
  243. uint8_t * p_reg_val);
  244. /**
  245. * @brief Function for reading hardware id register.
  246. *
  247. * @param[in] p_instance Pointer to sensor instance.
  248. * @param[in] user_cb Function to be called after register is read.
  249. * @param[out] p_reg_val Pointer to register value, single uint8_t.
  250. *
  251. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  252. */
  253. __STATIC_INLINE ret_code_t ccs811_hw_id_read(ccs811_instance_t const * p_instance,
  254. nrf_twi_sensor_reg_cb_t user_cb,
  255. uint8_t * p_reg_val);
  256. /**
  257. * @brief Function for reading error id register.
  258. *
  259. * @param[in] p_instance Pointer to sensor instance.
  260. * @param[in] user_cb Function to be called after register is read.
  261. * @param[out] p_reg_val Pointer to register value, single uint8_t.
  262. *
  263. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  264. */
  265. __STATIC_INLINE ret_code_t ccs811_error_read(ccs811_instance_t const * p_instance,
  266. nrf_twi_sensor_reg_cb_t user_cb,
  267. uint8_t * p_reg_val);
  268. #ifdef __cplusplus
  269. }
  270. #endif
  271. #endif // CCS811_H