From dd619ad808c1ab41ae1deda9e597dcbcdd5b887a Mon Sep 17 00:00:00 2001 From: dragonmux Date: Fri, 19 Aug 2022 11:14:59 +0100 Subject: [PATCH] aux_serial: Moved all the transmit logic into aux_serial.c and cleaned up in usb_serial.c --- src/platforms/common/aux_serial.c | 34 ++++++++++++++++++++++++- src/platforms/common/aux_serial.h | 9 ++++++- src/platforms/common/usb_serial.c | 41 ++++++------------------------- src/platforms/common/usbuart.h | 2 -- src/platforms/stm32/usbuart.c | 4 +-- 5 files changed, 49 insertions(+), 41 deletions(-) diff --git a/src/platforms/common/aux_serial.c b/src/platforms/common/aux_serial.c index 739dc75..84b41bc 100644 --- a/src/platforms/common/aux_serial.c +++ b/src/platforms/common/aux_serial.c @@ -37,6 +37,8 @@ #if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4) static char aux_serial_transmit_buffer[2U][TX_BUF_SIZE]; static uint8_t aux_serial_transmit_buffer_index; +/* Active buffer part used capacity */ +static uint8_t buf_tx_act_sz; #elif defined(LM4F) static char aux_serial_transmit_buffer[FIFO_SIZE]; #endif @@ -87,6 +89,11 @@ char *aux_serial_current_transmit_buffer(void) return aux_serial_transmit_buffer[aux_serial_transmit_buffer_index]; } +size_t aux_serial_transmit_buffer_fullness(void) +{ + return buf_tx_act_sz; +} + /* * Changes USBUSART TX buffer in which data is accumulated from USB. * Filled buffer is submitted to DMA for transfer. @@ -102,10 +109,35 @@ void aux_serial_switch_transmit_buffers(void) buf_tx_act_sz = 0; aux_serial_transmit_buffer_index ^= 1; } -#elif defined(LM4F) +void aux_serial_send(const size_t len) +{ + buf_tx_act_sz += len; + + /* If DMA is idle, schedule new transfer */ + if (len && tx_trfr_cplt) + { + tx_trfr_cplt = false; + aux_serial_switch_transmit_buffers(); + + /* Enable LED */ + usbuart_set_led_state(TX_LED_ACT, true); + } +} +#elif defined(LM4F) char *aux_serial_current_transmit_buffer(void) { return aux_serial_transmit_buffer; } + +size_t aux_serial_transmit_buffer_fullness(void) +{ + return 0; +} + +void aux_serial_send(const size_t len) +{ + for(size_t i = 0; i < len; ++i) + uart_send_blocking(USBUART, aux_serial_transmit_buffer[i]); +} #endif diff --git a/src/platforms/common/aux_serial.h b/src/platforms/common/aux_serial.h index ee10c94..c76e151 100644 --- a/src/platforms/common/aux_serial.h +++ b/src/platforms/common/aux_serial.h @@ -20,15 +20,22 @@ #ifndef AUX_SERIAL_H #define AUX_SERIAL_H +#include #include #include void aux_serial_init(void); void aux_serial_set_encoding(struct usb_cdc_line_coding *coding); -char *aux_serial_current_transmit_buffer(void); #if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4) void aux_serial_switch_transmit_buffers(void); #endif +/* Get the current transmit buffer to stage data into */ +char *aux_serial_current_transmit_buffer(void); +/* Get how full the current transmit buffer is */ +size_t aux_serial_transmit_buffer_fullness(void); +/* Send a number of bytes staged into the current transmit bufer */ +void aux_serial_send(size_t len); + #endif /*AUX_SERIAL_H*/ diff --git a/src/platforms/common/usb_serial.c b/src/platforms/common/usb_serial.c index 22d75b9..424ffe8 100644 --- a/src/platforms/common/usb_serial.c +++ b/src/platforms/common/usb_serial.c @@ -311,50 +311,23 @@ static void debug_uart_send_callback(usbd_device *dev, uint8_t ep) #ifndef ENABLE_RTT static void debug_uart_receive_callback(usbd_device *dev, uint8_t ep) { - char *const transmit_buffer = aux_serial_current_transmit_buffer() -#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4) - + buf_tx_act_sz -#endif - ; - + char *const transmit_buffer = aux_serial_current_transmit_buffer() + aux_serial_transmit_buffer_fullness(); const uint16_t len = usbd_ep_read_packet(dev, ep, transmit_buffer, CDCACM_PACKET_SIZE); -#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4) - usbd_ep_nak_set(dev, ep, 1); - #if defined(BLACKMAGIC) /* Don't bother if uart is disabled. * This will be the case on mini while we're being debugged. */ - if(!(RCC_APB2ENR & RCC_APB2ENR_USART1EN) && - !(RCC_APB1ENR & RCC_APB1ENR_USART2EN)) - { - usbd_ep_nak_set(dev, ep, 0); + if (!(RCC_APB2ENR & RCC_APB2ENR_USART1EN) && !(RCC_APB1ENR & RCC_APB1ENR_USART2EN)) return; - } #endif - if (len) - { - buf_tx_act_sz += len; + aux_serial_send(len); - /* If DMA is idle, schedule new transfer */ - if (tx_trfr_cplt) - { - tx_trfr_cplt = false; - aux_serial_switch_transmit_buffers(); - - /* Enable LED */ - usbuart_set_led_state(TX_LED_ACT, true); - } - } - - /* Enable USBUART TX packet reception if buffer has enough space */ - if (TX_BUF_SIZE - buf_tx_act_sz >= CDCACM_PACKET_SIZE) - usbd_ep_nak_set(dev, ep, 0); -#elif defined(LM4F) - for(uint16_t i = 0; i < len; i++) - uart_send_blocking(USBUART, transmit_buffer[i]); +#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4) + /* Disable USBUART TX packet reception if buffer does not have enough space */ + if (TX_BUF_SIZE - aux_serial_transmit_buffer_fullness() < CDCACM_PACKET_SIZE) + usbd_ep_nak_set(dev, ep, 1); #endif } #endif diff --git a/src/platforms/common/usbuart.h b/src/platforms/common/usbuart.h index cd769d3..564d5be 100644 --- a/src/platforms/common/usbuart.h +++ b/src/platforms/common/usbuart.h @@ -41,8 +41,6 @@ void debug_uart_run(void); #define RX_FIFO_SIZE (USART_DMA_BUF_SIZE) #define TX_BUF_SIZE (USART_DMA_BUF_SIZE) -/* Active buffer part used capacity */ -extern uint8_t buf_tx_act_sz; /* TX transfer complete */ extern bool tx_trfr_cplt; /* RX Fifo buffer with space for copy fn overrun */ diff --git a/src/platforms/stm32/usbuart.c b/src/platforms/stm32/usbuart.c index 164f9d3..0e05b8c 100644 --- a/src/platforms/stm32/usbuart.c +++ b/src/platforms/stm32/usbuart.c @@ -49,8 +49,6 @@ #define DMA_CGIF DMA_IFCR_CGIF_BIT #endif -/* Active buffer part used capacity */ -uint8_t buf_tx_act_sz; /* TX transfer complete */ bool tx_trfr_cplt = true; /* RX Fifo buffer with space for copy fn overrun */ @@ -257,7 +255,7 @@ void USBUSART2_ISR(void) /* If new buffer is ready, continue transmission. \ * Otherwise report transfer completion. \ */ \ - if (buf_tx_act_sz) \ + if (aux_serial_transmit_buffer_fullness()) \ { \ aux_serial_switch_transmit_buffers(); \ usbd_ep_nak_set(usbdev, CDCACM_UART_ENDPOINT, 0); \