hard_sdio_sd.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "hard_sdio_sd.h"
  2. #include "board.h"
  3. #include "test.h"
  4. // 使用官方的SDMMC例程包 SD1 挂载在
  5. // PD27 D2 PD26 D3 PD21 CMD PD22 CLK PD18 D0 PD17 D1 PD28 CD
  6. // 修改时要修改初始化io 和时钟 在port文件内
  7. // 这里负责操作初始化IO 和 时钟的接口 注意在port文件填写对应的回调函数
  8. // SD_TEST 20260322 pass
  9. #ifdef SD_TEST
  10. #include "hpm_sdmmc_sd.h"
  11. #include "ff.h"
  12. #include "diskio.h"
  13. FATFS s_sd_disk;
  14. FIL s_file;
  15. DIR s_dir;
  16. FRESULT fatfs_result;
  17. BYTE work[FF_MAX_SS];
  18. const TCHAR driver_num_buf[4] = { DEV_SD + '0', ':', '/', '\0' };
  19. #define TEST_DIR_NAME "hpmicro_sd_test_dir0"
  20. static FRESULT sd_write_file(void)
  21. {
  22. FRESULT fresult = f_open(&s_file, "readme.txt", FA_WRITE | FA_CREATE_ALWAYS);
  23. if (fresult != FR_OK) {
  24. printf("Create new file failed, cause: %d\n", show_error_string(fresult));
  25. } else {
  26. printf("Create new file successfully, status=%d\n", fresult);
  27. }
  28. char hello_str[] = "Hello, this is SD card FATFS demo\n";
  29. UINT byte_written;
  30. fresult = f_write(&s_file, hello_str, sizeof(hello_str), &byte_written);
  31. if (fresult != FR_OK) {
  32. printf("Write file failed, cause: %s\n", show_error_string(fresult));
  33. } else {
  34. printf("Write file operation is successfully\n");
  35. }
  36. f_close(&s_file);
  37. return fresult;
  38. }
  39. static FRESULT sd_read_file(void)
  40. {
  41. FRESULT fresult = f_open(&s_file, "readme.txt", FA_READ);
  42. if (fresult != FR_OK) {
  43. printf("Open file failed, cause: %s\n", show_error_string(fresult));
  44. } else {
  45. printf("Open file successfully\n");
  46. }
  47. if (fresult != FR_OK) {
  48. return fresult;
  49. }
  50. TCHAR str[100];
  51. f_gets(str, sizeof(str), &s_file);
  52. printf("%s\n", str);
  53. f_close(&s_file);
  54. return fresult;
  55. }
  56. const char *show_error_string(FRESULT fresult)
  57. {
  58. const char *result_str;
  59. switch (fresult) {
  60. case FR_OK:
  61. result_str = "succeeded";
  62. break;
  63. case FR_DISK_ERR:
  64. result_str = "A hard error occurred in the low level disk I/O level";
  65. break;
  66. case FR_INT_ERR:
  67. result_str = "Assertion failed";
  68. break;
  69. case FR_NOT_READY:
  70. result_str = "The physical drive cannot work";
  71. break;
  72. case FR_NO_FILE:
  73. result_str = "Could not find the file";
  74. break;
  75. case FR_NO_PATH:
  76. result_str = "Could not find the path";
  77. break;
  78. case FR_INVALID_NAME:
  79. result_str = "Tha path name format is invalid";
  80. break;
  81. case FR_DENIED:
  82. result_str = "Access denied due to prohibited access or directory full";
  83. break;
  84. case FR_EXIST:
  85. result_str = "Access denied due to prohibited access";
  86. break;
  87. case FR_INVALID_OBJECT:
  88. result_str = "The file/directory object is invalid";
  89. break;
  90. case FR_WRITE_PROTECTED:
  91. result_str = "The physical drive is write protected";
  92. break;
  93. case FR_INVALID_DRIVE:
  94. result_str = "The logical driver number is invalid";
  95. break;
  96. case FR_NOT_ENABLED:
  97. result_str = "The volume has no work area";
  98. break;
  99. case FR_NO_FILESYSTEM:
  100. result_str = "There is no valid FAT volume";
  101. break;
  102. case FR_MKFS_ABORTED:
  103. result_str = "THe f_mkfs() aborted due to any problem";
  104. break;
  105. case FR_TIMEOUT:
  106. result_str = "Could not get a grant to access the volume within defined period";
  107. break;
  108. case FR_LOCKED:
  109. result_str = "The operation is rejected according to the file sharing policy";
  110. break;
  111. case FR_NOT_ENOUGH_CORE:
  112. result_str = "LFN working buffer could not be allocated";
  113. break;
  114. case FR_TOO_MANY_OPEN_FILES:
  115. result_str = "Number of open files > FF_FS_LOCK";
  116. break;
  117. case FR_INVALID_PARAMETER:
  118. result_str = "Given parameter is invalid";
  119. break;
  120. default:
  121. result_str = "Unknown error";
  122. break;
  123. }
  124. return result_str;
  125. }
  126. static FRESULT sd_mount_fs(void)
  127. {
  128. FRESULT fresult = f_mount(&s_sd_disk, driver_num_buf, 1);
  129. if (fresult == FR_OK) {
  130. printf("SD card has been mounted successfully\n");
  131. } else {
  132. printf("Failed to mount SD card, cause: %s\n", show_error_string(fresult));
  133. }
  134. fresult = f_chdrive(driver_num_buf);
  135. return fresult;
  136. }
  137. static FRESULT sd_mkfs(void)
  138. {
  139. printf("Formatting the SD card, depending on the SD card capacity, the formatting process may take a long time\n");
  140. FRESULT fresult = f_mkfs(driver_num_buf, NULL, work, sizeof(work));
  141. if (fresult != FR_OK) {
  142. printf("Making File system failed, cause: %s\n", show_error_string(fresult));
  143. } else {
  144. printf("Making file system is successful\n");
  145. }
  146. return fresult;
  147. }
  148. void SD1_test(void)
  149. {
  150. bool need_init_filesystem = true;
  151. board_init();
  152. /* Before doing FATFS operation, ensure the SD card is present */
  153. DSTATUS dstatus = disk_status(DEV_SD);
  154. if (dstatus == STA_NODISK) {
  155. printf("No disk in the SD slot, please insert an SD card...\n");
  156. do {
  157. dstatus = disk_status(DEV_SD);
  158. } while (dstatus == STA_NODISK);
  159. board_delay_ms(100);
  160. printf("Detected SD card, re-initialize the filesystem...\n");
  161. need_init_filesystem = true;
  162. }
  163. dstatus = disk_initialize(DEV_SD);
  164. if (dstatus != RES_OK) {
  165. printf("Failed to initialize SD disk\n");
  166. }
  167. if (need_init_filesystem) {
  168. fatfs_result = sd_mount_fs();
  169. if (fatfs_result == FR_NO_FILESYSTEM) {
  170. printf("There is no File system available, making file system...\n");
  171. fatfs_result = sd_mkfs();
  172. if (fatfs_result != FR_OK) {
  173. printf("Failed to make filesystem, cause:%s\n", show_error_string(fatfs_result));
  174. } else {
  175. need_init_filesystem = false;
  176. }
  177. }
  178. }
  179. }
  180. #endif