STM_ATEM/User/data_packet.h
zhoujie bc37e14fba feat(optic_mag): 集成光泵磁力仪驱动并重构数据包架构
- 新增光泵磁力仪驱动模块,通过 USART2 中断接收 BCD 编码数据,采样率 115200bps
- 重构数据包架构:引入标准包与扩展包(含光泵数据)两种类型,通过帧头魔数区分
- 新增 DataPacketWithOptic_t、CorrectedDataPacketWithOptic_t 两种扩展数据包类型
- 数据存储改为通用字节流写入(方案Y),支持任意包类型混流存储
- 将编译期配置集中到 app_config.h,包括 UART 输出、SD 存储、GPS 位置等开关
- 移除 ADC_SYNC GPIO 引脚配置,释放 PA2 用于 USART2_TX
- 主循环 ProcessAdcData 改为按需选择数据包类型,光泵数据快照在 ADC 中断前完成
- 新增 USART2 错误回调处理,支持接收异常时自动恢复
2026-06-07 22:50:54 +08:00

111 lines
4.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef DATA_PACKET_H
#define DATA_PACKET_H
#include <stdint.h>
#include "app_config.h"
// 帧同步魔数高3字节固定低字节为类型标志
#define PACKET_TYPE_STANDARD 0xFFFFFFFFUL // 标准包(无光泵)
#define PACKET_TYPE_WITH_OPTIC 0xFFFFFFFEUL // 扩展包(含光泵,末尾追加 optical_mag
#define PACKET_SYNC_MASK 0xFFFFFF00UL // 同步检测掩码
// 兼容旧代码
#define PACKET_START_BYTE PACKET_TYPE_STANDARD
// GPS位置字段由 app_config.h 中 CFG_ENABLE_GPS_POSITION 控制
#if CFG_ENABLE_GPS_POSITION
#define ENABLE_GPS_POSITION
#endif
// ─── 原始ADC数据包 ────────────────────────────────────────────────────────────
typedef struct __attribute__((packed)) {
uint32_t start_byte; // PACKET_TYPE_STANDARD
int32_t adc_data1;
int32_t adc_data2;
int32_t adc_data3;
uint32_t gps_time; // HHMMSS格式始终存在
#ifdef ENABLE_GPS_POSITION
float gps_latitude;
float gps_longitude;
float gps_altitude;
#endif
} DataPacket_t;
typedef struct __attribute__((packed)) {
uint32_t start_byte; // PACKET_TYPE_WITH_OPTIC
int32_t adc_data1;
int32_t adc_data2;
int32_t adc_data3;
uint32_t gps_time;
#ifdef ENABLE_GPS_POSITION
float gps_latitude;
float gps_longitude;
float gps_altitude;
#endif
uint32_t optical_mag; // 单位 0.0001 nT例如 123456789 = 12345.6789 nT
} DataPacketWithOptic_t;
// ─── 校正数据包 ───────────────────────────────────────────────────────────────
typedef struct __attribute__((packed)) {
uint32_t start_byte; // PACKET_TYPE_STANDARD
float corrected_x;
float corrected_y;
float corrected_z;
uint32_t gps_time;
#ifdef ENABLE_GPS_POSITION
float gps_latitude;
float gps_longitude;
float gps_altitude;
#endif
} CorrectedDataPacket_t;
// 与 CorrectedDataPacket_t 相同,保留别名兼容旧代码
typedef CorrectedDataPacket_t CorrectedDataPacketWithGPS_t;
typedef struct __attribute__((packed)) {
uint32_t start_byte; // PACKET_TYPE_WITH_OPTIC
float corrected_x;
float corrected_y;
float corrected_z;
uint32_t gps_time;
#ifdef ENABLE_GPS_POSITION
float gps_latitude;
float gps_longitude;
float gps_altitude;
#endif
uint32_t optical_mag; // 单位 0.0001 nT
} CorrectedDataPacketWithOptic_t;
// ─── 函数声明 ─────────────────────────────────────────────────────────────────
uint16_t Calculate_CRC16(const uint8_t *data, uint16_t len);
void PackData(DataPacket_t *packet,
int32_t adc1, int32_t adc2, int32_t adc3,
uint32_t gps_time, float latitude, float longitude, float altitude);
void PackDataWithOptic(DataPacketWithOptic_t *packet,
int32_t adc1, int32_t adc2, int32_t adc3,
uint32_t gps_time, float latitude, float longitude, float altitude,
uint32_t optical_mag);
void PackCorrectedData(CorrectedDataPacket_t *packet,
float x, float y, float z,
uint32_t gps_time, float latitude, float longitude, float altitude);
void PackCorrectedDataWithGPS(CorrectedDataPacketWithGPS_t *packet,
float x, float y, float z,
uint32_t gps_time, float latitude, float longitude, float altitude);
void PackCorrectedDataWithOptic(CorrectedDataPacketWithOptic_t *packet,
float x, float y, float z,
uint32_t gps_time, float latitude, float longitude, float altitude,
uint32_t optical_mag);
uint8_t ValidatePacket(const DataPacket_t *packet);
uint8_t ValidateCorrectedPacket(const CorrectedDataPacket_t *packet);
#endif // DATA_PACKET_H