aux_serial: Moved all the transmit logic into aux_serial.c and cleaned up in usb_serial.c

This commit is contained in:
dragonmux 2022-08-19 11:14:59 +01:00 committed by Piotr Esden-Tempski
parent b94476841f
commit dd619ad808
5 changed files with 49 additions and 41 deletions

View File

@ -37,6 +37,8 @@
#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4) #if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
static char aux_serial_transmit_buffer[2U][TX_BUF_SIZE]; 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_index;
/* Active buffer part used capacity */
static uint8_t buf_tx_act_sz;
#elif defined(LM4F) #elif defined(LM4F)
static char aux_serial_transmit_buffer[FIFO_SIZE]; static char aux_serial_transmit_buffer[FIFO_SIZE];
#endif #endif
@ -87,6 +89,11 @@ char *aux_serial_current_transmit_buffer(void)
return aux_serial_transmit_buffer[aux_serial_transmit_buffer_index]; 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. * Changes USBUSART TX buffer in which data is accumulated from USB.
* Filled buffer is submitted to DMA for transfer. * Filled buffer is submitted to DMA for transfer.
@ -102,10 +109,35 @@ void aux_serial_switch_transmit_buffers(void)
buf_tx_act_sz = 0; buf_tx_act_sz = 0;
aux_serial_transmit_buffer_index ^= 1; 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) char *aux_serial_current_transmit_buffer(void)
{ {
return aux_serial_transmit_buffer; 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 #endif

View File

@ -20,15 +20,22 @@
#ifndef AUX_SERIAL_H #ifndef AUX_SERIAL_H
#define AUX_SERIAL_H #define AUX_SERIAL_H
#include <stddef.h>
#include <libopencm3/usb/usbd.h> #include <libopencm3/usb/usbd.h>
#include <libopencm3/usb/cdc.h> #include <libopencm3/usb/cdc.h>
void aux_serial_init(void); void aux_serial_init(void);
void aux_serial_set_encoding(struct usb_cdc_line_coding *coding); 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) #if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
void aux_serial_switch_transmit_buffers(void); void aux_serial_switch_transmit_buffers(void);
#endif #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*/ #endif /*AUX_SERIAL_H*/

View File

@ -311,50 +311,23 @@ static void debug_uart_send_callback(usbd_device *dev, uint8_t ep)
#ifndef ENABLE_RTT #ifndef ENABLE_RTT
static void debug_uart_receive_callback(usbd_device *dev, uint8_t ep) static void debug_uart_receive_callback(usbd_device *dev, uint8_t ep)
{ {
char *const transmit_buffer = aux_serial_current_transmit_buffer() char *const transmit_buffer = aux_serial_current_transmit_buffer() + aux_serial_transmit_buffer_fullness();
#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
+ buf_tx_act_sz
#endif
;
const uint16_t len = usbd_ep_read_packet(dev, ep, transmit_buffer, CDCACM_PACKET_SIZE); 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) #if defined(BLACKMAGIC)
/* Don't bother if uart is disabled. /* Don't bother if uart is disabled.
* This will be the case on mini while we're being debugged. * This will be the case on mini while we're being debugged.
*/ */
if(!(RCC_APB2ENR & RCC_APB2ENR_USART1EN) && if (!(RCC_APB2ENR & RCC_APB2ENR_USART1EN) && !(RCC_APB1ENR & RCC_APB1ENR_USART2EN))
!(RCC_APB1ENR & RCC_APB1ENR_USART2EN))
{
usbd_ep_nak_set(dev, ep, 0);
return; return;
}
#endif #endif
if (len) aux_serial_send(len);
{
buf_tx_act_sz += len;
/* If DMA is idle, schedule new transfer */ #if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
if (tx_trfr_cplt) /* Disable USBUART TX packet reception if buffer does not have enough space */
{ if (TX_BUF_SIZE - aux_serial_transmit_buffer_fullness() < CDCACM_PACKET_SIZE)
tx_trfr_cplt = false; usbd_ep_nak_set(dev, ep, 1);
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]);
#endif #endif
} }
#endif #endif

View File

@ -41,8 +41,6 @@ void debug_uart_run(void);
#define RX_FIFO_SIZE (USART_DMA_BUF_SIZE) #define RX_FIFO_SIZE (USART_DMA_BUF_SIZE)
#define TX_BUF_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 */ /* TX transfer complete */
extern bool tx_trfr_cplt; extern bool tx_trfr_cplt;
/* RX Fifo buffer with space for copy fn overrun */ /* RX Fifo buffer with space for copy fn overrun */

View File

@ -49,8 +49,6 @@
#define DMA_CGIF DMA_IFCR_CGIF_BIT #define DMA_CGIF DMA_IFCR_CGIF_BIT
#endif #endif
/* Active buffer part used capacity */
uint8_t buf_tx_act_sz;
/* TX transfer complete */ /* TX transfer complete */
bool tx_trfr_cplt = true; bool tx_trfr_cplt = true;
/* RX Fifo buffer with space for copy fn overrun */ /* RX Fifo buffer with space for copy fn overrun */
@ -257,7 +255,7 @@ void USBUSART2_ISR(void)
/* If new buffer is ready, continue transmission. \ /* If new buffer is ready, continue transmission. \
* Otherwise report transfer completion. \ * Otherwise report transfer completion. \
*/ \ */ \
if (buf_tx_act_sz) \ if (aux_serial_transmit_buffer_fullness()) \
{ \ { \
aux_serial_switch_transmit_buffers(); \ aux_serial_switch_transmit_buffers(); \
usbd_ep_nak_set(usbdev, CDCACM_UART_ENDPOINT, 0); \ usbd_ep_nak_set(usbdev, CDCACM_UART_ENDPOINT, 0); \