TA的每日心情 | 奋斗 2015-1-22 18:04 |
---|
签到天数: 189 天 连续签到: 1 天 [LV.7]常住居民III
|
在做好spi驱动以后,进行SD卡的FATFS文件系统移植只需要去实现FATFS给出的6个接口函数即可跑起文件系统了,接口代码实现如下:- /*--------------------------------------------------------------------------
- 这里为FS文件系统的底层函数
- 移植FS文件系统总共需要写6个底层函数
- ---------------------------------------------------------------------------*/
- DSTATUS disk_initialize (
- BYTE drv /* Physical drive nmuber (0..) */
- )
- {
- uint8_t Status;
-
- switch (drv)
- {
- case 0 :
- Status = SDCard_Init();
- if(Status == 0)
- return 0;
- else
- return STA_NOINIT;
-
- case 1 :
- return STA_NOINIT;
-
- case 2 :
- return STA_NOINIT;
- }
- return STA_NOINIT;
- }
- /*-----------------------------------------------------------------------*/
- /* Return Disk Status */
- DSTATUS disk_status (
- BYTE drv /* Physical drive nmuber (0..) */
- )
- {
- switch (drv)
- {
- case 0 :
- /* translate the reslut code here */
- return 0;
- case 1 :
- /* translate the reslut code here */
- return 0;
- case 2 :
- /* translate the reslut code here */
- return 0;
- default:
- break;
- }
- return STA_NOINIT;
- }
- /*-----------------------------------------------------------------------*/
- /* Read Sector(s) */
- DRESULT disk_read (
- BYTE drv, /* Physical drive nmuber (0..) */
- BYTE *buff, /* Data buffer to store read data */
- DWORD sector, /* Sector address (LBA) */
- BYTE count /* Number of sectors to read (1..255) */
- )
- {
- uint8_t Status;
- if( !count )
- {
- return RES_PARERR; /* count不能等于0,否则返回参数错误 */
- }
- switch (drv)
- {
- case 0:
- if(count==1) /* 1个sector的读操作 */
- {
- Status = SDCard_ReadSingleBlock(sector ,&buff[0] );
- }
- else /* 多个sector的读操作 */
- {
- Status = SDCard_ReadMultiBlock(sector , &buff[0] ,count);
- }
- /* Check if the Transfer is finished */
- if(Status == 0)
- {
- return RES_OK;
- }
- else
- {
- return RES_ERROR;
- }
- case 1:
- break;
- case 2:
- break;
- default:
- break;
- }
- return RES_ERROR;
- }
- /*-----------------------------------------------------------------------*/
- /* Write Sector(s) */
- #if _READONLY == 0
- DRESULT disk_write (
- BYTE drv, /* Physical drive nmuber (0..) */
- const BYTE *buff, /* Data to be written */
- DWORD sector, /* Sector address (LBA) */
- BYTE count /* Number of sectors to write (1..255) */
- )
- {
- uint8_t Status;
- if( !count )
- {
- return RES_PARERR; /* count不能等于0,否则返回参数错误 */
- }
- switch (drv)
- {
- case 0:
- if(count==1) /* 1个sector的写操作 */
- {
- Status = SDCard_WriteSingleBlock( sector ,&buff[0] );
- }
- else /* 多个sector的写操作 */
- {
- Status = SDCard_WriteMultiBlock(sector ,&buff[0] ,count);
- }
-
- if(Status == 0)
- {
- return RES_OK;
- }
- else
- {
- return RES_ERROR;
- }
- case 2:
- break;
- default :
- break;
- }
- return RES_ERROR;
- }
- #endif /* _READONLY */
- /*-----------------------------------------------------------------------*/
- /* Miscellaneous Functions */
- DRESULT disk_ioctl (
- BYTE drv, /* Physical drive nmuber (0..) */
- BYTE ctrl, /* Control code */
- void *buff /* Buffer to send/receive control data */
- )
- {
- if (drv)
- {
- return RES_PARERR; /* 仅支持单磁盘操作,否则返回参数错误 */
- }
- switch (ctrl)
- {
- case CTRL_SYNC :
- return RES_OK;
- case GET_SECTOR_COUNT :
- // *(DWORD*)buff = SDCardInfo.CardCapacity/SDCardInfo.CardBlockSize;
- return RES_OK;
- case GET_BLOCK_SIZE :
- //*(WORD*)buff = SDCardInfo.CardBlockSize;
- return RES_OK;
- case CTRL_POWER :
- break;
- case CTRL_LOCK :
- break;
- case CTRL_EJECT :
- break;
- /* MMC/SDC command */
- case MMC_GET_TYPE :
- break;
- case MMC_GET_CSD :
- break;
- case MMC_GET_CID :
- break;
- case MMC_GET_OCR :
- break;
- case MMC_GET_SDSTAT :
- break;
- }
- return RES_PARERR;
- }
- /* 得到文件Calendar格式的建立日期,是DWORD get_fattime (void) 逆变换 */
- /*-----------------------------------------------------------------------*/
- /* User defined function to give a current time to fatfs module */
- /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
- /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
- DWORD get_fattime (void)
- {
-
- return 0;
- }
复制代码 最后一个是获取时间的函数,因为还没有驱动RTC,所以这个留空,可以不用去写。
下面是基于文件系统读写的一个小例子:
- FATFS fs;
- FRESULT res;
- DIR dirs;
- FIL fsrc, fdst; // file objects
- UINT br, bw; // File R/W count
- char path[20]="0:/webnet/test.txt";
- uint8_t textFileBuffer[] = "这是一个FATFS移植demo\r\n";
- FRESULT scan_files (char* path)
- {
- FRESULT res;
- FILINFO fno;
- DIR dir;
- int i;
- char *fn;
- #if _USE_LFN
- static char lfn[_MAX_LFN * (_DF1S ? 2 : 1) + 1];
- fno.lfname = lfn;
- fno.lfsize = sizeof(lfn);
- #endif
- res = f_opendir(&dir, path);
- if (res == FR_OK) {
- i = strlen(path);
- for (;;) {
- res = f_readdir(&dir, &fno);
- if (res != FR_OK || fno.fname[0] == 0) break;
- if (fno.fname[0] == '.') continue;
- #if _USE_LFN
- fn = *fno.lfname ? fno.lfname : fno.fname;
- #else
- fn = fno.fname;
- #endif
- if (fno.fattrib & AM_DIR) {
- sprintf(&path[i], "/%s", fn);
- res = scan_files(path);
- if (res != FR_OK) break;
- path[i] = 0;
- } else {
- printf("%s/%s\r\n", path, fn);
- }
- }
- }
-
- return res;
- }
- int main(void)
- {
- uint8_t state;
- FATFS fs;
- FRESULT res;
- DIR dirs;
- FILINFO finfo;
-
- systick_hw_init();
- led_hw_init();
- UART0_Init(115200);
- state=SDCard_Init();
- if(state==0)
- {
- printf("SD卡初始化成功!\r\n");
- }
- else
- {
- printf("SD卡初始化失败!\r\n");
- }
- printf("SD容量为:%dM\r\n",SDCard_GetCapacity()/(1024*1024));
- res = disk_initialize(0);
- while(res!= FR_OK)printf("Fatfs failed!\r\n");
- res=f_mount(0, &fs);
- while(res!= FR_OK)printf("Fatfs failed!\r\n");
- if ( res == FR_OK )
- {
- /*创建test.txt文件*/
- res = f_open( &fsrc,(char*)path, FA_CREATE_NEW | FA_WRITE);
- /* 将缓冲区的数据写到文件中 */
- res = f_write(&fsrc, textFileBuffer, sizeof(textFileBuffer), &br);
- printf( "\r\n test.txt 文件创建成功 \r\n" );
- /*关闭文件 */
- f_close(&fsrc);
- }else
- {
- printf("Fatfs failed!");
- }
- res=scan_files("0:/webnet");//遍历webnet文件夹下所有文件
- while(1){
- PIOB->PIO_CODR=(0x01<<LED0_PIN);
- delay_ms(100);
- PIOB->PIO_SODR=(0x01<<LED0_PIN);
- delay_ms(100);
- }
- }
复制代码 这个例子是在sd卡的webnet目录下创建一个test.txt,同时把这个目录下所以文件都遍历,把文件名都打印输出,运行会输出如下:
9.fatfs.rar
(1.31 MB, 下载次数: 21)
|
|