From eb1064eef4b5f5d8bc504a2ca214a4bdc8b416a4 Mon Sep 17 00:00:00 2001 From: Nishchay-sopho Date: Wed, 14 Oct 2020 13:37:18 +0200 Subject: [PATCH] drivers/scd30: Add reset function to Sensirion scd30 sensor --- drivers/include/scd30.h | 9 +++++++++ drivers/scd30/include/scd30_internal.h | 1 + drivers/scd30/scd30.c | 28 ++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/drivers/include/scd30.h b/drivers/include/scd30.h index 08bc44a9e4..edd762c34c 100644 --- a/drivers/include/scd30.h +++ b/drivers/include/scd30.h @@ -167,6 +167,15 @@ int scd30_start_periodic_measurement(scd30_t *dev, uint16_t *interval, */ int8_t scd30_stop_measurements(const scd30_t *dev); +/** + * @brief Soft reset sensor + * + * @param dev scd30 dev device + * + * @return SCD30_OK if soft reset successful + */ +int8_t scd30_reset(scd30_t *dev); + #ifdef __cplusplus } #endif diff --git a/drivers/scd30/include/scd30_internal.h b/drivers/scd30/include/scd30_internal.h index 2ff9d20719..4ab3f689d0 100644 --- a/drivers/scd30/include/scd30_internal.h +++ b/drivers/scd30/include/scd30_internal.h @@ -44,6 +44,7 @@ extern "C" { 1013.25 mBar */ #define SCD30_READ_WRITE_SLEEP_US (4 * US_PER_MS) +#define SCD30_RESET_SLEEP_US (25 * US_PER_MS) #define SCD30_DATA_RDY_TIMEOUT (1 * US_PER_SEC) #define SCD30_MIN_INTERVAL 2 diff --git a/drivers/scd30/scd30.c b/drivers/scd30/scd30.c index 637b3dfece..c773b7147b 100644 --- a/drivers/scd30/scd30.c +++ b/drivers/scd30/scd30.c @@ -45,11 +45,18 @@ static float _raw_val_to_float(const uint8_t *buffer); int8_t scd30_init(scd30_t *dev, const scd30_params_t *params) { DEBUG("[scd30] init\n"); + int ret = 0; + uint16_t version; + dev->params = *params; - int ret = 0; + /* Soft reset sensor */ + ret = scd30_reset(dev); + if (ret < 0) { + DEBUG("[scd30] Failed to reset sensor in init\n"); + return ret; + } - uint16_t version; ret = scd30_get_param(dev, SCD30_VERSION, &version); DEBUG("[scd30] --- Version 0x%02x%02x\n", (version >> 8), version); if (ret < 0) { @@ -237,6 +244,23 @@ int8_t scd30_stop_measurements(const scd30_t *dev) return SCD30_OK; } +int8_t scd30_reset(scd30_t *dev) +{ + const uint16_t cmd = htons(SCD30_SOFT_RESET); + int ret; + + i2c_acquire(SCD30_I2C); + ret = i2c_write_bytes(SCD30_I2C, SCD30_I2C_ADDRESS, &cmd, 2, 0); + i2c_release(SCD30_I2C); + + if (ret != 0) { + DEBUG("[scd30]: Error resetting sensor.\n"); + return ret; + } + xtimer_usleep(SCD30_RESET_SLEEP_US); + return SCD30_OK; +} + /** * intern function to read data *