查看: 4618|回复: 8

RT1052+12、fatfs文件系统测试

[复制链接]
  • TA的每日心情
    开心
    2024-4-10 10:22
  • 签到天数: 1317 天

    [LV.10]以坛为家III

    124

    主题

    2831

    帖子

    31

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    7570
    最后登录
    2024-4-10
    发表于 2018-11-13 16:22:08 | 显示全部楼层 |阅读模式
           将fatfs文件系统搬到自己创建的项目文件里。
          
        一、硬件电路:

             201.png
             202.png
             203.png
             204.png
             205.png

        二、软件部分
            
            main.c
    1. int main(void)
    2. {
    3.     FRESULT error;
    4.     DIR directory; /* Directory object */
    5.     FILINFO fileInformation;
    6.                 UINT bytesWritten;
    7.     UINT bytesRead;
    8.     const TCHAR driverNumberBuffer[3U] = {SDDISK + '0', ':', '/'};
    9.     volatile bool failedFlag = false;
    10.                 char ch = '0';
    11.     BYTE work[_MAX_SS];

    12.     BOARD_ConfigMPU();
    13.     BOARD_InitPins();
    14.                 MMC_Init();                                                                                                                //MMC初始化
    15.                
    16.     BOARD_BootClockRUN();
    17.                 BOARD_USDHCClockConfiguration();
    18.     BOARD_InitDebugConsole();
    19.         
    20.                 /* Set PERCLK_CLK source to OSC_CLK*/
    21.     CLOCK_SetMux(kCLOCK_PerclkMux, 1U);
    22.     /* Set PERCLK_CLK divider to 1 */
    23.     CLOCK_SetDiv(kCLOCK_PerclkDiv, 0U);
    24.         
    25.                 PITIMER_Init(1000000);                                                                //PIT定时器1S
    26.                 LED_Init();                                                                                                                //LED初始化
    27.                 ST7565_Init();                                                                                                //初始化LCM
    28.                 CAN_Init();
    29.                
    30.                 PRINTF("\r\nFATFS example to demonstrate how to use FATFS with SD card.\r\n");
    31.     PRINTF("\r\nPlease insert a card into board.\r\n");

    32.     if (sdcardWaitCardInsert() != kStatus_Success)                                //检查MMC卡插入
    33.     {
    34.         return -1;
    35.     }

    36.     if (f_mount(&g_fileSystem, driverNumberBuffer, 0U))                //挂载
    37.     {
    38.         PRINTF("Mount volume failed.\r\n");
    39.         return -1;
    40.     }
    41. #if (_FS_RPATH >= 2U)
    42.     error = f_chdrive((char const *)&driverNumberBuffer[0U]);        //改变当前驱动器
    43.     if (error)
    44.     {
    45.         PRINTF("Change drive failed.\r\n");
    46.         return -1;
    47.     }
    48. #endif

    49. #if _USE_MKFS
    50.     PRINTF("\r\nMake file system......The time may be long if the card capacity is big.\r\n");
    51.     if (f_mkfs(driverNumberBuffer, FM_ANY, 0U, work, sizeof work))
    52.     {
    53.         PRINTF("Make file system failed.\r\n");
    54.         return -1;
    55.     }
    56. #endif /* _USE_MKFS */

    57.     PRINTF("\r\nCreate directory......\r\n");
    58.     error = f_mkdir(_T("/nxpic"));                                                //创建目录
    59.     if (error)
    60.     {
    61.         if (error == FR_EXIST)
    62.         {
    63.             PRINTF("Directory exists.\r\n");
    64.         }
    65.         else
    66.         {
    67.             PRINTF("Make directory failed.\r\n");
    68.             return -1;
    69.         }
    70.     }
    71.     PRINTF("\r\nCreate a file in that directory......\r\n");
    72.                 //目录下创建文件
    73.     error = f_open(&g_fileObject, _T("/nxpic/nxpic.txt"), (FA_WRITE | FA_READ | FA_CREATE_ALWAYS));
    74.     if (error)
    75.     {
    76.         if (error == FR_EXIST)
    77.         {
    78.             PRINTF("File exists.\r\n");
    79.         }
    80.         else
    81.         {
    82.             PRINTF("Open file failed.\r\n");
    83.             return -1;
    84.         }
    85.     }
    86.                 //创建子目录
    87.     PRINTF("\r\nCreate a directory in that directory......\r\n");
    88.     error = f_mkdir(_T("/nxpic/nxpic2"));
    89.     if (error)
    90.     {
    91.         if (error == FR_EXIST)
    92.         {
    93.             PRINTF("Directory exists.\r\n");
    94.         }
    95.         else
    96.         {
    97.             PRINTF("Directory creation failed.\r\n");
    98.             return -1;
    99.         }
    100.     }        
    101.     PRINTF("\r\nList the file in that directory......\r\n");
    102.                
    103.     if (f_opendir(&directory, "/nxpic"))
    104.     {
    105.         PRINTF("Open directory failed.\r\n");
    106.         return -1;
    107.     }
    108.     for (;;)
    109.     {
    110.         error = f_readdir(&directory, &fileInformation);

    111.         /* To the end. */
    112.         if ((error != FR_OK) || (fileInformation.fname[0U] == 0U))
    113.         {
    114.             break;
    115.         }
    116.         if (fileInformation.fname[0] == '.')
    117.         {
    118.             continue;
    119.         }
    120.         if (fileInformation.fattrib & AM_DIR)
    121.         {
    122.             PRINTF("Directory file : %s.\r\n", fileInformation.fname);
    123.         }
    124.         else
    125.         {
    126.             PRINTF("General file : %s.\r\n", fileInformation.fname);
    127.         }
    128.     }        
    129.     memset(g_bufferWrite, 'a', sizeof(g_bufferWrite));
    130.     g_bufferWrite[BUFFER_SIZE - 2U] = '\r';
    131.     g_bufferWrite[BUFFER_SIZE - 1U] = '\n';

    132.     PRINTF("\r\nWrite/read file until encounters error......\r\n");               
    133.                 while (true)
    134.     {
    135.         if (failedFlag || (ch == 'q'))
    136.         {
    137.             break;
    138.         }

    139.         PRINTF("\r\nWrite to above created file.\r\n");
    140.         error = f_write(&g_fileObject, g_bufferWrite, sizeof(g_bufferWrite), &bytesWritten);
    141.                                 
    142.         if ((error) || (bytesWritten != sizeof(g_bufferWrite)))
    143.         {
    144.             PRINTF("Write file failed. \r\n");
    145.             failedFlag = true;
    146.             continue;
    147.         }
    148.                     /* Move the file pointer */
    149.         if (f_lseek(&g_fileObject, 0U))
    150.         {
    151.             PRINTF("Set file pointer position failed. \r\n");
    152.             failedFlag = true;
    153.             continue;
    154.         }

    155.         PRINTF("Read from above created file.\r\n");
    156.         memset(g_bufferRead, 0U, sizeof(g_bufferRead));
    157.         error = f_read(&g_fileObject, g_bufferRead, sizeof(g_bufferRead), &bytesRead);
    158.         if ((error) || (bytesRead != sizeof(g_bufferRead)))
    159.         {
    160.             PRINTF("Read file failed. \r\n");
    161.             failedFlag = true;
    162.             continue;
    163.         }

    164.         PRINTF("Compare the read/write content......\r\n");
    165.         if (memcmp(g_bufferWrite, g_bufferRead, sizeof(g_bufferWrite)))
    166.         {
    167.             PRINTF("Compare read/write content isn't consistent.\r\n");
    168.             failedFlag = true;
    169.             continue;
    170.         }

    171.         PRINTF("The read/write content is consistent.\r\n");

    172.         PRINTF("\r\nInput 'q' to quit read/write.\r\nInput other char to read/write file again.\r\n");
    173.         ch = GETCHAR();
    174.         PUTCHAR(ch);
    175.     }
    176.                 PRINTF("\r\nThe example will not read/write file again.\r\n");

    177.     if (f_close(&g_fileObject))
    178.     {
    179.         PRINTF("\r\nClose file failed.\r\n");
    180.         return -1;
    181.     }

    182.     while (true)
    183.     {
    184.     }
    185. //-----------------------------------------------
    186. //    while (1)
    187. //    {
    188. //                        CAN_Handle();
    189. //    }
    190. }
    复制代码

         三、执行结果:

            3.1、串口输出:
             1.png         
            3.2、SD卡创建的文件
             301.png
             302.png
             303.png


        四、程序源码


        RT1052-181113.rar (2.81 MB, 下载次数: 59)
    哎...今天够累的,签到来了~
    回复

    使用道具 举报

    该用户从未签到

    16

    主题

    107

    帖子

    2

    中级会员

    Rank: 3Rank: 3

    积分
    365
    最后登录
    2022-12-14
    发表于 2018-11-15 09:46:19 | 显示全部楼层
    为什么我的是make filesystem failed?你的SD卡是空白的还是已经有一个FAT文件系统?
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2024-4-10 10:22
  • 签到天数: 1317 天

    [LV.10]以坛为家III

    124

    主题

    2831

    帖子

    31

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    7570
    最后登录
    2024-4-10
     楼主| 发表于 2018-11-15 11:39:48 | 显示全部楼层
    mars4zhu 发表于 2018-11-15 09:46
    为什么我的是make filesystem failed?你的SD卡是空白的还是已经有一个FAT文件系统? ...

    格式化的SD卡
    哎...今天够累的,签到来了~
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    16

    主题

    107

    帖子

    2

    中级会员

    Rank: 3Rank: 3

    积分
    365
    最后登录
    2022-12-14
    发表于 2018-11-15 19:28:17 | 显示全部楼层

    意思就是一张没有文件系统的SD卡?
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2024-4-10 10:22
  • 签到天数: 1317 天

    [LV.10]以坛为家III

    124

    主题

    2831

    帖子

    31

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    7570
    最后登录
    2024-4-10
     楼主| 发表于 2018-11-16 07:23:25 | 显示全部楼层
    mars4zhu 发表于 2018-11-15 19:28
    意思就是一张没有文件系统的SD卡?

    嗯,我是这样操作的
    哎...今天够累的,签到来了~
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2024-4-10 22:38
  • 签到天数: 1335 天

    [LV.10]以坛为家III

    88

    主题

    4292

    帖子

    12

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    9049
    最后登录
    2024-4-13
    发表于 2018-11-16 09:33:48 | 显示全部楼层
    FatFS看来还是挺靠谱的。
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2023-2-15 00:12
  • 签到天数: 969 天

    [LV.10]以坛为家III

    175

    主题

    2843

    帖子

    34

    金牌会员

    Rank: 6Rank: 6

    积分
    7092
    最后登录
    2023-4-20
    发表于 2018-11-24 23:26:56 | 显示全部楼层
    mars4zhu 发表于 2018-11-15 09:46
    为什么我的是make filesystem failed?你的SD卡是空白的还是已经有一个FAT文件系统? ...

    我的现象和你的一样,后来解决了吗?对所用的SD卡是否有要求?
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    16

    主题

    107

    帖子

    2

    中级会员

    Rank: 3Rank: 3

    积分
    365
    最后登录
    2022-12-14
    发表于 2018-11-27 10:49:24 | 显示全部楼层
    jinglixixi 发表于 2018-11-24 23:26
    我的现象和你的一样,后来解决了吗?对所用的SD卡是否有要求?

    后来没有去测试了。我的SD卡是给另一块开发板Zynq的,里面不光有一个FAT分区,还有一个EXT4分区,linux系统。我就不想去格式化SD卡了。。。。。

    用usb_examples里面的usb_device_msc_ramdisk,利用RT1052的内部RAM当做存储,可以模拟一个几十K的磁盘。可以格式化,存文件(格式化之后只有几KB的存储空间了。可以存几个txt文件。。。。。。)
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2023-2-15 00:12
  • 签到天数: 969 天

    [LV.10]以坛为家III

    175

    主题

    2843

    帖子

    34

    金牌会员

    Rank: 6Rank: 6

    积分
    7092
    最后登录
    2023-4-20
    发表于 2018-11-27 12:28:52 | 显示全部楼层
    mars4zhu 发表于 2018-11-27 10:49
    后来没有去测试了。我的SD卡是给另一块开发板Zynq的,里面不光有一个FAT分区,还有一个EXT4分区,linux系 ...

    谢谢,知道了!
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2024-4-24 20:44 , Processed in 0.144533 second(s), 30 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

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