Flash 存储信息 API 使用指南
新增 API: log_manager_get_flash_info()
函数签名
int log_manager_get_flash_info(unsigned int *start_addr,
unsigned int *total_size,
unsigned int *free_size);
参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
start_addr | unsigned int * | 输出参数:Flash 分区起始地址(字节,十六进制) |
total_size | unsigned int * | 输出参数:Flash 分区总大小(字节) |
free_size | unsigned int * | 输出参数:Flash 剩余空间(字节) |
返回值
0- 成功获取信息-1- 获取失败(参数无效或分区信息不可用)
使用示例
基础使用
#include "log_manager.h"
#include <stdio.h>
void check_storage(void)
{
unsigned int start_addr, total_size, free_size;
// 获取 Flash 信息
if (log_manager_get_flash_info(&start_addr, &total_size, &free_size) == 0) {
printf("Flash 起始地址: 0x%08X\n", start_addr);
printf("Flash 总大小: %u 字节\n", total_size);
printf("Flash 剩余空间: %u 字节\n", free_size);
} else {
printf("获取 Flash 信息失败\n");
}
}
计算存储使用情况
void calculate_storage_usage(void)
{
unsigned int start_addr, total_size, free_size;
if (log_manager_get_flash_info(&start_addr, &total_size, &free_size) != 0) {
return;
}
// 计算已用空间
unsigned int used_size = total_size - free_size;
// 计算使用百分比
float usage_percent = (total_size > 0) ?
(100.0 * used_size / total_size) : 0;
printf("已用空间: %u 字节 (%.1f%%)\n", used_size, usage_percent);
printf("剩余空间: %u 字节 (%.1f%%)\n", free_size,
100.0 - usage_percent);
}
存储空间预警
void storage_warning_check(void)
{
unsigned int start_addr, total_size, free_size;
if (log_manager_get_flash_info(&start_addr, &total_size, &free_size) != 0) {
return;
}
float usage_percent = (100.0 * (total_size - free_size)) / total_size;
// 根据使用率显示不同级别的警告
if (usage_percent > 95.0f) {
printf("❌ 严重警告:存储空间即将耗尽!\n");
// 可以选择清理最旧的日志
log_manager_cleanup_old_files(5); // 只保留 5 个文件
} else if (usage_percent > 90.0f) {
printf("⚠️ 警告:存储使用率超过 90%%\n");
// 可以考虑清理或压缩旧日志
} else if (usage_percent > 70.0f) {
printf("ℹ️ 提示:存储使用率为 %.1f%%\n", usage_percent);
}
}
估算容量
void estimate_remaining_capacity(void)
{
unsigned int start_addr, total_size, free_size;
if (log_manager_get_flash_info(&start_addr, &total_size, &free_size) != 0) {
return;
}
// 假设平均每个日志文件为 100KB
unsigned int avg_file_size = 100 * 1024;
unsigned int estimated_files = free_size / avg_file_size;
// 假设每分钟产生 10KB 日志
unsigned int bytes_per_minute = 10 * 1024;
unsigned int estimated_days = (free_size / bytes_per_minute) / (24 * 60);
printf("剩余可容纳日志文件数: ~%u 个 (100KB/文件)\n",
estimated_files > 0 ? estimated_files : 1);
printf("剩余存储时间: ~%u 天 (假设 10KB/分钟)\n",
estimated_days > 0 ? estimated_days : 1);
}
实际应用场景
场景1:定期检查存储状态
void periodic_storage_check(void)
{
static int check_counter = 0;
// 每 100 条日志检查一次存储状态
if (++check_counter >= 100) {
check_counter = 0;
unsigned int start_addr, total_size, free_size;
if (log_manager_get_flash_info(&start_addr, &total_size, &free_size) == 0) {
float usage = (100.0 * (total_size - free_size)) / total_size;
log_manager_printf("Storage usage: %.1f%%, Free: %u KB",
usage, free_size / 1024);
}
}
}
场景2:动态调整日志级别
void adaptive_logging(const char *message)
{
unsigned int start_addr, total_size, free_size;
if (log_manager_get_flash_info(&start_addr, &total_size, &free_size) != 0) {
log_manager_write(message); // 使用默认模式
return;
}
float usage_percent = (100.0 * (total_size - free_size)) / total_size;
if (usage_percent > 80.0f) {
// 存储压力大,只记录错误
log_manager_write_level("ERROR", message);
} else if (usage_percent > 60.0f) {
// 中等压力,记录警告及以上
log_manager_write_level("WARN", message);
} else {
// 正常,记录所有信息
log_manager_write_level("INFO", message);
}
}
场景3:系统初始化检查
void system_init_check(void)
{
unsigned int start_addr, total_size, free_size;
if (log_manager_get_flash_info(&start_addr, &total_size, &free_size) != 0) {
printf("ERROR: Cannot access Flash storage\n");
return;
}
printf("\n=== System Storage Information ===\n");
printf("Flash Base Address: 0x%08X\n", start_addr);
printf("Partition Size: %u KB (%.2f MB)\n",
total_size / 1024, total_size / (1024.0 * 1024.0));
printf("Available Space: %u KB\n", free_size / 1024);
if (free_size < (1024 * 1024)) { // 少于 1MB
printf("WARNING: Low storage space!\n");
}
}
输出示例
=== Flash Storage Information ===
Start Address: 0x00200000
Total Size: 262144 bytes (256.00 KB, 0.25 MB)
Free Space: 180000 bytes (175.78 KB, 0.17 MB)
Used Space: 82144 bytes (80.22 KB, 0.08 MB)
Storage Usage: 31.3%
=== Log File Statistics ===
Number of log files: 3
[1] 20250206_140000.log - 30000 bytes (29.30 KB)
[2] 20250206_141000.log - 25000 bytes (24.41 KB)
[3] 20250206_142000.log - 27144 bytes (26.51 KB)
Total log size: 82144 bytes (80.22 KB, 0.08 MB)
Estimated space for ~1 more log files (100KB each)
关键特性
✅ 获取 Flash 分区信息
- 起始地址(物理地址)
- 分区总大小
- 剩余可用空间
✅ 自动计算
- 已用空间 = 总大小 - 剩余空间
- 已用所有日志文件的实时大小
✅ 与日志系统集成
- 自动从日志统计计算剩余空间
- 可用于动态调整日志策略
✅ 性能友好
- 快速查询,不涉及文件遍历
- 使用 littlefs 底层分区信息
注意事项
1. 调用前提
// 必须先初始化日志系统
log_manager_init();
// 然后才能调用
log_manager_get_flash_info(&start, &total, &free);
// 系统关闭时清理
log_manager_cleanup();
2. 返回值检查
// 总是检查返回值
if (log_manager_get_flash_info(&start, &total, &free) != 0) {
printf("Error: Cannot get flash info\n");
return;
}
3. 单位说明
// 所有大小都是以字节为单位
unsigned int free_size; // 字节
float free_kb = free_size / 1024.0; // 转换为 KB
float free_mb = free_size / (1024 * 1024); // 转换为 MB
4. 地址格式
// start_addr 是一个 32 位的物理地址
unsigned int addr = 0x00200000; // 例如:0x200000
// 打印时使用正确的格式
printf("Address: 0x%08X\n", start_addr); // 8 位大写十六进制
API 兼容性
| 组件 | 版本 | 兼容性 |
|---|---|---|
| littlefs | v2.5.0 | ✅ 完全兼容 |
| littlefs_adapt | 项目内置 | ✅ 依赖分区 API |
| log_manager | v1.0+ | ✅ 包含本 API |