- 将日志环形缓冲区从静态数组改为动态堆分配
- 实现启动时大容量缓冲区和运行时小容量缓冲区的切换机制
- 添加日志环满时优先保留错误和警告信息的清理策略
- 优化内存使用,在日志为空时避免不必要的内存分配
- 移除调试日志中的冗余字段显示
feat(api): 更新CAN接口为批量发送模式
- 将 `/can/send` 接口替换为 `/can/send_batch`,支持1到N帧的批量发送
- 更新API文档中的接口描述和使用示例
- 修改请求参数格式为 `{"ch":0,"frames":[{...}]}` 返回格式为 `{"status":"ok","results":[...]}`
- 单帧发送也需要通过send_batch接口实现
feat(monitor): 新增GD32 ADC调试反馈处理功能
- 添加gd32_adc_feedback组件用于解析0xC4类型的SPI反馈
- 实现ADC数据解析包括温度、电压等参数的计算和转换
- 在can_feedback_task中集成ADC反馈分发逻辑
- 添加线程安全的数据获取和年龄检查接口
refactor(config): 优化任务栈大小配置并调整OTA块大小
- 重新评估各任务栈使用情况,优化HTTP、WiFi、SPI等工作任务的栈大小
- 将HTTP服务器栈从8KB调整至7KB,WiFi OTA任务栈从4KB调整至3KB
- OTA块大小从2KB调整回4KB以提升传输吞吐性能
- 重新整理配置文件结构,移动WiFi模式配置位置
151 lines
3.8 KiB
C++
151 lines
3.8 KiB
C++
/*
|
|
* gd32_adc_feedback.cpp - GD32 ADC 调试反馈解析与缓存
|
|
*/
|
|
#include "monitor/gd32_adc_feedback.h"
|
|
#include "esp_timer.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/semphr.h"
|
|
|
|
#include <string.h>
|
|
|
|
namespace {
|
|
|
|
constexpr uint8_t kPayloadVer = 1U;
|
|
constexpr size_t kPayloadLen = 20U;
|
|
|
|
static SemaphoreHandle_t s_mux = nullptr;
|
|
static gd32_adc_feedback_t s_latest = {};
|
|
static bool s_have = false;
|
|
|
|
/** @brief 读小端 u16。 */
|
|
static uint16_t rdU16Le(const uint8_t *p)
|
|
{
|
|
return static_cast<uint16_t>(p[0] | (static_cast<uint16_t>(p[1]) << 8));
|
|
}
|
|
|
|
/** @brief 读小端 u32。 */
|
|
static uint32_t rdU32Le(const uint8_t *p)
|
|
{
|
|
return static_cast<uint32_t>(p[0])
|
|
| (static_cast<uint32_t>(p[1]) << 8)
|
|
| (static_cast<uint32_t>(p[2]) << 16)
|
|
| (static_cast<uint32_t>(p[3]) << 24);
|
|
}
|
|
|
|
/** @brief 懒创建快照互斥量。 */
|
|
static void ensureMux()
|
|
{
|
|
if (s_mux == nullptr) {
|
|
s_mux = xSemaphoreCreateMutex();
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
/**
|
|
* @brief 处理一帧 0xC4 ADC 调试反馈并更新最新快照。
|
|
* @param payload 固定 20B 载荷
|
|
* @param len 实际长度
|
|
* @return true 解析并写入成功
|
|
*/
|
|
bool onGd32AdcFeedbackEvent(const uint8_t *payload, size_t len)
|
|
{
|
|
if (payload == nullptr || len < kPayloadLen) {
|
|
return false;
|
|
}
|
|
if (payload[0] != kPayloadVer) {
|
|
return false;
|
|
}
|
|
|
|
const uint8_t flags = payload[1];
|
|
const int16_t temp_x100 = static_cast<int16_t>(rdU16Le(&payload[2]));
|
|
const uint16_t vdda_mV = rdU16Le(&payload[4]);
|
|
const uint16_t vin_mV = rdU16Le(&payload[6]);
|
|
const uint16_t temp_raw = rdU16Le(&payload[8]);
|
|
const uint16_t vref_raw = rdU16Le(&payload[10]);
|
|
const uint32_t seq = rdU32Le(&payload[12]);
|
|
const uint32_t uptime = rdU32Le(&payload[16]);
|
|
|
|
gd32_adc_feedback_t fb = {};
|
|
fb.valid = (flags & 0x01U) != 0U;
|
|
fb.temperature_c = static_cast<float>(temp_x100) / 100.0f;
|
|
fb.vdda_v = static_cast<float>(vdda_mV) / 1000.0f;
|
|
fb.vin_adc_v = static_cast<float>(vin_mV) / 1000.0f;
|
|
/* 原理图:VCC — R18 47k — VIN_ADC — R13 4.7k — GND
|
|
* VCC = V_adc * (R18+R13)/R13 = V_adc * 11 */
|
|
fb.vcc_v = fb.vin_adc_v * ((47.0f + 4.7f) / 4.7f);
|
|
fb.temp_raw = temp_raw;
|
|
fb.vref_raw = vref_raw;
|
|
fb.seq = seq;
|
|
fb.uptime_sec = uptime;
|
|
fb.recv_ms = esp_timer_get_time() / 1000;
|
|
|
|
ensureMux();
|
|
if (s_mux == nullptr) {
|
|
return false;
|
|
}
|
|
if (xSemaphoreTake(s_mux, pdMS_TO_TICKS(20)) != pdTRUE) {
|
|
return false;
|
|
}
|
|
s_latest = fb;
|
|
s_have = true;
|
|
xSemaphoreGive(s_mux);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @brief 线程安全拷贝最近一次 ADC 反馈。
|
|
* @param[out] out 输出缓冲
|
|
* @return true 已有至少一帧有效数据
|
|
*/
|
|
bool getLatestGd32AdcFeedback(gd32_adc_feedback_t *out)
|
|
{
|
|
if (out == nullptr) {
|
|
return false;
|
|
}
|
|
ensureMux();
|
|
if (s_mux == nullptr) {
|
|
return false;
|
|
}
|
|
bool ok = false;
|
|
if (xSemaphoreTake(s_mux, pdMS_TO_TICKS(20)) == pdTRUE) {
|
|
if (s_have) {
|
|
*out = s_latest;
|
|
ok = true;
|
|
}
|
|
xSemaphoreGive(s_mux);
|
|
}
|
|
return ok;
|
|
}
|
|
|
|
/**
|
|
* @brief 距上次收到 0xC4 的毫秒数。
|
|
* @return 年龄 ms;尚无数据返回 UINT32_MAX
|
|
*/
|
|
uint32_t gd32AdcFeedbackAgeMs(void)
|
|
{
|
|
ensureMux();
|
|
if (s_mux == nullptr) {
|
|
return UINT32_MAX;
|
|
}
|
|
int64_t recv = 0;
|
|
bool have = false;
|
|
if (xSemaphoreTake(s_mux, pdMS_TO_TICKS(20)) == pdTRUE) {
|
|
have = s_have;
|
|
recv = s_latest.recv_ms;
|
|
xSemaphoreGive(s_mux);
|
|
}
|
|
if (!have || recv <= 0) {
|
|
return UINT32_MAX;
|
|
}
|
|
const int64_t now = esp_timer_get_time() / 1000;
|
|
if (now < recv) {
|
|
return 0U;
|
|
}
|
|
const int64_t age = now - recv;
|
|
if (age > static_cast<int64_t>(UINT32_MAX)) {
|
|
return UINT32_MAX;
|
|
}
|
|
return static_cast<uint32_t>(age);
|
|
}
|