硬汉嵌入式论坛

 找回密码
 立即注册
查看: 169|回复: 3
收起左侧

[FatFs] 新建文件报错16,FR_LOCKED

[复制链接]

2

主题

13

回帖

19

积分

新手上路

积分
19
发表于 2025-7-11 09:48:44 | 显示全部楼层 |阅读模式
串口打印内容:

时间变化,关闭当前文件

[08:55:25.207]收←◆新建HEX数据文件:0:/Board4/20250701110000.hex
无法创建数据文件: 0:/Board4/20250701110000.hex,错误: 16
无法创建数据文件!


Code:


FIL* FATFS_CreateRawDataFileWithTimestamp(void)
{
    static FIL file;
    FRESULT res;
    RTC_TimeTypeDef sTime;
    RTC_DateTypeDef sDate;
   
    //确保缓冲区足够大
    static char filename[64];


    // 获取当前时间
    HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
    HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
   
    // // 检查是否需要创建新文件(年份、月份、日期或小时任一变化)
    // bool needNewFile = (sDate.Year != currentFileYear) ||
    //                   (sDate.Month != currentFileMonth) ||
    //                   (sDate.Date != currentFileDay) ||
    //                   (sTime.Hours != currentFileHour);
   
    // 更新当前文件时间
    currentFileYear = sDate.Year;
    currentFileMonth = sDate.Month;
    currentFileDay = sDate.Date;
    currentFileHour = sTime.Hours;


    // 构造目录路径
    char folder[32];
    snprintf(folder, sizeof(folder), "0:/Board%u", g_config.board_id);
   
    // 确保目录存在(每次调用都检查,不再使用静态变量)
    res = f_mkdir(folder);
    if (res != FR_OK && res != FR_EXIST)
    {
        printf("无法创建文件夹 %s,错误码: %d\r\n", folder, res);
        return NULL;
    }
   
    // 生成带完整时间戳的文件名 (格式: YYYYMMDDHHMMSS.hex)
    // 修改文件名格式
    snprintf(filename, sizeof(filename),
            "%s/%04d%02d%02d%02d%02d%02d.hex",
            folder,
            2000 + sDate.Year,  // RTC年份是从2000开始的偏移
            sDate.Month,
            sDate.Date,
            sTime.Hours,
            sTime.Minutes,
            sTime.Seconds);
   
    // // 尝试打开文件(如果存在则追加)
    // res = f_open(&file, filename, FA_OPEN_ALWAYS | FA_WRITE);
    // if(res != FR_OK)
    // {
    //     // 直接输出错误代码(方法1)
    //     printf("无法打开数据文件: %s, 错误: %d\r\n", filename, res);
   
    //     // 尝试创建备用文件
    //     static int backupCount = 0;
    //     snprintf(filename, sizeof(filename), "%s/backup%d.hex", folder, backupCount++);
    //     res = f_open(&file, filename, FA_CREATE_ALWAYS | FA_WRITE);
    //     if(res != FR_OK)
    //     {
    //         printf("创建备用文件失败: %d\r\n", res);
    //         return NULL;
    //     }
    //     else
    //     {
    //         printf("创建备用文件: %s\r\n", filename);
    //     }
    // }


    // 创建文件
    res = f_open(&file, filename, FA_CREATE_ALWAYS | FA_WRITE);
    if(res != FR_OK)
    {
        printf("无法创建数据文件: %s, 错误: %d\r\n", filename, res);
        return NULL;
    }
   
    // 移动到文件末尾准备追加数据
    res = f_lseek(&file, f_size(&file));
    if (res != FR_OK)
    {
        printf("文件定位失败: %d\r\n", res);
        f_close(&file);
        return NULL;
    }
   
    // 打印文件状态
    if (f_size(&file) == 0)
    {
        printf("新建HEX数据文件: %s\r\n", filename);
    }
    else
    {
        printf("续写HEX数据文件: %s\r\n", filename);
    }
   
    return &file;
}




void CheckAndCreateNewFileIfNeeded(void)
{
    RTC_TimeTypeDef sTime;
    RTC_DateTypeDef sDate;
   
    // 获取当前时间
    HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
    HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
   
    // 检查是否需要创建新文件
    if ((currentFileHour != sTime.Hours) || (currentFileDay != sDate.Date))
    {
        // 关闭当前文件
        // f_close(&rawDataFile);
                if (fileCreated)
        {
            f_sync(&rawDataFile);
            f_close(&rawDataFile);
            fileCreated = false;
                }
        printf("时间变化,关闭当前文件\r\n");
        
        // 创建新文件
        FIL* filePtr = FATFS_CreateRawDataFileWithTimestamp();
        if(filePtr == NULL)
        {
            printf("无法创建新数据文件!\r\n");
            Error_Handler();
        }
        rawDataFile = *filePtr;
        currentFileHour = sTime.Hours;// 更新当前文件时间
        currentFileDay = sDate.Date;// 更新当前文件时间
    }
}

回复

使用道具 举报

2

主题

13

回帖

19

积分

新手上路

积分
19
 楼主| 发表于 2025-7-11 09:49:45 | 显示全部楼层

/* File function return code (FRESULT) */

typedef enum {
        FR_OK = 0,                                /* (0) Succeeded */
        FR_DISK_ERR,                        /* (1) A hard error occurred in the low level disk I/O layer */
        FR_INT_ERR,                                /* (2) Assertion failed */
        FR_NOT_READY,                        /* (3) The physical drive cannot work */
        FR_NO_FILE,                                /* (4) Could not find the file */
        FR_NO_PATH,                                /* (5) Could not find the path */
        FR_INVALID_NAME,                /* (6) The path name format is invalid */
        FR_DENIED,                                /* (7) Access denied due to prohibited access or directory full */
        FR_EXIST,                                /* (8) Access denied due to prohibited access */
        FR_INVALID_OBJECT,                /* (9) The file/directory object is invalid */
        FR_WRITE_PROTECTED,                /* (10) The physical drive is write protected */
        FR_INVALID_DRIVE,                /* (11) The logical drive number is invalid */
        FR_NOT_ENABLED,                        /* (12) The volume has no work area */
        FR_NO_FILESYSTEM,                /* (13) There is no valid FAT volume */
        FR_MKFS_ABORTED,                /* (14) The f_mkfs() aborted due to any problem */
        FR_TIMEOUT,                                /* (15) Could not get a grant to access the volume within defined period */
        FR_LOCKED,                                /* (16) The operation is rejected according to the file sharing policy */
        FR_NOT_ENOUGH_CORE,                /* (17) LFN working buffer could not be allocated */
        FR_TOO_MANY_OPEN_FILES,        /* (18) Number of open files > _FS_LOCK */
        FR_INVALID_PARAMETER        /* (19) Given parameter is invalid */
} FRESULT;
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
117512
QQ
发表于 2025-7-11 09:52:57 | 显示全部楼层
你之前 f_open的文件,操作完毕是不是没有关闭。

这个同时打开的文件个数是在ffconf.h文件里面配置的。
回复

使用道具 举报

2

主题

13

回帖

19

积分

新手上路

积分
19
 楼主| 发表于 2025-7-11 09:56:28 | 显示全部楼层
eric2013 发表于 2025-7-11 09:52
你之前 f_open的文件,操作完毕是不是没有关闭。

这个同时打开的文件个数是在ffconf.h文件里面配置的。

每次新建前都会关闭的
//#define _FS_LOCK    2     /* 0isable or >=1:Enable */
#define _FS_LOCK    4
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-8-11 20:57 , Processed in 0.047894 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表