Projectkk2glider/usb hardening (#4125)
* USB fixes: * USB initialized when USB cable is inserted and de-initialized when disconnected (this was the original way, which proved itself unreliable in the past. Now with the new USB library it appears to work without any problems. Used in this way result in safer operation - USB controller is disabled while unconnected) Works without force device mod * Serial: initialization of global variables * Reduced RAM usage - data moved to FLASH * Detailed USB interrupt counters (+ debug print fix)
This commit is contained in:
committed by
Bertrand Songis
parent
c3238ee38c
commit
29c43d9e33
+1
-1
@@ -117,7 +117,7 @@ CDC_IF_Prop_TypeDef;
|
||||
* @{
|
||||
*/
|
||||
|
||||
extern USBD_Class_cb_TypeDef USBD_CDC_cb;
|
||||
extern const USBD_Class_cb_TypeDef USBD_CDC_cb; // modified my OpenTX
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
+28
-19
@@ -138,22 +138,24 @@ static uint8_t *USBD_cdc_GetOtherCfgDesc (uint8_t speed, uint16_t *length);
|
||||
/** @defgroup usbd_cdc_Private_Variables
|
||||
* @{
|
||||
*/
|
||||
extern CDC_IF_Prop_TypeDef APP_FOPS;
|
||||
extern const CDC_IF_Prop_TypeDef APP_FOPS; // modified my OpenTX
|
||||
extern const uint8_t USBD_DeviceDesc [USB_SIZ_DEVICE_DESC]; // modified by OpenTX
|
||||
|
||||
#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
|
||||
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
|
||||
#pragma data_alignment=4
|
||||
#endif
|
||||
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
|
||||
__ALIGN_BEGIN uint8_t usbd_cdc_CfgDesc [USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END ;
|
||||
// modified my OpenTX
|
||||
// #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
|
||||
// #if defined ( __ICCARM__ ) /*!< IAR Compiler */
|
||||
// #pragma data_alignment=4
|
||||
// #endif
|
||||
// #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
|
||||
// __ALIGN_BEGIN uint8_t usbd_cdc_CfgDesc [USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END ;
|
||||
|
||||
#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
|
||||
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
|
||||
#pragma data_alignment=4
|
||||
#endif
|
||||
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
|
||||
__ALIGN_BEGIN uint8_t usbd_cdc_OtherCfgDesc [USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END ;
|
||||
// modified my OpenTX
|
||||
// #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
|
||||
// #if defined ( __ICCARM__ ) /*!< IAR Compiler */
|
||||
// #pragma data_alignment=4
|
||||
// #endif
|
||||
// #endif USB_OTG_HS_INTERNAL_DMA_ENABLED
|
||||
// __ALIGN_BEGIN uint8_t usbd_cdc_OtherCfgDesc [USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END ;
|
||||
|
||||
#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
|
||||
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
|
||||
@@ -191,10 +193,10 @@ uint32_t APP_Rx_length = 0;
|
||||
uint8_t USB_Tx_State = USB_CDC_IDLE;
|
||||
|
||||
static uint32_t cdcCmd = 0xFF;
|
||||
static uint32_t cdcLen = 0;
|
||||
static uint8_t cdcLen = 0; // modified by OpenTX
|
||||
|
||||
/* CDC interface class callbacks structure */
|
||||
USBD_Class_cb_TypeDef USBD_CDC_cb =
|
||||
const USBD_Class_cb_TypeDef USBD_CDC_cb = // modified by OpenTX
|
||||
{
|
||||
usbd_cdc_Init,
|
||||
usbd_cdc_DeInit,
|
||||
@@ -218,7 +220,7 @@ USBD_Class_cb_TypeDef USBD_CDC_cb =
|
||||
#endif
|
||||
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
|
||||
/* USB CDC device Configuration Descriptor */
|
||||
__ALIGN_BEGIN uint8_t usbd_cdc_CfgDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END =
|
||||
__ALIGN_BEGIN const uint8_t usbd_cdc_CfgDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = // modified by OpenTX
|
||||
{
|
||||
/*Configuration Descriptor*/
|
||||
0x09, /* bLength: Configuration Descriptor size */
|
||||
@@ -323,7 +325,7 @@ __ALIGN_BEGIN uint8_t usbd_cdc_CfgDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END =
|
||||
#pragma data_alignment=4
|
||||
#endif
|
||||
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
|
||||
__ALIGN_BEGIN uint8_t usbd_cdc_OtherCfgDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END =
|
||||
__ALIGN_BEGIN const uint8_t usbd_cdc_OtherCfgDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = // modified by OpenTX
|
||||
{
|
||||
0x09, /* bLength: Configuration Descriptor size */
|
||||
USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION,
|
||||
@@ -436,6 +438,13 @@ uint8_t usbd_cdc_Init (void *pdev,
|
||||
{
|
||||
uint8_t *pbuf;
|
||||
|
||||
APP_Rx_ptr_in = 0; // modified by OpenTX
|
||||
APP_Rx_ptr_out = 0; // modified by OpenTX
|
||||
APP_Rx_length = 0; // modified by OpenTX
|
||||
USB_Tx_State = USB_CDC_IDLE; // modified by OpenTX
|
||||
cdcCmd = 0xFF; // modified by OpenTX
|
||||
cdcLen = 0; // modified by OpenTX
|
||||
|
||||
/* Open EP IN */
|
||||
DCD_EP_Open(pdev,
|
||||
CDC_IN_EP,
|
||||
@@ -702,9 +711,9 @@ uint8_t usbd_cdc_DataOut (void *pdev, uint8_t epnum)
|
||||
*/
|
||||
uint8_t usbd_cdc_SOF (void *pdev)
|
||||
{
|
||||
static uint32_t FrameCount = 0;
|
||||
static uint8_t FrameCount = 0; // modified by OpenTX
|
||||
|
||||
if (FrameCount++ == CDC_IN_FRAME_INTERVAL)
|
||||
if (FrameCount++ >= CDC_IN_FRAME_INTERVAL) // modified by OpenTX
|
||||
{
|
||||
/* Reset the frame counter */
|
||||
FrameCount = 0;
|
||||
|
||||
+1
-1
@@ -61,7 +61,7 @@
|
||||
* @{
|
||||
*/
|
||||
|
||||
extern USBD_Class_cb_TypeDef USBD_MSC_cb;
|
||||
extern const USBD_Class_cb_TypeDef USBD_MSC_cb; // modified my OpenTX
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
+1
-1
@@ -92,7 +92,7 @@ typedef struct _USBD_STORAGE
|
||||
/** @defgroup USBD_MEM_Exported_FunctionsPrototype
|
||||
* @{
|
||||
*/
|
||||
extern USBD_STORAGE_cb_TypeDef *USBD_STORAGE_fops;
|
||||
extern const USBD_STORAGE_cb_TypeDef * const USBD_STORAGE_fops; // modified my OpenTX
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
+11
-10
@@ -110,7 +110,7 @@ uint8_t *USBD_MSC_GetOtherCfgDesc (uint8_t speed,
|
||||
#endif
|
||||
|
||||
|
||||
uint8_t USBD_MSC_CfgDesc[USB_MSC_CONFIG_DESC_SIZ];
|
||||
// uint8_t USBD_MSC_CfgDesc[USB_MSC_CONFIG_DESC_SIZ]; // modified by OpenTX
|
||||
|
||||
|
||||
|
||||
@@ -125,7 +125,7 @@ uint8_t USBD_MSC_CfgDesc[USB_MSC_CONFIG_DESC_SIZ];
|
||||
*/
|
||||
|
||||
|
||||
USBD_Class_cb_TypeDef USBD_MSC_cb =
|
||||
const USBD_Class_cb_TypeDef USBD_MSC_cb = // modified by OpenTX
|
||||
{
|
||||
USBD_MSC_Init,
|
||||
USBD_MSC_DeInit,
|
||||
@@ -150,7 +150,7 @@ USBD_Class_cb_TypeDef USBD_MSC_cb =
|
||||
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
|
||||
/* USB Mass storage device Configuration Descriptor */
|
||||
/* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */
|
||||
__ALIGN_BEGIN uint8_t USBD_MSC_CfgDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END =
|
||||
__ALIGN_BEGIN const uint8_t USBD_MSC_CfgDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END = // modified by OpenTX
|
||||
{
|
||||
|
||||
0x09, /* bLength: Configuation Descriptor size */
|
||||
@@ -240,12 +240,13 @@ __ALIGN_BEGIN uint8_t USBD_MSC_OtherCfgDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
|
||||
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
|
||||
#pragma data_alignment=4
|
||||
#endif
|
||||
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
|
||||
__ALIGN_BEGIN static uint8_t USBD_MSC_MaxLun __ALIGN_END = 0;
|
||||
// modified my OpenTX
|
||||
// #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
|
||||
// #if defined ( __ICCARM__ ) /*!< IAR Compiler */
|
||||
// #pragma data_alignment=4
|
||||
// #endif
|
||||
// #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
|
||||
// __ALIGN_BEGIN static uint8_t USBD_MSC_MaxLun __ALIGN_END = 0;
|
||||
|
||||
#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
|
||||
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
|
||||
@@ -334,6 +335,7 @@ uint8_t USBD_MSC_Setup (void *pdev, USB_SETUP_REQ *req)
|
||||
(req->wLength == 1) &&
|
||||
((req->bmRequest & 0x80) == 0x80))
|
||||
{
|
||||
__ALIGN_BEGIN uint8_t USBD_MSC_MaxLun __ALIGN_END; // modified by OpenTX
|
||||
USBD_MSC_MaxLun = USBD_STORAGE_fops->GetMaxLun();
|
||||
if(USBD_MSC_MaxLun > 0)
|
||||
{
|
||||
@@ -345,7 +347,6 @@ uint8_t USBD_MSC_Setup (void *pdev, USB_SETUP_REQ *req)
|
||||
{
|
||||
USBD_CtlError(pdev , req);
|
||||
return USBD_FAIL;
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
-4
@@ -153,10 +153,6 @@ void USBD_Init(USB_OTG_CORE_HANDLE *pdev,
|
||||
/* Upon Init call usr callback */
|
||||
pdev->dev.usr_cb->Init();
|
||||
|
||||
/* Force Device Mode*/
|
||||
// IMPORTANT: without this here, the USB works upon radio start only in 50% cases
|
||||
USB_OTG_SetCurrentMode(pdev, DEVICE_MODE); // modified by OpenTX
|
||||
|
||||
/* Enable Interrupts */
|
||||
USB_OTG_BSP_EnableInterrupt(pdev);
|
||||
}
|
||||
|
||||
+18
@@ -28,6 +28,7 @@
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "usb_dcd_int.h"
|
||||
#include "board.h" // modified by OpenTX
|
||||
#include "debug.h" // modified by OpenTX
|
||||
|
||||
/** @addtogroup USB_OTG_DRIVER
|
||||
* @{
|
||||
@@ -216,21 +217,25 @@ uint32_t USBD_OTG_ISR_Handler (USB_OTG_CORE_HANDLE *pdev)
|
||||
gintr_status.d32 = USB_OTG_ReadCoreItr(pdev);
|
||||
if (!gintr_status.d32) /* avoid spurious interrupt */
|
||||
{
|
||||
DEBUG_USB_INTERRUPT(INT_OTG_FS_SPURIOUS); // modified by OpenTX
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (gintr_status.b.outepintr)
|
||||
{
|
||||
DEBUG_USB_INTERRUPT(INT_OTG_FS_OUT_EP); // modified by OpenTX
|
||||
retval |= DCD_HandleOutEP_ISR(pdev);
|
||||
}
|
||||
|
||||
if (gintr_status.b.inepint)
|
||||
{
|
||||
DEBUG_USB_INTERRUPT(INT_OTG_FS_IN_EP); // modified by OpenTX
|
||||
retval |= DCD_HandleInEP_ISR(pdev);
|
||||
}
|
||||
|
||||
if (gintr_status.b.modemismatch)
|
||||
{
|
||||
DEBUG_USB_INTERRUPT(INT_OTG_FS_MODEMISMATCH); // modified by OpenTX
|
||||
USB_OTG_GINTSTS_TypeDef gintsts;
|
||||
|
||||
/* Clear interrupt */
|
||||
@@ -241,56 +246,69 @@ uint32_t USBD_OTG_ISR_Handler (USB_OTG_CORE_HANDLE *pdev)
|
||||
|
||||
if (gintr_status.b.wkupintr)
|
||||
{
|
||||
DEBUG_USB_INTERRUPT(INT_OTG_FS_WAKEUP); // modified by OpenTX
|
||||
retval |= DCD_HandleResume_ISR(pdev);
|
||||
}
|
||||
|
||||
if (gintr_status.b.usbsuspend)
|
||||
{
|
||||
DEBUG_USB_INTERRUPT(INT_OTG_FS_SUSPEND); // modified by OpenTX
|
||||
retval |= DCD_HandleUSBSuspend_ISR(pdev);
|
||||
}
|
||||
if (gintr_status.b.sofintr)
|
||||
{
|
||||
DEBUG_USB_INTERRUPT(INT_OTG_FS_SOF); // modified by OpenTX
|
||||
retval |= DCD_HandleSof_ISR(pdev);
|
||||
|
||||
}
|
||||
|
||||
if (gintr_status.b.rxstsqlvl)
|
||||
{
|
||||
DEBUG_USB_INTERRUPT(INT_OTG_FS_RX_STAT); // modified by OpenTX
|
||||
retval |= DCD_HandleRxStatusQueueLevel_ISR(pdev);
|
||||
|
||||
}
|
||||
|
||||
if (gintr_status.b.usbreset)
|
||||
{
|
||||
DEBUG_USB_INTERRUPT(INT_OTG_FS_RESET); // modified by OpenTX
|
||||
retval |= DCD_HandleUsbReset_ISR(pdev);
|
||||
|
||||
}
|
||||
if (gintr_status.b.enumdone)
|
||||
{
|
||||
DEBUG_USB_INTERRUPT(INT_OTG_FS_ENUM); // modified by OpenTX
|
||||
retval |= DCD_HandleEnumDone_ISR(pdev);
|
||||
}
|
||||
|
||||
if (gintr_status.b.incomplisoin)
|
||||
{
|
||||
DEBUG_USB_INTERRUPT(INT_OTG_FS_INCOMPLETE_IN); // modified by OpenTX
|
||||
retval |= DCD_IsoINIncomplete_ISR(pdev);
|
||||
}
|
||||
|
||||
if (gintr_status.b.incomplisoout)
|
||||
{
|
||||
DEBUG_USB_INTERRUPT(INT_OTG_FS_INCOMPLETE_OUT); // modified by OpenTX
|
||||
retval |= DCD_IsoOUTIncomplete_ISR(pdev);
|
||||
}
|
||||
#ifdef VBUS_SENSING_ENABLED
|
||||
if (gintr_status.b.sessreqintr)
|
||||
{
|
||||
DEBUG_USB_INTERRUPT(INT_OTG_FS_SESSION); // modified by OpenTX
|
||||
retval |= DCD_SessionRequest_ISR(pdev);
|
||||
}
|
||||
|
||||
if (gintr_status.b.otgintr)
|
||||
{
|
||||
DEBUG_USB_INTERRUPT(INT_OTG_FS_OTG); // modified by OpenTX
|
||||
retval |= DCD_OTG_ISR(pdev);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
DEBUG_USB_INTERRUPT(INT_OTG_FS_RX_NOT_DEVICE); // modified by OpenTX
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user