2021 USB HS interface now working, but SD card initialisation is broken
authorMichael McMaster <michael@codesrc.com>
Tue, 9 Feb 2021 12:33:12 +0000 (22:33 +1000)
committerMichael McMaster <michael@codesrc.com>
Tue, 9 Feb 2021 12:33:12 +0000 (22:33 +1000)
Makefile.2021
src/firmware/config.c
src/firmware/main.c
src/firmware/usb_device/usb_device.c
src/firmware/usb_device/usbd_composite.c
src/firmware/usb_device/usbd_desc.c
src/firmware/usb_device/usbd_desc.h
src/firmware/usb_device/usbd_hid.h
src/firmware/usb_device/usbd_msc.h
src/firmware/usb_device/usbd_msc_storage_sd.c

index d8d6860fdbf1585727dc7b9d04ba56f7dd09463e..084efda9cd8a74e6911c79db1e89f28b5e135831 100644 (file)
@@ -2,8 +2,8 @@
 ARMCC=arm-none-eabi-gcc
 OBJCOPY=arm-none-eabi-objcopy
 
-#CPPFLAGS=-DSTM32F446xx -DSTM32F4xx -DUSE_HAL_DRIVER -Wall -DS2S_USB_HS
-CPPFLAGS=-DSTM32F446xx -DSTM32F4xx -DUSE_HAL_DRIVER -Wall -DS2S_USB_FS
+CPPFLAGS=-DSTM32F446xx -DSTM32F4xx -DUSE_HAL_DRIVER -Wall -DS2S_USB_HS
+#CPPFLAGS=-DSTM32F446xx -DSTM32F4xx -DUSE_HAL_DRIVER -Wall -DS2S_USB_FS
 CFLAGS=-mcpu=cortex-m4 -mthumb -mslow-flash-data \
        -std=gnu11 \
        -specs=nosys.specs \
index 1803aa107753a05d0f32a81da31a7afa486a1247..f4522a65f961dcc0972a731ca61e23cb0e54c243 100755 (executable)
@@ -44,7 +44,7 @@ extern uint8_t* __fixed_config;
 #ifdef S2S_USB_HS\r
 #define configUsbDev hUsbDeviceHS\r
 #else\r
-#define configUsbDev configUsbDev\r
+#define configUsbDev hUsbDeviceFS\r
 #endif\r
 \r
 // 1 flash row\r
index a254b065c6fd22274fcc61542ed8011940dc059f..3a282ce6606a90be568fd776b937a469245f2427 100755 (executable)
@@ -105,7 +105,7 @@ void mainLoop()
 #ifdef S2S_USB_FS\r
        s2s_usbDevicePoll(&hUsbDeviceFS);\r
 #endif\r
-#ifdef S2S_USB_FS\r
+#ifdef S2S_USB_HS\r
        s2s_usbDevicePoll(&hUsbDeviceHS);\r
 #endif\r
 \r
index 189fbef31828dfc078a77c3b2eda33687ac924bd..085de2639c043a723171dfa2532ed4e357a005c5 100755 (executable)
@@ -39,8 +39,6 @@
 #include "usbd_desc.h"
 #include "usbd_composite.h"
 
-//#include "usbd_hidt.h"
-
 /* USB Device Core handle declaration */
 USBD_HandleTypeDef hUsbDeviceFS;
 USBD_HandleTypeDef hUsbDeviceHS;
@@ -53,11 +51,14 @@ void MX_USB_DEVICE_Init(void)
        USBD_Start(&hUsbDeviceFS);
 #endif
 #if S2S_USB_HS
-       USBD_Init(&hUsbDeviceHS, &FS_Desc, DEVICE_HS);
+       USBD_Init(&hUsbDeviceHS, &HS_Desc, DEVICE_HS);
        USBD_RegisterClass(&hUsbDeviceHS, &USBD_Composite);
 
-USB_OTG_GlobalTypeDef* USBx  = USB_OTG_HS;
-USBx_DEVICE->DCFG |= 0x4000; // BIT 14
+    // Hack to work with LPM ULPI (STM32F4446 only)
+    // See https://community.st.com/s/question/0D50X00009XkZUeSAN/stm32f2f4-problems-with-various-ulpi-usb-phys
+    USB_OTG_GlobalTypeDef* USBx  = USB_OTG_HS;
+    uint32_t USBx_BASE = (uint32_t)USBx; // Needed for macro expansion
+    USBx_DEVICE->DCFG |= 0x4000; // BIT 14
 
        USBD_Start(&hUsbDeviceHS);
 
