初始版本

This commit is contained in:
xiaozhengsheng
2025-08-19 09:49:41 +08:00
parent 10f1ddf1c1
commit 6df0f7d96e
2974 changed files with 1712873 additions and 54 deletions

View File

@@ -0,0 +1,225 @@
/**
* Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "ccs811.h"
#define SWAP_16(arg) ((arg) = (MSB_16(arg) | (LSB_16(arg) << 8)))
ret_code_t ccs811_init(ccs811_instance_t const * p_instance)
{
ASSERT(p_instance != NULL);
if (p_instance->p_sensor_data->p_twi_mngr->p_queue->size < CCS811_MIN_QUEUE_SIZE)
{
return NRF_ERROR_INVALID_LENGTH;
}
static uint8_t const send_msg[] = {
CCS811_REG_APP_START
};
ret_code_t err = nrf_twi_sensor_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
send_msg,
ARRAY_SIZE(send_msg),
true);
err = ccs811_drive_mode_set(p_instance, CCS811_MODE_0, false, false);
if (err != NRF_SUCCESS)
{
return err;
}
err = ccs811_env_set(p_instance, CCS811_DEFAULT_TEMPERATURE, 0, CCS811_DEFAULT_HUMIDITY, 0);
if (err != NRF_SUCCESS)
{
return err;
}
return ccs811_thr_cfg(p_instance,
CCS811_DEFAULT_LOW_THR,
CCS811_DEFAULT_HIGH_THR,
CCS811_DEFAULT_HYSTERESIS);
}
ret_code_t ccs811_drive_mode_set(ccs811_instance_t const * p_instance,
ccs811_drive_mode_t mode,
bool drdy_en,
bool thr_en)
{
ASSERT(p_instance != NULL);
uint8_t reg = 0;
NRF_TWI_SENSOR_REG_SET(reg, CCS811_DRIVE_MODE_MASK, CCS811_DRIVE_MODE_POS, mode);
NRF_TWI_SENSOR_REG_SET(reg, CCS811_DATA_READY_MASK, CCS811_DATA_READY_POS, drdy_en);
NRF_TWI_SENSOR_REG_SET(reg, CCS811_THRESH_MASK, CCS811_THRESH_POS, thr_en);
uint8_t send_msg[] = {
CCS811_REG_MEAS_MODE,
reg
};
return nrf_twi_sensor_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
send_msg,
ARRAY_SIZE(send_msg),
true);
}
ret_code_t ccs811_alg_data_read(ccs811_instance_t const * p_instance,
ccs811_data_callback_t user_cb,
ccs811_alg_data_t * p_alg_data,
ccs811_last_data_byte_t last)
{
ASSERT(p_instance != NULL);
ASSERT(p_alg_data != NULL);
return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
p_instance->sensor_addr,
CCS811_REG_ALG_RESULT_DATA,
(nrf_twi_sensor_reg_cb_t) user_cb,
(uint8_t *) p_alg_data,
last);
}
void ccs811_alg_data_process(ccs811_alg_data_t * p_alg_data)
{
ASSERT(p_alg_data != NULL);
SWAP_16(p_alg_data->eco2);
SWAP_16(p_alg_data->tvoc);
SWAP_16(p_alg_data->raw);
}
ret_code_t ccs811_env_set(ccs811_instance_t const * p_instance,
int8_t temp_value,
uint16_t temp_fraction,
uint8_t hum_percent,
uint16_t hum_fraction)
{
ASSERT(p_instance != NULL);
temp_value += CCS811_TEMPERATURE_OFFSET;
if(temp_value < 0)
{
temp_value = 0;
}
uint16_t env_temp = ( *((uint16_t *) &temp_value) << CCS811_ENV_TEMP_VALUE_POS)
| (temp_fraction & CCS811_ENV_TEMP_FRACTION_MASK);
uint16_t env_hum = ( *((uint16_t *) &hum_percent) << CCS811_ENV_HUM_PERCENT_POS)
| (hum_fraction & CCS811_ENV_HUM_FRACTION_MASK);
uint8_t send_msg[] = {
CCS811_REG_ENV_DATA,
MSB_16(env_temp),
LSB_16(env_temp),
MSB_16(env_hum),
LSB_16(env_hum)
};
return nrf_twi_sensor_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
send_msg,
ARRAY_SIZE(send_msg),
true);
}
ret_code_t ccs811_thr_cfg(ccs811_instance_t const * p_instance,
uint16_t l_to_m,
uint16_t m_to_h,
uint8_t hysteresis)
{
ASSERT(p_instance != NULL);
uint8_t send_msg[] = {
CCS811_REG_THRESHOLDS,
MSB_16(l_to_m),
LSB_16(l_to_m),
MSB_16(m_to_h),
LSB_16(m_to_h),
hysteresis
};
return nrf_twi_sensor_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
send_msg,
ARRAY_SIZE(send_msg),
true);
}
ret_code_t ccs811_baseline_read(ccs811_instance_t const * p_instance,
nrf_twi_sensor_reg_cb_t user_cb,
uint16_t * p_baseline)
{
ASSERT(p_instance != NULL);
ASSERT(p_baseline != NULL);
return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
p_instance->sensor_addr,
CCS811_REG_BASELINE,
user_cb,
(uint8_t *) p_baseline,
2);
}
ret_code_t ccs811_baseline_set(ccs811_instance_t const * p_instance, uint16_t baseline)
{
ASSERT(p_instance != NULL);
uint8_t * p_base = (uint8_t *) &baseline;
uint8_t send_msg[] = {
CCS811_REG_BASELINE,
p_base[0],
p_base[1]
};
return nrf_twi_sensor_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
send_msg,
ARRAY_SIZE(send_msg),
true);
}
ret_code_t ccs811_sw_reset(ccs811_instance_t const * p_instance)
{
ASSERT(p_instance != NULL);
static uint8_t const send_msg[] = {
CCS811_REG_SW_RESET,
CCS811_SW_RESET_BYTE0,
CCS811_SW_RESET_BYTE1,
CCS811_SW_RESET_BYTE2,
CCS811_SW_RESET_BYTE3
};
return nrf_twi_sensor_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
send_msg,
ARRAY_SIZE(send_msg),
true);
}

View File

@@ -0,0 +1,297 @@
/**
* Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef CCS811_H
#define CCS811_H
#include "nrf_twi_sensor.h"
#include "ccs811_internal.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Possible sensor addresses.
*/
#define CCS811_BASE_ADDRESS_LOW 0x5AU
#define CCS811_BASE_ADDRESS_HIGH 0x5BU
// Minimum nrf_twi_sensor message buffer size and nrf_twi_mngr queue length.
#define CCS811_MIN_QUEUE_SIZE 6
// Hardware ID register value
#define CCS811_HARDWARE_ID 0x81
/**
* @brief Sensor driver usage.
*
* Sensor instance has to be defined first in global context using @ref CCS811_INSTANCE_DEF.
* After that it has to be initialized using @ref ccs811_init.
* At this point sensor instance is ready and all other functions can be used.
*
* Configuration functions schedule TWI operation using @ref nrf_twi_sensor module.
* After calling function, setting will be automatically send to sensor when TWI bus is free.
*
* There are designated functions to read status sensor registers e.g. @ref ccs811_status_read
* As parameters they receive function to be called after register is read, and pointer where
* register value should be stored. From that value specific parameters can be extracted
* using @ref NRF_TWI_SENSOR_REG_VAL_GET macro.
* Example:
* bool drdy = NRF_TWI_SENSOR_REG_VAL_GET(status, CCS811_DATA_READY_MASK, CCS811_DATA_READY_POS);
*
* Other functions are self-explanatory or have description on their usage.
*/
/**
* @brief Drive mode setting.
*/
typedef enum
{
CCS811_MODE_0,//!< CCS811_MODE_0 - Idle
CCS811_MODE_1,//!< CCS811_MODE_1 - Constant power, measure every second.
CCS811_MODE_2,//!< CCS811_MODE_2 - Pulse heating mode, measure every 10 seconds.
CCS811_MODE_3,//!< CCS811_MODE_3 - Low power pulse heating mode, measure every 60 seconds.
CCS811_MODE_4 //!< CCS811_MODE_4 - Constant power, measure every 250ms, only raw data.
} ccs811_drive_mode_t;
/**
* @brief Last byte read from algorithm data.
*
* Used with @ref ccs811_alg_data_read function, defines to which byte data should be read.
*/
typedef enum
{
CCS811_LAST_ECO2 = 2,
CCS811_LAST_TVOC = 4,
CCS811_LAST_STATUS,
CCS811_LAST_ERROR_ID,
CCS811_LAST_RAW = 8
} ccs811_last_data_byte_t;
/**
* @brief Structure for holding algorithm data.
*/
typedef struct
{
uint16_t eco2; //!< eC02 value in ppm.
uint16_t tvoc; //!< TVOC value in ppb.
uint8_t status; //!< Status register data.
uint8_t error_id; //!< Error register data.
uint16_t raw; //!< Raw data.
} ccs811_alg_data_t;
/**
* @brief Data callback prototype.
*
* @param[in] result Return code from TWI manager and underlying drivers.
* @param[in] p_data Pointer to sensor data.
*/
typedef void (* ccs811_data_callback_t)(ret_code_t result, ccs811_alg_data_t * p_data);
/**
* @brief Macro that creates sensor instance.
*
* @param[in] _ccs811_inst_name Sensor instance name.
* @param[in] _p_twi_sensor Pointer to common TWI sensor instance.
* @param[in] _sensor_address Sensor base address.
*/
#define CCS811_INSTANCE_DEF(_ccs811_inst_name, _p_twi_sensor, _sensor_address) \
CCS811_INTERNAL_INSTANCE_DEF(_ccs811_inst_name, _p_twi_sensor, _sensor_address)
/**
* @brief Function initializing ccs811 sensor
*
* TWI manager queue length has to be at least CCS811_MIN_QUEUE_SIZE
*
* @param[in] p_instance Pointer to sensor instance created by macro
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t ccs811_init(ccs811_instance_t const * p_instance);
/**
* @brief Function for setting drive mode.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] mode Drive mode.
* @param[in] drdy_en Enable data ready pin.
* @param[in] thr_en Enable threshold.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t ccs811_drive_mode_set(ccs811_instance_t const * p_instance,
ccs811_drive_mode_t mode,
bool drdy_en,
bool thr_en);
/**
* @brief Function for reading sensor data.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] user_cb Function to be called after data read.
* @param[out] p_alg_data Pointer to structure holding sensor algorithm data.
* @param[in] last Last byte to read.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
*/
ret_code_t ccs811_alg_data_read(ccs811_instance_t const * p_instance,
ccs811_data_callback_t user_cb,
ccs811_alg_data_t * p_alg_data,
ccs811_last_data_byte_t last);
/**
* @brief Function for processing algorithm data
*
* @param[in/out] p_alg_data Pointer to read data to be processed.
*/
void ccs811_alg_data_process(ccs811_alg_data_t * p_alg_data);
/**
* @brief Function for setting environment temperature.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] temp_value Temperature value (-25 to 100 degree celsius).
* @param[in] temp_fraction Temperature fraction.
* @param[in] hum_percent Humidity percent.
* @param[in] hum_fraction Humidity fraction.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t ccs811_env_set(ccs811_instance_t const * p_instance,
int8_t temp_value,
uint16_t temp_fraction,
uint8_t hum_percent,
uint16_t hum_fraction);
/**
* @brief Function for threshold configuration
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] l_to_m Low to medium threshold.
* @param[in] m_to_h Medium to high threshold.
* @param[in] hysteresis Hysteresis.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t ccs811_thr_cfg(ccs811_instance_t const * p_instance,
uint16_t l_to_m,
uint16_t m_to_h,
uint8_t hysteresis);
/**
* @brief Function for reading baseline.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] user_cb Function to be called after baseline read.
* @param[out] baseline Baseline value, single uint16_t.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
*/
ret_code_t ccs811_baseline_read(ccs811_instance_t const * p_instance,
nrf_twi_sensor_reg_cb_t user_cb,
uint16_t * p_baseline);
/**
* @brief Function for setting baseline.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] baseline Baseline value.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t ccs811_baseline_set(ccs811_instance_t const * p_instance,
uint16_t baseline);
/**
* @brief Function commencing software reset.
*
* @param[in] p_instance Pointer to sensor instance.
*
* @note To use sensor after reset, it has to be initialized again using @ref ccs811_init function.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t ccs811_sw_reset(ccs811_instance_t const * p_instance);
/**
* @brief Function for reading status register.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] user_cb Function to be called after register is read.
* @param[out] p_reg_val Pointer to register value, single uint8_t.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
*/
__STATIC_INLINE ret_code_t ccs811_status_read(ccs811_instance_t const * p_instance,
nrf_twi_sensor_reg_cb_t user_cb,
uint8_t * p_reg_val);
/**
* @brief Function for reading hardware id register.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] user_cb Function to be called after register is read.
* @param[out] p_reg_val Pointer to register value, single uint8_t.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
*/
__STATIC_INLINE ret_code_t ccs811_hw_id_read(ccs811_instance_t const * p_instance,
nrf_twi_sensor_reg_cb_t user_cb,
uint8_t * p_reg_val);
/**
* @brief Function for reading error id register.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] user_cb Function to be called after register is read.
* @param[out] p_reg_val Pointer to register value, single uint8_t.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
*/
__STATIC_INLINE ret_code_t ccs811_error_read(ccs811_instance_t const * p_instance,
nrf_twi_sensor_reg_cb_t user_cb,
uint8_t * p_reg_val);
#ifdef __cplusplus
}
#endif
#endif // CCS811_H

