feat(canfd): 添加CANFD硬件自检功能并优化初始化时序
- 新增s_channelHwOk数组跟踪各通道硬件初始化状态 - 修改CANFD初始化流程,在GPIO上电后延时200ms再进入Normal模式 - 实现selfTestL0()轻量自检函数,检查硬件初始化、退出init模式、收发器状态等 - 更新CANFD初始化逻辑,只有自检通过才标记为成功 refactor(spi): 调整SPI DMA和EXTI配置顺序避免通信失步 - 将armSpiDmasForNextCs()调用移至configureExti()之前 - 添加注释说明DMA预装和EXTI开启的时序要求 - 避免OTA跳转后ESP32立即发送HELLO导致的SPI从机失步问题 fix(app_spi): 修复HELLO_ACK响应中credit初始化问题 - 在HELLO_ACK构建前将g_localCredit设置为1 - 与requestEsp32Read()保持一致避免TX占用时credit=0的问题 - 防止主机因remoteCredit=0而拒绝发送PING帧 refactor(initial): 重构应用初始化流程添加模块状态跟踪 - 引入ModuleBit枚举和掩码机制跟踪各模块初始化状态 - 实现mark()和report()方法统一管理模块启动结果 - 按NVIC→SysTick→ESP32_EN→USART0→AppSpi→ADC→CANFD→OTA→TaskManager→Scheduler→EtherCAT→Watchdog顺序启动 - 将CANFD模块成功条件改为selfTestL0()通过才标记成功
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
/**
|
||||
* @file initial.cpp
|
||||
* @brief 应用启动初始化实现。
|
||||
* @brief 应用启动初始化:模块成功置位,结束统一报告结果。
|
||||
*
|
||||
* 顺序:
|
||||
* NVIC → SysTick → ESP32_EN 保持 → USART0 → AppSpi(尽早 HELLO 探活)→
|
||||
* ADC → CANFD → OtaBootRequest → TaskManager → Scheduler →
|
||||
* EtherCAT(+Sync 可选) → Watchdog → report
|
||||
*/
|
||||
|
||||
#include "sys/initial.h"
|
||||
@@ -18,49 +23,129 @@
|
||||
#include "comm/ethercat_slave.h"
|
||||
#include "comm/ethercat_sync.h"
|
||||
|
||||
/**
|
||||
* @brief 执行应用级外设与调度初始化。
|
||||
* @param[in] 无。
|
||||
* @param[out] 无。
|
||||
* @retval 无返回值。
|
||||
* @note 依次完成 SysTick、USART0、ADC/温度采样、CANFD、任务管理器与调度定时器的启动。
|
||||
*/
|
||||
uint32_t AppInitializer::s_mask = 0U;
|
||||
|
||||
namespace {
|
||||
|
||||
struct InitEntry {
|
||||
uint32_t bit;
|
||||
const char *name;
|
||||
bool critical;
|
||||
};
|
||||
|
||||
static const InitEntry kEntries[] = {
|
||||
{ AppInitializer::kNvic, "nvic", true },
|
||||
{ AppInitializer::kSysTick, "systick", true },
|
||||
{ AppInitializer::kEsp32Hold, "esp32_hold", true },
|
||||
{ AppInitializer::kUsart0, "usart0", true },
|
||||
{ AppInitializer::kAppSpi, "app_spi", true },
|
||||
{ AppInitializer::kAdc, "adc", true },
|
||||
{ AppInitializer::kCanFd, "canfd", true },
|
||||
{ AppInitializer::kOtaBootReq, "ota_boot_req", true },
|
||||
{ AppInitializer::kTaskManager, "task_manager", true },
|
||||
{ AppInitializer::kScheduler, "scheduler", true },
|
||||
{ AppInitializer::kEthercat, "ethercat", false },
|
||||
{ AppInitializer::kEthercatSync, "ethercat_sync", false },
|
||||
{ AppInitializer::kWatchdog, "watchdog", true },
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
void AppInitializer::mark(ModuleBit b)
|
||||
{
|
||||
s_mask |= static_cast<uint32_t>(b);
|
||||
}
|
||||
|
||||
void AppInitializer::report()
|
||||
{
|
||||
bool critical_ok = true;
|
||||
bool all_ok = true;
|
||||
char fail_list[160] = {0};
|
||||
size_t fail_len = 0U;
|
||||
|
||||
for (size_t i = 0; i < sizeof(kEntries) / sizeof(kEntries[0]); ++i) {
|
||||
const InitEntry &e = kEntries[i];
|
||||
const bool ok = (s_mask & e.bit) != 0U;
|
||||
if (ok) {
|
||||
continue;
|
||||
}
|
||||
all_ok = false;
|
||||
if (e.critical) {
|
||||
critical_ok = false;
|
||||
}
|
||||
if (fail_len + 2U < sizeof(fail_list)) {
|
||||
if (fail_len > 0U) {
|
||||
fail_list[fail_len++] = ',';
|
||||
}
|
||||
const char *p = e.name;
|
||||
while (*p != '\0' && fail_len + 1U < sizeof(fail_list)) {
|
||||
fail_list[fail_len++] = *p++;
|
||||
}
|
||||
fail_list[fail_len] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
if (critical_ok && all_ok) {
|
||||
printf("[APP] init: OK (all modules)\r\n");
|
||||
} else if (critical_ok) {
|
||||
printf("[APP] init: OK (critical), skipped/failed: %s\r\n",
|
||||
fail_list[0] != '\0' ? fail_list : "?");
|
||||
} else {
|
||||
printf("[APP] init: FAIL, modules: %s\r\n",
|
||||
fail_list[0] != '\0' ? fail_list : "?");
|
||||
}
|
||||
}
|
||||
|
||||
void AppInitializer::init()
|
||||
{
|
||||
/* 必须最先设置 NVIC 优先级分组:确保后续 CanFd/SPI0 等外设 init 中
|
||||
* 编码抢占/子优先级字节时使用正确的分组(PRE4_SUB0),避免在错误分组
|
||||
* 下编码后被重新解释。EtherCAT 实时架构整改 Task3。 */
|
||||
s_mask = 0U;
|
||||
|
||||
nvic_priority_group_set(CONFIG_NVIC_PRIORITY_GROUP);
|
||||
printf("[APP] init: SysTick...\r\n");
|
||||
mark(kNvic);
|
||||
|
||||
SysTickTimer::initMs();
|
||||
/* ESP32 EN/RESET (PC9) 直接配置为推挽输出高电平,保持 ESP32 正常运行 */
|
||||
mark(kSysTick);
|
||||
|
||||
rcu_periph_clock_enable(RCU_GPIOC);
|
||||
gpio_bit_set(CONFIG_ESP32_RESET_PORT, CONFIG_ESP32_RESET_PIN);
|
||||
gpio_init(CONFIG_ESP32_RESET_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, CONFIG_ESP32_RESET_PIN);
|
||||
printf("[APP] init: USART0...\r\n");
|
||||
mark(kEsp32Hold);
|
||||
|
||||
Usart0::configurePins();
|
||||
Usart0::init();
|
||||
printf("[APP] init: ADC...\r\n");
|
||||
Adc::init();
|
||||
printf("[APP] init: CANFD...\r\n");
|
||||
CanFd::init();
|
||||
printf("[APP] init: OtaBootRequest...\r\n");
|
||||
OtaBootRequest::init();
|
||||
printf("[APP] init: AppSpiCommand...\r\n");
|
||||
mark(kUsart0);
|
||||
|
||||
/* SPI 尽早就绪:OTA FINISH 跳转后 ESP32 用 HELLO 探活,不能等 CAN 上电延时 */
|
||||
AppSpiCommandBus.init();
|
||||
printf("[APP] init: TaskManager...\r\n");
|
||||
mark(kAppSpi);
|
||||
|
||||
Adc::init();
|
||||
mark(kAdc);
|
||||
|
||||
CanFd::init();
|
||||
/* L0 通过才置 canfd 位 */
|
||||
if (CanFd::selfTestL0()) {
|
||||
mark(kCanFd);
|
||||
}
|
||||
|
||||
OtaBootRequest::init();
|
||||
mark(kOtaBootReq);
|
||||
|
||||
TaskManager::init();
|
||||
printf("[APP] init: SchedulerTimer...\r\n");
|
||||
mark(kTaskManager);
|
||||
|
||||
SchedulerTimer::init();
|
||||
printf("[APP] init: EthercatSlave...\r\n");
|
||||
mark(kScheduler);
|
||||
|
||||
EthercatSlave::init();
|
||||
if (EthercatSlave::isEscReady()) {
|
||||
printf("[APP] init: EthercatSync...\r\n");
|
||||
mark(kEthercat);
|
||||
EthercatSync::init();
|
||||
} else {
|
||||
printf("[APP] init: EthercatSync SKIPPED (ESC not ready)\r\n");
|
||||
mark(kEthercatSync);
|
||||
}
|
||||
printf("[APP] init: Watchdog...\r\n");
|
||||
|
||||
Watchdog::init();
|
||||
printf("[APP] init: all done\r\n");
|
||||
mark(kWatchdog);
|
||||
|
||||
report();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user