sd_card_spi.cpp 7.8 KB

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