diff --git a/src/platforms/common/aux_serial.c b/src/platforms/common/aux_serial.c index be80055..3d9708f 100644 --- a/src/platforms/common/aux_serial.c +++ b/src/platforms/common/aux_serial.c @@ -40,6 +40,7 @@ static char aux_serial_transmit_buffer[2U][TX_BUF_SIZE]; static uint8_t aux_serial_transmit_buffer_index; static uint8_t aux_serial_transmit_buffer_consumed; +static bool aux_serial_transmit_complete = true; #ifdef DMA_STREAM0 #define dma_channel_reset(dma, channel) dma_stream_reset(dma, channel) @@ -285,6 +286,29 @@ static void aux_serial_receive_isr(const uint32_t usart, const uint8_t dma_irq) nvic_enable_irq(dma_irq); } +static void aux_serial_dma_transmit_isr(const uint8_t dma_tx_channel) +{ + nvic_disable_irq(USB_IRQ); + + /* Stop DMA */ + dma_disable_channel(USBUSART_DMA_BUS, dma_tx_channel); + dma_clear_interrupt_flags(USBUSART_DMA_BUS, dma_tx_channel, DMA_CGIF); + + /* + * If a new buffer is ready, continue transmission. + * Otherwise we report the transfer has completed. + */ + if (aux_serial_transmit_buffer_fullness()) { + aux_serial_switch_transmit_buffers(); + usbd_ep_nak_set(usbdev, CDCACM_UART_ENDPOINT, 0); + } else { + usbuart_set_led_state(TX_LED_ACT, false); + aux_serial_transmit_complete = true; + } + + nvic_enable_irq(USB_IRQ); +} + #if defined(USBUSART_ISR) void USBUSART_ISR(void) { @@ -318,6 +342,27 @@ void USBUSART2_ISR(void) } #endif +#if defined(USBUSART_DMA_TX_ISR) +void USBUSART_DMA_TX_ISR(void) +{ + aux_serial_dma_transmit_isr(USBUSART_DMA_TX_CHAN); +} +#endif + +#if defined(USBUSART1_DMA_TX_ISR) +void USBUSART1_DMA_TX_ISR(void) +{ + aux_serial_dma_transmit_isr(USBUSART1_DMA_TX_CHAN); +} +#endif + +#if defined(USBUSART2_DMA_TX_ISR) +void USBUSART2_DMA_TX_ISR(void) +{ + aux_serial_dma_transmit_isr(USBUSART2_DMA_TX_CHAN); +} +#endif + #elif defined(LM4F) char *aux_serial_current_transmit_buffer(void) { diff --git a/src/platforms/common/usbuart.h b/src/platforms/common/usbuart.h index d465b39..d7f32a4 100644 --- a/src/platforms/common/usbuart.h +++ b/src/platforms/common/usbuart.h @@ -41,7 +41,6 @@ void debug_uart_run(void); #define RX_FIFO_SIZE (USART_DMA_BUF_SIZE) #define TX_BUF_SIZE (USART_DMA_BUF_SIZE) -extern bool aux_serial_transmit_complete; /* RX Fifo buffer with space for copy fn overrun */ extern char buf_rx[RX_FIFO_SIZE + sizeof(uint64_t)]; /* RX Fifo out pointer, writes assumed to be atomic */ diff --git a/src/platforms/stm32/usbuart.c b/src/platforms/stm32/usbuart.c index f953ddb..a971f6c 100644 --- a/src/platforms/stm32/usbuart.c +++ b/src/platforms/stm32/usbuart.c @@ -38,8 +38,6 @@ #define DMA_CGIF DMA_IFCR_CGIF_BIT #endif -/* TX transfer complete */ -bool aux_serial_transmit_complete = true; /* RX Fifo buffer with space for copy fn overrun */ char buf_rx[RX_FIFO_SIZE + sizeof(uint64_t)]; /* RX Fifo out pointer, writes assumed to be atomic */ @@ -78,51 +76,6 @@ void usbuart_set_led_state(uint8_t ledn, bool state) } } -#define USBUSART_DMA_TX_ISR_TEMPLATE(DMA_TX_CHAN) do { \ - nvic_disable_irq(USB_IRQ); \ - \ - /* Stop DMA */ \ - dma_disable_channel(USBUSART_DMA_BUS, DMA_TX_CHAN); \ - dma_clear_interrupt_flags(USBUSART_DMA_BUS, DMA_TX_CHAN, DMA_CGIF); \ - \ - /* If new buffer is ready, continue transmission. \ - * Otherwise report transfer completion. \ - */ \ - if (aux_serial_transmit_buffer_fullness()) \ - { \ - aux_serial_switch_transmit_buffers(); \ - usbd_ep_nak_set(usbdev, CDCACM_UART_ENDPOINT, 0); \ - } \ - else \ - { \ - usbuart_set_led_state(TX_LED_ACT, false); \ - aux_serial_transmit_complete = true; \ - } \ - \ - nvic_enable_irq(USB_IRQ); \ -} while(0) - -#if defined(USBUSART_DMA_TX_ISR) -void USBUSART_DMA_TX_ISR(void) -{ - USBUSART_DMA_TX_ISR_TEMPLATE(USBUSART_DMA_TX_CHAN); -} -#endif - -#if defined(USBUSART1_DMA_TX_ISR) -void USBUSART1_DMA_TX_ISR(void) -{ - USBUSART_DMA_TX_ISR_TEMPLATE(USBUSART1_DMA_TX_CHAN); -} -#endif - -#if defined(USBUSART2_DMA_TX_ISR) -void USBUSART2_DMA_TX_ISR(void) -{ - USBUSART_DMA_TX_ISR_TEMPLATE(USBUSART2_DMA_TX_CHAN); -} -#endif - #define USBUSART_DMA_RX_ISR_TEMPLATE(USART_IRQ, DMA_RX_CHAN) do { \ nvic_disable_irq(USART_IRQ); \ \