From e8219dc09a4a30099504cf570269d52c05ecbad9 Mon Sep 17 00:00:00 2001 From: Daniel Fekete Date: Tue, 9 May 2017 11:25:31 +0200 Subject: [PATCH] USB CDC: Move memory allocation from config to cdc --- STM32/cores/arduino/usb/usbd_cdc.c | 23 ++++++++++++++++++++--- STM32/cores/arduino/usb/usbd_conf_F1.c | 22 ---------------------- STM32/cores/arduino/usb/usbd_conf_F4.c | 17 ----------------- STM32/cores/arduino/usb/usbd_conf_F7.c | 17 ----------------- STM32/cores/arduino/usb/usbd_conf_L0.c | 21 --------------------- 5 files changed, 20 insertions(+), 80 deletions(-) diff --git a/STM32/cores/arduino/usb/usbd_cdc.c b/STM32/cores/arduino/usb/usbd_cdc.c index b2ca5f1..f0ec2e4 100644 --- a/STM32/cores/arduino/usb/usbd_cdc.c +++ b/STM32/cores/arduino/usb/usbd_cdc.c @@ -131,6 +131,11 @@ static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc (uint16_t *length); uint8_t *USBD_CDC_GetDeviceQualifierDescriptor (uint16_t *length); + +static uint32_t staticMemory[(sizeof(USBD_CDC_HandleTypeDef)/4)+1]; + +static uint8_t staticMemoryUsed = 0; + /* USB Standard Device Descriptor */ __ALIGN_BEGIN static uint8_t USBD_CDC_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = { @@ -513,9 +518,15 @@ static uint8_t USBD_CDC_Init (USBD_HandleTypeDef *pdev, USBD_EP_TYPE_INTR, CDC_CMD_PACKET_SIZE); - - pdev->pClassData = USBD_malloc(sizeof (USBD_CDC_HandleTypeDef)); + if (staticMemoryUsed == 0) { + pdev->pClassData = &staticMemory; + staticMemoryUsed = 1; + } else { + // On the rare ocasion when you want to use CDC on both FS and HS USB + pdev->pClassData = malloc(sizeof(USBD_CDC_HandleTypeDef)); + } + if(pdev->pClassData == NULL) { ret = 1; @@ -582,7 +593,13 @@ static uint8_t USBD_CDC_DeInit (USBD_HandleTypeDef *pdev, if(pdev->pClassData != NULL) { ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->DeInit(); - USBD_free(pdev->pClassData); + + if (pdev->pClassData != &staticMemory) { + free(pdev->pClassData); + } else { + staticMemoryUsed = 0; + } + pdev->pClassData = NULL; } diff --git a/STM32/cores/arduino/usb/usbd_conf_F1.c b/STM32/cores/arduino/usb/usbd_conf_F1.c index c5c6576..00fdc99 100644 --- a/STM32/cores/arduino/usb/usbd_conf_F1.c +++ b/STM32/cores/arduino/usb/usbd_conf_F1.c @@ -39,7 +39,6 @@ #include "usbd_def.h" #include "usbd_core.h" -#include "usbd_cdc.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ @@ -466,27 +465,6 @@ void USBD_LL_Delay (uint32_t Delay) HAL_Delay(Delay); } -/** - * @brief static single allocation. - * @param size: size of allocated memory - * @retval None - */ -void *USBD_static_malloc(uint32_t size) -{ - static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef)/4)+1];/* On 32-bit boundary */ - return mem; -} - -/** - * @brief Dummy memory free - * @param *p pointer to allocated memory address - * @retval None - */ -void USBD_static_free(void *p) -{ - -} - /** * @brief Software Device Connection * @param hpcd: PCD handle diff --git a/STM32/cores/arduino/usb/usbd_conf_F4.c b/STM32/cores/arduino/usb/usbd_conf_F4.c index d1cc286..6379015 100644 --- a/STM32/cores/arduino/usb/usbd_conf_F4.c +++ b/STM32/cores/arduino/usb/usbd_conf_F4.c @@ -38,7 +38,6 @@ #include "usbd_def.h" #include "usbd_core.h" -#include "usbd_cdc.h" PCD_HandleTypeDef hpcd_USB_OTG_FS; void Error_Handler(void); @@ -536,22 +535,6 @@ void USBD_LL_Delay (uint32_t Delay) } -void *USBD_static_malloc(uint32_t size) -{ - static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef)/4)+1];/* On 32-bit boundary */ - return mem; -} - -/** - * @brief Dummy memory free - * @param *p pointer to allocated memory address - * @retval None - */ -void USBD_static_free(void *p) -{ - -} - #endif /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/STM32/cores/arduino/usb/usbd_conf_F7.c b/STM32/cores/arduino/usb/usbd_conf_F7.c index ef9e08d..13315a8 100644 --- a/STM32/cores/arduino/usb/usbd_conf_F7.c +++ b/STM32/cores/arduino/usb/usbd_conf_F7.c @@ -48,7 +48,6 @@ #include "usbd_def.h" #include "usbd_core.h" -#include "usbd_cdc.h" PCD_HandleTypeDef hpcd_USB_OTG_FS; void Error_Handler(void); @@ -768,22 +767,6 @@ void USBD_LL_Delay (uint32_t Delay) HAL_Delay(Delay); } -void *USBD_static_malloc(uint32_t size) -{ - static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef)/4)+1];/* On 32-bit boundary */ - return mem; -} - -/** - * @brief Dummy memory free - * @param *p pointer to allocated memory address - * @retval None - */ -void USBD_static_free(void *p) -{ - -} - /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ #endif diff --git a/STM32/cores/arduino/usb/usbd_conf_L0.c b/STM32/cores/arduino/usb/usbd_conf_L0.c index c331032..45dc138 100644 --- a/STM32/cores/arduino/usb/usbd_conf_L0.c +++ b/STM32/cores/arduino/usb/usbd_conf_L0.c @@ -38,7 +38,6 @@ #include "usbd_def.h" #include "usbd_core.h" -#include "usbd_cdc.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ @@ -475,26 +474,6 @@ void USBD_LL_Delay (uint32_t Delay) HAL_Delay(Delay); } -/** - * @brief static single allocation. - * @param size: size of allocated memory - * @retval None - */ -void *USBD_static_malloc(uint32_t size) -{ - static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef)/4)+1];/* On 32-bit boundary */ - return mem; -} - -/** - * @brief Dummy memory free - * @param *p pointer to allocated memory address - * @retval None - */ -void USBD_static_free(void *p) -{ - -} /* USER CODE BEGIN 5 */ /**