sensor-xensiv-pasco2
XENSIV™ PAS CO2 sensor ModusToolbox™ interface

General Description

Basic set of APIs for interacting with the XENSIV™ PAS CO2 sensor.

This provides basic initialization and access to the CO2 ppm value. It also provides access to the base XENSIV™ PAS CO2 driver for full control.

Note
XENSIV™ PAS CO2 sensor support requires delays. If the RTOS_AWARE component is set or CY_RTOS_AWARE is defined, the HAL driver will defer to the RTOS for delays. Because of this, make sure that the RTOS scheduler has started before calling any functions.

Code snippets

Snippet 1: Simple initialization with I2C

The following snippet initializes an I2C instance and the XENSIV™ PAS CO2 sensor, and then reads from the XENSIV™ PAS CO2 sensor.

/* Initialize I2C */
cyhal_i2c_cfg_t i2c_master_config = {CYHAL_I2C_MODE_MASTER,
0,
I2C_MASTER_FREQUENCY};
cyhal_i2c_t cyhal_i2c;
result = cyhal_i2c_init(&cyhal_i2c, PIN_XENSIV_PASCO2_I2C_SDA, PIN_XENSIV_PASCO2_I2C_SCL, NULL);
CY_ASSERT(result == CY_RSLT_SUCCESS);
result = cyhal_i2c_configure(&cyhal_i2c, &i2c_master_config);
CY_ASSERT(result == CY_RSLT_SUCCESS);
/* Wait until the sensor is ready */
cyhal_system_delay_ms(WAIT_SENSOR_RDY_MS);
/* Initialize the PAS CO2 sensor with the default parameter values */
xensiv_pasco2_mtb_t xensiv_pasco2;
result = xensiv_pasco2_mtb_init_i2c(&xensiv_pasco2, &cyhal_i2c);
if (result != CY_RSLT_SUCCESS)
{
printf("PAS CO2 device initialization error");
CY_ASSERT(0);
}
/* Poll PAS CO2 sensor data ready */
uint16_t ppm;
do
{
cyhal_system_delay_ms(10);
result = xensiv_pasco2_mtb_read(&xensiv_pasco2, DEFAULT_PRESSURE_REF_HPA, &ppm);
} while (result != CY_RSLT_SUCCESS);
printf("CO2 %d ppm.\n", ppm);

Snippet 2: XENSIV™ PAS CO2 sensor interrupt configuration

The following snippet demonstrates how to configure a XENSIV™ PAS CO2 sensor interrupt.

{
};
{
.cb = xensiv_pasco2_int_cb,
.arg = NULL;
};
result = xensiv_pasco2_mtb_interrupt_init_ex(&xensiv_pasco2,
int_config,
0,
PIN_XENSIV_PASCO2_INT,
CYHAL_ISR_PRIORITY_DEFAULT,
&int_cb);
if (result != CY_RSLT_SUCCESS)
{
printf("PAS CO2 interrupt initialization error");
CY_ASSERT(0);
}
for (;;)
{
if (xensiv_pasco2_drdy)
{
printf("XENSIV PAS CO2 interrupt detected\r\n");
xensiv_pasco2_drdy = false;
result = xensiv_pasco2_mtb_read(&xensiv_pasco2, DEFAULT_PRESSURE_REF_HPA, &ppm);
if (result != CY_RSLT_SUCCESS)
{
printf("XENSIV PAS CO2 read failed\r\n");
}
else
{
printf("CO2 %d ppm.\n", ppm);
}
}
cyhal_system_delay_ms(10);
}

Macros

#define CY_RSLT_MODULE_BOARD_HARDWARE_XENSIV_PASCO2   0x01CA
 
#define XENSIV_PASCO2_RSLT_ERR_COMM    (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_BOARD_HARDWARE_XENSIV_PASCO2, XENSIV_PASCO2_ERR_COMM))
 Result code indicating a communication error.
 
#define XENSIV_PASCO2_RSLT_ERR_WRITE_TOO_LARGE    (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_BOARD_HARDWARE_XENSIV_PASCO2, XENSIV_PASCO2_ERR_WRITE_TOO_LARGE))
 Result code indicating that an unexpectedly large I2C write was requested which is not supported.
 
#define XENSIV_PASCO2_RSLT_ERR_NOT_READY    (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_BOARD_HARDWARE_XENSIV_PASCO2, XENSIV_PASCO2_ERR_NOT_READY))
 Result code indicating that the sensor is not yet ready after reset.
 
#define XENSIV_PASCO2_RSLT_ICCERR    (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_BOARD_HARDWARE_XENSIV_PASCO2, XENSIV_PASCO2_ICCERR))
 Result code indicating whether an invalid command has been received by the serial communication interface.
 
