diff --git a/boards/thingy91_nrf9160_ns.overlay b/boards/thingy91_nrf9160_ns.overlay index 130f2aa..e1b15bb 100644 --- a/boards/thingy91_nrf9160_ns.overlay +++ b/boards/thingy91_nrf9160_ns.overlay @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Nordic Semiconductor ASA + * Copyright (c) 2023 Nordic Semiconductor ASA * * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause */ @@ -9,6 +9,7 @@ temp-sensor = &bme680; humidity-sensor = &bme680; pressure-sensor = &bme680; + iaq-sensor = &bme680; accelerometer = &adxl362; impact-sensor = &adxl372; }; diff --git a/doc/sensor_module.rst b/doc/sensor_module.rst index 09ab14c..138aa65 100644 --- a/doc/sensor_module.rst +++ b/doc/sensor_module.rst @@ -104,15 +104,16 @@ Bosch Software Environmental Cluster (BSEC) library The sensor module supports integration with the BSEC signal processing library using the external sensors, internal convenience API. If enabled, the BSEC library is used instead of the BME680 Zephyr driver to provide sensor readings from the BME680 for temperature, humidity, and atmospheric pressure. In addition, the BSEC driver provides an additional sensor reading, indoor air quality (IAQ), which is a metric given in between 0-500 range, which estimates the air quality of the environment. +In the beginning, the IAQ shows 50 (good air), but it is automatically calibrated over time. -As the BSEC library requires a separate license, it is not a default part of |NCS|, but can be downloaded externally and imported into the |NCS| source tree. +.. note:: + Using the BSEC library requires accepting a separate license agreement. + For details, see :ref:`bme68x_iaq`. Perform the following steps to enable BSEC: -1. Download the BSEC library, using the `Bosch BSEC`_ link. -#. Extract and store the folder containing the library contents in the path specified by :ref:`CONFIG_EXTERNAL_SENSORS_BME680_BSEC_PATH ` option or update the path configuration to reference the library location. -#. Disable the Zephyr BME680 driver by setting :kconfig:option:`CONFIG_BME680` to false. -#. Enable the external sensors API BSEC integration layer by enabling :ref:`CONFIG_EXTERNAL_SENSORS_BME680_BSEC ` option. +1. To disable the Zephyr BME680 driver, set the :kconfig:option:`CONFIG_BME680` Kconfig option to false. +#. To enable the external sensors API BSEC integration layer, use the :ref:`CONFIG_BME68X_IAQ ` Kconfig option. Air quality readings are provided with the :c:enum:`SENSOR_EVT_ENVIRONMENTAL_DATA_READY` event. @@ -136,16 +137,11 @@ CONFIG_EXTERNAL_SENSORS_IMPACT_DETECTION External sensors API BSEC configurations ======================================== -.. _CONFIG_EXTERNAL_SENSORS_BME680_BSEC: +.. _CONFIG_BME68X_IAQ: -CONFIG_EXTERNAL_SENSORS_BME680_BSEC +CONFIG_BME68X_IAQ This option configures the Bosch BSEC library for the BME680. -.. _CONFIG_EXTERNAL_SENSORS_BME680_BSEC_PATH: - -CONFIG_EXTERNAL_SENSORS_BME680_BSEC_PATH - This option sets the path for the Bosch BSEC library folder. - .. _CONFIG_EXTERNAL_SENSORS_BSEC_SAMPLE_MODE_ULTRA_LOW_POWER: CONFIG_EXTERNAL_SENSORS_BSEC_SAMPLE_MODE_ULTRA_LOW_POWER diff --git a/src/ext_sensors/CMakeLists.txt b/src/ext_sensors/CMakeLists.txt index 141e1dd..cd66714 100644 --- a/src/ext_sensors/CMakeLists.txt +++ b/src/ext_sensors/CMakeLists.txt @@ -1,38 +1,8 @@ # -# Copyright (c) 2021 Nordic Semiconductor ASA +# Copyright (c) 2023 Nordic Semiconductor ASA # # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause # target_include_directories(app PRIVATE .) target_sources_ifdef(CONFIG_EXTERNAL_SENSORS app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ext_sensors.c) - -if (CONFIG_EXTERNAL_SENSORS_BME680_BSEC) - target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ext_sensors_bsec.c) - set(bsec_dir ${CONFIG_EXTERNAL_SENSORS_BME680_BSEC_PATH}) - if (CONFIG_FP_SOFTABI) - set(BSEC_LIB_DIR ${bsec_dir}/algo/normal_version/bin/gcc/Cortex_M33) - endif() - if (CONFIG_FP_HARDABI) - set(BSEC_LIB_DIR ${bsec_dir}/algo/normal_version/bin/gcc/Cortex_M33F) - endif() - - if(NOT EXISTS "${BSEC_LIB_DIR}/libalgobsec.a") - assert(0 "Could not find BSEC library. Minimum version is 1.4.8.0_Generic_Release") - endif() - - target_include_directories(app PRIVATE ${bsec_dir}/examples/bsec_iot_example) - target_include_directories(app PRIVATE ${BSEC_LIB_DIR}) - - target_sources(app PRIVATE ${bsec_dir}/examples/bsec_iot_example/bsec_integration.c) - target_sources(app PRIVATE ${bsec_dir}/examples/bsec_iot_example/bme680.c) - - add_library(bsec_lib STATIC IMPORTED GLOBAL) - add_dependencies(bsec_lib math_lib bsec_target) - - set_target_properties(bsec_lib PROPERTIES IMPORTED_LOCATION "${BSEC_LIB_DIR}/libalgobsec.a") - set_target_properties(bsec_lib PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${BSEC_LIB_DIR}) - - target_link_libraries(bsec_lib INTERFACE -L${LIBC_LIBRARY_DIR}) - target_link_libraries(app PUBLIC bsec_lib) -endif() diff --git a/src/ext_sensors/Kconfig b/src/ext_sensors/Kconfig index bd78bbf..2a91033 100644 --- a/src/ext_sensors/Kconfig +++ b/src/ext_sensors/Kconfig @@ -1,5 +1,5 @@ # -# Copyright (c) 2021 Nordic Semiconductor +# Copyright (c) 2023 Nordic Semiconductor # # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause # @@ -28,66 +28,6 @@ endchoice endif # EXTERNAL_SENSORS_IMPACT_DETECTION -config EXTERNAL_SENSORS_BME680_BSEC - bool "Use Bosch BME680 library" - depends on !BME680 - help - Enable the use of Bosch BSEC library. The library itself is not a part of - NCS and must be downloaded from Bosch Sensortec and placed in the path referenced to by - the CONFIG_EXTERNAL_SENSORS_BME680_BSEC_PATH. - This configuration depends on the BME680 zephyr driver being disabled. - -if EXTERNAL_SENSORS_BME680_BSEC - -config EXTERNAL_SENSORS_BSEC_THREAD_STACK_SIZE - int "BSEC thread stack size" - default 4096 - -choice EXTERNAL_SENSORS_BSEC_SAMPLE_MODE - prompt "Bosch BSEC sample mode" - default EXTERNAL_SENSORS_BSEC_SAMPLE_MODE_LOW_POWER - help - Configuration that sets how often sensor data is sampled from the BSEC library. - Each mode corresponds an internal preset that decides how often data is sampled from the - BME680. - -config EXTERNAL_SENSORS_BSEC_SAMPLE_MODE_ULTRA_LOW_POWER - bool "BSEC low ultra power mode" - help - Sample data from BSEC every 0.0033333 Hz (300 second interval). - -config EXTERNAL_SENSORS_BSEC_SAMPLE_MODE_LOW_POWER - bool "BSEC low power mode" - help - Sample data from BSEC every 0.33333 Hz (3 second interval). - -config EXTERNAL_SENSORS_BSEC_SAMPLE_MODE_CONTINUOUS - bool "BSEC continuous mode" - help - Sample data from BSEC every 1 Hz (1 second interval). - This is a particularly power-hungry sample mode that should only be considered for - testing purposes. - -endchoice # EXTERNAL_SENSORS_BME680_BSEC_MODE - -config EXTERNAL_SENSORS_BSEC_TEMPERATURE_OFFSET - int "BSEC temperature offset in degree celsius * 100" - default 120 if BOARD_THINGY91_NRF9160_NS - default 100 - help - BSEC temperature offset. To account for external heat sources on the board. - The actual value is divided by 100. This is due to the Kconfig parser - not supporting floating point types. - The default value 120 is translated to 1.2 degrees celsius offset. - -config EXTERNAL_SENSORS_BME680_BSEC_PATH - string "Path to Bosch BSEC library folder" - default "$(ZEPHYR_NRF_MODULE_DIR)/ext/BSEC_1.4.8.0_Generic_Release_updated_v3" - help - Path to the folder where the Bosch BSEC library is placed. - -endif # EXTERNAL_SENSORS_BME680_BSEC - module = EXTERNAL_SENSORS module-str = External sensors source "subsys/logging/Kconfig.template.log_config" diff --git a/src/ext_sensors/ext_sensors.c b/src/ext_sensors/ext_sensors.c index 0aa9b76..4d4bb29 100644 --- a/src/ext_sensors/ext_sensors.c +++ b/src/ext_sensors/ext_sensors.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Nordic Semiconductor ASA + * Copyright (c) 2023 Nordic Semiconductor ASA * * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause */ @@ -11,8 +11,8 @@ #include #include -#if defined(CONFIG_EXTERNAL_SENSORS_BME680_BSEC) -#include "ext_sensors_bsec.h" +#if defined(CONFIG_BME68X_IAQ) +#include #endif #include "ext_sensors.h" @@ -75,6 +75,14 @@ static struct env_sensor press_sensor = { .dev = DEVICE_DT_GET(DT_ALIAS(pressure_sensor)), }; +#if defined(CONFIG_BME68X_IAQ) +static struct env_sensor iaq_sensor = { + .channel = SENSOR_CHAN_IAQ, + .dev = DEVICE_DT_GET(DT_ALIAS(iaq_sensor)), + +}; +#endif + /** Sensor struct for the low-power accelerometer */ static struct env_sensor accel_sensor_lp = { .channel = SENSOR_CHAN_ACCEL_XYZ, @@ -195,15 +203,15 @@ int ext_sensors_init(ext_sensor_handler_t handler) evt_handler = handler; -#if defined(CONFIG_EXTERNAL_SENSORS_BME680_BSEC) - int err = ext_sensors_bsec_init(); - - if (err) { - LOG_ERR("ext_sensors_bsec_init, error: %d", err); - evt.type = EXT_SENSOR_EVT_BME680_BSEC_ERROR; +#if defined(CONFIG_BME68X_IAQ) + if (!device_is_ready(iaq_sensor.dev)) { + LOG_ERR("Air quality sensor device is not ready"); + evt.type = EXT_SENSOR_EVT_AIR_QUALITY_ERROR; evt_handler(&evt); } -#else +#endif /* if defined(CONFIG_BME68X_IAQ) */ + + if (!device_is_ready(temp_sensor.dev)) { LOG_ERR("Temperature sensor device is not ready"); evt.type = EXT_SENSOR_EVT_TEMPERATURE_ERROR; @@ -221,7 +229,6 @@ int ext_sensors_init(ext_sensor_handler_t handler) evt.type = EXT_SENSOR_EVT_PRESSURE_ERROR; evt_handler(&evt); } -#endif /* if defined(CONFIG_EXTERNAL_SENSORS_BME680_BSEC) */ if (!device_is_ready(accel_sensor_lp.dev)) { LOG_ERR("Low-power accelerometer device is not ready"); @@ -253,10 +260,6 @@ int ext_sensors_temperature_get(double *ext_temp) struct sensor_value data = {0}; struct ext_sensor_evt evt = {0}; -#if defined(CONFIG_EXTERNAL_SENSORS_BME680_BSEC) - return ext_sensors_bsec_temperature_get(ext_temp); -#endif - err = sensor_sample_fetch_chan(temp_sensor.dev, SENSOR_CHAN_ALL); if (err) { LOG_ERR("Failed to fetch data from %s, error: %d", @@ -288,10 +291,6 @@ int ext_sensors_humidity_get(double *ext_hum) struct sensor_value data = {0}; struct ext_sensor_evt evt = {0}; -#if defined(CONFIG_EXTERNAL_SENSORS_BME680_BSEC) - return ext_sensors_bsec_humidity_get(ext_hum); -#endif - err = sensor_sample_fetch_chan(humid_sensor.dev, SENSOR_CHAN_ALL); if (err) { LOG_ERR("Failed to fetch data from %s, error: %d", @@ -323,10 +322,6 @@ int ext_sensors_pressure_get(double *ext_press) struct sensor_value data = {0}; struct ext_sensor_evt evt = {0}; -#if defined(CONFIG_EXTERNAL_SENSORS_BME680_BSEC) - return ext_sensors_bsec_pressure_get(ext_press); -#endif - err = sensor_sample_fetch_chan(press_sensor.dev, SENSOR_CHAN_ALL); if (err) { LOG_ERR("Failed to fetch data from %s, error: %d", @@ -346,7 +341,7 @@ int ext_sensors_pressure_get(double *ext_press) } k_spinlock_key_t key = k_spin_lock(&(press_sensor.lock)); - *ext_press = sensor_value_to_double(&data); + *ext_press = sensor_value_to_double(&data) / 1000.0f; k_spin_unlock(&(press_sensor.lock), key); return 0; @@ -354,10 +349,38 @@ int ext_sensors_pressure_get(double *ext_press) int ext_sensors_air_quality_get(uint16_t *ext_bsec_air_quality) { -#if defined(CONFIG_EXTERNAL_SENSORS_BME680_BSEC) - return ext_sensors_bsec_air_quality_get(ext_bsec_air_quality); -#endif +#if defined(CONFIG_BME68X_IAQ) + + int err; + struct sensor_value data = {0}; + struct ext_sensor_evt evt = {0}; + + err = sensor_sample_fetch_chan(iaq_sensor.dev, SENSOR_CHAN_ALL); + if (err) { + LOG_ERR("Failed to fetch data from %s, error: %d", + iaq_sensor.dev->name, err); + evt.type = EXT_SENSOR_EVT_AIR_QUALITY_ERROR; + evt_handler(&evt); + return -ENODATA; + } + + err = sensor_channel_get(iaq_sensor.dev, iaq_sensor.channel, &data); + if (err) { + LOG_ERR("Failed to fetch data from %s, error: %d", + iaq_sensor.dev->name, err); + evt.type = EXT_SENSOR_EVT_AIR_QUALITY_ERROR; + evt_handler(&evt); + return -ENODATA; + } + + k_spinlock_key_t key = k_spin_lock(&(iaq_sensor.lock)); + *ext_bsec_air_quality = sensor_value_to_double(&data); + k_spin_unlock(&(iaq_sensor.lock), key); + + return 0; +#else return -ENOTSUP; +#endif /* defined(CONFIG_BME68X_IAQ) */ } int ext_sensors_accelerometer_threshold_set(double threshold, bool upper) diff --git a/src/ext_sensors/ext_sensors.h b/src/ext_sensors/ext_sensors.h index 0093267..fcf0a88 100644 --- a/src/ext_sensors/ext_sensors.h +++ b/src/ext_sensors/ext_sensors.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Nordic Semiconductor ASA + * Copyright (c) 2023 Nordic Semiconductor ASA * * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause */ @@ -42,8 +42,8 @@ enum ext_sensor_evt_type { EXT_SENSOR_EVT_HUMIDITY_ERROR, /** Event propagated when an error has occurred with the pressure sensor. */ EXT_SENSOR_EVT_PRESSURE_ERROR, - /** Event propagated when an error has occurred with the Bosch BSEC library. */ - EXT_SENSOR_EVT_BME680_BSEC_ERROR + /** Event propagated when an error has occurred with the virtual air quality sensor. */ + EXT_SENSOR_EVT_AIR_QUALITY_ERROR }; /** @brief Structure containing external sensor data. */ diff --git a/src/ext_sensors/ext_sensors_bsec.c b/src/ext_sensors/ext_sensors_bsec.c deleted file mode 100644 index 8631164..0000000 --- a/src/ext_sensors/ext_sensors_bsec.c +++ /dev/null @@ -1,307 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause - */ - -#include -#include -#include -#include -#include -#include -#include - -#include "bsec_integration.h" -#include "ext_sensors_bsec.h" - -#include -LOG_MODULE_REGISTER(ext_sensors_bsec, CONFIG_EXTERNAL_SENSORS_LOG_LEVEL); - -/* Sample rate for the BSEC library - * - * BSEC_SAMPLE_RATE_ULP = 0.0033333 Hz = 300 second interval - * BSEC_SAMPLE_RATE_LP = 0.33333 Hz = 3 second interval - * BSEC_SAMPLE_RATE_HP = 1 Hz = 1 second interval - */ -#if defined(CONFIG_EXTERNAL_SENSORS_BSEC_SAMPLE_MODE_ULTRA_LOW_POWER) -#define BSEC_SAMPLE_RATE BSEC_SAMPLE_RATE_ULP -#elif defined(CONFIG_EXTERNAL_SENSORS_BSEC_SAMPLE_MODE_LOW_POWER) -#define BSEC_SAMPLE_RATE BSEC_SAMPLE_RATE_LP -#elif defined(CONFIG_EXTERNAL_SENSORS_BSEC_SAMPLE_MODE_CONTINUOUS) -#define BSEC_SAMPLE_RATE BSEC_SAMPLE_RATE_HP -#endif - -/* Temperature offset due to external heat sources. */ -float temp_offset = (CONFIG_EXTERNAL_SENSORS_BSEC_TEMPERATURE_OFFSET / (float)100); - -/* @breif Interval for saving BSEC state to flash - * - * Interval = BSEC_SAVE_INTERVAL * 1/BSEC_SAMPLE_RATE - * Example: 600 * 1/0.33333 Hz = 1800 seconds = 0.5 hour - */ -#define BSEC_SAVE_INTERVAL 600 - -/* Definitions used to store and retrieve BSEC state from the settings API */ -#define SETTINGS_NAME_BSEC "bsec" -#define SETTINGS_KEY_STATE "state" -#define SETTINGS_BSEC_STATE SETTINGS_NAME_BSEC "/" SETTINGS_KEY_STATE - -/* Stack size of internal BSEC thread. */ -#define BSEC_STACK_SIZE CONFIG_EXTERNAL_SENSORS_BSEC_THREAD_STACK_SIZE -K_THREAD_STACK_DEFINE(thread_stack, BSEC_STACK_SIZE); - -struct config { - /* Variable used to reference the I2C device where BME680 is connected to. */ - const struct device *i2c_master; -}; - -/* Structure used to maintain internal variables used by the library. */ -struct ctx { - /* Spinlock used to safely read out sensor readings from the BSEC library driver. */ - struct k_spinlock sensor_read_lock; - - /* Intermediate libraries used to hold the latest sampled sensor readings. */ - double temperature_latest; - double humidity_latest; - double pressure_latest; - uint16_t air_quality_latest; - - /* Internal BSEC thread metadata value. */ - struct k_thread thread; - - /* Buffer used to maintain the BSEC library state. */ - uint8_t state_buffer[BSEC_MAX_STATE_BLOB_SIZE]; - int32_t state_buffer_len; - -}; - -static const struct config config = { - .i2c_master = DEVICE_DT_GET(DT_BUS(DT_NODELABEL(bme680))), -}; - -static struct ctx ctx; - -/* Forward declarations */ -static int settings_set(const char *key, size_t len_rd, settings_read_cb read_cb, void *cb_arg); -SETTINGS_STATIC_HANDLER_DEFINE(bsec, SETTINGS_NAME_BSEC, NULL, settings_set, NULL, NULL); - -/* Private API */ - -static int settings_set(const char *key, size_t len_rd, settings_read_cb read_cb, void *cb_arg) -{ - if (!key) { - LOG_ERR("Invalid key"); - return -EINVAL; - } - - LOG_DBG("Settings key: %s, size: %d", key, len_rd); - - if (!strncmp(key, SETTINGS_KEY_STATE, strlen(SETTINGS_KEY_STATE))) { - if (len_rd > sizeof(ctx.state_buffer)) { - LOG_ERR("Setting too big: %d", len_rd); - return -EMSGSIZE; - } - - ctx.state_buffer_len = read_cb(cb_arg, ctx.state_buffer, len_rd); - if (ctx.state_buffer_len > 0) { - return 0; - } - - LOG_ERR("No settings data read"); - return -ENODATA; - } - - return -ENOTSUP; -} - -static int enable_settings(void) -{ - int err; - - err = settings_subsys_init(); - if (err) { - LOG_ERR("settings_subsys_init, error: %d", err); - return err; - } - - err = settings_load_subtree(settings_handler_bsec.name); - if (err) { - LOG_ERR("settings_load_subtree, error: %d", err); - } - - return err; -} - -static int8_t bus_write(uint8_t dev_addr, uint8_t reg_addr, uint8_t *reg_data_ptr, uint16_t len) -{ - uint8_t buf[len + 1]; - - buf[0] = reg_addr; - memcpy(&buf[1], reg_data_ptr, len); - - return i2c_write(config.i2c_master, buf, len + 1, dev_addr); -} - -static int8_t bus_read(uint8_t dev_addr, uint8_t reg_addr, uint8_t *reg_data_ptr, uint16_t len) -{ - return i2c_write_read(config.i2c_master, dev_addr, ®_addr, 1, reg_data_ptr, len); -} - -static int64_t get_timestamp_us(void) -{ - return k_uptime_get() * MSEC_PER_SEC; -} - -static void delay_ms(uint32_t period) -{ - k_sleep(K_MSEC(period)); -} - -static void output_ready(int64_t timestamp, float iaq, uint8_t iaq_accuracy, float temperature, - float humidity, float pressure, float raw_temperature, float raw_humidity, - float gas, bsec_library_return_t bsec_status, float static_iaq, - float co2_equivalent, float breath_voc_equivalent) -{ - k_spinlock_key_t key = k_spin_lock(&(ctx.sensor_read_lock)); - - ctx.temperature_latest = (double)temperature; - ctx.humidity_latest = (double)humidity; - ctx.pressure_latest = (double)pressure / 1000; - ctx.air_quality_latest = (uint16_t)iaq; - - k_spin_unlock(&(ctx.sensor_read_lock), key); -} - -static uint32_t state_load(uint8_t *state_buffer, uint32_t n_buffer) -{ - if ((ctx.state_buffer_len > 0) && (ctx.state_buffer_len <= n_buffer)) { - memcpy(state_buffer, ctx.state_buffer, ctx.state_buffer_len); - return ctx.state_buffer_len; - } - - LOG_DBG("No stored state to load"); - return 0; -} - -static void state_save(const uint8_t *state_buffer, uint32_t length) -{ - LOG_INF("Storing state to flash"); - if (length > sizeof(ctx.state_buffer)) { - LOG_ERR("State buffer too big to save: %d", length); - return; - } - - int err = settings_save_one(SETTINGS_BSEC_STATE, state_buffer, length); - - if (err) { - LOG_ERR("Storing state to flash failed"); - } -} - -static uint32_t config_load(uint8_t *config_buffer, uint32_t n_buffer) -{ - /* Not implemented */ - return 0; -} - -static void bsec_thread_fn(void) -{ - bsec_iot_loop(delay_ms, get_timestamp_us, output_ready, state_save, BSEC_SAVE_INTERVAL); -} - -/* Public API */ - -int ext_sensors_bsec_temperature_get(double *temperature) -{ - if (temperature == NULL) { - return -EINVAL; - } - - k_spinlock_key_t key = k_spin_lock(&ctx.sensor_read_lock); - - memcpy(temperature, &ctx.temperature_latest, sizeof(ctx.temperature_latest)); - k_spin_unlock(&ctx.sensor_read_lock, key); - return 0; -} - -int ext_sensors_bsec_humidity_get(double *humidity) -{ - if (humidity == NULL) { - return -EINVAL; - } - - k_spinlock_key_t key = k_spin_lock(&ctx.sensor_read_lock); - - memcpy(humidity, &ctx.humidity_latest, sizeof(ctx.humidity_latest)); - k_spin_unlock(&ctx.sensor_read_lock, key); - return 0; -} - -int ext_sensors_bsec_pressure_get(double *pressure) -{ - if (pressure == NULL) { - return -EINVAL; - } - - k_spinlock_key_t key = k_spin_lock(&ctx.sensor_read_lock); - - memcpy(pressure, &ctx.pressure_latest, sizeof(ctx.pressure_latest)); - k_spin_unlock(&ctx.sensor_read_lock, key); - return 0; -} - -int ext_sensors_bsec_air_quality_get(uint16_t *air_quality) -{ - if (air_quality == NULL) { - return -EINVAL; - } - - k_spinlock_key_t key = k_spin_lock(&ctx.sensor_read_lock); - - memcpy(air_quality, &ctx.air_quality_latest, sizeof(ctx.air_quality_latest)); - k_spin_unlock(&ctx.sensor_read_lock, key); - return 0; -} - -int ext_sensors_bsec_init(void) -{ - int err; - return_values_init bsec_ret; - - err = enable_settings(); - if (err) { - LOG_ERR("enable_settings, error: %d", err); - return err; - } - - if (!device_is_ready(config.i2c_master)) { - LOG_ERR("I2C device not ready"); - return -ENODEV; - } - - bsec_ret = bsec_iot_init(BSEC_SAMPLE_RATE, temp_offset, bus_write, bus_read, delay_ms, - state_load, config_load); - - if (bsec_ret.bme680_status) { - LOG_ERR("Could not initialize BME680: %d", bsec_ret.bme680_status); - return -EIO; - } else if (bsec_ret.bsec_status) { - LOG_ERR("Could not initialize BSEC library: %d", bsec_ret.bsec_status); - - if (bsec_ret.bsec_status == BSEC_E_CONFIG_VERSIONMISMATCH) { - LOG_ERR("Deleting state from flash"); - settings_delete(SETTINGS_BSEC_STATE); - } - - return -EIO; - } - - k_thread_create(&ctx.thread, - thread_stack, - BSEC_STACK_SIZE, - (k_thread_entry_t)bsec_thread_fn, - NULL, NULL, NULL, K_HIGHEST_APPLICATION_THREAD_PRIO, 0, K_NO_WAIT); - - return 0; -} diff --git a/src/ext_sensors/ext_sensors_bsec.h b/src/ext_sensors/ext_sensors_bsec.h deleted file mode 100644 index c09def8..0000000 --- a/src/ext_sensors/ext_sensors_bsec.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause - */ - -int ext_sensors_bsec_temperature_get(double *temperature); - -int ext_sensors_bsec_humidity_get(double *humidity); - -int ext_sensors_bsec_pressure_get(double *pressure); - -int ext_sensors_bsec_air_quality_get(uint16_t *air_quality); - -int ext_sensors_bsec_init(void); diff --git a/src/modules/sensor_module.c b/src/modules/sensor_module.c index 243ba2f..8e865b8 100644 --- a/src/modules/sensor_module.c +++ b/src/modules/sensor_module.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Nordic Semiconductor ASA + * Copyright (c) 2023 Nordic Semiconductor ASA * * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause */ @@ -190,8 +190,8 @@ static void ext_sensor_handler(const struct ext_sensor_evt *const evt) case EXT_SENSOR_EVT_PRESSURE_ERROR: LOG_ERR("EXT_SENSOR_EVT_PRESSURE_ERROR"); break; - case EXT_SENSOR_EVT_BME680_BSEC_ERROR: - LOG_ERR("EXT_SENSOR_EVT_BME680_BSEC_ERROR"); + case EXT_SENSOR_EVT_AIR_QUALITY_ERROR: + LOG_ERR("EXT_SENSOR_EVT_AIR_QUALITY_ERROR"); break; default: break;