c
复制代码
void BMI160_Init(void)
{
uint16_t chipID = BMI323_read(BMI160_REG_CHIP_ID);
debug("BMI323芯片ID为0x%x;", chipID);
if (chipID != 0x43)
{
debug("未检测到BMI323;");
}
else
debug("检测到陀螺仪BMI323;");
u8 buf_config[2] = {0xC9, 0x70};
BMI160_WriteBytes(0x21, buf_config, 2);
uint16_t state = BMI323_read(0x02);
debug("BMI323传感器状态为0x%x;", state);
prev_yaw = agv_currentAngle.f;
}
uint16_t BMI323_read(uint8_t reg)
{
uint16_t value;
HAL_GPIO_WritePin(BMI160_CS_GPIO_Port, BMI160_CS_Pin, GPIO_PIN_RESET);
SPI_TransferByte(reg | 0x80);
value = SPI_TransferByte(0) << 8; /* 读取高8位字节 */
value |= SPI_TransferByte(0); /* 读取低8位字节 */
HAL_GPIO_WritePin(BMI160_CS_GPIO_Port, BMI160_CS_Pin, GPIO_PIN_SET);
return value;
}
uint8_t SPI_TransferByte(uint8_t data)
{
uint8_t rxData;
HAL_SPI_TransmitReceive(&hspi2, &data, &rxData, 1, HAL_MAX_DELAY);
return rxData;
}