Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

213 linhas
4.3 KiB

  1. #include "SDCardMgr.h"
  2. #ifdef USE_FATFS
  3. #include "FatFS/diskio.h"
  4. #include "FatFS/ff.h"
  5. FATFS FatFs; /* File system object */
  6. FIL File[2]; /* File objects */
  7. BYTE Buff[4096]; /* Working buffer */
  8. #else
  9. #include "sd_hw_ctl.h"
  10. #include "FileSystem/fileio_lfn.h"
  11. #endif
  12. #include <stdio.h>
  13. #include "timer.h"
  14. int mSDCardState;
  15. int mCardDetected;
  16. int mCardMounted;
  17. #ifdef USE_FATFS
  18. #else
  19. uint16_t mDriveLetter;
  20. #endif
  21. #ifdef USE_FATFS
  22. int InitSDCard()
  23. {
  24. mSDCardState = SD_CARD_INIT_STATE;
  25. mCardDetected = 0;
  26. mCardMounted = 0;
  27. return MountDrive();
  28. }
  29. int TickSDCard()
  30. {
  31. return 1;
  32. }
  33. int MountDrive()
  34. {
  35. FRESULT res;
  36. res = f_mount(&FatFs, "", 0); /* Give a work area to the default drive */
  37. if(!res)
  38. {
  39. printf("Could not mount SD card\n");
  40. return 1;
  41. }
  42. else
  43. {
  44. printf("SD Card mounted successfuly");
  45. }
  46. return 0;
  47. }
  48. int IsDriveDetected()
  49. {
  50. return mCardDetected;
  51. }
  52. int IsDriveMounted()
  53. {
  54. return mCardMounted;
  55. }
  56. int ListRootDir()
  57. {
  58. FRESULT res;
  59. DIR dir;
  60. UINT i;
  61. static FILINFO fno;
  62. char path[] = "/";
  63. res = f_opendir(&dir, path); /* Open the directory */
  64. if (res == FR_OK) {
  65. for (;;) {
  66. res = f_readdir(&dir, &fno); /* Read a directory item */
  67. if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
  68. // if (fno.fattrib & AM_DIR) { /* It is a directory */
  69. // i = strlen(path);
  70. // sprintf(&path[i], "/%s", fno.fname);
  71. // res = scan_files(path); /* Enter the directory */
  72. // if (res != FR_OK) break;
  73. // path[i] = 0;
  74. // } else { /* It is a file. */
  75. printf("%s/%s\n", path, fno.fname);
  76. //}
  77. }
  78. f_closedir(&dir);
  79. }
  80. return 1;
  81. }
  82. #else
  83. void FILEIO_GetTimestamp (FILEIO_TIMESTAMP * timeStamp)
  84. {
  85. timeStamp->date.bitfield.day = 6;
  86. timeStamp->date.bitfield.month = 5;
  87. timeStamp->date.bitfield.year = (2017 - 1980);
  88. timeStamp->time.bitfield.hours = 9;
  89. timeStamp->time.bitfield.minutes = 5;
  90. timeStamp->time.bitfield.secondsDiv2 = 0;
  91. timeStamp->timeMs = 0;
  92. }
  93. int InitSDCard()
  94. {
  95. SD_SPIConfigurePins();
  96. FILEIO_Initialize();
  97. mSDCardState = SD_CARD_INIT_STATE;
  98. mCardDetected = 0;
  99. mCardMounted = 0;
  100. mDriveLetter = 'D';
  101. TimerStart(SD_CARD_DETECT_TIMER,5000);
  102. return 1;
  103. }
  104. int TickSDCard()
  105. {
  106. switch(mSDCardState)
  107. {
  108. case SD_CARD_INIT_STATE:
  109. {
  110. if(IsTimerExpired(SD_CARD_DETECT_TIMER))
  111. {
  112. if(FILEIO_MediaDetect(0, 0) == true)
  113. {
  114. mCardDetected = 1;
  115. mSDCardState = SD_CARD_MOUNT_DRIVE_STATE;
  116. printf("SD Card detected\n");
  117. }
  118. else
  119. {
  120. TimerStart(SD_CARD_DETECT_TIMER,999);
  121. }
  122. }
  123. break;
  124. }
  125. case SD_CARD_MOUNT_DRIVE_STATE:
  126. {
  127. if(FILEIO_DriveMount(mDriveLetter, 0, 0) == FILEIO_ERROR_NONE)
  128. {
  129. mCardMounted = 1;
  130. mSDCardState = SD_CARD_READY_STATE;
  131. printf("SD Card mounted on drive %c\n",(unsigned char)mDriveLetter);
  132. }
  133. else
  134. {
  135. mSDCardState = SD_CARD_ERROR_MOUNTING_STATE;
  136. printf("Error mounting SD card.\n");
  137. }
  138. break;
  139. }
  140. case SD_CARD_READY_STATE:
  141. {
  142. // if(FILEIO_MediaDetect(0, 0) == false)
  143. // {
  144. // mCardDetected = 0;
  145. // mSDCardState = SD_CARD_INIT_STATE;
  146. // }
  147. break;
  148. }
  149. case SD_CARD_ERROR_MOUNTING_STATE:
  150. {
  151. //wait for removal of the sd card...
  152. // if(FILEIO_MediaDetect(0, 0) == false)
  153. // {
  154. // mCardDetected = 0;
  155. // mSDCardState = SD_CARD_INIT_STATE;
  156. // printf("SD Card removed");
  157. // }
  158. break;
  159. }
  160. }
  161. }
  162. int MountDrive()
  163. {
  164. }
  165. int IsDriveDetected()
  166. {
  167. return mCardDetected;
  168. }
  169. int IsDriveMounted()
  170. {
  171. return mCardMounted;
  172. }
  173. #endif