Files
tmr.textandClaude Opus 5.5 7c26ea5272 docs: 补回文件开头的整体说明(只加注释,程序不变)
9de485e 把旧的大段文件头换成了三行式(文件名称/功能描述/备注),但有 28 个文件没写,
旧文件头里的重要说明也大多删掉了。本提交按 lyf 的三行式格式:
- 给 28 个没有文件头的文件补上文件头(两板 Lib/Bsp 通用文件、can_task、dr16、c_comm、
  c_comm_task、chassis、gimbal、shoot、robot_typedef、app_tasks)
- 给 9de485e 新增的 hlj_pc.h、supercap.h 补上文件头
- 在 lyf 写的 21 个文件头末尾补回被删掉的重要说明:灯语与蜂鸣规则、BMI088 初始化顺序与原因、
  欧拉角映射、底盘/云台状态机与自检手势、遥控映射、裁判系统字段偏移等
- 旧说明里已过时的内容按新代码改正(5 个板间帧、底盘电机槽位、初始化顺序、CAN 发送策略、
  z 轴零偏需上电静止标定、HLJ_PC 功率控制等)
- doc/目录整理说明.md 同步更新

验证:只新增注释行,没有删除或修改任何代码行;两块板重新编译,Program Size 与 .map 符号表
(底盘 1155 个、云台 1156 个)与整理前完全一致。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 11:45:15 +08:00

154 lines
4.9 KiB
C

/*
*文件名称:ist8310.c
*功能描述:C板板载磁力计IST8310驱动(硬件I2C3版)
*备注:总线I2C3(PA8/PC9,100kHz),RSTN=PG6,初始化前须先拉高RSTN释放复位态
*配置:CNTL1连续200Hz、CNTL2 DRDY下降沿、AVGCNTL 4次平均;ist8310_poll由IMU_Task调用,内部每5ms阻塞读一次,不用DRDY中断
*报警:检测失败或连续读失败时绿灯慢闪(见monitor_task)
*/
#include "ist8310.h"
#include "i2c.h"
#include "main.h"
#include "led.h"
#define IST8310_ADDR (0x0EU << 1)//HAL 8位I2C从机地址
#define IST8310_WHO_AM_I 0x00U //设备ID寄存器
#define IST8310_CNTL1 0x0AU //控制寄存器1
#define IST8310_CNTL2 0x0BU //控制寄存器2
#define IST8310_AVGCNTL 0x41U //采样平均配置寄存器
#define IST8310_PDCNTL 0x42U //脉冲时长配置寄存器
#define IST8310_DATA_START 0x03U //磁场数据起始寄存器
#define IST8310_WHO_AM_I_VALUE 0x10U //预期WHO_AM_I值
#define IST8310_POLL_PERIOD_MS 5U //200Hz轮询周期(ms)
float ist8310_magnetic_field[3] = {0.0f};//三轴磁场强度(uT)
float ist8310_temperature = 0.0f; //温度(℃)
uint8_t ist8310_online = 0U; //在线标志
uint8_t ist8310_last_who = 0U; //最近读取到的WHO_AM_I
static uint8_t raw_buf[6] = {0}; //I2C六字节原始数据缓冲
static uint8_t read_error_count = 0U; //连续读失败计数
/*
*函数简介:控制IST8310复位引脚RSTN(PG6)电平
*参数说明:level:1=释放复位(高电平),0=拉低复位
*返回类型:无
*备注:内部函数,电平变化后延时1ms等待生效
*/
static void ist8310_rstn(uint8_t level)
{
HAL_GPIO_WritePin(RSTN_IST8310_GPIO_Port, RSTN_IST8310_Pin,
level ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_Delay(1);
}
/*
*函数简介:向IST8310指定寄存器写入单字节
*参数说明:reg:寄存器地址
*参数说明:val:写入值
*返回类型:无
*备注:内部函数
*/
static void ist8310_write_reg(uint8_t reg, uint8_t val)
{
(void)HAL_I2C_Mem_Write(&hi2c3, IST8310_ADDR, reg,
I2C_MEMADD_SIZE_8BIT, &val, 1U, 10U);
}
/*
*函数简介:读取IST8310 WHO_AM_I寄存器
*参数说明:无
*返回类型:uint8_t:WHO_AM_I值,读取失败返回0
*备注:内部函数
*/
static uint8_t ist8310_read_who(void)
{
uint8_t who = 0U;
if (HAL_I2C_Mem_Read(&hi2c3, IST8310_ADDR, IST8310_WHO_AM_I,
I2C_MEMADD_SIZE_8BIT, &who, 1U, 10U) != HAL_OK)
return 0U;
return who;
}
/*
*函数简介:ID校验失败时的绿灯慢闪报警延时
*参数说明:无
*返回类型:无
*备注:内部函数,由ist8310_init重试循环调用
*/
static void ist8310_error_blink(void)
{
static uint8_t counter = 0U;
counter++;
if (counter == 1U)
led_set_green(1);
else if (counter == 5U)
led_set_green(0);
else if (counter == 40U)
counter = 0U;
HAL_Delay(25);
}
/*
*函数简介:初始化IST8310磁力计(硬件I2C3)
*参数说明:无
*返回类型:无
*备注:先拉高RSTN解除CubeMX默认低电平复位态,有界重试40次
*/
void ist8310_init(void)
{
HAL_GPIO_WritePin(RSTN_IST8310_GPIO_Port, RSTN_IST8310_Pin, GPIO_PIN_SET);
HAL_Delay(2);
for (uint8_t retry = 0U; retry < 40U; retry++)
{
ist8310_last_who = ist8310_read_who();
if (ist8310_last_who == IST8310_WHO_AM_I_VALUE)
{
ist8310_online = 1U;
ist8310_write_reg(IST8310_CNTL1, 0x03U); //连续模式200Hz
ist8310_write_reg(IST8310_CNTL2, 0x08U); //DRDY下降沿
ist8310_write_reg(IST8310_AVGCNTL, 0x12U);//4次采样平均
ist8310_write_reg(IST8310_PDCNTL, 0xC0U); //正常脉冲时长
ist8310_rstn(0U);
ist8310_rstn(1U);
return;
}
ist8310_error_blink();
}
}
/*
*函数简介:周期轮询读取IST8310三轴磁场数据
*参数说明:无
*返回类型:无
*备注:连续100次读取失败判定离线并置ist8310_online=0
*/
void ist8310_poll(void)
{
static uint32_t last_poll_tick = 0U;
uint32_t now = HAL_GetTick();
if (now - last_poll_tick < IST8310_POLL_PERIOD_MS)
return;
last_poll_tick = now;
if (!ist8310_online)
return;
if (HAL_I2C_Mem_Read(&hi2c3, IST8310_ADDR, IST8310_DATA_START,
I2C_MEMADD_SIZE_8BIT, raw_buf, 6U, 2U) != HAL_OK)
{
if (++read_error_count > 100U)
ist8310_online = 0U;
return;
}
read_error_count = 0U;
int16_t raw_x = (int16_t)(((uint16_t)raw_buf[1] << 8) | raw_buf[0]);
int16_t raw_y = (int16_t)(((uint16_t)raw_buf[3] << 8) | raw_buf[2]);
int16_t raw_z = (int16_t)(((uint16_t)raw_buf[5] << 8) | raw_buf[4]);
ist8310_magnetic_field[0] = (float)raw_x * 0.3f;
ist8310_magnetic_field[1] = (float)raw_y * 0.3f;
ist8310_magnetic_field[2] = (float)raw_z * 0.3f;
}