sd_card_sdio.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // Driver for accessing SD card in SDIO mode on RP2040.
  2. #include "ZuluSCSI_platform.h"
  3. #ifdef SD_USE_SDIO
  4. #include "ZuluSCSI_log.h"
  5. #include "rp2040_sdio.h"
  6. #include <hardware/gpio.h>
  7. #include <SdFat.h>
  8. #include <SdCard/SdCardInfo.h>
  9. static uint32_t g_sdio_ocr; // Operating condition register from card
  10. static uint32_t g_sdio_rca; // Relative card address
  11. static cid_t g_sdio_cid;
  12. static int g_sdio_error_line;
  13. static sdio_status_t g_sdio_error;
  14. #define checkReturnOk(call) ((g_sdio_error = (call)) == SDIO_OK ? true : logSDError(__LINE__))
  15. static bool logSDError(int line)
  16. {
  17. g_sdio_error_line = line;
  18. azlog("SDIO SD card error on line ", line, ", error code ", (int)g_sdio_error);
  19. return false;
  20. }
  21. bool SdioCard::begin(SdioConfig sdioConfig)
  22. {
  23. uint32_t reply;
  24. sdio_status_t status;
  25. rp2040_sdio_init();
  26. // Establish initial connection with the card
  27. for (int retries = 0; retries < 5; retries++)
  28. {
  29. delayMicroseconds(1000);
  30. reply = 0;
  31. rp2040_sdio_command_R1(CMD0, 0, NULL); // GO_IDLE_STATE
  32. status = rp2040_sdio_command_R1(CMD8, 0x1AA, &reply); // SEND_IF_COND
  33. if (status == SDIO_OK && reply == 0x1AA)
  34. {
  35. break;
  36. }
  37. }
  38. if (reply != 0x1AA || status != SDIO_OK)
  39. {
  40. azdbg("SDIO not responding to CMD8 SEND_IF_COND, status ", (int)status, " reply ", reply);
  41. return false;
  42. }
  43. // Send ACMD41 to begin card initialization and wait for it to complete
  44. uint32_t start = millis();
  45. do {
  46. if (!checkReturnOk(rp2040_sdio_command_R1(CMD55, 0, &reply)) || // APP_CMD
  47. !checkReturnOk(rp2040_sdio_command_R3(ACMD41, 0xD0040000, &g_sdio_ocr))) // 3.0V voltage
  48. // !checkReturnOk(rp2040_sdio_command_R1(ACMD41, 0xC0100000, &g_sdio_ocr)))
  49. {
  50. return false;
  51. }
  52. if ((uint32_t)(millis() - start) > 1000)
  53. {
  54. azlog("SDIO card initialization timeout");
  55. return false;
  56. }
  57. } while (!(g_sdio_ocr & (1 << 31)));
  58. // Get CID
  59. if (!checkReturnOk(rp2040_sdio_command_R2(CMD2, 0, (uint8_t*)&g_sdio_cid)))
  60. {
  61. azdbg("SDIO failed to read CID");
  62. return false;
  63. }
  64. // Get relative card address
  65. if (!checkReturnOk(rp2040_sdio_command_R1(CMD3, 0, &g_sdio_rca)))
  66. {
  67. azdbg("SDIO failed to get RCA");
  68. return false;
  69. }
  70. // Select card
  71. if (!checkReturnOk(rp2040_sdio_command_R1(CMD7, g_sdio_rca, &reply)))
  72. {
  73. azdbg("SDIO failed to select card");
  74. return false;
  75. }
  76. // Set 4-bit bus mode
  77. if (!checkReturnOk(rp2040_sdio_command_R1(CMD55, g_sdio_rca, &reply)) ||
  78. !checkReturnOk(rp2040_sdio_command_R1(ACMD6, 2, &reply)))
  79. {
  80. azdbg("SDIO failed to set bus width");
  81. return false;
  82. }
  83. return true;
  84. }
  85. uint8_t SdioCard::errorCode() const
  86. {
  87. return g_sdio_error;
  88. }
  89. uint32_t SdioCard::errorData() const
  90. {
  91. return 0;
  92. }
  93. uint32_t SdioCard::errorLine() const
  94. {
  95. return g_sdio_error_line;
  96. }
  97. bool SdioCard::isBusy()
  98. {
  99. return (sio_hw->gpio_in & (1 << SDIO_D0)) == 0;
  100. }
  101. uint32_t SdioCard::kHzSdClk()
  102. {
  103. return 0;
  104. }
  105. bool SdioCard::readCID(cid_t* cid)
  106. {
  107. *cid = g_sdio_cid;
  108. return true;
  109. }
  110. bool SdioCard::readCSD(csd_t* csd)
  111. {
  112. return checkReturnOk(rp2040_sdio_command_R2(CMD9, g_sdio_rca, (uint8_t*)csd)); // SEND_CSD
  113. }
  114. bool SdioCard::readOCR(uint32_t* ocr)
  115. {
  116. // SDIO mode does not have CMD58, but main program uses this to
  117. // poll for card presence. Return status register instead.
  118. return checkReturnOk(rp2040_sdio_command_R1(CMD13, g_sdio_rca, ocr));
  119. }
  120. bool SdioCard::readData(uint8_t* dst)
  121. {
  122. azlog("SdioCard::readData() called but not implemented!");
  123. return false;
  124. }
  125. bool SdioCard::readStart(uint32_t sector)
  126. {
  127. azlog("SdioCard::readStart() called but not implemented!");
  128. return false;
  129. }
  130. bool SdioCard::readStop()
  131. {
  132. azlog("SdioCard::readStop() called but not implemented!");
  133. return false;
  134. }
  135. uint32_t SdioCard::sectorCount()
  136. {
  137. csd_t csd;
  138. readCSD(&csd);
  139. return sdCardCapacity(&csd);
  140. }
  141. uint32_t SdioCard::status()
  142. {
  143. uint32_t reply;
  144. if (checkReturnOk(rp2040_sdio_command_R1(CMD13, g_sdio_rca, &reply)))
  145. return reply;
  146. else
  147. return 0;
  148. }
  149. bool SdioCard::stopTransmission(bool blocking)
  150. {
  151. uint32_t reply;
  152. if (!checkReturnOk(rp2040_sdio_command_R1(CMD12, 0, &reply)))
  153. {
  154. return false;
  155. }
  156. if (!blocking)
  157. {
  158. return true;
  159. }
  160. else
  161. {
  162. uint32_t end = millis() + 100;
  163. while (millis() < end && isBusy())
  164. {
  165. }
  166. if (isBusy())
  167. {
  168. azlog("SdioCard::stopTransmission() timeout");
  169. return false;
  170. }
  171. else
  172. {
  173. return true;
  174. }
  175. }
  176. }
  177. bool SdioCard::syncDevice()
  178. {
  179. return true;
  180. }
  181. uint8_t SdioCard::type() const
  182. {
  183. if (g_sdio_ocr & (1 << 30))
  184. return SD_CARD_TYPE_SDHC;
  185. else
  186. return SD_CARD_TYPE_SD2;
  187. }
  188. bool SdioCard::writeData(const uint8_t* src)
  189. {
  190. azlog("SdioCard::writeData() called but not implemented!");
  191. return false;
  192. }
  193. bool SdioCard::writeStart(uint32_t sector)
  194. {
  195. azlog("SdioCard::writeStart() called but not implemented!");
  196. return false;
  197. }
  198. bool SdioCard::writeStop()
  199. {
  200. azlog("SdioCard::writeStop() called but not implemented!");
  201. return false;
  202. }
  203. bool SdioCard::erase(uint32_t firstSector, uint32_t lastSector)
  204. {
  205. return false;
  206. // return checkReturnOk(sd_erase(firstSector * 512, lastSector * 512));
  207. }
  208. /* Writing and reading, with progress callback */
  209. static sd_callback_t m_stream_callback;
  210. static const uint8_t *m_stream_buffer;
  211. static uint32_t m_stream_count;
  212. static uint32_t m_stream_count_start;
  213. void azplatform_set_sd_callback(sd_callback_t func, const uint8_t *buffer)
  214. {
  215. m_stream_callback = func;
  216. m_stream_buffer = buffer;
  217. m_stream_count = 0;
  218. m_stream_count_start = 0;
  219. }
  220. static sd_callback_t get_stream_callback(const uint8_t *buf, uint32_t count)
  221. {
  222. m_stream_count_start = m_stream_count;
  223. if (m_stream_callback)
  224. {
  225. if (buf == m_stream_buffer + m_stream_count)
  226. {
  227. m_stream_count += count;
  228. return m_stream_callback;
  229. }
  230. else
  231. {
  232. azdbg("Stream buffer mismatch: ", (uint32_t)buf, " vs. ", (uint32_t)(m_stream_buffer + m_stream_count));
  233. return NULL;
  234. }
  235. }
  236. return NULL;
  237. }
  238. bool SdioCard::writeSector(uint32_t sector, const uint8_t* src)
  239. {
  240. sd_callback_t callback = get_stream_callback(src, 512);
  241. uint32_t reply;
  242. if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  243. !checkReturnOk(rp2040_sdio_command_R1(CMD24, sector, &reply)) || // WRITE_BLOCK
  244. !checkReturnOk(rp2040_sdio_tx_start(src, 1))) // Start transmission
  245. {
  246. return false;
  247. }
  248. do {
  249. uint32_t bytes_done;
  250. g_sdio_error = rp2040_sdio_tx_poll(&bytes_done);
  251. if (callback)
  252. {
  253. callback(m_stream_count_start + bytes_done);
  254. }
  255. } while (g_sdio_error == SDIO_BUSY);
  256. if (g_sdio_error != SDIO_OK)
  257. {
  258. azdbg("SdioCard::writeSector(", sector, ") failed: ", (int)g_sdio_error);
  259. }
  260. return g_sdio_error == SDIO_OK;
  261. }
  262. bool SdioCard::writeSectors(uint32_t sector, const uint8_t* src, size_t n)
  263. {
  264. sd_callback_t callback = get_stream_callback(src, 512);
  265. uint32_t reply;
  266. if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  267. !checkReturnOk(rp2040_sdio_command_R1(CMD55, g_sdio_rca, &reply)) || // APP_CMD
  268. !checkReturnOk(rp2040_sdio_command_R1(ACMD23, n, &reply)) || // SET_WR_CLK_ERASE_COUNT
  269. !checkReturnOk(rp2040_sdio_command_R1(CMD25, sector, &reply)) || // WRITE_MULTIPLE_BLOCK
  270. !checkReturnOk(rp2040_sdio_tx_start(src, n))) // Start transmission
  271. {
  272. return false;
  273. }
  274. do {
  275. uint32_t bytes_done;
  276. g_sdio_error = rp2040_sdio_tx_poll(&bytes_done);
  277. if (callback)
  278. {
  279. callback(m_stream_count_start + bytes_done);
  280. }
  281. } while (g_sdio_error == SDIO_BUSY);
  282. checkReturnOk(rp2040_sdio_command_R1(CMD12, 0, &reply)); // STOP_TRANSMISSION
  283. if (g_sdio_error != SDIO_OK)
  284. {
  285. azdbg("SdioCard::writeSectors(", sector, ",...,", (int)n, ") failed: ", (int)g_sdio_error);
  286. }
  287. return g_sdio_error == SDIO_OK;
  288. }
  289. bool SdioCard::readSector(uint32_t sector, uint8_t* dst)
  290. {
  291. sd_callback_t callback = get_stream_callback(dst, 512);
  292. uint32_t reply;
  293. if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  294. !checkReturnOk(rp2040_sdio_rx_start(dst, 1)) || // Prepare for reception
  295. !checkReturnOk(rp2040_sdio_command_R1(CMD17, sector, &reply))) // READ_SINGLE_BLOCK
  296. {
  297. return false;
  298. }
  299. do {
  300. uint32_t bytes_done;
  301. g_sdio_error = rp2040_sdio_rx_poll(&bytes_done);
  302. if (callback)
  303. {
  304. callback(m_stream_count_start + bytes_done);
  305. }
  306. } while (g_sdio_error == SDIO_BUSY);
  307. if (g_sdio_error != SDIO_OK)
  308. {
  309. azdbg("SdioCard::readSector(", sector, ") failed: ", (int)g_sdio_error);
  310. }
  311. return g_sdio_error == SDIO_OK;
  312. }
  313. bool SdioCard::readSectors(uint32_t sector, uint8_t* dst, size_t n)
  314. {
  315. sd_callback_t callback = get_stream_callback(dst, n * 512);
  316. uint32_t reply;
  317. if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  318. !checkReturnOk(rp2040_sdio_rx_start(dst, n)) || // Prepare for reception
  319. !checkReturnOk(rp2040_sdio_command_R1(CMD18, sector, &reply))) // READ_MULTIPLE_BLOCK
  320. {
  321. return false;
  322. }
  323. do {
  324. uint32_t bytes_done;
  325. g_sdio_error = rp2040_sdio_rx_poll(&bytes_done);
  326. if (callback)
  327. {
  328. callback(m_stream_count_start + bytes_done);
  329. }
  330. } while (g_sdio_error == SDIO_BUSY);
  331. checkReturnOk(rp2040_sdio_command_R1(CMD12, 0, &reply)); // STOP_TRANSMISSION
  332. if (g_sdio_error != SDIO_OK)
  333. {
  334. azdbg("SdioCard::readSectors(", sector, ",...,", (int)n, ") failed: ", (int)g_sdio_error);
  335. }
  336. return g_sdio_error == SDIO_OK;
  337. }
  338. // These functions are not used for SDIO mode but are needed to avoid build error.
  339. void sdCsInit(SdCsPin_t pin) {}
  340. void sdCsWrite(SdCsPin_t pin, bool level) {}
  341. // SDIO configuration for main program
  342. SdioConfig g_sd_sdio_config(DMA_SDIO);
  343. #endif