#define XENSIV_PASCO2_RSLT_ORVS    (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_BOARD_HARDWARE_XENSIV_PASCO2, XENSIV_PASCO2_ORVS))
 Result code indicating whether a condition where VDD12V has been outside the specified valid range has been detected.
 
#define XENSIV_PASCO2_RSLT_ORTMP    (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_BOARD_HARDWARE_XENSIV_PASCO2, XENSIV_PASCO2_ORTMP))
 Result code indicating whether a condition where the temperature has been outside the specified valid range has been detected.
 
#define XENSIV_PASCO2_RSLT_READ_NRDY    (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_BOARD_HARDWARE_XENSIV_PASCO2, XENSIV_PASCO2_READ_NRDY))
 Result code indicating that a new CO2 value is not yet ready.
 

Functions

cy_rslt_t xensiv_pasco2_mtb_init_i2c (xensiv_pasco2_t *dev, cyhal_i2c_t *i2c)
 Initializes the XENSIV™ PAS CO2 sensor and configures it to use the specified I2C peripheral. More...
 
cy_rslt_t xensiv_pasco2_mtb_init_uart (xensiv_pasco2_t *dev, cyhal_uart_t *uart)
 Initializes the XENSIV™ PAS CO2 sensor, and configures it to use the specified UART peripheral. More...
 
cy_rslt_t xensiv_pasco2_mtb_interrupt_init (const xensiv_pasco2_t *dev, const xensiv_pasco2_interrupt_config_t int_config, uint16_t alarm_threshold, cyhal_gpio_t pin, uint8_t intr_priority, cyhal_gpio_event_callback_t callback, void *callback_arg)
 Configures a GPIO pin as an interrupt for the PAS CO2 sensor. More...
 
cy_rslt_t xensiv_pasco2_mtb_interrupt_init_ex (xensiv_pasco2_t *dev, const xensiv_pasco2_interrupt_config_t int_config, uint16_t alarm_threshold, cyhal_gpio_t pin, uint8_t intr_priority, xensiv_pasco2_mtb_interrupt_cb_t *interrupt_cb)
 Configures a GPIO pin as an interrupt for the PAS CO2 sensor. More...
 
cy_rslt_t xensiv_pasco2_mtb_read (const xensiv_pasco2_t *dev, uint16_t press_ref, uint16_t *co2_ppm_val)
 Reads the CO2 value value if available. More...
 

Function Documentation

◆ xensiv_pasco2_mtb_init_i2c()

cy_rslt_t xensiv_pasco2_mtb_init_i2c ( xensiv_pasco2_t dev,
cyhal_i2c_t *  i2c 
)

Initializes the XENSIV™ PAS CO2 sensor and configures it to use the specified I2C peripheral.

Parameters
[in,out]objPointer to the ModusToolbox&trade PAS CO2 object. The caller must allocate the memory for this object but the init function will initialize its contents
[in]i2cPointer to an initialized I2C object
Returns
CY_RSLT_SUCCESS if the initialization was successful; an error indicating what went wrong otherwise

◆ xensiv_pasco2_mtb_init_uart()

cy_rslt_t xensiv_pasco2_mtb_init_uart ( xensiv_pasco2_t dev,
cyhal_uart_t *  uart 
)

Initializes the XENSIV™ PAS CO2 sensor, and configures it to use the specified UART peripheral.

Parameters
[in,out]objPointer to the ModusToolbox&trade PAS CO2 object. The caller must allocate the memory for this object but the init function will initialize its contents
[in]uartPointer to an initialized UART object
Returns
CY_RSLT_SUCCESS if the initialization was successful; an error indicating what went wrong otherwise

◆ xensiv_pasco2_mtb_interrupt_init()

cy_rslt_t xensiv_pasco2_mtb_interrupt_init ( const xensiv_pasco2_t dev,
const xensiv_pasco2_interrupt_config_t  int_config,
uint16_t  alarm_threshold,
cyhal_gpio_t  pin,
uint8_t  intr_priority,
cyhal_gpio_event_callback_t  callback,
void *  callback_arg 
)

Configures a GPIO pin as an interrupt for the PAS CO2 sensor.

This initializes and configures the pin as an interrupt, and calls the PAS CO2 interrupt configuration API with the application-supplied settings structure

Note
Should be called only after xensiv_pasco2_mtb_init_i2c or xensiv_pasco2_mtb_init_uart.
Deprecated:
Use xensiv_pasco2_mtb_interrupt_init_ex
Parameters
[in]devPointer to the PAS CO2 sensor device
[in]int_configNew sensor device interrupt configuration to apply
[in]alarm_thresholdNew alarm threshold value to apply
[in]pinPin connected to the INT pin of the sensor
[in]intr_priorityPriority for NVIC interrupt events
[in]callbackFunction to call when the specified event happens. Pass NULL to unregister the handler
[in]callback_argGeneric argument that will be provided to the callback when called; can be NULL
Returns
CY_RSLT_SUCCESS if interrupt was successfully enabled; an error occurred while initializing the pin otherwise

