1. 接入了sd_mount,将sd挂载到文件系统当中。

This commit is contained in:
Misaki
2026-10-27 22:52:23 +08:00
parent 40c593200c
commit 4d416419ef
6 changed files with 111 additions and 10 deletions
+7 -4
View File
@@ -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 宏注册
@@ -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); // 核心底座,无卡不致命,但必然会导致无法记录日志和读写参数
+2 -5
View File
@@ -8,8 +8,7 @@
* 只管理"应用/服务"初始化;驱动/板级仍由Zephyr的DEVICE_DT_DEFINE负责。
*/
#ifndef APP_INIT_H_
#define APP_INIT_H_
#pragma once
#include <stdbool.h>
#include <stddef.h>
@@ -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_
+58
View File
@@ -0,0 +1,58 @@
/*
* SD卡挂载实现
*/
#include <zephyr/kernel.h>
#include <zephyr/fs/fs.h>
#include <zephyr/storage/disk_access.h>
#include <zephyr/logging/log.h>
#include <ff.h>
#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;
}
+26
View File
@@ -0,0 +1,26 @@
/*
* SD卡挂载。
*
* Zephyr 的 FATFS 同一份卷只能挂载一次,故统一走这里挂到 "/SD:",其它模块
* 用 sd_mount()(幂等)即可,不要再各自fs_mount
*/
#pragma once
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* 挂载 SD 卡上的 FAT/exFAT 到 /SD:(幂等,多次调用只有第一次真正挂载)。
* @return 0 已挂载;负值为错误码(无卡/-ENODEV、挂载失败等)。
*/
int sd_mount(void);
// 是否已挂载
bool sd_is_mounted(void);
#ifdef __cplusplus
}
#endif
+11 -1
View File
@@ -9,6 +9,7 @@
#include <zephyr/logging/log.h>
#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);
}