scsi_accel_dma.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #include "scsi_accel_dma.h"
  2. #include <ZuluSCSI_log.h>
  3. #include <gd32f4xx_timer.h>
  4. #include <gd32f4xx_rcu.h>
  5. #include <assert.h>
  6. #include <string.h>
  7. #ifndef SCSI_ACCEL_DMA_AVAILABLE
  8. void scsi_accel_timer_dma_init() {}
  9. void scsi_accel_greenpak_dma_init() {}
  10. void scsi_accel_dma_startWrite(const uint8_t* data, uint32_t count, volatile int *resetFlag) {}
  11. void scsi_accel_dma_stopWrite() {}
  12. void scsi_accel_dma_finishWrite(volatile int *resetFlag) {}
  13. bool scsi_accel_dma_isWriteFinished(const uint8_t* data) { return true; }
  14. #else
  15. #define DMA_BUF_SIZE 256
  16. #define DMA_BUF_MASK (DMA_BUF_SIZE - 1)
  17. static struct {
  18. uint8_t *app_buf; // Buffer provided by application
  19. uint32_t dma_buf[DMA_BUF_SIZE]; // Buffer of data formatted for GPIO BOP register
  20. uint32_t dma_idx; // Write index to DMA buffer
  21. uint32_t dma_fillto; // Point up to which DMA buffer is available for refilling
  22. uint32_t timer_buf; // Control value for timer SWEVG register
  23. uint32_t bytes_app; // Bytes available in application buffer
  24. uint32_t bytes_dma; // Bytes (words) written so far to DMA buffer
  25. uint32_t scheduled_dma; // Bytes (words) that DMA data count was last set to
  26. uint8_t *next_app_buf; // Next buffer from application after current one finishes
  27. uint32_t next_app_bytes; // Bytes in next buffer
  28. } g_scsi_dma;
  29. enum scsidma_state_t { SCSIDMA_IDLE = 0, SCSIDMA_WRITE };
  30. static volatile scsidma_state_t g_scsi_dma_state;
  31. void scsi_accel_timer_dma_init()
  32. {
  33. g_scsi_dma_state = SCSIDMA_IDLE;
  34. rcu_periph_clock_enable(SCSI_TIMER_RCU);
  35. rcu_periph_clock_enable(SCSI_TIMER_DMA_RCU);
  36. // DMA Channel A: data copy
  37. // GPIO DMA copies data from memory buffer to GPIO BOP register.
  38. // The memory buffer is filled by interrupt routine.
  39. dma_multi_data_parameter_struct gpio_dma_config =
  40. {
  41. .periph_addr = (uint32_t)&GPIO_BOP(SCSI_OUT_PORT),
  42. .periph_width = DMA_PERIPH_WIDTH_32BIT,
  43. .periph_inc = DMA_PERIPH_INCREASE_DISABLE,
  44. .memory0_addr = 0, // Filled before transfer
  45. .memory_width = DMA_MEMORY_WIDTH_32BIT,
  46. .memory_inc = DMA_MEMORY_INCREASE_ENABLE,
  47. .memory_burst_width = DMA_MEMORY_BURST_SINGLE,
  48. .periph_burst_width = DMA_PERIPH_BURST_SINGLE,
  49. .critical_value = DMA_FIFO_1_WORD,
  50. .circular_mode = DMA_CIRCULAR_MODE_ENABLE,
  51. .direction = DMA_MEMORY_TO_PERIPH,
  52. .number = DMA_BUF_SIZE,
  53. .priority = DMA_PRIORITY_ULTRA_HIGH
  54. };
  55. dma_multi_data_mode_init(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, &gpio_dma_config);
  56. dma_channel_subperipheral_select(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, SCSI_TIMER_DMACHA_SUB_PERIPH);
  57. NVIC_SetPriority(SCSI_TIMER_DMACHA_IRQn, 1);
  58. NVIC_EnableIRQ(SCSI_TIMER_DMACHA_IRQn);
  59. // DMA Channel B: timer update
  60. // Timer DMA causes update event to restart timer after
  61. // GPIO DMA operation is done.
  62. dma_multi_data_parameter_struct timer_dma_config =
  63. {
  64. .periph_addr = (uint32_t)&TIMER_SWEVG(SCSI_TIMER),
  65. .periph_width = DMA_PERIPH_WIDTH_32BIT,
  66. .periph_inc = DMA_PERIPH_INCREASE_DISABLE,
  67. .memory0_addr = (uint32_t)&g_scsi_dma.timer_buf,
  68. .memory_width = DMA_MEMORY_WIDTH_32BIT,
  69. .memory_inc = DMA_PERIPH_INCREASE_DISABLE,
  70. .memory_burst_width = DMA_MEMORY_BURST_SINGLE,
  71. .periph_burst_width = DMA_PERIPH_BURST_SINGLE,
  72. .critical_value = DMA_FIFO_1_WORD,
  73. .circular_mode = DMA_CIRCULAR_MODE_DISABLE,
  74. .direction = DMA_MEMORY_TO_PERIPH,
  75. .number = DMA_BUF_SIZE,
  76. .priority = DMA_PRIORITY_HIGH
  77. };
  78. dma_multi_data_mode_init(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, &timer_dma_config);
  79. dma_channel_subperipheral_select(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, SCSI_TIMER_DMACHB_SUB_PERIPH);
  80. NVIC_SetPriority(SCSI_TIMER_DMACHB_IRQn, 2);
  81. NVIC_EnableIRQ(SCSI_TIMER_DMACHB_IRQn);
  82. g_scsi_dma.timer_buf = TIMER_SWEVG_UPG;
  83. // Timer is used to toggle the request signal based on external trigger input.
  84. // OUT_REQ is driven by timer output.
  85. // 1. On timer update event, REQ is set low.
  86. // 2. When ACK goes low, timer counts and OUT_REQ is set high.
  87. // Simultaneously a DMA request is triggered to write next data to GPIO.
  88. // 3. When ACK goes high, a DMA request is triggered to cause timer update event.
  89. // The DMA request priority is set so that 2. always completes before it.
  90. TIMER_CTL0(SCSI_TIMER) = 0;
  91. TIMER_SMCFG(SCSI_TIMER) = TIMER_SLAVE_MODE_EXTERNAL0 | TIMER_SMCFG_TRGSEL_CI0F_ED;
  92. TIMER_CAR(SCSI_TIMER) = 65535;
  93. TIMER_PSC(SCSI_TIMER) = 0;
  94. TIMER_DMAINTEN(SCSI_TIMER) = 0;
  95. TIMER_CHCTL0(SCSI_TIMER) = 0x6001; // CH0 as input, CH1 as DMA trigger
  96. TIMER_CHCTL1(SCSI_TIMER) = 0x6074; // CH2 as fast PWM output, CH3 as DMA trigger
  97. TIMER_CHCTL2(SCSI_TIMER) = TIMER_CHCTL2_CH2NEN;
  98. TIMER_CCHP(SCSI_TIMER) = TIMER_CCHP_POEN;
  99. TIMER_CH1CV(SCSI_TIMER) = 1; // Copy data when ACK goes low
  100. TIMER_CH2CV(SCSI_TIMER) = 1; // REQ is low until ACK goes low
  101. TIMER_CH3CV(SCSI_TIMER) = 2; // Reset timer after ACK goes high & previous DMA is complete
  102. gpio_mode_set(SCSI_TIMER_IN_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, SCSI_TIMER_IN_PIN);
  103. gpio_af_set(SCSI_TIMER_IN_PORT, SCSI_TIMER_IN_AF, SCSI_TIMER_IN_PIN);
  104. scsi_accel_dma_stopWrite();
  105. }
  106. // Select whether OUT_REQ is connected to timer or GPIO port
  107. static void scsi_dma_gpio_config(bool enable)
  108. {
  109. if (enable)
  110. {
  111. gpio_mode_set(SCSI_OUT_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, SCSI_OUT_REQ);
  112. gpio_mode_set(SCSI_TIMER_OUT_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, SCSI_TIMER_OUT_PIN);
  113. // @TODO determine if the output should be set to 200MHZ instead of 50MHZ
  114. gpio_output_options_set(SCSI_TIMER_OUT_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, SCSI_TIMER_OUT_PIN);
  115. // @TODO find if TIMER2_CH3 (AF2) is the correct AF
  116. gpio_af_set(SCSI_TIMER_OUT_PORT, SCSI_TIMER_OUT_AF, SCSI_TIMER_OUT_PIN);
  117. }
  118. else
  119. {
  120. // @ DELETE this line shouldn't be needed?
  121. // GPIO_BC(SCSI_OUT_PORT) = GREENPAK_PLD_IO2;
  122. gpio_mode_set(SCSI_TIMER_OUT_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, SCSI_TIMER_OUT_PIN);
  123. gpio_output_options_set(SCSI_OUT_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, SCSI_OUT_REQ);
  124. gpio_mode_set(SCSI_OUT_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, SCSI_OUT_REQ);
  125. }
  126. }
  127. // Convert input bytes into BOP values in the DMA buffer
  128. static void refill_dmabuf()
  129. {
  130. // Check how many bytes we have available from the application
  131. uint32_t count = g_scsi_dma.bytes_app - g_scsi_dma.bytes_dma;
  132. // Check amount of free space in DMA buffer
  133. uint32_t max = g_scsi_dma.dma_fillto - g_scsi_dma.dma_idx;
  134. if (count > max) count = max;
  135. if (count == 0) return;
  136. uint8_t *src = g_scsi_dma.app_buf + g_scsi_dma.bytes_dma;
  137. uint32_t *dst = g_scsi_dma.dma_buf;
  138. uint32_t pos = g_scsi_dma.dma_idx;
  139. uint32_t end = pos + count;
  140. g_scsi_dma.dma_idx = end;
  141. g_scsi_dma.bytes_dma += count;
  142. while (pos + 4 <= end)
  143. {
  144. uint32_t input = *(uint32_t*)src;
  145. src += 4;
  146. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop[(input >> 0) & 0xFF];
  147. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop[(input >> 8) & 0xFF];
  148. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop[(input >> 16) & 0xFF];
  149. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop[(input >> 24) & 0xFF];
  150. }
  151. while (pos < end)
  152. {
  153. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop[*src++];
  154. }
  155. if (end < g_scsi_dma.dma_fillto)
  156. {
  157. // Partial buffer fill, this will get refilled from interrupt if we
  158. // get more data. Set next byte to an invalid parity value so that
  159. // any race conditions will get caught as parity error.
  160. dst[pos & DMA_BUF_MASK] = g_scsi_out_byte_to_bop[0] ^ SCSI_OUT_DBP;
  161. }
  162. }
  163. // Start DMA transfer
  164. static void start_dma()
  165. {
  166. // Disable channels while configuring
  167. dma_channel_disable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA);
  168. dma_channel_disable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB);
  169. TIMER_CTL0(SCSI_TIMER) = 0;
  170. // Set new buffer address and size
  171. // CHA / Data channel is in circular mode and always has DMA_BUF_SIZE buffer size.
  172. // CHB / Update channel limits the number of data.
  173. dma_memory_address_config(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_MEMORY_0, (uint32_t)g_scsi_dma.dma_buf);
  174. // DMA_CHM0ADDR(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA) = (uint32_t)g_scsi_dma.dma_buf;
  175. dma_transfer_number_config(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_BUF_SIZE);
  176. // DMA_CHCNT(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA) = DMA_BUF_SIZE;
  177. uint32_t dma_to_schedule = g_scsi_dma.bytes_app - g_scsi_dma.scheduled_dma;
  178. dma_transfer_number_config(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, dma_to_schedule);
  179. // DMA_CHCNT(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB) = dma_to_schedule;
  180. g_scsi_dma.scheduled_dma += dma_to_schedule;
  181. // Clear pending DMA events
  182. TIMER_DMAINTEN(SCSI_TIMER) = 0;
  183. TIMER_DMAINTEN(SCSI_TIMER) = TIMER_DMAINTEN_CH1DEN | TIMER_DMAINTEN_CH3DEN;
  184. // Clear and enable interrupt
  185. dma_interrupt_flag_clear(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_FLAG_HTF | DMA_FLAG_FTF | DMA_FLAG_FEE);
  186. // DMA_INTC1(SCSI_TIMER_DMA) = DMA_FLAG_ADD(DMA_FLAG_HTF | DMA_FLAG_FTF | DMA_FLAG_FEE, SCSI_TIMER_DMACHA);
  187. dma_interrupt_flag_clear(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, DMA_FLAG_HTF | DMA_FLAG_FTF | DMA_FLAG_FEE);
  188. // DMA_INTC0(SCSI_TIMER_DMA) = DMA_FLAG_ADD(DMA_FLAG_HTF | DMA_FLAG_FTF | DMA_FLAG_FEE, SCSI_TIMER_DMACHB);
  189. dma_interrupt_enable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_CHXCTL_FTFIE | DMA_CHXCTL_HTFIE);
  190. // DMA_CHCTL(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA) |= DMA_CHXCTL_FTFIE | DMA_CHXCTL_HTFIE;
  191. dma_interrupt_enable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, DMA_CHXCTL_FTFIE);
  192. // DMA_CHCTL(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB) |= DMA_CHXCTL_FTFIE;
  193. // Enable channels
  194. dma_channel_enable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA);
  195. //DMA_CHCTL(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA) |= DMA_CHXCTL_CHEN;
  196. dma_channel_enable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB);
  197. //DMA_CHCTL(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB) |= DMA_CHXCTL_CHEN;
  198. // Make sure REQ is initially high
  199. TIMER_CNT(SCSI_TIMER) = 16;
  200. TIMER_CHCTL1(SCSI_TIMER) = 0x6050;
  201. TIMER_CHCTL1(SCSI_TIMER) = 0x6074;
  202. // Enable timer
  203. timer_enable(SCSI_TIMER);
  204. //TIMER_CTL0(SCSI_TIMER) |= TIMER_CTL0_CEN;
  205. // Generate first events
  206. TIMER_SWEVG(SCSI_TIMER) = TIMER_SWEVG_CH1G;
  207. TIMER_SWEVG(SCSI_TIMER) = TIMER_SWEVG_CH3G;
  208. }
  209. // Stop DMA transfer
  210. static void stop_dma()
  211. {
  212. dma_channel_disable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA);
  213. // DMA_CHCTL(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA) &= ~DMA_CHXCTL_CHEN;
  214. dma_channel_disable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB);
  215. // DMA_CHCTL(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB) &= ~DMA_CHXCTL_CHEN;
  216. dma_interrupt_disable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_CHXCTL_FTFIE | DMA_CHXCTL_HTFIE);
  217. // DMA_CHCTL(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA) &= ~(DMA_CHXCTL_FTFIE | DMA_CHXCTL_HTFIE);
  218. dma_interrupt_disable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, DMA_CHXCTL_FTFIE);
  219. //DMA_CHCTL(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB) &= ~DMA_CHXCTL_FTFIE;
  220. timer_disable(SCSI_TIMER);
  221. //TIMER_CTL0(SCSI_TIMER) &= ~TIMER_CTL0_CEN;
  222. g_scsi_dma_state = SCSIDMA_IDLE;
  223. SCSI_RELEASE_DATA_REQ();
  224. }
  225. static void check_dma_next_buffer()
  226. {
  227. // Check if we are at the end of the application buffer
  228. if (g_scsi_dma.next_app_buf && g_scsi_dma.bytes_dma == g_scsi_dma.bytes_app)
  229. {
  230. // Switch to next buffer
  231. assert(g_scsi_dma.scheduled_dma == g_scsi_dma.bytes_app);
  232. g_scsi_dma.app_buf = g_scsi_dma.next_app_buf;
  233. g_scsi_dma.bytes_app = g_scsi_dma.next_app_bytes;
  234. g_scsi_dma.bytes_dma = 0;
  235. g_scsi_dma.scheduled_dma = 0;
  236. g_scsi_dma.next_app_buf = 0;
  237. g_scsi_dma.next_app_bytes = 0;
  238. refill_dmabuf();
  239. }
  240. }
  241. // Convert new data from application buffer to DMA buffer
  242. extern "C" void SCSI_TIMER_DMACHA_IRQ()
  243. {
  244. // azdbg("DMA irq A, counts: ", dma_transfer_number_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA), " ",
  245. // dma_transfer_number_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB), " ",
  246. // TIMER_CNT(SCSI_TIMER));
  247. uint32_t intf0 = DMA_INTF1(SCSI_TIMER_DMA);
  248. uint32_t intf1 = DMA_INTF1(SCSI_TIMER_DMA);
  249. if (dma_interrupt_flag_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_FLAG_HTF))
  250. {
  251. if (dma_interrupt_flag_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_FLAG_FTF))
  252. {
  253. azlog("ERROR: SCSI DMA overrun: ", intf0, intf1,
  254. " bytes_app: ", g_scsi_dma.bytes_app,
  255. " bytes_dma: ", g_scsi_dma.bytes_dma,
  256. " dma_idx: ", g_scsi_dma.dma_idx,
  257. " sched_dma: ", g_scsi_dma.scheduled_dma);
  258. stop_dma();
  259. return;
  260. }
  261. dma_interrupt_flag_clear(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_FLAG_HTF);
  262. g_scsi_dma.dma_fillto += DMA_BUF_SIZE / 2;
  263. }
  264. else if (dma_interrupt_flag_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_FLAG_FTF))
  265. {
  266. dma_interrupt_flag_clear(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_FLAG_FTF);
  267. g_scsi_dma.dma_fillto += DMA_BUF_SIZE / 2;
  268. }
  269. // Fill DMA buffer with data from current application buffer
  270. refill_dmabuf();
  271. check_dma_next_buffer();
  272. }
  273. // Check if enough data is available to continue DMA transfer
  274. extern "C" void SCSI_TIMER_DMACHB_IRQ()
  275. {
  276. azlog("Delaying DMA");
  277. // azdbg("DMA irq B, counts: ", dma_transfer_number_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA), " ",
  278. // dma_transfer_number_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB), " ",
  279. // TIMER_CNT(SCSI_TIMER));
  280. if (dma_interrupt_flag_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, DMA_FLAG_FTF))
  281. {
  282. dma_interrupt_flag_clear(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, DMA_FLAG_FTF);
  283. if (g_scsi_dma.bytes_app > g_scsi_dma.scheduled_dma)
  284. {
  285. if (g_scsi_dma.dma_idx < g_scsi_dma.dma_fillto)
  286. {
  287. // Previous request didn't have a complete buffer worth of data.
  288. // Refill the buffer and ensure that the first byte of the new data gets
  289. // written to outputs.
  290. __disable_irq();
  291. refill_dmabuf();
  292. __enable_irq();
  293. }
  294. // Verify the first byte of the new data has been written to outputs
  295. // It may have been updated after the DMA write occurred.
  296. __disable_irq();
  297. uint32_t first_data_idx = g_scsi_dma.dma_idx - (g_scsi_dma.bytes_dma - g_scsi_dma.scheduled_dma);
  298. uint32_t first_data = g_scsi_dma.dma_buf[first_data_idx & DMA_BUF_MASK];
  299. GPIO_BOP(SCSI_OUT_PORT) = first_data;
  300. __enable_irq();
  301. // Update the total number of bytes available for DMA
  302. uint32_t dma_to_schedule = g_scsi_dma.bytes_app - g_scsi_dma.scheduled_dma;
  303. dma_channel_disable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB);
  304. // DMA_CHCTL(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB) &= ~DMA_CHXCTL_CHEN;
  305. dma_transfer_number_config(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, dma_to_schedule);
  306. // DMA_CHCNT(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB) = dma_to_schedule;
  307. dma_channel_enable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB);
  308. // DMA_CHCTL(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB) |= DMA_CHXCTL_CHEN;
  309. g_scsi_dma.scheduled_dma += dma_to_schedule;
  310. }
  311. else
  312. {
  313. // No more data available
  314. stop_dma();
  315. }
  316. }
  317. }
  318. void scsi_accel_dma_startWrite(const uint8_t* data, uint32_t count, volatile int *resetFlag)
  319. {
  320. __disable_irq();
  321. if (g_scsi_dma_state == SCSIDMA_WRITE)
  322. {
  323. if (!g_scsi_dma.next_app_buf && data == g_scsi_dma.app_buf + g_scsi_dma.bytes_app)
  324. {
  325. // Combine with currently running request
  326. g_scsi_dma.bytes_app += count;
  327. count = 0;
  328. }
  329. else if (data == g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  330. {
  331. // Combine with queued request
  332. g_scsi_dma.next_app_bytes += count;
  333. count = 0;
  334. }
  335. else if (!g_scsi_dma.next_app_buf)
  336. {
  337. // Add as queued request
  338. g_scsi_dma.next_app_buf = (uint8_t*)data;
  339. g_scsi_dma.next_app_bytes = count;
  340. count = 0;
  341. }
  342. }
  343. __enable_irq();
  344. // Check if the request was combined
  345. if (count == 0) return;
  346. if (g_scsi_dma_state != SCSIDMA_IDLE)
  347. {
  348. // Wait for previous request to finish
  349. scsi_accel_dma_finishWrite(resetFlag);
  350. if (*resetFlag)
  351. {
  352. return;
  353. }
  354. }
  355. azdbg("Starting DMA write of ", (int)count, " bytes");
  356. scsi_dma_gpio_config(true);
  357. g_scsi_dma_state = SCSIDMA_WRITE;
  358. g_scsi_dma.app_buf = (uint8_t*)data;
  359. g_scsi_dma.dma_idx = 0;
  360. g_scsi_dma.dma_fillto = DMA_BUF_SIZE;
  361. g_scsi_dma.bytes_app = count;
  362. g_scsi_dma.bytes_dma = 0;
  363. g_scsi_dma.scheduled_dma = 0;
  364. g_scsi_dma.next_app_buf = NULL;
  365. g_scsi_dma.next_app_bytes = 0;
  366. refill_dmabuf();
  367. start_dma();
  368. }
  369. bool scsi_accel_dma_isWriteFinished(const uint8_t* data)
  370. {
  371. // Check if everything has completed
  372. if (g_scsi_dma_state == SCSIDMA_IDLE)
  373. {
  374. return true;
  375. }
  376. if (!data)
  377. return false;
  378. // Check if this data item is still in queue.
  379. __disable_irq();
  380. bool finished = true;
  381. if (data >= g_scsi_dma.app_buf + g_scsi_dma.bytes_dma &&
  382. data < g_scsi_dma.app_buf + g_scsi_dma.bytes_app)
  383. {
  384. finished = false; // In current transfer
  385. }
  386. else if (data >= g_scsi_dma.next_app_buf &&
  387. data < g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  388. {
  389. finished = false; // In queued transfer
  390. }
  391. __enable_irq();
  392. return finished;
  393. }
  394. void scsi_accel_dma_stopWrite()
  395. {
  396. stop_dma();
  397. scsi_dma_gpio_config(false);
  398. }
  399. void scsi_accel_dma_finishWrite(volatile int *resetFlag)
  400. {
  401. uint32_t start = millis();
  402. while (g_scsi_dma_state != SCSIDMA_IDLE && !*resetFlag)
  403. {
  404. if ((uint32_t)(millis() - start) > 5000)
  405. {
  406. azlog("scsi_accel_dma_finishWrite() timeout, DMA counts ",
  407. dma_transfer_number_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA), " ",
  408. dma_transfer_number_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB), " ",
  409. TIMER_CNT(SCSI_TIMER));
  410. *resetFlag = 1;
  411. break;
  412. }
  413. }
  414. scsi_accel_dma_stopWrite();
  415. }
  416. #endif