◆ xensiv_pasco2_mtb_interrupt_init_ex()

cy_rslt_t xensiv_pasco2_mtb_interrupt_init_ex ( xensiv_pasco2_t dev,
const xensiv_pasco2_interrupt_config_t  int_config,
uint16_t  alarm_threshold,
cyhal_gpio_t  pin,
uint8_t  intr_priority,
xensiv_pasco2_mtb_interrupt_cb_t interrupt_cb 
)

Configures a GPIO pin as an interrupt for the PAS CO2 sensor.

This initializes and configures the pin as an interrupt, and calls the PAS CO2 interrupt configuration API with the application-supplied settings structure

Note
Should be called only after xensiv_pasco2_mtb_init_i2c or xensiv_pasco2_mtb_init_uart.
Parameters
[in]objPointer to the ModusToolbox&trade PAS CO2 sensor object
[in]int_configNew sensor device interrupt configuration to apply
[in]alarm_thresholdNew alarm threshold value to apply
[in]pinPin connected to the INT pin of the sensor
[in]intr_priorityPriority for NVIC interrupt events
[in]interrupt_cbInterrupt callback
Note
Instances of this object are expected to persist for the length of time the callback is registered. As such, care must be given if declaring it on the stack to ensure the frame does not go away while the callback is still registered.
Returns
CY_RSLT_SUCCESS if interrupt was successfully enabled; an error occurred while initializing the pin otherwise

◆ xensiv_pasco2_mtb_read()

cy_rslt_t xensiv_pasco2_mtb_read ( const xensiv_pasco2_t dev,
uint16_t  press_ref,
uint16_t *  co2_ppm_val 
)

Reads the CO2 value value if available.

This checks whether a new CO2 value is available, in which case it returns it and sets the new pressure reference value for the next measurement

Parameters
[in]objPointer to the ModusToolbox&trade PAS CO2 object
[in]press_refNew pressure reference value to apply
[out]co2_ppm_valPointer to populate with the CO2 ppm value
Returns
CY_RSLT_SUCCESS if PPM value was successfully read. XENSIV_PASCO2_RSLT_READ_NRDY if the measurement value is not ready yet; an error indicating what went wrong otherwise
XENSIV_PASCO2_INTERRUPT_FUNCTION_DRDY
@ XENSIV_PASCO2_INTERRUPT_FUNCTION_DRDY
Pin INT is configured as the data ready notification pin.
Definition: xensiv_pasco2.h:128
xensiv_pasco2_mtb_init_i2c
cy_rslt_t xensiv_pasco2_mtb_init_i2c(xensiv_pasco2_t *dev, cyhal_i2c_t *i2c)
Initializes the XENSIV™ PAS CO2 sensor and configures it to use the specified I2C peripheral.
Definition: xensiv_pasco2_mtb.c:39
xensiv_pasco2_mtb_interrupt_init_ex
cy_rslt_t xensiv_pasco2_mtb_interrupt_init_ex(xensiv_pasco2_t *dev, const xensiv_pasco2_interrupt_config_t int_config, uint16_t alarm_threshold, cyhal_gpio_t pin, uint8_t intr_priority, xensiv_pasco2_mtb_interrupt_cb_t *interrupt_cb)
Configures a GPIO pin as an interrupt for the PAS CO2 sensor.
Definition: xensiv_pasco2_mtb.c:110
xensiv_pasco2_interrupt_config_t
Structure of the sensor's interrupt configuration register (INT_CFG)
Definition: xensiv_pasco2.h:184
xensiv_pasco2_interrupt_config_t::b
struct xensiv_pasco2_interrupt_config_t::@3 b
xensiv_pasco2_mtb_read
cy_rslt_t xensiv_pasco2_mtb_read(const xensiv_pasco2_t *dev, uint16_t press_ref, uint16_t *co2_ppm_val)
Reads the CO2 value value if available.
Definition: xensiv_pasco2_mtb.c:146
xensiv_pasco2_mtb_interrupt_cb_t
Structure containing callback data for handling interrupts from sensor.
Definition: xensiv_pasco2_mtb.h:101
XENSIV_PASCO2_INTERRUPT_TYPE_LOW_ACTIVE
@ XENSIV_PASCO2_INTERRUPT_TYPE_LOW_ACTIVE
Pin INT is configured as push-pull and is active LOW.
Definition: xensiv_pasco2.h:119
xensiv_pasco2_mtb_interrupt_cb_t::cb
cyhal_gpio_event_callback_t cb
The callback function to run.
Definition: xensiv_pasco2_mtb.h:102