✨ feat(main): 新增性能监控和调试输出功能
- 新增性能监控模块(performance_monitor),用于实时跟踪系统性能指标 - 添加串口调试输出功能,支持系统状态和性能统计的定期输出 - 实现双缓冲机制,提升ADC数据采集和存储的实时性 - 优化数据存储模块,支持校正后数据的存储和双缓冲管理 - 增强错误处理机制,完善中断回调函数和系统错误恢复 ♻️ refactor(ltc2508): 重构ADC驱动支持双缓冲 - 将ADC数据存储从单缓冲区重构为双缓冲区结构 - 新增缓冲区状态管理和自动切换机制 - 优化DMA传输完成回调,支持多缓冲区处理 - 提供缓冲区获取和释放的API接口 📝 docs(performance): 新增性能评估报告和使用指南 - 创建STM32F405性能评估报告,详细分析系统性能指标 - 编写双缓冲机制使用指南,说明实现原理和使用方法 - 添加LTC2508驱动使用示例代码 🐛 fix(dma): 调整DMA中断优先级 - 将DMA2_Stream7中断优先级从9调整为6,优化中断响应 - 更新STM32CubeMX配置文件中的中断优先级设置 🔧 chore(config): 优化系统配置和代码结构 - 添加串口调试输出控制开关和间隔配置 - 清理中断处理文件,移除重复的回调函数定义 - 增强错误处理函数,添加系统状态恢复机制
This commit is contained in:
+1
-1
@@ -60,7 +60,7 @@ void MX_DMA_Init(void)
|
||||
HAL_NVIC_SetPriority(DMA2_Stream6_IRQn, 3, 0);
|
||||
HAL_NVIC_EnableIRQ(DMA2_Stream6_IRQn);
|
||||
/* DMA2_Stream7_IRQn interrupt configuration */
|
||||
HAL_NVIC_SetPriority(DMA2_Stream7_IRQn, 9, 0);
|
||||
HAL_NVIC_SetPriority(DMA2_Stream7_IRQn, 6, 0);
|
||||
HAL_NVIC_EnableIRQ(DMA2_Stream7_IRQn);
|
||||
|
||||
}
|
||||
|
||||
+320
-25
@@ -35,6 +35,9 @@
|
||||
#include "correction.h"
|
||||
#include "data_storage.h"
|
||||
#include "system_monitor.h"
|
||||
#include "performance_monitor.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -44,7 +47,9 @@
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PD */
|
||||
|
||||
// 串口输出控制开关
|
||||
#define ENABLE_UART_DEBUG_OUTPUT 1
|
||||
#define DEBUG_OUTPUT_INTERVAL_MS 1000 // 调试输出间隔(毫秒)
|
||||
/* USER CODE END PD */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
@@ -55,26 +60,172 @@
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
// 外部SPI句柄声明
|
||||
extern SPI_HandleTypeDef hspi1;
|
||||
extern SPI_HandleTypeDef hspi2;
|
||||
extern SPI_HandleTypeDef hspi3;
|
||||
extern UART_HandleTypeDef huart1;
|
||||
extern UART_HandleTypeDef huart3;
|
||||
|
||||
// 校正参数
|
||||
CorrectionParams_t g_correction_params;
|
||||
// 数据包
|
||||
DataPacket_t g_data_packet;
|
||||
CorrectedDataPacket_t g_corrected_packet;
|
||||
// 数据存储句柄
|
||||
DataStorageHandle_t g_data_storage;
|
||||
// 系统状态
|
||||
static uint32_t g_last_monitor_update = 0;
|
||||
static uint8_t g_recording_enabled = 0;
|
||||
static uint32_t g_sample_count = 0;
|
||||
|
||||
// 性能监控和调试输出
|
||||
static uint32_t g_last_debug_output = 0;
|
||||
static uint8_t g_debug_output_enabled = ENABLE_UART_DEBUG_OUTPUT;
|
||||
static SystemPerfStats_t g_perf_stats;
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
static void StartRecording(void);
|
||||
static void StopRecording(void);
|
||||
static HAL_StatusTypeDef ValidateSystemHealth(void);
|
||||
static void DebugOutput_Init(void);
|
||||
static void DebugOutput_SendString(const char* str);
|
||||
static void DebugOutput_PrintSystemStats(void);
|
||||
static void DebugOutput_PrintPerformanceStats(void);
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/**
|
||||
* @brief 开始数据记录
|
||||
* @retval None
|
||||
*/
|
||||
static void StartRecording(void)
|
||||
{
|
||||
if (!g_recording_enabled) {
|
||||
if (DataStorage_StartRecording(&g_data_storage) == HAL_OK) {
|
||||
g_recording_enabled = 1;
|
||||
SystemMonitor_SetState(SYSTEM_STATE_RECORDING);
|
||||
} else {
|
||||
SystemMonitor_ReportError(SYSTEM_ERROR_STORAGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 停止数据记录
|
||||
* @retval None
|
||||
*/
|
||||
static void StopRecording(void)
|
||||
{
|
||||
if (g_recording_enabled) {
|
||||
g_recording_enabled = 0;
|
||||
DataStorage_StopRecording(&g_data_storage);
|
||||
SystemMonitor_SetState(SYSTEM_STATE_IDLE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 初始化调试输出
|
||||
* @retval None
|
||||
*/
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 通过USART3发送字符串
|
||||
* @param str: 要发送的字符串
|
||||
* @retval None
|
||||
*/
|
||||
static void DebugOutput_SendString(const char* str)
|
||||
{
|
||||
if (!g_debug_output_enabled || str == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
HAL_UART_Transmit(&huart3, (uint8_t*)str, strlen(str), 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 输出系统监控统计信息
|
||||
* @retval None
|
||||
*/
|
||||
static void DebugOutput_PrintSystemStats(void)
|
||||
{
|
||||
if (!g_debug_output_enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
char buffer[256];
|
||||
SystemMonitorStats_t sys_stats;
|
||||
SystemMonitor_GetStats(&sys_stats);
|
||||
|
||||
snprintf(buffer, sizeof(buffer),
|
||||
"\r\n=== System Monitor Stats ===\r\n"
|
||||
"State: %d, Uptime: %lu s\r\n"
|
||||
"Total Samples: %lu, Errors: %lu\r\n"
|
||||
"Memory Usage: %lu bytes\r\n"
|
||||
"CPU Usage: %d%%, Temp: %d°C\r\n",
|
||||
sys_stats.current_state,
|
||||
sys_stats.uptime_seconds,
|
||||
sys_stats.total_samples,
|
||||
sys_stats.error_count,
|
||||
sys_stats.memory_usage,
|
||||
sys_stats.cpu_usage_percent,
|
||||
sys_stats.temperature_celsius);
|
||||
|
||||
DebugOutput_SendString(buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 输出性能监控统计信息
|
||||
* @retval None
|
||||
*/
|
||||
static void DebugOutput_PrintPerformanceStats(void)
|
||||
{
|
||||
if (!g_debug_output_enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
char buffer[512];
|
||||
PerformanceMonitor_GetStats(&g_perf_stats);
|
||||
|
||||
snprintf(buffer, sizeof(buffer),
|
||||
"\r\n=== Performance Monitor Stats ===\r\n"
|
||||
"Total CPU Usage: %lu%%\r\n"
|
||||
"Free Heap: %lu bytes (Min: %lu)\r\n"
|
||||
"Stack Usage: %lu%%\r\n",
|
||||
g_perf_stats.total_cpu_usage_percent,
|
||||
g_perf_stats.free_heap_size,
|
||||
g_perf_stats.min_free_heap_size,
|
||||
g_perf_stats.stack_usage_percent);
|
||||
|
||||
DebugOutput_SendString(buffer);
|
||||
|
||||
// 输出各任务性能统计
|
||||
for (int i = 0; i < PERF_MON_MAX_TASKS; i++) {
|
||||
if (g_perf_stats.tasks[i].call_count > 0) {
|
||||
snprintf(buffer, sizeof(buffer),
|
||||
"Task[%d]: Calls=%lu, Avg=%lu us, Max=%lu us, CPU=%.1f%%\r\n",
|
||||
i,
|
||||
g_perf_stats.tasks[i].call_count,
|
||||
g_perf_stats.tasks[i].avg_time_us,
|
||||
g_perf_stats.tasks[i].max_time_us,
|
||||
g_perf_stats.tasks[i].cpu_usage_percent);
|
||||
DebugOutput_SendString(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
@@ -121,6 +272,12 @@ int main(void)
|
||||
SystemMonitor_Init();
|
||||
SystemMonitor_SetState(SYSTEM_STATE_INIT);
|
||||
|
||||
// 初始化性能监控
|
||||
PerformanceMonitor_Init();
|
||||
|
||||
// 初始化调试输出
|
||||
DebugOutput_Init();
|
||||
|
||||
// 初始化LTC2508驱动
|
||||
if (LTC2508_Init(&hspi1, &hspi2, &hspi3) != LTC2508_OK) {
|
||||
SystemMonitor_ReportError(SYSTEM_ERROR_ADC);
|
||||
@@ -157,61 +314,125 @@ int main(void)
|
||||
// 系统监控更新 (每100ms更新一次)
|
||||
uint32_t current_tick = HAL_GetTick();
|
||||
if (current_tick - g_last_monitor_update >= 100) {
|
||||
PerformanceMonitor_TaskStart(PERF_TASK_SYSTEM_MONITOR);
|
||||
SystemMonitor_Update();
|
||||
PerformanceMonitor_Update();
|
||||
PerformanceMonitor_TaskEnd(PERF_TASK_SYSTEM_MONITOR);
|
||||
g_last_monitor_update = current_tick;
|
||||
}
|
||||
|
||||
// 检查ADC数据是否准备就绪
|
||||
if (g_adc_data_ready_flag)
|
||||
LTC2508_BufferTypeDef *ready_buffer = NULL;
|
||||
if (LTC2508_GetReadyBuffer(&ready_buffer) == LTC2508_OK && ready_buffer != NULL)
|
||||
{
|
||||
g_adc_data_ready_flag = 0; // 清除标志
|
||||
PerformanceMonitor_TaskStart(PERF_TASK_ADC_PROCESSING);
|
||||
SystemMonitor_SetState(SYSTEM_STATE_SAMPLING);
|
||||
g_sample_count++;
|
||||
|
||||
// 1. 合并数据 (高位16位在前)
|
||||
// 1. 从双缓冲区获取数据并合并 (高位16位在前)
|
||||
int32_t raw_adc[NUM_LTC2508];
|
||||
raw_adc[0] = (int32_t)(((uint32_t)g_adc_data[0][0] << 16) | g_adc_data[0][1]);
|
||||
raw_adc[1] = (int32_t)(((uint32_t)g_adc_data[1][0] << 16) | g_adc_data[1][1]);
|
||||
raw_adc[2] = (int32_t)(((uint32_t)g_adc_data[2][0] << 16) | g_adc_data[2][1]);
|
||||
for (uint8_t i = 0; i < NUM_LTC2508; i++) {
|
||||
raw_adc[i] = (int32_t)(((uint32_t)ready_buffer->data[i][0] << 16) | ready_buffer->data[i][1]);
|
||||
}
|
||||
PerformanceMonitor_TaskEnd(PERF_TASK_ADC_PROCESSING);
|
||||
|
||||
// 2. 验证数据有效性
|
||||
uint8_t data_valid = 1;
|
||||
for (uint8_t i = 0; i < NUM_LTC2508; i++) {
|
||||
if (LTC2508_ValidateData(i) != LTC2508_OK) {
|
||||
SystemMonitor_ReportError(SYSTEM_ERROR_ADC);
|
||||
continue; // 跳过无效数据
|
||||
}
|
||||
if (LTC2508_ValidateData(ready_buffer, i) != LTC2508_OK) {
|
||||
SystemMonitor_ReportError(SYSTEM_ERROR_ADC);
|
||||
data_valid = 0;
|
||||
break; // 如果有任何通道数据无效,跳过整个样本
|
||||
}
|
||||
}
|
||||
|
||||
if (!data_valid) {
|
||||
// 释放缓冲区并继续下一次循环
|
||||
LTC2508_ReleaseBuffer(g_current_read_buffer);
|
||||
SystemMonitor_SetState(SYSTEM_STATE_IDLE);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 3. 应用校正算法
|
||||
CorrectionResult_t correction_result;
|
||||
if (Apply_Correction(raw_adc[0], raw_adc[1], raw_adc[2],
|
||||
uint8_t correction_applied = 0;
|
||||
|
||||
PerformanceMonitor_TaskStart(PERF_TASK_CORRECTION);
|
||||
if (g_correction_params.params_valid &&
|
||||
Apply_Correction(raw_adc[0], raw_adc[1], raw_adc[2],
|
||||
&correction_result, &g_correction_params) == HAL_OK) {
|
||||
PerformanceMonitor_TaskEnd(PERF_TASK_CORRECTION);
|
||||
|
||||
// 4. 打包校正后的数据
|
||||
PackData(&g_data_packet, (int32_t)correction_result.corrected_x,
|
||||
(int32_t)correction_result.corrected_y,
|
||||
(int32_t)correction_result.corrected_z);
|
||||
// 4a. 打包校正后的数据
|
||||
PerformanceMonitor_TaskStart(PERF_TASK_DATA_PACKET);
|
||||
PackCorrectedData(&g_corrected_packet,
|
||||
correction_result.corrected_x,
|
||||
correction_result.corrected_y,
|
||||
correction_result.corrected_z);
|
||||
PerformanceMonitor_TaskEnd(PERF_TASK_DATA_PACKET);
|
||||
correction_applied = 1;
|
||||
|
||||
// 发送校正后的数据包
|
||||
PerformanceMonitor_TaskStart(PERF_TASK_RS485_TX);
|
||||
if (RS485_SendData((uint8_t*)&g_corrected_packet, sizeof(CorrectedDataPacket_t)) != HAL_OK) {
|
||||
SystemMonitor_ReportError(SYSTEM_ERROR_COMMUNICATION);
|
||||
}
|
||||
PerformanceMonitor_TaskEnd(PERF_TASK_RS485_TX);
|
||||
} else {
|
||||
// 校正失败,使用原始数据
|
||||
PerformanceMonitor_TaskEnd(PERF_TASK_CORRECTION);
|
||||
|
||||
// 4b. 校正失败或未启用,使用原始数据
|
||||
PerformanceMonitor_TaskStart(PERF_TASK_DATA_PACKET);
|
||||
PackData(&g_data_packet, raw_adc[0], raw_adc[1], raw_adc[2]);
|
||||
}
|
||||
|
||||
// 5. 发送数据包
|
||||
if (RS485_SendData((uint8_t*)&g_data_packet, sizeof(DataPacket_t)) != HAL_OK) {
|
||||
SystemMonitor_ReportError(SYSTEM_ERROR_COMMUNICATION);
|
||||
PerformanceMonitor_TaskEnd(PERF_TASK_DATA_PACKET);
|
||||
|
||||
// 发送原始数据包
|
||||
PerformanceMonitor_TaskStart(PERF_TASK_RS485_TX);
|
||||
if (RS485_SendData((uint8_t*)&g_data_packet, sizeof(DataPacket_t)) != HAL_OK) {
|
||||
SystemMonitor_ReportError(SYSTEM_ERROR_COMMUNICATION);
|
||||
}
|
||||
PerformanceMonitor_TaskEnd(PERF_TASK_RS485_TX);
|
||||
}
|
||||
|
||||
// 6. 存储数据到SD卡 (如果启用记录)
|
||||
if (g_recording_enabled) {
|
||||
SystemMonitor_SetState(SYSTEM_STATE_RECORDING);
|
||||
if (DataStorage_WriteData(&g_data_storage, &g_data_packet) != HAL_OK) {
|
||||
SystemMonitor_ReportError(SYSTEM_ERROR_STORAGE);
|
||||
PerformanceMonitor_TaskStart(PERF_TASK_FATFS_WRITE);
|
||||
|
||||
if (correction_applied) {
|
||||
// 存储校正后的数据
|
||||
if (DataStorage_WriteCorrectedData(&g_data_storage, &correction_result) != HAL_OK) {
|
||||
SystemMonitor_ReportError(SYSTEM_ERROR_STORAGE);
|
||||
}
|
||||
} else {
|
||||
// 存储原始数据
|
||||
if (DataStorage_WriteData(&g_data_storage, &g_data_packet) != HAL_OK) {
|
||||
SystemMonitor_ReportError(SYSTEM_ERROR_STORAGE);
|
||||
}
|
||||
}
|
||||
PerformanceMonitor_TaskEnd(PERF_TASK_FATFS_WRITE);
|
||||
}
|
||||
|
||||
// 7. 释放已处理的缓冲区
|
||||
LTC2508_ReleaseBuffer(g_current_read_buffer);
|
||||
|
||||
SystemMonitor_SetState(SYSTEM_STATE_IDLE);
|
||||
}
|
||||
|
||||
// 处理数据存储后台任务
|
||||
if (g_recording_enabled) {
|
||||
DataStorage_ProcessBackgroundTasks(&g_data_storage);
|
||||
}
|
||||
|
||||
// 定期输出调试信息 (每1秒输出一次)
|
||||
if (g_debug_output_enabled && (current_tick - g_last_debug_output >= DEBUG_OUTPUT_INTERVAL_MS)) {
|
||||
DebugOutput_PrintSystemStats();
|
||||
DebugOutput_PrintPerformanceStats();
|
||||
g_last_debug_output = current_tick;
|
||||
}
|
||||
|
||||
// ADC采样由PA1外部中断触发,不在主循环中触发
|
||||
// 可以在这里添加其他低优先级任务
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
@@ -263,6 +484,63 @@ void SystemClock_Config(void)
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/**
|
||||
* @brief 外部中断回调函数 - ADC数据就绪信号
|
||||
* @param GPIO_Pin: 触发中断的GPIO引脚
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
|
||||
{
|
||||
if (GPIO_Pin == ADC_DRY_Pin) {
|
||||
// ADC数据就绪,触发DMA读取
|
||||
if (LTC2508_TriggerDmaRead() != LTC2508_OK) {
|
||||
SystemMonitor_ReportError(SYSTEM_ERROR_ADC);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI DMA传输完成回调函数
|
||||
* @param hspi: SPI句柄指针
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
// 调用LTC2508驱动的DMA完成回调
|
||||
LTC2508_DmaComplete_Callback(hspi);
|
||||
}
|
||||
|
||||
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
// 调用LTC2508驱动的DMA完成回调
|
||||
LTC2508_DmaComplete_Callback(hspi);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI错误回调函数
|
||||
* @param hspi: SPI句柄指针
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
// 调用LTC2508驱动的错误回调
|
||||
LTC2508_ErrorCallback(hspi);
|
||||
SystemMonitor_ReportError(SYSTEM_ERROR_ADC);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief UART传输完成回调函数
|
||||
* @param huart: UART句柄指针
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
|
||||
{
|
||||
if (huart == &huart1) {
|
||||
// RS485传输完成回调
|
||||
RS485_TxCpltCallback(huart);
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/**
|
||||
@@ -273,9 +551,26 @@ void Error_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN Error_Handler_Debug */
|
||||
/* User can add his own implementation to report the HAL error return state */
|
||||
|
||||
// 设置系统状态为错误状态
|
||||
SystemMonitor_SetState(SYSTEM_STATE_ERROR);
|
||||
SystemMonitor_ReportError(SYSTEM_ERROR_CRITICAL);
|
||||
|
||||
// 停止所有DMA传输
|
||||
HAL_SPI_DMAStop(&hspi1);
|
||||
HAL_SPI_DMAStop(&hspi2);
|
||||
HAL_SPI_DMAStop(&hspi3);
|
||||
|
||||
// 停止数据记录
|
||||
g_recording_enabled = 0;
|
||||
DataStorage_StopRecording(&g_data_storage);
|
||||
|
||||
// 禁用中断并进入无限循环
|
||||
__disable_irq();
|
||||
while (1)
|
||||
{
|
||||
// 可以在这里添加LED指示或其他错误指示
|
||||
HAL_Delay(500);
|
||||
}
|
||||
/* USER CODE END Error_Handler_Debug */
|
||||
}
|
||||
|
||||
@@ -323,27 +323,4 @@ void DMA2_Stream7_IRQHandler(void)
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
// Handle error
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
LTC2508_DmaComplete_Callback(hspi);
|
||||
}
|
||||
|
||||
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
LTC2508_DmaComplete_Callback(hspi);
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
|
||||
{
|
||||
if(GPIO_Pin == GPIO_PIN_1)
|
||||
{
|
||||
LTC2508_TriggerDmaRead();
|
||||
}
|
||||
}
|
||||
/* USER CODE END 1 */
|
||||
|
||||
Reference in New Issue
Block a user