sd_card_sdio.cpp 6.6 KB

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