co23  2.1.0.0
co23.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 CO23_H
29 #define CO23_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_i2c_master.h"
52 #include "drv_uart.h"
53 
74 #define CO23_REG_PROD_ID 0x00
75 #define CO23_REG_SENS_STS 0x01
76 #define CO23_REG_MEAS_RATE_H 0x02
77 #define CO23_REG_MEAS_RATE_L 0x03
78 #define CO23_REG_MEAS_CFG 0x04
79 #define CO23_REG_CO2PPM_H 0x05
80 #define CO23_REG_CO2PPM_L 0x06
81 #define CO23_REG_MEAS_STS 0x07
82 #define CO23_REG_INT_CFG 0x08
83 #define CO23_REG_ALARM_TH_H 0x09
84 #define CO23_REG_ALARM_TH_L 0x0A
85 #define CO23_REG_PRESS_REF_H 0x0B
86 #define CO23_REG_PRESS_REF_L 0x0C
87 #define CO23_REG_CALIB_REF_H 0x0D
88 #define CO23_REG_CALIB_REF_L 0x0E
89 #define CO23_REG_SCRATCH_PAD 0x0F
90 #define CO23_REG_SENS_RST 0x10
91  // co23_reg
93 
108 #define CO23_OP_MODE_IDLE 0x00
109 #define CO23_OP_MODE_SINGLE 0x01
110 #define CO23_OP_MODE_CONTINUOUS 0x02
111 #define CO23_OP_BOC_CFG_DISABLE 0x00
112 #define CO23_OP_BOC_CFG_AUTOMATIC 0x01
113 #define CO23_OP_BOC_CFG_FORCED 0x02
114 #define CO23_PWM_MODE_SINGLE_PULSE 0x00
115 #define CO23_PWM_MODE_TRAIN_PULSE 0x01
116 
122 #define CO23_TX_DRV_BUFFER_SIZE 100
123 #define CO23_RX_DRV_BUFFER_SIZE 300
124 
130 #define CO23_DEVICE_ADDRESS 0x28
131  // co23_set
133 
148 #define CO23_MAP_MIKROBUS( cfg, mikrobus ) \
149  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
150  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
151  cfg.pwo = MIKROBUS( mikrobus, MIKROBUS_AN ); \
152  cfg.pws = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
153  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
154  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
155  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
156  // co23_map // co23
159 
164 typedef enum
165 {
170 
175 typedef err_t ( *co23_master_io_t )( struct co23_s*, uint8_t, uint8_t*, uint8_t );
181 typedef struct co23_s
182 {
183  // Output pins
184  digital_out_t pws;
186  // Input pins
187  digital_in_t pwo;
188  digital_in_t int_pin;
190  // Modules
191  i2c_master_t i2c;
192  uart_t uart;
194  // I2C slave address
195  uint8_t slave_address;
197  // Buffers
206 
211 typedef struct
212 {
213  // Communication gpio pins
214  pin_name_t rx_pin;
215  pin_name_t tx_pin;
217  pin_name_t scl;
218  pin_name_t sda;
220  pin_name_t pwo;
221  pin_name_t pws;
222  pin_name_t int_pin;
224  // Static variable
225  uint32_t baud_rate;
227  uart_data_bits_t data_bit;
228  uart_parity_t parity_bit;
229  uart_stop_bits_t stop_bit;
231  uint32_t i2c_speed;
232  uint8_t i2c_address;
236 } co23_cfg_t;
237 
242 typedef struct
243 {
244  uint8_t sen_rdy;
245  uint8_t pwm_dis_st;
246  uint8_t ortmp;
247  uint8_t orvs;
248  uint8_t iccer;
249  uint8_t ortmp_clr;
250  uint8_t orvs_clr;
251  uint8_t iccer_clr;
252 
254 
259 typedef struct
260 {
261  uint8_t pwm_outen;
262  uint8_t pwm_mode;
263  uint8_t boc_cfg;
264  uint8_t op_mode;
265 
267 
272 typedef struct
273 {
274  uint8_t drdy;
275  uint8_t int_sts;
276  uint8_t alarm;
277  uint8_t int_sts_clr;
278  uint8_t alarm_clr;
279 
281 
286 typedef struct
287 {
288  uint8_t int_typ;
289  uint8_t int_func;
290  uint8_t alarm_typ;
291 
293 
298 typedef enum
299 {
306 
308 
313 typedef enum
314 {
315  CO23_OK = 0,
316  CO23_ERROR = -1
317 
319 
336 
352 
366 err_t co23_init ( co23_t *ctx, co23_cfg_t *cfg );
367 
380 err_t co23_default_cfg ( co23_t *ctx );
381 
396 err_t co23_generic_write ( co23_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
397 
412 err_t co23_generic_read ( co23_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
413 
427 err_t co23_get_device_id ( co23_t *ctx, uint8_t *prod, uint8_t *rev );
428 
442 err_t co23_get_sens_status ( co23_t *ctx, co23_sens_status_t *sens_status );
443 
456 err_t co23_set_meas_rate ( co23_t *ctx, uint16_t meas_rate_sec );
457 
471 err_t co23_get_meas_cfg ( co23_t *ctx, co23_meas_cfg_t *meas_cfg );
472 
487 err_t co23_set_meas_cfg ( co23_t *ctx, co23_meas_cfg_t meas_cfg );
488 
501 err_t co23_get_co2_ppm ( co23_t *ctx, uint16_t *co2_ppm );
502 
516 err_t co23_get_meas_status ( co23_t *ctx, co23_meas_status_t *meas_status );
517 
531 err_t co23_get_int_cfg ( co23_t *ctx, co23_int_cfg_t *int_cfg );
532 
547 err_t co23_set_int_cfg ( co23_t *ctx, co23_int_cfg_t int_cfg );
548 
561 err_t co23_set_alarm_threshold ( co23_t *ctx, uint16_t alarm_th_ppm );
562 
575 err_t co23_set_pressure_ref ( co23_t *ctx, uint16_t pressure_mbar );
576 
589 err_t co23_set_calib_ref ( co23_t *ctx, uint16_t calib_ppm );
590 
605 
616 uint8_t co23_get_pwo_pin ( co23_t *ctx );
617 
628 uint8_t co23_get_int_pin ( co23_t *ctx );
629 
630 #ifdef __cplusplus
631 }
632 #endif
633 #endif // CO23_H
634  // co23
636 
637 // ------------------------------------------------------------------------ END
co23_cfg_t::tx_pin
pin_name_t tx_pin
Definition: co23.h:215
co23_get_meas_status
err_t co23_get_meas_status(co23_t *ctx, co23_meas_status_t *meas_status)
CO2 3 get measurement status function.
co23_s::i2c
i2c_master_t i2c
Definition: co23.h:191
co23_get_int_cfg
err_t co23_get_int_cfg(co23_t *ctx, co23_int_cfg_t *int_cfg)
CO2 3 get interrupt pin configuration function.
co23_get_pwo_pin
uint8_t co23_get_pwo_pin(co23_t *ctx)
CO2 3 get PWM output function.
co23_int_cfg_t
CO2 3 Click interrupt configuration object.
Definition: co23.h:287
co23_cfg_t::drv_sel
co23_drv_t drv_sel
Definition: co23.h:234
CO23_OK
@ CO23_OK
Definition: co23.h:315
co23_get_meas_cfg
err_t co23_get_meas_cfg(co23_t *ctx, co23_meas_cfg_t *meas_cfg)
CO2 3 get measurement configuration function.
co23_int_cfg_t::int_func
uint8_t int_func
Definition: co23.h:289
co23_s::drv_sel
co23_drv_t drv_sel
Definition: co23.h:201
co23_cfg_setup
void co23_cfg_setup(co23_cfg_t *cfg)
CO2 3 configuration object setup function.
CO23_TX_DRV_BUFFER_SIZE
#define CO23_TX_DRV_BUFFER_SIZE
CO2 3 driver buffer size.
Definition: co23.h:122
co23_set_pressure_ref
err_t co23_set_pressure_ref(co23_t *ctx, uint16_t pressure_mbar)
CO2 3 set alarm threshold function.
co23_set_meas_rate
err_t co23_set_meas_rate(co23_t *ctx, uint16_t meas_rate_sec)
CO2 3 set measurement rate function.
co23_sens_status_t::orvs_clr
uint8_t orvs_clr
Definition: co23.h:250
co23_soft_reset_value_t
co23_soft_reset_value_t
CO2 3 Click soft reset command value data.
Definition: co23.h:299
co23_cfg_t::uart_blocking
bool uart_blocking
Definition: co23.h:226
co23_set_alarm_threshold
err_t co23_set_alarm_threshold(co23_t *ctx, uint16_t alarm_th_ppm)
CO2 3 set alarm threshold function.
co23_cfg_t::pws
pin_name_t pws
Definition: co23.h:221
co23_meas_cfg_t::pwm_mode
uint8_t pwm_mode
Definition: co23.h:262
co23_cfg_t::rx_pin
pin_name_t rx_pin
Definition: co23.h:214
co23_meas_cfg_t::op_mode
uint8_t op_mode
Definition: co23.h:264
co23_sens_status_t
CO2 3 Click sens status object.
Definition: co23.h:243
CO23_DRV_SEL_I2C
@ CO23_DRV_SEL_I2C
Definition: co23.h:166
co23_get_int_pin
uint8_t co23_get_int_pin(co23_t *ctx)
CO2 3 get interrupt function.
co23_generic_read
err_t co23_generic_read(co23_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
CO2 3 I2C reading function.
CO23_CMD_SAVE_FCS_CALIB_OFFSET
@ CO23_CMD_SAVE_FCS_CALIB_OFFSET
Definition: co23.h:303
co23_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: co23.h:229
co23_s::pws
digital_out_t pws
Definition: co23.h:184
co23_meas_status_t::int_sts
uint8_t int_sts
Definition: co23.h:275
co23_drv_t
co23_drv_t
CO2 3 Click driver selector.
Definition: co23.h:165
co23_int_cfg_t::int_typ
uint8_t int_typ
Definition: co23.h:288
co23_cfg_t::pwo
pin_name_t pwo
Definition: co23.h:220
co23_s::read_f
co23_master_io_t read_f
Definition: co23.h:203
co23_cfg_t::i2c_address
uint8_t i2c_address
Definition: co23.h:232
co23_meas_status_t::int_sts_clr
uint8_t int_sts_clr
Definition: co23.h:277
co23_default_cfg
err_t co23_default_cfg(co23_t *ctx)
CO2 3 default configuration function.
co23_set_calib_ref
err_t co23_set_calib_ref(co23_t *ctx, uint16_t calib_ppm)
CO2 3 set calibration function.
CO23_ERROR
@ CO23_ERROR
Definition: co23.h:316
co23_int_cfg_t::alarm_typ
uint8_t alarm_typ
Definition: co23.h:290
co23_s::uart_rx_buffer
char uart_rx_buffer[CO23_RX_DRV_BUFFER_SIZE]
Definition: co23.h:198
co23_meas_status_t::alarm_clr
uint8_t alarm_clr
Definition: co23.h:278
CO23_CMD_RESET_FCS
@ CO23_CMD_RESET_FCS
Definition: co23.h:304
co23_s::uart_tx_buffer
char uart_tx_buffer[CO23_TX_DRV_BUFFER_SIZE]
Definition: co23.h:199
co23_s::pwo
digital_in_t pwo
Definition: co23.h:187
co23_set_meas_cfg
err_t co23_set_meas_cfg(co23_t *ctx, co23_meas_cfg_t meas_cfg)
CO2 3 set measurement configuration function.
co23_drv_interface_selection
void co23_drv_interface_selection(co23_cfg_t *cfg, co23_drv_t drv_sel)
CO2 3 driver interface setup function.
CO23_CMD_STEPWISE_FILTER_EN
@ CO23_CMD_STEPWISE_FILTER_EN
Definition: co23.h:305
co23_meas_cfg_t::boc_cfg
uint8_t boc_cfg
Definition: co23.h:263
co23_s
CO2 3 Click context object.
Definition: co23.h:182
co23_sens_status_t::iccer
uint8_t iccer
Definition: co23.h:248
co23_master_io_t
err_t(* co23_master_io_t)(struct co23_s *, uint8_t, uint8_t *, uint8_t)
CO2 3 Click driver interface.
Definition: co23.h:175
co23_sens_status_t::sen_rdy
uint8_t sen_rdy
Definition: co23.h:244
co23_s::int_pin
digital_in_t int_pin
Definition: co23.h:188
co23_meas_status_t::alarm
uint8_t alarm
Definition: co23.h:276
co23_get_device_id
err_t co23_get_device_id(co23_t *ctx, uint8_t *prod, uint8_t *rev)
CO2 3 get device ID function.
CO23_CMD_STEPWISE_FILTER_DIS
@ CO23_CMD_STEPWISE_FILTER_DIS
Definition: co23.h:302
co23_cfg_t::int_pin
pin_name_t int_pin
Definition: co23.h:222
co23_s::uart
uart_t uart
Definition: co23.h:192
co23_meas_cfg_t::pwm_outen
uint8_t pwm_outen
Definition: co23.h:261
co23_get_sens_status
err_t co23_get_sens_status(co23_t *ctx, co23_sens_status_t *sens_status)
CO2 3 get sensor status function.
co23_t
struct co23_s co23_t
CO2 3 Click context object.
co23_get_co2_ppm
err_t co23_get_co2_ppm(co23_t *ctx, uint16_t *co2_ppm)
CO2 3 get CO2 concentration function.
co23_meas_status_t
CO2 3 Click measurement status object.
Definition: co23.h:273
CO23_RX_DRV_BUFFER_SIZE
#define CO23_RX_DRV_BUFFER_SIZE
Definition: co23.h:123
co23_cfg_t::sda
pin_name_t sda
Definition: co23.h:218
co23_sens_status_t::pwm_dis_st
uint8_t pwm_dis_st
Definition: co23.h:245
CO23_CMD_RESET_ABOC
@ CO23_CMD_RESET_ABOC
Definition: co23.h:301
co23_set_int_cfg
err_t co23_set_int_cfg(co23_t *ctx, co23_int_cfg_t int_cfg)
CO2 3 set interrupt pin configuration function.
co23_cfg_t
CO2 3 Click configuration object.
Definition: co23.h:212
co23_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: co23.h:227
co23_sens_status_t::orvs
uint8_t orvs
Definition: co23.h:247
co23_return_value_t
co23_return_value_t
CO2 3 Click return value data.
Definition: co23.h:314
co23_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: co23.h:228
co23_s::slave_address
uint8_t slave_address
Definition: co23.h:195
co23_sens_status_t::ortmp_clr
uint8_t ortmp_clr
Definition: co23.h:249
co23_sens_status_t::ortmp
uint8_t ortmp
Definition: co23.h:246
co23_meas_status_t::drdy
uint8_t drdy
Definition: co23.h:274
co23_init
err_t co23_init(co23_t *ctx, co23_cfg_t *cfg)
CO2 3 initialization function.
CO23_DRV_SEL_UART
@ CO23_DRV_SEL_UART
Definition: co23.h:167
co23_generic_write
err_t co23_generic_write(co23_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
CO2 3 I2C writing function.
co23_meas_cfg_t
CO2 3 Click measurement configuration object.
Definition: co23.h:260
co23_sens_status_t::iccer_clr
uint8_t iccer_clr
Definition: co23.h:251
co23_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: co23.h:231
co23_soft_reset_trigger
err_t co23_soft_reset_trigger(co23_t *ctx, co23_soft_reset_value_t reset_cmd)
CO2 3 soft reset trigger function.
co23_cfg_t::scl
pin_name_t scl
Definition: co23.h:217
CO23_CMD_SOFT_RESET
@ CO23_CMD_SOFT_RESET
Definition: co23.h:300
co23_s::write_f
co23_master_io_t write_f
Definition: co23.h:202
co23_cfg_t::baud_rate
uint32_t baud_rate
Definition: co23.h:225