对移植FatFs文件系统的浅解
引用《零死角玩转STM32-F429挑战者》第25章串行FLASH文件系统FatFs
- FatFs官网下载源码 http://elm-chan.org/fsw/ff/00index_e.html
- 文中的源码用的是R0.11a 目前源码已经更新到R0.14 源码框架有所改变 所有的语言支持文件都集成到了ffunicode.c中
- 将下载的源码添加进工程,注意添加.h的路径
- 主要修改diskio.c文件 这是底层函数接口文件 ffconf.h是配置文件 可按照教程进行配置
#define _USE_MKFS 1 #define _CODE_PAGE 936 #define _USE_LFN 2 #define _VOLUMES 2 #define _MIN_SS 512 #define _MAX_SS 4096
1 /*-----------------------------------------------------------------------*/ 2 /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */ 3 /*-----------------------------------------------------------------------*/ 4 /* If a working storage control module is available, it should be */ 5 /* attached to the FatFs via a glue function rather than modifying it. */ 6 /* This is an example of glue functions to attach various exsisting */ 7 /* storage control modules to the FatFs module with a defined API. */ 8 /*-----------------------------------------------------------------------*/ 9 10 #include "ff.h" /* Obtains integer types */ 11 #include "diskio.h" /* Declarations of disk functions */ 12 #include "flash_w25n01.h" 13 14 15 /* Definitions of physical drive number for each drive */ 16 #define SPI_FLASH 0 17 18 19 20 /*-----------------------------------------------------------------------*/ 21 /* Get Drive Status */ 22 /*-----------------------------------------------------------------------*/ 23 24 DSTATUS disk_status ( 25 BYTE pdrv /* Physical drive nmuber to identify the drive */ 26 ) 27 { 28 if(pdrv == SPI_FLASH) 29 { 30 return RES_OK; 31 } 32 else 33 { 34 return RES_PARERR; 35 } 36 } 37 38 39 40 /*-----------------------------------------------------------------------*/ 41 /* Inidialize a Drive */ 42 /*-----------------------------------------------------------------------*/ 43 44 DSTATUS disk_initialize ( 45 BYTE pdrv /* Physical drive nmuber to identify the drive */ 46 ) 47 { 48 if(pdrv == SPI_FLASH) 49 { 50 //flash_w25n01_init(); 51 if(flash_w25n01_readID() == 0xEFAA21) 52 return RES_OK; 53 else 54 return RES_PARERR; 55 } 56 else 57 { 58 return RES_PARERR; 59 } 60 } 61 62 63 64 /*-----------------------------------------------------------------------*/ 65 /* Read Sector(s) */ 66 /*-----------------------------------------------------------------------*/ 67 68 DRESULT disk_read ( 69 BYTE pdrv, /* Physical drive nmuber to identify the drive */ 70 BYTE *buff, /* Data buffer to store read data */ 71 DWORD sector, /* Start sector in LBA */ 72 UINT count /* Number of sectors to read */ 73 ) 74 { 75 unsigned short addr_block = 0, addr_page = 0; 76 if(pdrv == SPI_FLASH) 77 { 78 addr_block = sector / 64;//每块有64页 79 addr_page = sector % 64; 80 flash_w25n01_buffer_read(buff, addr_block,addr_page,0, count * 2048); 81 return RES_OK; 82 } 83 else 84 { 85 return RES_PARERR; 86 } 87 } 88 89 90 91 /*-----------------------------------------------------------------------*/ 92 /* Write Sector(s) */ 93 /*-----------------------------------------------------------------------*/ 94 95 #if FF_FS_READONLY == 0 96 97 DRESULT disk_write ( 98 BYTE pdrv, /* Physical drive nmuber to identify the drive */ 99 const BYTE *buff, /* Data to be written */ 100 DWORD sector, /* Start sector in LBA */ 101 UINT count /* Number of sectors to write */ 102 ) 103 { 104 unsigned short addr_block = 0, addr_byte = 0; 105 unsigned char addr_page = 0; 106 if(pdrv == SPI_FLASH) 107 { 108 addr_block = sector / 64;//每块有64页 109 addr_page = sector % 64; 110 flash_w25n01_sector_write((unsigned char *)buff, addr_block,addr_page, count * 2048); 111 return RES_OK; 112 } 113 else 114 { 115 return RES_PARERR; 116 } 117 } 118 119 #endif 120 121 122 /*-----------------------------------------------------------------------*/ 123 /* Miscellaneous Functions */ 124 /*-----------------------------------------------------------------------*/ 125 126 DRESULT disk_ioctl ( 127 BYTE pdrv, /* Physical drive nmuber (0..) */ 128 BYTE cmd, /* Control code */ 129 void *buff /* Buffer to send/receive control data */ 130 ) 131 { 132 if (pdrv == SPI_FLASH) 133 { 134 switch (cmd) 135 { 136 case CTRL_SYNC: 137 return RES_OK; 138 139 /* 扇区数量 1024*1024*1024 =4 (MB) */ 140 141 case GET_SECTOR_COUNT: 142 143 *(DWORD * )buff = 65536;// 144 return RES_OK; 145 146 /* 扇区大小 */ 147 148 case GET_SECTOR_SIZE : 149 150 *(WORD * )buff = 2048;//spi flash的扇区大小是 2048Byte 151 return RES_OK; 152 153 /*块大小 */ 154 155 case GET_BLOCK_SIZE : 156 *(DWORD * )buff = 64;//64页 157 return RES_OK; 158 159 default: 160 return RES_PARERR; 161 } 162 } 163 else 164 { 165 return RES_PARERR; 166 } 167 } 168 169 DWORD get_fattime(void) 170 { 171 return 0; 172 }

浙公网安备 33010602011771号