refactor: 哨兵分支整理目录结构,按功能归类(只搬家,程序不变)
基于战队 main 9de485e。两块板统一改为三层目录,Keil 工程分组与文件夹一一对应:
- App/ 本板业务,按功能分文件夹(system/ cmd/ comm/ referee/,
云台另有 gimbal/ shoot/ vision/,底盘另有 chassis/,含 hlj_pc、supercap)
- Lib/ 与业务无关的通用模块(algorithm/ motor/ imu/ sensor/ dev/),两板逐字节相同
- Bsp/ bsp_can、bsp_usart,不再分 Inc/Src
具体改动:
- 107 个文件全部 git mv(纯重命名,内容不变),文件名不变,#include 不用改
- .uvprojx:Application/Task、Modules、BSP 三个分组换成新分组,登记文件集合不变;
头文件路径换成新目录,CubeMX/第三方目录不动
- .uvoptx 分组同步,文件序号重新连续编号
- 根 README 和两块板 README 的路径、代码地图、模块清单同步更新(补 hlj_pc、supercap)
- 新增 doc/目录整理说明.md(新旧路径对照、验证方法、以后如何跟进步兵更新)
验证(整理前后同一编译器):
- 两块板 Program Size 完全相同
底盘 Code=111184 RO=1956 RW=432 ZI=62720
云台 Code=109876 RO=1960 RW=420 ZI=62764
- .map 符号表(名字/类型/大小/所在 .o)底盘 1155 个、云台 1156 个,逐条一致
未改动:Core/、Drivers/、Middlewares/、USB_DEVICE/、CubeMX 分组名与任务名、module_test/
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9de485e4ef
commit
4b75e4ec6e
115 files changed
+1200
-818
No files matched your search
@@ -0,0 +1,536 @@
|
||||
/*
|
||||
*文件名称:bmi088.c
|
||||
*功能描述:C板板载六轴IMU BMI088驱动(SPI1模式3+DMA读取+EXTI数据就绪+健康自恢复)
|
||||
*备注:量程±3g/±2000dps,陀螺1kHz输出
|
||||
*/
|
||||
#include "bmi088.h"
|
||||
#include "main.h"
|
||||
#include "spi.h"
|
||||
#include "led.h"
|
||||
|
||||
/* 加速度计寄存器地址 */
|
||||
#define BMI088_ACC_CHIP_ID 0x00U//加速度计芯片ID寄存器
|
||||
#define BMI088_ACC_PWR_CTRL 0x7DU//电源控制寄存器
|
||||
#define BMI088_ACC_PWR_CONF 0x7CU//电源模式寄存器
|
||||
#define BMI088_ACC_CONF 0x40U//输出速率与滤波配置
|
||||
#define BMI088_ACC_RANGE 0x41U//量程配置
|
||||
#define BMI088_INT1_IO_CONF 0x53U//INT1引脚配置
|
||||
#define BMI088_INT1_INT2_MAP_DATA 0x58U//数据就绪中断映射
|
||||
#define BMI088_ACC_SOFTRESET 0x7EU//加速度计软复位寄存器
|
||||
#define BMI088_ACC_DATA_START 0x12U//加速度数据起始寄存器
|
||||
#define BMI088_ACC_TEMP_MSB 0x22U//温度数据起始寄存器
|
||||
|
||||
/* 陀螺仪寄存器地址 */
|
||||
#define BMI088_GYRO_CHIP_ID 0x00U//陀螺仪芯片ID寄存器
|
||||
#define BMI088_GYRO_RANGE 0x0FU//量程配置
|
||||
#define BMI088_GYRO_BANDWIDTH 0x10U//带宽与ODR配置
|
||||
#define BMI088_GYRO_LPM1 0x11U//电源模式寄存器
|
||||
#define BMI088_GYRO_INT_CTRL 0x15U//中断使能控制
|
||||
#define BMI088_INT3_INT4_IO_CONF 0x16U//INT3/4引脚配置
|
||||
#define BMI088_INT3_INT4_IO_MAP 0x18U//INT3/4中断映射
|
||||
#define BMI088_GYRO_SOFTRESET 0x14U//陀螺仪软复位寄存器
|
||||
#define BMI088_GYRO_DATA_START 0x02U//角速度数据起始寄存器
|
||||
|
||||
#define BMI088_ACC_ID_VALUE 0x1EU//加速度计预期ID
|
||||
#define BMI088_GYRO_ID_VALUE 0x0FU//陀螺仪预期ID
|
||||
#define BMI088_STALL_MS 5U //停摆判定超时(ms)
|
||||
|
||||
#define BMI088_CS_ACC(x) HAL_GPIO_WritePin(CS1_ACCEL_GPIO_Port, CS1_ACCEL_Pin, (x) ? GPIO_PIN_SET : GPIO_PIN_RESET)
|
||||
#define BMI088_CS_GYRO(x) HAL_GPIO_WritePin(CS1_GYRO_GPIO_Port, CS1_GYRO_Pin, (x) ? GPIO_PIN_SET : GPIO_PIN_RESET)
|
||||
|
||||
extern SPI_HandleTypeDef hspi1;//SPI1句柄
|
||||
|
||||
float bmi088_accel[3] = {0.0f}; //三轴加速度(m/s^2)
|
||||
float bmi088_gyro[3] = {0.0f}; //三轴角速度(rad/s)
|
||||
float bmi088_temperature = 0.0f; //芯片温度(℃)
|
||||
uint8_t bmi088_online = 0U; //在线标志(1=在线,0=离线)
|
||||
uint8_t bmi088_last_acc_id = 0U; //最近读到的加速度计ID
|
||||
uint8_t bmi088_last_gyro_id = 0U; //最近读到的陀螺仪ID
|
||||
volatile uint32_t bmi088_accel_cnt = 0U; //诊断:加速度计采样计数
|
||||
volatile uint32_t bmi088_gyro_cnt = 0U; //诊断:陀螺仪采样计数
|
||||
volatile uint32_t bmi088_spi_errors = 0U;//诊断:SPI错误计数
|
||||
|
||||
static uint8_t spi_tx_buf[8];//SPI DMA发送缓冲
|
||||
static uint8_t spi_rx_buf[8];//SPI DMA接收缓冲
|
||||
|
||||
/* 当前SPI DMA读取阶段枚举 */
|
||||
typedef enum
|
||||
{
|
||||
BMI088_PHASE_NONE = 0,//空闲
|
||||
BMI088_PHASE_ACCEL, //正在读取加速度
|
||||
BMI088_PHASE_TEMP, //正在读取温度
|
||||
BMI088_PHASE_GYRO, //正在读取角速度
|
||||
} bmi088_phase_e;
|
||||
|
||||
static volatile bmi088_phase_e read_phase = BMI088_PHASE_NONE;//当前DMA读取阶段
|
||||
static volatile uint32_t read_phase_tick = 0U; //当前阶段启动时间戳(ms)
|
||||
|
||||
/*
|
||||
*函数简介:释放指定读取阶段对应的SPI片选引脚
|
||||
*参数说明:phase:读取阶段枚举
|
||||
*返回类型:无
|
||||
*备注:内部函数
|
||||
*/
|
||||
static void cs_release(bmi088_phase_e phase)
|
||||
{
|
||||
if (phase == BMI088_PHASE_ACCEL || phase == BMI088_PHASE_TEMP)
|
||||
BMI088_CS_ACC(1);
|
||||
else
|
||||
BMI088_CS_GYRO(1);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:向加速度计写入单字节寄存器
|
||||
*参数说明:reg:寄存器地址
|
||||
*参数说明:data:写入值
|
||||
*返回类型:无
|
||||
*备注:阻塞式SPI收发
|
||||
*/
|
||||
static void accel_write_reg(uint8_t reg, uint8_t data)
|
||||
{
|
||||
uint8_t tx[2] = {(uint8_t)(reg & 0x7FU), data};
|
||||
BMI088_CS_ACC(0);
|
||||
(void)HAL_SPI_TransmitReceive(&hspi1, tx, spi_rx_buf, 2U, 10U);
|
||||
BMI088_CS_ACC(1);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:从加速度计读取单字节寄存器
|
||||
*参数说明:reg:寄存器地址
|
||||
*返回类型:uint8_t:读取到的寄存器值
|
||||
*备注:加速度计首字节回读为无效值,有效数据在rx[2]
|
||||
*/
|
||||
static uint8_t accel_read_reg(uint8_t reg)
|
||||
{
|
||||
uint8_t tx[3] = {(uint8_t)(reg | 0x80U), 0x00U, 0x00U};
|
||||
uint8_t rx[3] = {0};
|
||||
BMI088_CS_ACC(0);
|
||||
(void)HAL_SPI_TransmitReceive(&hspi1, tx, rx, 3U, 10U);
|
||||
BMI088_CS_ACC(1);
|
||||
return rx[2];
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:向陀螺仪写入单字节寄存器
|
||||
*参数说明:reg:寄存器地址
|
||||
*参数说明:data:写入值
|
||||
*返回类型:无
|
||||
*备注:阻塞式SPI收发
|
||||
*/
|
||||
static void gyro_write_reg(uint8_t reg, uint8_t data)
|
||||
{
|
||||
uint8_t tx[2] = {(uint8_t)(reg & 0x7FU), data};
|
||||
BMI088_CS_GYRO(0);
|
||||
(void)HAL_SPI_TransmitReceive(&hspi1, tx, spi_rx_buf, 2U, 10U);
|
||||
BMI088_CS_GYRO(1);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:从陀螺仪读取单字节寄存器
|
||||
*参数说明:reg:寄存器地址
|
||||
*返回类型:uint8_t:读取到的寄存器值
|
||||
*备注:阻塞式SPI收发
|
||||
*/
|
||||
static uint8_t gyro_read_reg(uint8_t reg)
|
||||
{
|
||||
uint8_t tx[2] = {(uint8_t)(reg | 0x80U), 0x00U};
|
||||
uint8_t rx[2] = {0};
|
||||
BMI088_CS_GYRO(0);
|
||||
(void)HAL_SPI_TransmitReceive(&hspi1, tx, rx, 2U, 10U);
|
||||
BMI088_CS_GYRO(1);
|
||||
return rx[1];
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:解析spi_rx_buf中的三轴加速度原始数据
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:量程±3g,换算系数(3/32768)*9.8
|
||||
*/
|
||||
static void process_accel(void)
|
||||
{
|
||||
int16_t raw[3];
|
||||
raw[0] = (int16_t)(((uint16_t)spi_rx_buf[3] << 8) | spi_rx_buf[2]);
|
||||
raw[1] = (int16_t)(((uint16_t)spi_rx_buf[5] << 8) | spi_rx_buf[4]);
|
||||
raw[2] = (int16_t)(((uint16_t)spi_rx_buf[7] << 8) | spi_rx_buf[6]);
|
||||
bmi088_accel[0] = (float)raw[0] * 0.0008974358974f;
|
||||
bmi088_accel[1] = (float)raw[1] * 0.0008974358974f;
|
||||
bmi088_accel[2] = (float)raw[2] * 0.0008974358974f;
|
||||
bmi088_accel_cnt++;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:解析spi_rx_buf中的11位温度原始数据
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:无
|
||||
*/
|
||||
static void process_temperature(void)
|
||||
{
|
||||
uint16_t raw = (uint16_t)(((uint16_t)spi_rx_buf[2] << 3) | (spi_rx_buf[3] >> 5));
|
||||
int16_t temp = (raw > 1023U) ? (int16_t)(raw - 2048U) : (int16_t)raw;
|
||||
bmi088_temperature = (float)temp * 0.125f + 23.0f;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:解析spi_rx_buf中的三轴角速度原始数据
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:量程±2000dps,换算为rad/s
|
||||
*/
|
||||
static void process_gyro(void)
|
||||
{
|
||||
int16_t raw[3];
|
||||
raw[0] = (int16_t)(((uint16_t)spi_rx_buf[2] << 8) | spi_rx_buf[1]);
|
||||
raw[1] = (int16_t)(((uint16_t)spi_rx_buf[4] << 8) | spi_rx_buf[3]);
|
||||
raw[2] = (int16_t)(((uint16_t)spi_rx_buf[6] << 8) | spi_rx_buf[5]);
|
||||
bmi088_gyro[0] = (float)raw[0] * 0.0010652644360316952f;
|
||||
bmi088_gyro[1] = (float)raw[1] * 0.0010652644360316952f;
|
||||
bmi088_gyro[2] = (float)raw[2] * 0.0010652644360316952f;
|
||||
bmi088_gyro_cnt++;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:启动一次SPI DMA传感器数据读取
|
||||
*参数说明:phase:读取阶段枚举
|
||||
*参数说明:reg:起始寄存器地址
|
||||
*参数说明:len:读取字节数(含地址字节,不超过8)
|
||||
*返回类型:无
|
||||
*备注:SPI非READY时直接返回;DMA启动失败时立即回滚片选与read_phase
|
||||
*/
|
||||
static void start_dma_read(bmi088_phase_e phase, uint8_t reg, uint8_t len)
|
||||
{
|
||||
if (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY || len == 0U || len > 8U)
|
||||
return;
|
||||
|
||||
read_phase = phase;
|
||||
read_phase_tick = HAL_GetTick();
|
||||
spi_tx_buf[0] = (uint8_t)(reg | 0x80U);
|
||||
for (uint8_t i = 1U; i < len; i++)
|
||||
spi_tx_buf[i] = 0x00U;
|
||||
|
||||
if (phase == BMI088_PHASE_ACCEL || phase == BMI088_PHASE_TEMP)
|
||||
BMI088_CS_ACC(0);
|
||||
else
|
||||
BMI088_CS_GYRO(0);
|
||||
|
||||
if (HAL_SPI_TransmitReceive_DMA(&hspi1, spi_tx_buf, spi_rx_buf, len) != HAL_OK)
|
||||
{
|
||||
cs_release(phase);
|
||||
read_phase = BMI088_PHASE_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:ID校验失败时的绿灯双闪报警延时
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:内部函数,由bmi088_init重试循环调用
|
||||
*/
|
||||
static void error_blink(void)
|
||||
{
|
||||
static uint8_t counter = 0U;
|
||||
counter++;
|
||||
if (counter == 1U || counter == 13U)
|
||||
led_set_green(1);
|
||||
else if (counter == 5U || counter == 17U)
|
||||
led_set_green(0);
|
||||
else if (counter == 40U)
|
||||
counter = 0U;
|
||||
HAL_Delay(25);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:按legacy序列完成加速度计与陀螺仪软复位及工作寄存器配置
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:内部函数,由bmi088_init在ID检测成功后调用
|
||||
*/
|
||||
static void bmi088_config_registers(void)
|
||||
{
|
||||
accel_write_reg(BMI088_ACC_SOFTRESET, 0xB6U);
|
||||
HAL_Delay(1);
|
||||
gyro_write_reg(BMI088_GYRO_SOFTRESET, 0xB6U);
|
||||
HAL_Delay(30);
|
||||
|
||||
(void)accel_read_reg(BMI088_ACC_CHIP_ID);//软复位后首笔读写丢弃
|
||||
(void)gyro_read_reg(BMI088_GYRO_CHIP_ID);
|
||||
|
||||
accel_write_reg(BMI088_ACC_PWR_CTRL, 0x04U);
|
||||
HAL_Delay(1);
|
||||
accel_write_reg(BMI088_ACC_PWR_CONF, 0x00U);
|
||||
HAL_Delay(1);
|
||||
accel_write_reg(BMI088_ACC_CONF, 0xABU); //800Hz ODR,正常低通
|
||||
HAL_Delay(1);
|
||||
accel_write_reg(BMI088_ACC_RANGE, 0x00U); //±3g
|
||||
HAL_Delay(1);
|
||||
accel_write_reg(BMI088_INT1_IO_CONF, 0x08U); //INT1推挽,低有效
|
||||
HAL_Delay(1);
|
||||
accel_write_reg(BMI088_INT1_INT2_MAP_DATA, 0x04U); //INT1=数据就绪
|
||||
HAL_Delay(1);
|
||||
|
||||
gyro_write_reg(BMI088_GYRO_RANGE, 0x00U); //±2000°/s
|
||||
HAL_Delay(1);
|
||||
gyro_write_reg(BMI088_GYRO_BANDWIDTH, 0x82U); //116Hz带宽,1kHz ODR
|
||||
HAL_Delay(1);
|
||||
gyro_write_reg(BMI088_GYRO_LPM1, 0x00U); //正常模式
|
||||
HAL_Delay(1);
|
||||
gyro_write_reg(BMI088_GYRO_INT_CTRL, 0x80U); //使能数据就绪中断
|
||||
HAL_Delay(1);
|
||||
gyro_write_reg(BMI088_INT3_INT4_IO_CONF, 0x00U); //INT3推挽,低有效
|
||||
HAL_Delay(1);
|
||||
gyro_write_reg(BMI088_INT3_INT4_IO_MAP, 0x01U); //INT3=数据就绪
|
||||
HAL_Delay(1);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:初始化BMI088(重配SPI1模式3/16分频、ID检测与寄存器配置)
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:初始化期间先屏蔽EXTI4/EXTI9_5,配置完成后清标志并放行
|
||||
*/
|
||||
void bmi088_init(void)
|
||||
{
|
||||
HAL_NVIC_DisableIRQ(EXTI4_IRQn);
|
||||
HAL_NVIC_DisableIRQ(EXTI9_5_IRQn);
|
||||
|
||||
/* 重配SPI1为模式3/16分频(5.25MHz),满足BMI088时序要求 */
|
||||
HAL_SPI_DeInit(&hspi1);
|
||||
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
|
||||
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
|
||||
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
|
||||
if (HAL_SPI_Init(&hspi1) != HAL_OK)
|
||||
return;
|
||||
|
||||
BMI088_CS_ACC(1);
|
||||
BMI088_CS_GYRO(1);
|
||||
|
||||
for (uint8_t retry = 0U; retry < 40U; retry++)
|
||||
{
|
||||
bmi088_last_acc_id = accel_read_reg(BMI088_ACC_CHIP_ID);
|
||||
bmi088_last_gyro_id = gyro_read_reg(BMI088_GYRO_CHIP_ID);
|
||||
if (bmi088_last_acc_id == BMI088_ACC_ID_VALUE &&
|
||||
bmi088_last_gyro_id == BMI088_GYRO_ID_VALUE)
|
||||
{
|
||||
bmi088_online = 1U;
|
||||
bmi088_config_registers();
|
||||
break;
|
||||
}
|
||||
error_blink();
|
||||
}
|
||||
|
||||
if (bmi088_online)
|
||||
{
|
||||
__HAL_GPIO_EXTI_CLEAR_IT(INT1_ACCEL_Pin);
|
||||
__HAL_GPIO_EXTI_CLEAR_IT(INT1_GYRO_Pin);
|
||||
HAL_NVIC_EnableIRQ(EXTI4_IRQn);
|
||||
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:GPIO外部中断回调(EXTI4加速度就绪/EXTI5陀螺仪就绪)
|
||||
*参数说明:GPIO_Pin:触发中断的引脚掩码
|
||||
*返回类型:无
|
||||
*备注:含20ms超时兜底,防止SPI异常丢回调导致永久冻结
|
||||
*/
|
||||
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
|
||||
{
|
||||
if (!bmi088_online)
|
||||
return;
|
||||
|
||||
if (read_phase != BMI088_PHASE_NONE && (HAL_GetTick() - read_phase_tick) > 20U)
|
||||
{
|
||||
cs_release(read_phase);
|
||||
read_phase = BMI088_PHASE_NONE;
|
||||
}
|
||||
|
||||
if (GPIO_Pin == INT1_ACCEL_Pin)
|
||||
{
|
||||
if (read_phase == BMI088_PHASE_NONE)
|
||||
start_dma_read(BMI088_PHASE_ACCEL, BMI088_ACC_DATA_START, 8U);
|
||||
}
|
||||
else if (GPIO_Pin == INT1_GYRO_Pin)
|
||||
{
|
||||
if (read_phase == BMI088_PHASE_NONE)
|
||||
start_dma_read(BMI088_PHASE_GYRO, BMI088_GYRO_DATA_START, 7U);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:SPI全双工DMA收发完成回调
|
||||
*参数说明:hspi:SPI句柄指针
|
||||
*返回类型:无
|
||||
*备注:加速度读取完成后链式启动温度读取
|
||||
*/
|
||||
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef * hspi)
|
||||
{
|
||||
if (hspi == NULL || hspi->Instance != SPI1)
|
||||
return;
|
||||
|
||||
switch (read_phase)
|
||||
{
|
||||
case BMI088_PHASE_ACCEL:
|
||||
BMI088_CS_ACC(1);
|
||||
process_accel();
|
||||
read_phase = BMI088_PHASE_NONE;
|
||||
start_dma_read(BMI088_PHASE_TEMP, BMI088_ACC_TEMP_MSB, 4U);
|
||||
break;
|
||||
|
||||
case BMI088_PHASE_TEMP:
|
||||
BMI088_CS_ACC(1);
|
||||
process_temperature();
|
||||
read_phase = BMI088_PHASE_NONE;
|
||||
break;
|
||||
|
||||
case BMI088_PHASE_GYRO:
|
||||
BMI088_CS_GYRO(1);
|
||||
process_gyro();
|
||||
read_phase = BMI088_PHASE_NONE;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:获取SPI1当前HAL状态码
|
||||
*参数说明:无
|
||||
*返回类型:uint8_t:HAL_SPI_StateTypeDef枚举值(1=READY)
|
||||
*备注:无
|
||||
*/
|
||||
uint8_t bmi088_spi_state(void)
|
||||
{
|
||||
return (uint8_t)HAL_SPI_GetState(&hspi1);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:执行一次阻塞式SPI读取并拷贝至spi_rx_buf
|
||||
*参数说明:is_accel:1=加速度计片选,0=陀螺仪片选
|
||||
*参数说明:reg:起始寄存器地址
|
||||
*参数说明:len:读取字节数(不超过8)
|
||||
*返回类型:无
|
||||
*备注:仅在EXTI已关闭的恢复路径中使用
|
||||
*/
|
||||
static void bmi088_blocking_xfer(uint8_t is_accel, uint8_t reg, uint8_t len)
|
||||
{
|
||||
uint8_t tx[8] = {0};
|
||||
uint8_t rx[8] = {0};
|
||||
|
||||
if (len == 0U || len > 8U)
|
||||
return;
|
||||
|
||||
tx[0] = (uint8_t)(reg | 0x80U);
|
||||
if (is_accel)
|
||||
BMI088_CS_ACC(0);
|
||||
else
|
||||
BMI088_CS_GYRO(0);
|
||||
|
||||
(void)HAL_SPI_TransmitReceive(&hspi1, tx, rx, len, 10U);
|
||||
|
||||
if (is_accel)
|
||||
BMI088_CS_ACC(1);
|
||||
else
|
||||
BMI088_CS_GYRO(1);
|
||||
|
||||
for (uint8_t i = 0U; i < len; i++)
|
||||
spi_rx_buf[i] = rx[i];
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:阻塞式补读一次加速度计与陀螺仪数据
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:同时清除传感器DRDY锁存电平
|
||||
*/
|
||||
static void bmi088_read_blocking(void)
|
||||
{
|
||||
bmi088_blocking_xfer(1U, BMI088_ACC_DATA_START, 8U);
|
||||
process_accel();
|
||||
|
||||
bmi088_blocking_xfer(0U, BMI088_GYRO_DATA_START, 7U);
|
||||
process_gyro();
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:重启BMI088中断数据链路
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:复位相位/片选,必要时abort或重建SPI1,清EXTI标志并阻塞补读一拍
|
||||
*/
|
||||
static void bmi088_recover(void)
|
||||
{
|
||||
HAL_NVIC_DisableIRQ(EXTI4_IRQn);
|
||||
HAL_NVIC_DisableIRQ(EXTI9_5_IRQn);
|
||||
|
||||
cs_release(read_phase);
|
||||
read_phase = BMI088_PHASE_NONE;
|
||||
|
||||
if (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY)
|
||||
(void)HAL_SPI_Abort(&hspi1);
|
||||
if (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY)
|
||||
{
|
||||
HAL_SPI_DeInit(&hspi1);
|
||||
(void)HAL_SPI_Init(&hspi1);
|
||||
}
|
||||
|
||||
__HAL_GPIO_EXTI_CLEAR_IT(INT1_ACCEL_Pin);
|
||||
__HAL_GPIO_EXTI_CLEAR_IT(INT1_GYRO_Pin);
|
||||
|
||||
bmi088_read_blocking();
|
||||
|
||||
HAL_NVIC_EnableIRQ(EXTI4_IRQn);
|
||||
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:BMI088数据路健康检查与自恢复
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:超过BMI088_STALL_MS无新样本时触发bmi088_recover
|
||||
*/
|
||||
void bmi088_health_check(void)
|
||||
{
|
||||
static uint32_t last_cnt = 0U;
|
||||
static uint32_t stall_since = 0U;
|
||||
const uint32_t now = HAL_GetTick();
|
||||
|
||||
if (!bmi088_online)
|
||||
return;
|
||||
|
||||
if (bmi088_gyro_cnt != last_cnt)
|
||||
{
|
||||
last_cnt = bmi088_gyro_cnt;
|
||||
stall_since = 0U;
|
||||
return;
|
||||
}
|
||||
|
||||
if (stall_since == 0U)
|
||||
{
|
||||
stall_since = now;
|
||||
return;
|
||||
}
|
||||
if (now - stall_since < BMI088_STALL_MS)
|
||||
return;
|
||||
|
||||
bmi088_recover();
|
||||
last_cnt = bmi088_gyro_cnt;
|
||||
stall_since = now;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:SPI错误回调(复位片选与读取阶段并记录错误计数)
|
||||
*参数说明:hspi:SPI句柄指针
|
||||
*返回类型:无
|
||||
*备注:防止SPI出错后read_phase永久停在非NONE态
|
||||
*/
|
||||
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef * hspi)
|
||||
{
|
||||
if (hspi == NULL || hspi->Instance != SPI1)
|
||||
return;
|
||||
|
||||
cs_release(read_phase);
|
||||
read_phase = BMI088_PHASE_NONE;
|
||||
bmi088_spi_errors++;
|
||||
}
|
||||
Reference in new issue
Block a user