1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-13 11:29:58 +03:00

Refactor USB serial ring buffer code

This commit is contained in:
Steve Evans 2022-07-21 23:19:02 +01:00
parent 8b258bc2ad
commit a3a3b74fd5
4 changed files with 97 additions and 139 deletions

View file

@ -184,9 +184,9 @@ __ALIGN_BEGIN uint8_t APP_Rx_Buffer [APP_RX_DATA_SIZE] __ALIGN_END ;
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */ #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
__ALIGN_BEGIN uint8_t CmdBuff[CDC_CMD_PACKET_SZE] __ALIGN_END ; __ALIGN_BEGIN uint8_t CmdBuff[CDC_CMD_PACKET_SZE] __ALIGN_END ;
uint32_t APP_Rx_ptr_in = 0; volatile uint32_t APP_Rx_ptr_in = 0;
uint32_t APP_Rx_ptr_out = 0; volatile uint32_t APP_Rx_ptr_out = 0;
uint32_t APP_Rx_length = 0; volatile uint32_t APP_Rx_length = 0;
uint8_t USB_Tx_State = USB_CDC_IDLE; uint8_t USB_Tx_State = USB_CDC_IDLE;
@ -619,7 +619,6 @@ uint8_t usbd_cdc_DataIn (void *pdev, uint8_t epnum)
{ {
(void)pdev; (void)pdev;
(void)epnum; (void)epnum;
uint16_t USB_Tx_ptr;
uint16_t USB_Tx_length; uint16_t USB_Tx_length;
if (USB_Tx_State == USB_CDC_BUSY) if (USB_Tx_State == USB_CDC_BUSY)
@ -627,46 +626,32 @@ uint8_t usbd_cdc_DataIn (void *pdev, uint8_t epnum)
if (APP_Rx_length == 0) if (APP_Rx_length == 0)
{ {
USB_Tx_State = USB_CDC_IDLE; USB_Tx_State = USB_CDC_IDLE;
} } else {
else if (APP_Rx_length > CDC_DATA_IN_PACKET_SIZE) {
{
if (APP_Rx_length > CDC_DATA_IN_PACKET_SIZE){
USB_Tx_ptr = APP_Rx_ptr_out;
USB_Tx_length = CDC_DATA_IN_PACKET_SIZE; USB_Tx_length = CDC_DATA_IN_PACKET_SIZE;
} else {
APP_Rx_ptr_out += CDC_DATA_IN_PACKET_SIZE;
APP_Rx_length -= CDC_DATA_IN_PACKET_SIZE;
}
else
{
USB_Tx_ptr = APP_Rx_ptr_out;
USB_Tx_length = APP_Rx_length; USB_Tx_length = APP_Rx_length;
APP_Rx_ptr_out += APP_Rx_length; if (USB_Tx_length == CDC_DATA_IN_PACKET_SIZE) {
APP_Rx_length = 0;
if(USB_Tx_length == CDC_DATA_IN_PACKET_SIZE)
{
USB_Tx_State = USB_CDC_ZLP; USB_Tx_State = USB_CDC_ZLP;
} }
} }
/* Prepare the available data buffer to be sent on IN endpoint */ /* Prepare the available data buffer to be sent on IN endpoint */
DCD_EP_Tx (pdev, DCD_EP_Tx(pdev, CDC_IN_EP, (uint8_t*)&APP_Rx_Buffer[APP_Rx_ptr_out], USB_Tx_length);
CDC_IN_EP,
(uint8_t*)&APP_Rx_Buffer[USB_Tx_ptr], // Advance the out pointer
USB_Tx_length); APP_Rx_ptr_out = (APP_Rx_ptr_out + USB_Tx_length) % APP_RX_DATA_SIZE;;
APP_Rx_length -= USB_Tx_length;
return USBD_OK; return USBD_OK;
} }
} }
/* Avoid any asynchronous transfer during ZLP */ /* Avoid any asynchronous transfer during ZLP */
if (USB_Tx_State == USB_CDC_ZLP) if (USB_Tx_State == USB_CDC_ZLP) {
{
/*Send ZLP to indicate the end of the current transfer */ /*Send ZLP to indicate the end of the current transfer */
DCD_EP_Tx (pdev, DCD_EP_Tx(pdev, CDC_IN_EP, NULL, 0);
CDC_IN_EP,
NULL,
0);
USB_Tx_State = USB_CDC_IDLE; USB_Tx_State = USB_CDC_IDLE;
} }
@ -731,66 +716,48 @@ uint8_t usbd_cdc_SOF (void *pdev)
*/ */
static void Handle_USBAsynchXfer (void *pdev) static void Handle_USBAsynchXfer (void *pdev)
{ {
uint16_t USB_Tx_ptr;
uint16_t USB_Tx_length; uint16_t USB_Tx_length;
if(USB_Tx_State == USB_CDC_IDLE) if (USB_Tx_State == USB_CDC_IDLE) {
{ if (APP_Rx_ptr_out == APP_Rx_ptr_in) {
if (APP_Rx_ptr_out == APP_RX_DATA_SIZE) // Ring buffer is empty
{
APP_Rx_ptr_out = 0;
}
if(APP_Rx_ptr_out == APP_Rx_ptr_in)
{
USB_Tx_State = USB_CDC_IDLE;
return; return;
} }
if(APP_Rx_ptr_out > APP_Rx_ptr_in) /* rollback */ if (APP_Rx_ptr_out > APP_Rx_ptr_in) {
{ // Transfer bytes up to the end of the ring buffer
APP_Rx_length = APP_RX_DATA_SIZE - APP_Rx_ptr_out; APP_Rx_length = APP_RX_DATA_SIZE - APP_Rx_ptr_out;
} else {
} // Transfer all bytes in ring buffer
else
{
APP_Rx_length = APP_Rx_ptr_in - APP_Rx_ptr_out; APP_Rx_length = APP_Rx_ptr_in - APP_Rx_ptr_out;
} }
#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
// Only transfer whole 32 bit words of data
APP_Rx_length &= ~0x03; APP_Rx_length &= ~0x03;
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */ #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
if (APP_Rx_length > CDC_DATA_IN_PACKET_SIZE) if (APP_Rx_length > CDC_DATA_IN_PACKET_SIZE) {
{
USB_Tx_ptr = APP_Rx_ptr_out;
USB_Tx_length = CDC_DATA_IN_PACKET_SIZE; USB_Tx_length = CDC_DATA_IN_PACKET_SIZE;
APP_Rx_ptr_out += CDC_DATA_IN_PACKET_SIZE;
APP_Rx_length -= CDC_DATA_IN_PACKET_SIZE;
USB_Tx_State = USB_CDC_BUSY; USB_Tx_State = USB_CDC_BUSY;
} } else {
else
{
USB_Tx_ptr = APP_Rx_ptr_out;
USB_Tx_length = APP_Rx_length; USB_Tx_length = APP_Rx_length;
APP_Rx_ptr_out += APP_Rx_length; if (USB_Tx_length == CDC_DATA_IN_PACKET_SIZE) {
APP_Rx_length = 0;
if(USB_Tx_length == CDC_DATA_IN_PACKET_SIZE)
{
USB_Tx_State = USB_CDC_ZLP; USB_Tx_State = USB_CDC_ZLP;
} } else {
else
{
USB_Tx_State = USB_CDC_BUSY; USB_Tx_State = USB_CDC_BUSY;
} }
} }
DCD_EP_Tx (pdev, DCD_EP_Tx (pdev,
CDC_IN_EP, CDC_IN_EP,
(uint8_t*)&APP_Rx_Buffer[USB_Tx_ptr], (uint8_t*)&APP_Rx_Buffer[APP_Rx_ptr_out],
USB_Tx_length); USB_Tx_length);
APP_Rx_ptr_out = (APP_Rx_ptr_out + USB_Tx_length) % APP_RX_DATA_SIZE;
APP_Rx_length -= USB_Tx_length;
} }
} }

