usbuart: Moved usbuart_usb_out_cb() into usb_serial.c

This commit is contained in:
dragonmux 2022-08-19 02:30:45 +01:00 committed by Piotr Esden-Tempski
parent 9afa946703
commit 693c408b98
4 changed files with 84 additions and 72 deletions

View File

@ -52,13 +52,17 @@
#include <libopencm3/cm3/cortex.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/usb/cdc.h>
#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/dma.h>
#endif
static bool gdb_uart_dtr = true;
static void usb_serial_set_state(usbd_device *dev, uint16_t iface, uint8_t ep);
static void debug_uart_send_callback(usbd_device *dev, uint8_t ep);
static void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep);
#ifdef ENABLE_DEBUG
/*
@ -304,6 +308,59 @@ static void debug_uart_send_callback(usbd_device *dev, uint8_t ep)
#endif
}
#ifndef ENABLE_RTT
static void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep)
{
(void)ep;
#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
usbd_ep_nak_set(dev, CDCACM_UART_ENDPOINT, 1);
/* Read new packet directly into TX buffer */
char *const tx_buf_ptr = &buf_tx[buf_tx_act_idx * TX_BUF_SIZE];
const uint16_t len = usbd_ep_read_packet(dev, CDCACM_UART_ENDPOINT, tx_buf_ptr + buf_tx_act_sz, CDCACM_PACKET_SIZE);
#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, CDCACM_UART_ENDPOINT, 0);
return;
}
#endif
if (len)
{
buf_tx_act_sz += len;
/* If DMA is idle, schedule new transfer */
if (tx_trfr_cplt)
{
tx_trfr_cplt = false;
usbuart_change_dma_tx_buf();
/* 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, CDCACM_UART_ENDPOINT, 0);
#elif defined(LM4F)
char buf[CDCACM_PACKET_SIZE];
int len = usbd_ep_read_packet(dev, CDCACM_UART_ENDPOINT,
buf, CDCACM_PACKET_SIZE);
for(int i = 0; i < len; i++)
uart_send_blocking(USBUART, buf[i]);
#endif
}
#endif
/*
* newlib defines _write as a weak link'd function for user code to override.
*

View File

@ -20,18 +20,16 @@
#ifndef __USBUART_H
#define __USBUART_H
#include <libopencm3/usb/usbd.h>
#include "general.h"
void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep);
void usbuart_set_led_state(uint8_t ledn, bool state);
void usbuart_change_dma_tx_buf(void);
void debug_uart_run(void);
#define TX_LED_ACT (1 << 0)
#define RX_LED_ACT (1 << 1)
#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
/* F072 with st_usbfs_v2_usb_drive drops characters at the 64 byte boundary!*/
#if !defined(USART_DMA_BUF_SIZE)
# define USART_DMA_BUF_SIZE 128
@ -39,6 +37,12 @@ void debug_uart_run(void);
#define RX_FIFO_SIZE (USART_DMA_BUF_SIZE)
#define TX_BUF_SIZE (USART_DMA_BUF_SIZE)
/* TX double buffer */
extern char buf_tx[TX_BUF_SIZE * 2];
/* Active buffer part idx */
extern uint8_t buf_tx_act_idx;
/* 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 */
@ -56,5 +60,15 @@ extern uint8_t usb_dbg_in;
/* Debug Fifo out pointer */
extern uint8_t usb_dbg_out;
#endif
#elif defined(LM4F)
#define FIFO_SIZE 128
/* RX Fifo buffer */
extern char buf_rx[FIFO_SIZE];
/* Fifo in pointer, writes assumed to be atomic, should be only incremented within RX ISR */
extern uint8_t buf_rx_in;
/* Fifo out pointer, writes assumed to be atomic, should be only incremented outside RX ISR */
extern uint8_t buf_rx_out;
#endif
#endif

View File

@ -50,11 +50,11 @@
#endif
/* TX double buffer */
static uint8_t buf_tx[TX_BUF_SIZE * 2];
char buf_tx[TX_BUF_SIZE * 2];
/* Active buffer part idx */
static uint8_t buf_tx_act_idx;
uint8_t buf_tx_act_idx;
/* Active buffer part used capacity */
static uint8_t buf_tx_act_sz;
uint8_t buf_tx_act_sz;
/* TX transfer complete */
bool tx_trfr_cplt = true;
/* RX Fifo buffer with space for copy fn overrun */
@ -186,13 +186,13 @@ void aux_serial_init(void)
* Changes USBUSART TX buffer in which data is accumulated from USB.
* Filled buffer is submitted to DMA for transfer.
*/
static void usbuart_change_dma_tx_buf(void)
void usbuart_change_dma_tx_buf(void)
{
/* Select buffer for transmission */
uint8_t *const tx_buf_ptr = &buf_tx[buf_tx_act_idx * TX_BUF_SIZE];
char *const tx_buf_ptr = &buf_tx[buf_tx_act_idx * TX_BUF_SIZE];
/* Configure DMA */
dma_set_memory_address(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN, (uint32_t)tx_buf_ptr);
dma_set_memory_address(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN, (uintptr_t)tx_buf_ptr);
dma_set_number_of_data(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN, buf_tx_act_sz);
dma_enable_channel(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN);
@ -201,51 +201,6 @@ static void usbuart_change_dma_tx_buf(void)
buf_tx_act_idx ^= 1;
}
#ifndef ENABLE_RTT
void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep)
{
(void)ep;
usbd_ep_nak_set(dev, CDCACM_UART_ENDPOINT, 1);
/* Read new packet directly into TX buffer */
uint8_t *const tx_buf_ptr = &buf_tx[buf_tx_act_idx * TX_BUF_SIZE];
const uint16_t len = usbd_ep_read_packet(dev, CDCACM_UART_ENDPOINT,
tx_buf_ptr + buf_tx_act_sz, CDCACM_PACKET_SIZE);
#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, CDCACM_UART_ENDPOINT, 0);
return;
}
#endif
if (len)
{
buf_tx_act_sz += len;
/* If DMA is idle, schedule new transfer */
if (tx_trfr_cplt)
{
tx_trfr_cplt = false;
usbuart_change_dma_tx_buf();
/* 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, CDCACM_UART_ENDPOINT, 0);
}
#endif
#if defined(USART_ICR)
#define USBUSART_ISR_TEMPLATE(USART, DMA_IRQ) do { \
nvic_disable_irq(DMA_IRQ); \

View File

@ -33,11 +33,11 @@
#define FIFO_SIZE 128
/* RX Fifo buffer */
static uint8_t buf_rx[FIFO_SIZE];
char buf_rx[FIFO_SIZE];
/* Fifo in pointer, writes assumed to be atomic, should be only incremented within RX ISR */
static uint8_t buf_rx_in;
uint8_t buf_rx_in;
/* Fifo out pointer, writes assumed to be atomic, should be only incremented outside RX ISR */
static uint8_t buf_rx_out;
uint8_t buf_rx_out;
void aux_serial_init(void)
{
@ -76,20 +76,6 @@ void aux_serial_init(void)
nvic_enable_irq(USBUART_IRQ);
}
#ifndef ENABLE_RTT
void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep)
{
(void)ep;
char buf[CDCACM_PACKET_SIZE];
int len = usbd_ep_read_packet(dev, CDCACM_UART_ENDPOINT,
buf, CDCACM_PACKET_SIZE);
for(int i = 0; i < len; i++)
uart_send_blocking(USBUART, buf[i]);
}
#endif
/*
* Read a character from the UART RX and stuff it in a software FIFO.
* Allowed to read from FIFO out pointer, but not write to it.