cancel
Showing results for 
Search instead for 
Did you mean: 

Hi I am going to get sensor data from the BMI160 through SPI with ST32L432KC. I appreciate it if anyone can help me with how I can configure the SPI. Thanks

SKaze
Associate II
 
13 REPLIES 13
TDK
Guru

What have you tried? Where are you getting stuck?

If you feel a post has answered your question, please click "Accept as Solution".
SKaze
Associate II

Thanks for your reply. Please kindly find the pin connections for SPI connections.

0693W000003BYwIQAW.png

Also, the piece of the code that I am using to get gyro data is as follows. In this code, I followed the procedure explained in the Bosch GitHub, here. The problem here is that it doesn't update the gyro variable. Also, tested with the logic analyzer. Seems it is not changing on the pins.

Thanks

#include <SPI.h>
#include <bmi160.h>
struct bmi160_dev sensor;
#define A3 SS;
#define D12 MISO;
#define D11 MOSI;
#define D13 SCK;
int16_t gyro_x; 
int i;
int8_t user_spi_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t length)
{
        int8_t rslt = 0; /* Return 0 for Success, non-zero for failure*/
        reg_addr = reg_addr & 0x7F;
       //SPI.begin();
       digitalWrite(SS,LOW);
        /* send register address byte */
       rslt=SPI.transfer(reg_addr);
       delayMicroseconds(15);
             /* Communicate len number of bytes: if TX - the procedure doesn't overwrite pData */
       for (i = 0; i < length; i++)
          {
           SPI.transfer(*reg_data++);
          }
 
          // SPI.end();
          digitalWrite(SS,HIGH);
           /* return the status byte value */
           return rslt;
}
int8_t user_spi_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 */
 //Communicate len number of bytes: if RX - the procedure sends 0x00 to push bytes from slave
 
reg_addr = reg_addr | 0x80; /* For read operation, the reg_addr should be ORed with 0x80.*/
digitalWrite(SS,LOW);
 
/* send register address byte */
rslt=SPI.transfer(reg_addr);
delayMicroseconds(15);
/* Communicate len number of bytes: if RX - the procedure sends 0x00 to push bytes from slave*/
for (i = 0; i < len; i++)
{
*reg_data = SPI.transfer(0);
 /* Store pData from last pData RX */
reg_data++;
}
digitalWrite(SS,HIGH);
/* return the status byte value */
return(rslt);
}
void user_delay_ms(uint32_t period)
{
int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
delay(period);
}
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600); 
SPI.begin(); 
SPI.setClockDivider(SPI_CLOCK_DIV16);
digitalWrite(SS,LOW);   
digitalWrite(SS,HIGH);
 
sensor.id = 0;
sensor.interface = BMI160_SPI_INTF;
sensor.read = user_spi_read;
sensor.write = user_spi_write;
sensor.delay_ms = user_delay_ms;  
int8_t rslt = 0;
rslt = bmi160_init(&sensor);
   // Select the Output data rate, range of accelerometer sensor
            sensor.accel_cfg.odr = BMI160_ACCEL_ODR_1600HZ;
            sensor.accel_cfg.range = BMI160_ACCEL_RANGE_2G;
            sensor.accel_cfg.bw = BMI160_ACCEL_BW_NORMAL_AVG4;
            // Select the Output data rate, range of Gyroscope sensor
            sensor.gyro_cfg.odr = BMI160_GYRO_ODR_3200HZ;
            sensor.gyro_cfg.range = BMI160_GYRO_RANGE_2000_DPS;
            sensor.gyro_cfg.bw = BMI160_GYRO_BW_NORMAL_MODE;
 
             //Select the power mode
            sensor.accel_cfg.power = BMI160_ACCEL_NORMAL_MODE;
            sensor.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;
 
             //Set the Power mode
            rslt = bmi160_set_power_mode(&sensor);
 
             //Set the sensor configuration
            rslt = bmi160_set_sens_conf(&sensor);
}
void loop() {
  
  // put your main code here, to run repeatedly:
int8_t rslt = 0;
struct bmi160_sensor_data accel;
struct bmi160_sensor_data gyro;
/* To read both Accel and Gyro data */
bmi160_get_sensor_data((BMI160_ACCEL_SEL | BMI160_GYRO_SEL), &accel, &gyro, &sensor);
gyro_x=gyro.z;
Serial.println(gyro_x);
delay(100);
}

Would have chosen I2C myself.

How have you got it connected? Written any software for this, or expecting others too?

Some SPI interface examples in the BSP code for the example boards, try porting that into your target.

https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmi160-ds000.pdf

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Look very Arduino, not HAL

Is this not working? Is it compiling?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
TDK
Guru

> Also, tested with the logic analyzer. Seems it is not changing on the pins.

If pins are not sending the signals, ensure they're configured correctly. Look at the GPIO registers. Looks like Arduino code, which I'm not really familiar with.

If you feel a post has answered your question, please click "Accept as Solution".

Yes, this program is written in Arduino IDE. It compiles but doesn't work.

Am I following the right procedure here?

I am trying to connect the shuttle board with I2C in Arduino IDE. It mentions that the code is not compile-able in for Nucleo-32. Please see the piece of code for reference. The library of the Hanyazou is used.

#include <Wire.h>
#include <BMI160Gen.h>
 
//const int select_pin = 10;
const int i2c_addr = 0x68;
 
void setup() {
  Serial.begin(9600); // initialize Serial communication
  while (!Serial);    // wait for the serial port to open
 
  // initialize device
  //BMI160.begin(BMI160GenClass::SPI_MODE, select_pin);
  BMI160.begin(BMI160GenClass::I2C_MODE, i2c_addr);
}
 
void loop() {
  int gx, gy, gz;         // raw gyro values
 
  // read raw gyro measurements from device
  BMI160.readGyro(gx, gy, gz);
 
  // display tab-separated gyro x/y/z values
  Serial.print("g:\t");
  Serial.print(gx);
  Serial.print("\t");
  Serial.print(gy);
  Serial.print("\t");
  Serial.print(gz);
  Serial.println();
 
  delay(500);
}

>>Am I following the right procedure here?

Hard to say, I suspect the approach you're taking will take many man-days to execute.

I'd start with reading/writing the registers of the BMI160 first.

See if that were sufficient, and then decide if I needed to port all the library functionality, or just salient portions.

Either way, need to focus on the low-level stuff first for there to be any chance of getting the high-level stuff to function.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal

Got bmx055 and bmw160 working in i2c modr witg bit banging to keep bringup time brief. Bosch provide an spi and ani2c function for developper to.fill up. Then there is a.demo function code to power up, run one shot measurement and power.down the sensor. That is the one to use. Do check prrsence of the sensor by reading a chip id...