scsi_accel_dma.cpp 17 KB

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