✨ feat(monitor): 增强系统监控与数据存储功能
- 新增数据存储缓冲区可用性检查,防止缓冲区满时数据丢失 - 新增会话文件夹管理功能,每次上电自动创建新的数据存储文件夹 - 新增监控状态定期保存功能,将系统统计信息写入MONITOR.TXT文件 - 新增数据丢弃统计,记录因缓冲区满而未存储的数据包数量 - 优化数据输出模式配置,支持串口输出和存储到卡的独立控制 - 优化USB连接处理逻辑,增加系统稳定性检查 🐛 fix(interrupt): 调整中断优先级配置 - 提高USART1中断优先级(从6调整为2),确保串口通信及时响应 - 调整DMA2_Stream5中断优先级(从0调整为5),优化数据传输调度 - 修复RS485驱动中的忙标志逻辑,改为阻塞式传输以提高可靠性 ♻️ refactor(config): 优化系统配置和存储设置 - 重构宏定义配置,统一系统监控开关,分离数据输出模式控制 - 将SD卡最大扇区大小从512调整为4096,优化大文件存储性能 - 增加堆栈大小配置(从0x800调整为0x1000),提高系统稳定性 - 优化USB存储读写超时设置,使用最大超时值确保操作完成 📝 docs(comments): 更新代码注释和文档 - 更新数据存储模块的注释,说明新的会话文件夹管理机制 - 在main.c中添加数据输出模式选择的详细说明注释 - 更新系统监控统计输出格式,包含新增的数据丢弃统计项
This commit is contained in:
+1
-1
@@ -57,7 +57,7 @@ void MX_DMA_Init(void)
|
||||
HAL_NVIC_SetPriority(DMA2_Stream3_IRQn, 10, 0);
|
||||
HAL_NVIC_EnableIRQ(DMA2_Stream3_IRQn);
|
||||
/* DMA2_Stream5_IRQn interrupt configuration */
|
||||
HAL_NVIC_SetPriority(DMA2_Stream5_IRQn, 0, 0);
|
||||
HAL_NVIC_SetPriority(DMA2_Stream5_IRQn, 5, 0);
|
||||
HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn);
|
||||
/* DMA2_Stream6_IRQn interrupt configuration */
|
||||
HAL_NVIC_SetPriority(DMA2_Stream6_IRQn, 10, 0);
|
||||
|
||||
+87
-37
@@ -46,12 +46,15 @@
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PD */
|
||||
// 串口输出控制开关
|
||||
#define ENABLE_UART_DEBUG_OUTPUT 1
|
||||
#define DEBUG_OUTPUT_INTERVAL_MS 1000 // 调试输出间隔(毫秒)
|
||||
|
||||
// 监控功能宏开关
|
||||
// 监控功能宏开关(统一控制串口输出和文件存储)
|
||||
#define ENABLE_SYSTEM_MONITOR 1 // 系统监控开关
|
||||
#define DEBUG_OUTPUT_INTERVAL_MS 1000 // 调试输出间隔(毫秒)
|
||||
#define MONITOR_SAVE_INTERVAL_MS 10000 // 监控状态保存间隔(毫秒) - 10秒
|
||||
|
||||
// 数据输出模式选择(可以同时启用)
|
||||
#define DATA_OUTPUT_MODE_UART 0 // 0=禁用, 1=启用串口输出数据
|
||||
#define DATA_OUTPUT_MODE_STORAGE 1 // 0=禁用, 1=启用存储到卡
|
||||
// 注意:两个模式可以同时启用,但会增加系统负载
|
||||
/* USER CODE END PD */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
@@ -87,7 +90,9 @@ static uint32_t g_last_usb_check = 0;
|
||||
|
||||
// 性能监控和调试输出
|
||||
static uint32_t g_last_debug_output = 0;
|
||||
static uint8_t g_debug_output_enabled = ENABLE_UART_DEBUG_OUTPUT;
|
||||
|
||||
// 监控状态保存
|
||||
static uint32_t g_last_monitor_save = 0;
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
@@ -147,6 +152,13 @@ static void ProcessAdcData(void)
|
||||
LTC2508_BufferTypeDef *ready_buffer = NULL;
|
||||
if (LTC2508_GetReadyBuffer(&ready_buffer) == LTC2508_OK && ready_buffer != NULL)
|
||||
{
|
||||
// 检查存储缓冲区是否可用(用于决定是否存储数据)
|
||||
uint8_t can_store_data = 0;
|
||||
if (g_recording_enabled) {
|
||||
uint32_t max_packet_size = sizeof(CorrectedDataPacket_t);
|
||||
can_store_data = DataStorage_IsBufferAvailable(&g_data_storage, max_packet_size);
|
||||
}
|
||||
|
||||
#if ENABLE_SYSTEM_MONITOR
|
||||
SystemMonitor_IncrementSampleCount();
|
||||
#endif
|
||||
@@ -174,28 +186,41 @@ static void ProcessAdcData(void)
|
||||
|
||||
correction_applied = 1;
|
||||
|
||||
// 发送校正后的数据包
|
||||
// RS485_SendData((uint8_t*)&g_corrected_packet, sizeof(CorrectedDataPacket_t));
|
||||
#if DATA_OUTPUT_MODE_UART
|
||||
// 发送校正后的数据包到串口
|
||||
RS485_SendData((uint8_t*)&g_corrected_packet, sizeof(CorrectedDataPacket_t));
|
||||
#endif
|
||||
|
||||
} else {
|
||||
|
||||
// 4b. 校正失败或未启用,使用原始数据
|
||||
PackData(&g_data_packet, raw_adc[0], raw_adc[1], raw_adc[2]);
|
||||
|
||||
// 发送原始数据包
|
||||
#if DATA_OUTPUT_MODE_UART
|
||||
// 发送原始数据包到串口
|
||||
RS485_SendData((uint8_t*)&g_data_packet, sizeof(DataPacket_t));
|
||||
#endif
|
||||
}
|
||||
|
||||
// 6. 存储数据到SD卡 (如果启用记录)
|
||||
// 6. 存储数据到SD卡 (如果启用记录且缓冲区可用)
|
||||
#if DATA_OUTPUT_MODE_STORAGE
|
||||
if (g_recording_enabled) {
|
||||
if (correction_applied) {
|
||||
// 存储校正后的数据
|
||||
DataStorage_WriteCorrectedData(&g_data_storage, &g_corrected_packet);
|
||||
if (can_store_data) {
|
||||
if (correction_applied) {
|
||||
// 存储校正后的数据
|
||||
DataStorage_WriteCorrectedData(&g_data_storage, &g_corrected_packet);
|
||||
} else {
|
||||
// 存储原始数据
|
||||
DataStorage_WriteData(&g_data_storage, &g_data_packet);
|
||||
}
|
||||
} else {
|
||||
// 存储原始数据
|
||||
DataStorage_WriteData(&g_data_storage, &g_data_packet);
|
||||
// 缓冲区满,数据被丢弃
|
||||
#if ENABLE_SYSTEM_MONITOR
|
||||
SystemMonitor_ReportDataDropped();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// 7. 释放已处理的缓冲区
|
||||
LTC2508_ReleaseBuffer(LTC2508_GetCurrentReadBuffer());
|
||||
@@ -211,9 +236,9 @@ static void ProcessAdcData(void)
|
||||
static void DebugOutput_Init(void)
|
||||
{
|
||||
// USART3已在MX_USART3_UART_Init()中初始化
|
||||
if (g_debug_output_enabled) {
|
||||
DebugOutput_SendString("\r\n=== System Debug Output Initialized ===\r\n");
|
||||
}
|
||||
#if ENABLE_SYSTEM_MONITOR
|
||||
DebugOutput_SendString("\r\n=== System Debug Output Initialized ===\r\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -223,11 +248,13 @@ static void DebugOutput_Init(void)
|
||||
*/
|
||||
static void DebugOutput_SendString(const char* str)
|
||||
{
|
||||
if (!g_debug_output_enabled || str == NULL) {
|
||||
#if ENABLE_SYSTEM_MONITOR
|
||||
if (str == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
HAL_UART_Transmit(&huart3, (uint8_t*)str, strlen(str), 100);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -237,10 +264,6 @@ static void DebugOutput_SendString(const char* str)
|
||||
static void DebugOutput_PrintSystemStats(void)
|
||||
{
|
||||
#if ENABLE_SYSTEM_MONITOR
|
||||
if (!g_debug_output_enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
char buffer[256];
|
||||
SystemMonitorStats_t sys_stats;
|
||||
SystemMonitor_GetStats(&sys_stats);
|
||||
@@ -261,12 +284,14 @@ static void DebugOutput_PrintSystemStats(void)
|
||||
"SD Write Errors: %lu\r\n"
|
||||
"SD Buffer Full: %lu\r\n"
|
||||
"SD Total Bytes: %lu\r\n"
|
||||
"SD File Count: %lu\r\n",
|
||||
"SD File Count: %lu\r\n"
|
||||
"SD Data Dropped: %lu\r\n",
|
||||
sys_stats.sd_write_count,
|
||||
sys_stats.sd_write_error_count,
|
||||
sys_stats.sd_buffer_full_count,
|
||||
sys_stats.sd_total_bytes_written,
|
||||
sys_stats.sd_file_count);
|
||||
sys_stats.sd_file_count,
|
||||
sys_stats.sd_data_dropped_count);
|
||||
DebugOutput_SendString(buffer);
|
||||
|
||||
// 计算并打印统计指标
|
||||
@@ -328,6 +353,12 @@ static void HandleUSBConnectionChange(void)
|
||||
|
||||
// 卸载用于采样的文件系统
|
||||
UnmountFileSystemForSampling();
|
||||
|
||||
while(1)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
} else {
|
||||
// USB断开:重新挂载文件系统,开始数据采集
|
||||
DebugOutput_SendString("USB Disconnected: Starting data acquisition\r\n");
|
||||
@@ -461,10 +492,6 @@ int main(void)
|
||||
MX_TIM2_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
// SD_NAND_Init_And_Mount();
|
||||
// SD_Test_Write();
|
||||
// SD_Test_Read();
|
||||
|
||||
// 初始化系统监控
|
||||
#if ENABLE_SYSTEM_MONITOR
|
||||
SystemMonitor_Init();
|
||||
@@ -489,7 +516,7 @@ int main(void)
|
||||
// 初始化数据存储
|
||||
if (DataStorage_Init(&g_data_storage) == HAL_OK) {
|
||||
// 开始数据记录
|
||||
StartRecording();
|
||||
StartRecording();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -502,6 +529,16 @@ int main(void)
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef NEED_FORMAT_SD
|
||||
// Raw_Hardware_Test();
|
||||
// SDNAND_ForceFormat_and_Mount();
|
||||
Run_SDNAND_SpeedTest_V2();
|
||||
while(1)
|
||||
{
|
||||
;
|
||||
}
|
||||
#endif
|
||||
|
||||
// 启动TIM2定时器用于1ms周期的ADC数据处理
|
||||
if (HAL_TIM_Base_Start_IT(&htim2) != HAL_OK) {
|
||||
Error_Handler();
|
||||
@@ -533,21 +570,34 @@ int main(void)
|
||||
uint32_t current_tick = HAL_GetTick();
|
||||
|
||||
// USB连接状态检测 (每500ms检测一次)
|
||||
// if (current_tick - g_last_usb_check >= 500) {
|
||||
// HandleUSBConnectionChange();
|
||||
// g_last_usb_check = current_tick;
|
||||
// }
|
||||
if (current_tick - g_last_usb_check >= 500) {
|
||||
HandleUSBConnectionChange();
|
||||
g_last_usb_check = current_tick;
|
||||
}
|
||||
|
||||
// 处理数据存储后台任务 (轮询方式)
|
||||
// 优化:连续处理3次,加快缓冲区刷新速度
|
||||
if (g_recording_enabled) {
|
||||
DataStorage_ProcessBackgroundTasks(&g_data_storage);
|
||||
// for (int i = 0; i < 3; i++) {
|
||||
DataStorage_ProcessBackgroundTasks(&g_data_storage);
|
||||
// }
|
||||
}
|
||||
|
||||
// 定期输出调试信息 (每1秒输出一次)
|
||||
if (g_debug_output_enabled && (current_tick - g_last_debug_output >= DEBUG_OUTPUT_INTERVAL_MS)) {
|
||||
#if ENABLE_SYSTEM_MONITOR
|
||||
if (current_tick - g_last_debug_output >= DEBUG_OUTPUT_INTERVAL_MS) {
|
||||
DebugOutput_PrintSystemStats();
|
||||
g_last_debug_output = current_tick;
|
||||
}
|
||||
#endif
|
||||
|
||||
// 定期保存监控状态 (每1分钟保存一次)
|
||||
#if ENABLE_SYSTEM_MONITOR
|
||||
if (current_tick - g_last_monitor_save >= MONITOR_SAVE_INTERVAL_MS) {
|
||||
SystemMonitor_SaveStatus();
|
||||
g_last_monitor_save = current_tick;
|
||||
}
|
||||
#endif
|
||||
|
||||
// ADC采样由PA1外部中断触发,不在主循环中触发
|
||||
// 可以在这里添加其他低优先级任务
|
||||
@@ -617,7 +667,7 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
|
||||
if(LTC2508_IsInited() == 0) return;
|
||||
|
||||
cnt ++;
|
||||
// if(cnt % 2 == 0)
|
||||
// if(cnt % 5 == 0)
|
||||
{
|
||||
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
|
||||
if (GPIO_Pin == ADC_DRY_Pin) {
|
||||
|
||||
+1
-1
@@ -131,7 +131,7 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
|
||||
__HAL_LINKDMA(uartHandle,hdmatx,hdma_usart1_tx);
|
||||
|
||||
/* USART1 interrupt Init */
|
||||
HAL_NVIC_SetPriority(USART1_IRQn, 6, 0);
|
||||
HAL_NVIC_SetPriority(USART1_IRQn, 2, 0);
|
||||
HAL_NVIC_EnableIRQ(USART1_IRQn);
|
||||
/* USER CODE BEGIN USART1_MspInit 1 */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user