04-13-2022 03:22 PM
HI,
I'm using BME688 breakout board sensor with nRF52832 microcontroller. They communicate through I2C protocol.
For the moment, I do not want ot use the BSEC library, I want to read the datas of Temperature, Pressure and Humidity and gas.
I downloaded the API from github and I modified the common.c file to adapt to my microcontroller in this way
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "bme68x.h"
#include "bme68x_defs.h"
#include "common.h"
#include "nrf_drv_twi.h"
#include "nrf_delay.h"
/******************************************************************************/
/*! Macro definitions */
/*! BME68X shuttle board ID */
#define BME68X_SHUTTLE_ID 0x93
#define SDA_PIN 18
#define SCL_PIN 19
/******************************************************************************/
/*! Static variable definition */
static uint8_t dev_addr;
static const nrf_drv_twi_t i2c_instance = NRF_DRV_TWI_INSTANCE(0);
/******************************************************************************/
/*! User interface functions */
/*!
* I2C read function map to COINES platform
*/
BME68X_INTF_RET_TYPE bme68x_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr)
{
uint8_t dev_addr = *(uint8_t*)intf_ptr;
ret_code_t ret_code;
ret_code = nrf_drv_twi_tx(&i2c_instance, dev_addr, ®_addr,1,false);
if(ret_code != NRF_SUCCESS)
{
return ret_code;
}
ret_code = nrf_drv_twi_rx(&i2c_instance, dev_addr, reg_data, len);
if (ret_code == NRF_SUCCESS)
return BME68X_OK;
else
return 1;
}
/*!
* I2C write function map to COINES platform
*/
BME68X_INTF_RET_TYPE bme68x_i2c_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr)
{
uint8_t dev_addr = *(uint8_t*)intf_ptr;
ret_code_t ret_code;
uint8_t send_tmp[10] = {0};
send_tmp[0] = reg_addr;
memcpy(send_tmp+1, reg_data, len);
ret_code_t err_code = nrf_drv_twi_tx(&i2c_instance, dev_addr, send_tmp, len, false);
if (ret_code == NRF_SUCCESS) return BME68X_OK;
else return 1;
}
/*!
* Delay function map to COINES platform
*/
void bme68x_delay_us(uint32_t period, void *intf_ptr)
{
nrf_delay_us(period);
}
int8_t bme68x_interface_init(struct bme68x_dev *bme, uint8_t intf)
{
int8_t rslt = BME68X_OK;
uint8_t reg_addr;
const uint8_t *reg_data;
uint32_t len;
void *intf_ptr;
if (bme != NULL)
{
/* Bus configuration : I2C */
if (intf == BME68X_I2C_INTF)
{
dev_addr = BME68X_I2C_ADDR_LOW;
bme->read = bme68x_i2c_read;
bme->write = bme68x_i2c_write;
bme->intf = BME68X_I2C_INTF;
//coines_config_i2c_bus(COINES_I2C_BUS_0, COINES_I2C_STANDARD_MODE);
/* const nrf_drv_twi_config_t i2c_instance_config = { .scl = SCL_PIN,
.sda = SDA_PIN,
.frequency = NRF_TWI_FREQ_100K,
.interrupt_priority = 0};
rslt = nrf_drv_twi_init(&i2c_instance, &i2c_instance_config, NULL, NULL);
//enable TWI instance
nrf_drv_twi_enable(&i2c_instance);
*/
}
//coines_set_shuttleboard_vdd_vddio_config(3300, 3300);
nrf_delay_ms(100);
bme->delay_us = bme68x_delay_us;
bme->intf_ptr = &dev_addr;
bme->amb_temp = 25; /* The ambient temperature in deg C is used for defining the heater temperature */
}
else
{
rslt = BME68X_E_NULL_PTR;
}
return rslt;
}
void bme68x_check_rslt(const char api_name[], int8_t rslt)
{
switch (rslt)
{
case BME68X_OK:
printf("API name [%s]: Tutto ok\n", api_name);
/* Do nothing */
break;
case BME68X_E_NULL_PTR:
printf("API name [%s] Error [%d] : Null pointer\r\n", api_name, rslt);
break;
case BME68X_E_COM_FAIL:
printf("API name [%s] Error [%d] : Communication failure\r\n", api_name, rslt);
break;
case BME68X_E_INVALID_LENGTH:
printf("API name [%s] Error [%d] : Incorrect length parameter\r\n", api_name, rslt);
break;
case BME68X_E_DEV_NOT_FOUND:
printf("API name [%s] Error [%d] : Device not found\r\n", api_name, rslt);
break;
case BME68X_E_SELF_TEST:
printf("API name [%s] Error [%d] : Self test error\r\n", api_name, rslt);
break;
case BME68X_W_NO_NEW_DATA:
printf("API name [%s] Warning [%d] : No new data found\r\n", api_name, rslt);
break;
default:
printf("API name [%s] Error [%d] : Unknown error code\r\n", api_name, rslt);
break;
}
}
/*
void bme68x_coines_deinit(void)
{
fflush(stdout);
coines_set_shuttleboard_vdd_vddio_config(0, 0);
coines_delay_msec(1000);
// Coines interface reset
coines_soft_reset();
coines_delay_msec(1000);
coines_close_comm_intf(COINES_COMM_INTF_USB);
}
*/
Then, I try the example of forced mode
struct bme68x_dev bme;
int8_t rslt;
struct bme68x_conf conf;
struct bme68x_heatr_conf heatr_conf;
struct bme68x_data data;
uint32_t del_period;
uint32_t time_ms = 0;
uint8_t n_fields;
uint16_t sample_count = 1;
/* Interface preference is updated as a parameter
* For I2C : BME68X_I2C_INTF
* For SPI : BME68X_SPI_INTF
*/
rslt = bme68x_interface_init(&bme, BME68X_I2C_INTF);
bme68x_check_rslt("bme68x_interface_init", rslt);
rslt = bme68x_init(&bme);
bme68x_check_rslt("bme68x_init", rslt);
/* Check if rslt == BME68X_OK, report or handle if otherwise */
conf.filter = BME68X_FILTER_OFF;
conf.odr = BME68X_ODR_NONE;
conf.os_hum = BME68X_OS_16X;
conf.os_pres = BME68X_OS_1X;
conf.os_temp = BME68X_OS_2X;
rslt = bme68x_set_conf(&conf, &bme);
bme68x_check_rslt("bme68x_set_conf", rslt);
/* Check if rslt == BME68X_OK, report or handle if otherwise */
heatr_conf.enable = BME68X_ENABLE;
heatr_conf.heatr_temp = 300;
heatr_conf.heatr_dur = 100;
rslt = bme68x_set_heatr_conf(BME68X_FORCED_MODE, &heatr_conf, &bme);
bme68x_check_rslt("bme68x_set_heatr_conf", rslt);
printf("Sample, TimeStamp(ms), Temperature(deg C), Pressure(Pa), Humidity(%%), Gas resistance(ohm), Status\n");
while (sample_count <= SAMPLE_COUNT)
{
rslt = bme68x_set_op_mode(BME68X_FORCED_MODE, &bme);
bme68x_check_rslt("bme68x_set_op_mode", rslt);
/* Calculate delay period in microseconds */
del_period = bme68x_get_meas_dur(BME68X_FORCED_MODE, &conf, &bme) + (heatr_conf.heatr_dur * 1000);
bme.delay_us(del_period, bme.intf_ptr);
time_ms = count*50;
/* Check if rslt == BME68X_OK, report or handle if otherwise */
rslt = bme68x_get_data(BME68X_FORCED_MODE, &data, &n_fields, &bme);
bme68x_check_rslt("bme68x_get_data", rslt);
if (n_fields)
{
#ifdef BME68X_USE_FPU
printf("%u, %lu, %.2f, %.2f, %.2f, %.2f, 0x%x\n",
sample_count,
(long unsigned int)time_ms,
data.temperature,
data.pressure,
data.humidity,
data.gas_resistance,
data.status);
#else
printf("%u, %lu, %d, %lu, %lu, %lu, 0x%x\n",
sample_count,
(long unsigned int)time_ms,
(data.temperature / 100),
(long unsigned int)data.pressure,
(long unsigned int)(data.humidity / 1000),
(long unsigned int)data.gas_resistance,
data.status);
#endif
sample_count++;
}
}
It seems all rigth, I build and the code does not gave any error, but the output is this:
API name [bme68x_interface_init]: Tutto ok
API name [bme68x_init]: Tutto ok
API name [bme68x_set_conf]: Tutto ok
API name [bme68x_set_heatr_conf]: Tutto ok
Sample, TimeStamp(ms), Temperature(deg C), Pressure(Pa), Humidity(%), Gas resistance(ohm), Status
API name [bme68x_set_op_mode]: Tutto ok
API name [bme68x_get_data] Warning [2] : No new data found
API name [bme68x_set_op_mode]: Tutto ok
API name [bme68x_get_data] Warning [2] : No new data found
API name [bme68x_set_op_mode]: Tutto ok
API name [bme68x_get_data] Warning [2] : No new data found
API name [bme68x_set_op_mode]: Tutto ok
API name [bme68x_get_data] Warning [2] : No new data found
API name [bme68x_set_op_mode]: Tutto ok
API name [bme68x_get_data] Warning [2] : No new data found
API name [bme68x_set_op_mode]: Tutto ok
API name [bme68x_get_data] Warning [2] : No new data found
API name [bme68x_set_op_mode]: Tutto ok
API name [bme68x_get_data] Warning [2] : No new data found
API name [bme68x_set_op_mode]: Tutto ok
API name [bme68x_get_data] Warning [2] : No new data found
API name [bme68x_set_op_mode]: Tutto ok
API name [bme68x_get_data] Warning [2] : No new data found
API name [bme68x_set_op_mode]: Tutto ok
API name [bme68x_get_data] Warning [2] : No new data found
API name [bme68x_set_op_mode]: Tutto ok
API name [bme68x_get_data] Warning [2] : No new data found
API name [bme68x_set_op_mode]: Tutto ok
API name [bme68x_get_data] Warning [2] : No new data found (and it continues with no new data)
I try to wait a lot, but the result does not change. I do not understand where the problem is. What can I do to solve the problem?
Is it possible that the sensor is damaged? In that case, how can I understand it?
Thanks for the help
04-14-2022 04:52 AM - edited 04-15-2022 10:49 AM
04-14-2022 09:12 AM
Hi, thanks.
Where can I find these examples?
04-15-2022 10:50 AM
Sorry, I forget to upload attachment.
You could get it now.
04-21-2022 08:33 AM
Hi,
I have looked at the example and I think my functions are quite similar, but the code gives back the same error: no new data found.
I attach my common.c file, if you could give it a look. I use I2C communication (in the function, I use the 7 bit address because the microcontroller wants the 7 bit address).
Thanks