rp2040_sdio.pio 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ; RP2040 PIO program for implementing SD card access in SDIO mode
  2. ; Run "pioasm rp2040_sdio.pio rp2040_sdio.pio.h" to regenerate the C header from this.
  3. ;
  4. ; Copyright (c) 2022 Rabbit Hole Computing™
  5. ;
  6. ; The RP2040 official work-in-progress code at
  7. ; https://github.com/raspberrypi/pico-extras/tree/master/src/rp2_common/pico_sd_card
  8. ; may be useful reference, but this is independent implementation.
  9. ;
  10. ; For official SDIO specifications, refer to:
  11. ; https://www.sdcard.org/downloads/pls/
  12. ; "SDIO Physical Layer Simplified Specification Version 8.00"
  13. ; Clock settings
  14. ; For 3.3V communication the available speeds are:
  15. ; - Default speed: max. 25 MHz clock
  16. ; - High speed: max. 50 MHz clock
  17. ;
  18. ; From the default RP2040 clock speed of 125 MHz, the closest dividers
  19. ; are 3 for 41.7 MHz and 5 for 25 MHz. The CPU can apply further divider
  20. ; through state machine registers for the initial handshake.
  21. ;
  22. ; Because data is written on the falling edge and read on the rising
  23. ; edge, it is preferrable to have a long 0 state and short 1 state.
  24. ;.define CLKDIV 3
  25. .define CLKDIV 5
  26. .define D0 ((CLKDIV + 1) / 2 - 1)
  27. .define D1 (CLKDIV/2 - 1)
  28. .define SDIO_CLK_GPIO 10
  29. ; State machine 0 is used to:
  30. ; - generate continuous clock on SDIO_CLK
  31. ; - send CMD packets
  32. ; - receive response packets
  33. ;
  34. ; Pin mapping for this state machine:
  35. ; - Sideset : CLK
  36. ; - IN/OUT/SET : CMD
  37. ; - JMP_PIN : CMD
  38. ;
  39. ; The commands to send are put on TX fifo and must have two words:
  40. ; Word 0 bits 31-24: Number of bits in command minus one (usually 47)
  41. ; Word 0 bits 23-00: First 24 bits of the command packet, shifted out MSB first
  42. ; Word 1 bits 31-08: Last 24 bits of the command packet, shifted out MSB first
  43. ; Word 1 bits 07-00: Number of bits in response minus one (usually 47), or 0 if no response
  44. ;
  45. ; The response is put on RX fifo, starting with the MSB.
  46. ; Partial last word will be padded with zero bits at the top.
  47. ;
  48. ; The state machine EXECCTRL should be set so that STATUS indicates TX FIFO < 2
  49. ; and that AUTOPULL and AUTOPUSH are enabled.
  50. .program sdio_cmd_clk
  51. .side_set 1
  52. mov OSR, NULL side 1 [D1] ; Make sure OSR is full of zeros to prevent autopull
  53. wait_cmd:
  54. mov Y, !STATUS side 0 [D0] ; Check if TX FIFO has data
  55. jmp !Y wait_cmd side 1 [D1]
  56. load_cmd:
  57. out NULL, 32 side 0 [D0] ; Load first word (trigger autopull)
  58. out X, 8 side 1 [D1] ; Number of bits to send
  59. set pins, 1 side 0 [D0] ; Initial state of CMD is high
  60. set pindirs, 1 side 1 [D1] ; Set SDIO_CMD as output
  61. send_cmd:
  62. out pins, 1 side 0 [D0] ; Write output on falling edge of CLK
  63. jmp X-- send_cmd side 1 [D1]
  64. prep_resp:
  65. set pindirs, 0 side 0 [D0] ; Set SDIO_CMD as input
  66. out X, 8 side 1 [D1] ; Get number of bits in response
  67. nop side 0 [D0] ; For clock alignment
  68. jmp !X resp_done side 1 [D1] ; Check if we expect a response
  69. wait_resp:
  70. nop side 0 [D0]
  71. jmp PIN wait_resp side 1 [D1] ; Loop until SDIO_CMD = 0
  72. ; Note: input bits are read at the same time as we write CLK=0.
  73. ; Because the host controls the clock, the read happens before
  74. ; the card sees the falling clock edge. This gives maximum time
  75. ; for the data bit to settle.
  76. read_resp:
  77. in PINS, 1 side 0 [D0] ; Read input data bit
  78. jmp X-- read_resp side 1 [D1] ; Loop to receive all data bits
  79. resp_done:
  80. push side 0 [D0] ; Push the remaining part of response
  81. ; State machine 1 is used to send and receive data blocks.
  82. ; Pin mapping for this state machine:
  83. ; - IN / OUT: SDIO_D0-D3
  84. ; - GPIO defined at beginning of this file: SDIO_CLK
  85. ; Data reception program
  86. ; This program will wait for initial start of block token and then
  87. ; receive a data block. The application must set number of nibbles
  88. ; to receive minus 1 to Y register before running this program.
  89. .program sdio_data_rx
  90. wait_start:
  91. mov X, Y ; Reinitialize number of nibbles to receive
  92. wait 0 pin 0 ; Wait for zero state on D0
  93. wait 1 gpio SDIO_CLK_GPIO [CLKDIV-1] ; Wait for rising edge and then whole clock cycle
  94. rx_data:
  95. in PINS, 4 [CLKDIV-2] ; Read nibble
  96. jmp X--, rx_data
  97. ; Data transmission program
  98. ;
  99. ; Before running this program, pindirs should be set as output
  100. ; and register X should be initialized with the number of nibbles
  101. ; to send minus 1 (typically 8 + 1024 + 16 + 1 - 1 = 1048)
  102. ; and register Y with the number of response bits minus 1 (typically 31).
  103. ;
  104. ; Words written to TX FIFO must be:
  105. ; - Word 0: start token 0xFFFFFFF0
  106. ; - Word 1-128: transmitted data (512 bytes)
  107. ; - Word 129-130: CRC checksum
  108. ; - Word 131: end token 0xFFFFFFFF
  109. ;
  110. ; After the card reports idle status, RX FIFO will get a word that
  111. ; contains the D0 line response from card.
  112. .program sdio_data_tx
  113. wait 0 gpio SDIO_CLK_GPIO
  114. wait 1 gpio SDIO_CLK_GPIO [CLKDIV + D1 - 1]; Synchronize so that write occurs on falling edge
  115. tx_loop:
  116. out PINS, 4 [D0] ; Write nibble and wait for whole clock cycle
  117. jmp X-- tx_loop [D1]
  118. set pindirs, 0x00 [D0] ; Set data bus as input
  119. .wrap_target
  120. response_loop:
  121. in PINS, 1 [D1] ; Read D0 on rising edge
  122. jmp Y--, response_loop [D0]
  123. wait_idle:
  124. wait 1 pin 0 [D1] ; Wait for card to indicate idle condition
  125. push [D0] ; Push the response token
  126. .wrap