1. 接入了Initer子系统,用于解决初始化依赖问题
2. 接入了led_ui,用于显示系统的实时状态
This commit is contained in:
+15
-1
@@ -5,14 +5,28 @@ project(Sakimi-UAV)
|
|||||||
|
|
||||||
target_sources(app PRIVATE
|
target_sources(app PRIVATE
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
|
|
||||||
|
# Init子系统 负责处理各种子系统的init以及init依赖拓扑
|
||||||
|
src/SubSystem/Initer/app_init.c
|
||||||
|
# Init子系统注册
|
||||||
|
src/SubSystem/Initer/SubInit/ui/init_ui.c
|
||||||
|
|
||||||
|
|
||||||
|
# LED UI
|
||||||
|
src/Tools/ui/status_led.c
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(app PRIVATE
|
target_include_directories(app PRIVATE
|
||||||
src
|
src
|
||||||
|
# Init
|
||||||
|
src/SubSystem/Initer
|
||||||
|
|
||||||
|
# LED UI
|
||||||
|
src/Tools/ui
|
||||||
)
|
)
|
||||||
|
|
||||||
# 自定义 iterable section(app_init_entry),供 APP_INIT 宏注册
|
# 自定义 iterable section(app_init_entry),供 APP_INIT 宏注册
|
||||||
# zephyr_linker_sources(SECTIONS src/sys/app_init/app_init.ld)
|
zephyr_linker_sources(SECTIONS src/SubSystem/Initer/app_init.ld)
|
||||||
|
|
||||||
# The vendored MAVLink generated headers use packed structs; the resulting
|
# The vendored MAVLink generated headers use packed structs; the resulting
|
||||||
# address-of-packed-member warnings are harmless.
|
# address-of-packed-member warnings are harmless.
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
//
|
||||||
|
// Created by misaki on 2026/10/27.
|
||||||
|
//
|
||||||
|
#include "app_init.h"
|
||||||
|
#include "status_led.h"
|
||||||
|
|
||||||
|
APP_INIT(status_led_init, APP_INIT_SERVICE, 0, NULL);
|
||||||
@@ -0,0 +1,387 @@
|
|||||||
|
/*
|
||||||
|
* 应用层init管理器实现
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
#include <zephyr/sys/time_units.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "app_init.h"
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(app_init, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
// 状态标记的强调显示:反显(保留后端给整行上的级别色,用 27 关闭反显而非 0 复位)
|
||||||
|
#if defined(CONFIG_LOG_BACKEND_SHOW_COLOR) && CONFIG_LOG_BACKEND_SHOW_COLOR
|
||||||
|
#define INIT_HL_ON "\x1b[7m"
|
||||||
|
#define INIT_HL_OFF "\x1b[27m"
|
||||||
|
#else
|
||||||
|
#define INIT_HL_ON ""
|
||||||
|
#define INIT_HL_OFF ""
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static const struct app_init_entry *s_entries[APP_INIT_MAX];
|
||||||
|
static const struct app_init_entry *s_order[APP_INIT_MAX];
|
||||||
|
static struct app_init_info s_info[APP_INIT_MAX];
|
||||||
|
static int s_order_of[APP_INIT_MAX];
|
||||||
|
static int s_count;
|
||||||
|
static bool s_prepared;
|
||||||
|
static bool s_ran;
|
||||||
|
|
||||||
|
static const char *phase_name(uint8_t phase)
|
||||||
|
{
|
||||||
|
switch (phase) {
|
||||||
|
case APP_INIT_CORE:
|
||||||
|
return "CORE";
|
||||||
|
case APP_INIT_FIXUP:
|
||||||
|
return "FIXUP";
|
||||||
|
case APP_INIT_SERVICE:
|
||||||
|
return "SERVICE";
|
||||||
|
case APP_INIT_CONFIG:
|
||||||
|
return "CONFIG";
|
||||||
|
case APP_INIT_RUNTIME:
|
||||||
|
return "RUNTIME";
|
||||||
|
case APP_INIT_IO:
|
||||||
|
return "IO";
|
||||||
|
default:
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmp_entry(const struct app_init_entry *a,
|
||||||
|
const struct app_init_entry *b)
|
||||||
|
{
|
||||||
|
if (a->phase != b->phase) {
|
||||||
|
return (int)a->phase - (int)b->phase;
|
||||||
|
}
|
||||||
|
if (a->prio != b->prio) {
|
||||||
|
return (int)a->prio - (int)b->prio;
|
||||||
|
}
|
||||||
|
return strcmp(a->name, b->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int find_entry(const char *name)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < s_count; i++) {
|
||||||
|
if (strcmp(s_entries[i]->name, name) == 0) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 从逗号/空白分隔的依赖串里取下一个名字。
|
||||||
|
* 参数 pp:游标指针的地址;tok/n:输出缓冲。
|
||||||
|
* 返回:true 取到,false 结束。
|
||||||
|
*/
|
||||||
|
static bool next_dep(const char **pp, char *tok, size_t n)
|
||||||
|
{
|
||||||
|
const char *p = *pp;
|
||||||
|
size_t k = 0;
|
||||||
|
|
||||||
|
if (p == NULL) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
while (*p == ',' || *p == ' ' || *p == '\t') {
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
if (*p == '\0') {
|
||||||
|
*pp = p;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
while (*p != '\0' && *p != ',' && *p != ' ' && *p != '\t') {
|
||||||
|
if (k + 1 < n) {
|
||||||
|
tok[k++] = *p;
|
||||||
|
}
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
tok[k] = '\0';
|
||||||
|
while (*p == ' ' || *p == '\t') {
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
if (*p == ',') {
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
*pp = p;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool deps_has(const char *deps, const char *name)
|
||||||
|
{
|
||||||
|
const char *p = deps;
|
||||||
|
char tok[32];
|
||||||
|
|
||||||
|
while (next_dep(&p, tok, sizeof(tok))) {
|
||||||
|
if (strcmp(tok, name) == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int prepare(void)
|
||||||
|
{
|
||||||
|
if (s_prepared) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
s_count = 0;
|
||||||
|
STRUCT_SECTION_FOREACH(app_init_entry, e) {
|
||||||
|
if (s_count >= APP_INIT_MAX) {
|
||||||
|
LOG_ERR("too many init entries (max %d)", APP_INIT_MAX);
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
s_entries[s_count++] = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 段为空说明链接片段没生效(常见于增量 configure 重复 include)
|
||||||
|
if (s_count == 0) {
|
||||||
|
LOG_ERR("no init entries (linker section empty; clean rebuild?)");
|
||||||
|
return -ENOENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重名检查
|
||||||
|
for (int i = 0; i < s_count; i++) {
|
||||||
|
for (int j = i + 1; j < s_count; j++) {
|
||||||
|
if (strcmp(s_entries[i]->name, s_entries[j]->name) == 0) {
|
||||||
|
LOG_ERR("duplicate init name '%s'",
|
||||||
|
s_entries[i]->name);
|
||||||
|
return -EEXIST;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 依赖校验 + 入度
|
||||||
|
int indeg[APP_INIT_MAX] = { 0 };
|
||||||
|
|
||||||
|
for (int i = 0; i < s_count; i++) {
|
||||||
|
const char *p = s_entries[i]->deps;
|
||||||
|
char tok[32];
|
||||||
|
|
||||||
|
while (next_dep(&p, tok, sizeof(tok))) {
|
||||||
|
if (find_entry(tok) < 0) {
|
||||||
|
LOG_ERR("init '%s': unknown dependency '%s'",
|
||||||
|
s_entries[i]->name, tok);
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
indeg[i]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kahn 拓扑排序;同层按 phase/prio/name 稳定选择
|
||||||
|
bool done[APP_INIT_MAX] = { false };
|
||||||
|
|
||||||
|
for (int k = 0; k < s_count; k++) {
|
||||||
|
int best = -1;
|
||||||
|
|
||||||
|
for (int i = 0; i < s_count; i++) {
|
||||||
|
if (done[i] || indeg[i] != 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (best < 0 ||
|
||||||
|
cmp_entry(s_entries[i], s_entries[best]) < 0) {
|
||||||
|
best = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (best < 0) {
|
||||||
|
LOG_ERR("init dependency cycle; unresolved:");
|
||||||
|
for (int i = 0; i < s_count; i++) {
|
||||||
|
if (!done[i]) {
|
||||||
|
LOG_ERR(" - %s", s_entries[i]->name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
done[best] = true;
|
||||||
|
s_order[k] = s_entries[best];
|
||||||
|
s_order_of[best] = k;
|
||||||
|
|
||||||
|
for (int i = 0; i < s_count; i++) {
|
||||||
|
if (!done[i] &&
|
||||||
|
deps_has(s_entries[i]->deps, s_entries[best]->name)) {
|
||||||
|
indeg[i]--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int k = 0; k < s_count; k++) {
|
||||||
|
const struct app_init_entry *e = s_order[k];
|
||||||
|
|
||||||
|
s_info[k].name = e->name;
|
||||||
|
s_info[k].phase = e->phase;
|
||||||
|
s_info[k].prio = e->prio;
|
||||||
|
s_info[k].required = (e->flags & APP_INIT_F_REQUIRED) != 0;
|
||||||
|
s_info[k].deps = e->deps;
|
||||||
|
s_info[k].order = k;
|
||||||
|
s_info[k].status = APP_INIT_PENDING;
|
||||||
|
s_info[k].ret = 0;
|
||||||
|
s_info[k].duration_us = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
s_prepared = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int app_init_run(void)
|
||||||
|
{
|
||||||
|
int ret = prepare();
|
||||||
|
|
||||||
|
if (ret != 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
if (s_ran) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
s_ran = true;
|
||||||
|
|
||||||
|
uint64_t t_all = k_cycle_get_64();
|
||||||
|
int first_err = 0, nok = 0, nskip = 0, nfail = 0;
|
||||||
|
|
||||||
|
for (int k = 0; k < s_count; k++) {
|
||||||
|
const struct app_init_entry *e = s_order[k];
|
||||||
|
struct app_init_info *info = &s_info[k];
|
||||||
|
|
||||||
|
// 依赖是否都成功
|
||||||
|
bool deps_ok = true;
|
||||||
|
const char *p = e->deps;
|
||||||
|
char tok[32];
|
||||||
|
|
||||||
|
while (next_dep(&p, tok, sizeof(tok))) {
|
||||||
|
int j = find_entry(tok);
|
||||||
|
int oj = (j >= 0) ? s_order_of[j] : -1;
|
||||||
|
|
||||||
|
if (oj < 0 || s_info[oj].status != APP_INIT_OK) {
|
||||||
|
deps_ok = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!deps_ok) {
|
||||||
|
info->status = APP_INIT_SKIPPED;
|
||||||
|
nskip++;
|
||||||
|
LOG_WRN("init: " INIT_HL_ON "[skip]" INIT_HL_OFF
|
||||||
|
" %-24s (dependency not ok)", e->name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t t0 = k_cycle_get_64();
|
||||||
|
int r = (e->fn != NULL) ? e->fn() : 0;
|
||||||
|
|
||||||
|
info->duration_us =
|
||||||
|
(uint32_t)k_cyc_to_us_floor64(k_cycle_get_64() - t0);
|
||||||
|
info->ret = r;
|
||||||
|
|
||||||
|
if (r == 0) {
|
||||||
|
info->status = APP_INIT_OK;
|
||||||
|
nok++;
|
||||||
|
LOG_INF("init: " INIT_HL_ON "[ ok ]" INIT_HL_OFF
|
||||||
|
" %-24s %6u us", e->name, info->duration_us);
|
||||||
|
} else {
|
||||||
|
info->status = APP_INIT_FAILED;
|
||||||
|
nfail++;
|
||||||
|
LOG_ERR("init: " INIT_HL_ON "[FAIL]" INIT_HL_OFF
|
||||||
|
" %-24s %6u us (ret %d)", e->name,
|
||||||
|
info->duration_us, r);
|
||||||
|
if (info->required && first_err == 0) {
|
||||||
|
first_err = r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_INF("init: %d ok, %d skipped, %d failed, %u us total", nok, nskip,
|
||||||
|
nfail, (uint32_t)k_cyc_to_us_floor64(k_cycle_get_64() - t_all));
|
||||||
|
return first_err;
|
||||||
|
}
|
||||||
|
|
||||||
|
int app_init_count(void)
|
||||||
|
{
|
||||||
|
return s_prepared ? s_count : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const struct app_init_info *app_init_get(int index)
|
||||||
|
{
|
||||||
|
if (index < 0 || index >= s_count) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return &s_info[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
int app_init_failed_count(void)
|
||||||
|
{
|
||||||
|
int n = 0;
|
||||||
|
|
||||||
|
for (int k = 0; k < s_count; k++) {
|
||||||
|
if (s_info[k].status == APP_INIT_FAILED) {
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
int app_init_skipped_count(void)
|
||||||
|
{
|
||||||
|
int n = 0;
|
||||||
|
|
||||||
|
for (int k = 0; k < s_count; k++) {
|
||||||
|
if (s_info[k].status == APP_INIT_SKIPPED) {
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
int app_init_find(const char *name)
|
||||||
|
{
|
||||||
|
if (!s_prepared || name == NULL) {
|
||||||
|
return -ENOENT;
|
||||||
|
}
|
||||||
|
for (int k = 0; k < s_count; k++) {
|
||||||
|
if (strcmp(s_info[k].name, name) == 0) {
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -ENOENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *app_init_phase_name(uint8_t phase)
|
||||||
|
{
|
||||||
|
return phase_name(phase);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *app_init_status_name(int status)
|
||||||
|
{
|
||||||
|
switch (status) {
|
||||||
|
case APP_INIT_PENDING:
|
||||||
|
return "pending";
|
||||||
|
case APP_INIT_OK:
|
||||||
|
return "ok";
|
||||||
|
case APP_INIT_SKIPPED:
|
||||||
|
return "skipped";
|
||||||
|
case APP_INIT_FAILED:
|
||||||
|
return "failed";
|
||||||
|
default:
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void app_init_dump(void)
|
||||||
|
{
|
||||||
|
if (!s_prepared) {
|
||||||
|
LOG_WRN("init: not prepared");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_INF("init chain (%d):", s_count);
|
||||||
|
for (int k = 0; k < s_count; k++) {
|
||||||
|
const struct app_init_info *i = &s_info[k];
|
||||||
|
|
||||||
|
LOG_INF(" %2d. %-24s phase=%-7s prio=%3d req=%u deps=[%s] "
|
||||||
|
"status=%s ret=%d %u us",
|
||||||
|
i->order, i->name, phase_name(i->phase), i->prio,
|
||||||
|
i->required, i->deps ? i->deps : "",
|
||||||
|
app_init_status_name(i->status), i->ret,
|
||||||
|
i->duration_us);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
/*
|
||||||
|
* 应用层 init 管理器。
|
||||||
|
*
|
||||||
|
* 各模块写完int xxx_init(void)后,在函数附近贴一个APP_INIT(...)宏即可
|
||||||
|
* 加入启动序列,无需维护中央清单。启动时按阶段+优先级排序,再按命名依赖
|
||||||
|
* 做拓扑排序,逐个执行并打印 名字/结果/耗时。
|
||||||
|
*
|
||||||
|
* 只管理"应用/服务"初始化;驱动/板级仍由Zephyr的DEVICE_DT_DEFINE负责。
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef APP_INIT_H_
|
||||||
|
#define APP_INIT_H_
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <zephyr/sys/iterable_sections.h>
|
||||||
|
#include <zephyr/sys/util.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define APP_INIT_MAX 64
|
||||||
|
|
||||||
|
// 粗粒度阶段(按枚举值顺序执行),阶段内再按 prio、最后按依赖
|
||||||
|
enum app_init_phase {
|
||||||
|
APP_INIT_CORE = 0, // 底座:SD/存储等
|
||||||
|
APP_INIT_FIXUP, // 驱动补丁
|
||||||
|
APP_INIT_SERVICE, // 电池/标定/参数注册
|
||||||
|
APP_INIT_CONFIG, // 参数加载/应用
|
||||||
|
APP_INIT_RUNTIME, // imu_fast / fc_task 等运行期
|
||||||
|
APP_INIT_IO, // 日志/遥测
|
||||||
|
APP_INIT_PHASE_COUNT,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum app_init_status {
|
||||||
|
APP_INIT_PENDING = 0,
|
||||||
|
APP_INIT_OK,
|
||||||
|
APP_INIT_SKIPPED, // 依赖未成功,跳过
|
||||||
|
APP_INIT_FAILED,
|
||||||
|
};
|
||||||
|
|
||||||
|
#define APP_INIT_F_REQUIRED BIT(0)
|
||||||
|
|
||||||
|
struct app_init_entry {
|
||||||
|
const char *name;
|
||||||
|
int (*fn)(void);
|
||||||
|
uint8_t phase;
|
||||||
|
int16_t prio;
|
||||||
|
uint8_t flags;
|
||||||
|
const char *deps; // 逗号分隔的依赖名字
|
||||||
|
};
|
||||||
|
|
||||||
|
// 供调试 / 上位机查询的解析结果(按执行顺序排列)
|
||||||
|
struct app_init_info {
|
||||||
|
const char *name;
|
||||||
|
uint8_t phase;
|
||||||
|
int16_t prio;
|
||||||
|
bool required;
|
||||||
|
const char *deps;
|
||||||
|
int order; // 执行序号
|
||||||
|
int status; // enum app_init_status
|
||||||
|
int ret; // init 函数返回值
|
||||||
|
uint32_t duration_us; // 执行耗时(µs)
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 登记一个可选初始化(失败仅警告,其依赖者跳过)。
|
||||||
|
* @param fn 初始化函数(int fn(void))。
|
||||||
|
* @param phase 阶段(enum app_init_phase)。
|
||||||
|
* @param prio 同阶段内排序(小的先执行)。
|
||||||
|
* @param deps 依赖名字,逗号分隔;无依赖传 NULL。
|
||||||
|
*/
|
||||||
|
#define APP_INIT(init_fn, init_phase, init_prio, init_deps) \
|
||||||
|
STRUCT_SECTION_ITERABLE(app_init_entry, app_init_##init_fn) = { \
|
||||||
|
.name = #init_fn, \
|
||||||
|
.fn = init_fn, \
|
||||||
|
.phase = (init_phase), \
|
||||||
|
.prio = (init_prio), \
|
||||||
|
.flags = 0, \
|
||||||
|
.deps = (init_deps), \
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 登记一个必需初始化(失败则 app_init_run 返回该错误码)。
|
||||||
|
* 参数同 APP_INIT。
|
||||||
|
*/
|
||||||
|
#define APP_INIT_REQUIRED(init_fn, init_phase, init_prio, init_deps) \
|
||||||
|
STRUCT_SECTION_ITERABLE(app_init_entry, app_init_##init_fn) = { \
|
||||||
|
.name = #init_fn, \
|
||||||
|
.fn = init_fn, \
|
||||||
|
.phase = (init_phase), \
|
||||||
|
.prio = (init_prio), \
|
||||||
|
.flags = APP_INIT_F_REQUIRED, \
|
||||||
|
.deps = (init_deps), \
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 解析依赖并执行全部初始化。
|
||||||
|
* 返回:0 成功(可选失败不影响);否则为首个必需项的错误码。
|
||||||
|
*/
|
||||||
|
int app_init_run(void);
|
||||||
|
|
||||||
|
// 已解析的条目数(app_init_run 之后有效)
|
||||||
|
int app_init_count(void);
|
||||||
|
|
||||||
|
// 按执行顺序取条目信息;越界返回 NULL
|
||||||
|
const struct app_init_info *app_init_get(int index);
|
||||||
|
|
||||||
|
// 按名字查执行序号;找不到返回 -ENOENT
|
||||||
|
int app_init_find(const char *name);
|
||||||
|
|
||||||
|
// 执行后的失败/跳过计数(供状态指示/上位机)
|
||||||
|
int app_init_failed_count(void);
|
||||||
|
int app_init_skipped_count(void);
|
||||||
|
|
||||||
|
const char *app_init_phase_name(uint8_t phase);
|
||||||
|
const char *app_init_status_name(int status);
|
||||||
|
|
||||||
|
// 把依赖链/结果打印到日志
|
||||||
|
void app_init_dump(void);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // APP_INIT_H_
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#include <zephyr/linker/iterable_sections.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 幂等守卫:Zephyr 的 snippets-sections.ld 在增量 configure 后可能重复
|
||||||
|
* #include 本文件;重复展开会让 _start/_end 被第二次定义到段尾,导致
|
||||||
|
* 运行期 STRUCT_SECTION_FOREACH 看到空段(init: 0 ok)。
|
||||||
|
*/
|
||||||
|
#ifndef APP_INIT_ENTRY_LD_INCLUDED
|
||||||
|
#define APP_INIT_ENTRY_LD_INCLUDED
|
||||||
|
|
||||||
|
ITERABLE_SECTION_ROM(app_init_entry, Z_LINK_ITERABLE_SUBALIGN)
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
/*
|
||||||
|
* 状态 LED 与用户按键实现。说明见 status_led.h。
|
||||||
|
*
|
||||||
|
* PB12/PC13 没有定时器 PWM 输出,故用 k_timer 做 100Hz 软件 PWM
|
||||||
|
* (10ms 周期、100 级占空比)。ISR 只做"计数 + 写 GPIO"。
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/drivers/gpio.h>
|
||||||
|
#include <zephyr/sys/util.h>
|
||||||
|
|
||||||
|
#include "status_led.h"
|
||||||
|
|
||||||
|
// 均为低有效:写 1 = 点亮 / 按下
|
||||||
|
static const struct gpio_dt_spec s_led_state =
|
||||||
|
GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
|
||||||
|
static const struct gpio_dt_spec s_led_signl =
|
||||||
|
GPIO_DT_SPEC_GET(DT_ALIAS(led1), gpios);
|
||||||
|
static const struct gpio_dt_spec s_user_btn =
|
||||||
|
GPIO_DT_SPEC_GET(DT_NODELABEL(user_bt1), gpios);
|
||||||
|
|
||||||
|
#define PWM_TICK_US 100 // 10kHz 软件 PWM tick
|
||||||
|
#define PWM_PERIOD 100 // 100 ticks = 10ms = 100Hz
|
||||||
|
#define SIGNAL_HOLD_MS 50 // CDC 活动后 SIGNAL 保持时间
|
||||||
|
|
||||||
|
// 一个周期的正弦亮度表(0..100,0→max→0)
|
||||||
|
static const uint8_t s_sine[64] = {
|
||||||
|
0, 0, 1, 2, 4, 6, 8, 11, 15, 18, 22, 26, 31, 35, 40, 45,
|
||||||
|
50, 55, 60, 65, 69, 74, 78, 82, 85, 89, 92, 94, 96, 98, 99, 100,
|
||||||
|
100, 100, 99, 98, 96, 94, 92, 89, 85, 82, 78, 74, 69, 65, 60, 55,
|
||||||
|
50, 45, 40, 35, 31, 26, 22, 18, 15, 11, 8, 6, 4, 2, 1, 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
static volatile uint8_t s_effect;
|
||||||
|
static volatile uint8_t s_duty_state;
|
||||||
|
static volatile uint8_t s_duty_signal;
|
||||||
|
static volatile uint32_t s_ms; // 软件时基(ISR 每 10ms +10)
|
||||||
|
static volatile uint32_t s_signal_until;
|
||||||
|
static uint8_t s_tick;
|
||||||
|
|
||||||
|
static bool s_btn_prev;
|
||||||
|
static int64_t s_btn_ms;
|
||||||
|
|
||||||
|
static uint8_t effect_duty(uint8_t eff, uint32_t ms)
|
||||||
|
{
|
||||||
|
switch (eff) {
|
||||||
|
case STATUS_LED_OFF:
|
||||||
|
return 0;
|
||||||
|
case STATUS_LED_ON:
|
||||||
|
return 100;
|
||||||
|
case STATUS_LED_BLINK_SLOW:
|
||||||
|
return ((ms % 1000) < 500) ? 100 : 0;
|
||||||
|
case STATUS_LED_BLINK_FAST:
|
||||||
|
return ((ms % 200) < 100) ? 100 : 0;
|
||||||
|
case STATUS_LED_BREATHE_SLOW:
|
||||||
|
return s_sine[(ms % 3000) * 64 / 3000];
|
||||||
|
case STATUS_LED_BREATHE_FAST:
|
||||||
|
return s_sine[(ms % 800) * 64 / 800];
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void update_effects(void)
|
||||||
|
{
|
||||||
|
s_ms += 10;
|
||||||
|
s_duty_state = effect_duty(s_effect, s_ms);
|
||||||
|
s_duty_signal = (s_ms < s_signal_until) ? 100 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pwm_expiry(struct k_timer *timer)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(timer);
|
||||||
|
|
||||||
|
if (++s_tick >= PWM_PERIOD) {
|
||||||
|
s_tick = 0;
|
||||||
|
update_effects();
|
||||||
|
}
|
||||||
|
gpio_pin_set_dt(&s_led_state, (s_tick < s_duty_state) ? 1 : 0);
|
||||||
|
gpio_pin_set_dt(&s_led_signl, (s_tick < s_duty_signal) ? 1 : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
K_TIMER_DEFINE(s_pwm_timer, pwm_expiry, NULL);
|
||||||
|
|
||||||
|
int status_led_init(void)
|
||||||
|
{
|
||||||
|
if (!gpio_is_ready_dt(&s_led_state) || !gpio_is_ready_dt(&s_led_signl) ||
|
||||||
|
!gpio_is_ready_dt(&s_user_btn)) {
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
(void)gpio_pin_configure_dt(&s_led_state, GPIO_OUTPUT_INACTIVE);
|
||||||
|
(void)gpio_pin_configure_dt(&s_led_signl, GPIO_OUTPUT_INACTIVE);
|
||||||
|
(void)gpio_pin_configure_dt(&s_user_btn, GPIO_INPUT);
|
||||||
|
|
||||||
|
s_effect = STATUS_LED_OFF;
|
||||||
|
s_tick = 0;
|
||||||
|
s_ms = 0;
|
||||||
|
s_signal_until = 0;
|
||||||
|
|
||||||
|
k_timer_start(&s_pwm_timer, K_USEC(PWM_TICK_US), K_USEC(PWM_TICK_US));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void status_led_set_effect(enum status_led_effect eff)
|
||||||
|
{
|
||||||
|
s_effect = (uint8_t)eff;
|
||||||
|
}
|
||||||
|
|
||||||
|
void status_led_signal_activity(void)
|
||||||
|
{
|
||||||
|
s_signal_until = s_ms + SIGNAL_HOLD_MS;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool status_button_take_press(void)
|
||||||
|
{
|
||||||
|
int64_t now = k_uptime_get();
|
||||||
|
bool btn = gpio_pin_get_dt(&s_user_btn) > 0;
|
||||||
|
bool event = false;
|
||||||
|
|
||||||
|
if (btn && !s_btn_prev && (now - s_btn_ms) > 300) {
|
||||||
|
s_btn_ms = now;
|
||||||
|
event = true;
|
||||||
|
}
|
||||||
|
s_btn_prev = btn;
|
||||||
|
return event;
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* 状态 LED 与用户按键。
|
||||||
|
*
|
||||||
|
* led0(STATE):系统状态,软件 PWM 100Hz,支持 常灭/常亮/慢闪/快闪/慢呼吸/快呼吸。
|
||||||
|
* led1(SIGNAL):CDC 收发活动指示(有数据时亮约 50ms,否则灭)。
|
||||||
|
*
|
||||||
|
* 建议的 STATE 含义:
|
||||||
|
* 常灭 未初始化/关闭
|
||||||
|
* 常亮 严重错误(停止飞行)
|
||||||
|
* 慢闪 降级运行(如无 SD / 可选模块失败)
|
||||||
|
* 快闪 初始化关键模块失败
|
||||||
|
* 慢呼吸 正常待机/运行
|
||||||
|
* 快呼吸 已解锁/飞行中(业务层按需设置)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef STATUS_LED_H_
|
||||||
|
#define STATUS_LED_H_
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum status_led_effect {
|
||||||
|
STATUS_LED_OFF = 0,
|
||||||
|
STATUS_LED_ON,
|
||||||
|
STATUS_LED_BLINK_SLOW,
|
||||||
|
STATUS_LED_BLINK_FAST,
|
||||||
|
STATUS_LED_BREATHE_SLOW,
|
||||||
|
STATUS_LED_BREATHE_FAST,
|
||||||
|
STATUS_LED_EFFECT_COUNT,
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置 LED 输出与按键输入,并启动软件 PWM(init 时调用)。
|
||||||
|
* @return 0 成功,-ENODEV 有节点未就绪。
|
||||||
|
*/
|
||||||
|
int status_led_init(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置 STATE LED 的效果(掉电前一直保持)。
|
||||||
|
* @param eff 效果。
|
||||||
|
*/
|
||||||
|
void status_led_set_effect(enum status_led_effect eff);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通知 SIGNAL LED 有一次 CDC 收发活动(点亮约 50ms)。
|
||||||
|
*/
|
||||||
|
void status_led_signal_activity(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取一次"按键按下"事件(内部去抖 + 冷却),无事件返回 false。
|
||||||
|
* @return true 表示本次检测到一次按下。
|
||||||
|
*/
|
||||||
|
bool status_button_take_press(void);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // STATUS_LED_H_
|
||||||
+13
-2
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* main.cpp
|
* main.cpp
|
||||||
* 应用入口
|
* 应用入口
|
||||||
* 负责启用init管理和执行主循环
|
* 负责启用init管理和执行主循环
|
||||||
*/
|
*/
|
||||||
@@ -8,10 +8,21 @@
|
|||||||
#include <zephyr/device.h>
|
#include <zephyr/device.h>
|
||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
#include "app_init.h"
|
||||||
|
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(app, LOG_LEVEL_INF);
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
|
int ret = app_init_run(); // 初始化init子系统
|
||||||
|
|
||||||
|
if (ret != 0) {
|
||||||
|
LOG_ERR("init failed: %d", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
while (true) {
|
while (true) {
|
||||||
__NOP();
|
k_msleep(1000);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user