diff --git a/targets/stm32l432/lib/usbd/usbd_composite.c b/targets/stm32l432/lib/usbd/usbd_composite.c index 7d5fdb4..b1de7d2 100644 --- a/targets/stm32l432/lib/usbd/usbd_composite.c +++ b/targets/stm32l432/lib/usbd/usbd_composite.c @@ -4,6 +4,7 @@ #include "usbd_cdc.h" #include "usbd_ccid.h" #include "usbd_ctlreq.h" +#include "app.h" static uint8_t USBD_Composite_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx); @@ -27,20 +28,29 @@ static uint8_t *USBD_Composite_GetOtherSpeedCfgDesc (uint16_t *length); static uint8_t *USBD_Composite_GetDeviceQualifierDescriptor (uint16_t *length); +#ifdef ENABLE_CCID +#define CCID_SIZE 84 +#define CCID_NUM_INTERFACE 1 +#else +#define CCID_NUM_INTERFACE 0 +#define CCID_SIZE 0 +#endif + +#if DEBUG_LEVEL > 0 +#define CDC_SIZE (49 + 8 + 9 + 4) +#define CDC_NUM_INTERFACE 2 +#else +#define CDC_SIZE 0 +#define CDC_NUM_INTERFACE 0 +#endif + +#define HID_SIZE 41 + +#define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (HID_SIZE + CDC_SIZE + CCID_SIZE) +#define NUM_INTERFACES (1 + CDC_NUM_INTERFACE + CCID_NUM_INTERFACE) #define NUM_CLASSES 3 -#if NUM_CLASSES>2 -#define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (90 + 8+9 + 4 + 84) -#define NUM_INTERFACES 4 -#elif NUM_CLASSES>1 -#define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (90 + 8+9 + 4) -#define NUM_INTERFACES 3 -#else -#define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (41) -#define NUM_INTERFACES 1 -#endif - #define HID_INTF_NUM 0 #define CDC_MASTER_INTF_NUM 1 #define CDC_SLAVE_INTF_NUM 2 @@ -101,7 +111,7 @@ __ALIGN_BEGIN uint8_t COMPOSITE_CDC_HID_DESCRIPTOR[COMPOSITE_CDC_HID_DESCRIPTOR_ 0x00, HID_BINTERVAL, /*bInterval: Polling Interval */ -#if NUM_INTERFACES > 2 +#if DEBUG_LEVEL > 0 /* */ /* CDC */ @@ -199,7 +209,7 @@ __ALIGN_BEGIN uint8_t COMPOSITE_CDC_HID_DESCRIPTOR[COMPOSITE_CDC_HID_DESCRIPTOR_ 0x04, #endif -#if NUM_INTERFACES>3 +#ifdef ENABLE_CCID /* CCID Interface Descriptor */ 9, /* bLength: Interface Descriptor size */ @@ -295,16 +305,21 @@ USBD_ClassTypeDef USBD_Composite = USBD_Composite_GetDeviceQualifierDescriptor, }; -static USBD_ClassTypeDef *USBD_Classes[MAX_CLASSES]; +static USBD_ClassTypeDef * USBD_Classes[MAX_CLASSES]; int in_endpoint_to_class[MAX_ENDPOINTS]; int out_endpoint_to_class[MAX_ENDPOINTS]; void USBD_Composite_Set_Classes(USBD_ClassTypeDef *hid_class, USBD_ClassTypeDef *ccid_class, USBD_ClassTypeDef *cdc_class) { + memset(USBD_Classes, 0 , sizeof(USBD_Classes)); USBD_Classes[0] = hid_class; +#ifdef ENABLE_CCID USBD_Classes[1] = ccid_class; +#endif +#if DEBUG_LEVEL > 0 USBD_Classes[2] = cdc_class; +#endif } static USBD_ClassTypeDef * getClass(uint8_t index) @@ -313,12 +328,15 @@ static USBD_ClassTypeDef * getClass(uint8_t index) { case HID_INTF_NUM: return USBD_Classes[0]; +#ifdef ENABLE_CCID case CCID_INTF_NUM: return USBD_Classes[1]; +#endif +#if DEBUG_LEVEL > 0 case CDC_MASTER_INTF_NUM: case CDC_SLAVE_INTF_NUM: return USBD_Classes[2]; - +#endif } return NULL; } @@ -326,7 +344,7 @@ static USBD_ClassTypeDef * getClass(uint8_t index) static uint8_t USBD_Composite_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx) { int i; for(i = 0; i < NUM_CLASSES; i++) { - if (USBD_Classes[i]->Init(pdev, cfgidx) != USBD_OK) { + if (USBD_Classes[i] != NULL && USBD_Classes[i]->Init(pdev, cfgidx) != USBD_OK) { return USBD_FAIL; } } @@ -337,7 +355,7 @@ static uint8_t USBD_Composite_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx) { static uint8_t USBD_Composite_DeInit (USBD_HandleTypeDef *pdev, uint8_t cfgidx) { int i; for(i = 0; i < NUM_CLASSES; i++) { - if (USBD_Classes[i]->DeInit(pdev, cfgidx) != USBD_OK) { + if (USBD_Classes[i] != NULL && USBD_Classes[i]->DeInit(pdev, cfgidx) != USBD_OK) { return USBD_FAIL; } } @@ -363,7 +381,7 @@ static uint8_t USBD_Composite_Setup (USBD_HandleTypeDef *pdev, USBD_SetupReqType case USB_REQ_GET_DESCRIPTOR : for(i = 0; i < NUM_CLASSES; i++) { - if (USBD_Classes[i]->Setup(pdev, req) != USBD_OK) { + if (USBD_Classes[i] != NULL && USBD_Classes[i]->Setup(pdev, req) != USBD_OK) { return USBD_FAIL; } } @@ -386,6 +404,8 @@ static uint8_t USBD_Composite_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum) { i = in_endpoint_to_class[epnum]; + if (USBD_Classes[i] == NULL) return USBD_FAIL; + return USBD_Classes[i]->DataIn(pdev, epnum); } @@ -394,6 +414,8 @@ static uint8_t USBD_Composite_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum) i = out_endpoint_to_class[epnum]; + if (USBD_Classes[i] == NULL) return USBD_FAIL; + return USBD_Classes[i]->DataOut(pdev, epnum); } @@ -401,7 +423,7 @@ static uint8_t USBD_Composite_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum) static uint8_t USBD_Composite_EP0_RxReady (USBD_HandleTypeDef *pdev) { int i; for(i = 0; i < NUM_CLASSES; i++) { - if (USBD_Classes[i]->EP0_RxReady != NULL) { + if (USBD_Classes[i] != NULL && USBD_Classes[i]->EP0_RxReady != NULL) { if (USBD_Classes[i]->EP0_RxReady(pdev) != USBD_OK) { return USBD_FAIL; } diff --git a/targets/stm32l432/src/app.h b/targets/stm32l432/src/app.h index 308e5f6..fcd5629 100644 --- a/targets/stm32l432/src/app.h +++ b/targets/stm32l432/src/app.h @@ -12,9 +12,13 @@ #define DEBUG_UART USART1 #ifndef DEBUG_LEVEL +// Enable the CDC ACM USB interface & debug logs (DEBUG_LEVEL > 0) #define DEBUG_LEVEL 0 #endif +// Enable the CCID USB interface +// #define ENABLE_CCID + #define NON_BLOCK_PRINTING 0 diff --git a/targets/stm32l432/src/init.c b/targets/stm32l432/src/init.c index c57392c..a51e8b8 100644 --- a/targets/stm32l432/src/init.c +++ b/targets/stm32l432/src/init.c @@ -707,7 +707,6 @@ void init_usb() // Enable USB Clock SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_USBFSEN); -#if DEBUG_LEVEL > 0 USBD_Composite_Set_Classes(&USBD_HID, &USBD_CCID, &USBD_CDC); in_endpoint_to_class[HID_EPIN_ADDR & 0x7F] = 0; out_endpoint_to_class[HID_EPOUT_ADDR & 0x7F] = 0; @@ -720,17 +719,10 @@ void init_usb() USBD_Init(&Solo_USBD_Device, &Solo_Desc, 0); USBD_RegisterClass(&Solo_USBD_Device, &USBD_Composite); - // USBD_RegisterClass(&Solo_USBD_Device, &USBD_HID); - // - // USBD_RegisterClass(&Solo_USBD_Device, &USBD_CDC); +#if DEBUG_LEVEL > 0 USBD_CDC_RegisterInterface(&Solo_USBD_Device, &USBD_Interface_fops_FS); -#else - USBD_Init(&Solo_USBD_Device, &Solo_Desc, 0); - USBD_RegisterClass(&Solo_USBD_Device, &USBD_HID); #endif - //Y USBD_Start(&Solo_USBD_Device); - //Y } void init_pwm(void)