04-06-2022 05:20 AM
I followed the steps for initializing the BMI160 sensor on Cypress PSoC6 BLE pioneer kit as introduced in this video (https://youtu.be/gyWWnTfdNOQ); however, the output values are always zero regardless of the motion of the pioneer kit board.
Could someone help me figuring out what I should do to get the correct output values?
Thank you.
#include "project.h"
// Added
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>
#include "bmi160.h"
#include "bmi160_defs.h"
static struct bmi160_dev bmi160Dev; // Added
static int8_t BMI160BurstWrite(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
Cy_SCB_I2C_MasterSendReStart(I2C_1_HW, dev_addr, CY_SCB_I2C_WRITE_XFER, 0, &I2C_1_context);
Cy_SCB_I2C_MasterWriteByte(I2C_1_HW, reg_addr, 0, &I2C_1_context);
for(int i=0; i<len; i++)
{
Cy_SCB_I2C_MasterWriteByte(I2C_1_HW, data[i], 0, &I2C_1_context);
}
Cy_SCB_I2C_MasterSendStop(I2C_1_HW, 0, &I2C_1_context);
return 0;
}
// This function supports the BMP?180 library and read I2C Registers
static int8_t BMI160BurstRead(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
Cy_SCB_I2C_MasterSendReStart(I2C_1_HW, dev_addr, CY_SCB_I2C_WRITE_XFER, 0, &I2C_1_context);
Cy_SCB_I2C_MasterWriteByte(I2C_1_HW, reg_addr, 0, & I2C_1_context);
Cy_SCB_I2C_MasterSendReStart(I2C_1_HW, dev_addr, CY_SCB_I2C_WRITE_XFER, 0, &I2C_1_context);
for(int i=0; i<len-1; i++)
{
Cy_SCB_I2C_MasterReadByte(I2C_1_HW, CY_SCB_I2C_ACK, &data[i], 0, &I2C_1_context);
}
Cy_SCB_I2C_MasterReadByte(I2C_1_HW, CY_SCB_I2C_NAK, &data[len-1], 0, &I2C_1_context);
Cy_SCB_I2C_MasterSendStop(I2C_1_HW, 0, &I2C_1_context);
return 0;
}
static void bmi160Init(void)
{
vTaskDelay(100); // Wait to boot
/* BMI160 */
bmi160Dev.read = (bmi160_read_fptr_t)BMI160BurstRead;
bmi160Dev.write = (bmi160_write_fptr_t)BMI160BurstWrite;
bmi160Dev.delay_ms = (bmi160_delay_fptr_t)vTaskDelay;
bmi160Dev.id = BMI160_I2C_ADDR; // I2C device address
bmi160_init(&bmi160Dev); // Initialize the device
bmi160Dev.gyro_cfg.odr = BMI160_GYRO_ODR_800HZ;
bmi160Dev.gyro_cfg.range = BMI160_GYRO_RANGE_125_DPS;
bmi160Dev.gyro_cfg.bw = BMI160_GYRO_BW_OSR4_MODE;
/* Select the power mode of Gyroscope sensor */
bmi160Dev.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;
bmi160Dev.accel_cfg.odr = BMI160_ACCEL_ODR_1600HZ;
bmi160Dev.accel_cfg.range = BMI160_ACCEL_RANGE_2G;
bmi160Dev.accel_cfg.bw = BMI160_ACCEL_BW_OSR4_AVG1;
bmi160Dev.accel_cfg.power = BMI160_ACCEL_NORMAL_MODE;
/* Set the sensor configuration */
bmi160_set_sens_conf(&bmi160Dev);
bmi160Dev.delay_ms(50);
}
// 32768 = 2g
#define MAXACCEL (32768/2)
void motionTask(void *arg)
{
(void)arg;
I2C_1_Start();
bmi160Init();
struct bmi160_sensor_data acc;
float gx, gy, gz;
while(1)
{
bmi160_get_sensor_data(BMI160_ACCEL_ONLY, &acc, NULL, &bmi160Dev);
gx = (float)acc.x/MAXACCEL;
gy = (float)acc.y/MAXACCEL;
gz = (float)acc.z/MAXACCEL;
printf ("x=%1.2f y=%1.2f z=%1.2f\r\n", gx, gy, gz);
vTaskDelay(200);
}
}
int main(void)
{
__enable_irq(); /* Enable global interrupts. */
UART_1_Start();
xTaskCreate(motionTask, "Motion Task", 400, 0, 1, 0);
vTaskStartScheduler();
/* Place your initialization/startup code here (e.g. MyInst_Start()) */
}
/* [] END OF FILE */
04-06-2022 07:46 AM
Hi showns,
There was example code worked well on STM32 for your reference, based on BMI160 Github code: https://github.com/BoschSensortec/BMI160_driver/tree/master/examples/read_sensor_data