04-15-2021 06:15 PM
Hello,
I am writing a code to initialize the BMI055 IMU on my STM32F765. To do so I am using the HAL libraries to enable the SPI connection through DMA and the drivers provided by Bosch: bma2x2 and bmg160.
The two sensors work perfectly when only one of the two is enabled, but together they don't work.
// Init Gyro
int32_t bmg160_sensor_init(void)
{
SPI_routine_gyro();
com_rslt_2 = bmg160_init(&bmg160);
com_rslt_2 += bmg160_set_power_mode(BMG160_MODE_NORMAL);
v_bw_u8 = C_BMG160_BW_230HZ_U8X; /* set gyro bandwidth of 230Hz*/
com_rslt_2 += bmg160_set_bw(v_bw_u8);
/* This API used to read back the written value of bandwidth for gyro*/
com_rslt_2 += bmg160_get_bw(&v_gyro_value_u8);
bmg160_set_intr_output_type(BMG160_INTR1, 0);
// 1. Interrupt on Data is on INT1 or INT2 on register 0x18
com_rslt_2 += bmg160_set_intr_data(0, BMG160_ENABLE);
// ENABLE INTERRUPT
// 2. Set the interrupt mode to new-data on register 0x15
com_rslt_2 += bmg160_set_data_enable(BMG160_ENABLE);
return com_rslt_2;
}
// Init Accelerometer
int32_t bma2x2_sensor_init(void)
{
SPI_routine_acc();
com_rslt = bma2x2_init(&bma2x2);
com_rslt += bma2x2_set_power_mode(BMA2x2_MODE_NORMAL);
/* This API used to read back the written value of bandwidth*/
com_rslt += bma2x2_get_bw(&banwid);
// Set source to new-data
com_rslt += bma2x2_set_source(5, 0x01);
// Set register 0x19 to INT1 in new-data
com_rslt += bma2x2_set_new_data(0, 0x01);
// Enable interrupt for new-data
com_rslt += bma2x2_set_intr_enable(BMA2x2_DATA_ENABLE, 0x01);
return com_rslt;
}
The transmit and receive is similar for the accelerometer (chip select: PG10) and the gyroscope (chip select: PF4):
s8 BMA2x2_SPI_bus_write(u8 dev_addr, u8 reg_addr, u8* reg_data, u8 cnt)
{
s32 iError = BMA2x2_INIT_VALUE;
u8 array[SPI_BUFFER_LEN * 2];
u8 stringpos = BMA2x2_INIT_VALUE;
for (stringpos = BMA2x2_INIT_VALUE; stringpos < cnt; stringpos++)
{
array[stringpos * 2] = (reg_addr++) & BMA2x2_SPI_BUS_WRITE_CONTROL_BYTE;
array[stringpos * 2 + BMA2x2_BUS_READ_WRITE_ARRAY_INDEX]
= *(reg_data + stringpos);
}
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_10, RESET);
iError = HAL_SPI_Transmit_DMA(&hspi1, array, cnt * 2);
while (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY)
;
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_10, SET);
return (s8) iError;
}
//--------------------------------------------------------------
s8 BMA2x2_SPI_bus_read(u8 dev_addr, u8 reg_addr, u8* reg_data, u8 cnt)
{
s32 iError = BMA2x2_INIT_VALUE;
u8 array[SPI_BUFFER_LEN] = { 0xFF };
u8 stringpos;
/* For the SPI mode only 7 bits of register addresses are used.
The MSB of register address is declared the bit what functionality it is
read/write (read as 1/write as 0)*/
array[BMA2x2_INIT_VALUE] = reg_addr | BMA2x2_SPI_BUS_READ_CONTROL_BYTE;
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_10, RESET);
iError = HAL_SPI_Receive_DMA(&hspi1, array, cnt + 1);
while (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY)
;
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_10, SET);
for (stringpos = BMA2x2_INIT_VALUE; stringpos < cnt; stringpos++)
{
*(reg_data + stringpos)
= array[stringpos + BMA2x2_BUS_READ_WRITE_ARRAY_INDEX];
}
return (s8) iError;
}
For brevity, I'm not writing here also the gpio initialization, the spi etc. However, when only one sensor is enabled, everything works. When together, problems start at the initialization level: I can't read back for example a proper bandwidth for one of the two sensors.
I'm not sure if the problem is at the sensor level (so the way I am initializing the peripherals) or at the microcontroller level (maybe some errors in the spi initialization, etc).
I hope someone can help to find the problem.
NOTE: The code posted shows this series of events:
But I've also tried:
but nothing changed.
Thanks in advance.
04-19-2021 05:05 PM
Hi,
After power on by default BMI055 accel will be working in normal mode with +/-2g full scale range and 2000Hz ODR (output data rate) and BMI055 gyro will be working in normal mode with +/-2000dps full scale range and 2000Hz ODR.
In order to see accel DRDY interrupt on INT1 pin and gyro DRDY interrupt on INT3 pin, you can use SPI registers write directly without API. For example, you can do the following:
// accel DRDY on INT1 pin
Write value of 0x01 to register 0x1A; // map DRDY to INT1 pin
Write value of 0x10 to register 0x17; // enable DRDY inerrupt
// gyro DRDY on INT3 pin
Write value of 0x05 to register 0x16; // set INT3 pin to push-pull and active high
Write value of 0x01 to register 0x18; // map DRDY to INT3 pin
Write value of 0x80 to register 0x15; // enable DRDY inerrupt
Please check if you can see these DRDY signals or not.
Thanks.
04-22-2021 09:43 AM
DRDY signal for the accelerometer works well but the gyro still does not work when initialized with the accelerometer.
04-22-2021 06:06 PM
Hi,
Inside BMI055 the accel and gyro are independent to each other which means that they work on themselves and don't talk to each other. I tried the above registers' writing (2 writes for accel and 3 writes for gyro) and I can see both DRDY signals as shown below on the BMI055 shuttle board at https://www.bosch-sensortec.com/media/boschsensortec/downloads/shuttle_board_flyer/bst-dhw-fl020.pdf pin-21 (acc DRDY) and pin-22 (gyro DRDY) respectively.
Please use your logic analyzer to monitor SPI transactions to see if you have successfully written to the registers by reading values back from those registers after writing.
Thanks.
04-23-2021 04:40 PM
Accelerometer, INT1
INT1 works very well while gyroscope INT3 is completely inconsistent. Sometimes it works (wave same as the INT1), but most of the time there is only noise on the pin.
04-23-2021 04:55 PM
Hi,
This is strange. Are you testing BMI055 on your own PCB? If yes, then I would like to suggest that you test BMI055 shuttle board using your MCU.
BMI055 shuttle board schematics is available online at https://www.bosch-sensortec.com/media/boschsensortec/downloads/shuttle_board_flyer/bst-dhw-fl020.pdf. Pin-21 is INT1A for accel DRDY and pin-22 INT1G for gyro DRDY. You can connect VDD&VDDIO, GND, SPI bus from your MCU to BMI055 shuttle board and ignore those CODxxx pins because these pins are used for shuttle ID for the application base board to recognize each shuttle board.
Please send me a private message with your shipping address. I will then ship one BMI055 shuttle board to you for your testing.
Thanks.