Skip to main content
SKaze
Associate II
July 28, 2020
Question

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

  • July 28, 2020
  • 5 replies
  • 4162 views

..

This topic has been closed for replies.

5 replies

TDK
July 28, 2020

What have you tried? Where are you getting stuck?

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

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);
}

Tesla DeLorean
Guru
July 28, 2020

Look very Arduino, not HAL

Is this not working? Is it compiling?

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
July 28, 2020

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 VenmoUp vote any posts that you find helpful, it shows what's working..
SKaze
SKazeAuthor
Associate II
July 30, 2020

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);
}

SKaze
SKazeAuthor
Associate II
July 30, 2020

@Community member​ , Here I am going to use the wire.h library written for i2c connection. Also the pins used for SDA and SCK are as follows:

Is it true to use these two pins for i2c?

The problem I am getting is "error compiling"0693W000003Bm4UQAS.jpg

Thanks

TDK
July 28, 2020

> 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""."
S.Ma
Principal
July 30, 2020

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...

Tesla DeLorean
Guru
July 30, 2020

You might want to edit that when you get home, guessing you're on a phone, or having a stroke, or drunk

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
July 30, 2020
Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..