index 2cf9f2ee00cd34791e8407156e16cd46a5f81fa6..87715b84969dbce2c91f3ac7ab5d65eb3891a8c6 100755 (executable)
@@ -26,6 +26,9 @@
 #include "usbd_desc.h"
 #include "usbd_ctlreq.h"
 
+#define MSC_MAX_FS_PACKET 64
+#define MSC_MAX_HS_PACKET 512
+
 
 // Support 2 USB devices.
 __ALIGN_BEGIN static USBD_CompositeClassData fsClassData __ALIGN_END;
@@ -37,7 +40,8 @@ static uint8_t  USBD_Composite_DeInit (USBD_HandleTypeDef *pdev, uint8_t cfgidx)
 
 static uint8_t  USBD_Composite_Setup (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
 
-static uint8_t  *USBD_Composite_GetCfgDesc (uint16_t *length);
+static uint8_t  *USBD_Composite_GetHSCfgDesc (uint16_t *length);
+static uint8_t  *USBD_Composite_GetFSCfgDesc (uint16_t *length);
 
 static uint8_t  *USBD_Composite_GetDeviceQualifierDesc (uint16_t *length);
 
@@ -57,13 +61,102 @@ USBD_ClassTypeDef USBD_Composite =
        NULL, /*SOF */
        NULL,
        NULL,      
-       USBD_Composite_GetCfgDesc,
-       USBD_Composite_GetCfgDesc,
-       USBD_Composite_GetCfgDesc,
+       USBD_Composite_GetHSCfgDesc,
+       USBD_Composite_GetFSCfgDesc,
+       USBD_Composite_GetFSCfgDesc, // "Other" speed
        USBD_Composite_GetDeviceQualifierDesc,
 };
 
