sd_card_spi.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Driver and interface for accessing SD card in SPI mode
  2. // Used on AzulSCSI v1.0.
  3. #include "AzulSCSI_platform.h"
  4. #include "AzulSCSI_log.h"
  5. #include "gd32f20x_spi.h"
  6. #include "gd32f20x_dma.h"
  7. #include <SdFat.h>
  8. class GD32SPIDriver : public SdSpiBaseClass
  9. {
  10. public:
  11. void begin(SdSpiConfig config) {
  12. rcu_periph_clock_enable(RCU_SPI0);
  13. rcu_periph_clock_enable(RCU_DMA0);
  14. dma_parameter_struct rx_dma_config =
  15. {
  16. .periph_addr = (uint32_t)&SPI_DATA(SD_SPI),
  17. .periph_width = DMA_PERIPHERAL_WIDTH_8BIT,
  18. .memory_addr = 0, // Set before transfer
  19. .memory_width = DMA_MEMORY_WIDTH_8BIT,
  20. .number = 0, // Set before transfer
  21. .priority = DMA_PRIORITY_ULTRA_HIGH,
  22. .periph_inc = DMA_PERIPH_INCREASE_DISABLE,
  23. .memory_inc = DMA_MEMORY_INCREASE_ENABLE,
  24. .direction = DMA_PERIPHERAL_TO_MEMORY
  25. };
  26. dma_init(DMA0, SD_SPI_RX_DMA_CHANNEL, &rx_dma_config);
  27. dma_parameter_struct tx_dma_config =
  28. {
  29. .periph_addr = (uint32_t)&SPI_DATA(SD_SPI),
  30. .periph_width = DMA_PERIPHERAL_WIDTH_8BIT,
  31. .memory_addr = 0, // Set before transfer
  32. .memory_width = DMA_MEMORY_WIDTH_8BIT,
  33. .number = 0, // Set before transfer
  34. .priority = DMA_PRIORITY_HIGH,
  35. .periph_inc = DMA_PERIPH_INCREASE_DISABLE,
  36. .memory_inc = DMA_MEMORY_INCREASE_ENABLE,
  37. .direction = DMA_MEMORY_TO_PERIPHERAL
  38. };
  39. dma_init(DMA0, SD_SPI_TX_DMA_CHANNEL, &tx_dma_config);
  40. }
  41. void activate() {
  42. spi_parameter_struct config = {
  43. SPI_MASTER,
  44. SPI_TRANSMODE_FULLDUPLEX,
  45. SPI_FRAMESIZE_8BIT,
  46. SPI_NSS_SOFT,
  47. SPI_ENDIAN_MSB,
  48. SPI_CK_PL_LOW_PH_1EDGE,
  49. SPI_PSC_256
  50. };
  51. // Select closest available divider based on system frequency
  52. int divider = (SystemCoreClock + m_sckfreq / 2) / m_sckfreq;
  53. if (divider <= 2)
  54. config.prescale = SPI_PSC_2;
  55. else if (divider <= 4)
  56. config.prescale = SPI_PSC_4;
  57. else if (divider <= 8)
  58. config.prescale = SPI_PSC_8;
  59. else if (divider <= 16)
  60. config.prescale = SPI_PSC_16;
  61. else if (divider <= 32)
  62. config.prescale = SPI_PSC_32;
  63. else if (divider <= 64)
  64. config.prescale = SPI_PSC_64;
  65. else if (divider <= 128)
  66. config.prescale = SPI_PSC_128;
  67. else
  68. config.prescale = SPI_PSC_256;
  69. spi_init(SD_SPI, &config);
  70. spi_enable(SD_SPI);
  71. }
  72. void deactivate() {
  73. spi_disable(SD_SPI);
  74. }
  75. void wait_idle() {
  76. while (!(SPI_STAT(SD_SPI) & SPI_STAT_TBE));
  77. while (SPI_STAT(SD_SPI) & SPI_STAT_TRANS);
  78. }
  79. // Single byte receive
  80. uint8_t receive() {
  81. // Wait for idle and clear RX buffer
  82. wait_idle();
  83. (void)SPI_DATA(SD_SPI);
  84. // Send dummy byte and wait for receive
  85. SPI_DATA(SD_SPI) = 0xFF;
  86. while (!(SPI_STAT(SD_SPI) & SPI_STAT_RBNE));
  87. return SPI_DATA(SD_SPI);
  88. }
  89. // Single byte send
  90. void send(uint8_t data) {
  91. SPI_DATA(SD_SPI) = data;
  92. wait_idle();
  93. }
  94. // Multiple byte receive
  95. uint8_t receive(uint8_t* buf, size_t count)
  96. {
  97. // Wait for idle and clear RX buffer
  98. wait_idle();
  99. (void)SPI_DATA(SD_SPI);
  100. // Check if this is part of callback streaming request
  101. bool stream = false;
  102. if (m_stream_callback && buf == m_stream_buffer + m_stream_count)
  103. {
  104. stream = true;
  105. m_stream_count += count;
  106. }
  107. // Use DMA to stream dummy TX data and store RX data
  108. uint8_t tx_data = 0xFF;
  109. DMA_INTC(DMA0) = DMA_FLAG_ADD(DMA_FLAG_FTF | DMA_FLAG_ERR, SD_SPI_RX_DMA_CHANNEL);
  110. DMA_INTC(DMA0) = DMA_FLAG_ADD(DMA_FLAG_FTF | DMA_FLAG_ERR, SD_SPI_TX_DMA_CHANNEL);
  111. DMA_CHMADDR(DMA0, SD_SPI_RX_DMA_CHANNEL) = (uint32_t)buf;
  112. DMA_CHMADDR(DMA0, SD_SPI_TX_DMA_CHANNEL) = (uint32_t)&tx_data;
  113. DMA_CHCTL(DMA0, SD_SPI_TX_DMA_CHANNEL) &= ~DMA_CHXCTL_MNAGA; // No memory increment for TX
  114. DMA_CHCNT(DMA0, SD_SPI_RX_DMA_CHANNEL) = count;
  115. DMA_CHCNT(DMA0, SD_SPI_TX_DMA_CHANNEL) = count;
  116. DMA_CHCTL(DMA0, SD_SPI_RX_DMA_CHANNEL) |= DMA_CHXCTL_CHEN;
  117. DMA_CHCTL(DMA0, SD_SPI_TX_DMA_CHANNEL) |= DMA_CHXCTL_CHEN;
  118. SPI_CTL1(SD_SPI) |= SPI_CTL1_DMAREN | SPI_CTL1_DMATEN;
  119. uint32_t start = millis();
  120. while (!(DMA_INTF(DMA0) & DMA_FLAG_ADD(DMA_FLAG_FTF | DMA_FLAG_ERR, SD_SPI_RX_DMA_CHANNEL)))
  121. {
  122. if (millis() - start > 500)
  123. {
  124. azlog("ERROR: SPI DMA receive of ", (int)count, " bytes timeouted");
  125. return 1;
  126. }
  127. if (stream)
  128. {
  129. uint32_t complete = (count - DMA_CHCNT(DMA0, SD_SPI_RX_DMA_CHANNEL));
  130. m_stream_callback(complete);
  131. }
  132. }
  133. if (DMA_INTF(DMA0) & DMA_FLAG_ADD(DMA_FLAG_ERR, SD_SPI_RX_DMA_CHANNEL))
  134. {
  135. azlog("ERROR: SPI DMA receive set DMA_FLAG_ERR");
  136. }
  137. SPI_CTL1(SD_SPI) &= ~(SPI_CTL1_DMAREN | SPI_CTL1_DMATEN);
  138. DMA_CHCTL(DMA0, SD_SPI_RX_DMA_CHANNEL) &= ~DMA_CHXCTL_CHEN;
  139. DMA_CHCTL(DMA0, SD_SPI_TX_DMA_CHANNEL) &= ~DMA_CHXCTL_CHEN;
  140. return 0;
  141. }
  142. // Multiple byte send
  143. void send(const uint8_t* buf, size_t count) {
  144. // Check if this is part of callback streaming request
  145. bool stream = false;
  146. if (m_stream_callback && buf == m_stream_buffer + m_stream_count)
  147. {
  148. stream = true;
  149. m_stream_count += count;
  150. }
  151. // Use DMA to stream TX data
  152. DMA_INTC(DMA0) = DMA_FLAG_ADD(DMA_FLAG_FTF | DMA_FLAG_ERR, SD_SPI_TX_DMA_CHANNEL);
  153. DMA_CHMADDR(DMA0, SD_SPI_TX_DMA_CHANNEL) = (uint32_t)buf;
  154. DMA_CHCTL(DMA0, SD_SPI_TX_DMA_CHANNEL) |= DMA_CHXCTL_MNAGA; // Memory increment for TX
  155. DMA_CHCNT(DMA0, SD_SPI_TX_DMA_CHANNEL) = count;
  156. DMA_CHCTL(DMA0, SD_SPI_TX_DMA_CHANNEL) |= DMA_CHXCTL_CHEN;
  157. SPI_CTL1(SD_SPI) |= SPI_CTL1_DMATEN;
  158. uint32_t start = millis();
  159. while (!(DMA_INTF(DMA0) & DMA_FLAG_ADD(DMA_FLAG_FTF | DMA_FLAG_ERR, SD_SPI_TX_DMA_CHANNEL)))
  160. {
  161. if (millis() - start > 500)
  162. {
  163. azlog("ERROR: SPI DMA transmit of ", (int)count, " bytes timeouted");
  164. return;
  165. }
  166. if (stream)
  167. {
  168. uint32_t complete = (count - DMA_CHCNT(DMA0, SD_SPI_TX_DMA_CHANNEL));
  169. m_stream_callback(complete);
  170. }
  171. }
  172. if (DMA_INTF(DMA0) & DMA_FLAG_ADD(DMA_FLAG_ERR, SD_SPI_TX_DMA_CHANNEL))
  173. {
  174. azlog("ERROR: SPI DMA transmit set DMA_FLAG_ERR");
  175. }
  176. wait_idle();
  177. SPI_CTL1(SD_SPI) &= ~(SPI_CTL1_DMAREN | SPI_CTL1_DMATEN);
  178. DMA_CHCTL(DMA0, SD_SPI_TX_DMA_CHANNEL) &= ~DMA_CHXCTL_CHEN;
  179. }
  180. void setSckSpeed(uint32_t maxSck) {
  181. m_sckfreq = maxSck;
  182. }
  183. void set_sd_callback(sd_callback_t func, const uint8_t *buffer)
  184. {
  185. m_stream_buffer = buffer;
  186. m_stream_count = 0;
  187. m_stream_callback = func;
  188. }
  189. private:
  190. uint32_t m_sckfreq;
  191. const uint8_t *m_stream_buffer;
  192. uint32_t m_stream_count;
  193. sd_callback_t m_stream_callback;
  194. };
  195. void sdCsInit(SdCsPin_t pin)
  196. {
  197. }
  198. void sdCsWrite(SdCsPin_t pin, bool level)
  199. {
  200. if (level)
  201. GPIO_BOP(SD_PORT) = SD_CS_PIN;
  202. else
  203. GPIO_BC(SD_PORT) = SD_CS_PIN;
  204. }
  205. GD32SPIDriver g_sd_spi_port;
  206. SdSpiConfig g_sd_spi_config(0, DEDICATED_SPI, SD_SCK_MHZ(30), &g_sd_spi_port);