04-18-2019 05:25 PM
Hi,
I'm new using a Bosch sensor and I want to acess it using "HAL" library.
I have a BME680 sensor and I'm using a Nucleo-144 from ST. I want to use the API in GitHub (https://github.com/BoschSensortec/BME680_driver) but in file "bme680.c" I don't understand how use "read" and "write" called, for example, in function get_mem_page().
In "readme" file there are a template for "int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)" and "int8_t user_i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)" but I don´t know how to fill it and where to include it (in main.c? or bme680.c?). Can someone explain it?
Best resgard.
04-18-2019 09:08 PM
Hi
You could follow the README and copy the code examples and put the code clips in your main.c.
and in user_i2c_read() and user_i2c_write(), call the I2C functions provided by your ST MCU I2C functions.
for example:
int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
{
int err;
//pseudo code below
//the dev_id is the i2c address for BME680 and needs to be passed in some way to the st mcu's I2C function
err = st_nucleo_144_i2c_read(..., reg_addr, reg_data, len);
return err;
}
The bme680.c is the so called API code and supposed to be kept intact in most cases (unless you spot some bug fix or change needed).
Hope this is clear.
04-18-2019 09:18 PM
to compliment the above answer, you could refer to a working example code which uses the same API here:
https://github.com/adafruit/Adafruit_BME680/blob/master/Adafruit_BME680.cpp
in this case, it's using SPI to interface with the sensor, but the way how the API is used should be similar to I2C.
more info about this example could be found here: https://learn.adafruit.com/adafruit-bme680-humidity-temperature-barometic-pressure-voc-gas/arduino-w...
04-24-2019 04:44 PM
Hi
Thanks for you explanation! I already fill the functions user_i2c_read() and user_i2c_write() but something isn't right. My code doesn't advance from function bme680_init() (return always NULL) and I think that those functions are reason that didn´t work. This is the code that I used:
int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) { int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */ HAL_StatusTypeDef status = HAL_OK; uint8_t array[I2C_RX_SIZE_BUFF] = { 0 }; uint8_t stringpos = 0; array[0] = reg_addr; while (HAL_I2C_IsDeviceReady(&hi2c1, (uint8_t) (dev_id << 1), 3, 100) != HAL_OK) { } status = HAL_I2C_Mem_Read(&hi2c1, // i2c handle (uint8_t) (dev_id << 1), // i2c address, left aligned (uint8_t) reg_addr, // register address I2C_MEMADD_SIZE_8BIT, // bme280 uses 8bit register addresses (uint8_t*) (&array), // write returned data to this variable len, // how many bytes to expect returned 100); // timeout if (status != HAL_OK) { rslt = (-1); } for (stringpos = 0; stringpos < len; stringpos++) { *(reg_data + stringpos) = array[stringpos]; } return rslt; }
int8_t user_i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) { int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */ HAL_StatusTypeDef status = HAL_OK; while (HAL_I2C_IsDeviceReady(&hi2c1, (uint8_t) (dev_id << 1), 3, 100) != HAL_OK) { } status = HAL_I2C_Mem_Write(&hi2c1, // i2c handle (uint8_t) (dev_id << 1), // i2c address, left aligned (uint8_t) reg_addr, // register address I2C_MEMADD_SIZE_8BIT, // bme280 uses 8bit register addresses (uint8_t*) (®_data), // write returned data to reg_data len, // write how many bytes 100); // timeout if (status != HAL_OK) { rslt = (-1); } return rslt; }
best regards!
06-03-2020 09:48 AM
This is also a question of mine!
did you got your answer? My code returns strange values of Temp/Press/Hum for example 0.5 deg or 130 deg temp!!
I think this is for I2C read/Write function
Can you please share your experience
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int8_t user_i2c_read (uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) {
if (HAL_I2C_Master_Transmit(&hi2c4, (dev_id << 1), reg_addr, 1, 10)
!= HAL_OK)
return -1;
if (HAL_I2C_Master_Receive (& hi2c4, (dev_id << 1), reg_data, len, 10)
!= HAL_OK)
return -1;
return 0;
}
//int8_t user_i2c_read (uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) {
// if (HAL_I2C_Mem_Read(& hi2c4, dev_id<<1, reg_addr,1, reg_data, len, 5000) == HAL_OK)
// return 0;
// else
// return 1;}
void user_delay_ms (uint32_t period) {
HAL_Delay (period);
}
int8_t user_i2c_write (uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
{
int8_t * buf;
buf = malloc (len + 1);
buf [0] = reg_addr;
memcpy (buf + 1, reg_data, len);
if (HAL_I2C_Master_Transmit (& hi2c4, (dev_id << 1), (uint8_t *) buf, len + 1, HAL_MAX_DELAY)!= HAL_OK)
return -1;
free(buf);
return 0;
}