sd_card_sdio.cpp 6.4 KB

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