275 lines
8.4 KiB
C
275 lines
8.4 KiB
C
|
|
/*-----------------------------------------------------------------------*/
|
|||
|
|
/* 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<53><44>,<2C><><EFBFBD><EFBFBD>Ϊ0
|
|||
|
|
#define EX_FLASH 1 //<2F>ⲿflash,<2C><><EFBFBD><EFBFBD>Ϊ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<50><49>bug,<2C><>sd<73><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܵ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>ܵ<EFBFBD><DCB5><EFBFBD>SPI<50><49>д<EFBFBD>쳣
|
|||
|
|
{
|
|||
|
|
SD_SPI_SpeedLow();
|
|||
|
|
SD_SPI_ReadWriteByte(0xff);//<2F>ṩ<EFBFBD><E1B9A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>8<EFBFBD><38>ʱ<EFBFBD><CAB1>
|
|||
|
|
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; //<2F><>ʼ<EFBFBD><CABC><EFBFBD>ɹ<EFBFBD>
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
/*-----------------------------------------------------------------------*/
|
|||
|
|
/* 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<53><44>
|
|||
|
|
res = SD_Initialize();//SD_Initialize()
|
|||
|
|
if(res)//STM32 SPI<50><49>bug,<2C><>sd<73><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܵ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>ܵ<EFBFBD><DCB5><EFBFBD>SPI<50><49>д<EFBFBD>쳣
|
|||
|
|
{
|
|||
|
|
SD_SPI_SpeedLow();
|
|||
|
|
SD_SPI_ReadWriteByte(0xff);//<2F>ṩ<EFBFBD><E1B9A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>8<EFBFBD><38>ʱ<EFBFBD><CAB1>
|
|||
|
|
SD_SPI_SpeedHigh();
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
case EX_FLASH://<2F>ⲿflash
|
|||
|
|
//SPI_Flash_Init();
|
|||
|
|
// if(SPI_FLASH_TYPE==W25Q64)FLASH_SECTOR_COUNT=2048*6;//W25Q64
|
|||
|
|
// else FLASH_SECTOR_COUNT=2048*2; //<2F><><EFBFBD><EFBFBD>
|
|||
|
|
break;
|
|||
|
|
default:
|
|||
|
|
res=1;
|
|||
|
|
}
|
|||
|
|
if(res)return STA_NOINIT;
|
|||
|
|
else return 0; //<2F><>ʼ<EFBFBD><CABC><EFBFBD>ɹ<EFBFBD>
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
/*-----------------------------------------------------------------------*/
|
|||
|
|
/* 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<6E><74><EFBFBD>ܵ<EFBFBD><DCB5><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD>ز<F2B7B5BB><D8B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
switch(pdrv)
|
|||
|
|
{
|
|||
|
|
case SD_CARD://SD<53><44>
|
|||
|
|
res=SD_ReadDisk(buff,sector,count);
|
|||
|
|
if(res)//STM32 SPI<50><49>bug,<2C><>sd<73><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܵ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>ܵ<EFBFBD><DCB5><EFBFBD>SPI<50><49>д<EFBFBD>쳣
|
|||
|
|
{
|
|||
|
|
SD_SPI_SpeedLow();
|
|||
|
|
SD_SPI_ReadWriteByte(0xff);//<2F>ṩ<EFBFBD><E1B9A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>8<EFBFBD><38>ʱ<EFBFBD><CAB1>
|
|||
|
|
SD_SPI_SpeedHigh();
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
case EX_FLASH://<2F>ⲿ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;
|
|||
|
|
}
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>SPI_SD_driver.c<>ķ<EFBFBD><C4B7><EFBFBD>ֵת<D6B5><D7AA>ff.c<>ķ<EFBFBD><C4B7><EFBFBD>ֵ
|
|||
|
|
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<6E><74><EFBFBD>ܵ<EFBFBD><DCB5><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD>ز<F2B7B5BB><D8B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
switch(pdrv)
|
|||
|
|
{
|
|||
|
|
case SD_CARD://SD<53><44>
|
|||
|
|
res=SD_WriteDisk((u8*)buff,sector,count);
|
|||
|
|
break;
|
|||
|
|
case EX_FLASH://<2F>ⲿ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;
|
|||
|
|
}
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>SPI_SD_driver.c<>ķ<EFBFBD><C4B7><EFBFBD>ֵת<D6B5><D7AA>ff.c<>ķ<EFBFBD><C4B7><EFBFBD>ֵ
|
|||
|
|
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<53><44>
|
|||
|
|
{
|
|||
|
|
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) //<2F>ⲿ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;//<2F><><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD>֧<EFBFBD><D6A7>
|
|||
|
|
return res;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
//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) */
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
//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
|
|||
|
|
);
|
|||
|
|
}
|