sd_card_sdio.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // Driver and interface for accessing SD card in SDIO mode
  2. // Used on BlueSCSI v1.1.
  3. #include "BlueSCSI_platform.h"
  4. #ifdef SD_USE_SDIO
  5. #include "BlueSCSI_log.h"
  6. #include "gd32f20x_sdio.h"
  7. #include "gd32f20x_dma.h"
  8. #include "gd32_sdio_sdcard.h"
  9. #include <SdFat.h>
  10. static sd_error_enum g_sdio_error = SD_OK;
  11. static int g_sdio_error_line = 0;
  12. static uint32_t g_sdio_card_status;
  13. static uint32_t g_sdio_clk_kHz;
  14. static sdio_card_type_enum g_sdio_card_type;
  15. static uint16_t g_sdio_card_rca;
  16. static uint32_t g_sdio_sector_count;
  17. #define checkReturnOk(call) ((g_sdio_error = (call)) == SD_OK ? true : logSDError(__LINE__))
  18. static bool logSDError(int line)
  19. {
  20. g_sdio_error_line = line;
  21. bluelog("SDIO SD card error on line ", line, ", error code ", (int)g_sdio_error);
  22. return false;
  23. }
  24. bool SdioCard::begin(SdioConfig sdioConfig)
  25. {
  26. rcu_periph_clock_enable(RCU_SDIO);
  27. rcu_periph_clock_enable(RCU_DMA1);
  28. nvic_irq_enable(SDIO_IRQn, 0, 0);
  29. g_sdio_error = sd_init();
  30. if (g_sdio_error != SD_OK)
  31. {
  32. // Don't spam the log when main program polls for card insertion.
  33. bluedbg("sd_init() failed: ", (int)g_sdio_error);
  34. return false;
  35. }
  36. return checkReturnOk(sd_card_information_get_short(&g_sdio_card_type, &g_sdio_card_rca))
  37. && checkReturnOk(sd_card_select_deselect(g_sdio_card_rca))
  38. && checkReturnOk(sd_cardstatus_get(&g_sdio_card_status))
  39. && checkReturnOk(sd_bus_mode_config(SDIO_BUSMODE_4BIT))
  40. && checkReturnOk(sd_transfer_mode_config(sdioConfig.useDma() ? SD_DMA_MODE : SD_POLLING_MODE))
  41. && (g_sdio_sector_count = sectorCount());
  42. }
  43. uint8_t SdioCard::errorCode() const
  44. {
  45. if (g_sdio_error == SD_OK)
  46. return SD_CARD_ERROR_NONE;
  47. else
  48. return 0x80 + g_sdio_error;
  49. }
  50. uint32_t SdioCard::errorData() const
  51. {
  52. return 0;
  53. }
  54. uint32_t SdioCard::errorLine() const
  55. {
  56. return g_sdio_error_line;
  57. }
  58. bool SdioCard::isBusy()
  59. {
  60. return (GPIO_ISTAT(SD_SDIO_DATA_PORT) & SD_SDIO_D0) == 0;
  61. }
  62. uint32_t SdioCard::kHzSdClk()
  63. {
  64. return g_sdio_clk_kHz;
  65. }
  66. bool SdioCard::readCID(cid_t* cid)
  67. {
  68. sd_cid_get((uint8_t*)cid);
  69. return true;
  70. }
  71. bool SdioCard::readCSD(csd_t* csd)
  72. {
  73. sd_csd_get((uint8_t*)csd);
  74. return true;
  75. }
  76. bool SdioCard::readOCR(uint32_t* ocr)
  77. {
  78. // SDIO mode does not have CMD58, but main program uses this to
  79. // poll for card presence. Return status register instead.
  80. return sd_cardstatus_get(ocr) == SD_OK;
  81. }
  82. bool SdioCard::readData(uint8_t* dst)
  83. {
  84. bluelog("SdioCard::readData() called but not implemented!");
  85. return false;
  86. }
  87. bool SdioCard::readStart(uint32_t sector)
  88. {
  89. bluelog("SdioCard::readStart() called but not implemented!");
  90. return false;
  91. }
  92. bool SdioCard::readStop()
  93. {
  94. bluelog("SdioCard::readStop() called but not implemented!");
  95. return false;
  96. }
  97. uint32_t SdioCard::sectorCount()
  98. {
  99. csd_t csd;
  100. sd_csd_get((uint8_t*)&csd);
  101. return sdCardCapacity(&csd);
  102. }
  103. uint32_t SdioCard::status()
  104. {
  105. uint32_t status = 0;
  106. if (!checkReturnOk(sd_cardstatus_get(&status)))
  107. return 0;
  108. else
  109. return status;
  110. }
  111. bool SdioCard::stopTransmission(bool blocking)
  112. {
  113. if (!checkReturnOk(sd_transfer_stop()))
  114. return false;
  115. if (!blocking)
  116. {
  117. return true;
  118. }
  119. else
  120. {
  121. uint32_t end = millis() + 100;
  122. while (millis() < end && isBusy())
  123. {
  124. }
  125. if (isBusy())
  126. {
  127. bluelog("SdioCard::stopTransmission() timeout");
  128. return false;
  129. }
  130. else
  131. {
  132. return true;
  133. }
  134. }
  135. }
  136. bool SdioCard::syncDevice()
  137. {
  138. if (sd_transfer_state_get() != SD_NO_TRANSFER)
  139. {
  140. return stopTransmission(true);
  141. }
  142. return true;
  143. }
  144. uint8_t SdioCard::type() const
  145. {
  146. if (g_sdio_card_type == SDIO_HIGH_CAPACITY_SD_CARD)
  147. return SD_CARD_TYPE_SDHC;
  148. else if (g_sdio_card_type == SDIO_STD_CAPACITY_SD_CARD_V2_0)
  149. return SD_CARD_TYPE_SD2;
  150. else
  151. return SD_CARD_TYPE_SD1;
  152. }
  153. bool SdioCard::writeData(const uint8_t* src)
  154. {
  155. bluelog("SdioCard::writeData() called but not implemented!");
  156. return false;
  157. }
  158. bool SdioCard::writeStart(uint32_t sector)
  159. {
  160. bluelog("SdioCard::writeStart() called but not implemented!");
  161. return false;
  162. }
  163. bool SdioCard::writeStop()
  164. {
  165. bluelog("SdioCard::writeStop() called but not implemented!");
  166. return false;
  167. }
  168. bool SdioCard::erase(uint32_t firstSector, uint32_t lastSector)
  169. {
  170. return checkReturnOk(sd_erase(firstSector * 512, lastSector * 512));
  171. }
  172. /* Writing and reading, with progress callback */
  173. static sd_callback_t m_stream_callback;
  174. static const uint8_t *m_stream_buffer;
  175. static uint32_t m_stream_count;
  176. static uint32_t m_stream_count_start;
  177. void bluescsiplatform_set_sd_callback(sd_callback_t func, const uint8_t *buffer)
  178. {
  179. m_stream_callback = func;
  180. m_stream_buffer = buffer;
  181. m_stream_count = 0;
  182. m_stream_count_start = 0;
  183. }
  184. static void sdio_callback(uint32_t complete)
  185. {
  186. if (m_stream_callback)
  187. {
  188. m_stream_callback(m_stream_count_start + complete);
  189. }
  190. }
  191. static sdio_callback_t get_stream_callback(const uint8_t *buf, uint32_t count, const char *accesstype, uint32_t sector)
  192. {
  193. m_stream_count_start = m_stream_count;
  194. if (m_stream_callback)
  195. {
  196. if (buf == m_stream_buffer + m_stream_count)
  197. {
  198. m_stream_count += count;
  199. return &sdio_callback;
  200. }
  201. else
  202. {
  203. bluedbg("SD card ", accesstype, "(", (int)sector,
  204. ") slow transfer, buffer", (uint32_t)buf, " vs. ", (uint32_t)(m_stream_buffer + m_stream_count));
  205. return NULL;
  206. }
  207. }
  208. return NULL;
  209. }
  210. bool SdioCard::writeSector(uint32_t sector, const uint8_t* src)
  211. {
  212. return checkReturnOk(sd_block_write((uint32_t*)src, (uint64_t)sector * 512, 512,
  213. get_stream_callback(src, 512, "writeSector", sector)));
  214. }
  215. bool SdioCard::writeSectors(uint32_t sector, const uint8_t* src, size_t n)
  216. {
  217. return checkReturnOk(sd_multiblocks_write((uint32_t*)src, (uint64_t)sector * 512, 512, n,
  218. get_stream_callback(src, n * 512, "writeSectors", sector)));
  219. }
  220. bool SdioCard::readSector(uint32_t sector, uint8_t* dst)
  221. {
  222. return checkReturnOk(sd_block_read((uint32_t*)dst, (uint64_t)sector * 512, 512,
  223. get_stream_callback(dst, 512, "readSector", sector)));
  224. }
  225. bool SdioCard::readSectors(uint32_t sector, uint8_t* dst, size_t n)
  226. {
  227. if (sector + n >= g_sdio_sector_count)
  228. {
  229. // sd_multiblocks_read() seems to have trouble reading the very last sector
  230. for (int i = 0; i < n; i++)
  231. {
  232. if (!readSector(sector + i, dst + i * 512))
  233. {
  234. bluelog("End of drive read failed at ", sector, " + ", i);
  235. return false;
  236. }
  237. }
  238. return true;
  239. }
  240. return checkReturnOk(sd_multiblocks_read((uint32_t*)dst, (uint64_t)sector * 512, 512, n,
  241. get_stream_callback(dst, n * 512, "readSectors", sector)));
  242. }
  243. // Check if a DMA request for SD card read has completed.
  244. // This is used to optimize the timing of data transfers on SCSI bus.
  245. bool check_sd_read_done()
  246. {
  247. return (DMA_CHCTL(DMA1, DMA_CH3) & DMA_CHXCTL_CHEN)
  248. && (DMA_INTF(DMA1) & DMA_FLAG_ADD(DMA_FLAG_FTF, DMA_CH3));
  249. }
  250. // These functions are not used for SDIO mode but are needed to avoid build error.
  251. void sdCsInit(SdCsPin_t pin) {}
  252. void sdCsWrite(SdCsPin_t pin, bool level) {}
  253. // Interrupt handler for SDIO
  254. extern "C" void SDIO_IRQHandler(void)
  255. {
  256. sd_interrupts_process();
  257. }
  258. // SDIO configuration for main program
  259. SdioConfig g_sd_sdio_config(DMA_SDIO);
  260. // SDIO configuration in crash
  261. SdioConfig g_sd_sdio_config_crash(0);
  262. #endif