第一次提交

This commit is contained in:
2025-07-05 20:11:15 +08:00
commit 89b0708dfb
56 changed files with 10876 additions and 0 deletions

175
app/app_frm_monitor.c Normal file
View File

@@ -0,0 +1,175 @@
#include "app_config.h"
#include "interface.h"
#include "app_frm_monitor.h"
#include "app_frm_signal.h"
#include "app_frm_timer.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
// 全局日志缓冲区实例
LogBuffer log_buffer;
// 打印当前信号队列状态
void printSignalQueueStatus(void)
{
for (uint32_t i = 0; i < PRIORITY_LEVELS; i++)
{
printf("Priority %u Signal Count: %d\n", i,
getSignalCount(i));
}
}
// 打印订阅者信息
void printSubscriberInfo(void)
{
printf("Total Subscribers: %u\n", getSubscriberCount());
}
// 打印定时器状态
void printTimerStatus(void)
{
printf("Current Timer Count: %u / %u\n",
getCurrentTimerCount(), MAX_TIMERS);
}
// 监控函数
void monitorSignalSystem(void)
{
static uint32_t last_print_time = 0;
uint32_t current_print_time = getCurrentTime();
uint32_t time_interval = current_print_time - last_print_time;
printf("---------------------------------------------------\n");
printf("Time since last print: %u us\n", time_interval);
printSignalQueueStatus();
printSubscriberInfo();
printTimerStatus();
printf("---------------------------------------------------\n");
last_print_time = current_print_time;
}
int logBufferWrite(LogBuffer *lb, const char *format, ...)
{
if (format == NULL || lb == NULL)
{
return -1;
}
char temp_buffer[256]; // 临时缓冲区假设单条日志不超过256字节
va_list args;
va_start(args, format);
int length = vsnprintf(temp_buffer, sizeof(temp_buffer), format, args);
va_end(args);
if (length <= 0)
{
return -1;
}
// 进入关键区,保护缓冲区的写操作
irq_state_t saved_state = enter_critical_section();
// 检查缓冲区是否有足够的空间
if (lb->count + length > LOG_BUFFER_SIZE)
{
// 缓冲区满,无法写入
exit_critical_section(saved_state);
return -1;
}
// 写入数据到缓冲区
for (int i = 0; i < length; i++)
{
lb->buffer[lb->tail] = temp_buffer[i];
lb->tail = (lb->tail + 1) % LOG_BUFFER_SIZE;
}
lb->count += length;
exit_critical_section(saved_state);
return 0;
}
int logBufferRead(LogBuffer *lb, char *data, uint32_t max_length)
{
if (max_length == 0 || data == NULL || lb == NULL)
{
return -1;
}
// 进入关键区,保护缓冲区的读操作
irq_state_t saved_state = enter_critical_section();
uint32_t length = lb->count < max_length ? lb->count : max_length;
// 从缓冲区读取数据
for (uint32_t i = 0; i < length; i++)
{
data[i] = lb->buffer[lb->head];
lb->head = (lb->head + 1) % LOG_BUFFER_SIZE;
}
lb->count -= length;
exit_critical_section(saved_state);
return length;
}
// 初始化日志缓冲区
void initLogBuffer(LogBuffer *lb)
{
lb->head = 0;
lb->tail = 0;
lb->count = 0;
memset(lb->buffer, 0, LOG_BUFFER_SIZE);
}
// 日志信号处理函数
void logSignalHandler(void *signal_id)
{
(void)signal_id;
char log_data[256]; // 假设单次读取不超过256字节
int read_length;
// 从缓冲区读取日志信息
read_length = logBufferRead(&log_buffer, log_data, sizeof(log_data) - 1);
if (read_length > 0)
{
log_data[read_length] = '\0'; // 确保字符串以NULL结尾
// 调用printf进行打印
printf("%s", log_data);
}
}
Timer log_timer;
void logTimerHandler(void *timer_id)
{
(void)timer_id;
// monitorSignalSystem();
timerStart(&log_timer, 1000, 1);
}
void appMonitorInit(void)
{
// 初始化日志缓冲区
initLogBuffer(&log_buffer);
// 订阅日志信号
subscribe(&log_buffer, logSignalHandler);
timerInit(&log_timer);
timerStart(&log_timer, 1000, 1);
subscribe(&log_timer, logTimerHandler);
}
// // 使用格式化字符串记录日志
// if (logBufferWrite(&log_buffer, "ISR triggered: IRQ %d\n", irq_number) == 0) {
// // 发送信号,通知主线程有新日志
// publishMessage(&log_buffer, LOG_SIGNAL_PRIORITY);
// }