|
@@ -24,15 +24,16 @@
|
|
|
#if USE_FATFS_RECORD
|
|
|
|
|
|
#include "dev_spi_sdcard.h"
|
|
|
+#include "func_spi_w25qxx.h"
|
|
|
#include "func_fatfs.h"
|
|
|
-
|
|
|
+#include "func_fatfs_record.h"
|
|
|
/* Private includes ----------------------------------------------------------*/
|
|
|
/* USER CODE BEGIN Includes */
|
|
|
|
|
|
/* USER CODE END Includes */
|
|
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
|
-#define MAX_FILE_COUNT 20
|
|
|
+
|
|
|
|
|
|
/* Private define ------------------------------------------------------------*/
|
|
|
/* USER CODE BEGIN PD */
|
|
@@ -59,10 +60,13 @@ FIL testFile;
|
|
|
uint8_t testBuffer[16] = "SD write success";
|
|
|
|
|
|
|
|
|
-char readbuffer[512];
|
|
|
-
|
|
|
+char fatfs_buf[4*1024];
|
|
|
|
|
|
-char SDPath[4];
|
|
|
+#if USE_SPI_SD
|
|
|
+const char FATFS_Path[4] = "0:/"; //挂载SD卡
|
|
|
+#else
|
|
|
+const char FATFS_Path[4] ;//= "1:/"; //挂载FLASH.
|
|
|
+#endif
|
|
|
char readnrf,reafsd;
|
|
|
|
|
|
/* USER CODE END PFP */
|
|
@@ -80,18 +84,42 @@ void func_fatfs_Get_Capacity(void);
|
|
|
int func_fatfs_init(void)
|
|
|
{
|
|
|
FRESULT res;
|
|
|
+
|
|
|
+ uint8_t format_disk = 0;
|
|
|
|
|
|
/* Initialize all configured peripherals */
|
|
|
- SDCARD_GPIO_Init();
|
|
|
- SDCARD_SPI_Init();
|
|
|
- SDCARD_FATFS_Init();
|
|
|
+
|
|
|
+ FATFS_Init();
|
|
|
+
|
|
|
+ //res = f_mkfs ( FATFS_Path, FM_ANY, 0, fatfs_buf, sizeof(fatfs_buf));
|
|
|
/* USER CODE BEGIN 2 */
|
|
|
- res = f_mount(&fileSystem, (const TCHAR*)&SDPath, 1);
|
|
|
+ res = f_mount(&fileSystem, (const TCHAR*)FATFS_Path, 1);
|
|
|
|
|
|
if(res != FR_OK)
|
|
|
{
|
|
|
func_record_fatfs_set_OK(false);
|
|
|
}
|
|
|
+ if(res == FR_NO_FILESYSTEM)//FLASH磁盘,FAT文件系统错误,重新格式化FLASH
|
|
|
+ {
|
|
|
+//
|
|
|
+ while(res != FR_OK)
|
|
|
+ {
|
|
|
+ if(format_disk)
|
|
|
+ {
|
|
|
+ func_w25q_erase_ship();
|
|
|
+ format_disk = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(format_disk)
|
|
|
+ {
|
|
|
+ res = f_mkfs (FATFS_Path, FM_ANY, 0, fatfs_buf, sizeof(fatfs_buf));
|
|
|
+ format_disk = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ res = f_mount(&fileSystem, (const TCHAR*)FATFS_Path, 1);
|
|
|
+ };
|
|
|
+
|
|
|
+ }
|
|
|
/* USER CODE END 2 */
|
|
|
|
|
|
/* Infinite loop */
|
|
@@ -120,12 +148,27 @@ int func_fatfs_main(void)
|
|
|
|
|
|
typedef struct fatfs_st
|
|
|
{
|
|
|
- uint16_t TotalSpace;
|
|
|
+ DWORD TotalSpace;
|
|
|
DWORD AvailableSize;
|
|
|
DWORD UsedSize;
|
|
|
}fatfs_st;
|
|
|
|
|
|
-fatfs_st sd_fatfs_st;
|
|
|
+fatfs_st fatfs_run_st;
|
|
|
+
|
|
|
+void func_fatfs_Capacity(void)
|
|
|
+{
|
|
|
+ FATFS *hFatfs = &fileSystem;
|
|
|
+
|
|
|
+ fatfs_run_st.TotalSpace=((hFatfs->n_fatent - 2) * hFatfs->ssize *hFatfs->csize)/1024;
|
|
|
+ fatfs_run_st.AvailableSize=(hFatfs->free_clst*hFatfs->ssize*hFatfs->csize)/1024;
|
|
|
+ fatfs_run_st.UsedSize=fatfs_run_st.TotalSpace-fatfs_run_st.AvailableSize;
|
|
|
+}
|
|
|
+
|
|
|
+uint32_t func_fatfs_get_free(void)
|
|
|
+{
|
|
|
+ return (fileSystem.free_clst*fileSystem.ssize*fileSystem.csize)/1024;
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
void func_fatfs_Get_Capacity(void)
|
|
|
{
|
|
@@ -136,12 +179,37 @@ void func_fatfs_Get_Capacity(void)
|
|
|
|
|
|
FRESULT res;
|
|
|
|
|
|
- res = f_getfree(SDPath, &fre_clust, &fs); /* 根目录 */
|
|
|
+ res = f_getfree(FATFS_Path, &fre_clust, &fs); /* 根目录 */
|
|
|
if ( res == FR_OK )
|
|
|
{
|
|
|
- sd_fatfs_st.TotalSpace=(uint16_t)(((fs->n_fatent - 2) * fs->csize ) / 2 /1024);
|
|
|
- sd_fatfs_st.AvailableSize=(uint16_t)((fre_clust * fs->csize) / 2 /1024);
|
|
|
- sd_fatfs_st.UsedSize=sd_fatfs_st.TotalSpace-sd_fatfs_st.AvailableSize;
|
|
|
+ fatfs_run_st.TotalSpace=(fs->n_fatent - 2);
|
|
|
+ fatfs_run_st.AvailableSize=fre_clust;
|
|
|
+ fatfs_run_st.UsedSize=fatfs_run_st.TotalSpace-fatfs_run_st.AvailableSize;
|
|
|
+ /* Print free space in unit of MB (assuming 512 bytes/sector) */
|
|
|
+// printf("\r\n%d MB total drive space.\r\n""%d MB available.\r\n""%d MB used.\r\n",TotalSpace, AvailableSize,UsedSize);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+// printf("Get SDCard Capacity Failed (%d)\r\n", result);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|
|
|
+void func_fatfs_Get_Capacity_Sample(void)
|
|
|
+{
|
|
|
+// FRESULT result;
|
|
|
+// FATFS FS;
|
|
|
+ FATFS *fs;
|
|
|
+ DWORD fre_clust;
|
|
|
+
|
|
|
+ FRESULT res;
|
|
|
+
|
|
|
+ res = f_getfree(FATFS_Path, &fre_clust, &fs); /* 根目录 */
|
|
|
+ if ( res == FR_OK )
|
|
|
+ {
|
|
|
+ fatfs_run_st.TotalSpace=(uint16_t)(((fs->n_fatent - 2) * fs->csize ) / 2 /1024);
|
|
|
+ fatfs_run_st.AvailableSize=(uint16_t)((fre_clust * fs->csize) / 2 /1024);
|
|
|
+ fatfs_run_st.UsedSize=fatfs_run_st.TotalSpace-fatfs_run_st.AvailableSize;
|
|
|
/* Print free space in unit of MB (assuming 512 bytes/sector) */
|
|
|
// printf("\r\n%d MB total drive space.\r\n""%d MB available.\r\n""%d MB used.\r\n",TotalSpace, AvailableSize,UsedSize);
|
|
|
}
|
|
@@ -165,7 +233,7 @@ FRESULT func_fatfs_miscellaneous(void)
|
|
|
|
|
|
printf("\n*************** 设备信息获取 ***************\r\n");
|
|
|
/* 获取设备信息和空簇大小 */
|
|
|
- f_res = f_getfree((TCHAR const*)SDPath, &fre_clust, &pfs);
|
|
|
+ f_res = f_getfree((TCHAR const*)FATFS_Path, &fre_clust, &pfs);
|
|
|
|
|
|
/* 计算得到总的扇区个数和空扇区个数 */
|
|
|
tot_sect = (pfs->n_fatent - 2) * pfs->csize;
|
|
@@ -189,10 +257,10 @@ FRESULT func_fatfs_miscellaneous(void)
|
|
|
/* 文件定位到文件起始位置 */
|
|
|
f_res = f_lseek(&file,0);
|
|
|
/* 读取文件所有内容到缓存区 */
|
|
|
- f_res = f_read(&file,readbuffer,f_size(&file),&fnum);
|
|
|
+ f_res = f_read(&file,fatfs_buf,f_size(&file),&fnum);
|
|
|
if(f_res == FR_OK)
|
|
|
{
|
|
|
- printf("》文件内容:\n%s\n",readbuffer);
|
|
|
+ printf("》文件内容:\n%s\n",fatfs_buf);
|
|
|
}
|
|
|
}
|
|
|
f_close(&file);
|
|
@@ -300,7 +368,7 @@ FRESULT func_fatfs_scan_files (char* path)
|
|
|
path[i] = 0;
|
|
|
//打开失败,跳出循环
|
|
|
if (res != FR_OK)
|
|
|
- break;
|
|
|
+ break;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -344,18 +412,34 @@ void func_fatfs_ReadDirTest(char*path)
|
|
|
}
|
|
|
|
|
|
|
|
|
+char* pstr = 0;
|
|
|
+char* pidx = 0;
|
|
|
+void func_record_fatfs_str(void)
|
|
|
+{
|
|
|
+ char str_name[] ="20221112.TXT" ;
|
|
|
|
|
|
+ while(1)
|
|
|
+ {
|
|
|
+ pstr = strstr(str_name,FILE_START);
|
|
|
+
|
|
|
+ pidx = strstr(str_name,FILE_END);
|
|
|
+ UNUSED(pstr);
|
|
|
+ UNUSED(pidx);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#if 0
|
|
|
static int qsort_cmp(const void *a,const void *b)
|
|
|
{
|
|
|
return strcmp(( char * ) a, (const char* )b);
|
|
|
}
|
|
|
-
|
|
|
+#define MAX_FILE_COUNT 20
|
|
|
char fileList[MAX_FILE_COUNT][30];//保存文件名
|
|
|
-
|
|
|
void func_fatfs_dir_qsort(uint32_t fcount)
|
|
|
{
|
|
|
qsort(fileList, fcount, sizeof(fileList[0]), qsort_cmp);
|
|
|
}
|
|
|
+#endif
|
|
|
|
|
|
#endif //--------------------------USE_FATFS_RECORD------------------------------
|
|
|
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|