在线时间796 小时
UID3067179
注册时间2014-10-23
NXP金币1077
TA的每日心情 | 开心 昨天 08:01 |
---|
签到天数: 1319 天 [LV.10]以坛为家III
版主
- 积分
- 7578
- 最后登录
- 2024-5-5
|
将fatfs文件系统搬到自己创建的项目文件里。
一、硬件电路:
二、软件部分
main.c
- int main(void)
- {
- FRESULT error;
- DIR directory; /* Directory object */
- FILINFO fileInformation;
- UINT bytesWritten;
- UINT bytesRead;
- const TCHAR driverNumberBuffer[3U] = {SDDISK + '0', ':', '/'};
- volatile bool failedFlag = false;
- char ch = '0';
- BYTE work[_MAX_SS];
- BOARD_ConfigMPU();
- BOARD_InitPins();
- MMC_Init(); //MMC初始化
-
- BOARD_BootClockRUN();
- BOARD_USDHCClockConfiguration();
- BOARD_InitDebugConsole();
-
- /* Set PERCLK_CLK source to OSC_CLK*/
- CLOCK_SetMux(kCLOCK_PerclkMux, 1U);
- /* Set PERCLK_CLK divider to 1 */
- CLOCK_SetDiv(kCLOCK_PerclkDiv, 0U);
-
- PITIMER_Init(1000000); //PIT定时器1S
- LED_Init(); //LED初始化
- ST7565_Init(); //初始化LCM
- CAN_Init();
-
- PRINTF("\r\nFATFS example to demonstrate how to use FATFS with SD card.\r\n");
- PRINTF("\r\nPlease insert a card into board.\r\n");
- if (sdcardWaitCardInsert() != kStatus_Success) //检查MMC卡插入
- {
- return -1;
- }
- if (f_mount(&g_fileSystem, driverNumberBuffer, 0U)) //挂载
- {
- PRINTF("Mount volume failed.\r\n");
- return -1;
- }
- #if (_FS_RPATH >= 2U)
- error = f_chdrive((char const *)&driverNumberBuffer[0U]); //改变当前驱动器
- if (error)
- {
- PRINTF("Change drive failed.\r\n");
- return -1;
- }
- #endif
- #if _USE_MKFS
- PRINTF("\r\nMake file system......The time may be long if the card capacity is big.\r\n");
- if (f_mkfs(driverNumberBuffer, FM_ANY, 0U, work, sizeof work))
- {
- PRINTF("Make file system failed.\r\n");
- return -1;
- }
- #endif /* _USE_MKFS */
- PRINTF("\r\nCreate directory......\r\n");
- error = f_mkdir(_T("/nxpic")); //创建目录
- if (error)
- {
- if (error == FR_EXIST)
- {
- PRINTF("Directory exists.\r\n");
- }
- else
- {
- PRINTF("Make directory failed.\r\n");
- return -1;
- }
- }
- PRINTF("\r\nCreate a file in that directory......\r\n");
- //目录下创建文件
- error = f_open(&g_fileObject, _T("/nxpic/nxpic.txt"), (FA_WRITE | FA_READ | FA_CREATE_ALWAYS));
- if (error)
- {
- if (error == FR_EXIST)
- {
- PRINTF("File exists.\r\n");
- }
- else
- {
- PRINTF("Open file failed.\r\n");
- return -1;
- }
- }
- //创建子目录
- PRINTF("\r\nCreate a directory in that directory......\r\n");
- error = f_mkdir(_T("/nxpic/nxpic2"));
- if (error)
- {
- if (error == FR_EXIST)
- {
- PRINTF("Directory exists.\r\n");
- }
- else
- {
- PRINTF("Directory creation failed.\r\n");
- return -1;
- }
- }
- PRINTF("\r\nList the file in that directory......\r\n");
-
- if (f_opendir(&directory, "/nxpic"))
- {
- PRINTF("Open directory failed.\r\n");
- return -1;
- }
- for (;;)
- {
- error = f_readdir(&directory, &fileInformation);
- /* To the end. */
- if ((error != FR_OK) || (fileInformation.fname[0U] == 0U))
- {
- break;
- }
- if (fileInformation.fname[0] == '.')
- {
- continue;
- }
- if (fileInformation.fattrib & AM_DIR)
- {
- PRINTF("Directory file : %s.\r\n", fileInformation.fname);
- }
- else
- {
- PRINTF("General file : %s.\r\n", fileInformation.fname);
- }
- }
- memset(g_bufferWrite, 'a', sizeof(g_bufferWrite));
- g_bufferWrite[BUFFER_SIZE - 2U] = '\r';
- g_bufferWrite[BUFFER_SIZE - 1U] = '\n';
- PRINTF("\r\nWrite/read file until encounters error......\r\n");
- while (true)
- {
- if (failedFlag || (ch == 'q'))
- {
- break;
- }
- PRINTF("\r\nWrite to above created file.\r\n");
- error = f_write(&g_fileObject, g_bufferWrite, sizeof(g_bufferWrite), &bytesWritten);
-
- if ((error) || (bytesWritten != sizeof(g_bufferWrite)))
- {
- PRINTF("Write file failed. \r\n");
- failedFlag = true;
- continue;
- }
- /* Move the file pointer */
- if (f_lseek(&g_fileObject, 0U))
- {
- PRINTF("Set file pointer position failed. \r\n");
- failedFlag = true;
- continue;
- }
- PRINTF("Read from above created file.\r\n");
- memset(g_bufferRead, 0U, sizeof(g_bufferRead));
- error = f_read(&g_fileObject, g_bufferRead, sizeof(g_bufferRead), &bytesRead);
- if ((error) || (bytesRead != sizeof(g_bufferRead)))
- {
- PRINTF("Read file failed. \r\n");
- failedFlag = true;
- continue;
- }
- PRINTF("Compare the read/write content......\r\n");
- if (memcmp(g_bufferWrite, g_bufferRead, sizeof(g_bufferWrite)))
- {
- PRINTF("Compare read/write content isn't consistent.\r\n");
- failedFlag = true;
- continue;
- }
- PRINTF("The read/write content is consistent.\r\n");
- PRINTF("\r\nInput 'q' to quit read/write.\r\nInput other char to read/write file again.\r\n");
- ch = GETCHAR();
- PUTCHAR(ch);
- }
- PRINTF("\r\nThe example will not read/write file again.\r\n");
- if (f_close(&g_fileObject))
- {
- PRINTF("\r\nClose file failed.\r\n");
- return -1;
- }
- while (true)
- {
- }
- //-----------------------------------------------
- // while (1)
- // {
- // CAN_Handle();
- // }
- }
复制代码
三、执行结果:
3.1、串口输出:
3.2、SD卡创建的文件
四、程序源码
RT1052-181113.rar
(2.81 MB, 下载次数: 59)
|
|