#include "fatfs_user.h"
#include "usart.h"
#include "stdbool.h"
#include "string.h"
uint32_t totalsize = 0;
uint32_t freesize = 0;
FIL openfile;
FATFS SDFatFs = {0};
char SD_Path[4]={0,0,0,0};
FATFS USBfatfs;
char UDisk_Path[4]={0,0,0,0};
extern Diskio_drvTypeDef USBH_Driver;
uint8_t retSD; /* Return value for SD */
const char* DriverString[2]={
{"USBDisk"},
{"SDCard"}
};
//return 0:no sd card find
//return 1: find sd card
uint8_t Fatfs_init(uint8_t driver)
{
uint8_t res;
FATFS *Fatfs;
char *Path;
Diskio_drvTypeDef *Driver;
if(driver == SDCard)
{
Fatfs = &SDFatFs;
Path = (char*)&SD_Path;
Driver = &SD_Driver;
}else if(driver == USBDisk)
{
// Fatfs = &USBfatfs;
// Path = (char*)&UDisk_Path;
// Driver = &USBH_Driver;
}
/*## FatFS: Link the SD driver ###########################*/
res = FATFS_LinkDriver(Driver, Path);
if(res != FR_OK)
{
printf("\r\n%s FATFS LinkDriver error\r\n",DriverString[driver]);
return 0;
}
else{
printf("\r\n%s FATFS LinkDriver OK,Drive path is %s\r\n",DriverString[driver],Path);
}
/*##-2- Register the file system object to the FatFs module ##############*/
printf("%s FATFS mount\r\n",DriverString[driver]);
res = f_mount(Fatfs, (TCHAR const*)Path, 0);
if(res != FR_OK)
{
printf("%s FATFS mount Error\r\n",DriverString[driver]);
FATFS_UnLinkDriver(Path);
return 0;
}
else{
printf("%s FATFS mount OK,Fatfs init done\r\n",DriverString[driver]);
}
res = Fatfs_getfree(driver);//计算当时SD的容量跟剩余大小
if(res != FR_OK)
{
printf("%s FATFS get size Error\r\n",DriverString[driver]);
return 0;
}
else{
printf("%s FATFS total size is %d,free size is %d.\r\n",DriverString[driver],totalsize,freesize);
return 1;
}
}
uint8_t Fatfs_getfree(uint8_t driver)
{
uint8_t res;
FATFS *fs1;
uint32_t fre_clust=0, fre_sect=0, tot_sect=0;
char *Path;
if(driver == SDCard)
{
Path = (char*)&SD_Path;
}else if(driver == USBDisk)
{
Path = (char*)&UDisk_Path;
}
//得到磁盘信息及空闲簇数量
res =(uint32_t)f_getfree((const TCHAR*)Path, (DWORD*)&fre_clust, &fs1);
if(res==0)
{
tot_sect=(fs1->n_fatent-2)*fs1->csize; //得到总扇区数
fre_sect=fre_clust*fs1->csize; //得到空闲扇区数
#if _MAX_SS!=512 //扇区大小不是512字节,则转换为512字节
tot_sect*=fs1->ssize/512;
fre_sect*=fs1->ssize/512;
#endif
totalsize=tot_sect>>1; //单位为KB
freesize=fre_sect>>1; //单位为KB
}
return res;
}
uint8_t Fatfs_open(uint8_t driver,uint8_t*path,uint8_t mode)
{
char filepath[50];
memset(filepath,0,20);
if(driver == SDCard)
{
strcat(filepath,SD_Path);
}else if(driver == USBDisk)
{
strcat(filepath,UDisk_Path);
}
strcat((char*)filepath,(const char*)path);
retSD=f_open(&openfile,(const TCHAR*)filepath,mode);//打开文件夹
if(retSD != FR_OK)
{
printf("FATFS open Error:%d\r\n",retSD);
}
else printf("FATFS open file[%s] ok!\r\n",filepath);
return retSD;
}
uint8_t Fatfs_close(void)
{
retSD=f_close(&openfile);
return retSD;
}
//读出数据
//len:读出的长度
//返回值:执行结果
uint8_t Fatfs_read(uint8_t *data,uint32_t len)
{
uint16_t i;
uint32_t tlen=0;
uint32_t bytesread;
uint8_t *pdata = data;
for(i=0;i<len/512;i++)
{
retSD=f_read(&openfile,pdata,512,&bytesread);
if(retSD)
{
printf("Read Error:%d\r\n",retSD);
break;
}else
{
tlen+=bytesread;
pdata += bytesread;
}
}
if(len%512)
{
retSD=f_read(&openfile,pdata,len%512,&bytesread);
if(retSD) //读数据出错了
{
printf("\r\nRead Error:%d\r\n",retSD);
}else
{
tlen+=bytesread;
}
}
if(tlen)printf("\r\nReaded data len:%d\r\n",tlen);//读到的数据长度
printf("Read data over\r\n");
return retSD;
}
//写入数据
//data:数据缓存区
//len:写入长度
//返回值:执行结果
uint8_t Fatfs_write(uint8_t *data,uint32_t len)
{
uint32_t byteswrite;
printf("\r\nBegin Write file...\r\n");
printf("Write data len:%d\r\n",len);
retSD=f_write(&openfile,data,len,&byteswrite);
if(retSD)
{
printf("Write Error:%d\r\n",retSD);
}else printf("Writed data len:%d\r\n",byteswrite);
printf("Write data over.\r\n");
return retSD;
}