-__ALIGN_BEGIN static uint8_t USBD_Composite_CfgDesc[USB_COMPOSITE_CONFIG_DESC_SIZ]  __ALIGN_END =
+__ALIGN_BEGIN static uint8_t USBD_Composite_CfgHSDesc[USB_COMPOSITE_CONFIG_DESC_SIZ]  __ALIGN_END =
+{
+  0x09, /* bLength: Configuration Descriptor size */
+  USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
+  USB_COMPOSITE_CONFIG_DESC_SIZ,
+  /* wTotalLength: Bytes returned */
+  0x00,
+  0x02,         /*bNumInterfaces: 1 interface*/
+  0x01,         /*bConfigurationValue: Configuration value*/
+  0x00,         /*iConfiguration: Index of string descriptor describing
+  the configuration*/
+  0x80,         /*bmAttributes: bus powered */
+  0xFA,         /*MaxPower 500 mA: this current is used for detecting Vbus*/
+  
+  /************** Descriptor of GENERIC interface ****************/
+  /* 09 */
+  0x09,         /*bLength: Interface Descriptor size*/
+  USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/
+  0x00,         /*bInterfaceNumber: Number of Interface*/
+  0x00,         /*bAlternateSetting: Alternate setting*/
+  0x02,         /*bNumEndpoints*/
+  0x03,         /*bInterfaceClass: HID*/
+  0x00,         /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
+  0x00,         /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
+  0,            /*iInterface: Index of string descriptor*/
+  /******************** Descriptor of GENERIC HID ********************/
+  /* 18 */
+  0x09,         /*bLength: HID Descriptor size*/
+  HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/
+  0x11,         /*bcdHID: HID Class Spec release number*/
+  0x01,
+  0x00,         /*bCountryCode: Hardware target country*/
+  0x01,         /*bNumDescriptors: Number of HID class descriptors to follow*/
+  0x22,         /*bDescriptorType*/
+  HID_GENERIC_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/
+  0x00,
+  /******************** Descriptor of Generic HID endpoint ********************/
+  /* 27 */
+  0x07,          /*bLength: Endpoint Descriptor size*/
+  USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
+  
+  HID_EPIN_ADDR,     /*bEndpointAddress: Endpoint Address (IN)*/
+  0x03,          /*bmAttributes: Interrupt endpoint*/
+  HID_EPIN_SIZE, /*wMaxPacketSize: 64 Byte max */
+  0x00,
+  HID_HS_BINTERVAL,          /*bInterval*/
+  /* 34 */
+
+  /******************** Descriptor of GENERIC HID endpoint ********************/
+  /* 34 */
+  0x07,          /*bLength: Endpoint Descriptor size*/
+  USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
+  
+  HID_EPOUT_ADDR,    /*bEndpointAddress: Endpoint Address (OUT)*/
+  0x03,          /*bmAttributes: Interrupt endpoint*/
+  HID_EPOUT_SIZE, /*wMaxPacketSize */
+  0x00,
+  HID_HS_BINTERVAL,          /*bInterval*/
+  /* 41 */
+
+  /********************  Mass Storage interface ********************/
+  0x09,   /* bLength: Interface Descriptor size */
+  USB_DESC_TYPE_INTERFACE,   /* bDescriptorType: */
+  0x01,   /* bInterfaceNumber: Number of Interface */
+  0x00,   /* bAlternateSetting: Alternate setting */
+  0x02,   /* bNumEndpoints*/
+  0x08,   /* bInterfaceClass: MSC Class */
+  0x06,   /* bInterfaceSubClass : SCSI transparent*/
+  0x50,   /* nInterfaceProtocol */
+  0x00,          /* iInterface: */
+  /********************  Mass Storage Endpoints ********************/
+  0x07,   /*Endpoint descriptor length = 7*/
+  0x05,   /*Endpoint descriptor type */
+  MSC_EPIN_ADDR,   /*Endpoint address */
+  0x02,   /*Bulk endpoint type */
+  LOBYTE(MSC_MAX_HS_PACKET),
+  HIBYTE(MSC_MAX_HS_PACKET),
+  0x00,   /*Polling interval in milliseconds */
+  
+  0x07,   /*Endpoint descriptor length = 7 */
+  0x05,   /*Endpoint descriptor type */
+  MSC_EPOUT_ADDR,   /*Endpoint address  */
+  0x02,   /*Bulk endpoint type */
+  LOBYTE(MSC_MAX_HS_PACKET),
+  HIBYTE(MSC_MAX_HS_PACKET),
+  0x00     /*Polling interval in milliseconds*/
+
+};
+
+__ALIGN_BEGIN static uint8_t USBD_Composite_CfgFSDesc[USB_COMPOSITE_CONFIG_DESC_SIZ]  __ALIGN_END =
 {
   0x09, /* bLength: Configuration Descriptor size */
   USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
@@ -162,7 +255,7 @@ __ALIGN_BEGIN static uint8_t USBD_Composite_DeviceQualifierDesc[USB_LEN_DEV_QUAL
   0x00,
   0x00,
   0x00,
-  0x40,
+  MSC_MAX_FS_PACKET,
   0x01,
   0x00,
 };
@@ -176,20 +269,22 @@ static uint8_t  USBD_Composite_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
        USBD_LL_OpenEP(pdev, HID_EPIN_ADDR, USBD_EP_TYPE_INTR, HID_EPIN_SIZE);
        USBD_LL_OpenEP(pdev, HID_EPOUT_ADDR, USBD_EP_TYPE_INTR, HID_EPOUT_SIZE);
 
-
-       // MSC Endpoints
-       USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_FS_PACKET);
-       USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_FS_PACKET);
-
-
        USBD_CompositeClassData* classData;
-       if (pdev->id == DEVICE_HS)
+    if(pdev->dev_speed == USBD_SPEED_HIGH)
        {
                classData = &hsClassData;
+
+           // MSC Endpoints
+       USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_HS_PACKET);
+       USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_HS_PACKET);
        }
        else
        {
                classData = &fsClassData;
+
+           // MSC Endpoints
+       USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_FS_PACKET);
+       USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_FS_PACKET);
        }
        classData->hid.state = HID_IDLE;
        classData->hid.reportReady = 0;
@@ -335,11 +430,19 @@ static uint8_t USBD_Composite_Setup(
                                switch ((uint8_t)req->wIndex)
                                {
                                case MSC_EPIN_ADDR:
-                                       USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_FS_PACKET);
+                                       USBD_LL_OpenEP(
+                        pdev,
+                        MSC_EPIN_ADDR,
+                        USBD_EP_TYPE_BULK,
+                        pdev->dev_speed == USBD_SPEED_HIGH ? MSC_MAX_HS_PACKET : MSC_MAX_FS_PACKET);
                                        break;
 
                                case MSC_EPOUT_ADDR:
-                                       USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_FS_PACKET);  
+                                       USBD_LL_OpenEP(
+                        pdev,
+                        MSC_EPOUT_ADDR,
+                        USBD_EP_TYPE_BULK,
+                        pdev->dev_speed == USBD_SPEED_HIGH ? MSC_MAX_HS_PACKET : MSC_MAX_FS_PACKET);
                                        break;
 
                                case HID_EPIN_ADDR:
