12-05-2020 10:06 AM
Hallo
I try to use this programm.When I use SPI, BMI runs normally.But in the process of using IIC, there was a problem. In the setup program, the value of rslt is not equal to the BMI_OK value.This means this connection was not successful.
09:58:59.151 rslt-> -2
09:58:59.151 BMI_OK-> 0
This is my setup program, what went wrong. Thank you very much
void setup() {
/* Variable to define result */
int8_t rslt;
pinMode(BMI270_CS, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
digitalWrite(BMI270_CS, HIGH);
// Wire.setModule(0);
Serial.begin(115200);
i2c_bus = 0x68;
// spi_bus = BMI270_CS;
/* To initialize the hal function */
//SPI.begin();
Wire.begin();
bmi2.intf_ptr = &i2c_bus;
bmi2.intf = BMI2_I2C_INTF;
bmi2.read = bmi2xy_hal_i2c_bus_read;
bmi2.write = bmi2xy_hal_i2c_bus_write;
bmi2.read_write_len = 32;
bmi2.delay_us = bmi2xy_hal_delay_usec;
/* Config file pointer should be assigned to NULL, so that default file address is assigned in bmi270_init */
bmi2.config_file_ptr = NULL;
/* Initialize bmi270 */
rslt = bmi270_init(&bmi2);
Serial.println("bmi270_init done");
rslt = bmi2_accel_gyro_set_config(&bmi2);
Serial.println(rslt);
Serial.println(BMI2_OK);
if (rslt == BMI2_OK)
{
Serial.println("Accel data Ready : X Y Z ");
Serial.println("Gyro data Ready : X Y Z ");
}
delay(250);
// Interrupt PINs configuration
struct bmi2_int_pin_config data_int_cfg;
data_int_cfg.pin_type = BMI2_INT1;
data_int_cfg.int_latch = BMI2_INT_NON_LATCH;
data_int_cfg.pin_cfg[0].output_en = BMI2_INT_OUTPUT_ENABLE; // Output enabled
data_int_cfg.pin_cfg[0].od = BMI2_INT_PUSH_PULL; // OpenDrain disabled
data_int_cfg.pin_cfg[0].lvl = BMI2_INT_ACTIVE_LOW; // Signal Low Active
data_int_cfg.pin_cfg[0].input_en = BMI2_INT_INPUT_DISABLE; // Input Disabled
rslt = bmi2_set_int_pin_config( &data_int_cfg, &bmi2);
pinMode(interruptPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), RawDataHandler, FALLING);
}
12-06-2020 04:43 PM
Hello Eason,
Which line you got -2 return value?
Suggest you could capture I2C waveform by logic analyzer, and compare the difference between SW code and I2C waveform.
12-06-2020 07:52 PM
From this line "Serial.println(rslt);" I get -2
rslt = bmi2_accel_gyro_set_config(&bmi2);
Serial.println(BMI2_OK);
Serial.println(rslt);
void setup() {
/* Variable to define result */
int8_t rslt;
pinMode(BMI270_CS, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
digitalWrite(BMI270_CS, HIGH);
// Wire.setModule(0);
Serial.begin(115200);
i2c_bus = 0x68;
// spi_bus = BMI270_CS;
/* To initialize the hal function */
//SPI.begin();
Wire.begin();
bmi2.intf_ptr = &i2c_bus;
bmi2.intf = BMI2_I2C_INTF;
bmi2.read = bmi2xy_hal_i2c_bus_read;
bmi2.write = bmi2xy_hal_i2c_bus_write;
bmi2.read_write_len = 16;
bmi2.delay_us = bmi2xy_hal_delay_usec;
/* Config file pointer should be assigned to NULL, so that default file address is assigned in bmi270_init */
bmi2.config_file_ptr = NULL;
/* Initialize bmi270 */
rslt = bmi270_init(&bmi2);
Serial.println("bmi270_init done");
rslt = bmi2_accel_gyro_set_config(&bmi2);
Serial.println(BMI2_OK);
Serial.println(rslt);
if (rslt == BMI2_OK)
{
Serial.println("Accel data Ready : X Y Z ");
Serial.println("Gyro data Ready : X Y Z ");
}
delay(250);
// Interrupt PINs configuration
struct bmi2_int_pin_config data_int_cfg;
data_int_cfg.pin_type = BMI2_INT1;
data_int_cfg.int_latch = BMI2_INT_NON_LATCH;
data_int_cfg.pin_cfg[0].output_en = BMI2_INT_OUTPUT_ENABLE; // Output enabled
data_int_cfg.pin_cfg[0].od = BMI2_INT_PUSH_PULL; // OpenDrain disabled
data_int_cfg.pin_cfg[0].lvl = BMI2_INT_ACTIVE_LOW; // Signal Low Active
data_int_cfg.pin_cfg[0].input_en = BMI2_INT_INPUT_DISABLE; // Input Disabled
rslt = bmi2_set_int_pin_config( &data_int_cfg, &bmi2);
pinMode(interruptPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), RawDataHandler, FALLING);
}
12-07-2020 12:48 AM
12-07-2020 09:27 AM
Hello Eason,
Return value -2 indicated communication fail error. Firstly you should check I2C communication works well.
This is I2C read/write function on STM32 platform for your reference.
int8_t SensorAPI_I2Cx_Read(uint8_t slave_address7, uint8_t subaddress, uint8_t *pBuffer, uint16_t ReadNumbr)
{
uint16_t DevAddress = slave_address7 << 1;
// send register address
HAL_I2C_Master_Transmit(&I2C_HANDLE, DevAddress, &subaddress, 1, BUS_TIMEOUT);
HAL_I2C_Master_Receive(&I2C_HANDLE, DevAddress, pBuffer, ReadNumbr, BUS_TIMEOUT);
return 0;
}
int8_t SensorAPI_I2Cx_Write(uint8_t slave_address7, uint8_t subaddress, uint8_t *pBuffer, uint16_t WriteNumbr)
{
uint16_t DevAddress = slave_address7 << 1;
GTXBuffer[0] = subaddress;
memcpy(>XBuffer[1], pBuffer, WriteNumbr);
// send register address
HAL_I2C_Master_Transmit(&I2C_HANDLE, DevAddress, GTXBuffer, WriteNumbr+1, BUS_TIMEOUT);
return 0;
}