From f3fbb8d32bf1027d7e75b3958e37edd7f749d3a2 Mon Sep 17 00:00:00 2001 From: muzkr Date: Sun, 9 Nov 2025 04:27:35 +0800 Subject: [PATCH] Some fix --- App/app/app.c | 20 +++++----- App/app/uart.c | 92 +++++++++++++++++++++++++++++-------------- App/driver/systick.c | 2 + App/driver/vcp.c | 7 ++-- App/driver/vcp.h | 11 +++++- App/usb/usb_config.h | 6 +-- App/usb/usbd_cdc_if.c | 46 ++++++++-------------- CMakePresets.json | 1 + 8 files changed, 108 insertions(+), 77 deletions(-) diff --git a/App/app/app.c b/App/app/app.c index 73fd30d8..b7d17795 100644 --- a/App/app/app.c +++ b/App/app/app.c @@ -898,6 +898,14 @@ void APP_Update(void) } #endif +#ifdef ENABLE_USB + if (UART_IsCommandAvailable(UART_PORT_VCP)) { + // SCHEDULER_Disable(); + UART_HandleCommand(UART_PORT_VCP); + // SCHEDULER_Enable(); + } +#endif + #ifdef ENABLE_FEAT_F4HWN if (gCurrentFunction == FUNCTION_TRANSMIT && (gTxTimeoutReachedAlert || SerialConfigInProgress())) { @@ -1365,17 +1373,9 @@ void APP_TimeSlice10ms(void) #ifdef ENABLE_UART if (UART_IsCommandAvailable(UART_PORT_UART)) { - SCHEDULER_Disable(); + // SCHEDULER_Disable(); UART_HandleCommand(UART_PORT_UART); - SCHEDULER_Enable(); - } -#endif - -#ifdef ENABLE_USB - if (UART_IsCommandAvailable(UART_PORT_VCP)) { - SCHEDULER_Disable(); - UART_HandleCommand(UART_PORT_VCP); - SCHEDULER_Enable(); + // SCHEDULER_Enable(); } #endif diff --git a/App/app/uart.c b/App/app/uart.c index f50b8c52..bd63a24b 100644 --- a/App/app/uart.c +++ b/App/app/uart.c @@ -57,6 +57,9 @@ #define DMA_CHANNEL LL_DMA_CHANNEL_2 #endif +// !! Make sure this is correct! +#define MAX_REPLY_SIZE 144 + typedef struct { uint16_t ID; uint16_t Size; @@ -187,8 +190,65 @@ typedef union // static bool bIsEncrypted = true; #define bIsEncrypted true +#ifdef ENABLE_USB +static void SendReply_VCP(void *pReply, uint16_t Size) +{ + static uint8_t VCP_ReplyBuf[MAX_REPLY_SIZE + sizeof(Header_t) + sizeof(Footer_t)]; + + // !! + if (Size > MAX_REPLY_SIZE) + { + return; + } + + memcpy(VCP_ReplyBuf + sizeof(Header_t), pReply, Size); + + Header_t *pHeader = (Header_t *)VCP_ReplyBuf; + Footer_t *pFooter = (Footer_t *)(VCP_ReplyBuf + sizeof(Header_t) + Size); + pReply = VCP_ReplyBuf + sizeof(Header_t); + + if (bIsEncrypted) + { + uint8_t *pBytes = (uint8_t *)pReply; + unsigned int i; + for (i = 0; i < Size; i++) + pBytes[i] ^= Obfuscation[i % 16]; + } + + pHeader->ID = 0xCDAB; + pHeader->Size = Size; + + // VCP_Send((uint8_t *)&Header, sizeof(Header)); + // VCP_Send(pReply, Size); + + if (bIsEncrypted) + { + pFooter->Padding[0] = Obfuscation[(Size + 0) % 16] ^ 0xFF; + pFooter->Padding[1] = Obfuscation[(Size + 1) % 16] ^ 0xFF; + } + else + { + pFooter->Padding[0] = 0xFF; + pFooter->Padding[1] = 0xFF; + } + pFooter->ID = 0xBADC; + + // VCP_Send((uint8_t *)&Footer, sizeof(Footer)); + + VCP_SendAsync(VCP_ReplyBuf, sizeof(Header_t) + Size + sizeof(Footer_t)); +} +#endif // ENABLE_USB + static void SendReply(uint32_t Port, void *pReply, uint16_t Size) { +#if defined(ENABLE_USB) + if (Port == UART_PORT_VCP) + { + SendReply_VCP(pReply, Size); + return; + } +#endif + Header_t Header; Footer_t Footer; @@ -203,21 +263,8 @@ static void SendReply(uint32_t Port, void *pReply, uint16_t Size) Header.ID = 0xCDAB; Header.Size = Size; - if(0) {} -#if defined(ENABLE_UART) - else if (Port == UART_PORT_UART) - { - UART_Send(&Header, sizeof(Header)); - UART_Send(pReply, Size); - } -#endif -#if defined(ENABLE_USB) - else if (Port == UART_PORT_VCP) - { - VCP_Send((uint8_t *)&Header, sizeof(Header)); - VCP_Send(pReply, Size); - } -#endif + UART_Send(&Header, sizeof(Header)); + UART_Send(pReply, Size); if (bIsEncrypted) { @@ -231,20 +278,7 @@ static void SendReply(uint32_t Port, void *pReply, uint16_t Size) } Footer.ID = 0xBADC; - if(0) {} -#if defined(ENABLE_UART) - else if (Port == UART_PORT_UART) - { - UART_Send(&Footer, sizeof(Footer)); - } -#endif -#if defined(ENABLE_USB) - else if (Port == UART_PORT_VCP) - { - VCP_Send((uint8_t *)&Footer, sizeof(Footer)); - } -#endif - + UART_Send(&Footer, sizeof(Footer)); } static void SendVersion(uint32_t Port) diff --git a/App/driver/systick.c b/App/driver/systick.c index b907e075..9033f686 100644 --- a/App/driver/systick.c +++ b/App/driver/systick.c @@ -25,6 +25,8 @@ void SYSTICK_Init(void) { SysTick_Config(480000); gTickMultiplier = 48; + + NVIC_SetPriority(SysTick_IRQn, 0); } void SYSTICK_DelayUs(uint32_t Delay) diff --git a/App/driver/vcp.c b/App/driver/vcp.c index f6d7fec8..f4398592 100644 --- a/App/driver/vcp.c +++ b/App/driver/vcp.c @@ -18,12 +18,12 @@ #include "usb_config.h" #include "py32f071_ll_bus.h" -uint8_t VCP_RxBuf[256]; -uint32_t VCP_RxBufPointer = 0; +uint8_t VCP_RxBuf[VCP_RX_BUF_SIZE]; +volatile uint32_t VCP_RxBufPointer = 0; void VCP_Init() { - // LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SYSCFG); + LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SYSCFG); LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA); // PA12:11 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USBD); @@ -34,5 +34,6 @@ void VCP_Init() }; cdc_acm_init(rx_buf); + NVIC_SetPriority(USBD_IRQn, 3); NVIC_EnableIRQ(USBD_IRQn); } diff --git a/App/driver/vcp.h b/App/driver/vcp.h index 80bc0b45..24e9606e 100644 --- a/App/driver/vcp.h +++ b/App/driver/vcp.h @@ -21,8 +21,10 @@ #include #include "usb_config.h" -extern uint8_t VCP_RxBuf[256]; -extern uint32_t VCP_RxBufPointer; +#define VCP_RX_BUF_SIZE 256 + +extern uint8_t VCP_RxBuf[VCP_RX_BUF_SIZE]; +extern volatile uint32_t VCP_RxBufPointer; void VCP_Init(); @@ -39,4 +41,9 @@ static inline void VCP_SendStr(const char *Str) } } +static inline void VCP_SendAsync(const uint8_t *Buf, uint32_t Size) +{ + cdc_acm_data_send_with_dtr_async(Buf, Size); +} + #endif // _DRIVER_VCP_H diff --git a/App/usb/usb_config.h b/App/usb/usb_config.h index bb316eae..3d2af932 100644 --- a/App/usb/usb_config.h +++ b/App/usb/usb_config.h @@ -89,12 +89,12 @@ typedef struct { uint8_t *buf; - uint32_t size; - uint32_t *write_pointer; + const uint32_t size; + volatile uint32_t *write_pointer; } cdc_acm_rx_buf_t; void cdc_acm_init(cdc_acm_rx_buf_t rx_buf); void cdc_acm_data_send_with_dtr(const uint8_t *buf, uint32_t size); - +void cdc_acm_data_send_with_dtr_async(const uint8_t *buf, uint32_t size); #endif diff --git a/App/usb/usbd_cdc_if.c b/App/usb/usbd_cdc_if.c index aedf716a..61bace17 100644 --- a/App/usb/usbd_cdc_if.c +++ b/App/usb/usbd_cdc_if.c @@ -87,7 +87,7 @@ static const uint8_t cdc_descriptor[] = { }; USB_MEM_ALIGNX uint8_t read_buffer[128]; -USB_MEM_ALIGNX uint8_t write_buffer[4]; +// USB_MEM_ALIGNX uint8_t write_buffer[4]; static cdc_acm_rx_buf_t client_rx_buf = {0}; @@ -161,7 +161,8 @@ struct usbd_interface intf1; void cdc_acm_init(cdc_acm_rx_buf_t rx_buf) { - client_rx_buf = rx_buf; + // client_rx_buf = rx_buf; + memcpy(&client_rx_buf, &rx_buf, sizeof(cdc_acm_rx_buf_t)); *client_rx_buf.write_pointer = 0; usbd_desc_register(cdc_descriptor); @@ -185,34 +186,19 @@ void usbd_cdc_acm_set_dtr(uint8_t intf, bool dtr) void cdc_acm_data_send_with_dtr(const uint8_t *buf, uint32_t size) { - if (dtr_enable) + if (dtr_enable && 0 != size) { - if (0 != size) - { - uint32_t size1 = ((uint32_t)buf) % 4; - if (0 != size1) - { - if (size < size1) - { - size1 = size; - } - memcpy(write_buffer, buf, size1); - buf += size1; - size -= size1; - - ep_tx_busy_flag = true; - usbd_ep_start_write(CDC_IN_EP, write_buffer, size1); - while (ep_tx_busy_flag) - ; - } - } - - if (0 != size) - { - ep_tx_busy_flag = true; - usbd_ep_start_write(CDC_IN_EP, buf, size); - while (ep_tx_busy_flag) - ; - } + ep_tx_busy_flag = true; + usbd_ep_start_write(CDC_IN_EP, buf, size); + while (ep_tx_busy_flag) + ; + } +} + +void cdc_acm_data_send_with_dtr_async(const uint8_t *buf, uint32_t size) +{ + if (0 != size) + { + usbd_ep_start_write(CDC_IN_EP, buf, size); } } diff --git a/CMakePresets.json b/CMakePresets.json index 17e3e50b..94e76638 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -11,6 +11,7 @@ "CMAKE_BUILD_TYPE": "Release", "ENABLE_FMRADIO": false, "ENABLE_UART": true, + "ENABLE_USB": true, "ENABLE_AIRCOPY": false, "ENABLE_NOAA": false, "ENABLE_VOICE": false,