@@ -418,9 +521,15 @@ static uint8_t *USBD_Composite_GetDeviceQualifierDesc (uint16_t *length)
        return USBD_Composite_DeviceQualifierDesc;
 }
 
+uint8_t  *USBD_Composite_GetHSCfgDesc (uint16_t *length)
+{
+  *length = sizeof (USBD_Composite_CfgHSDesc);
+  return USBD_Composite_CfgHSDesc;
+}
 
-static uint8_t *USBD_Composite_GetCfgDesc (uint16_t *length)
+uint8_t  *USBD_Composite_GetFSCfgDesc (uint16_t *length)
 {
-       *length = sizeof (USBD_Composite_CfgDesc);
-       return USBD_Composite_CfgDesc;
+  *length = sizeof (USBD_Composite_CfgFSDesc);
+  return USBD_Composite_CfgFSDesc;
 }
+
index bc017f8be696cfbe31f5bac929a6bb63c2be1647..dd803998525503ccc6140bbb90936e92fbfa04c1 100755 (executable)
@@ -63,7 +63,7 @@
 #define USBD_PRODUCT_STRING_FS           (uint8_t*)"SCSI2SD 2020"
 #define USBD_CONFIGURATION_STRING_FS     (uint8_t*)"SCSI2SD Config"
 #define USBD_INTERFACE_STRING_FS         (uint8_t*)"SCSI2SD Interface"
-
+#define USB_SIZ_BOS_DESC                 0x0C
 /**
   * @}
   */ 
@@ -89,10 +89,10 @@ uint8_t *     USBD_FS_ProductStrDescriptor ( USBD_SpeedTypeDef speed , uint16_t
 uint8_t *     USBD_FS_SerialStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
 uint8_t *     USBD_FS_ConfigStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
 uint8_t *     USBD_FS_InterfaceStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
+#if (USBD_LPM_ENABLED == 1)
+uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
+#endif /* (USBD_LPM_ENABLED == 1) */
 
-#ifdef USB_SUPPORT_USER_STRING_DESC
-uint8_t *     USBD_FS_USRStringDesc (USBD_SpeedTypeDef speed, uint8_t idx , uint16_t *length);  
-#endif /* USB_SUPPORT_USER_STRING_DESC */  
 
 USBD_DescriptorsTypeDef FS_Desc =
 {
@@ -103,6 +103,23 @@ USBD_DescriptorsTypeDef FS_Desc =
   USBD_FS_SerialStrDescriptor,
   USBD_FS_ConfigStrDescriptor,
   USBD_FS_InterfaceStrDescriptor,
+#if (USBD_LPM_ENABLED == 1)
+  USBD_FS_USR_BOSDescriptor
+#endif /* (USBD_LPM_ENABLED == 1) */
+};
+
+USBD_DescriptorsTypeDef HS_Desc =
+{
+  USBD_FS_DeviceDescriptor,
+  USBD_FS_LangIDStrDescriptor, 
+  USBD_FS_ManufacturerStrDescriptor,
+  USBD_FS_ProductStrDescriptor,
+  USBD_FS_SerialStrDescriptor,
+  USBD_FS_ConfigStrDescriptor,
+  USBD_FS_InterfaceStrDescriptor,
+#if (USBD_LPM_ENABLED == 1)
+  USBD_FS_USR_BOSDescriptor
+#endif /* (USBD_LPM_ENABLED == 1) */
 };
 
 #if defined ( __ICCARM__ ) /*!< IAR Compiler */
@@ -113,7 +130,13 @@ __ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
   {
     0x12,                       /*bLength */
     USB_DESC_TYPE_DEVICE,       /*bDescriptorType*/
-    0x00,                       /* bcdUSB */  
+#if (USBD_LPM_ENABLED == 1)
+    0x01,                       /*bcdUSB */ /* changed to USB version 2.01
+                                               in order to support LPM L1 suspend
+                                               resume test of USBCV3.0*/
+#else
+    0x00,                       /*bcdUSB */
+#endif /* (USBD_LPM_ENABLED == 1) */
     0x02,
     0x00,                       /*bDeviceClass*/
     0x00,                       /*bDeviceSubClass*/
@@ -132,9 +155,25 @@ __ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
   } ; 
 /* USB_DeviceDescriptor */
 
-#if defined ( __ICCARM__ ) /*!< IAR Compiler */
-  #pragma data_alignment=4   
-#endif
+/** BOS descriptor. */
+#if (USBD_LPM_ENABLED == 1)
+__ALIGN_BEGIN uint8_t USBD_FS_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END =
+{
+  0x5,
+  USB_DESC_TYPE_BOS,
+  0xC,
+  0x0,
+  0x1,  /* 1 device capability*/
+        /* device capability*/
+  0x7,
+  USB_DEVICE_CAPABITY_TYPE,
+  0x2,
+  0x2,  /* LPM capability bit set*/
+  0x0,
+  0x0,
+  0x0
+};
+#endif /* (USBD_LPM_ENABLED == 1) */
 
 /* USB Standard Device Descriptor */
 __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
@@ -346,6 +385,22 @@ uint8_t *  USBD_FS_InterfaceStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *l
   }
   return USBD_StrDesc;  
 }
