- DRESULT disk_read(
- BYTE pdrv, /* Physical drive nmuber to identify the drive */
- BYTE *buff, /* Data buffer to store read data */
- LBA_t sector, /* Start sector in LBA */
- UINT count /* Number of sectors to read */
- )
- {
- DRESULT res;
- //uart_printf("disk_read---sector:%d,count:%d\r\n",sector,count);
- if(pdrv == SPI_FLASH)
- {
- for(;count>0;count--)
- {
- SPI_Flash_Read(buff,sector*FLASH_SECTOR_SIZE,FLASH_SECTOR_SIZE);
- sector++;
- buff+=FLASH_SECTOR_SIZE;
- }
- // SPI_Flash_Read(buff,sector,count);//spi flash的读接口,注意函数参数类型一致性
- res = 0;
- return res;
- }
- else
- {
- printf("!!!disk_read ERR\r\n");
- return RES_PARERR;
- }
- }
- DRESULT disk_write(
- BYTE pdrv, /* Physical drive nmuber to identify the drive */
- const BYTE *buff, /* Data to be written */
- LBA_t sector, /* Start sector in LBA */
- UINT count /* Number of sectors to write */
- )
- {
- DRESULT res;
- if(pdrv == SPI_FLASH)
- {
- for(;count>0;count--)
- {
- SPI_Flash_Write((u8*)buff,sector*FLASH_SECTOR_SIZE,FLASH_SECTOR_SIZE);
- sector++;
- buff+=FLASH_SECTOR_SIZE;
- }
- res=0;
- return res;
- }
- else
- {
- printf("!!!disk_write ERR\r\n");
- return RES_PARERR;
- }
- }
- DRESULT disk_ioctl(
- BYTE pdrv, /* Physical drive nmuber (0..) */
- BYTE cmd, /* Control code */
- void *buff /* Buffer to send/receive control data */
- )
- {
- if (pdrv == SPI_FLASH)
- {
- switch (cmd)
- {
- case CTRL_SYNC:
- return RES_OK;
- /* 扇区数量 1024*1024*1024 =4 (MB) */
- case GET_SECTOR_COUNT:
- *(DWORD * )buff = 512;//W25Q32 有1024个大小为4k bytes 的扇区
- return RES_OK;
- /* 扇区大小 */
- case GET_SECTOR_SIZE :
- *(WORD * )buff = 2048;//spi flash的扇区大小是 4K Bytes
- return RES_OK;
- /*块大小 */
- case GET_BLOCK_SIZE :
- *(DWORD * )buff = 1;
- return RES_OK;
- default:
- return RES_PARERR;
- }
- }
- else
- {
- printf("!!!disk_ioctl ERR\r\n");
- return RES_PARERR;
- }
- }
- DWORD get_fattime (void)
- {
- return 0;
- }
实现底层就可以开始了
- void fs_test(void)
- {
- /* Filesystem object */
- FIL fil; /* File object */
- FRESULT res; /* API result code */
- UINT bw; /* Bytes written */
- BYTE work[FF_MAX_SS]; /* Work area (larger is better for processing time) */
- BYTE mm[50];//读缓存
- UINT i;
- printf("file system start:\r\n");
- fs = malloc(sizeof (FATFS));
- res = f_mount(fs, "0:",1);
- printf("res=%d\n\r",res);
- /* 格式化文件系统 */
- if(res!=0)
- {
- res = f_mkfs("0:",0,work,sizeof work );
- /* 卸载文件系统 */
- f_mount(NULL,"0:",1);
- /* 挂载文件系统 */
- res = f_mount(fs, "0:",1);
- }
- if(res==0) printf("mkfs is success!%d\n\r",res);
- else printf("mkfs is fail!%d\n\r",res);
-
- if(res==0) printf("f_mount is success!%d\n\r",res);
- else printf("f_mount is fail!%d\n\r",res);
- /* Create a file as new */
- res = f_open(&fil, "0:/1.txt", FA_CREATE_NEW | FA_WRITE | FA_READ);
- if (res)
- {
- printf("open file fail.id=%d\r\n",res);
- }
- else
- {
- printf("open file success.\r\n");
- }
- /* Write a message */
- res = f_write(&fil, "Hello,World!", 12, &bw);
- //printf("res write:%d\r\n",res);
- if (bw == 12)
- {
- printf("writing ok!\r\n");
- }
- else
- {
- printf("writing fail!\r\n");
- }
- res = f_size(&fil);
- printf("file size:%d Bytes.\r\n",res);
- memset(mm,0x0,50);
- f_lseek(&fil,0);
- res = f_read(&fil,mm,12,&i);
- if (res == FR_OK)
- {
- printf("read file ok!\r\n");
- printf("read file lengh:%d Bytes.\r\n",i);
- }
- else
- {
- printf("read fail\r\n");
- }
- printf("read file:\r\n");
- printf("%s\n\r",mm);
- /* Close the file */
- f_close(&fil);
- /*卸载文件系统*/
- f_mount(0, "0:", 0);
- free(fs);
- printf("file system down.\r\n");
- }
- int main(void)
- {
- u8 datap[SIZE];
- u16 Flash_Model;
- FRESULT res;
- FATFS * fs;
- SystemCoreClockUpdate();
- Delay_Init();
- USART_Printf_Init(115200);
- printf("SystemClk:%d\r\n", SystemCoreClock);
- printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
- // SPI_Flash_Init();
- disk_initialize(0);
- Flash_Model = SPI_Flash_ReadID();
- switch(Flash_Model)
- {
- case W25Q80:
- printf("W25Q80 OK!\r\n");
- break;
- case W25Q16:
- printf("W25Q16 OK!\r\n");
- break;
- case W25Q32:
- printf("W25Q32 OK!\r\n");
- break;
- case W25Q64:
- printf("W25Q64 OK!\r\n");
- break;
- case W25Q128:
- printf("W25Q128 OK!\r\n");
- break;
- default:
- printf("Fail!\r\n");
- break;
- }
↑↑↑↑↑↑↑main函数老是复制不上来↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓接上面↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
- Delay_Ms(1000);
- fs_test();
- while(1)
- {
- Delay_Ms(1000);
- fs = malloc(sizeof (FATFS));
- res = f_mount(fs, "0:",1);
- printf("mount=%d\n\r",res);
- get_fatfs_free();
- Delay_Ms(500);
- res=f_mount(0, "0:", 0);
- free(fs);
- printf("un mount=%d\n\r",res);
- // printf("Start Read W25Qxx....\r\n");
- // SPI_Flash_Read(datap, 0x0, SIZE);
- // printf("%s\r\n", datap);
- }
- }
4、实验效果
每次上电都要格式化
,百度必应了半天也没有解决。
后来发现是例程的扇区擦除函数没有删掉。
排查了老半天!!!!
//printf("Start Erase W25Qxx....\r\n");
//SPI_Flash_Erase_Sector(0);
// printf("W25Qxx Erase Finished!\r\n");
附件:
SPI_FLASH.zip
(3.27 MB, 下载次数: 34)
https://bbs.21ic.com/forum.php?mod=attachment&aid=MjE2NjMyNHw1MWJmYzNkNXwxNzU2MDYxODkxfDB8