Files
STM32F4-Base/App/cfg_file_store_mcu.c
T

197 lines
5.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* 模块名称:cfg_file_store (MCU 后端)
* 模块功能:cfg_file 存储后端——NAND 上 FatFS 文件 /config.ini(卷根,8.3 合规);
* 原子写 = 临时文件 /config.TMP + f_rename;mtime 变更令牌 =
* (fdate << 16) | fmtime;旧版遗留文件 /cfg/netcfg.bin 供迁移读取
* 适用平台:STM32F407ZGTx
* 作者:王建锋
* 创建日期:2026-09-14
* 修改记录:
* v1.0 2026-09-14 王建锋 创建初始版本
* v1.1 2026-09-18 王建锋 增 cfg_store_alloc / cfg_store_free / cfg_store_heap_free:
* 共享核心临时缓冲改走 FreeRTOS 堆(P0-1,MicroLIB C 堆仅 512B)
*/
/* 头文件包含区 */
#include <string.h>
#include "FreeRTOS.h"
#include "cfg_file_store.h"
#include "ff.h"
/* 静态变量定义区 */
static const char s_path[] = "/config.ini";
static const char s_path_tmp[] = "/config.TMP";
static const char s_path_legacy[] = "/cfg/netcfg.bin";
/* 函数定义区 */
/*
* 函数功能:读文件(最多 cap 字节)
* 入口参数:p_path - 文件路径 const char *
* p_buf - 接收缓冲(不可为 NULL) uint8_t *
* cap - 缓冲容量 size_t
* p_nread - 实际读字节数输出(不可为 NULL) size_t *
* 返回值:0 - 成功;-1 - 文件缺失/读失败 int
* 限定条件:卷已挂载(未挂载时 f_open 返回 FR_NO_SYSTEM,转 -1)
*/
int cfg_store_read(const char *p_path, uint8_t *p_buf, size_t cap, size_t *p_nread) {
FIL f;
FRESULT fr;
UINT br = 0u;
if (p_buf == NULL || p_nread == NULL) {
return -1;
}
fr = f_open(&f, p_path, FA_READ);
if (fr != FR_OK) {
return -1;
}
fr = f_read(&f, p_buf, (UINT)cap, &br);
(void)f_close(&f);
if (fr != FR_OK) {
return -1;
}
*p_nread = (size_t)br;
return 0;
}
/*
* 函数功能:原子写文件(先写 /config.TMP,成功后 f_rename 覆盖目标)
* 入口参数:p_path - 目标文件路径 const char *
* p_buf - 待写数据(不可为 NULL) const uint8_t *
* len - 数据长度 size_t
* 返回值:0 - 成功;-1 - 写/改名失败(目标文件保持不动,临时文件清理) int
* 限定条件:卷已挂载;中途失败(含掉电)不损坏目标文件
*/
int cfg_store_write_atomic(const char *p_path, const uint8_t *p_buf, size_t len) {
FIL f;
FRESULT fr;
UINT bw = 0u;
if (p_buf == NULL) {
return -1;
}
fr = f_open(&f, s_path_tmp, FA_WRITE | FA_CREATE_ALWAYS);
if (fr != FR_OK) {
return -1;
}
fr = f_write(&f, (const void *)p_buf, (UINT)len, &bw);
(void)f_close(&f);
if (fr != FR_OK || bw != (UINT)len) {
(void)f_unlink(s_path_tmp);
return -1;
}
fr = f_rename(s_path_tmp, p_path);
if (fr != FR_OK) {
(void)f_unlink(s_path_tmp);
return -1;
}
return 0;
}
/*
* 函数功能:文件状态(size + mtime 变更令牌)
* 入口参数:p_path - 文件路径 const char *
* p_st - 输出(不可为 NULL) cfg_file_stat_t *
* 返回值:0 - 成功;-1 - 文件缺失/读失败 int
* 限定条件:无
* 函数说明:FAT 时间分钟级粒度——同分钟内同长度重写不可检出(可接受,
* 见《配置文件设计说明》§6.2 mtime 令牌说明)
*/
int cfg_store_stat(const char *p_path, cfg_file_stat_t *p_st) {
FILINFO fno;
FRESULT fr;
if (p_st == NULL) {
return -1;
}
fr = f_stat(p_path, &fno);
if (fr != FR_OK) {
return -1;
}
p_st->size = (uint32_t)fno.fsize;
p_st->mtime = (uint32_t)(((uint32_t)fno.fdate << 16) | (uint32_t)fno.ftime);
return 0;
}
/*
* 函数功能:删除文件
* 入口参数:p_path - 文件路径 const char *
* 返回值:0 - 成功;-1 - 文件缺失/删除失败 int
* 限定条件:无
*/
int cfg_store_remove(const char *p_path) {
if (f_unlink(p_path) != FR_OK) {
return -1;
}
return 0;
}
/*
* 函数功能:取配置文件路径
* 入口参数:无
* 返回值:"/config.ini"(卷根,8.3 合规:CONFIG + INI) const char *
* 限定条件:无
*/
const char *cfg_store_config_path(void) {
return s_path;
}
/*
* 函数功能:取配置文件临时文件路径
* 入口参数:无
* 返回值:"/config.TMP"(卷根,8.3 合规:CONFIG + TMP) const char *
* 限定条件:无
*/
const char *cfg_store_tmp_path(void) {
return s_path_tmp;
}
/*
* 函数功能:取旧版配置遗留文件路径
* 入口参数:无
* 返回值:"/cfg/netcfg.bin"(旧 NCFG 二进制格式) const char *
* 限定条件:无
*/
const char *cfg_store_legacy_path(void) {
return s_path_legacy;
}
/*
* 函数功能:分配共享核心临时缓冲(FreeRTOS 堆)
* 入口参数:size - 字节数 size_t
* 返回值:缓冲指针(内容未清零);失败返回 NULL void *
* 限定条件:任务上下文调用(heap_4 内部临界区自护);释放必须用 cfg_store_free
* 函数说明:MCU 端 MicroLIB C 堆仅 512B(startup Heap_Size=0x200),共享核心的 KB 级
* 缓冲若走 C 库 malloc 必然返回 NULL,故统一由本接口取 FreeRTOS 堆(P0-1)
*/
void *cfg_store_alloc(size_t size) {
return pvPortMalloc(size);
}
/*
* 函数功能:释放 cfg_store_alloc 分配的临时缓冲
* 入口参数:p_buf - 缓冲指针(NULL 安全) void *
* 返回值:无
* 限定条件:p_buf 必须来自 cfg_store_alloc(禁止释放栈地址或 C 库指针)
*/
void cfg_store_free(void *p_buf) {
if (p_buf != NULL) {
vPortFree(p_buf);
}
}
/*
* 函数功能:FreeRTOS 堆当前空闲字节数(分配失败诊断)
* 入口参数:无
* 返回值:空闲字节数(未计入碎片影响) uint32_t
* 限定条件:无
*/
uint32_t cfg_store_heap_free(void) {
return (uint32_t)xPortGetFreeHeapSize();
}