+
+#if (USBD_LPM_ENABLED == 1)
+/**
+  * @brief  Return the BOS descriptor
+  * @param  speed : Current device speed
+  * @param  length : Pointer to data length variable
+  * @retval Pointer to descriptor buffer
+  */
+uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
+{
+  UNUSED(speed);
+  *length = sizeof(USBD_FS_BOSDesc);
+  return (uint8_t*)USBD_FS_BOSDesc;
+}
+#endif /* (USBD_LPM_ENABLED == 1) */
+
 /**
   * @}
   */ 
index de7b0256c0502f83810fe021648533cc2a141ad5..64a352663d79a8bfa44c325cd4d9f763e8a17751 100755 (executable)
@@ -76,7 +76,7 @@
   * @{
   */ 
 extern USBD_DescriptorsTypeDef FS_Desc;
-//extern USBD_DescriptorsTypeDef HS_Desc;
+extern USBD_DescriptorsTypeDef HS_Desc;
 /**
   * @}
   */ 
index bd0e55f87bece4abd805f6c32c4b62c47a45f207..75fc2d10d98088ca40dcf1d5ea92858722085be3 100755 (executable)
@@ -51,7 +51,7 @@
 #define HID_REPORT_DESC               0x22
 
 #define HID_FS_BINTERVAL               0x20 // 32ms
-#define HID_HS_BINTERVAL               0x20 // 32ms
+#define HID_HS_BINTERVAL               0x9
 
 #define HID_REQ_SET_PROTOCOL          0x0B
 #define HID_REQ_GET_PROTOCOL          0x03
index 0c7902d21714baf54f47827e3520fa5ab4e73580..9c7a8faaafc2c8e5350e806277f9ee253c5bfa37 100755 (executable)
@@ -51,7 +51,6 @@
 /** @defgroup USBD_BOT_Exported_Defines
   * @{
   */ 
-#define MSC_MAX_FS_PACKET            0x40
 
 #define BOT_GET_MAX_LUN              0xFE
 #define BOT_RESET                    0xFF
index 79d4141f0ce848382878e204f638c6c24b63a945..afa86e67c538adc27018115b59b4e7ad48b9326d 100755 (executable)
@@ -113,7 +113,7 @@ int8_t s2s_usbd_storage_GetCapacity (uint8_t lun, uint32_t *block_num, uint16_t
 
        *block_num  = capacity;
        *block_size = cfg->bytesPerSector;
-       return (0);
+       return capacity ? 0 : 1;
 }
 
 uint32_t s2s_usbd_storage_Inquiry (uint8_t lun, uint8_t* buf, uint8_t maxlen)
@@ -248,6 +248,11 @@ int8_t s2s_usbd_storage_GetMaxLun (void)
 
 void s2s_initUsbDeviceStorage(void)
 {
+#ifdef S2S_USB_FS
        USBD_MSC_RegisterStorage(&hUsbDeviceFS, &USBD_MSC_SD_fops);
+#endif
+#ifdef S2S_USB_HS
+       USBD_MSC_RegisterStorage(&hUsbDeviceHS, &USBD_MSC_SD_fops);
+#endif
 }