/*-----------------------------------------------------------------------*/ /* Low level disk I/O module SKELETON for FatFs (C)ChaN, 2019 */ /*-----------------------------------------------------------------------*/ /* If a working storage control module is available, it should be */ /* attached to the FatFs via a glue function rather than modifying it. */ /* This is an example of glue functions to attach various exsisting */ /* storage control modules to the FatFs module with a defined API. */ /*-----------------------------------------------------------------------*/ #include "ff.h" /* Obtains integer types */ #include "diskio.h" /* Declarations of disk functions */ #include "SPI_SD_driver.h" #include "diskio.h" //#include "flash.h" #include "spi.h" /* Definitions of physical drive number for each drive */ #define DEV_RAM 0 /* Example: Map Ramdisk to physical drive 0 */ #define DEV_MMC 1 /* Example: Map MMC/SD card to physical drive 1 */ #define DEV_USB 2 /* Example: Map USB MSD to physical drive 2 */ /*-----------------------------------------------------------------------*/ /* Get Drive Status */ /*-----------------------------------------------------------------------*/ #define SD_CARD 0 //SD卡,卷标为0 #define EX_FLASH 1 //外部flash,卷标为1 DSTATUS disk_status ( BYTE pdrv /* Physical drive nmuber to identify the drive */ ) { //DSTATUS stat; //int result; uint8_t res=0; switch (pdrv) { case SD_CARD : res = SD_Initialize(); if(res)//STM32 SPI的bug,在sd卡操作失败的时候如果不执行下面的语句,可能导致SPI读写异常 { SD_SPI_SpeedLow(); SD_SPI_ReadWriteByte(0xff);//提供额外的8个时钟 SD_SPI_SpeedHigh(); } break; // translate the reslut code here //return stat; // case DEV_MMC : // result = MMC_disk_status(); // // translate the reslut code here // return stat; // case DEV_USB : // result = USB_disk_status(); // // translate the reslut code here // return stat; } if(res)return STA_NOINIT; else return 0; //初始化成功 } /*-----------------------------------------------------------------------*/ /* Inidialize a Drive */ /*-----------------------------------------------------------------------*/ DSTATUS disk_initialize ( BYTE pdrv /* Physical drive nmuber to identify the drive */ ) { uint8_t res=0; switch(pdrv) { case SD_CARD://SD卡 res = SD_Initialize();//SD_Initialize() if(res)//STM32 SPI的bug,在sd卡操作失败的时候如果不执行下面的语句,可能导致SPI读写异常 { SD_SPI_SpeedLow(); SD_SPI_ReadWriteByte(0xff);//提供额外的8个时钟 SD_SPI_SpeedHigh(); } break; case EX_FLASH://外部flash //SPI_Flash_Init(); // if(SPI_FLASH_TYPE==W25Q64)FLASH_SECTOR_COUNT=2048*6;//W25Q64 // else FLASH_SECTOR_COUNT=2048*2; //其他 break; default: res=1; } if(res)return STA_NOINIT; else return 0; //初始化成功 } /*-----------------------------------------------------------------------*/ /* Read Sector(s) */ /*-----------------------------------------------------------------------*/ 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 */ ) { uint8_t res=0; if (!count)return RES_PARERR;//count不能等于0,否则返回参数错误 switch(pdrv) { case SD_CARD://SD卡 res=SD_ReadDisk(buff,sector,count); if(res)//STM32 SPI的bug,在sd卡操作失败的时候如果不执行下面的语句,可能导致SPI读写异常 { SD_SPI_SpeedLow(); SD_SPI_ReadWriteByte(0xff);//提供额外的8个时钟 SD_SPI_SpeedHigh(); } break; case EX_FLASH://外部flash // for(;count>0;count--) // { // //SPI_Flash_Read(buff,sector*FLASH_SECTOR_SIZE,FLASH_SECTOR_SIZE); // sector++; // buff+=FLASH_SECTOR_SIZE; // } res=0; break; default: res=1; } //处理返回值,将SPI_SD_driver.c的返回值转成ff.c的返回值 if(res==0x00)return RES_OK; else return RES_ERROR; } /*-----------------------------------------------------------------------*/ /* Write Sector(s) */ /*-----------------------------------------------------------------------*/ #if FF_FS_READONLY == 0 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 */ ) { uint8_t res=0; if (!count)return RES_PARERR;//count不能等于0,否则返回参数错误 switch(pdrv) { case SD_CARD://SD卡 res=SD_WriteDisk((u8*)buff,sector,count); break; case EX_FLASH://外部flash // for(;count>0;count--) // { // //SPI_Flash_Write((u8*)buff,sector*FLASH_SECTOR_SIZE,FLASH_SECTOR_SIZE); // sector++; // buff+=FLASH_SECTOR_SIZE; // } // res=0; break; default: res=1; } //处理返回值,将SPI_SD_driver.c的返回值转成ff.c的返回值 if(res == 0x00)return RES_OK; else return RES_ERROR; } #endif /*-----------------------------------------------------------------------*/ /* Miscellaneous Functions */ /*-----------------------------------------------------------------------*/ DRESULT disk_ioctl ( BYTE pdrv, /* Physical drive nmuber (0..) */ BYTE cmd, /* Control code */ void *buff /* Buffer to send/receive control data */ ) { DRESULT res; if(pdrv==SD_CARD)//SD卡 { switch(cmd) { case CTRL_SYNC: SD_CS_ENABLE(); if(SD_WaitReady()==0)res = RES_OK; else res = RES_ERROR; SD_CS_DISABLE(); break; case GET_SECTOR_SIZE: *(WORD*)buff = 512; res = RES_OK; break; case GET_BLOCK_SIZE: *(WORD*)buff = 8; res = RES_OK; break; case GET_SECTOR_COUNT: *(DWORD*)buff = SD_GetSectorCount(); res = RES_OK; break; default: res = RES_PARERR; break; } }else if(pdrv==EX_FLASH) //外部FLASH { switch(cmd) { case CTRL_SYNC: res = RES_OK; break; case GET_SECTOR_SIZE: // *(WORD*)buff = FLASH_SECTOR_SIZE; res = RES_OK; break; case GET_BLOCK_SIZE: // *(WORD*)buff = FLASH_BLOCK_SIZE; res = RES_OK; break; case GET_SECTOR_COUNT: // *(DWORD*)buff = FLASH_SECTOR_COUNT; res = RES_OK; break; default: res = RES_PARERR; break; } }else res=RES_ERROR;//其他的不支持 return res; } //获得时间 //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) */ //获得时间 //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) { RTC_TimeTypeDef time; RTC_DateTypeDef date; RTC_GetTime(RTC_Format_BIN,&time); RTC_GetDate(RTC_Format_BIN,&date); return ((date.RTC_Year+2000-1980)<<25| date.RTC_Month<<21| date.RTC_Date<<16| time.RTC_Hours<<11| time.RTC_Minutes<<5| time.RTC_Seconds>>1 ); }