Selaa lähdekoodia

2021 USB HS interface now working, but SD card initialisation is broken

Michael McMaster 4 vuotta sitten
vanhempi
sitoutus
931b30f235

+ 2 - 2
lib/SCSI2SD/Makefile.2021

@@ -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 \

+ 1 - 1
lib/SCSI2SD/src/firmware/config.c

@@ -44,7 +44,7 @@ extern uint8_t* __fixed_config;
 #ifdef S2S_USB_HS
 #define configUsbDev hUsbDeviceHS
 #else
-#define configUsbDev configUsbDev
+#define configUsbDev hUsbDeviceFS
 #endif
 
 // 1 flash row

+ 1 - 1
lib/SCSI2SD/src/firmware/main.c

@@ -105,7 +105,7 @@ void mainLoop()
 #ifdef S2S_USB_FS
 	s2s_usbDevicePoll(&hUsbDeviceFS);
 #endif
-#ifdef S2S_USB_FS
+#ifdef S2S_USB_HS
 	s2s_usbDevicePoll(&hUsbDeviceHS);
 #endif
 

+ 6 - 5
lib/SCSI2SD/src/firmware/usb_device/usb_device.c

@@ -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);
 

+ 127 - 18
lib/SCSI2SD/src/firmware/usb_device/usbd_composite.c

@@ -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;
 }
+

+ 63 - 8
lib/SCSI2SD/src/firmware/usb_device/usbd_desc.c

@@ -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) */
+
 /**
   * @}
   */ 

+ 1 - 1
lib/SCSI2SD/src/firmware/usb_device/usbd_desc.h

@@ -76,7 +76,7 @@
   * @{
   */ 
 extern USBD_DescriptorsTypeDef FS_Desc;
-//extern USBD_DescriptorsTypeDef HS_Desc;
+extern USBD_DescriptorsTypeDef HS_Desc;
 /**
   * @}
   */ 

+ 1 - 1
lib/SCSI2SD/src/firmware/usb_device/usbd_hid.h

@@ -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

+ 0 - 1
lib/SCSI2SD/src/firmware/usb_device/usbd_msc.h

@@ -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

+ 6 - 1
lib/SCSI2SD/src/firmware/usb_device/usbd_msc_storage_sd.c

@@ -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
 }