aux_serial: Cleaned up the buffer sizing macros
This commit is contained in:
parent
3afb041f03
commit
35ae0adcd1
|
@ -37,7 +37,7 @@
|
|||
#include "aux_serial.h"
|
||||
|
||||
#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][AUX_UART_BUFFER_SIZE];
|
||||
static uint8_t aux_serial_transmit_buffer_index;
|
||||
static uint8_t aux_serial_transmit_buffer_consumed;
|
||||
static bool aux_serial_transmit_complete = true;
|
||||
|
@ -86,7 +86,7 @@ void aux_serial_init(void)
|
|||
# define USBUSART_RDR USART_DR(USBUSART)
|
||||
#endif
|
||||
dma_channel_reset(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN);
|
||||
dma_set_peripheral_address(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN, (uint32_t)&USBUSART_TDR);
|
||||
dma_set_peripheral_address(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN, (uintptr_t)&USBUSART_TDR);
|
||||
dma_enable_memory_increment_mode(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN);
|
||||
dma_set_peripheral_size(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN, DMA_PSIZE_8BIT);
|
||||
dma_set_memory_size(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN, DMA_MSIZE_8BIT);
|
||||
|
@ -103,9 +103,9 @@ void aux_serial_init(void)
|
|||
|
||||
/* Setup USART RX DMA */
|
||||
dma_channel_reset(USBUSART_DMA_BUS, USBUSART_DMA_RX_CHAN);
|
||||
dma_set_peripheral_address(USBUSART_DMA_BUS, USBUSART_DMA_RX_CHAN, (uint32_t)&USBUSART_RDR);
|
||||
dma_set_memory_address(USBUSART_DMA_BUS, USBUSART_DMA_RX_CHAN, (uint32_t)buf_rx);
|
||||
dma_set_number_of_data(USBUSART_DMA_BUS, USBUSART_DMA_RX_CHAN, RX_FIFO_SIZE);
|
||||
dma_set_peripheral_address(USBUSART_DMA_BUS, USBUSART_DMA_RX_CHAN, (uintptr_t)&USBUSART_RDR);
|
||||
dma_set_memory_address(USBUSART_DMA_BUS, USBUSART_DMA_RX_CHAN, (uintptr_t)buf_rx);
|
||||
dma_set_number_of_data(USBUSART_DMA_BUS, USBUSART_DMA_RX_CHAN, AUX_UART_BUFFER_SIZE);
|
||||
dma_enable_memory_increment_mode(USBUSART_DMA_BUS, USBUSART_DMA_RX_CHAN);
|
||||
dma_enable_circular_mode(USBUSART_DMA_BUS, USBUSART_DMA_RX_CHAN);
|
||||
dma_set_peripheral_size(USBUSART_DMA_BUS, USBUSART_DMA_RX_CHAN, DMA_PSIZE_8BIT);
|
||||
|
@ -145,7 +145,7 @@ void aux_serial_init(void)
|
|||
usart_enable_rx_dma(USBUSART);
|
||||
}
|
||||
#elif defined(LM4F)
|
||||
static char aux_serial_transmit_buffer[FIFO_SIZE];
|
||||
static char aux_serial_transmit_buffer[AUX_UART_BUFFER_SIZE];
|
||||
|
||||
void aux_serial_init(void)
|
||||
{
|
||||
|
@ -436,12 +436,12 @@ void USBUART_ISR(void)
|
|||
/* If the next increment of rx_in would put it at the same point
|
||||
* as rx_out, the FIFO is considered full.
|
||||
*/
|
||||
if (((buf_rx_in + 1) % FIFO_SIZE) != buf_rx_out) {
|
||||
if (((buf_rx_in + 1) % AUX_UART_BUFFER_SIZE) != buf_rx_out) {
|
||||
/* insert into FIFO */
|
||||
buf_rx[buf_rx_in++] = c;
|
||||
|
||||
/* wrap out pointer */
|
||||
if (buf_rx_in >= FIFO_SIZE)
|
||||
if (buf_rx_in >= AUX_UART_BUFFER_SIZE)
|
||||
buf_rx_in = 0;
|
||||
} else
|
||||
flush = true;
|
||||
|
@ -463,14 +463,14 @@ void USBUART_ISR(void)
|
|||
packet_buf[packet_size++] = buf_rx[buf_out++];
|
||||
|
||||
/* wrap out pointer */
|
||||
if (buf_out >= FIFO_SIZE)
|
||||
if (buf_out >= AUX_UART_BUFFER_SIZE)
|
||||
buf_out = 0;
|
||||
}
|
||||
|
||||
/* advance fifo out pointer by amount written */
|
||||
buf_rx_out += usbd_ep_write_packet(usbdev,
|
||||
CDCACM_UART_ENDPOINT, packet_buf, packet_size);
|
||||
buf_rx_out %= FIFO_SIZE;
|
||||
buf_rx_out %= AUX_UART_BUFFER_SIZE;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -204,16 +204,16 @@ size_t debug_uart_write(const char *buf, const size_t len)
|
|||
|
||||
CM_ATOMIC_CONTEXT();
|
||||
|
||||
for (size_t i = 0; i < len && (usb_dbg_in + 1) % RX_FIFO_SIZE != usb_dbg_out; ++i) {
|
||||
for (size_t i = 0; i < len && (usb_dbg_in + 1) % AUX_UART_BUFFER_SIZE != usb_dbg_out; ++i) {
|
||||
if (buf[i] == '\n') {
|
||||
usb_dbg_buf[usb_dbg_in++] = '\r';
|
||||
usb_dbg_in %= RX_FIFO_SIZE;
|
||||
usb_dbg_in %= AUX_UART_BUFFER_SIZE;
|
||||
|
||||
if ((usb_dbg_in + 1) % RX_FIFO_SIZE == usb_dbg_out)
|
||||
if ((usb_dbg_in + 1) % AUX_UART_BUFFER_SIZE == usb_dbg_out)
|
||||
break;
|
||||
}
|
||||
usb_dbg_buf[usb_dbg_in++] = buf[i];
|
||||
usb_dbg_in %= RX_FIFO_SIZE;
|
||||
usb_dbg_in %= AUX_UART_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
debug_uart_run();
|
||||
|
@ -240,7 +240,8 @@ static void debug_uart_send_rx_packet(void)
|
|||
{
|
||||
aux_serial_receive_complete = false;
|
||||
/* Calculate writing position in the FIFO */
|
||||
const uint32_t buf_rx_in = (RX_FIFO_SIZE - dma_get_number_of_data(USBUSART_DMA_BUS, USBUSART_DMA_RX_CHAN)) % RX_FIFO_SIZE;
|
||||
const uint32_t buf_rx_in =
|
||||
(AUX_UART_BUFFER_SIZE - dma_get_number_of_data(USBUSART_DMA_BUS, USBUSART_DMA_RX_CHAN)) % AUX_UART_BUFFER_SIZE;
|
||||
|
||||
/* Forcibly empty fifo if no USB endpoint.
|
||||
* If fifo empty, nothing further to do. */
|
||||
|
@ -268,22 +269,22 @@ static void debug_uart_send_rx_packet(void)
|
|||
|
||||
#ifdef ENABLE_DEBUG
|
||||
/* Copy data from DEBUG FIFO into local usb packet buffer */
|
||||
packet_size = copy_from_fifo(packet_buf, usb_dbg_buf, usb_dbg_out, usb_dbg_in, CDCACM_PACKET_SIZE - 1, RX_FIFO_SIZE);
|
||||
packet_size = copy_from_fifo(packet_buf, usb_dbg_buf, usb_dbg_out, usb_dbg_in, CDCACM_PACKET_SIZE - 1, AUX_UART_BUFFER_SIZE);
|
||||
/* Send if buffer not empty */
|
||||
if (packet_size)
|
||||
{
|
||||
const uint16_t written = usbd_ep_write_packet(usbdev, CDCACM_UART_ENDPOINT, packet_buf, packet_size);
|
||||
usb_dbg_out = (usb_dbg_out + written) % RX_FIFO_SIZE;
|
||||
usb_dbg_out = (usb_dbg_out + written) % AUX_UART_BUFFER_SIZE;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Copy data from uart RX FIFO into local usb packet buffer */
|
||||
packet_size = copy_from_fifo(packet_buf, buf_rx, buf_rx_out, buf_rx_in, CDCACM_PACKET_SIZE - 1, RX_FIFO_SIZE);
|
||||
packet_size = copy_from_fifo(packet_buf, buf_rx, buf_rx_out, buf_rx_in, CDCACM_PACKET_SIZE - 1, AUX_UART_BUFFER_SIZE);
|
||||
|
||||
/* Advance fifo out pointer by amount written */
|
||||
const uint16_t written = usbd_ep_write_packet(usbdev, CDCACM_UART_ENDPOINT, packet_buf, packet_size);
|
||||
buf_rx_out = (buf_rx_out + written) % RX_FIFO_SIZE;
|
||||
buf_rx_out = (buf_rx_out + written) % AUX_UART_BUFFER_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -328,7 +329,7 @@ static void debug_uart_receive_callback(usbd_device *dev, uint8_t ep)
|
|||
|
||||
#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)
|
||||
if (AUX_UART_BUFFER_SIZE - aux_serial_transmit_buffer_fullness() < CDCACM_PACKET_SIZE)
|
||||
usbd_ep_nak_set(dev, ep, 1);
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -38,27 +38,26 @@ void debug_uart_run(void);
|
|||
#endif
|
||||
|
||||
#define USART_DMA_BUF_SIZE (1U << USART_DMA_BUF_SHIFT)
|
||||
#define RX_FIFO_SIZE (USART_DMA_BUF_SIZE)
|
||||
#define TX_BUF_SIZE (USART_DMA_BUF_SIZE)
|
||||
#define AUX_UART_BUFFER_SIZE (USART_DMA_BUF_SIZE)
|
||||
|
||||
/* RX Fifo buffer with space for copy fn overrun */
|
||||
extern char buf_rx[RX_FIFO_SIZE + sizeof(uint64_t)];
|
||||
extern char buf_rx[AUX_UART_BUFFER_SIZE + sizeof(uint64_t)];
|
||||
/* RX Fifo out pointer, writes assumed to be atomic */
|
||||
extern uint8_t buf_rx_out;
|
||||
|
||||
#ifdef ENABLE_DEBUG
|
||||
/* Debug Fifo buffer with space for copy fn overrun */
|
||||
extern char usb_dbg_buf[RX_FIFO_SIZE + sizeof(uint64_t)];
|
||||
extern char usb_dbg_buf[AUX_UART_BUFFER_SIZE + sizeof(uint64_t)];
|
||||
/* Debug Fifo in pointer */
|
||||
extern uint8_t usb_dbg_in;
|
||||
/* Debug Fifo out pointer */
|
||||
extern uint8_t usb_dbg_out;
|
||||
#endif
|
||||
#elif defined(LM4F)
|
||||
#define FIFO_SIZE 128
|
||||
#define AUX_UART_BUFFER_SIZE 128
|
||||
|
||||
/* RX Fifo buffer */
|
||||
extern char buf_rx[FIFO_SIZE];
|
||||
extern char buf_rx[AUX_UART_BUFFER_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 */
|
||||
|
|
|
@ -39,13 +39,13 @@
|
|||
#endif
|
||||
|
||||
/* RX Fifo buffer with space for copy fn overrun */
|
||||
char buf_rx[RX_FIFO_SIZE + sizeof(uint64_t)];
|
||||
char buf_rx[AUX_UART_BUFFER_SIZE + sizeof(uint64_t)];
|
||||
/* RX Fifo out pointer, writes assumed to be atomic */
|
||||
uint8_t buf_rx_out;
|
||||
|
||||
#ifdef ENABLE_DEBUG
|
||||
/* Debug Fifo buffer with space for copy fn overrun */
|
||||
char usb_dbg_buf[RX_FIFO_SIZE + sizeof(uint64_t)];
|
||||
char usb_dbg_buf[AUX_UART_BUFFER_SIZE + sizeof(uint64_t)];
|
||||
/* Debug Fifo in pointer */
|
||||
uint8_t usb_dbg_in;
|
||||
/* Debug Fifo out pointer */
|
||||
|
|
|
@ -30,10 +30,8 @@
|
|||
#include "general.h"
|
||||
#include "usb.h"
|
||||
|
||||
#define FIFO_SIZE 128
|
||||
|
||||
/* RX Fifo buffer */
|
||||
char buf_rx[FIFO_SIZE];
|
||||
char buf_rx[AUX_UART_BUFFER_SIZE];
|
||||
/* Fifo in pointer, writes assumed to be atomic, should be only incremented within RX ISR */
|
||||
uint8_t buf_rx_in;
|
||||
/* Fifo out pointer, writes assumed to be atomic, should be only incremented outside RX ISR */
|
||||
|
|
Loading…
Reference in New Issue