diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ae4b80..97511f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,10 +10,12 @@ target_sources(app PRIVATE src/SubSystem/Initer/app_init.c # Init子系统注册 src/SubSystem/Initer/SubInit/ui/init_ui.c + src/SubSystem/Initer/SubInit/storage/init_storage.c - # LED UI - src/Tools/ui/status_led.c + # tools + src/Tools/ui/status_led.c # LED UI + src/Tools/storage/sd_card.c ) target_include_directories(app PRIVATE @@ -21,8 +23,9 @@ target_include_directories(app PRIVATE # Init src/SubSystem/Initer - # LED UI - src/Tools/ui + # tools + src/Tools/ui # LED UI + src/Tools/storage ) # 自定义 iterable section(app_init_entry),供 APP_INIT 宏注册 diff --git a/src/SubSystem/Initer/SubInit/storage/init_storage.c b/src/SubSystem/Initer/SubInit/storage/init_storage.c new file mode 100644 index 0000000..f9827b9 --- /dev/null +++ b/src/SubSystem/Initer/SubInit/storage/init_storage.c @@ -0,0 +1,7 @@ +// +// Created by misaki on 2026/10/27. +// +#include "app_init.h" +#include "sd_card.h" + +APP_INIT(sd_mount, APP_INIT_CORE, 5, NULL); // 核心底座,无卡不致命,但必然会导致无法记录日志和读写参数 diff --git a/src/SubSystem/Initer/app_init.h b/src/SubSystem/Initer/app_init.h index f869320..51a4bab 100644 --- a/src/SubSystem/Initer/app_init.h +++ b/src/SubSystem/Initer/app_init.h @@ -8,8 +8,7 @@ * 只管理"应用/服务"初始化;驱动/板级仍由Zephyr的DEVICE_DT_DEFINE负责。 */ -#ifndef APP_INIT_H_ -#define APP_INIT_H_ +#pragma once #include #include @@ -21,7 +20,7 @@ extern "C" { #endif -#define APP_INIT_MAX 64 +#define APP_INIT_MAX 64 // 初始化init项的最大值 // 粗粒度阶段(按枚举值顺序执行),阶段内再按 prio、最后按依赖 enum app_init_phase { @@ -124,5 +123,3 @@ void app_init_dump(void); #ifdef __cplusplus } #endif - -#endif // APP_INIT_H_ diff --git a/src/Tools/storage/sd_card.c b/src/Tools/storage/sd_card.c new file mode 100644 index 0000000..4e9a045 --- /dev/null +++ b/src/Tools/storage/sd_card.c @@ -0,0 +1,58 @@ +/* + * SD卡挂载实现 + */ + +#include +#include +#include +#include +#include + +#include "sd_card.h" + +LOG_MODULE_REGISTER(sd_card, LOG_LEVEL_INF); + +#define SD_DISK_NAME "SD" +#define SD_MOUNT_POINT "/SD:" + +static FATFS s_fatfs; +static struct fs_mount_t s_mp = { + .type = FS_FATFS, + .fs_data = &s_fatfs, + .mnt_point = SD_MOUNT_POINT, +}; + +static K_MUTEX_DEFINE(s_lock); +static bool s_mounted; + +int sd_mount(void) +{ + int ret = 0; + + k_mutex_lock(&s_lock, K_FOREVER); + + if (s_mounted) { + goto out; + } + if (disk_access_ioctl(SD_DISK_NAME, DISK_IOCTL_CTRL_INIT, NULL) != 0) { + LOG_ERR("SD init failed (no card?)"); + ret = -ENODEV; + goto out; + } + ret = fs_mount(&s_mp); + if (ret != 0) { + LOG_ERR("FAT/exFAT mount failed: %d", ret); + goto out; + } + s_mounted = true; + LOG_INF("SD mounted at %s", SD_MOUNT_POINT); + +out: + k_mutex_unlock(&s_lock); + return ret; +} + +bool sd_is_mounted(void) +{ + return s_mounted; +} diff --git a/src/Tools/storage/sd_card.h b/src/Tools/storage/sd_card.h new file mode 100644 index 0000000..5dc3203 --- /dev/null +++ b/src/Tools/storage/sd_card.h @@ -0,0 +1,26 @@ +/* + * SD卡挂载。 + * + * Zephyr 的 FATFS 同一份卷只能挂载一次,故统一走这里挂到 "/SD:",其它模块 + * 用 sd_mount()(幂等)即可,不要再各自fs_mount + */ + +#pragma once +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * 挂载 SD 卡上的 FAT/exFAT 到 /SD:(幂等,多次调用只有第一次真正挂载)。 + * @return 0 已挂载;负值为错误码(无卡/-ENODEV、挂载失败等)。 + */ +int sd_mount(void); + +// 是否已挂载 +bool sd_is_mounted(void); + +#ifdef __cplusplus +} +#endif diff --git a/src/main.cpp b/src/main.cpp index b88fa37..1875fc4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,7 @@ #include #include "app_init.h" +#include "status_led.h" LOG_MODULE_REGISTER(app, LOG_LEVEL_INF); @@ -17,10 +18,19 @@ int main() { int ret = app_init_run(); // 初始化init子系统 - if (ret != 0) { + if (ret != 0) { // init子系统初始化失败 + status_led_set_effect(STATUS_LED_BLINK_FAST); // 快闪LED_SIGNL提示 + LOG_ERR("init failed: %d", ret); return ret; } + if (app_init_failed_count() > 0 || app_init_skipped_count() > 0) { + // 若可选模块失败,则慢闪 + status_led_set_effect(STATUS_LED_BLINK_SLOW); + } else { + status_led_set_effect(STATUS_LED_BREATHE_SLOW); + } + while (true) { k_msleep(1000); }