View file

@ -183,7 +183,7 @@ static serialPort_t *cliPort = NULL;
static bufWriter_t *cliWriter = NULL; static bufWriter_t *cliWriter = NULL;
static bufWriter_t *cliErrorWriter = NULL; static bufWriter_t *cliErrorWriter = NULL;
static uint8_t cliWriteBuffer[sizeof(*cliWriter) + CLI_OUT_BUFFER_SIZE]; static uint8_t cliWriteBuffer[sizeof(bufWriter_t) + CLI_OUT_BUFFER_SIZE];
static char cliBuffer[CLI_IN_BUFFER_SIZE]; static char cliBuffer[CLI_IN_BUFFER_SIZE];
static uint32_t bufferIndex = 0; static uint32_t bufferIndex = 0;

View file

@ -409,17 +409,11 @@ uint32_t CDC_Receive_BytesAvailable(void)
uint32_t CDC_Send_FreeBytes(void) uint32_t CDC_Send_FreeBytes(void)
{ {
/* // return the bytes free in the circular buffer
return the bytes free in the circular buffer
functionally equivalent to:
(APP_Rx_ptr_out > APP_Rx_ptr_in ? APP_Rx_ptr_out - APP_Rx_ptr_in : APP_RX_DATA_SIZE - APP_Rx_ptr_in + APP_Rx_ptr_in)
but without the impact of the condition check.
*/
uint32_t freeBytes; uint32_t freeBytes;
ATOMIC_BLOCK(NVIC_BUILD_PRIORITY(6, 0)) { ATOMIC_BLOCK(NVIC_BUILD_PRIORITY(6, 0)) {
freeBytes = ((UserTxBufPtrOut - UserTxBufPtrIn) + (-((int)(UserTxBufPtrOut <= UserTxBufPtrIn)) & APP_TX_DATA_SIZE)) - 1; freeBytes = APP_RX_DATA_SIZE - rxAvailable;
} }
return freeBytes; return freeBytes;

View file

@ -25,8 +25,11 @@
#include "platform.h" #include "platform.h"
#include "build/atomic.h"
#include "usbd_cdc_vcp.h" #include "usbd_cdc_vcp.h"
#include "stm32f4xx_conf.h" #include "stm32f4xx_conf.h"
#include "drivers/nvic.h"
#include "drivers/time.h" #include "drivers/time.h"
#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
@ -44,11 +47,11 @@ __IO uint32_t bDeviceState = UNCONNECTED; /* USB device status */
/* This is the buffer for data received from the MCU to APP (i.e. MCU TX, APP RX) */ /* This is the buffer for data received from the MCU to APP (i.e. MCU TX, APP RX) */
extern uint8_t APP_Rx_Buffer[]; extern uint8_t APP_Rx_Buffer[];
extern uint32_t APP_Rx_ptr_out; extern volatile uint32_t APP_Rx_ptr_out;
/* Increment this buffer position or roll it back to /* Increment this buffer position or roll it back to
start address when writing received data start address when writing received data
in the buffer APP_Rx_Buffer. */ in the buffer APP_Rx_Buffer. */
extern uint32_t APP_Rx_ptr_in; extern volatile uint32_t APP_Rx_ptr_in;
/* /*
APP TX is the circular buffer for data that is transmitted from the APP (host) APP TX is the circular buffer for data that is transmitted from the APP (host)
@ -188,14 +191,7 @@ uint32_t CDC_Send_DATA(const uint8_t *ptrBuffer, uint32_t sendLength)
uint32_t CDC_Send_FreeBytes(void) uint32_t CDC_Send_FreeBytes(void)
{ {
/* return APP_RX_DATA_SIZE - CDC_Receive_BytesAvailable();;
return the bytes free in the circular buffer
functionally equivalent to:
(APP_Rx_ptr_out > APP_Rx_ptr_in ? APP_Rx_ptr_out - APP_Rx_ptr_in : APP_RX_DATA_SIZE - APP_Rx_ptr_in + APP_Rx_ptr_in)
but without the impact of the condition check.
*/
return ((APP_Rx_ptr_out - APP_Rx_ptr_in) + (-((int)(APP_Rx_ptr_out <= APP_Rx_ptr_in)) & APP_RX_DATA_SIZE)) - 1;
} }
/** /**
@ -215,12 +211,13 @@ static uint16_t VCP_DataTx(const uint8_t* Buf, uint32_t Len)
while (USB_Tx_State != 0); while (USB_Tx_State != 0);
for (uint32_t i = 0; i < Len; i++) { for (uint32_t i = 0; i < Len; i++) {
APP_Rx_Buffer[APP_Rx_ptr_in] = Buf[i]; // Stall if the ring buffer is full
APP_Rx_ptr_in = (APP_Rx_ptr_in + 1) % APP_RX_DATA_SIZE; while (((APP_Rx_ptr_in + 1) % APP_RX_DATA_SIZE) == APP_Rx_ptr_out) {
while (CDC_Send_FreeBytes() == 0) {
delay(1); delay(1);
} }
APP_Rx_Buffer[APP_Rx_ptr_in] = Buf[i];
APP_Rx_ptr_in = (APP_Rx_ptr_in + 1) % APP_RX_DATA_SIZE;
} }
return USBD_OK; return USBD_OK;
@ -237,7 +234,7 @@ uint32_t CDC_Receive_DATA(uint8_t* recvBuf, uint32_t len)
{ {
uint32_t count = 0; uint32_t count = 0;
while (APP_Tx_ptr_out != APP_Tx_ptr_in && count < len) { while (APP_Tx_ptr_out != APP_Tx_ptr_in && (count < len)) {
recvBuf[count] = APP_Tx_Buffer[APP_Tx_ptr_out]; recvBuf[count] = APP_Tx_Buffer[APP_Tx_ptr_out];
APP_Tx_ptr_out = (APP_Tx_ptr_out + 1) % APP_TX_DATA_SIZE; APP_Tx_ptr_out = (APP_Tx_ptr_out + 1) % APP_TX_DATA_SIZE;
count++; count++;
@ -248,7 +245,7 @@ uint32_t CDC_Receive_DATA(uint8_t* recvBuf, uint32_t len)
uint32_t CDC_Receive_BytesAvailable(void) uint32_t CDC_Receive_BytesAvailable(void)
{ {
/* return the bytes available in the receive circular buffer */ /* return the bytes available in the receive circular buffer */
return APP_Tx_ptr_out > APP_Tx_ptr_in ? APP_TX_DATA_SIZE - APP_Tx_ptr_out + APP_Tx_ptr_in : APP_Tx_ptr_in - APP_Tx_ptr_out; return (APP_Tx_ptr_in + APP_TX_DATA_SIZE - APP_Tx_ptr_out) % APP_TX_DATA_SIZE;
} }
/** /**