rp2040_sdio.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. // Implementation of SDIO communication for RP2040
  2. // Copyright (c) 2022 Rabbit Hole Computing™
  3. // Copyright (c) 2024 Tech by Androda, LLC
  4. //
  5. // The RP2040 official work-in-progress code at
  6. // https://github.com/raspberrypi/pico-extras/tree/master/src/rp2_common/pico_sd_card
  7. // may be useful reference, but this is independent implementation.
  8. //
  9. // For official SDIO specifications, refer to:
  10. // https://www.sdcard.org/downloads/pls/
  11. // "SDIO Physical Layer Simplified Specification Version 8.00"
  12. #include "rp2040_sdio.h"
  13. #include "rp2040_sdio.pio.h"
  14. #include <hardware/pio.h>
  15. #include <hardware/dma.h>
  16. //#include <hardware/gpio.h>
  17. #include <BlueSCSI_platform.h>
  18. #include <BlueSCSI_log.h>
  19. #define SDIO_PIO pio1
  20. #define SDIO_CMD_SM 0
  21. #define SDIO_DATA_SM 1
  22. #define SDIO_DMA_CH 4
  23. #define SDIO_DMA_CHB 5
  24. #define PIO_INSTR_MASK_REMOVE_DELAY 0xF8FF
  25. #define PIO_INSTR_MASK_GET_DELAY 0x700
  26. #define PIO_INSTR_JMP_MASK 0xE000
  27. #define PIO_INSTR_JMP_ADDR 0x1F
  28. // Maximum number of 512 byte blocks to transfer in one request
  29. #define SDIO_MAX_BLOCKS 256
  30. enum sdio_transfer_state_t { SDIO_IDLE, SDIO_RX, SDIO_TX, SDIO_TX_WAIT_IDLE};
  31. static struct {
  32. uint32_t pio_cmd_rsp_clk_offset;
  33. pio_sm_config pio_cfg_cmd_rsp;
  34. uint32_t pio_data_rx_offset;
  35. pio_sm_config pio_cfg_data_rx;
  36. uint32_t pio_data_tx_offset;
  37. pio_sm_config pio_cfg_data_tx;
  38. sdio_transfer_state_t transfer_state;
  39. uint32_t transfer_start_time;
  40. uint32_t *data_buf;
  41. uint32_t blocks_done; // Number of blocks transferred so far
  42. uint32_t total_blocks; // Total number of blocks to transfer
  43. uint32_t blocks_checksumed; // Number of blocks that have had CRC calculated
  44. uint32_t checksum_errors; // Number of checksum errors detected
  45. uint8_t cmdBuf[6];
  46. // Variables for block writes
  47. uint64_t next_wr_block_checksum;
  48. uint32_t end_token_buf[3]; // CRC and end token for write block
  49. sdio_status_t wr_status;
  50. uint32_t card_response;
  51. // Variables for block reads
  52. // This is used to perform DMA into data buffers and checksum buffers separately.
  53. struct {
  54. void * write_addr;
  55. uint32_t transfer_count;
  56. } dma_blocks[SDIO_MAX_BLOCKS * 2];
  57. struct {
  58. uint32_t top;
  59. uint32_t bottom;
  60. } received_checksums[SDIO_MAX_BLOCKS];
  61. } g_sdio;
  62. void rp2040_sdio_dma_irq();
  63. /*******************************************************
  64. * Checksum algorithms
  65. *******************************************************/
  66. // Table lookup for calculating CRC-7 checksum that is used in SDIO command packets.
  67. // Usage:
  68. // uint8_t crc = 0;
  69. // crc = crc7_table[crc ^ byte];
  70. // .. repeat for every byte ..
  71. static const uint8_t crc7_table[256] = {
  72. 0x00, 0x12, 0x24, 0x36, 0x48, 0x5a, 0x6c, 0x7e,
  73. 0x90, 0x82, 0xb4, 0xa6, 0xd8, 0xca, 0xfc, 0xee,
  74. 0x32, 0x20, 0x16, 0x04, 0x7a, 0x68, 0x5e, 0x4c,
  75. 0xa2, 0xb0, 0x86, 0x94, 0xea, 0xf8, 0xce, 0xdc,
  76. 0x64, 0x76, 0x40, 0x52, 0x2c, 0x3e, 0x08, 0x1a,
  77. 0xf4, 0xe6, 0xd0, 0xc2, 0xbc, 0xae, 0x98, 0x8a,
  78. 0x56, 0x44, 0x72, 0x60, 0x1e, 0x0c, 0x3a, 0x28,
  79. 0xc6, 0xd4, 0xe2, 0xf0, 0x8e, 0x9c, 0xaa, 0xb8,
  80. 0xc8, 0xda, 0xec, 0xfe, 0x80, 0x92, 0xa4, 0xb6,
  81. 0x58, 0x4a, 0x7c, 0x6e, 0x10, 0x02, 0x34, 0x26,
  82. 0xfa, 0xe8, 0xde, 0xcc, 0xb2, 0xa0, 0x96, 0x84,
  83. 0x6a, 0x78, 0x4e, 0x5c, 0x22, 0x30, 0x06, 0x14,
  84. 0xac, 0xbe, 0x88, 0x9a, 0xe4, 0xf6, 0xc0, 0xd2,
  85. 0x3c, 0x2e, 0x18, 0x0a, 0x74, 0x66, 0x50, 0x42,
  86. 0x9e, 0x8c, 0xba, 0xa8, 0xd6, 0xc4, 0xf2, 0xe0,
  87. 0x0e, 0x1c, 0x2a, 0x38, 0x46, 0x54, 0x62, 0x70,
  88. 0x82, 0x90, 0xa6, 0xb4, 0xca, 0xd8, 0xee, 0xfc,
  89. 0x12, 0x00, 0x36, 0x24, 0x5a, 0x48, 0x7e, 0x6c,
  90. 0xb0, 0xa2, 0x94, 0x86, 0xf8, 0xea, 0xdc, 0xce,
  91. 0x20, 0x32, 0x04, 0x16, 0x68, 0x7a, 0x4c, 0x5e,
  92. 0xe6, 0xf4, 0xc2, 0xd0, 0xae, 0xbc, 0x8a, 0x98,
  93. 0x76, 0x64, 0x52, 0x40, 0x3e, 0x2c, 0x1a, 0x08,
  94. 0xd4, 0xc6, 0xf0, 0xe2, 0x9c, 0x8e, 0xb8, 0xaa,
  95. 0x44, 0x56, 0x60, 0x72, 0x0c, 0x1e, 0x28, 0x3a,
  96. 0x4a, 0x58, 0x6e, 0x7c, 0x02, 0x10, 0x26, 0x34,
  97. 0xda, 0xc8, 0xfe, 0xec, 0x92, 0x80, 0xb6, 0xa4,
  98. 0x78, 0x6a, 0x5c, 0x4e, 0x30, 0x22, 0x14, 0x06,
  99. 0xe8, 0xfa, 0xcc, 0xde, 0xa0, 0xb2, 0x84, 0x96,
  100. 0x2e, 0x3c, 0x0a, 0x18, 0x66, 0x74, 0x42, 0x50,
  101. 0xbe, 0xac, 0x9a, 0x88, 0xf6, 0xe4, 0xd2, 0xc0,
  102. 0x1c, 0x0e, 0x38, 0x2a, 0x54, 0x46, 0x70, 0x62,
  103. 0x8c, 0x9e, 0xa8, 0xba, 0xc4, 0xd6, 0xe0, 0xf2
  104. };
  105. // Calculate the CRC16 checksum for parallel 4 bit lines separately.
  106. // When the SDIO bus operates in 4-bit mode, the CRC16 algorithm
  107. // is applied to each line separately and generates total of
  108. // 4 x 16 = 64 bits of checksum.
  109. __attribute__((optimize("O3")))
  110. uint64_t __not_in_flash_func(sdio_crc16_4bit_checksum)(uint32_t *data, uint32_t num_words)
  111. {
  112. uint64_t crc = 0;
  113. uint32_t *end = data + num_words;
  114. while (data < end)
  115. {
  116. for (int unroll = 0; unroll < 4; unroll++)
  117. {
  118. // Each 32-bit word contains 8 bits per line.
  119. // Reverse the bytes because SDIO protocol is big-endian.
  120. uint32_t data_in = __builtin_bswap32(*data++);
  121. // Shift out 8 bits for each line
  122. uint32_t data_out = crc >> 32;
  123. crc <<= 32;
  124. // XOR outgoing data to itself with 4 bit delay
  125. data_out ^= (data_out >> 16);
  126. // XOR incoming data to outgoing data with 4 bit delay
  127. data_out ^= (data_in >> 16);
  128. // XOR outgoing and incoming data to accumulator at each tap
  129. uint64_t xorred = data_out ^ data_in;
  130. crc ^= xorred;
  131. crc ^= xorred << (5 * 4);
  132. crc ^= xorred << (12 * 4);
  133. }
  134. }
  135. return crc;
  136. }
  137. /*******************************************************
  138. * Clock Runner
  139. *******************************************************/
  140. void __not_in_flash_func(cycleSdClock)() {
  141. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_nop() | pio_encode_sideset_opt(1, 1) | pio_encode_delay(1));
  142. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_nop() | pio_encode_sideset_opt(1, 0) | pio_encode_delay(1));
  143. }
  144. /*******************************************************
  145. * Status Register Receiver
  146. *******************************************************/
  147. sdio_status_t __not_in_flash_func(receive_status_register)(uint8_t* sds) {
  148. rp2040_sdio_rx_start(sds, 1, 64);
  149. // Wait for the DMA operation to complete, or fail if it took too long
  150. waitagain:
  151. while (dma_channel_is_busy(SDIO_DMA_CHB) || dma_channel_is_busy(SDIO_DMA_CH))
  152. {
  153. if ((uint32_t)(millis() - g_sdio.transfer_start_time) > 2)
  154. {
  155. // Reset the state machine program
  156. dma_channel_abort(SDIO_DMA_CHB);
  157. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false);
  158. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  159. return SDIO_ERR_RESPONSE_TIMEOUT;
  160. }
  161. }
  162. // Assert that both DMA channels are complete
  163. if(dma_channel_is_busy(SDIO_DMA_CHB) || dma_channel_is_busy(SDIO_DMA_CH)) {
  164. // Wait failure, go back.
  165. goto waitagain;
  166. }
  167. pio_sm_set_enabled(SDIO_PIO, SDIO_DATA_SM, false);
  168. g_sdio.transfer_state = SDIO_IDLE;
  169. return SDIO_OK;
  170. }
  171. /*******************************************************
  172. * Basic SDIO command execution
  173. *******************************************************/
  174. static void __not_in_flash_func(sdio_send_command)(uint8_t command, uint32_t arg, uint8_t response_bits)
  175. {
  176. // if (command != 41 && command != 55) {
  177. // log("C: ", (int)command, " A: ", arg);
  178. // }
  179. io_wo_8* txFifo = reinterpret_cast<io_wo_8*>(&SDIO_PIO->txf[SDIO_CMD_SM]);
  180. // Reinitialize the CMD SM
  181. pio_sm_init(SDIO_PIO, SDIO_CMD_SM, g_sdio.pio_cmd_rsp_clk_offset, &g_sdio.pio_cfg_cmd_rsp);
  182. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_CLK, 1, true);
  183. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_CMD, 1, true);
  184. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_D0, 4, false);
  185. // Pin direction: output, initial state should be high
  186. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_set(pio_pins, 1));
  187. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_set(pio_pindirs, 1));
  188. // Write the number of tx / rx bits to the SM
  189. *txFifo = 55; // Write 56 bits total
  190. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_out(pio_x, 8));
  191. *txFifo = response_bits ? response_bits - 1 : 0; // Bit count to receive
  192. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_out(pio_y, 8));
  193. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, true);
  194. // Build the command bytes (commands are 48 bits long)
  195. g_sdio.cmdBuf[0] = command | 0x40;
  196. g_sdio.cmdBuf[1] = (uint8_t)(arg >> 24U);
  197. g_sdio.cmdBuf[2] = (uint8_t)(arg >> 16U);
  198. g_sdio.cmdBuf[3] = (uint8_t)(arg >> 8U);
  199. g_sdio.cmdBuf[4] = (uint8_t)arg;
  200. // Get the SM clocking while we calculate CRCs
  201. *txFifo = 0XFF;
  202. // CRC calculation
  203. uint8_t crc = 0;
  204. for(uint8_t i = 0; i < 5; i++) {
  205. crc = crc7_table[crc ^ g_sdio.cmdBuf[i]];
  206. }
  207. crc = crc | 0x1;
  208. g_sdio.cmdBuf[5] = crc;
  209. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CH);
  210. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_8);
  211. channel_config_set_read_increment(&dmacfg, true);
  212. channel_config_set_write_increment(&dmacfg, false);
  213. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_CMD_SM, true));
  214. dma_channel_configure(SDIO_DMA_CH, &dmacfg, &SDIO_PIO->txf[SDIO_CMD_SM], &g_sdio.cmdBuf, 6, true);
  215. }
  216. sdio_status_t __not_in_flash_func(rp2040_sdio_command_R1)(uint8_t command, uint32_t arg, uint32_t *response)
  217. {
  218. uint32_t resp[2];
  219. if (response) {
  220. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CHB);
  221. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_8);
  222. channel_config_set_read_increment(&dmacfg, false);
  223. channel_config_set_write_increment(&dmacfg, true);
  224. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_CMD_SM, false)); //6 * 8 = 48 bits
  225. dma_channel_configure(SDIO_DMA_CHB, &dmacfg, &resp, &SDIO_PIO->rxf[SDIO_CMD_SM], 6, true);
  226. }
  227. sdio_send_command(command, arg, response ? 48 : 0);
  228. uint32_t start = millis();
  229. if (response)
  230. {
  231. // Wait for DMA channel to receive response
  232. while (dma_channel_is_busy(SDIO_DMA_CHB))
  233. {
  234. if ((uint32_t)(millis() - start) > 2)
  235. {
  236. if (command != 8) {
  237. /*debug*/log("Timeout waiting for response in rp2040_sdio_command_R1(", (int)command, "), ",
  238. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_CMD_SM) - (int)g_sdio.pio_cmd_rsp_clk_offset,
  239. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_CMD_SM),
  240. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_CMD_SM));
  241. }
  242. // Reset the state machine program
  243. dma_channel_abort(SDIO_DMA_CHB);
  244. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false); // Turn off the CMD SM, there was an error
  245. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  246. return SDIO_ERR_RESPONSE_TIMEOUT;
  247. }
  248. }
  249. // Must bswap due to 8 bit segmentation
  250. resp[0] = __builtin_bswap32(resp[0]);
  251. resp[1] = __builtin_bswap32(resp[1]) >> 16;
  252. // debuglog("SDIO R1 response: ", resp0, " ", resp1);
  253. // Calculate response checksum
  254. uint8_t crc = 0;
  255. crc = crc7_table[crc ^ ((resp[0] >> 24) & 0xFF)];
  256. crc = crc7_table[crc ^ ((resp[0] >> 16) & 0xFF)];
  257. crc = crc7_table[crc ^ ((resp[0] >> 8) & 0xFF)];
  258. crc = crc7_table[crc ^ ((resp[0] >> 0) & 0xFF)];
  259. crc = crc7_table[crc ^ ((resp[1] >> 8) & 0xFF)];
  260. uint8_t actual_crc = ((resp[1] >> 0) & 0xFE);
  261. if (crc != actual_crc)
  262. {
  263. debuglog("rp2040_sdio_command_R1(", (int)command, "): CRC error, calculated ", crc, " packet has ", actual_crc);
  264. debuglog("resp[0]:", resp[0], "resp[1]:", resp[1]);
  265. return SDIO_ERR_RESPONSE_CRC;
  266. }
  267. uint8_t response_cmd = ((resp[0] >> 24) & 0xFF);
  268. if (response_cmd != command && command != 41)
  269. {
  270. debuglog("rp2040_sdio_command_R1(", (int)command, "): received reply for ", (int)response_cmd);
  271. return SDIO_ERR_RESPONSE_CODE;
  272. }
  273. *response = ((resp[0] & 0xFFFFFF) << 8) | ((resp[1] >> 8) & 0xFF);
  274. } else {
  275. // Wait for CMD SM TX FIFO Stall (all command bits were sent)
  276. uint32_t tx_stall_flag = 1u << (PIO_FDEBUG_TXSTALL_LSB + SDIO_CMD_SM);
  277. // Clear the stall marker
  278. SDIO_PIO->fdebug = tx_stall_flag;
  279. // Wait for the stall
  280. while (!(SDIO_PIO->fdebug & tx_stall_flag)) {
  281. if ((uint32_t)(millis() - start) > 2)
  282. {
  283. if (command != 8) {
  284. /*debug*/log("Timeout waiting for CMD TX in rp2040_sdio_command_R1(", (int)command, "), ",
  285. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_CMD_SM) - (int)g_sdio.pio_cmd_rsp_clk_offset,
  286. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_CMD_SM),
  287. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_CMD_SM));
  288. }
  289. // Reset the state machine program
  290. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false); // Turn off the CMD SM, there was an error
  291. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  292. return SDIO_ERR_RESPONSE_TIMEOUT;
  293. }
  294. }
  295. }
  296. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false);
  297. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  298. return SDIO_OK;
  299. }
  300. sdio_status_t __not_in_flash_func(rp2040_sdio_command_R2)(uint8_t command, uint32_t arg, uint8_t response[16])
  301. {
  302. // The response is too long to fit in the PIO FIFO, so use DMA to receive it.
  303. uint32_t response_buf[5];
  304. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CHB);
  305. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_8);
  306. channel_config_set_read_increment(&dmacfg, false);
  307. channel_config_set_write_increment(&dmacfg, true);
  308. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_CMD_SM, false)); //17 * 8 = 136
  309. dma_channel_configure(SDIO_DMA_CHB, &dmacfg, &response_buf, &SDIO_PIO->rxf[SDIO_CMD_SM], 17, true);
  310. sdio_send_command(command, arg, 136);
  311. uint32_t start = millis();
  312. while (dma_channel_is_busy(SDIO_DMA_CHB))
  313. {
  314. if ((uint32_t)(millis() - start) > 2)
  315. {
  316. debuglog("Timeout waiting for response in rp2040_sdio_command_R2(", (int)command, "), ",
  317. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_CMD_SM) - (int)g_sdio.pio_cmd_rsp_clk_offset,
  318. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_CMD_SM),
  319. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_CMD_SM));
  320. // Reset the state machine program
  321. dma_channel_abort(SDIO_DMA_CHB);
  322. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false); // Turn off the CMD SM, there was an error
  323. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  324. return SDIO_ERR_RESPONSE_TIMEOUT;
  325. }
  326. }
  327. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false); // Turn off the CMD SM, its job is done
  328. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  329. dma_channel_abort(SDIO_DMA_CHB);
  330. // Must byte swap because receiving 8-bit chunks instead of 32 bit
  331. response_buf[0] = __builtin_bswap32(response_buf[0]);
  332. response_buf[1] = __builtin_bswap32(response_buf[1]);
  333. response_buf[2] = __builtin_bswap32(response_buf[2]);
  334. response_buf[3] = __builtin_bswap32(response_buf[3]);
  335. response_buf[4] = __builtin_bswap32(response_buf[4]) >> 24;
  336. // Copy the response payload to output buffer
  337. response[0] = ((response_buf[0] >> 16) & 0xFF);
  338. response[1] = ((response_buf[0] >> 8) & 0xFF);
  339. response[2] = ((response_buf[0] >> 0) & 0xFF);
  340. response[3] = ((response_buf[1] >> 24) & 0xFF);
  341. response[4] = ((response_buf[1] >> 16) & 0xFF);
  342. response[5] = ((response_buf[1] >> 8) & 0xFF);
  343. response[6] = ((response_buf[1] >> 0) & 0xFF);
  344. response[7] = ((response_buf[2] >> 24) & 0xFF);
  345. response[8] = ((response_buf[2] >> 16) & 0xFF);
  346. response[9] = ((response_buf[2] >> 8) & 0xFF);
  347. response[10] = ((response_buf[2] >> 0) & 0xFF);
  348. response[11] = ((response_buf[3] >> 24) & 0xFF);
  349. response[12] = ((response_buf[3] >> 16) & 0xFF);
  350. response[13] = ((response_buf[3] >> 8) & 0xFF);
  351. response[14] = ((response_buf[3] >> 0) & 0xFF);
  352. response[15] = ((response_buf[4] >> 0) & 0xFF);
  353. // Calculate checksum of the payload
  354. uint8_t crc = 0;
  355. for (int i = 0; i < 15; i++)
  356. {
  357. crc = crc7_table[crc ^ response[i]];
  358. }
  359. uint8_t actual_crc = response[15] & 0xFE;
  360. if (crc != actual_crc)
  361. {
  362. debuglog("rp2040_sdio_command_R2(", (int)command, "): CRC error, calculated ", crc, " packet has ", actual_crc);
  363. return SDIO_ERR_RESPONSE_CRC;
  364. }
  365. uint8_t response_cmd = ((response_buf[0] >> 24) & 0xFF);
  366. if (response_cmd != 0x3F)
  367. {
  368. debuglog("rp2040_sdio_command_R2(", (int)command, "): Expected reply code 0x3F");
  369. return SDIO_ERR_RESPONSE_CODE;
  370. }
  371. return SDIO_OK;
  372. }
  373. sdio_status_t __not_in_flash_func(rp2040_sdio_command_R3)(uint8_t command, uint32_t arg, uint32_t *response)
  374. {
  375. uint32_t resp[2];
  376. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CHB);
  377. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_8);
  378. channel_config_set_read_increment(&dmacfg, false);
  379. channel_config_set_write_increment(&dmacfg, true);
  380. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_CMD_SM, false)); //6 * 8 = 48 bits
  381. dma_channel_configure(SDIO_DMA_CHB, &dmacfg, &resp, &SDIO_PIO->rxf[SDIO_CMD_SM], 6, true);
  382. sdio_send_command(command, arg, 48);
  383. // Wait for response
  384. uint32_t start = millis();
  385. while (dma_channel_is_busy(SDIO_DMA_CHB))
  386. {
  387. if ((uint32_t)(millis() - start) > 2)
  388. {
  389. debuglog("Timeout waiting for response in rp2040_sdio_command_R3(", (int)command, "), ",
  390. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_CMD_SM) - (int)g_sdio.pio_cmd_rsp_clk_offset,
  391. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_CMD_SM),
  392. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_CMD_SM));
  393. // Reset the state machine program
  394. dma_channel_abort(SDIO_DMA_CHB);
  395. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false); // Turn off the CMD SM, there was an error
  396. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  397. return SDIO_ERR_RESPONSE_TIMEOUT;
  398. }
  399. }
  400. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false); // Turn off the CMD SM, its job is done
  401. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  402. // Must bswap due to 8 bit transfer
  403. resp[0] = __builtin_bswap32(resp[0]);
  404. resp[1] = __builtin_bswap32(resp[1]) >> 16;
  405. *response = ((resp[0] & 0xFFFFFF) << 8) | ((resp[1] >> 8) & 0xFF);
  406. // debuglog("SDIO R3 response: ", resp0, " ", resp1);
  407. return SDIO_OK;
  408. }
  409. /*******************************************************
  410. * Data reception from SD card
  411. *******************************************************/
  412. sdio_status_t __not_in_flash_func(rp2040_sdio_rx_start)(uint8_t *buffer, uint32_t num_blocks, uint32_t block_size)
  413. {
  414. // Buffer must be aligned
  415. assert(((uint32_t)buffer & 3) == 0 && num_blocks <= SDIO_MAX_BLOCKS);
  416. g_sdio.transfer_state = SDIO_RX;
  417. g_sdio.transfer_start_time = millis();
  418. g_sdio.data_buf = (uint32_t*)buffer;
  419. g_sdio.blocks_done = 0;
  420. g_sdio.total_blocks = num_blocks;
  421. g_sdio.blocks_checksumed = 0;
  422. g_sdio.checksum_errors = 0;
  423. // Create DMA block descriptors to store each block of block_size bytes of data to buffer
  424. // and then 8 bytes to g_sdio.received_checksums.
  425. for (int i = 0; i < num_blocks; i++)
  426. {
  427. g_sdio.dma_blocks[i * 2].write_addr = buffer + (i * block_size);
  428. g_sdio.dma_blocks[i * 2].transfer_count = block_size / sizeof(uint32_t);
  429. g_sdio.dma_blocks[i * 2 + 1].write_addr = &g_sdio.received_checksums[i];
  430. g_sdio.dma_blocks[i * 2 + 1].transfer_count = 2;
  431. }
  432. g_sdio.dma_blocks[num_blocks * 2].write_addr = 0;
  433. g_sdio.dma_blocks[num_blocks * 2].transfer_count = 0;
  434. // Configure first DMA channel for reading from the PIO RX fifo
  435. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CH);
  436. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_32);
  437. channel_config_set_read_increment(&dmacfg, false);
  438. channel_config_set_write_increment(&dmacfg, true);
  439. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_DATA_SM, false));
  440. channel_config_set_bswap(&dmacfg, true);
  441. channel_config_set_chain_to(&dmacfg, SDIO_DMA_CHB);
  442. dma_channel_configure(SDIO_DMA_CH, &dmacfg, 0, &SDIO_PIO->rxf[SDIO_DATA_SM], 0, false);
  443. // Configure second DMA channel for reconfiguring the first one
  444. dmacfg = dma_channel_get_default_config(SDIO_DMA_CHB);
  445. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_32);
  446. channel_config_set_read_increment(&dmacfg, true);
  447. channel_config_set_write_increment(&dmacfg, true);
  448. channel_config_set_ring(&dmacfg, true, 3);
  449. dma_channel_configure(SDIO_DMA_CHB, &dmacfg, &dma_hw->ch[SDIO_DMA_CH].al1_write_addr,
  450. g_sdio.dma_blocks, 2, false);
  451. // Initialize PIO state machine
  452. pio_sm_init(SDIO_PIO, SDIO_DATA_SM, g_sdio.pio_data_rx_offset, &g_sdio.pio_cfg_data_rx);
  453. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_DATA_SM, SDIO_CLK, 1, true);
  454. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_DATA_SM, SDIO_D0, 4, false);
  455. // Write number of nibbles to receive to Y register
  456. pio_sm_put(SDIO_PIO, SDIO_DATA_SM, (block_size * 2) + 16 - 1);
  457. pio_sm_exec(SDIO_PIO, SDIO_DATA_SM, pio_encode_out(pio_y, 32));
  458. // Enable RX FIFO join because we don't need the TX FIFO during transfer.
  459. // This gives more leeway for the DMA block switching
  460. SDIO_PIO->sm[SDIO_DATA_SM].shiftctrl |= PIO_SM0_SHIFTCTRL_FJOIN_RX_BITS;
  461. // Start PIO and DMA
  462. dma_channel_start(SDIO_DMA_CHB);
  463. pio_sm_set_enabled(SDIO_PIO, SDIO_DATA_SM, true);
  464. return SDIO_OK;
  465. }
  466. // Check checksums for received blocks
  467. static void __not_in_flash_func(sdio_verify_rx_checksums)(uint32_t maxcount)
  468. {
  469. while (g_sdio.blocks_checksumed < g_sdio.blocks_done && maxcount-- > 0)
  470. {
  471. // Calculate checksum from received data
  472. int blockidx = g_sdio.blocks_checksumed++;
  473. uint64_t checksum = sdio_crc16_4bit_checksum(g_sdio.data_buf + blockidx * SDIO_WORDS_PER_BLOCK,
  474. SDIO_WORDS_PER_BLOCK);
  475. // Convert received checksum to little-endian format
  476. uint32_t top = __builtin_bswap32(g_sdio.received_checksums[blockidx].top);
  477. uint32_t bottom = __builtin_bswap32(g_sdio.received_checksums[blockidx].bottom);
  478. uint64_t expected = ((uint64_t)top << 32) | bottom;
  479. if (checksum != expected)
  480. {
  481. g_sdio.checksum_errors++;
  482. if (g_sdio.checksum_errors == 1)
  483. {
  484. log("SDIO checksum error in reception: block ", blockidx,
  485. " calculated ", checksum, " expected ", expected);
  486. }
  487. }
  488. }
  489. }
  490. sdio_status_t __not_in_flash_func(rp2040_sdio_rx_poll)(uint32_t *bytes_complete)
  491. {
  492. // Was everything done when the previous rx_poll() finished?
  493. if (g_sdio.blocks_done >= g_sdio.total_blocks)
  494. {
  495. g_sdio.transfer_state = SDIO_IDLE;
  496. }
  497. else
  498. {
  499. // Use the idle time to calculate checksums
  500. sdio_verify_rx_checksums(4);
  501. // Check how many DMA control blocks have been consumed
  502. uint32_t dma_ctrl_block_count = (dma_hw->ch[SDIO_DMA_CHB].read_addr - (uint32_t)&g_sdio.dma_blocks);
  503. dma_ctrl_block_count /= sizeof(g_sdio.dma_blocks[0]);
  504. // Compute how many complete 512 byte SDIO blocks have been transferred
  505. // When transfer ends, dma_ctrl_block_count == g_sdio.total_blocks * 2 + 1
  506. g_sdio.blocks_done = (dma_ctrl_block_count - 1) / 2;
  507. // NOTE: When all blocks are done, rx_poll() still returns SDIO_BUSY once.
  508. // This provides a chance to start the SCSI transfer before the last checksums
  509. // are computed. Any checksum failures can be indicated in SCSI status after
  510. // the data transfer has finished.
  511. }
  512. if (bytes_complete)
  513. {
  514. *bytes_complete = g_sdio.blocks_done * SDIO_BLOCK_SIZE;
  515. }
  516. if (g_sdio.transfer_state == SDIO_IDLE)
  517. {
  518. pio_sm_set_enabled(SDIO_PIO, SDIO_DATA_SM, false);
  519. // Verify all remaining checksums.
  520. sdio_verify_rx_checksums(g_sdio.total_blocks);
  521. if (g_sdio.checksum_errors == 0)
  522. return SDIO_OK;
  523. else
  524. return SDIO_ERR_DATA_CRC;
  525. }
  526. else if ((uint32_t)(millis() - g_sdio.transfer_start_time) > 1000)
  527. {
  528. debuglog("rp2040_sdio_rx_poll() timeout, "
  529. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_DATA_SM) - (int)g_sdio.pio_data_rx_offset,
  530. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_DATA_SM),
  531. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_DATA_SM),
  532. " DMA CNT: ", dma_hw->ch[SDIO_DMA_CH].al2_transfer_count,
  533. " BD: ", g_sdio.blocks_done);
  534. rp2040_sdio_stop();
  535. return SDIO_ERR_DATA_TIMEOUT;
  536. }
  537. return SDIO_BUSY;
  538. }
  539. /*******************************************************
  540. * Data transmission to SD card
  541. *******************************************************/
  542. static void __not_in_flash_func(sdio_start_next_block_tx)()
  543. {
  544. // Initialize PIOs
  545. pio_sm_init(SDIO_PIO, SDIO_CMD_SM, g_sdio.pio_data_tx_offset, &g_sdio.pio_cfg_data_tx);
  546. // Re-set the pin direction things here
  547. pio_sm_set_pins(SDIO_PIO, SDIO_CMD_SM, 0xF);
  548. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_CLK, 1, true);
  549. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_D0, 4, true);
  550. // Configure DMA to send the data block payload (512 bytes)
  551. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CH);
  552. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_32);
  553. channel_config_set_read_increment(&dmacfg, true);
  554. channel_config_set_write_increment(&dmacfg, false);
  555. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_CMD_SM, true));
  556. channel_config_set_bswap(&dmacfg, true);
  557. channel_config_set_chain_to(&dmacfg, SDIO_DMA_CHB);
  558. dma_channel_configure(SDIO_DMA_CH, &dmacfg,
  559. &SDIO_PIO->txf[SDIO_CMD_SM], g_sdio.data_buf + g_sdio.blocks_done * SDIO_WORDS_PER_BLOCK,
  560. SDIO_WORDS_PER_BLOCK, false);
  561. // Prepare second DMA channel to send the CRC and block end marker
  562. uint64_t crc = g_sdio.next_wr_block_checksum;
  563. g_sdio.end_token_buf[0] = (uint32_t)(crc >> 32);
  564. g_sdio.end_token_buf[1] = (uint32_t)(crc >> 0);
  565. g_sdio.end_token_buf[2] = 0xFFFFFFFF;
  566. channel_config_set_bswap(&dmacfg, false);
  567. dma_channel_configure(SDIO_DMA_CHB, &dmacfg,
  568. &SDIO_PIO->txf[SDIO_CMD_SM], g_sdio.end_token_buf, 3, false);
  569. // Enable IRQ to trigger when block is done
  570. dma_hw->ints1 = 1 << SDIO_DMA_CHB;
  571. dma_set_irq1_channel_mask_enabled(1 << SDIO_DMA_CHB, 1);
  572. // Initialize register X with nibble count
  573. pio_sm_put(SDIO_PIO, SDIO_CMD_SM, 1048);
  574. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_out(pio_x, 32));
  575. // Initialize CRC receiver Y bit count
  576. pio_sm_put(SDIO_PIO, SDIO_CMD_SM, 7);
  577. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_out(pio_y, 32));
  578. // Initialize pins to output and high
  579. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_set(pio_pins, 15));
  580. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_set(pio_pindirs, 15));
  581. // Write start token and start the DMA transfer.
  582. pio_sm_put(SDIO_PIO, SDIO_CMD_SM, 0xFFFFFFF0);
  583. dma_channel_start(SDIO_DMA_CH);
  584. // Start state machine
  585. pio_set_sm_mask_enabled(SDIO_PIO, (1ul << SDIO_CMD_SM)/* | (1ul << SDIO_DATA_SM)*/, true);
  586. }
  587. static void __not_in_flash_func(sdio_compute_next_tx_checksum)()
  588. {
  589. assert (g_sdio.blocks_done < g_sdio.total_blocks && g_sdio.blocks_checksumed < g_sdio.total_blocks);
  590. int blockidx = g_sdio.blocks_checksumed++;
  591. g_sdio.next_wr_block_checksum = sdio_crc16_4bit_checksum(g_sdio.data_buf + blockidx * SDIO_WORDS_PER_BLOCK,
  592. SDIO_WORDS_PER_BLOCK);
  593. }
  594. // Start transferring data from memory to SD card
  595. sdio_status_t __not_in_flash_func(rp2040_sdio_tx_start)(const uint8_t *buffer, uint32_t num_blocks)
  596. {
  597. // Buffer must be aligned
  598. assert(((uint32_t)buffer & 3) == 0 && num_blocks <= SDIO_MAX_BLOCKS);
  599. g_sdio.transfer_state = SDIO_TX;
  600. g_sdio.transfer_start_time = millis();
  601. g_sdio.data_buf = (uint32_t*)buffer;
  602. g_sdio.blocks_done = 0;
  603. g_sdio.total_blocks = num_blocks;
  604. g_sdio.blocks_checksumed = 0;
  605. g_sdio.checksum_errors = 0;
  606. // Compute first block checksum
  607. sdio_compute_next_tx_checksum();
  608. // Start first DMA transfer and PIO
  609. sdio_start_next_block_tx();
  610. if (g_sdio.blocks_checksumed < g_sdio.total_blocks)
  611. {
  612. // Precompute second block checksum
  613. sdio_compute_next_tx_checksum();
  614. }
  615. return SDIO_OK;
  616. }
  617. sdio_status_t __not_in_flash_func(check_sdio_write_response)(uint32_t card_response)
  618. {
  619. uint8_t wr_status = card_response & 0x1F;
  620. // 5 = 0b0101 = data accepted (11100101)
  621. // 11 = 0b1011 = CRC error (11101011)
  622. // 13 = 0b1101 = Write Error (11101101)
  623. if (wr_status == 0b101)
  624. {
  625. return SDIO_OK;
  626. }
  627. else if (wr_status == 0b1011)
  628. {
  629. log("SDIO card reports write CRC error, status ", card_response);
  630. return SDIO_ERR_WRITE_CRC;
  631. }
  632. else if (wr_status == 0b1101)
  633. {
  634. log("SDIO card reports write failure, status ", card_response);
  635. return SDIO_ERR_WRITE_FAIL;
  636. }
  637. else
  638. {
  639. log("SDIO card reports unknown write status ", card_response);
  640. return SDIO_ERR_WRITE_FAIL;
  641. }
  642. }
  643. // When a block finishes, this IRQ handler starts the next one
  644. static void __not_in_flash_func(rp2040_sdio_tx_irq)()
  645. {
  646. dma_hw->ints1 = 1 << SDIO_DMA_CHB;
  647. if (g_sdio.transfer_state == SDIO_TX)
  648. {
  649. if (!dma_channel_is_busy(SDIO_DMA_CH) && !dma_channel_is_busy(SDIO_DMA_CHB))
  650. {
  651. // Main data transfer is finished now.
  652. // When card is ready, PIO will put card response on RX fifo
  653. g_sdio.transfer_state = SDIO_TX_WAIT_IDLE;
  654. if (!pio_sm_is_rx_fifo_empty(SDIO_PIO, SDIO_CMD_SM))
  655. {
  656. // Card is already idle
  657. g_sdio.card_response = pio_sm_get(SDIO_PIO, SDIO_CMD_SM);
  658. }
  659. else
  660. {
  661. // Use DMA to wait for the response
  662. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CHB);
  663. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_8);
  664. channel_config_set_read_increment(&dmacfg, false);
  665. channel_config_set_write_increment(&dmacfg, false);
  666. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_CMD_SM, false));
  667. dma_channel_configure(SDIO_DMA_CHB, &dmacfg,
  668. &g_sdio.card_response, &SDIO_PIO->rxf[SDIO_CMD_SM], 1, true);
  669. }
  670. }
  671. }
  672. if (g_sdio.transfer_state == SDIO_TX_WAIT_IDLE)
  673. {
  674. if (!dma_channel_is_busy(SDIO_DMA_CHB))
  675. {
  676. g_sdio.wr_status = check_sdio_write_response(g_sdio.card_response);
  677. if (g_sdio.wr_status != SDIO_OK)
  678. {
  679. rp2040_sdio_stop();
  680. return;
  681. }
  682. g_sdio.blocks_done++;
  683. if (g_sdio.blocks_done < g_sdio.total_blocks)
  684. {
  685. sdio_start_next_block_tx();
  686. g_sdio.transfer_state = SDIO_TX;
  687. if (g_sdio.blocks_checksumed < g_sdio.total_blocks)
  688. {
  689. // Precompute the CRC for next block so that it is ready when
  690. // we want to send it.
  691. sdio_compute_next_tx_checksum();
  692. }
  693. }
  694. else
  695. {
  696. rp2040_sdio_stop();
  697. }
  698. }
  699. }
  700. }
  701. // Check if transmission is complete
  702. sdio_status_t __not_in_flash_func(rp2040_sdio_tx_poll)(uint32_t *bytes_complete)
  703. {
  704. if (scb_hw->icsr & (0x1FFUL))
  705. {
  706. // Verify that IRQ handler gets called even if we are in hardfault handler
  707. rp2040_sdio_tx_irq();
  708. }
  709. if (bytes_complete)
  710. {
  711. *bytes_complete = g_sdio.blocks_done * SDIO_BLOCK_SIZE;
  712. }
  713. if (g_sdio.transfer_state == SDIO_IDLE)
  714. {
  715. rp2040_sdio_stop();
  716. return g_sdio.wr_status;
  717. }
  718. else if ((uint32_t)(millis() - g_sdio.transfer_start_time) > 1000)
  719. {
  720. debuglog("rp2040_sdio_tx_poll() timeout, "
  721. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_CMD_SM) - (int)g_sdio.pio_data_tx_offset,
  722. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_CMD_SM),
  723. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_CMD_SM),
  724. " DMA CNT: ", dma_hw->ch[SDIO_DMA_CH].al2_transfer_count);
  725. rp2040_sdio_stop();
  726. return SDIO_ERR_DATA_TIMEOUT;
  727. }
  728. return SDIO_BUSY;
  729. }
  730. // Force everything to idle state
  731. sdio_status_t __not_in_flash_func(rp2040_sdio_stop)()
  732. {
  733. dma_channel_abort(SDIO_DMA_CH);
  734. dma_channel_abort(SDIO_DMA_CHB);
  735. dma_set_irq1_channel_mask_enabled(1 << SDIO_DMA_CHB, 0);
  736. pio_set_sm_mask_enabled(SDIO_PIO, (1ul << SDIO_CMD_SM) | (1ul << SDIO_DATA_SM), false);
  737. g_sdio.transfer_state = SDIO_IDLE;
  738. return SDIO_OK;
  739. }
  740. void __not_in_flash_func(rp2040_sdio_init)(int clock_divider)
  741. {
  742. // Mark resources as being in use, unless it has been done already.
  743. static bool resources_claimed = false;
  744. if (!resources_claimed)
  745. {
  746. pio_sm_claim(SDIO_PIO, SDIO_CMD_SM);
  747. pio_sm_claim(SDIO_PIO, SDIO_DATA_SM);
  748. dma_channel_claim(SDIO_DMA_CH);
  749. dma_channel_claim(SDIO_DMA_CHB);
  750. resources_claimed = true;
  751. }
  752. memset(&g_sdio, 0, sizeof(g_sdio));
  753. dma_channel_abort(SDIO_DMA_CH);
  754. dma_channel_abort(SDIO_DMA_CHB);
  755. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false);
  756. pio_sm_set_enabled(SDIO_PIO, SDIO_DATA_SM, false);
  757. // Load PIO programs
  758. pio_clear_instruction_memory(SDIO_PIO);
  759. // Set pull resistors for all SD data lines
  760. gpio_set_pulls(SDIO_CLK, true, false);
  761. gpio_set_pulls(SDIO_CMD, true, false);
  762. gpio_set_pulls(SDIO_D0, true, false);
  763. gpio_set_pulls(SDIO_D1, true, false);
  764. gpio_set_pulls(SDIO_D2, true, false);
  765. gpio_set_pulls(SDIO_D3, true, false);
  766. // Command state machine
  767. g_sdio.pio_cmd_rsp_clk_offset = pio_add_program(SDIO_PIO, &cmd_rsp_program);
  768. g_sdio.pio_cfg_cmd_rsp = pio_cmd_rsp_program_config(g_sdio.pio_cmd_rsp_clk_offset, SDIO_CMD, SDIO_CLK, clock_divider, 0);
  769. pio_sm_init(SDIO_PIO, SDIO_CMD_SM, g_sdio.pio_cmd_rsp_clk_offset, &g_sdio.pio_cfg_cmd_rsp);
  770. pio_sm_set_pins(SDIO_PIO, SDIO_CMD_SM, 1);
  771. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_CLK, 1, true);
  772. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_CMD, 1, true);
  773. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_D0, 4, false);
  774. // Data reception program
  775. g_sdio.pio_data_rx_offset = pio_add_program(SDIO_PIO, &rd_data_w_clock_program);
  776. g_sdio.pio_cfg_data_rx = pio_rd_data_w_clock_program_config(g_sdio.pio_data_rx_offset, SDIO_D0, SDIO_CLK, clock_divider);
  777. // Data transmission program
  778. g_sdio.pio_data_tx_offset = pio_add_program(SDIO_PIO, &sdio_tx_w_clock_program);
  779. g_sdio.pio_cfg_data_tx = pio_sdio_tx_w_clock_program_config(g_sdio.pio_data_tx_offset, SDIO_D0, SDIO_CLK, clock_divider);
  780. // Disable SDIO pins input synchronizer.
  781. // This reduces input delay.
  782. // Because the CLK is driven synchronously to CPU clock,
  783. // there should be no metastability problems.
  784. SDIO_PIO->input_sync_bypass |= (1 << SDIO_CLK) | (1 << SDIO_CMD)
  785. | (1 << SDIO_D0) | (1 << SDIO_D1) | (1 << SDIO_D2) | (1 << SDIO_D3);
  786. // Redirect GPIOs to PIO
  787. gpio_set_function(SDIO_CMD, GPIO_FUNC_PIO1);
  788. gpio_set_function(SDIO_CLK, GPIO_FUNC_PIO1);
  789. gpio_set_function(SDIO_D0, GPIO_FUNC_PIO1);
  790. gpio_set_function(SDIO_D1, GPIO_FUNC_PIO1);
  791. gpio_set_function(SDIO_D2, GPIO_FUNC_PIO1);
  792. gpio_set_function(SDIO_D3, GPIO_FUNC_PIO1);
  793. // Set up IRQ handler when DMA completes.
  794. irq_set_exclusive_handler(DMA_IRQ_1, rp2040_sdio_tx_irq);
  795. irq_set_enabled(DMA_IRQ_1, true);
  796. #if 0
  797. #ifndef ENABLE_AUDIO_OUTPUT
  798. irq_set_exclusive_handler(DMA_IRQ_1, rp2040_sdio_tx_irq);
  799. #else
  800. // seem to hit assertion in _exclusive_handler call due to DMA_IRQ_0 being shared?
  801. // slightly less efficient to do it this way, so investigate further at some point
  802. irq_add_shared_handler(DMA_IRQ_1, rp2040_sdio_tx_irq, 0xFF);
  803. #endif
  804. irq_set_enabled(DMA_IRQ_1, true);
  805. #endif
  806. }
  807. void __not_in_flash_func(rp2040_sdio_update_delays)(pio_program program, uint32_t offset, uint16_t additional_delay) {
  808. //log("Offset:", offset);
  809. uint16_t instr_to_rewrite;
  810. uint16_t existing_delay;
  811. for (int i = 0; i < program.length; i++) {
  812. instr_to_rewrite = program.instructions[i];
  813. //log("Old Instr:", i, ":", (uint32_t)instr_to_rewrite);
  814. if (instr_to_rewrite & PIO_INSTR_MASK_GET_DELAY) { // If there's a delay, increment it. Otherwise, leave it alone.
  815. existing_delay = (instr_to_rewrite & PIO_INSTR_MASK_GET_DELAY) >> 8;
  816. existing_delay += additional_delay;
  817. instr_to_rewrite = (instr_to_rewrite & PIO_INSTR_MASK_REMOVE_DELAY) | (existing_delay << 8);
  818. // Canonicalize JMP addresses
  819. if ((instr_to_rewrite & PIO_INSTR_JMP_MASK) == 0) { // Highest three bits are zero on a JMP
  820. uint32_t jmp_address = instr_to_rewrite & PIO_INSTR_JMP_ADDR;
  821. jmp_address += offset;
  822. instr_to_rewrite = (instr_to_rewrite & (~ PIO_INSTR_JMP_ADDR)) | jmp_address;
  823. }
  824. //log("New Instr:", i, ":", (uint32_t)instr_to_rewrite);
  825. SDIO_PIO->instr_mem[offset + i] = instr_to_rewrite;
  826. }
  827. }
  828. }
  829. void __not_in_flash_func(rp2040_sdio_delay_increment)(uint16_t additional_delay) {
  830. /*
  831. Rewrite in-place every SDIO instruction for all the SDIO programs.
  832. These additional delay cycles effectively decrease the SDIO clock rate, which can be helpful in electrically noisy environments.
  833. */
  834. rp2040_sdio_update_delays(cmd_rsp_program, g_sdio.pio_cmd_rsp_clk_offset, additional_delay);
  835. rp2040_sdio_update_delays(rd_data_w_clock_program, g_sdio.pio_data_rx_offset, additional_delay);
  836. rp2040_sdio_update_delays(sdio_tx_w_clock_program, g_sdio.pio_data_tx_offset, additional_delay);
  837. }