co2  2.0.0.0
co2.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef CO2_H
29 #define CO2_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_i2c_master.h"
48 
69 #define CO2_REG_DISABLE_CRC 0x3768
70 #define CO2_REG_SET_BINARY_GAS 0x3615
71 #define CO2_REG_SET_RELATIVE_HUMIDITY 0x3624
72 #define CO2_REG_SET_TEMPERATURE 0x361E
73 #define CO2_REG_SET_PRESSURE 0x362F
74 #define CO2_REG_MEASURE_GAS_CONCENTRATION 0x3639
75 #define CO2_REG_FORCED_RECALIBRATION 0x3661
76 #define CO2_REG_ENABLE_AUTO_SELF_CALIB 0x3FEF
77 #define CO2_REG_DISABLE_AUTO_SELF_CALIB 0x3F6E
78 #define CO2_REG_PREPARE_READ_STATE 0x3752
79 #define CO2_REG_READ_STATE 0xE133
80 #define CO2_REG_WRITE_STATE 0xE133
81 #define CO2_REG_APPLY_STATE 0x3650
82 #define CO2_REG_SELF_TEST 0x365B
83 #define CO2_REG_RESET 0x0006
84 #define CO2_REG_ENTER_SLEEP_MODE 0x3677
85  // co2_reg
87 
102 #define CO2_SET_BINARY_GAS_IN_N2_100PCT 0x0000
103 #define CO2_SET_BINARY_GAS_IN_AIR_100PCT 0x0001
104 #define CO2_SET_BINARY_GAS_IN_N2_25PCT 0x0002
105 #define CO2_SET_BINARY_GAS_IN_AIR_25PCT 0x0003
106 
107 #define CO2_DEVICE_ID 0x08010301
108 
114 #define CO2_SET_DEV_ADDR_GND 0x29
115 #define CO2_SET_DEV_ADDR_1KOHMS 0x2C
116 #define CO2_SET_DEV_ADDR_3p3KOHMS 0x2B
117 #define CO2_SET_DEV_ADDR_10KOHMS 0x2A
118  // co2_set
120 
135 #define CO2_MAP_MIKROBUS( cfg, mikrobus ) \
136  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
137  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA );
138  // co2_map // co2
141 
146 typedef struct
147 {
148  // Modules
149  i2c_master_t i2c;
151  // I2C slave address
152  uint8_t slave_address;
154  // Device & serial IDs
155  uint32_t device_id;
156  uint32_t serial_id[ 2 ];
157  uint8_t crc_enabled;
158 
159 } co2_t;
160 
165 typedef struct
166 {
167  pin_name_t scl;
168  pin_name_t sda;
170  uint32_t i2c_speed;
171  uint8_t i2c_address;
173 } co2_cfg_t;
174 
179 typedef enum
180 {
181  CO2_OK = 0,
182  CO2_ERROR = -1,
184  CO2_ERROR_CRC = -3
185 
187 
203 void co2_cfg_setup ( co2_cfg_t *cfg );
204 
219 err_t co2_init ( co2_t *ctx, co2_cfg_t *cfg );
220 
235 err_t co2_default_cfg ( co2_t *ctx );
236 
252 err_t co2_generic_write ( co2_t *ctx, uint16_t reg, uint16_t *tx_buf, uint8_t tx_len );
253 
270 err_t co2_generic_read ( co2_t *ctx, uint16_t reg, uint16_t *rx_buf, uint8_t rx_len );
271 
280 err_t co2_reset ( co2_t *ctx );
281 
293 err_t co2_get_id ( co2_t *ctx );
294 
308 err_t co2_set_reference ( co2_t *ctx, uint16_t humidity, uint16_t pressure );
309 
322 err_t co2_read_gas ( co2_t *ctx, float *gas_concentration, float *temperature );
323 
336 err_t co2_disable_crc ( co2_t *ctx );
337 
338 #ifdef __cplusplus
339 }
340 #endif
341 #endif // CO2_H
342  // co2
344 
345 // ------------------------------------------------------------------------ END
co2_generic_write
err_t co2_generic_write(co2_t *ctx, uint16_t reg, uint16_t *tx_buf, uint8_t tx_len)
CO2 I2C writing function.
co2_reset
err_t co2_reset(co2_t *ctx)
Reset device.
co2_cfg_t::scl
pin_name_t scl
Definition: co2.h:167
co2_get_id
err_t co2_get_id(co2_t *ctx)
Read device and serial ID's.
co2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: co2.h:170
co2_t::crc_enabled
uint8_t crc_enabled
Definition: co2.h:157
CO2_ERROR
@ CO2_ERROR
Definition: co2.h:182
co2_return_value_t
co2_return_value_t
CO2 Click return value data.
Definition: co2.h:180
co2_cfg_t::sda
pin_name_t sda
Definition: co2.h:168
CO2_ERROR_CRC
@ CO2_ERROR_CRC
Definition: co2.h:184
CO2_OK
@ CO2_OK
Definition: co2.h:181
co2_default_cfg
err_t co2_default_cfg(co2_t *ctx)
CO2 default configuration function.
co2_cfg_t::i2c_address
uint8_t i2c_address
Definition: co2.h:171
CO2_ERROR_SELF_TEST
@ CO2_ERROR_SELF_TEST
Definition: co2.h:183
co2_set_reference
err_t co2_set_reference(co2_t *ctx, uint16_t humidity, uint16_t pressure)
Set device refrence values for gas calculation.
co2_t::slave_address
uint8_t slave_address
Definition: co2.h:152
co2_t
CO2 Click context object.
Definition: co2.h:147
co2_t::device_id
uint32_t device_id
Definition: co2.h:155
co2_cfg_setup
void co2_cfg_setup(co2_cfg_t *cfg)
CO2 configuration object setup function.
co2_t::i2c
i2c_master_t i2c
Definition: co2.h:149
co2_disable_crc
err_t co2_disable_crc(co2_t *ctx)
Disable use of CRC.
co2_init
err_t co2_init(co2_t *ctx, co2_cfg_t *cfg)
CO2 initialization function.
co2_generic_read
err_t co2_generic_read(co2_t *ctx, uint16_t reg, uint16_t *rx_buf, uint8_t rx_len)
CO2 I2C reading function.
co2_read_gas
err_t co2_read_gas(co2_t *ctx, float *gas_concentration, float *temperature)
Read CO2 concentration and temperature value.
co2_cfg_t
CO2 Click configuration object.
Definition: co2.h:166