rp2040_sdio.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. // Implementation of SDIO communication for RP2040
  2. //
  3. // The RP2040 official work-in-progress code at
  4. // https://github.com/raspberrypi/pico-extras/tree/master/src/rp2_common/pico_sd_card
  5. // may be useful reference, but this is independent implementation.
  6. //
  7. // For official SDIO specifications, refer to:
  8. // https://www.sdcard.org/downloads/pls/
  9. // "SDIO Physical Layer Simplified Specification Version 8.00"
  10. #include "rp2040_sdio.h"
  11. #include "rp2040_sdio.pio.h"
  12. #include <hardware/pio.h>
  13. #include <hardware/dma.h>
  14. #include <hardware/gpio.h>
  15. #include <ZuluSCSI_platform.h>
  16. #include <ZuluSCSI_log.h>
  17. #define SDIO_PIO pio1
  18. #define SDIO_CMD_SM 0
  19. #define SDIO_DATA_SM 1
  20. #define SDIO_DMA_CH 1
  21. // Maximum number of 512 byte blocks to transfer in one request
  22. #define SDIO_MAX_BLOCKS 256
  23. enum sdio_transfer_state_t { SDIO_IDLE, SDIO_RX, SDIO_TX };
  24. static struct {
  25. uint32_t pio_cmd_clk_offset;
  26. uint32_t pio_data_rx_offset;
  27. pio_sm_config pio_cfg_data_rx;
  28. uint32_t pio_data_tx_offset;
  29. pio_sm_config pio_cfg_data_tx;
  30. sdio_transfer_state_t transfer_state;
  31. bool inside_irq_handler; // True if we are inside crash handler code
  32. uint32_t transfer_start_time;
  33. uint32_t *data_buf;
  34. uint32_t blocks_done; // Number of blocks transferred so far
  35. uint32_t total_blocks; // Total number of blocks to transfer
  36. uint32_t blocks_checksumed; // Number of blocks that have had CRC calculated
  37. uint32_t checksum_errors; // Number of checksum errors detected
  38. uint64_t block_checksums[SDIO_MAX_BLOCKS];
  39. } g_sdio;
  40. /*******************************************************
  41. * Checksum algorithms
  42. *******************************************************/
  43. // Table lookup for calculating CRC-7 checksum that is used in SDIO command packets.
  44. // Usage:
  45. // uint8_t crc = 0;
  46. // crc = crc7_table[crc ^ byte];
  47. // .. repeat for every byte ..
  48. static const uint8_t crc7_table[256] = {
  49. 0x00, 0x12, 0x24, 0x36, 0x48, 0x5a, 0x6c, 0x7e, 0x90, 0x82, 0xb4, 0xa6, 0xd8, 0xca, 0xfc, 0xee,
  50. 0x32, 0x20, 0x16, 0x04, 0x7a, 0x68, 0x5e, 0x4c, 0xa2, 0xb0, 0x86, 0x94, 0xea, 0xf8, 0xce, 0xdc,
  51. 0x64, 0x76, 0x40, 0x52, 0x2c, 0x3e, 0x08, 0x1a, 0xf4, 0xe6, 0xd0, 0xc2, 0xbc, 0xae, 0x98, 0x8a,
  52. 0x56, 0x44, 0x72, 0x60, 0x1e, 0x0c, 0x3a, 0x28, 0xc6, 0xd4, 0xe2, 0xf0, 0x8e, 0x9c, 0xaa, 0xb8,
  53. 0xc8, 0xda, 0xec, 0xfe, 0x80, 0x92, 0xa4, 0xb6, 0x58, 0x4a, 0x7c, 0x6e, 0x10, 0x02, 0x34, 0x26,
  54. 0xfa, 0xe8, 0xde, 0xcc, 0xb2, 0xa0, 0x96, 0x84, 0x6a, 0x78, 0x4e, 0x5c, 0x22, 0x30, 0x06, 0x14,
  55. 0xac, 0xbe, 0x88, 0x9a, 0xe4, 0xf6, 0xc0, 0xd2, 0x3c, 0x2e, 0x18, 0x0a, 0x74, 0x66, 0x50, 0x42,
  56. 0x9e, 0x8c, 0xba, 0xa8, 0xd6, 0xc4, 0xf2, 0xe0, 0x0e, 0x1c, 0x2a, 0x38, 0x46, 0x54, 0x62, 0x70,
  57. 0x82, 0x90, 0xa6, 0xb4, 0xca, 0xd8, 0xee, 0xfc, 0x12, 0x00, 0x36, 0x24, 0x5a, 0x48, 0x7e, 0x6c,
  58. 0xb0, 0xa2, 0x94, 0x86, 0xf8, 0xea, 0xdc, 0xce, 0x20, 0x32, 0x04, 0x16, 0x68, 0x7a, 0x4c, 0x5e,
  59. 0xe6, 0xf4, 0xc2, 0xd0, 0xae, 0xbc, 0x8a, 0x98, 0x76, 0x64, 0x52, 0x40, 0x3e, 0x2c, 0x1a, 0x08,
  60. 0xd4, 0xc6, 0xf0, 0xe2, 0x9c, 0x8e, 0xb8, 0xaa, 0x44, 0x56, 0x60, 0x72, 0x0c, 0x1e, 0x28, 0x3a,
  61. 0x4a, 0x58, 0x6e, 0x7c, 0x02, 0x10, 0x26, 0x34, 0xda, 0xc8, 0xfe, 0xec, 0x92, 0x80, 0xb6, 0xa4,
  62. 0x78, 0x6a, 0x5c, 0x4e, 0x30, 0x22, 0x14, 0x06, 0xe8, 0xfa, 0xcc, 0xde, 0xa0, 0xb2, 0x84, 0x96,
  63. 0x2e, 0x3c, 0x0a, 0x18, 0x66, 0x74, 0x42, 0x50, 0xbe, 0xac, 0x9a, 0x88, 0xf6, 0xe4, 0xd2, 0xc0,
  64. 0x1c, 0x0e, 0x38, 0x2a, 0x54, 0x46, 0x70, 0x62, 0x8c, 0x9e, 0xa8, 0xba, 0xc4, 0xd6, 0xe0, 0xf2
  65. };
  66. // Calculate the CRC16 checksum for parallel 4 bit lines separately.
  67. // When the SDIO bus operates in 4-bit mode, the CRC16 algorithm
  68. // is applied to each line separately and generates total of
  69. // 4 x 16 = 64 bits of checksum.
  70. uint64_t sdio_crc16_4bit_checksum(uint32_t *data, uint32_t num_words)
  71. {
  72. uint64_t crc = 0;
  73. uint32_t *end = data + num_words;
  74. while (data < end)
  75. {
  76. // Each 32-bit word contains 8 bits per line.
  77. // Reverse the bytes because SDIO protocol is big-endian.
  78. uint32_t data_in = __builtin_bswap32(*data++);
  79. // Shift out 8 bits for each line
  80. uint32_t data_out = crc >> 32;
  81. crc <<= 32;
  82. // XOR outgoing data to itself with 4 bit delay
  83. data_out ^= (data_out >> 16);
  84. // XOR incoming data to outgoing data with 4 bit delay
  85. data_out ^= (data_in >> 16);
  86. // XOR outgoing and incoming data to accumulator at each tap
  87. uint64_t xorred = data_out ^ data_in;
  88. crc ^= xorred;
  89. crc ^= xorred << (5 * 4);
  90. crc ^= xorred << (12 * 4);
  91. }
  92. return crc;
  93. }
  94. /*******************************************************
  95. * Basic SDIO command execution
  96. *******************************************************/
  97. static void sdio_send_command(uint8_t command, uint32_t arg, uint8_t response_bits)
  98. {
  99. // azdbg("SDIO Command: ", (int)command, " arg ", arg);
  100. // Format the arguments in the way expected by the PIO code.
  101. uint32_t word0 =
  102. (47 << 24) | // Number of bits in command minus one
  103. ( 1 << 22) | // Transfer direction from host to card
  104. (command << 16) | // Command byte
  105. (((arg >> 24) & 0xFF) << 8) | // MSB byte of argument
  106. (((arg >> 16) & 0xFF) << 0);
  107. uint32_t word1 =
  108. (((arg >> 8) & 0xFF) << 24) |
  109. (((arg >> 0) & 0xFF) << 16) | // LSB byte of argument
  110. ( 1 << 8); // End bit
  111. // Set number of bits in response minus one, or leave at 0 if no response expected
  112. if (response_bits)
  113. {
  114. word1 |= ((response_bits - 1) << 0);
  115. }
  116. // Calculate checksum in the order that the bytes will be transmitted (big-endian)
  117. uint8_t crc = 0;
  118. crc = crc7_table[crc ^ ((word0 >> 16) & 0xFF)];
  119. crc = crc7_table[crc ^ ((word0 >> 8) & 0xFF)];
  120. crc = crc7_table[crc ^ ((word0 >> 0) & 0xFF)];
  121. crc = crc7_table[crc ^ ((word1 >> 24) & 0xFF)];
  122. crc = crc7_table[crc ^ ((word1 >> 16) & 0xFF)];
  123. word1 |= crc << 8;
  124. // Transmit command
  125. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  126. pio_sm_put(SDIO_PIO, SDIO_CMD_SM, word0);
  127. pio_sm_put(SDIO_PIO, SDIO_CMD_SM, word1);
  128. }
  129. sdio_status_t rp2040_sdio_command_R1(uint8_t command, uint32_t arg, uint32_t *response)
  130. {
  131. sdio_send_command(command, arg, response ? 48 : 0);
  132. // Wait for response
  133. uint32_t start = millis();
  134. uint32_t wait_words = response ? 2 : 1;
  135. while (pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_CMD_SM) < wait_words)
  136. {
  137. if ((uint32_t)(millis() - start) > 2)
  138. {
  139. azdbg("Timeout waiting for response in rp2040_sdio_command_R1(", (int)command, "), ",
  140. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_CMD_SM) - (int)g_sdio.pio_cmd_clk_offset,
  141. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_CMD_SM),
  142. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_CMD_SM));
  143. // Reset the state machine program
  144. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  145. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_jmp(g_sdio.pio_cmd_clk_offset));
  146. return SDIO_ERR_RESPONSE_TIMEOUT;
  147. }
  148. }
  149. if (response)
  150. {
  151. // Read out response packet
  152. uint32_t resp0 = pio_sm_get(SDIO_PIO, SDIO_CMD_SM);
  153. uint32_t resp1 = pio_sm_get(SDIO_PIO, SDIO_CMD_SM);
  154. // azdbg("SDIO R1 response: ", resp0, " ", resp1);
  155. // Calculate response checksum
  156. uint8_t crc = 0;
  157. crc = crc7_table[crc ^ ((resp0 >> 24) & 0xFF)];
  158. crc = crc7_table[crc ^ ((resp0 >> 16) & 0xFF)];
  159. crc = crc7_table[crc ^ ((resp0 >> 8) & 0xFF)];
  160. crc = crc7_table[crc ^ ((resp0 >> 0) & 0xFF)];
  161. crc = crc7_table[crc ^ ((resp1 >> 8) & 0xFF)];
  162. uint8_t actual_crc = ((resp1 >> 0) & 0xFE);
  163. if (crc != actual_crc)
  164. {
  165. azdbg("rp2040_sdio_command_R1(", (int)command, "): CRC error, calculated ", crc, " packet has ", actual_crc);
  166. return SDIO_ERR_RESPONSE_CRC;
  167. }
  168. uint8_t response_cmd = ((resp0 >> 24) & 0xFF);
  169. if (response_cmd != command && command != 41)
  170. {
  171. azdbg("rp2040_sdio_command_R1(", (int)command, "): received reply for ", (int)response_cmd);
  172. return SDIO_ERR_RESPONSE_CODE;
  173. }
  174. *response = ((resp0 & 0xFFFFFF) << 8) | ((resp1 >> 8) & 0xFF);
  175. }
  176. else
  177. {
  178. // Read out dummy marker
  179. pio_sm_get(SDIO_PIO, SDIO_CMD_SM);
  180. }
  181. return SDIO_OK;
  182. }
  183. sdio_status_t rp2040_sdio_command_R2(uint8_t command, uint32_t arg, uint8_t response[16])
  184. {
  185. // The response is too long to fit in the PIO FIFO, so use DMA to receive it.
  186. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  187. uint32_t response_buf[5];
  188. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CH);
  189. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_32);
  190. channel_config_set_read_increment(&dmacfg, false);
  191. channel_config_set_write_increment(&dmacfg, true);
  192. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_CMD_SM, false));
  193. dma_channel_configure(SDIO_DMA_CH, &dmacfg, &response_buf, &SDIO_PIO->rxf[SDIO_CMD_SM], 5, true);
  194. sdio_send_command(command, arg, 136);
  195. uint32_t start = millis();
  196. while (dma_channel_is_busy(SDIO_DMA_CH))
  197. {
  198. if ((uint32_t)(millis() - start) > 2)
  199. {
  200. azdbg("Timeout waiting for response in rp2040_sdio_command_R2(", (int)command, "), ",
  201. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_CMD_SM) - (int)g_sdio.pio_cmd_clk_offset,
  202. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_CMD_SM),
  203. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_CMD_SM));
  204. // Reset the state machine program
  205. dma_channel_abort(SDIO_DMA_CH);
  206. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  207. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_jmp(g_sdio.pio_cmd_clk_offset));
  208. return SDIO_ERR_RESPONSE_TIMEOUT;
  209. }
  210. }
  211. dma_channel_abort(SDIO_DMA_CH);
  212. // Copy the response payload to output buffer
  213. response[0] = ((response_buf[0] >> 16) & 0xFF);
  214. response[1] = ((response_buf[0] >> 8) & 0xFF);
  215. response[2] = ((response_buf[0] >> 0) & 0xFF);
  216. response[3] = ((response_buf[1] >> 24) & 0xFF);
  217. response[4] = ((response_buf[1] >> 16) & 0xFF);
  218. response[5] = ((response_buf[1] >> 8) & 0xFF);
  219. response[6] = ((response_buf[1] >> 0) & 0xFF);
  220. response[7] = ((response_buf[2] >> 24) & 0xFF);
  221. response[8] = ((response_buf[2] >> 16) & 0xFF);
  222. response[9] = ((response_buf[2] >> 8) & 0xFF);
  223. response[10] = ((response_buf[2] >> 0) & 0xFF);
  224. response[11] = ((response_buf[3] >> 24) & 0xFF);
  225. response[12] = ((response_buf[3] >> 16) & 0xFF);
  226. response[13] = ((response_buf[3] >> 8) & 0xFF);
  227. response[14] = ((response_buf[3] >> 0) & 0xFF);
  228. response[15] = ((response_buf[4] >> 0) & 0xFF);
  229. // Calculate checksum of the payload
  230. uint8_t crc = 0;
  231. for (int i = 0; i < 15; i++)
  232. {
  233. crc = crc7_table[crc ^ response[i]];
  234. }
  235. uint8_t actual_crc = response[15] & 0xFE;
  236. if (crc != actual_crc)
  237. {
  238. azdbg("rp2040_sdio_command_R2(", (int)command, "): CRC error, calculated ", crc, " packet has ", actual_crc);
  239. return SDIO_ERR_RESPONSE_CRC;
  240. }
  241. uint8_t response_cmd = ((response_buf[0] >> 24) & 0xFF);
  242. if (response_cmd != 0x3F)
  243. {
  244. azdbg("rp2040_sdio_command_R2(", (int)command, "): Expected reply code 0x3F");
  245. return SDIO_ERR_RESPONSE_CODE;
  246. }
  247. return SDIO_OK;
  248. }
  249. sdio_status_t rp2040_sdio_command_R3(uint8_t command, uint32_t arg, uint32_t *response)
  250. {
  251. sdio_send_command(command, arg, 48);
  252. // Wait for response
  253. uint32_t start = millis();
  254. while (pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_CMD_SM) < 2)
  255. {
  256. if ((uint32_t)(millis() - start) > 2)
  257. {
  258. azdbg("Timeout waiting for response in rp2040_sdio_command_R3(", (int)command, "), ",
  259. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_CMD_SM) - (int)g_sdio.pio_cmd_clk_offset,
  260. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_CMD_SM),
  261. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_CMD_SM));
  262. // Reset the state machine program
  263. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  264. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_jmp(g_sdio.pio_cmd_clk_offset));
  265. return SDIO_ERR_RESPONSE_TIMEOUT;
  266. }
  267. }
  268. // Read out response packet
  269. uint32_t resp0 = pio_sm_get(SDIO_PIO, SDIO_CMD_SM);
  270. uint32_t resp1 = pio_sm_get(SDIO_PIO, SDIO_CMD_SM);
  271. *response = ((resp0 & 0xFFFFFF) << 8) | ((resp1 >> 8) & 0xFF);
  272. // azdbg("SDIO R3 response: ", resp0, " ", resp1);
  273. return SDIO_OK;
  274. }
  275. /*******************************************************
  276. * Data reception from SD card
  277. *******************************************************/
  278. static void sdio_start_next_block_rx()
  279. {
  280. assert (g_sdio.blocks_done < g_sdio.total_blocks);
  281. // Disable and reset PIO from previous block
  282. pio_sm_set_enabled(SDIO_PIO, SDIO_DATA_SM, false);
  283. pio_sm_restart(SDIO_PIO, SDIO_DATA_SM);
  284. pio_sm_exec(SDIO_PIO, SDIO_DATA_SM, pio_encode_jmp(g_sdio.pio_data_rx_offset));
  285. // Start new DMA transfer
  286. dma_channel_transfer_to_buffer_now(SDIO_DMA_CH, g_sdio.data_buf + 128 * g_sdio.blocks_done, 128);
  287. // Enable PIO
  288. pio_sm_set_enabled(SDIO_PIO, SDIO_DATA_SM, true);
  289. }
  290. // Check checksums for received blocks
  291. static void sdio_verify_rx_checksums(uint32_t maxcount)
  292. {
  293. while (g_sdio.blocks_checksumed < g_sdio.blocks_done && maxcount-- > 0)
  294. {
  295. int blockidx = g_sdio.blocks_checksumed++;
  296. uint64_t checksum = sdio_crc16_4bit_checksum(g_sdio.data_buf + blockidx * 128, 128);
  297. uint64_t expected = g_sdio.block_checksums[blockidx];
  298. if (checksum != expected)
  299. {
  300. g_sdio.checksum_errors++;
  301. if (g_sdio.checksum_errors == 1)
  302. {
  303. azlog("SDIO checksum error in reception: calculated ", checksum, " expected ", expected);
  304. }
  305. }
  306. }
  307. }
  308. static void rp2040_sdio_rx_irq()
  309. {
  310. dma_hw->ints1 = 1 << SDIO_DMA_CH;
  311. // Wait for CRC to be received
  312. int maxwait = 1000;
  313. while (pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_DATA_SM) < 2)
  314. {
  315. if (maxwait-- < 0)
  316. {
  317. azlog("rp2040_sdio_rx_irq(): timeout waiting for CRC reception");
  318. break;
  319. }
  320. }
  321. uint32_t crc0 = pio_sm_get(SDIO_PIO, SDIO_DATA_SM);
  322. uint32_t crc1 = pio_sm_get(SDIO_PIO, SDIO_DATA_SM);
  323. g_sdio.block_checksums[g_sdio.blocks_done] = ((uint64_t)crc0 << 32) | crc1;
  324. g_sdio.blocks_done++;
  325. if (g_sdio.blocks_done < g_sdio.total_blocks)
  326. {
  327. sdio_start_next_block_rx();
  328. }
  329. else
  330. {
  331. g_sdio.transfer_state = SDIO_IDLE;
  332. }
  333. }
  334. sdio_status_t rp2040_sdio_rx_start(uint8_t *buffer, uint32_t num_blocks)
  335. {
  336. // Buffer must be aligned
  337. assert(((uint32_t)buffer & 3) == 0 && num_blocks <= SDIO_MAX_BLOCKS);
  338. g_sdio.transfer_state = SDIO_RX;
  339. g_sdio.transfer_start_time = millis();
  340. g_sdio.data_buf = (uint32_t*)buffer;
  341. g_sdio.blocks_done = 0;
  342. g_sdio.total_blocks = num_blocks;
  343. g_sdio.blocks_checksumed = 0;
  344. g_sdio.checksum_errors = 0;
  345. // Check if we are inside interrupt handler.
  346. // This happens when saving crash log from hardfault.
  347. // If true, must use polling mode instead of interrupts.
  348. g_sdio.inside_irq_handler = (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk);
  349. pio_sm_init(SDIO_PIO, SDIO_DATA_SM, g_sdio.pio_data_rx_offset, &g_sdio.pio_cfg_data_rx);
  350. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_DATA_SM, SDIO_D0, 4, false);
  351. // Configure DMA to receive the data block payload (512 bytes).
  352. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CH);
  353. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_32);
  354. channel_config_set_read_increment(&dmacfg, false);
  355. channel_config_set_write_increment(&dmacfg, true);
  356. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_DATA_SM, false));
  357. channel_config_set_bswap(&dmacfg, true);
  358. dma_channel_configure(SDIO_DMA_CH, &dmacfg, 0, &SDIO_PIO->rxf[SDIO_DATA_SM], 0, false);
  359. sdio_start_next_block_rx();
  360. return SDIO_OK;
  361. }
  362. sdio_status_t rp2040_sdio_rx_poll(uint32_t *bytes_complete)
  363. {
  364. if (g_sdio.inside_irq_handler && (dma_hw->ints0 & (1 << SDIO_DMA_CH)))
  365. {
  366. // Make sure DMA interrupt handler gets called even from inside hardfault handler.
  367. rp2040_sdio_rx_irq();
  368. }
  369. if (bytes_complete)
  370. {
  371. *bytes_complete = g_sdio.blocks_done * 512;
  372. }
  373. if (g_sdio.transfer_state == SDIO_IDLE)
  374. {
  375. sdio_verify_rx_checksums(g_sdio.total_blocks);
  376. if (g_sdio.checksum_errors == 0)
  377. return SDIO_OK;
  378. else
  379. return SDIO_ERR_DATA_CRC;
  380. }
  381. else if ((uint32_t)(millis() - g_sdio.transfer_start_time) > 1000)
  382. {
  383. azdbg("rp2040_sdio_rx_poll() timeout, "
  384. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_DATA_SM) - (int)g_sdio.pio_data_rx_offset,
  385. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_DATA_SM),
  386. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_DATA_SM),
  387. " DMA CNT: ", dma_hw->ch[SDIO_DMA_CH].al2_transfer_count);
  388. rp2040_sdio_stop();
  389. return SDIO_ERR_DATA_TIMEOUT;
  390. }
  391. else
  392. {
  393. // Use the idle time to calculate checksums
  394. sdio_verify_rx_checksums(1);
  395. }
  396. return SDIO_BUSY;
  397. }
  398. /*******************************************************
  399. * Data transmission to SD card
  400. *******************************************************/
  401. static void sdio_start_next_block_tx()
  402. {
  403. assert (g_sdio.blocks_done < g_sdio.total_blocks && g_sdio.blocks_checksumed > g_sdio.blocks_done);
  404. // Start new DMA transfer
  405. dma_channel_transfer_from_buffer_now(SDIO_DMA_CH, g_sdio.data_buf + 128 * g_sdio.blocks_done, 128);
  406. }
  407. static void sdio_compute_tx_checksums(uint32_t maxcount)
  408. {
  409. while (g_sdio.blocks_checksumed < g_sdio.blocks_done && maxcount-- > 0)
  410. {
  411. int blockidx = g_sdio.blocks_checksumed++;
  412. g_sdio.block_checksums[blockidx] = sdio_crc16_4bit_checksum(g_sdio.data_buf + blockidx * 128, 128);
  413. }
  414. }
  415. static void rp2040_sdio_tx_irq()
  416. {
  417. // Wait for there to be enough space for checksum
  418. int maxwait = 1000;
  419. while (pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_DATA_SM) < 5)
  420. {
  421. if (maxwait-- < 0)
  422. {
  423. azlog("rp2040_sdio_tx_irq(): timeout waiting for space in TX buffer for CRC");
  424. break;
  425. }
  426. }
  427. // Send the checksum and block end marker
  428. uint64_t crc = g_sdio.block_checksums[g_sdio.blocks_done];
  429. pio_sm_put(SDIO_PIO, SDIO_DATA_SM, (uint32_t)(crc >> 32));
  430. pio_sm_put(SDIO_PIO, SDIO_DATA_SM, (uint32_t)(crc >> 0));
  431. pio_sm_put(SDIO_PIO, SDIO_DATA_SM, 0xFFFFFFFF);
  432. g_sdio.blocks_done++;
  433. if (g_sdio.blocks_done < g_sdio.total_blocks)
  434. {
  435. sdio_start_next_block_tx();
  436. }
  437. else
  438. {
  439. g_sdio.transfer_state = SDIO_IDLE;
  440. }
  441. }
  442. // Start transferring data from memory to SD card
  443. sdio_status_t rp2040_sdio_tx_start(const uint8_t *buffer, uint32_t num_blocks)
  444. {
  445. // Buffer must be aligned
  446. assert(((uint32_t)buffer & 3) == 0 && num_blocks <= SDIO_MAX_BLOCKS);
  447. g_sdio.transfer_state = SDIO_TX;
  448. g_sdio.transfer_start_time = millis();
  449. g_sdio.data_buf = (uint32_t*)buffer;
  450. g_sdio.blocks_done = 0;
  451. g_sdio.total_blocks = num_blocks;
  452. g_sdio.blocks_checksumed = 0;
  453. g_sdio.checksum_errors = 0;
  454. // Check if we are inside interrupt handler.
  455. // This happens when saving crash log from hardfault.
  456. // If true, must use polling mode instead of interrupts.
  457. g_sdio.inside_irq_handler = (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk);
  458. // Compute first block checksum
  459. sdio_compute_tx_checksums(1);
  460. // Initialize PIO
  461. pio_sm_init(SDIO_PIO, SDIO_DATA_SM, g_sdio.pio_data_tx_offset, &g_sdio.pio_cfg_data_tx);
  462. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_DATA_SM, SDIO_D0, 4, true);
  463. // Configure DMA to send the data block payload (512 bytes)
  464. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CH);
  465. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_32);
  466. channel_config_set_read_increment(&dmacfg, true);
  467. channel_config_set_write_increment(&dmacfg, false);
  468. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_DATA_SM, false));
  469. channel_config_set_bswap(&dmacfg, true);
  470. dma_channel_configure(SDIO_DMA_CH, &dmacfg, 0, &SDIO_PIO->txf[SDIO_DATA_SM], 0, false);
  471. // Start first DMA transfer and PIO
  472. sdio_start_next_block_tx();
  473. pio_sm_set_enabled(SDIO_PIO, SDIO_DATA_SM, true);
  474. // Compute rest of the block checksums so that they are ready when needed
  475. sdio_compute_tx_checksums(g_sdio.total_blocks);
  476. return SDIO_OK;
  477. }
  478. // Check if transmission is complete
  479. sdio_status_t rp2040_sdio_tx_poll(uint32_t *bytes_complete)
  480. {
  481. if (g_sdio.inside_irq_handler && (dma_hw->ints0 & (1 << SDIO_DMA_CH)))
  482. {
  483. // Make sure DMA interrupt handler gets called even from inside hardfault handler.
  484. rp2040_sdio_tx_irq();
  485. }
  486. if (bytes_complete)
  487. {
  488. *bytes_complete = g_sdio.blocks_done * 512;
  489. }
  490. if (g_sdio.transfer_state == SDIO_IDLE)
  491. {
  492. pio_sm_set_enabled(SDIO_PIO, SDIO_DATA_SM, false);
  493. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_DATA_SM, SDIO_D0, 4, false);
  494. return SDIO_OK;
  495. }
  496. else if ((uint32_t)(millis() - g_sdio.transfer_start_time) > 1000)
  497. {
  498. azdbg("rp2040_sdio_tx_poll() timeout, "
  499. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_DATA_SM) - (int)g_sdio.pio_data_rx_offset,
  500. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_DATA_SM),
  501. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_DATA_SM),
  502. " DMA CNT: ", dma_hw->ch[SDIO_DMA_CH].al2_transfer_count);
  503. rp2040_sdio_stop();
  504. return SDIO_ERR_DATA_TIMEOUT;
  505. }
  506. return SDIO_BUSY;
  507. }
  508. // Force everything to idle state
  509. sdio_status_t rp2040_sdio_stop()
  510. {
  511. dma_channel_abort(SDIO_DMA_CH);
  512. pio_sm_set_enabled(SDIO_PIO, SDIO_DATA_SM, false);
  513. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_DATA_SM, SDIO_D0, 4, false);
  514. g_sdio.transfer_state = SDIO_IDLE;
  515. return SDIO_OK;
  516. }
  517. void rp2040_sdio_dma_irq()
  518. {
  519. dma_hw->ints1 = 1 << SDIO_DMA_CH;
  520. if (g_sdio.transfer_state == SDIO_TX)
  521. rp2040_sdio_tx_irq();
  522. else if (g_sdio.transfer_state == SDIO_RX)
  523. rp2040_sdio_rx_irq();
  524. }
  525. void rp2040_sdio_init()
  526. {
  527. // Mark resources as being in use, unless it has been done already.
  528. static bool resources_claimed = false;
  529. if (!resources_claimed)
  530. {
  531. pio_sm_claim(SDIO_PIO, SDIO_CMD_SM);
  532. pio_sm_claim(SDIO_PIO, SDIO_DATA_SM);
  533. dma_channel_claim(SDIO_DMA_CH);
  534. resources_claimed = true;
  535. }
  536. memset(&g_sdio, 0, sizeof(g_sdio));
  537. // Load PIO programs
  538. pio_clear_instruction_memory(SDIO_PIO);
  539. // Command & clock state machine
  540. g_sdio.pio_cmd_clk_offset = pio_add_program(SDIO_PIO, &sdio_cmd_clk_program);
  541. pio_sm_config cfg = sdio_cmd_clk_program_get_default_config(g_sdio.pio_cmd_clk_offset);
  542. sm_config_set_out_pins(&cfg, SDIO_CMD, 1);
  543. sm_config_set_in_pins(&cfg, SDIO_CMD);
  544. sm_config_set_set_pins(&cfg, SDIO_CMD, 1);
  545. sm_config_set_jmp_pin(&cfg, SDIO_CMD);
  546. sm_config_set_sideset_pins(&cfg, SDIO_CLK);
  547. sm_config_set_out_shift(&cfg, false, true, 32);
  548. sm_config_set_in_shift(&cfg, false, true, 32);
  549. sm_config_set_clkdiv_int_frac(&cfg, 5, 0);
  550. sm_config_set_mov_status(&cfg, STATUS_TX_LESSTHAN, 2);
  551. pio_sm_init(SDIO_PIO, SDIO_CMD_SM, g_sdio.pio_cmd_clk_offset, &cfg);
  552. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_CLK, 1, true);
  553. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, true);
  554. // Data reception program
  555. g_sdio.pio_data_rx_offset = pio_add_program(SDIO_PIO, &sdio_data_rx_program);
  556. g_sdio.pio_cfg_data_rx = sdio_data_rx_program_get_default_config(g_sdio.pio_data_rx_offset);
  557. sm_config_set_in_pins(&g_sdio.pio_cfg_data_rx, SDIO_D0);
  558. sm_config_set_in_shift(&g_sdio.pio_cfg_data_rx, false, true, 32);
  559. sm_config_set_fifo_join(&g_sdio.pio_cfg_data_rx, PIO_FIFO_JOIN_RX);
  560. // Data transmission program
  561. g_sdio.pio_data_tx_offset = pio_add_program(SDIO_PIO, &sdio_data_tx_program);
  562. g_sdio.pio_cfg_data_tx = sdio_data_rx_program_get_default_config(g_sdio.pio_data_tx_offset);
  563. sm_config_set_out_pins(&g_sdio.pio_cfg_data_tx, SDIO_D0, 4);
  564. sm_config_set_out_shift(&g_sdio.pio_cfg_data_tx, false, true, 32);
  565. sm_config_set_fifo_join(&g_sdio.pio_cfg_data_tx, PIO_FIFO_JOIN_TX);
  566. // Disable CLK pin input synchronizer.
  567. // This reduces delay from clk state machine to data state machine.
  568. // Because the CLK pin is output and driven synchronously to CPU clock,
  569. // there is no metastability problems.
  570. SDIO_PIO->input_sync_bypass |= (1 << SDIO_CLK);
  571. // Redirect GPIOs to PIO
  572. gpio_set_function(SDIO_CMD, GPIO_FUNC_PIO1);
  573. gpio_set_function(SDIO_CLK, GPIO_FUNC_PIO1);
  574. gpio_set_function(SDIO_D0, GPIO_FUNC_PIO1);
  575. gpio_set_function(SDIO_D1, GPIO_FUNC_PIO1);
  576. gpio_set_function(SDIO_D2, GPIO_FUNC_PIO1);
  577. gpio_set_function(SDIO_D3, GPIO_FUNC_PIO1);
  578. // Set up IRQ handler when DMA completes.
  579. // This is time-critical because the CRC must be written / read before PIO FIFO runs out.
  580. dma_hw->ints1 = 1 << SDIO_DMA_CH;
  581. dma_channel_set_irq1_enabled(SDIO_DMA_CH, true);
  582. irq_set_exclusive_handler(DMA_IRQ_1, rp2040_sdio_dma_irq);
  583. irq_set_enabled(DMA_IRQ_1, true);
  584. irq_set_priority(DMA_IRQ_1, 255);
  585. }