View File

@@ -0,0 +1,259 @@
/**
* Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef CCS811_INTERNAL_H
#define CCS811_INTERNAL_H
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief CCS811 sensor registers.
*/
#define CCS811_REG_STATUS 0x00
#define CCS811_REG_MEAS_MODE 0x01
#define CCS811_REG_ALG_RESULT_DATA 0x02
#define CCS811_REG_RAW_DATA 0x03
#define CCS811_REG_ENV_DATA 0x05
#define CCS811_REG_NTC 0x06
#define CCS811_REG_THRESHOLDS 0x10
#define CCS811_REG_BASELINE 0x11
#define CCS811_REG_HW_ID 0x20
#define CCS811_REG_HW_VER 0x21
#define CCS811_REG_FW_BOOT_VER 0x23
#define CCS811_REG_FW_APP_VER 0x24
#define CCS811_REG_ERROR_ID 0xE0
#define CCS811_REG_SW_RESET 0xFF
#define CCS811_REG_APP_START 0xF4
/**
* @brief CCS811 Default configuration values.
*/
#define CCS811_DEFAULT_HUMIDITY 50
#define CCS811_DEFAULT_TEMPERATURE 25
#define CCS811_DEFAULT_LOW_THR 1500
#define CCS811_DEFAULT_HIGH_THR 2500
#define CCS811_DEFAULT_HYSTERESIS 50
/**
* @brief CCS811 Environment temperature offset.
*/
#define CCS811_TEMPERATURE_OFFSET 25
/**
* @brief Status register bitmasks.
*/
// Bitmasks for FW_MODE.
#define CCS811_FW_MODE_POS 7
#define CCS811_FW_MODE_MASK (1 << CCS811_FW_MODE_POS)
// Bitmasks for APP_VALID.
#define CCS811_APP_VALID_POS 4
#define CCS811_APP_VALID_MASK (1 << CCS811_APP_VALID_POS)
// Bitmasks for DATA_READY.
#define CCS811_DATA_READY_POS 3
#define CCS811_DATA_READY_MASK (1 << CCS811_DATA_READY_POS)
// Bitmasks for ERROR.
#define CCS811_ERROR_POS 0
#define CCS811_ERROR_MASK (1 << CCS811_ERROR_POS)
/**
* @brief Meas mode register bitmasks.
*/
// Register validity mask.
#define CCS811_MEAS_MODE_VALID_MASK 0x83U
// Bitmasks for DRIVE_MODE.
#define CCS811_DRIVE_MODE_POS 4
#define CCS811_DRIVE_MODE_MASK (7 << CCS811_DRIVE_MODE_POS)
// Bitmasks for INTERRUPT.
#define CCS811_INTERRUPT_POS 3
#define CCS811_INTERRUPT_MASK (1 << CCS811_INTERRUPT_MASK)
// Bitmasks for THRESH.
#define CCS811_THRESH_POS 2
#define CCS811_THRESH_MASK (1 << CCS811_THRESH_POS)
/**
* @brief Algorithm results data bytes.
*/
#define CCS811_ALG_RESULT_BYTE_NUM 8
#define CCS811_ALG_ECO2_H_BYTE 0
#define CCS811_ALG_ECO2_L_BYTE 1
#define CCS811_ALG_TVOC_H_BYTE 2
#define CCS811_ALG_TVOC_L_BYTE 3
#define CCS811_ALG_STATUS_BYTE 4
#define CCS811_ALG_ERROR_BYTE 5
#define CCS811_ALG_RAW_DATA1_BYTE 6
#define CCS811_ALG_RAW_DATA2_BYTE 7
/**
* @brief Environment data register.
*/
#define CCS811_ENV_HUMIDITY_H_BYTE 0
// Humidity percent position.
#define CCS811_ENV_HUM_PERCENT_POS 9
// Bitmasks for humidity fraction.
#define CCS811_ENV_HUM_FRACTION_MASK (0x01FF)
// Temperature value position.
#define CCS811_ENV_TEMP_VALUE_POS 9
// Bitmasks for temperature fraction.
#define CCS811_ENV_TEMP_FRACTION_MASK (0x01FF)
/**
* @brief Error register.
*/
// Bitmasks for HEATER_SUPPLY
#define CCS811_ERROR_HEATER_SUPPLY_POS 5
#define CCS811_ERROR_HEATER_SUPPLY_MASK (1 << CCS811_ERROR_HEATER_SUPPLY_POS)
// Bitmasks for HEATER_FAULT
#define CCS811_ERROR_HEATER_FAULT_POS 4
#define CCS811_ERROR_HEATER_FAULT_MASK (1 << CCS811_ERROR_HEATER_FAULT_POS)
// Bitmasks for MAX_RESISTANCE
#define CCS811_ERROR_MAX_RESISTANCE_POS 3
#define CCS811_ERROR_MAX_RESISTANCE_MASK (1 << CCS811_ERROR_MAX_RESISTANCE_POS)
// Bitmasks for MEASMODE_INVALID
#define CCS811_ERROR_MEAS_MODE_POS 2
#define CCS811_ERROR_MEAS_MODE_MASK (1 CCS811_ERROR_MEAS_MODE_POS)
// Bitmasks for READ_REG
#define CCS811_ERROR_READ_REG_POS 1
#define CCS811_ERROR_READ_REG_MASK (1 << CCS811_ERROR_READ_REG_POS)
// Bitmasks for WRITE_REG
#define CCS811_ERROR_WRITE_REG_POS 0
#define CCS811_ERROR_WRITE_REG_MASK (1 << CCS811_ERROR_WRITE_REG_POS)
/**
* @brief Software reset register.
*/
#define CCS811_SW_RESET_BYTE0 0x11
#define CCS811_SW_RESET_BYTE1 0xE5
#define CCS811_SW_RESET_BYTE2 0x72
#define CCS811_SW_RESET_BYTE3 0x8A
/**
* @brief Structure holding sensor instance
*/
typedef struct
{
nrf_twi_sensor_t * const p_sensor_data;
uint8_t const sensor_addr;
} ccs811_instance_t;
/**
* @brief Macro that creates sensor instance.
*/
#define CCS811_INTERNAL_INSTANCE_DEF(_ccs811_inst_name, _p_twi_sensor, _sensor_address) \
static ccs811_instance_t _ccs811_inst_name = \
{ \
.p_sensor_data = _p_twi_sensor, \
.sensor_addr = _sensor_address, \
}
#ifndef SUPPRESS_INLINE_IMPLEMENTATION
__STATIC_INLINE ret_code_t ccs811_status_read(ccs811_instance_t const * p_instance,
nrf_twi_sensor_reg_cb_t user_cb,
uint8_t * p_reg_val)
{
ASSERT(p_instance != NULL);
ASSERT(p_reg_val != NULL);
return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
p_instance->sensor_addr,
CCS811_REG_STATUS,
user_cb,
p_reg_val,
1);
}
__STATIC_INLINE ret_code_t ccs811_hw_id_read(ccs811_instance_t const * p_instance,
nrf_twi_sensor_reg_cb_t user_cb,
uint8_t * p_reg_val)
{
ASSERT(p_instance != NULL);
ASSERT(p_reg_val != NULL);
return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
p_instance->sensor_addr,
CCS811_REG_HW_ID,
user_cb,
p_reg_val,
1);
}
__STATIC_INLINE ret_code_t ccs811_error_read(ccs811_instance_t const * p_instance,
nrf_twi_sensor_reg_cb_t user_cb,
uint8_t * p_reg_val)
{
ASSERT(p_instance != NULL);
ASSERT(p_reg_val != NULL);
return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
p_instance->sensor_addr,
CCS811_REG_ERROR_ID,
user_cb,
p_reg_val,
1);
}
#endif //SUPPRESS_INLINE_IMPLEMENTATION
#ifdef __cplusplus
}
#endif
#endif // CCS811_INTERNAL_H