- 新增 `lastWorkerExecUs()`:获取最近一次 worker 任务执行耗时(µs)。 - 引入 `SpiProfile` 结构体:事务级详细剖析,记录 SPI 事务各阶段的时间戳。 - 实现 `lastProfile()`:诊断用,获取最近一次事务剖析快照。 - SPI 时钟配置提升至 13.3 MHz,改善性能。 - 修改前端 JavaScript 轮询逻辑:新增 uptime 检查,更好地处理 ESP32 重启状态。 - SPI 测速 HTML 的计时单位由毫秒改为微秒显示。 - 新建 `spi_ring_cache.h`:共享 SPI ring push 缓存工具。 - 新增 `http_util.h`:共享 HTTP 处理器工具,含 JSON 拼接与查询参数解析。 - 引入 `json_parse.h`:轻量级 JSON 解析器,针对扁平 JSON 对象设计。 - 新增 Windows 环境下的项目构建批处理脚本。 - 重构 CAN 反馈处理:从推送式改为请求驱动架构,降低内存占用、提升效率。
114 lines
3.0 KiB
C++
114 lines
3.0 KiB
C++
/*
|
|
* gd32_adc_feedback.cpp - GD32 ADC 调试反馈解析与缓存
|
|
*/
|
|
#include "monitor/gd32_adc_feedback.h"
|
|
#include "monitor/spi_ring_cache.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;
|
|
|
|
} // 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;
|
|
|
|
ringCacheEnsureMux(&s_mux);
|
|
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;
|
|
}
|
|
ringCacheEnsureMux(&s_mux);
|
|
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)
|
|
{
|
|
ringCacheEnsureMux(&s_mux);
|
|
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);
|
|
}
|
|
return ringCacheAgeMs(have, recv);
|
|
} |