Hi Minhwan, thanks for your reply and for confirming that the sensor_settings.next_call should be about 3,000,000,000 for 3 seconds. It returns a delta of 3,000 for me. I do base my code on the bsec_iot_example.c content. Besides moving it into one function (initialization and loop) I believe I only added printf's for debugging. Below the code and the log output. As you can see, the next_call is incremented by 3000, bsec_sensor_control is returning BSEC_W_SC_CALL_TIMING_VIOLATION. Thanks & regards, Tobi #include "sensor.h"
#include "i2c.h"
#include "bsec_integration.h"
void sensorLoop()
{
/* Timestamp variables */
int64_t time_stamp_start = 0;
int64_t time_stamp_end = 0;
int64_t time_stamp_interval_ms = 0;
/* Allocate enough memory for up to BSEC_MAX_PHYSICAL_SENSOR physical inputs*/
bsec_input_t bsec_inputs[BSEC_MAX_PHYSICAL_SENSOR];
/* Number of inputs to BSEC */
uint8_t num_bsec_inputs = 0;
/* BSEC sensor settings struct */
bsec_bme_settings_t sensor_settings;
/* Save state variables */
uint8_t bsec_state[BSEC_MAX_STATE_BLOB_SIZE];
uint8_t work_buffer[BSEC_MAX_WORKBUFFER_SIZE];
uint32_t bsec_state_len = 0;
uint32_t n_samples = 0;
bsec_library_return_t bsec_status = BSEC_OK;
return_values_init ret;
uint32_t save_intvl = 10000;
initI2C();
/* Call to the function which initializes the BSEC library
* Switch on low-power mode and provide no temperature offset */
ret = bsec_iot_init(BSEC_SAMPLE_RATE_LP, 0.0f, bus_write, bus_read, sensor_sleep, state_load, config_load);
if (ret.bme680_status) {
/* Could not intialize BME680 */
printf("Failed to initialize sensor\r\n");
} else if (ret.bsec_status) {
/* Could not intialize BSEC library */
printf("Failed to initialize BSEC\r\n");
}
/* Main loop */
while(1) {
/* get the timestamp in nanoseconds before calling bsec_sensor_control() */
time_stamp_start = get_timestamp_us() * 1000;
/* Retrieve sensor settings to be used in this time instant by calling bsec_sensor_control */
ret.bsec_status = bsec_sensor_control(time_stamp_start, &sensor_settings);
if(ret.bsec_status != BSEC_OK) {
printf("bsec_sensor_control: %d\r", ret.bsec_status);
}
/* Trigger a measurement if necessary */
bme680_bsec_trigger_measurement(&sensor_settings, sensor_sleep);
/* Read data from last measurement */
num_bsec_inputs = 0;
bme680_bsec_read_data(time_stamp_start, bsec_inputs, &num_bsec_inputs, sensor_settings.process_data);
/* Time to invoke BSEC to perform the actual processing */
bme680_bsec_process_data(bsec_inputs, num_bsec_inputs, output_ready);
/* Increment sample counter */
n_samples++;
/* Retrieve and store state if the passed save_intvl */
if (n_samples >= save_intvl) {
bsec_status = bsec_get_state(0, bsec_state, sizeof(bsec_state), work_buffer, sizeof(work_buffer), &bsec_state_len);
if (bsec_status == BSEC_OK) {
state_save(bsec_state, bsec_state_len);
}
n_samples = 0;
}
/* Compute how long we can sleep until we need to call bsec_sensor_control() next */
/* Time_stamp is converted from microseconds to nanoseconds first and then the difference to milliseconds */
time_stamp_end = get_timestamp_us() * 1000;
time_stamp_interval_ms = (sensor_settings.next_call - time_stamp_end) / 1000000;
if (time_stamp_interval_ms > 0) {
sensor_sleep((uint32_t)time_stamp_interval_ms);
}
printf("Start: %lld, End: %lld, next_call: %lld\r", time_stamp_start, time_stamp_end, sensor_settings.next_call);
}
} ts: 749000000d; acc: 0; iaq: 25.00; temp: 24.87; hum: 37.49; press: 98433.00; c02: 400.00; bvoc: 1000.00 ret: 0; stab: 1 Start: 749000000, End: 1069000000, next_call: 749003000 bsec_sensor_control: 100 ts: 1100000000d; acc: 0; iaq: 25.00; temp: 25.59; hum: 33.35; press: 98435.00; c02: 400.00; bvoc: 1000.00 ret: 0; stab: 1 Start: 1100000000, End: 1428000000, next_call: 1100003000 bsec_sensor_control: 100 ts: 1459000000d; acc: 0; iaq: 25.00; temp: 25.94; hum: 31.38; press: 98437.00; c02: 400.00; bvoc: 1000.00 ret: 0; stab: 1 Start: 1459000000, End: 1779000000, next_call: 1459003000 bsec_sensor_control: 100 ts: 1810000000d; acc: 0; iaq: 25.01; temp: 26.10; hum: 30.49; press: 98439.00; c02: 400.00; bvoc: 1000.00 ret: 0; stab: 1 Start: 1810000000, End: 2138000000, next_call: 1810003000 bsec_sensor_control: 100 ts: 2169000000d; acc: 0; iaq: 25.01; temp: 26.24; hum: 29.68; press: 98441.00; c02: 400.00; bvoc: 1000.00 ret: 0; stab: 1 Start: 2169000000, End: 2489000000, next_call: 2169003000
... View more