How to use BSEC_IOT exemple for the BME680

Hello 

I'm using the BME680 in STMCubeIDE with a STM32WLE,

I succed in importing all the files for the bsec algo with the 'bme680_iaq_33v_300s_4d' settings. 

And i used the GCC cortex M4 libalgobsec.a file 

I want the library to be called by my own sleep callback function that will be triggered every 120 sec (or semething close to BSEC requirement), to read IAQ data.

So i used the files in 'BSEC_Integration_Examples'

 

void sleep_n(uint32_t t_us, void *intf_ptr)
{
	//HAL_LPTIM_Counter_Start_IT(&hlptim1,(32768/ 128) * t_us);     //20sec timer
	//Delay_us(t_us);
}

int64_t get_timestamp_us()
{

	int64_t system_current_time = 0;
	// ...
	// Please insert system specific function to retrieve a timestamp (in microseconds)
	// ...
	system_current_time =((int64_t)(HAL_GetTick())*1000);
	return system_current_time;


}

uint32_t state_load(uint8_t *state_buffer, uint32_t n_buffer)
{
    // ...
    // Load a previous library state from non-volatile memory, if available.
    //
    // Return zero if loading was unsuccessful or no state was available,
    // otherwise return length of loaded state string.
    // ...
    return 0;
}

/*!
 * @brief           Save library state to non-volatile memory
 *
 * @param[in]       state_buffer    buffer holding the state to be stored
 * @param[in]       length          length of the state string to be stored
 *
 * @return          none
 */
void state_save(const uint8_t *state_buffer, uint32_t length)
{
    // ...
    // Save the string some form of non-volatile memory, if possible.
    // ...
	//SPI_Flash_Erase_4K(SECTOR_STATE_BSEC);
	//SPI_Flash_Write_Bytes(SECTOR_STATE_BSEC,length+1,state_buffer);
	//SPI_Flash_Sleep();
}


uint32_t config_load(uint8_t *config_buffer, uint32_t n_buffer)
{
    /*memcpy(config_buffer, Selectivity_config, n_buffer);
    return n_buffer;*/
	return 0;
}



void output_ready(int64_t timestamp, float gas_estimate_1, float gas_estimate_2, float gas_estimate_3, float gas_estimate_4,
     float raw_pressure, float raw_temp, float raw_humidity, float raw_gas, uint8_t raw_gas_index, bsec_library_return_t bsec_status)
{
    int64_t timestamp_int = (int64_t)timestamp;
    int32_t gas_estimate_1_int = (int32_t)gas_estimate_1;
    int32_t gas_estimate_2_int = (int32_t)gas_estimate_2;
    int32_t gas_estimate_3_int = (int32_t)gas_estimate_3;
    int32_t gas_estimate_4_int = (int32_t)gas_estimate_4;
    int32_t raw_pressure_int = (int32_t)raw_pressure;
    int32_t raw_temp_int = (int32_t)raw_temp;
    int32_t raw_humidity_int = (int32_t)raw_humidity;
    int32_t raw_gas_int = (int32_t)raw_gas;
    int32_t raw_gas_index_int = (int32_t)raw_gas_index;
    int32_t bsec_status_int = (int32_t)bsec_status;

    APP_LOG(TS_ON, VLEVEL_L, "Timestamp: %lld, Gas Estimate 1: %d, Gas Estimate 2: %d, Gas Estimate 3: %d, Gas Estimate 4: %d, Raw Pressure: %d, Raw Temperature: %d, Raw Humidity: %d, Raw Gas: %d, Raw Gas Index: %d, BSEC Status: %d\r\n",
            timestamp_int, gas_estimate_1_int, gas_estimate_2_int, gas_estimate_3_int, gas_estimate_4_int, raw_pressure_int, raw_temp_int, raw_humidity_int, raw_gas_int, raw_gas_index_int, bsec_status_int);
}
	Enable_sensor_BME680();
	return_values_init ret;
	struct bme68x_dev bme_dev;
	memset(&bme_dev,0,sizeof(bme_dev));
	bme_dev.intf = BME68X_SPI_INTF;
	bme_dev.read = bme68x_spi_read;
	bme_dev.write = bme68x_spi_write;
	bme_dev.delay_us = bme68x_delay_us;
	//bme_dev.intf_ptr = &communicationSetup;
	bme_dev.amb_temp = 25;

	ret = bsec_iot_init(BSEC_SAMPLE_RATE_ULP, 0.0f, bme68x_spi_write, bme68x_spi_read, sleep_n, state_load, config_load, bme_dev);
	if (ret.bme68x_status)
		{
			/* Could not initialize BME68x */
			APP_LOG(TS_ON, VLEVEL_L,"Error while initializing BME68x: %d",ret.bme68x_status);
			return;
		}
		else if (ret.bsec_status)
		{
			/* Could not initialize BSEC library */
			APP_LOG(TS_ON, VLEVEL_L,"Error while initializing BSEC library: %d",ret.bsec_status);
			return;
		}

 

Here i can see that the BME680 is initalised but when the code come to the bsec init the MCU stop working.

Also what is the code to be called when i trigger my sleep callback function (LPTIM)

 

Thanks

Best reply by BSTRobin

Hi hamady,

If you use BSEC2.4.0.0, you can refer attached example code on STM32, enable macro definition in the file user_define.h.
//#define BME68X_OUTPUT_GAS_ESTIMATE
#define BME68X_OUTPUT_GAS_IAQ

BSEC_2.4.0.0_ExampleCode.zip
20.05KB
View original
9 replies
Resolved