sd_card_sdio.cpp 7.5 KB

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