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

Fix CDC_Send_FreeBytes

This commit is contained in:
Štěpán Dalecký 2022-07-02 12:03:45 +02:00
parent 4339e4ffd6
commit b6e2790f15

View file

@ -44,7 +44,7 @@ __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. */
@ -190,12 +190,8 @@ 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.
*/ */
return ((APP_Rx_ptr_out - APP_Rx_ptr_in) + (-((int)(APP_Rx_ptr_out <= APP_Rx_ptr_in)) & APP_RX_DATA_SIZE)) - 1; return (APP_Rx_ptr_out > APP_Rx_ptr_in ? APP_Rx_ptr_out - APP_Rx_ptr_in : APP_RX_DATA_SIZE + APP_Rx_ptr_out - APP_Rx_ptr_in);
} }
/** /**
@ -212,15 +208,13 @@ static uint16_t VCP_DataTx(const uint8_t* Buf, uint32_t Len)
could just check for: USB_CDC_ZLP, but better to be safe could just check for: USB_CDC_ZLP, but better to be safe
and wait for any existing transmission to complete. and wait for any existing transmission to complete.
*/ */
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]; while ((APP_Rx_ptr_in + 1) % APP_RX_DATA_SIZE == APP_Rx_ptr_out || APP_Rx_ptr_out == APP_RX_DATA_SIZE || USB_Tx_State != 0) {
APP_Rx_ptr_in = (APP_Rx_ptr_in + 1) % APP_RX_DATA_SIZE;
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;