sd_card_spi.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022-2025 Rabbit Hole Computing™
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version. 
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details. 
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. // Driver and interface for accessing SD card in SPI mode
  22. // Used on ZuluSCSI v1.0.
  23. #include "ZuluSCSI_platform.h"
  24. #include "ZuluSCSI_log.h"
  25. #include "gd32f20x_spi.h"
  26. #include "gd32f20x_dma.h"
  27. #include <SdFat.h>
  28. #ifndef SD_USE_SDIO
  29. class GD32SPIDriver : public SdSpiBaseClass
  30. {
  31. public:
  32. void begin(SdSpiConfig config) {
  33. rcu_periph_clock_enable(RCU_SPI0);
  34. rcu_periph_clock_enable(RCU_DMA0);
  35. dma_parameter_struct rx_dma_config =
  36. {
  37. .periph_addr = (uint32_t)&SPI_DATA(SD_SPI),
  38. .periph_width = DMA_PERIPHERAL_WIDTH_8BIT,
  39. .memory_addr = 0, // Set before transfer
  40. .memory_width = DMA_MEMORY_WIDTH_8BIT,
  41. .number = 0, // Set before transfer
  42. .priority = DMA_PRIORITY_ULTRA_HIGH,
  43. .periph_inc = DMA_PERIPH_INCREASE_DISABLE,
  44. .memory_inc = DMA_MEMORY_INCREASE_ENABLE,
  45. .direction = DMA_PERIPHERAL_TO_MEMORY
  46. };
  47. dma_init(DMA0, SD_SPI_RX_DMA_CHANNEL, &rx_dma_config);
  48. dma_parameter_struct tx_dma_config =
  49. {
  50. .periph_addr = (uint32_t)&SPI_DATA(SD_SPI),
  51. .periph_width = DMA_PERIPHERAL_WIDTH_8BIT,
  52. .memory_addr = 0, // Set before transfer
  53. .memory_width = DMA_MEMORY_WIDTH_8BIT,
  54. .number = 0, // Set before transfer
  55. .priority = DMA_PRIORITY_HIGH,
  56. .periph_inc = DMA_PERIPH_INCREASE_DISABLE,
  57. .memory_inc = DMA_MEMORY_INCREASE_ENABLE,
  58. .direction = DMA_MEMORY_TO_PERIPHERAL
  59. };
  60. dma_init(DMA0, SD_SPI_TX_DMA_CHANNEL, &tx_dma_config);
  61. }
  62. void activate() {
  63. spi_parameter_struct config = {
  64. SPI_MASTER,
  65. SPI_TRANSMODE_FULLDUPLEX,
  66. SPI_FRAMESIZE_8BIT,
  67. SPI_NSS_SOFT,
  68. SPI_ENDIAN_MSB,
  69. SPI_CK_PL_LOW_PH_1EDGE,
  70. SPI_PSC_256
  71. };
  72. // Select closest available divider based on system frequency
  73. int divider = (SystemCoreClock + m_sckfreq / 2) / m_sckfreq;
  74. if (divider <= 2)
  75. config.prescale = SPI_PSC_2;
  76. else if (divider <= 4)
  77. config.prescale = SPI_PSC_4;
  78. else if (divider <= 8)
  79. config.prescale = SPI_PSC_8;
  80. else if (divider <= 16)
  81. config.prescale = SPI_PSC_16;
  82. else if (divider <= 32)
  83. config.prescale = SPI_PSC_32;
  84. else if (divider <= 64)
  85. config.prescale = SPI_PSC_64;
  86. else if (divider <= 128)
  87. config.prescale = SPI_PSC_128;
  88. else
  89. config.prescale = SPI_PSC_256;
  90. spi_init(SD_SPI, &config);
  91. spi_enable(SD_SPI);
  92. }
  93. void deactivate() {
  94. spi_disable(SD_SPI);
  95. }
  96. void wait_idle() {
  97. while (!(SPI_STAT(SD_SPI) & SPI_STAT_TBE));
  98. while (SPI_STAT(SD_SPI) & SPI_STAT_TRANS);
  99. }
  100. // Single byte receive
  101. uint8_t receive() {
  102. // Wait for idle and clear RX buffer
  103. wait_idle();
  104. (void)SPI_DATA(SD_SPI);
  105. // Send dummy byte and wait for receive
  106. SPI_DATA(SD_SPI) = 0xFF;
  107. while (!(SPI_STAT(SD_SPI) & SPI_STAT_RBNE));
  108. return SPI_DATA(SD_SPI);
  109. }
  110. // Single byte send
  111. void send(uint8_t data) {
  112. SPI_DATA(SD_SPI) = data;
  113. wait_idle();
  114. }
  115. // Multiple byte receive
  116. uint8_t receive(uint8_t* buf, size_t count)
  117. {
  118. // Wait for idle and clear RX buffer
  119. wait_idle();
  120. (void)SPI_DATA(SD_SPI);
  121. // Check if this is part of callback streaming request
  122. bool stream = false;
  123. if (m_stream_callback && buf == m_stream_buffer + m_stream_count)
  124. {
  125. stream = true;
  126. }
  127. else if (m_stream_callback)
  128. {
  129. dbgmsg("Stream buffer mismatch: ", (uint32_t)buf, " vs. ", (uint32_t)(m_stream_buffer + m_stream_count));
  130. }
  131. // Use DMA to stream dummy TX data and store RX data
  132. uint8_t tx_data = 0xFF;
  133. DMA_INTC(DMA0) = DMA_FLAG_ADD(DMA_FLAG_FTF | DMA_FLAG_ERR, SD_SPI_RX_DMA_CHANNEL);
  134. DMA_INTC(DMA0) = DMA_FLAG_ADD(DMA_FLAG_FTF | DMA_FLAG_ERR, SD_SPI_TX_DMA_CHANNEL);
  135. DMA_CHMADDR(DMA0, SD_SPI_RX_DMA_CHANNEL) = (uint32_t)buf;
  136. DMA_CHMADDR(DMA0, SD_SPI_TX_DMA_CHANNEL) = (uint32_t)&tx_data;
  137. DMA_CHCTL(DMA0, SD_SPI_TX_DMA_CHANNEL) &= ~DMA_CHXCTL_MNAGA; // No memory increment for TX
  138. DMA_CHCNT(DMA0, SD_SPI_RX_DMA_CHANNEL) = count;
  139. DMA_CHCNT(DMA0, SD_SPI_TX_DMA_CHANNEL) = count;
  140. DMA_CHCTL(DMA0, SD_SPI_RX_DMA_CHANNEL) |= DMA_CHXCTL_CHEN;
  141. DMA_CHCTL(DMA0, SD_SPI_TX_DMA_CHANNEL) |= DMA_CHXCTL_CHEN;
  142. SPI_CTL1(SD_SPI) |= SPI_CTL1_DMAREN | SPI_CTL1_DMATEN;
  143. uint32_t start = millis();
  144. while (!(DMA_INTF(DMA0) & DMA_FLAG_ADD(DMA_FLAG_FTF | DMA_FLAG_ERR, SD_SPI_RX_DMA_CHANNEL)))
  145. {
  146. if (millis() - start > 500)
  147. {
  148. logmsg("ERROR: SPI DMA receive of ", (int)count, " bytes timeouted");
  149. return 1;
  150. }
  151. if (stream)
  152. {
  153. uint32_t complete = m_stream_count + (count - DMA_CHCNT(DMA0, SD_SPI_RX_DMA_CHANNEL));
  154. m_stream_callback(complete);
  155. }
  156. }
  157. if (DMA_INTF(DMA0) & DMA_FLAG_ADD(DMA_FLAG_ERR, SD_SPI_RX_DMA_CHANNEL))
  158. {
  159. logmsg("ERROR: SPI DMA receive set DMA_FLAG_ERR");
  160. }
  161. SPI_CTL1(SD_SPI) &= ~(SPI_CTL1_DMAREN | SPI_CTL1_DMATEN);
  162. DMA_CHCTL(DMA0, SD_SPI_RX_DMA_CHANNEL) &= ~DMA_CHXCTL_CHEN;
  163. DMA_CHCTL(DMA0, SD_SPI_TX_DMA_CHANNEL) &= ~DMA_CHXCTL_CHEN;
  164. if (stream)
  165. {
  166. m_stream_count += count;
  167. }
  168. return 0;
  169. }
  170. // Multiple byte send
  171. void send(const uint8_t* buf, size_t count) {
  172. // Check if this is part of callback streaming request
  173. bool stream = false;
  174. if (m_stream_callback && buf == m_stream_buffer + m_stream_count)
  175. {
  176. stream = true;
  177. }
  178. else if (m_stream_callback)
  179. {
  180. dbgmsg("Stream buffer mismatch: ", (uint32_t)buf, " vs. ", (uint32_t)(m_stream_buffer + m_stream_count));
  181. }
  182. // Use DMA to stream TX data
  183. DMA_INTC(DMA0) = DMA_FLAG_ADD(DMA_FLAG_FTF | DMA_FLAG_ERR, SD_SPI_TX_DMA_CHANNEL);
  184. DMA_CHMADDR(DMA0, SD_SPI_TX_DMA_CHANNEL) = (uint32_t)buf;
  185. DMA_CHCTL(DMA0, SD_SPI_TX_DMA_CHANNEL) |= DMA_CHXCTL_MNAGA; // Memory increment for TX
  186. DMA_CHCNT(DMA0, SD_SPI_TX_DMA_CHANNEL) = count;
  187. DMA_CHCTL(DMA0, SD_SPI_TX_DMA_CHANNEL) |= DMA_CHXCTL_CHEN;
  188. SPI_CTL1(SD_SPI) |= SPI_CTL1_DMATEN;
  189. uint32_t start = millis();
  190. while (!(DMA_INTF(DMA0) & DMA_FLAG_ADD(DMA_FLAG_FTF | DMA_FLAG_ERR, SD_SPI_TX_DMA_CHANNEL)))
  191. {
  192. if (millis() - start > 500)
  193. {
  194. logmsg("ERROR: SPI DMA transmit of ", (int)count, " bytes timeouted");
  195. return;
  196. }
  197. if (stream)
  198. {
  199. uint32_t complete = m_stream_count + (count - DMA_CHCNT(DMA0, SD_SPI_TX_DMA_CHANNEL));
  200. m_stream_callback(complete);
  201. }
  202. }
  203. if (DMA_INTF(DMA0) & DMA_FLAG_ADD(DMA_FLAG_ERR, SD_SPI_TX_DMA_CHANNEL))
  204. {
  205. logmsg("ERROR: SPI DMA transmit set DMA_FLAG_ERR");
  206. }
  207. wait_idle();
  208. SPI_CTL1(SD_SPI) &= ~(SPI_CTL1_DMAREN | SPI_CTL1_DMATEN);
  209. DMA_CHCTL(DMA0, SD_SPI_TX_DMA_CHANNEL) &= ~DMA_CHXCTL_CHEN;
  210. if (stream)
  211. {
  212. m_stream_count += count;
  213. }
  214. }
  215. void setSckSpeed(uint32_t maxSck) {
  216. m_sckfreq = maxSck;
  217. }
  218. void set_sd_callback(sd_callback_t func, const uint8_t *buffer)
  219. {
  220. m_stream_buffer = buffer;
  221. m_stream_count = 0;
  222. m_stream_callback = func;
  223. }
  224. private:
  225. uint32_t m_sckfreq;
  226. const uint8_t *m_stream_buffer;
  227. uint32_t m_stream_count;
  228. sd_callback_t m_stream_callback;
  229. };
  230. void sdCsInit(SdCsPin_t pin)
  231. {
  232. }
  233. void sdCsWrite(SdCsPin_t pin, bool level)
  234. {
  235. if (level)
  236. GPIO_BOP(SD_PORT) = SD_CS_PIN;
  237. else
  238. GPIO_BC(SD_PORT) = SD_CS_PIN;
  239. }
  240. GD32SPIDriver g_sd_spi_port;
  241. SdSpiConfig g_sd_spi_config(0, DEDICATED_SPI, SD_SCK_MHZ(30), &g_sd_spi_port);
  242. void platform_set_sd_callback(sd_callback_t func, const uint8_t *buffer)
  243. {
  244. g_sd_spi_port.set_sd_callback(func, buffer);
  245. }
  246. // Check if a DMA request for SD card read has completed.
  247. // This is used to optimize the timing of data transfers on SCSI bus.
  248. bool check_sd_read_done()
  249. {
  250. return (DMA_CHCTL(DMA0, SD_SPI_RX_DMA_CHANNEL) & DMA_CHXCTL_CHEN)
  251. && (DMA_INTF(DMA0) & DMA_FLAG_ADD(DMA_FLAG_FTF, SD_SPI_RX_DMA_CHANNEL));
  252. }
  253. #endif