Skip to content
Snippets Groups Projects
Commit a44bc26e authored by schneider's avatar schneider
Browse files

fix(bsec): Reduce stack usage by calling init before stating the task

parent 48773132
No related branches found
No related tags found
No related merge requests found
...@@ -126,16 +126,18 @@ int main(void) ...@@ -126,16 +126,18 @@ int main(void)
} }
/* BSEC */ /* BSEC */
if(bsec_activate() == 0) {
if (xTaskCreate( if (xTaskCreate(
vBSECTask, vBSECTask,
(const char *)"BSEC", (const char *)"BSEC",
configMINIMAL_STACK_SIZE * 4, configMINIMAL_STACK_SIZE * 3, /* 768 bytes. Mainly for state saving... */
NULL, NULL,
tskIDLE_PRIORITY + 1, tskIDLE_PRIORITY + 1,
NULL) != pdPASS) { NULL) != pdPASS) {
LOG_CRIT("startup", "Failed to create %s task!", "BSEC"); LOG_CRIT("startup", "Failed to create %s task!", "BSEC");
abort(); abort();
} }
}
/* API */ /* API */
if (xTaskCreate( if (xTaskCreate(
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
TaskHandle_t bsec_task_id; TaskHandle_t bsec_task_id;
static struct bme680_sensor_data last_bme680_data; static struct bme680_sensor_data last_bme680_data;
static int64_t last_bme680_timestamp; static int64_t last_bme680_timestamp;
static bool active; static bool bsec_task_active;
#define ULP 0 #define ULP 0
// From generic_18v_3s_4d/bsec_serialized_configurations_iaq.c // From generic_18v_3s_4d/bsec_serialized_configurations_iaq.c
...@@ -227,6 +227,17 @@ static int8_t i2c_read(uint8_t addr, uint8_t reg, uint8_t *p_buf, uint16_t size) ...@@ -227,6 +227,17 @@ static int8_t i2c_read(uint8_t addr, uint8_t reg, uint8_t *p_buf, uint16_t size)
return ret; return ret;
} }
static void delay(uint32_t msec)
{
if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
/* We need to fall back to hardware waits if not running
* in a task context */
card10_bosch_delay(msec);
} else {
vTaskDelay(pdMS_TO_TICKS(msec));
}
}
/*! /*!
* @brief Load library config from non-volatile memory * @brief Load library config from non-volatile memory
* *
...@@ -280,19 +291,14 @@ void ulp_plus_trigger_iaq() ...@@ -280,19 +291,14 @@ void ulp_plus_trigger_iaq()
} }
#endif #endif
static void delay(uint32_t msec)
{
vTaskDelay(pdMS_TO_TICKS(msec));
}
bool bsec_active(void) bool bsec_active(void)
{ {
return active; return bsec_task_active;
} }
int bsec_read_bme680(struct bme680_sensor_data *data) int bsec_read_bme680(struct bme680_sensor_data *data)
{ {
if (!active) { if (!bsec_task_active) {
return BME680_E_COM_FAIL; return BME680_E_COM_FAIL;
} }
while (last_bme680_timestamp == 0) while (last_bme680_timestamp == 0)
...@@ -301,23 +307,24 @@ int bsec_read_bme680(struct bme680_sensor_data *data) ...@@ -301,23 +307,24 @@ int bsec_read_bme680(struct bme680_sensor_data *data)
return BME680_OK; return BME680_OK;
} }
void vBSECTask(void *pvParameters) /**
* Checks config and activates the BSEC libary if requested.
*
* Initializes the BSEC library before starting the task to
* reduce the stack size needed for the task by at least 512 bytes
*/
int bsec_activate(void)
{ {
return_values_init ret; return_values_init ret;
active = true;
bsec_task_id = xTaskGetCurrentTaskHandle();
#if ULP #if ULP
float sample_rate = BSEC_SAMPLE_RATE_ULP; float sample_rate = BSEC_SAMPLE_RATE_ULP;
/* State is saved every 100 samples, which means every 100 * 300 secs = 500 minutes */
const int stat_save_interval = 100;
#else #else
float sample_rate = BSEC_SAMPLE_RATE_LP; float sample_rate = BSEC_SAMPLE_RATE_LP;
/* State is saved every 10.000 samples, which means every 10.000 * 3 secs = 500 minutes */
const int stat_save_interval = 10000;
#endif #endif
float temperature_offset = 0.0; float temperature_offset = 0.0;
/* Puts AT LEAST 2 * #BSEC_MAX_PROPERTY_BLOB_SIZE = 2 * 454 = 908 bytes onto the stack */
ret = bsec_iot_init( ret = bsec_iot_init(
sample_rate, sample_rate,
temperature_offset, temperature_offset,
...@@ -331,16 +338,31 @@ void vBSECTask(void *pvParameters) ...@@ -331,16 +338,31 @@ void vBSECTask(void *pvParameters)
if (ret.bme680_status) { if (ret.bme680_status) {
printf("bme680 init failed\n"); printf("bme680 init failed\n");
/* Could not intialize BME680 */ /* Could not intialize BME680 */
while (1) return -1;
;
} else if (ret.bsec_status) { } else if (ret.bsec_status) {
printf("bsec init failed\n"); printf("bsec init failed\n");
/* Could not intialize BSEC library */ /* Could not intialize BSEC library */
while (1) return -1;
; }
return 0;
} }
void vBSECTask(void *pvParameters)
{
bsec_task_active = true;
bsec_task_id = xTaskGetCurrentTaskHandle();
#if ULP
/* State is saved every 100 samples, which means every 100 * 300 secs = 500 minutes */
const int stat_save_interval = 100;
#else
/* State is saved every 10.000 samples, which means every 10.000 * 3 secs = 500 minutes */
const int stat_save_interval = 10000;
#endif
/* Call to endless loop function which reads and processes data based on sensor settings */ /* Call to endless loop function which reads and processes data based on sensor settings */
/* Puts AT LEAST 2 * BSEC_MAX_STATE_BLOB_SIZE + 8 * sizeof(bsec_input_t) =
* 2 * 139 + 8 * 20 = 438 bytes onto the stack */
bsec_iot_loop( bsec_iot_loop(
delay, delay,
get_timestamp_us, get_timestamp_us,
......
...@@ -137,6 +137,7 @@ void max30001_mutex_init(void); ...@@ -137,6 +137,7 @@ void max30001_mutex_init(void);
extern gpio_cfg_t gpio_configs[]; extern gpio_cfg_t gpio_configs[];
/* ---------- BSEC / BME680 ------------------------------------------------ */ /* ---------- BSEC / BME680 ------------------------------------------------ */
int bsec_activate(void);
void vBSECTask(void *pvParameters); void vBSECTask(void *pvParameters);
bool bsec_active(void); bool bsec_active(void);
struct bme680_sensor_data; struct bme680_sensor_data;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment