mirror of
https://github.com/iNavFlight/inav.git
synced 2025-07-12 19:10:27 +03:00
update f4 vcp in line with BF (#9544)
This commit is contained in:
parent
b39a2dcde4
commit
9ba78683fc
2 changed files with 100 additions and 138 deletions
|
@ -184,8 +184,8 @@ __ALIGN_BEGIN uint8_t APP_Rx_Buffer [APP_RX_DATA_SIZE] __ALIGN_END ;
|
|||
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
|
||||
__ALIGN_BEGIN uint8_t CmdBuff[CDC_CMD_PACKET_SZE] __ALIGN_END ;
|
||||
|
||||
uint32_t APP_Rx_ptr_in = 0;
|
||||
uint32_t APP_Rx_ptr_out = 0;
|
||||
volatile uint32_t APP_Rx_ptr_in = 0;
|
||||
volatile uint32_t APP_Rx_ptr_out = 0;
|
||||
uint32_t APP_Rx_length = 0;
|
||||
|
||||
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)epnum;
|
||||
uint16_t USB_Tx_ptr;
|
||||
uint16_t USB_Tx_length;
|
||||
|
||||
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)
|
||||
{
|
||||
USB_Tx_State = USB_CDC_IDLE;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (APP_Rx_length > CDC_DATA_IN_PACKET_SIZE){
|
||||
USB_Tx_ptr = APP_Rx_ptr_out;
|
||||
} else {
|
||||
if (APP_Rx_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;
|
||||
}
|
||||
else
|
||||
{
|
||||
USB_Tx_ptr = APP_Rx_ptr_out;
|
||||
} else {
|
||||
USB_Tx_length = APP_Rx_length;
|
||||
|
||||
APP_Rx_ptr_out += APP_Rx_length;
|
||||
APP_Rx_length = 0;
|
||||
if(USB_Tx_length == CDC_DATA_IN_PACKET_SIZE)
|
||||
{
|
||||
if (USB_Tx_length == CDC_DATA_IN_PACKET_SIZE) {
|
||||
USB_Tx_State = USB_CDC_ZLP;
|
||||
}
|
||||
}
|
||||
|
||||
/* Prepare the available data buffer to be sent on IN endpoint */
|
||||
DCD_EP_Tx (pdev,
|
||||
CDC_IN_EP,
|
||||
(uint8_t*)&APP_Rx_Buffer[USB_Tx_ptr],
|
||||
USB_Tx_length);
|
||||
DCD_EP_Tx(pdev, CDC_IN_EP, (uint8_t*)&APP_Rx_Buffer[APP_Rx_ptr_out], USB_Tx_length);
|
||||
|
||||
// Advance the out pointer
|
||||
APP_Rx_ptr_out = (APP_Rx_ptr_out + USB_Tx_length) % APP_RX_DATA_SIZE;
|
||||
APP_Rx_length -= USB_Tx_length;
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
DCD_EP_Tx (pdev,
|
||||
CDC_IN_EP,
|
||||
NULL,
|
||||
0);
|
||||
DCD_EP_Tx(pdev, CDC_IN_EP, NULL, 0);
|
||||
|
||||
USB_Tx_State = USB_CDC_IDLE;
|
||||
}
|
||||
|
@ -731,66 +716,48 @@ uint8_t usbd_cdc_SOF (void *pdev)
|
|||
*/
|
||||
static void Handle_USBAsynchXfer (void *pdev)
|
||||
{
|
||||
uint16_t USB_Tx_ptr;
|
||||
uint16_t USB_Tx_length;
|
||||
|
||||
if(USB_Tx_State == USB_CDC_IDLE)
|
||||
{
|
||||
if (APP_Rx_ptr_out == APP_RX_DATA_SIZE)
|
||||
{
|
||||
APP_Rx_ptr_out = 0;
|
||||
}
|
||||
|
||||
if(APP_Rx_ptr_out == APP_Rx_ptr_in)
|
||||
{
|
||||
USB_Tx_State = USB_CDC_IDLE;
|
||||
if (USB_Tx_State == USB_CDC_IDLE) {
|
||||
if (APP_Rx_ptr_out == APP_Rx_ptr_in) {
|
||||
// Ring buffer is empty
|
||||
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;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Transfer all bytes in ring buffer
|
||||
APP_Rx_length = APP_Rx_ptr_in - APP_Rx_ptr_out;
|
||||
|
||||
}
|
||||
|
||||
#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
|
||||
// Only transfer whole 32 bit words of data
|
||||
APP_Rx_length &= ~0x03;
|
||||
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
|
||||
|
||||
if (APP_Rx_length > CDC_DATA_IN_PACKET_SIZE)
|
||||
{
|
||||
USB_Tx_ptr = APP_Rx_ptr_out;
|
||||
if (APP_Rx_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;
|
||||
}
|
||||
else
|
||||
{
|
||||
USB_Tx_ptr = APP_Rx_ptr_out;
|
||||
} else {
|
||||
USB_Tx_length = APP_Rx_length;
|
||||
|
||||
APP_Rx_ptr_out += APP_Rx_length;
|
||||
APP_Rx_length = 0;
|
||||
if(USB_Tx_length == CDC_DATA_IN_PACKET_SIZE)
|
||||
{
|
||||
if (USB_Tx_length == CDC_DATA_IN_PACKET_SIZE) {
|
||||
USB_Tx_State = USB_CDC_ZLP;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
USB_Tx_State = USB_CDC_BUSY;
|
||||
}
|
||||
}
|
||||
|
||||
DCD_EP_Tx (pdev,
|
||||
CDC_IN_EP,
|
||||
(uint8_t*)&APP_Rx_Buffer[USB_Tx_ptr],
|
||||
(uint8_t*)&APP_Rx_Buffer[APP_Rx_ptr_out],
|
||||
USB_Tx_length);
|
||||
|
||||
APP_Rx_ptr_out = (APP_Rx_ptr_out + USB_Tx_length) % APP_RX_DATA_SIZE;
|
||||
APP_Rx_length -= USB_Tx_length;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,15 +19,17 @@
|
|||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "usbd_cdc_vcp.h"
|
||||
#include "stm32f4xx_conf.h"
|
||||
#include <stdbool.h>
|
||||
#include "drivers/time.h"
|
||||
|
||||
#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
|
||||
#pragma data_alignment = 4
|
||||
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "usbd_cdc_vcp.h"
|
||||
#include "stm32f4xx_conf.h"
|
||||
#include "stdbool.h"
|
||||
#include "drivers/time.h"
|
||||
__ALIGN_BEGIN USB_OTG_CORE_HANDLE USB_OTG_dev __ALIGN_END;
|
||||
|
||||
LINE_CODING g_lc;
|
||||
|
||||
|
@ -38,11 +40,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) */
|
||||
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
|
||||
start address when writing received data
|
||||
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)
|
||||
|
@ -63,7 +65,6 @@ static void *ctrlLineStateCbContext;
|
|||
static void (*baudRateCb)(void *context, uint32_t baud);
|
||||
static void *baudRateCbContext;
|
||||
|
||||
__ALIGN_BEGIN USB_OTG_CORE_HANDLE USB_OTG_dev __ALIGN_END;
|
||||
|
||||
CDC_IF_Prop_TypeDef VCP_fops = {VCP_Init, VCP_DeInit, VCP_Ctrl, VCP_DataTx, VCP_DataRx };
|
||||
|
||||
|
@ -132,7 +133,7 @@ static uint16_t VCP_Ctrl(uint32_t Cmd, uint8_t* Buf, uint32_t Len)
|
|||
//Note - hw flow control on UART 1-3 and 6 only
|
||||
case SET_LINE_CODING:
|
||||
// If a callback is provided, tell the upper driver of changes in baud rate
|
||||
if (plc && (Len == sizeof (*plc))) {
|
||||
if (plc && (Len == sizeof(*plc))) {
|
||||
if (baudRateCb) {
|
||||
baudRateCb(baudRateCbContext, plc->bitrate);
|
||||
}
|
||||
|
@ -142,7 +143,7 @@ static uint16_t VCP_Ctrl(uint32_t Cmd, uint8_t* Buf, uint32_t Len)
|
|||
|
||||
|
||||
case GET_LINE_CODING:
|
||||
if (plc && (Len == sizeof (*plc))) {
|
||||
if (plc && (Len == sizeof(*plc))) {
|
||||
ust_cpy(plc, &g_lc);
|
||||
}
|
||||
break;
|
||||
|
@ -150,7 +151,7 @@ static uint16_t VCP_Ctrl(uint32_t Cmd, uint8_t* Buf, uint32_t Len)
|
|||
|
||||
case SET_CONTROL_LINE_STATE:
|
||||
// If a callback is provided, tell the upper driver of changes in DTR/RTS state
|
||||
if (plc && (Len == sizeof (uint16_t))) {
|
||||
if (plc && (Len == sizeof(uint16_t))) {
|
||||
if (ctrlLineStateCb) {
|
||||
ctrlLineStateCb(ctrlLineStateCbContext, *((uint16_t *)Buf));
|
||||
}
|
||||
|
@ -183,14 +184,7 @@ uint32_t CDC_Send_DATA(const uint8_t *ptrBuffer, uint32_t sendLength)
|
|||
|
||||
uint32_t CDC_Send_FreeBytes(void)
|
||||
{
|
||||
/*
|
||||
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_DATA_SIZE - CDC_Receive_BytesAvailable();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -210,12 +204,13 @@ static uint16_t VCP_DataTx(const uint8_t* Buf, uint32_t Len)
|
|||
while (USB_Tx_State != 0);
|
||||
|
||||
for (uint32_t i = 0; i < Len; i++) {
|
||||
APP_Rx_Buffer[APP_Rx_ptr_in] = Buf[i];
|
||||
APP_Rx_ptr_in = (APP_Rx_ptr_in + 1) % APP_RX_DATA_SIZE;
|
||||
|
||||
while (CDC_Send_FreeBytes() == 0) {
|
||||
// Stall if the ring buffer is full
|
||||
while (((APP_Rx_ptr_in + 1) % APP_RX_DATA_SIZE) == APP_Rx_ptr_out) {
|
||||
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;
|
||||
|
@ -232,7 +227,7 @@ uint32_t CDC_Receive_DATA(uint8_t* recvBuf, uint32_t len)
|
|||
{
|
||||
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];
|
||||
APP_Tx_ptr_out = (APP_Tx_ptr_out + 1) % APP_TX_DATA_SIZE;
|
||||
count++;
|
||||
|
@ -243,7 +238,7 @@ uint32_t CDC_Receive_DATA(uint8_t* recvBuf, uint32_t len)
|
|||
uint32_t CDC_Receive_BytesAvailable(void)
|
||||
{
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue