You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
6.3 KiB

3 months ago
  1. #include "file_operate.h"
  2. //�������ڸ�ʽ���Ĺ���������
  3. BYTE workBuffer[4*User_Sector];
  4. /*����FatFs�ļ�ϵͳ*/
  5. void Mount_FatFs(void)
  6. {
  7. //�����ļ�ϵͳ
  8. FRESULT retUSER = f_mount(&User_FatFs, User_SDPath, 1);
  9. //��������
  10. if(retUSER != FR_OK)
  11. {
  12. //û���ļ�ϵͳ,��Ҫ��ʽ��
  13. if(retUSER == FR_NO_FILESYSTEM)
  14. {
  15. printf("\r\nû���ļ�ϵͳ,��ʼ��ʽ��\r\n");
  16. //�����ļ�ϵͳ
  17. retUSER = f_mkfs(User_SDPath, FM_FAT32, 0, workBuffer, 4*User_Sector);
  18. //��ʽ��ʧ��
  19. if(retUSER != FR_OK)
  20. {
  21. printf("��ʽ��ʧ��,�������� = %d\r\n", retUSER);
  22. }
  23. //��ʽ���ɹ�
  24. else
  25. {
  26. printf("��ʽ���ɹ�,��ʼ���¹���\r\n");
  27. //���ļ�ϵͳ�����¹���
  28. retUSER = f_mount(&User_FatFs, User_SDPath, 1);
  29. //����ʧ��
  30. if(retUSER != FR_OK)
  31. {
  32. printf("��������,�������� = %d\r\n", retUSER);
  33. }
  34. //���سɹ�
  35. else
  36. {
  37. printf("*** �ļ�ϵͳ���سɹ� ***\r\n");
  38. }
  39. }
  40. }
  41. //����û���ļ�ϵͳ,���Ƿ�����������
  42. else
  43. {
  44. printf("������������,�������� = %d\r\n", retUSER);
  45. }
  46. }
  47. //���ļ�ϵͳֱ�ӹ��ڳɹ�
  48. else
  49. {
  50. printf("�ļ�ϵͳ���سɹ�\r\n");
  51. }
  52. }
  53. /*��ȡ������Ϣ����LCD����ʾ*/
  54. void FatFs_GetDiskInfo(void)
  55. {
  56. FATFS *fs;
  57. //����ʣ���ظ�������
  58. DWORD fre_clust;
  59. //��ȡʣ���ظ���
  60. FRESULT res = f_getfree("0:", &fre_clust, &fs);
  61. //��ȡʧ��
  62. if(res != FR_OK)
  63. {
  64. printf("f_getfree() error\r\n");
  65. return;
  66. }
  67. printf("\r\n*** FAT disk info ***\r\n");
  68. //�ܵ���������
  69. DWORD tot_sect = (fs->n_fatent - 2) * fs->csize;
  70. //ʣ������������ = ʣ���ظ��� * ÿ���ص���������
  71. DWORD fre_sect = fre_clust * fs->csize;
  72. //����SD����U��, _MIN_SS=512�ֽ�
  73. #if _MAX_SS == _MIN_SS
  74. //SD����_MIN_SS�̶�Ϊ512,����11λ�൱�ڳ���2048
  75. //ʣ���ռ���С,��λ:MB,����SD��,U��
  76. DWORD freespace= (fre_sect>>11);
  77. //�ܿռ���С,��λ:MB,����SD��,U��
  78. DWORD totalSpace= (tot_sect>>11);
  79. #else
  80. //Flash�洢��,С����
  81. //ʣ���ռ���С,��λ:KB
  82. DWORD freespace= (fre_sect*fs->ssize)>>10;
  83. //�ܿռ���С,��λ:KB
  84. DWORD totalSpace= (tot_sect*fs->ssize)>>10;
  85. #endif
  86. //FAT����
  87. printf("FAT type = %d\r\n",fs->fs_type);
  88. printf("[1=FAT12,2=FAT16,3=FAT32,4=exFAT]\r\n");
  89. //������С,��λ�ֽ�
  90. printf("Sector size(bytes) = ");
  91. //SD���̶�512�ֽ�
  92. #if _MAX_SS == _MIN_SS
  93. printf("%d\r\n", _MIN_SS);
  94. #else
  95. //FLASH�洢��
  96. printf("%d\r\n", fs->ssize);
  97. #endif
  98. printf("Cluster size(sectors) = %d\r\n", fs->csize);
  99. printf("Total cluster count = %ld\r\n", fs->n_fatent-2);
  100. printf("Total sector count = %ld\r\n", tot_sect);
  101. //�ܿռ�
  102. #if _MAX_SS == _MIN_SS
  103. printf("Total space(MB) = %ld\r\n", totalSpace);
  104. #else
  105. printf("Total space(KB) = %ld\r\n", totalSpace);
  106. #endif
  107. //�������
  108. printf("Free cluster count = %ld\r\n",fre_clust);
  109. //������������
  110. printf("Free sector count = %ld\r\n", fre_sect);
  111. //���пռ�
  112. #if _MAX_SS == _MIN_SS
  113. printf("Free space(MB) = %ld\r\n", freespace);
  114. #else
  115. printf("Free space(KB) = %ld\r\n", freespace);
  116. #endif
  117. printf("Get FAT disk info OK\r\n");
  118. }
  119. /*�����ı��ļ�*/
  120. void FatFs_WriteTXTFile(TCHAR *filename,uint16_t year, uint8_t month, uint8_t day)
  121. {
  122. FIL file;
  123. printf("\r\n*** Creating TXT file: %s ***\r\n", filename);
  124. FRESULT res = f_open(&file, filename, FA_CREATE_ALWAYS | FA_WRITE);
  125. //����/�����ļ��ɹ�
  126. if(res == FR_OK)
  127. {
  128. //�ַ��������л��з�"\n"
  129. TCHAR str[]="Line1: Hello, FatFs***\n";
  130. //�����������"\0"
  131. f_puts(str, &file);
  132. printf("Write file OK: %s\r\n", filename);
  133. }
  134. else
  135. {
  136. printf("Open file error,error code: %d\r\n", res);
  137. }
  138. //ʹ�����Ϲر��ļ�
  139. f_close(&file);
  140. }
  141. /*��ȡһ���ı��ļ�������*/
  142. void FatFs_ReadTXTFile(TCHAR *filename)
  143. {
  144. printf("\r\n*** Reading TXT file: %s ***\r\n", filename);
  145. FIL file;
  146. //��ֻ����ʽ�����ļ�
  147. FRESULT res = f_open(&file, filename, FA_READ);
  148. //�򿪳ɹ�
  149. if(res == FR_OK)
  150. {
  151. //��ȡ����
  152. TCHAR str[100];
  153. //û�ж����ļ�����ĩβ
  154. while(!f_eof(&file))
  155. {
  156. //��ȡ1���ַ���,�Զ����Ͻ�������\0��
  157. f_gets(str,100, &file);
  158. printf("%s", str);
  159. }
  160. printf("\r\n");
  161. }
  162. //����û�и��ļ�
  163. else if(res == FR_NO_FILE)
  164. printf("File does not exist\r\n");
  165. //����ʧ��
  166. else
  167. printf("f_open() error,error code: %d\r\n", res);
  168. //�ر��ļ�
  169. f_close(&file);
  170. }
  171. /*ɨ������ʾָ��Ŀ¼�µ��ļ���Ŀ¼*/
  172. void FatFs_ScanDir(const TCHAR* PathName)
  173. {
  174. DIR dir; //Ŀ¼����
  175. FILINFO fno; //�ļ���Ϣ
  176. //����Ŀ¼
  177. FRESULT res = f_opendir(&dir, PathName);
  178. //����ʧ��
  179. if(res != FR_OK)
  180. {
  181. //�ر�Ŀ¼,ֱ���˳�����
  182. f_closedir(&dir);
  183. printf("\r\nf_opendir() error,error code: %d\r\n", res);
  184. return;
  185. }
  186. printf("\r\n*** All entries in dir: %s ***\r\n", PathName);
  187. //˳����ȡĿ¼�е��ļ�
  188. while(1)
  189. {
  190. //��ȡĿ¼�µ�һ����
  191. res = f_readdir(&dir, &fno);
  192. //�ļ���Ϊ�ձ�ʾû�ж������ɶ���
  193. if(res != FR_OK || fno.fname[0] == 0)
  194. break;
  195. //������һ��Ŀ¼
  196. if(fno.fattrib & AM_DIR)
  197. {
  198. printf("DIR: %s\r\n", fno.fname);
  199. }
  200. //������һ���ļ�
  201. else
  202. {
  203. printf("FILE: %s\r\n",fno.fname);
  204. }
  205. }
  206. //ɨ������,�ر�Ŀ¼
  207. printf("Scan dir OK\r\n");
  208. f_closedir(&dir);
  209. }
  210. /*��ȡһ���ļ����ļ���Ϣ*/
  211. void FatFs_GetFileInfo(TCHAR *filename)
  212. {
  213. printf("\r\n*** File info of: %s ***\r\n", filename);
  214. FILINFO fno;
  215. //�����ļ�����Ŀ¼�Ƿ�����
  216. FRESULT fr = f_stat(filename, &fno);
  217. //�������ڴ�fno�ж�ȡ�ļ���Ϣ
  218. if(fr == FR_OK)
  219. {
  220. printf("File size(bytes) = %ld\r\n", fno.fsize);
  221. printf("File attribute = 0x%x\r\n", fno.fattrib);
  222. printf("File Name = %s\r\n", fno.fname);
  223. //��������/�޸��ļ�ʱ��ʱ����
  224. FatFs_PrintfFileDate(fno.fdate, fno.ftime);
  225. }
  226. //����û�и��ļ�
  227. else if (fr == FR_NO_FILE)
  228. printf("File does not exist\r\n");
  229. //������������
  230. else
  231. printf("f_stat() error,error code: %d\r\n", fr);
  232. }
  233. /*ɾ���ļ�*/
  234. void FatFs_DeleteFile(TCHAR *filename)
  235. {
  236. printf("\r\n*** Delete File: %s ***\r\n", filename);
  237. FIL file;
  238. //�����ļ�
  239. FRESULT res = f_open(&file, filename, FA_OPEN_EXISTING);
  240. if(res == FR_OK)
  241. {
  242. //�ر��ļ�
  243. f_close(&file);
  244. printf("open successfully!\r\n");
  245. }
  246. //ɾ���ļ�
  247. res = f_unlink(filename);
  248. //ɾ���ɹ�
  249. if(res == FR_OK)
  250. {
  251. printf("The file was deleted successfully!\r\n");
  252. }
  253. //ɾ��ʧ��
  254. else
  255. {
  256. printf("File deletion failed, error code:%d\r\n", res);
  257. }
  258. }
  259. /*��ӡ�����ļ�����*/
  260. void FatFs_PrintfFileDate(WORD date, WORD time)
  261. {
  262. printf("File data = %d/%d/%d\r\n", ((date>>9)&0x7F)+1980, (date>>5)&0xF, date&0x1F);
  263. printf("File time = %d:%d:%d\r\n", (time>>11)&0x1F, (time>>5)&0x3F, time&0x1F);
  264. }