Bläddra i källkod

USB问题待修复

樊春春 2 år sedan
förälder
incheckning
239a31e276

+ 553 - 0
User/Bsp/usbh/usb_bsp.c

@@ -0,0 +1,553 @@
+/**
+ ******************************************************************************
+ * @file    usb_bsp.c
+ * @author  MCD Application Team
+ * @version V2.1.0
+ * @date    19-March-2012
+ * @brief   This file implements the board support package for the USB host library
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
+ *
+ * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *        http://www.st.com/software_license_agreement_liberty_v2
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************
+ */
+
+/* Includes ------------------------------------------------------------------*/
+
+#include "usb_bsp.h"
+
+/** @addtogroup USBH_USER
+ * @{
+ */
+
+/** @defgroup USB_BSP
+ * @brief This file is responsible to offer board support package
+ * @{
+ */
+
+/** @defgroup USB_BSP_Private_Defines
+ * @{
+ */
+#define USE_ACCURATE_TIME
+#define TIM_MSEC_DELAY 0x01
+#define TIM_USEC_DELAY 0x02
+
+#if 0 /* 安富莱STM32-V5 不支持USB HOST接口的供电控制和过流检测 */
+
+#define HOST_OVRCURR_PORT        GPIOE
+#define HOST_OVRCURR_LINE        GPIO_Pin_1
+#define HOST_OVRCURR_PORT_SOURCE GPIO_PortSourceGPIOE
+#define HOST_OVRCURR_PIN_SOURCE  GPIO_PinSource1
+#define HOST_OVRCURR_PORT_RCC    RCC_APB2Periph_GPIOE
+#define HOST_OVRCURR_EXTI_LINE   EXTI_Line1
+#define HOST_OVRCURR_IRQn        EXTI1_IRQn
+
+#ifdef USE_STM3210C_EVAL
+#define HOST_POWERSW_PORT_RCC RCC_APB2Periph_GPIOC
+#define HOST_POWERSW_PORT     GPIOC
+#define HOST_POWERSW_VBUS     GPIO_Pin_9
+#else
+#ifdef USE_USB_OTG_FS
+#define HOST_POWERSW_PORT_RCC RCC_AHB1Periph_GPIOH
+#define HOST_POWERSW_PORT     GPIOH
+#define HOST_POWERSW_VBUS     GPIO_Pin_5
+#endif
+#endif
+
+#define HOST_SOF_OUTPUT_RCC RCC_APB2Periph_GPIOA
+#define HOST_SOF_PORT       GPIOA
+#define HOST_SOF_SIGNAL     GPIO_Pin_8
+
+#endif
+
+/**
+ * @}
+ */
+
+/** @defgroup USB_BSP_Private_TypesDefinitions
+ * @{
+ */
+/**
+ * @}
+ */
+
+/** @defgroup USB_BSP_Private_Macros
+ * @{
+ */
+/**
+ * @}
+ */
+
+/** @defgroup USBH_BSP_Private_Variables
+ * @{
+ */
+ErrorStatus HSEStartUpStatus;
+#ifdef USE_ACCURATE_TIME
+__IO uint32_t BSP_delay = 0;
+#endif
+/**
+ * @}
+ */
+
+/** @defgroup USBH_BSP_Private_FunctionPrototypes
+ * @{
+ */
+
+#ifdef USE_ACCURATE_TIME
+static void BSP_SetTime(uint8_t Unit);
+static void BSP_Delay(uint32_t nTime, uint8_t Unit);
+static void USB_OTG_BSP_TimeInit(void);
+#endif
+/**
+ * @}
+ */
+
+/** @defgroup USB_BSP_Private_Functions
+ * @{
+ */
+
+/**
+ * @brief  USB_OTG_BSP_Init
+ *         Initilizes BSP configurations
+ * @param  None
+ * @retval None
+ */
+
+void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev)
+{
+
+    // EXTI_InitTypeDef EXTI_InitStructure;
+#ifdef USE_STM3210C_EVAL
+
+    RCC_OTGFSCLKConfig(RCC_OTGFSCLKSource_PLLVCO_Div3);
+    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS, ENABLE);
+
+#else // USE_STM322xG_EVAL
+    GPIO_InitTypeDef GPIO_InitStructure;
+#ifdef USE_USB_OTG_FS
+
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
+
+    /* Configure SOF VBUS ID DM DP Pins */
+    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11 | GPIO_Pin_12;
+
+    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
+    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
+    GPIO_Init(GPIOA, &GPIO_InitStructure);
+
+    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
+
+    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN;
+    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
+    GPIO_Init(GPIOA, &GPIO_InitStructure);
+
+    GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_OTG1_FS);
+    GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_OTG1_FS);
+    GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_OTG1_FS);
+
+    /* this for ID line debug */
+
+    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_10;
+    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
+    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
+    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_Init(GPIOA, &GPIO_InitStructure);
+    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_OTG1_FS);
+
+    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
+    RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE);
+#else // USE_USB_OTG_HS
+
+#ifdef USE_ULPI_PHY // ULPI
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB |
+                               RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOH |
+                               RCC_AHB1Periph_GPIOI,
+                           ENABLE);
+
+    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_OTG2_HS);  // D0
+    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_OTG2_HS);  // CLK
+    GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_OTG2_HS);  // D1
+    GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_OTG2_HS);  // D2
+    GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_OTG2_HS);  // D7
+    GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_OTG2_HS); // D3
+    GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_OTG2_HS); // D4
+    GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_OTG2_HS); // D5
+    GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_OTG2_HS); // D6
+    GPIO_PinAFConfig(GPIOH, GPIO_PinSource4, GPIO_AF_OTG2_HS);  // NXT
+    GPIO_PinAFConfig(GPIOI, GPIO_PinSource11, GPIO_AF_OTG2_HS); // DIR
+    GPIO_PinAFConfig(GPIOC, GPIO_PinSource0, GPIO_AF_OTG2_HS);  // STP
+
+    // CLK
+    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_5;
+    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
+    GPIO_Init(GPIOA, &GPIO_InitStructure);
+
+    // D0
+    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_3;
+    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
+    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
+    GPIO_Init(GPIOA, &GPIO_InitStructure);
+
+    // D1 D2 D3 D4 D5 D6 D7
+    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 |
+                                  GPIO_Pin_5 | GPIO_Pin_10 |
+                                  GPIO_Pin_11 | GPIO_Pin_12 |
+                                  GPIO_Pin_13;
+
+    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
+    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
+    GPIO_Init(GPIOB, &GPIO_InitStructure);
+
+    // STP
+    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_0;
+    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
+    GPIO_Init(GPIOC, &GPIO_InitStructure);
+
+    // NXT
+    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_4;
+    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
+    GPIO_Init(GPIOH, &GPIO_InitStructure);
+
+    // DIR
+    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_11;
+    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
+    GPIO_Init(GPIOI, &GPIO_InitStructure);
+
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_OTG_HS |
+                               RCC_AHB1Periph_OTG_HS_ULPI,
+                           ENABLE);
+
+#else
+
+#if 1
+    /* 安富莱只使用2根线 PB14/OTG_HS_DM 和 PB15/OTG_HS_DP 接U盘 */
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
+
+    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15;
+
+    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
+    GPIO_Init(GPIOB, &GPIO_InitStructure);
+
+    GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_OTG2_FS);
+    GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_OTG2_FS);
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_OTG_HS, ENABLE);
+#else
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
+
+    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 |
+                                  GPIO_Pin_13 |
+                                  GPIO_Pin_14 |
+                                  GPIO_Pin_15;
+
+    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
+    GPIO_Init(GPIOB, &GPIO_InitStructure);
+
+    GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_OTG2_FS);
+    GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_OTG2_FS);
+    GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_OTG2_FS);
+    GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_OTG2_FS);
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_OTG_HS, ENABLE);
+#endif
+
+#endif
+#endif // USB_OTG_HS
+#endif // USE_STM322xG_EVAL
+
+    /* Intialize Timer for delay function */
+    USB_OTG_BSP_TimeInit();
+}
+/**
+ * @brief  USB_OTG_BSP_EnableInterrupt
+ *         Configures USB Global interrupt
+ * @param  None
+ * @retval None
+ */
+void USB_OTG_BSP_EnableInterrupt(USB_OTG_CORE_HANDLE *pdev)
+{
+    NVIC_InitTypeDef NVIC_InitStructure;
+
+    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
+#ifdef USE_USB_OTG_HS
+    NVIC_InitStructure.NVIC_IRQChannel = OTG_HS_IRQn;
+#else
+    NVIC_InitStructure.NVIC_IRQChannel = OTG_FS_IRQn;
+#endif
+    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
+    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 3;
+    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
+    NVIC_Init(&NVIC_InitStructure);
+}
+
+/**
+ * @brief  BSP_Drive_VBUS
+ *         Drives the Vbus signal through IO
+ * @param  state : VBUS states
+ * @retval None
+ */
+
+void USB_OTG_BSP_DriveVBUS(USB_OTG_CORE_HANDLE *pdev, uint8_t state)
+{
+#if 0 /* 安富莱STM32-F4开发板不支持对外供电的控制 */
+  /*
+  On-chip 5 V VBUS generation is not supported. For this reason, a charge pump 
+  or, if 5 V are available on the application board, a basic power switch, must 
+  be added externally to drive the 5 V VBUS line. The external charge pump can 
+  be driven by any GPIO output. When the application decides to power on VBUS 
+  using the chosen GPIO, it must also set the port power bit in the host port 
+  control and status register (PPWR bit in OTG_FS_HPRT).
+  
+  Bit 12 PPWR: Port power
+  The application uses this field to control power to this port, and the core 
+  clears this bit on an overcurrent condition.
+  */
+
+#ifndef USE_USB_OTG_HS   
+  if (0 == state)
+  { 
+    /* DISABLE is needed on output of the Power Switch */
+    GPIO_SetBits(HOST_POWERSW_PORT, HOST_POWERSW_VBUS);
+  }
+  else
+  {
+    /*ENABLE the Power Switch by driving the Enable LOW */
+    GPIO_ResetBits(HOST_POWERSW_PORT, HOST_POWERSW_VBUS);
+  }
+#endif
+#endif
+}
+
+/**
+ * @brief  USB_OTG_BSP_ConfigVBUS
+ *         Configures the IO for the Vbus and OverCurrent
+ * @param  None
+ * @retval None
+ */
+
+void USB_OTG_BSP_ConfigVBUS(USB_OTG_CORE_HANDLE *pdev)
+{
+#if 0 /* 安富莱STM32-F4 不支持VBUS */
+#ifndef USE_USB_OTG_HS 
+  GPIO_InitTypeDef GPIO_InitStructure;
+
+#ifdef USE_STM3210C_EVAL
+  RCC_APB2PeriphClockCmd(HOST_POWERSW_PORT_RCC, ENABLE);
+  
+  
+  /* Configure Power Switch Vbus Pin */
+  GPIO_InitStructure.GPIO_Pin = HOST_POWERSW_VBUS;
+  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
+  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
+  GPIO_Init(HOST_POWERSW_PORT, &GPIO_InitStructure);
+#else
+#ifdef USE_USB_OTG_FS  
+  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOH , ENABLE);  
+  
+  GPIO_InitStructure.GPIO_Pin = HOST_POWERSW_VBUS;
+  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
+  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
+  GPIO_Init(HOST_POWERSW_PORT,&GPIO_InitStructure);
+#endif
+#endif
+
+  /* By Default, DISABLE is needed on output of the Power Switch */
+  GPIO_SetBits(HOST_POWERSW_PORT, HOST_POWERSW_VBUS);
+  
+  USB_OTG_BSP_mDelay(200);   /* Delay is need for stabilising the Vbus Low 
+  in Reset Condition, when Vbus=1 and Reset-button is pressed by user */
+#endif
+#endif
+}
+
+/**
+ * @brief  USB_OTG_BSP_TimeInit
+ *         Initializes delay unit using Timer2
+ * @param  None
+ * @retval None
+ */
+static void USB_OTG_BSP_TimeInit(void)
+{
+#ifdef USE_ACCURATE_TIME
+    NVIC_InitTypeDef NVIC_InitStructure;
+
+    /* Set the Vector Table base address at 0x08000000 */
+    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x00);
+
+    /* Configure the Priority Group to 2 bits */
+    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
+
+    /* Enable the TIM2 gloabal Interrupt */
+    NVIC_InitStructure.NVIC_IRQChannel                   = TIM2_IRQn;
+    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
+    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
+    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
+
+    NVIC_Init(&NVIC_InitStructure);
+
+    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
+#endif
+}
+
+/**
+ * @brief  USB_OTG_BSP_uDelay
+ *         This function provides delay time in micro sec
+ * @param  usec : Value of delay required in micro sec
+ * @retval None
+ */
+void USB_OTG_BSP_uDelay(const uint32_t usec)
+{
+
+#ifdef USE_ACCURATE_TIME
+    BSP_Delay(usec, TIM_USEC_DELAY);
+#else
+    __IO uint32_t  count               = 0;
+    const uint32_t utime               = (120 * usec / 7);
+    do
+    {
+        if (++count > utime)
+        {
+            return;
+        }
+    } while (1);
+#endif
+}
+
+/**
+ * @brief  USB_OTG_BSP_mDelay
+ *          This function provides delay time in milli sec
+ * @param  msec : Value of delay required in milli sec
+ * @retval None
+ */
+void USB_OTG_BSP_mDelay(const uint32_t msec)
+{
+#ifdef USE_ACCURATE_TIME
+    BSP_Delay(msec, TIM_MSEC_DELAY);
+#else
+    USB_OTG_BSP_uDelay(msec * 1000);
+#endif
+}
+
+/**
+ * @brief  USB_OTG_BSP_TimerIRQ
+ *         Time base IRQ
+ * @param  None
+ * @retval None
+ */
+
+void USB_OTG_BSP_TimerIRQ(void)
+{
+#ifdef USE_ACCURATE_TIME
+
+    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
+    {
+        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
+        if (BSP_delay > 0x00)
+        {
+            BSP_delay--;
+        }
+        else
+        {
+            TIM_Cmd(TIM2, DISABLE);
+        }
+    }
+#endif
+}
+
+#ifdef USE_ACCURATE_TIME
+/**
+ * @brief  BSP_Delay
+ *         Delay routine based on TIM2
+ * @param  nTime : Delay Time
+ * @param  unit : Delay Time unit : mili sec / micro sec
+ * @retval None
+ */
+static void BSP_Delay(uint32_t nTime, uint8_t unit)
+{
+
+    BSP_delay = nTime;
+    BSP_SetTime(unit);
+    while (BSP_delay != 0)
+        ;
+    TIM_Cmd(TIM2, DISABLE);
+}
+
+/**
+ * @brief  BSP_SetTime
+ *         Configures TIM2 for delay routine based on TIM2
+ * @param  unit : msec /usec
+ * @retval None
+ */
+static void BSP_SetTime(uint8_t unit)
+{
+    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
+
+    TIM_Cmd(TIM2, DISABLE);
+    TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
+
+    if (unit == TIM_USEC_DELAY)
+    {
+        TIM_TimeBaseStructure.TIM_Period = 11;
+    }
+    else if (unit == TIM_MSEC_DELAY)
+    {
+        TIM_TimeBaseStructure.TIM_Period = 11999;
+    }
+    TIM_TimeBaseStructure.TIM_Prescaler     = 5;
+    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
+    TIM_TimeBaseStructure.TIM_CounterMode   = TIM_CounterMode_Up;
+
+    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
+    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
+
+    TIM_ARRPreloadConfig(TIM2, ENABLE);
+
+    /* TIM IT enable */
+    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
+
+    /* TIM2 enable counter */
+    TIM_Cmd(TIM2, ENABLE);
+}
+
+#endif
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 275 - 0
User/Bsp/usbh/usb_conf.h

@@ -0,0 +1,275 @@
+/**
+ ******************************************************************************
+ * @file    usb_conf.h
+ * @author  MCD Application Team
+ * @version V2.1.0
+ * @date    19-March-2012
+ * @brief   General low level driver configuration
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
+ *
+ * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *        http://www.st.com/software_license_agreement_liberty_v2
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************
+ */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USB_CONF__H__
+#define __USB_CONF__H__
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm32f4xx.h"
+#if 0 /* 不需要定义ST官方的板号 */
+#if defined(USE_STM322xG_EVAL)
+#include "stm322xg_eval.h"
+#include "stm322xg_eval_ioe.h"
+#include "stm322xg_eval_lcd.h"
+#include "stm322xg_eval_sdio_sd.h"
+#elif defined(USE_STM324xG_EVAL)
+#include "stm324xg_eval.h"
+#include "stm324xg_eval_ioe.h"
+#include "stm324xg_eval_lcd.h"
+#include "stm324xg_eval_sdio_sd.h"
+#include "stm32f4xx.h"
+#elif defined(USE_STM3210C_EVAL)
+#include "stm3210c_eval.h"
+#include "stm3210c_eval_ioe.h"
+#include "stm3210c_eval_lcd.h"
+#include "stm3210c_eval_spi_sd.h"
+#include "stm32f10x.h"
+#else
+#error "Missing define: Evaluation board (ie. USE_STM322xG_EVAL)"
+#endif
+
+#endif
+
+/** @addtogroup USB_OTG_DRIVER
+ * @{
+ */
+
+/** @defgroup USB_CONF
+ * @brief USB low level driver configuration file
+ * @{
+ */
+
+/** @defgroup USB_CONF_Exported_Defines
+ * @{
+ */
+
+/* USB Core and PHY interface configuration.
+   Tip: To avoid modifying these defines each time you need to change the USB
+        configuration, you can declare the needed define in your toolchain
+        compiler preprocessor.
+   */
+/****************** USB OTG FS PHY CONFIGURATION *******************************
+ *  The USB OTG FS Core supports one on-chip Full Speed PHY.
+ *
+ *  The USE_EMBEDDED_PHY symbol is defined in the project compiler preprocessor
+ *  when FS core is used.
+ *******************************************************************************/
+#ifndef USE_USB_OTG_FS
+//#define USE_USB_OTG_FS
+#endif /* USE_USB_OTG_FS */
+
+#ifdef USE_USB_OTG_FS
+#define USB_OTG_FS_CORE
+#endif
+
+/****************** USB OTG HS PHY CONFIGURATION *******************************
+ *  The USB OTG HS Core supports two PHY interfaces:
+ *   (i)  An ULPI interface for the external High Speed PHY: the USB HS Core will
+ *        operate in High speed mode
+ *   (ii) An on-chip Full Speed PHY: the USB HS Core will operate in Full speed mode
+ *
+ *  You can select the PHY to be used using one of these two defines:
+ *   (i)  USE_ULPI_PHY: if the USB OTG HS Core is to be used in High speed mode
+ *   (ii) USE_EMBEDDED_PHY: if the USB OTG HS Core is to be used in Full speed mode
+ *
+ *  Notes:
+ *   - The USE_ULPI_PHY symbol is defined in the project compiler preprocessor as
+ *     default PHY when HS core is used.
+ *   - On STM322xG-EVAL and STM324xG-EVAL boards, only configuration(i) is available.
+ *     Configuration (ii) need a different hardware, for more details refer to your
+ *     STM32 device datasheet.
+ *******************************************************************************/
+#ifndef USE_USB_OTG_HS
+//#define USE_USB_OTG_HS
+#endif /* USE_USB_OTG_HS */
+
+#ifndef USE_ULPI_PHY
+//#define USE_ULPI_PHY
+#endif /* USE_ULPI_PHY */
+
+#ifndef USE_EMBEDDED_PHY
+//#define USE_EMBEDDED_PHY
+#endif /* USE_EMBEDDED_PHY */
+
+#ifdef USE_USB_OTG_HS
+#define USB_OTG_HS_CORE
+#endif
+
+/*******************************************************************************
+ *                     FIFO Size Configuration in Host mode
+ *
+ *  (i) Receive data FIFO size = (Largest Packet Size / 4) + 1 or
+ *                             2x (Largest Packet Size / 4) + 1,  If a
+ *                             high-bandwidth channel or multiple isochronous
+ *                             channels are enabled
+ *
+ *  (ii) For the host nonperiodic Transmit FIFO is the largest maximum packet size
+ *      for all supported nonperiodic OUT channels. Typically, a space
+ *      corresponding to two Largest Packet Size is recommended.
+ *
+ *  (iii) The minimum amount of RAM required for Host periodic Transmit FIFO is
+ *        the largest maximum packet size for all supported periodic OUT channels.
+ *        If there is at least one High Bandwidth Isochronous OUT endpoint,
+ *        then the space must be at least two times the maximum packet size for
+ *        that channel.
+ *******************************************************************************/
+
+/****************** USB OTG HS CONFIGURATION **********************************/
+#ifdef USB_OTG_HS_CORE
+#define RX_FIFO_HS_SIZE   512
+#define TXH_NP_HS_FIFOSIZ 256
+#define TXH_P_HS_FIFOSIZ  256
+
+// #define USB_OTG_HS_LOW_PWR_MGMT_SUPPORT
+// #define USB_OTG_HS_SOF_OUTPUT_ENABLED
+
+#ifdef USE_ULPI_PHY
+#define USB_OTG_ULPI_PHY_ENABLED
+#endif
+#ifdef USE_EMBEDDED_PHY
+#define USB_OTG_EMBEDDED_PHY_ENABLED
+#endif
+#define USB_OTG_HS_INTERNAL_DMA_ENABLED
+#define USB_OTG_EXTERNAL_VBUS_ENABLED
+// #define USB_OTG_INTERNAL_VBUS_ENABLED
+#endif
+
+/****************** USB OTG FS CONFIGURATION **********************************/
+#ifdef USB_OTG_FS_CORE
+#define RX_FIFO_FS_SIZE   128
+#define TXH_NP_FS_FIFOSIZ 96
+#define TXH_P_FS_FIFOSIZ  96
+
+// #define USB_OTG_FS_LOW_PWR_MGMT_SUPPORT
+// #define USB_OTG_FS_SOF_OUTPUT_ENABLED
+#endif
+
+/****************** USB OTG MODE CONFIGURATION ********************************/
+#define USE_HOST_MODE
+//#define USE_DEVICE_MODE
+//#define USE_OTG_MODE
+
+#ifndef USB_OTG_FS_CORE
+#ifndef USB_OTG_HS_CORE
+#error "USB_OTG_HS_CORE or USB_OTG_FS_CORE should be defined"
+#endif
+#endif
+
+#ifndef USE_DEVICE_MODE
+#ifndef USE_HOST_MODE
+#error "USE_DEVICE_MODE or USE_HOST_MODE should be defined"
+#endif
+#endif
+
+#ifndef USE_USB_OTG_HS
+#ifndef USE_USB_OTG_FS
+#error "USE_USB_OTG_HS or USE_USB_OTG_FS should be defined"
+#endif
+#else // USE_USB_OTG_HS
+#ifndef USE_ULPI_PHY
+#ifndef USE_EMBEDDED_PHY
+#error "USE_ULPI_PHY or USE_EMBEDDED_PHY should be defined"
+#endif
+#endif
+#endif
+
+/****************** C Compilers dependant keywords ****************************/
+/* In HS mode and when the DMA is used, all variables and data structures dealing
+   with the DMA during the transaction process should be 4-bytes aligned */
+#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
+#if defined(__GNUC__) /* GNU Compiler */
+#define __ALIGN_END __attribute__((aligned(4)))
+#define __ALIGN_BEGIN
+#else
+#define __ALIGN_END
+#if defined(__CC_ARM) /* ARM Compiler */
+#define __ALIGN_BEGIN __align(4)
+#elif defined(__ICCARM__) /* IAR Compiler */
+#define __ALIGN_BEGIN
+#elif defined(__TASKING__) /* TASKING Compiler */
+#define __ALIGN_BEGIN __align(4)
+#endif /* __CC_ARM */
+#endif /* __GNUC__ */
+#else
+#define __ALIGN_BEGIN
+#define __ALIGN_END
+#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
+
+/* __packed keyword used to decrease the data type alignment to 1-byte */
+#if defined(__CC_ARM) /* ARM Compiler */
+#define __packed __packed
+#elif defined(__ICCARM__) /* IAR Compiler */
+#define __packed __packed
+#elif defined(__GNUC__) /* GNU Compiler */
+#define __packed __attribute__((__packed__))
+#elif defined(__TASKING__) /* TASKING Compiler */
+#define __packed __unaligned
+#endif /* __CC_ARM */
+
+/**
+ * @}
+ */
+
+/** @defgroup USB_CONF_Exported_Types
+ * @{
+ */
+/**
+ * @}
+ */
+
+/** @defgroup USB_CONF_Exported_Macros
+ * @{
+ */
+/**
+ * @}
+ */
+
+/** @defgroup USB_CONF_Exported_Variables
+ * @{
+ */
+/**
+ * @}
+ */
+
+/** @defgroup USB_CONF_Exported_FunctionsPrototype
+ * @{
+ */
+/**
+ * @}
+ */
+
+#endif //__USB_CONF__H__
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 38 - 0
User/Bsp/usbh/usbh_bsp_msc.h

@@ -0,0 +1,38 @@
+/*
+*********************************************************************************************************
+*
+*	模块名称 : USB HOST 驱动模块,大容量存储设备
+*	文件名称 : usbh_bsp_msc.h
+*	版    本 : V1.0
+*	说    明 : 应用程序包含本头文件,即可使用读写U盘的功能函数
+*
+*	修改记录 :
+*		版本号  日期        作者     说明
+*		V1.0    2013-02-01 armfly  正式发布
+*
+*	Copyright (C), 2013-2014, 安富莱电子 www.armfly.com
+*
+*********************************************************************************************************
+*/
+
+#ifndef _USBH_BSP_USR_H
+#define _USBH_BSP_USR_H
+
+#include "usb_bsp.h"
+#include "usb_conf.h"
+#include "usb_hcd_int.h"
+#include "usbh_core.h"
+#include "usbh_msc_bot.h"
+#include "usbh_msc_core.h"
+#include "usbh_msc_scsi.h"
+#include "usbh_usr.h"
+
+// void usbd_OpenMassStorage(void);
+// void usbd_CloseMassStorage(void);
+
+extern USB_OTG_CORE_HANDLE USB_OTG_Core;
+extern USBH_HOST           USB_Host;
+
+#endif
+
+/***************************** 安富莱电子 www.armfly.com (END OF FILE) *********************************/

+ 96 - 0
User/Bsp/usbh/usbh_conf.h

@@ -0,0 +1,96 @@
+/**
+ ******************************************************************************
+ * @file    USBH_conf.h
+ * @author  MCD Application Team
+ * @version V2.1.0
+ * @date    19-March-2012
+ * @brief   General low level driver configuration
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
+ *
+ * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *        http://www.st.com/software_license_agreement_liberty_v2
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************
+ */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USBH_CONF__H__
+#define __USBH_CONF__H__
+
+/* Includes ------------------------------------------------------------------*/
+
+/** @addtogroup USBH_OTG_DRIVER
+ * @{
+ */
+
+/** @defgroup USBH_CONF
+ * @brief usb otg low level driver configuration file
+ * @{
+ */
+
+/** @defgroup USBH_CONF_Exported_Defines
+ * @{
+ */
+
+#define USBH_MAX_NUM_ENDPOINTS  2
+#define USBH_MAX_NUM_INTERFACES 2
+#ifdef USE_USB_OTG_FS
+#define USBH_MSC_MPS_SIZE 0x40
+#else
+#define USBH_MSC_MPS_SIZE 0x200
+#endif
+
+/**
+ * @}
+ */
+
+/** @defgroup USBH_CONF_Exported_Types
+ * @{
+ */
+/**
+ * @}
+ */
+
+/** @defgroup USBH_CONF_Exported_Macros
+ * @{
+ */
+/**
+ * @}
+ */
+
+/** @defgroup USBH_CONF_Exported_Variables
+ * @{
+ */
+/**
+ * @}
+ */
+
+/** @defgroup USBH_CONF_Exported_FunctionsPrototype
+ * @{
+ */
+/**
+ * @}
+ */
+
+#endif //__USBH_CONF__H__
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 72 - 0
User/Bsp/usbh/usbh_it.c

@@ -0,0 +1,72 @@
+/*
+*********************************************************************************************************
+*
+*	模块名称 : USB中断服务程序
+*	文件名称 : usbh_it.c
+*	版    本 : V1.0
+*	说    明 : 本文件存放USB中断服务程序。只需将该文件加入工程即可,无需再到 stm32f4xx_it.c 中添加这些ISR程序
+*
+*	修改记录 :
+*		版本号  日期        作者     说明
+*		V1.0    2013-02-01 armfly  正式发布
+*
+*	Copyright (C), 2013-2014, 安富莱电子 www.armfly.com
+*
+*********************************************************************************************************
+*/
+
+#include "usb_bsp.h"
+#include "usb_hcd_int.h"
+#include "usbh_core.h"
+
+extern USB_OTG_CORE_HANDLE USB_OTG_Core;
+extern USBH_HOST           USB_Host;
+
+/* Private function prototypes -----------------------------------------------*/
+extern void USB_OTG_BSP_TimerIRQ(void);
+
+/**	过流检测,安富莱STM32-F4硬件不支持
+ * @brief  EXTI1_IRQHandler
+ *         This function handles External line 1 interrupt request.
+ * @param  None
+ * @retval None
+ */
+#if 0
+void EXTI1_IRQHandler(void)
+{
+  if(EXTI_GetITStatus(EXTI_Line1) != RESET)
+  {
+      USB_Host.usr_cb->OverCurrentDetected();
+      EXTI_ClearITPendingBit(EXTI_Line1);
+  }
+}
+#endif
+
+/**
+ * @brief  TIM2_IRQHandler
+ *         This function handles Timer2 Handler.
+ * @param  None
+ * @retval None
+ */
+// void TIM2_IRQHandler(void)
+// {
+//     USB_OTG_BSP_TimerIRQ();
+// }
+
+/**
+ * @brief  OTG_FS_IRQHandler
+ *          This function handles USB-On-The-Go FS global interrupt request.
+ *          requests.
+ * @param  None
+ * @retval None
+ */
+#ifdef USE_USB_OTG_FS
+void OTG_FS_IRQHandler(void)
+#else
+void OTG_HS_IRQHandler(void)
+#endif
+{
+    USBH_OTG_ISR_Handler(&USB_OTG_Core);
+}
+
+/***************************** 安富莱电子 www.armfly.com (END OF FILE) *********************************/

+ 383 - 0
User/Bsp/usbh/usbh_usr.c

@@ -0,0 +1,383 @@
+/**
+ ******************************************************************************
+ * @file    usbh_usr.c
+ * @author  MCD Application Team
+ * @version V2.1.0
+ * @date    19-March-2012
+ * @brief   This file includes the usb host library user callbacks
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
+ *
+ * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *        http://www.st.com/software_license_agreement_liberty_v2
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************
+ */
+
+/* Includes ------------------------------------------------------------------*/
+#include "usbh_usr.h"
+#include "ff.h" /* FATFS */
+#include "usbh_msc_bot.h"
+#include "usbh_msc_core.h"
+#include "usbh_msc_scsi.h"
+#include <string.h>
+
+#define usb_printf printf
+//#define usb_printf	printf(...)
+
+USB_OTG_CORE_HANDLE USB_OTG_Core;
+USBH_HOST           USB_Host;
+
+/*  Points to the DEVICE_PROP structure of current device */
+/*  The purpose of this register is to speed up the execution */
+
+USBH_Usr_cb_TypeDef USR_cb =
+    {
+        USBH_USR_Init,
+        USBH_USR_DeInit,
+        USBH_USR_DeviceAttached,
+        USBH_USR_ResetDevice,
+        USBH_USR_DeviceDisconnected,
+        USBH_USR_OverCurrentDetected,
+        USBH_USR_DeviceSpeedDetected,
+        USBH_USR_Device_DescAvailable,
+        USBH_USR_DeviceAddressAssigned,
+        USBH_USR_Configuration_DescAvailable,
+        USBH_USR_Manufacturer_String,
+        USBH_USR_Product_String,
+        USBH_USR_SerialNum_String,
+        USBH_USR_EnumerationDone,
+        USBH_USR_UserInput,
+        USBH_USR_MSC_Application,
+        USBH_USR_DeviceNotSupported,
+        USBH_USR_UnrecoveredError};
+
+/**
+ * @}
+ */
+
+/** @defgroup USBH_USR_Private_Constants
+ * @{
+ */
+/*--------------- LCD Messages ---------------*/
+const uint8_t MSG_HOST_INIT[]        = "> Host Library Initialized\r\n";
+const uint8_t MSG_DEV_ATTACHED[]     = "> Device Attached \r\n";
+const uint8_t MSG_DEV_DISCONNECTED[] = "> Device Disconnected\r\n";
+const uint8_t MSG_DEV_ENUMERATED[]   = "> Enumeration completed \r\n";
+const uint8_t MSG_DEV_HIGHSPEED[]    = "> High speed device detected\r\n";
+const uint8_t MSG_DEV_FULLSPEED[]    = "> Full speed device detected\r\n";
+const uint8_t MSG_DEV_LOWSPEED[]     = "> Low speed device detected\r\n";
+const uint8_t MSG_DEV_ERROR[]        = "> Device fault \r\n";
+
+const uint8_t MSG_MSC_CLASS[]   = "> Mass storage device connected\r\n";
+const uint8_t MSG_HID_CLASS[]   = "> HID device connected\r\n";
+const uint8_t MSG_DISK_SIZE[]   = "> Size of the disk in MBytes: \r\n";
+const uint8_t MSG_LUN[]         = "> LUN Available in the device:\r\n";
+const uint8_t MSG_ROOT_CONT[]   = "> Exploring disk flash ...\r\n";
+const uint8_t MSG_WR_PROTECT[]  = "> The disk is write protected\r\n";
+const uint8_t MSG_UNREC_ERROR[] = "> UNRECOVERED ERROR STATE\r\n";
+
+/**
+ * @}
+ */
+
+/** @defgroup USBH_USR_Private_FunctionPrototypes
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBH_USR_Private_Functions
+ * @{
+ */
+
+/**
+ * @brief  USBH_USR_Init
+ *         Displays the message on LCD for host lib initialization
+ * @param  None
+ * @retval None
+ */
+void USBH_USR_Init(void)
+{
+    static uint8_t startup = 0;
+
+    if (startup == 0)
+    {
+        startup = 1;
+
+#ifdef USE_USB_OTG_HS
+        usb_printf("> USB OTG HS MSC Host\r\n");
+#else
+        usb_printf("> USB OTG FS MSC Host\r\n");
+#endif
+        usb_printf("> USB Host library started.\r\n");
+        usb_printf("     USB Host Library v2.1.0\r\n");
+    }
+}
+
+/**
+ * @brief  USBH_USR_DeviceAttached
+ *         Displays the message on LCD on device attached
+ * @param  None
+ * @retval None
+ */
+void USBH_USR_DeviceAttached(void)
+{
+    usb_printf((char *)MSG_DEV_ATTACHED);
+}
+
+/**
+ * @brief  USBH_USR_UnrecoveredError
+ * @param  None
+ * @retval None
+ */
+void USBH_USR_UnrecoveredError(void)
+{
+    usb_printf((char *)MSG_UNREC_ERROR);
+}
+
+/**
+ * @brief  USBH_DisconnectEvent
+ *         Device disconnect event
+ * @param  None
+ * @retval Staus
+ */
+void USBH_USR_DeviceDisconnected(void)
+{
+    usb_printf((char *)MSG_DEV_DISCONNECTED);
+}
+/**
+ * @brief  USBH_USR_ResetUSBDevice
+ * @param  None
+ * @retval None
+ */
+void USBH_USR_ResetDevice(void)
+{
+    /* callback for USB-Reset */
+    usb_printf("> USBH_USR_ResetDevice \r\n");
+}
+
+/**
+ * @brief  USBH_USR_DeviceSpeedDetected
+ *         Displays the message on LCD for device speed
+ * @param  Device speed
+ * @retval None
+ */
+void USBH_USR_DeviceSpeedDetected(uint8_t DeviceSpeed)
+{
+    if (DeviceSpeed == HPRT0_PRTSPD_HIGH_SPEED)
+    {
+        usb_printf((char *)MSG_DEV_HIGHSPEED);
+    }
+    else if (DeviceSpeed == HPRT0_PRTSPD_FULL_SPEED)
+    {
+        usb_printf((char *)MSG_DEV_FULLSPEED);
+    }
+    else if (DeviceSpeed == HPRT0_PRTSPD_LOW_SPEED)
+    {
+        usb_printf((char *)MSG_DEV_LOWSPEED);
+    }
+    else
+    {
+        usb_printf((char *)MSG_DEV_ERROR);
+    }
+}
+
+/**
+ * @brief  USBH_USR_Device_DescAvailable
+ *         Displays the message on LCD for device descriptor
+ * @param  device descriptor
+ * @retval None
+ */
+void USBH_USR_Device_DescAvailable(void *DeviceDesc)
+{
+    USBH_DevDesc_TypeDef *hs;
+    hs = DeviceDesc;
+
+    usb_printf("> VID : %04Xh\r\n", (uint32_t)(*hs).idVendor);
+    usb_printf("> PID : %04Xh\r\n", (uint32_t)(*hs).idProduct);
+}
+
+/**
+ * @brief  USBH_USR_DeviceAddressAssigned
+ *         USB device is successfully assigned the Address
+ * @param  None
+ * @retval None
+ */
+void USBH_USR_DeviceAddressAssigned(void)
+{
+}
+
+/**
+ * @brief  USBH_USR_Conf_Desc
+ *         Displays the message on LCD for configuration descriptor
+ * @param  Configuration descriptor
+ * @retval None
+ */
+void USBH_USR_Configuration_DescAvailable(USBH_CfgDesc_TypeDef       *cfgDesc,
+                                          USBH_InterfaceDesc_TypeDef *itfDesc,
+                                          USBH_EpDesc_TypeDef        *epDesc)
+{
+    USBH_InterfaceDesc_TypeDef *id;
+
+    id = itfDesc;
+
+    if ((*id).bInterfaceClass == 0x08)
+    {
+        usb_printf((char *)MSG_MSC_CLASS);
+    }
+    else if ((*id).bInterfaceClass == 0x03)
+    {
+        usb_printf((char *)MSG_HID_CLASS);
+    }
+}
+
+/**
+ * @brief  USBH_USR_Manufacturer_String
+ *         Displays the message on LCD for Manufacturer String
+ * @param  Manufacturer String
+ * @retval None
+ */
+void USBH_USR_Manufacturer_String(void *ManufacturerString)
+{
+    usb_printf("> Manufacturer : %sr\r\n", (char *)ManufacturerString);
+}
+
+/**
+ * @brief  USBH_USR_Product_String
+ *         Displays the message on LCD for Product String
+ * @param  Product String
+ * @retval None
+ */
+void USBH_USR_Product_String(void *ProductString)
+{
+    usb_printf("> Product : %s\r\n", (char *)ProductString);
+}
+
+/**
+ * @brief  USBH_USR_SerialNum_String
+ *         Displays the message on LCD for SerialNum_String
+ * @param  SerialNum_String
+ * @retval None
+ */
+void USBH_USR_SerialNum_String(void *SerialNumString)
+{
+    usb_printf("> Serial Number : %s\r\n", (char *)SerialNumString);
+}
+
+/**
+ * @brief  EnumerationDone
+ *         User response request is displayed to ask application jump to class
+ * @param  None
+ * @retval None
+ */
+void USBH_USR_EnumerationDone(void)
+{
+    /* Enumeration complete */
+    usb_printf((void *)MSG_DEV_ENUMERATED);
+}
+
+/**
+ * @brief  USBH_USR_DeviceNotSupported
+ *         Device is not supported
+ * @param  None
+ * @retval None
+ */
+void USBH_USR_DeviceNotSupported(void)
+{
+    usb_printf("> Device not supported.\r\n");
+}
+
+/**
+ * @brief  USBH_USR_UserInput
+ *         User Action for application state entry
+ * @param  None
+ * @retval USBH_USR_Status : User response for key button
+ */
+USBH_USR_Status USBH_USR_UserInput(void)
+{
+#if 1
+    /* HOST_ENUMERATION 和 HOST_CLASS_REQUEST
+        在枚举成功和类请求之间等待用户输入。
+        此处直接返回OK,无需等待。
+    */
+    return USBH_USR_RESP_OK;
+#else
+    USBH_USR_Status usbh_usr_status;
+
+    usbh_usr_status = USBH_USR_NO_RESP;
+
+#if 0
+	/*Key B3 is in polling mode to detect user action */
+	if(STM_EVAL_PBGetState(Button_KEY) == RESET)
+	{
+		usbh_usr_status = USBH_USR_RESP_OK;
+	}
+#endif
+    return usbh_usr_status;
+#endif
+}
+
+/**
+ * @brief  USBH_USR_OverCurrentDetected
+ *         Over Current Detected on VBUS
+ * @param  None
+ * @retval Staus
+ */
+void USBH_USR_OverCurrentDetected(void)
+{
+    usb_printf("> Overcurrent detected.\r\n");
+}
+
+/**
+ * @brief  USBH_USR_MSC_Application
+ *         Demo application for mass storage
+ * @param  None
+ * @retval Staus
+ */
+int USBH_USR_MSC_Application(void)
+{
+    return 0;
+}
+
+/**
+ * @brief  USBH_USR_DeInit
+ *         Deint User state and associated variables
+ * @param  None
+ * @retval None
+ */
+void USBH_USR_DeInit(void)
+{
+    // USBH_USR_ApplicationState = USH_USR_FS_INIT;
+}
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 133 - 0
User/Bsp/usbh/usbh_usr.h

@@ -0,0 +1,133 @@
+/**
+ ******************************************************************************
+ * @file    usbh_usr.h
+ * @author  MCD Application Team
+ * @version V2.1.0
+ * @date    19-March-2012
+ * @brief   Header file for usbh_usr.c
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
+ *
+ * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *        http://www.st.com/software_license_agreement_liberty_v2
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************
+ */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USH_USR_H__
+#define __USH_USR_H__
+
+/* Includes ------------------------------------------------------------------*/
+#include "ff.h"
+#include "usb_conf.h"
+#include "usbh_core.h"
+#include "usbh_msc_core.h"
+#include <stdio.h>
+
+/** @addtogroup USBH_USER
+ * @{
+ */
+
+/** @addtogroup USBH_MSC_DEMO_USER_CALLBACKS
+ * @{
+ */
+
+/** @defgroup USBH_USR
+ * @brief This file is the Header file for usbh_usr.c
+ * @{
+ */
+
+/** @defgroup USBH_USR_Exported_Types
+ * @{
+ */
+
+extern USBH_Usr_cb_TypeDef USR_cb;
+
+/**
+ * @}
+ */
+
+/** @defgroup USBH_USR_Exported_Defines
+ * @{
+ */
+/* State Machine for the USBH_USR_ApplicationState */
+#define USH_USR_FS_INIT      0
+#define USH_USR_FS_READLIST  1
+#define USH_USR_FS_WRITEFILE 2
+#define USH_USR_FS_DRAW      3
+/**
+ * @}
+ */
+
+/** @defgroup USBH_USR_Exported_Macros
+ * @{
+ */
+/**
+ * @}
+ */
+
+/** @defgroup USBH_USR_Exported_Variables
+ * @{
+ */
+extern uint8_t USBH_USR_ApplicationState;
+/**
+ * @}
+ */
+
+/** @defgroup USBH_USR_Exported_FunctionsPrototype
+ * @{
+ */
+void            USBH_USR_ApplicationSelected(void);
+void            USBH_USR_Init(void);
+void            USBH_USR_DeInit(void);
+void            USBH_USR_DeviceAttached(void);
+void            USBH_USR_ResetDevice(void);
+void            USBH_USR_DeviceDisconnected(void);
+void            USBH_USR_OverCurrentDetected(void);
+void            USBH_USR_DeviceSpeedDetected(uint8_t DeviceSpeed);
+void            USBH_USR_Device_DescAvailable(void *);
+void            USBH_USR_DeviceAddressAssigned(void);
+void            USBH_USR_Configuration_DescAvailable(USBH_CfgDesc_TypeDef       *cfgDesc,
+                                                     USBH_InterfaceDesc_TypeDef *itfDesc,
+                                                     USBH_EpDesc_TypeDef        *epDesc);
+void            USBH_USR_Manufacturer_String(void *);
+void            USBH_USR_Product_String(void *);
+void            USBH_USR_SerialNum_String(void *);
+void            USBH_USR_EnumerationDone(void);
+USBH_USR_Status USBH_USR_UserInput(void);
+void            USBH_USR_DeInit(void);
+void            USBH_USR_DeviceNotSupported(void);
+void            USBH_USR_UnrecoveredError(void);
+int             USBH_USR_MSC_Application(void);
+
+/**
+ * @}
+ */
+
+#endif /*__USH_USR_H__*/
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 11 - 10
User/conf/fatfs/bsp_fatfs.c

@@ -1,6 +1,7 @@
 #include "bsp_fatfs.h"
 #include "ext_sram.h"
 #include "interface.h"
+#include "project_var.h"
 #include "stm32f4xx.h"
 #include <stdio.h>
 #include <stdlib.h>
@@ -47,25 +48,25 @@ void fatfs_init(void)
         res = f_mkfs("0:", 0, 0); // 格式化
         if (res == FR_OK)
         {
-            printf("TF format ok\r\n");
+            DEBUG("TF format ok\r\n");
             res = f_mount(NULL, "0:", 1);      /* 格式化后,先取消挂载 */
             res = f_mount(&FatFs[0], "0:", 1); /* 重新挂载	*/
         }
         else
         {
             LED1_RUN_OFF;
-            printf("TF format fail\r\n");
+            DEBUG("TF format fail\r\n");
         }
     }
     else if (res != FR_OK)
     {
         LED1_RUN_OFF;
-        printf("!!!TF mount file system fail (%d)\r\n", res);
+        DEBUG("!!!TF mount file system fail (%d)\r\n", res);
     }
     else
     {
         LED1_RUN_ON;
-        printf("File system mount ok !\r\n");
+        DEBUG("File system mount ok !\r\n");
     }
 
     res = f_mount(&FatFs[1], "1:", 1); // 文件系统挂载时会对NAND初始化
@@ -75,14 +76,14 @@ void fatfs_init(void)
     //     res = f_mkfs("1:", 0, 0); // 格式化
     //     if (res == FR_OK)
     //     {
-    //         printf("TF format ok\r\n");
+    //         DEBUG("TF format ok\r\n");
     //         res = f_mount(NULL, "1:", 1); /* 格式化后,先取消挂载 */
     //         res = f_mount(&fs, "1:", 1);  /* 重新挂载	*/
     //     }
     //     else
     //     {
     //         // LED_FAULT_ON;
-    //         printf("TF format fail\r\n");
+    //         DEBUG("TF format fail\r\n");
     //     }
     // }
     // else
@@ -92,24 +93,24 @@ void fatfs_init(void)
         res = f_mkfs("1:", 0, 0); // 格式化
         if (res == FR_OK)
         {
-            printf("Nand flash format ok\r\n");
+            DEBUG("Nand flash format ok\r\n");
             res = f_mount(NULL, "1:", 1);      /* 格式化后,先取消挂载 */
             res = f_mount(&FatFs[1], "1:", 1); /* 重新挂载	*/
         }
         else
         {
             LED1_RUN_OFF;
-            printf("Nand flash format fail\r\n");
+            DEBUG("Nand flash format fail\r\n");
         }
     }
     if (res != FR_OK)
     {
         LED0_RUN_OFF;
-        printf("!!!Nand flash mount file system fail (%d)\r\n", res);
+        DEBUG("!!!Nand flash mount file system fail (%d)\r\n", res);
     }
     else
     {
         LED0_RUN_ON;
-        printf("Nand flash file system mount ok !\r\n");
+        DEBUG("Nand flash file system mount ok !\r\n");
     }
 }

+ 92 - 23
User/conf/fatfs/diskio.c

@@ -13,6 +13,9 @@
 #include "sdcard.h" /* Example: MMC/SDC contorl */
 // #include "usbdisk.h"  /* Example: USB drive control */
 #include "string.h"
+#ifdef FS_USB
+#include "usbh_bsp_msc.h" /* 底层驱动 */
+#endif
 
 /* Definitions of physical drive number for each drive */
 /* 为每个设备定义一个物理编号 */
@@ -20,7 +23,7 @@
 #define FS_NAND 1 // 预留外部SPI Flash使用
 // #define ATA       0 /* Example: Map ATA drive to drive number 0 */
 // #define MMC       1 /* Example: Map MMC/SD card to drive number 1 */
-#define FS_USB 2 /* Example: Map USB drive to drive number 2 */
+// #define FS_USB 2 /* Example: Map USB drive to drive number 2 */
 
 #define SD_BLOCKSIZE 512
 
@@ -44,10 +47,11 @@ DSTATUS disk_status(
     case FS_NAND:
         status = 0;
         break;
-
+#ifdef FS_USB
     case FS_USB:
-        status = STA_NOINIT;
+        status = 0;
         break;
+#endif
     }
     return status;
 }
@@ -88,13 +92,14 @@ DSTATUS disk_initialize(
             status = RES_ERROR;
         }
         break;
-
+#ifdef FS_USB
     case FS_USB:
-        // result = USB_disk_initialize();
-
-        // translate the reslut code here
-
-        status = STA_NOINIT;
+        if (HCD_IsDeviceConnected(&USB_OTG_Core))
+        {
+            status &= ~STA_NOINIT;
+        }
+        break;
+#endif
     }
     return status;
 }
@@ -160,15 +165,36 @@ DRESULT disk_read(
             status = RES_ERROR;
         }
         break;
-
+#ifdef FS_USB
     case FS_USB:
-        // translate the arguments here
+    {
+        BYTE res = USBH_MSC_OK;
 
-        // result = USB_disk_read(buff, sector, count);
+        if (HCD_IsDeviceConnected(&USB_OTG_Core))
+        {
+            do
+            {
+                res = USBH_MSC_Read10(&USB_OTG_Core, buff, sector, 512 * count);
+                USBH_MSC_HandleBOTXfer(&USB_OTG_Core, &USB_Host);
 
-        // translate the reslut code here
+                if (!HCD_IsDeviceConnected(&USB_OTG_Core))
+                {
+                    break;
+                }
+            } while (res == USBH_MSC_BUSY);
+        }
 
-        status = RES_PARERR;
+        if (res == USBH_MSC_OK)
+        {
+            status = RES_OK;
+        }
+        else
+        {
+            status = RES_ERROR;
+        }
+    }
+#endif
+    break;
     }
 
     return status;
@@ -242,16 +268,36 @@ DRESULT disk_write(
             status = RES_ERROR;
         }
         break;
-
+#ifdef FS_USB
     case FS_USB:
-        // translate the arguments here
+    {
+        BYTE res = USBH_MSC_OK;
 
-        // result = USB_disk_write(buff, sector, count);
+        if (HCD_IsDeviceConnected(&USB_OTG_Core))
+        {
+            do
+            {
+                res = USBH_MSC_Write10(&USB_OTG_Core, (BYTE *)buff, sector, 512 * count);
+                USBH_MSC_HandleBOTXfer(&USB_OTG_Core, &USB_Host);
 
-        // translate the reslut code here
+                if (!HCD_IsDeviceConnected(&USB_OTG_Core))
+                {
+                    break;
+                }
+            } while (res == USBH_MSC_BUSY);
+        }
 
-        status = RES_PARERR;
-        break;
+        if (res == USBH_MSC_OK)
+        {
+            status = RES_OK;
+        }
+        else
+        {
+            status = RES_ERROR;
+        }
+    }
+#endif
+    break;
     }
 
     return status;
@@ -297,12 +343,35 @@ DRESULT disk_ioctl(
     case FS_NAND:
         status = RES_OK;
         break;
-
+#ifdef FS_USB
     case FS_USB:
+        // if (Stat & STA_NOINIT) return RES_NOTRDY;
+        switch (cmd)
+        {
+        case CTRL_SYNC: /* Make sure that no pending write process */
+            status = RES_OK;
+            break;
 
-        // Process of the command the USB drive
+        case GET_SECTOR_COUNT: /* Get number of sectors on the disk (DWORD) */
+            *(DWORD *)buff = (DWORD)USBH_MSC_Param.MSCapacity;
+            status         = RES_OK;
+            break;
 
-        status = RES_PARERR;
+        case GET_SECTOR_SIZE: /* Get R/W sector size (WORD) */
+            *(WORD *)buff = 512;
+            status        = RES_OK;
+            break;
+
+        case GET_BLOCK_SIZE: /* Get erase block size in unit of sector (DWORD) */
+            *(DWORD *)buff = 512;
+            status         = RES_OK;
+            break;
+
+        default:
+            status = RES_PARERR;
+            break;
+        }
+#endif
         break;
     }
 

+ 8 - 0
User/project_var.h

@@ -5,6 +5,14 @@
 typedef unsigned long long INT64U;
 typedef signed long long   INT64S;
 
+#define __DEBUG // 调试用
+
+#ifdef __DEBUG
+#define DEBUG(format, ...) printf(format, ##__VA_ARGS__)
+#else
+#define DEBUG(...)
+#endif
+
 // project version
 #define SW_VERSION_HD0 'C'
 #define SW_VERSION_HD1 'C'

+ 31 - 31
platformio.ini

@@ -13,6 +13,26 @@ extra_scripts = Project/GCC/scripts.py
 monitor_speed = 9600
 
 build_flags = 
+  -ILibraries/CMSIS/Include
+  -ILibraries/CMSIS/Device/ST/STM32F4xx/Include
+  -ILibraries/STM32F4xx_StdPeriph_Driver/inc
+  -ILibraries/STM32F4x7_ETH_Driver/inc
+  ; -ILibraries/STM32_USB_HOST_Library/Core/inc
+  ; -ILibraries/STM32_USB_HOST_Library/Class/MSC/inc
+  ; -ILibraries/STM32_USB_OTG_Driver/inc
+  -IMiddleWare/uCOS_II/uC-CPU
+  -IMiddleWare/uCOS_II/uC-CPU/ARM-Cortex-M4/GNU
+  -IMiddleWare/uCOS_II/uC-LIB
+  -IMiddleWare/uCOS_II/uCOS-BSP
+  -IMiddleWare/uCOS_II/uCOS-II/Source
+  -IMiddleWare/uCOS_II/uCOS-II/Ports/ARM-Cortex-M4/Generic/GNU
+  -IMiddleWare/lwip-2.0.2/src/include
+  -IMiddleWare/lwip-2.0.2/src/include/lwip
+  -IMiddleWare/lwip-2.0.2/src/include/netif
+  -IMiddleWare/lwip-2.0.2/src/include/posix
+  -IMiddleWare/lwip-2.0.2/src/include/posix/sys
+  -IMiddleWare/FATFS/src
+  -IMiddleWare/cJSON
   -IUser
   ; -IUser/bsp
   -IUser/bsp/ad7606
@@ -32,7 +52,7 @@ build_flags =
   -IUser/bsp/spi
   -IUser/bsp/timer
   -IUser/bsp/uart 
-  -IUser/bsp/usbh
+  ; -IUser/bsp/usbh
   -IUser/app
   -IUser/app/esp8266
   -IUser/app/iec104
@@ -47,36 +67,11 @@ build_flags =
   -IUser/conf/lwip/port
   -IUser/conf/lwip/port/arch
   -IUser/conf/ucos
-  -ILibraries/CMSIS/Include
-  -ILibraries/CMSIS/Device/ST/STM32F4xx/Include
-  -ILibraries/STM32F4xx_StdPeriph_Driver/inc
-  -ILibraries/STM32F4x7_ETH_Driver/inc
-  -ILibraries/STM32_USB_Device_Library/Core/inc
-  -ILibraries/STM32_USB_Device_Library/Class/audio/inc
-  -ILibraries/STM32_USB_Device_Library/Class/cdc/inc
-  -ILibraries/STM32_USB_Device_Library/Class/dfu/inc
-  -ILibraries/STM32_USB_Device_Library/Class/hid/inc
-  -ILibraries/STM32_USB_Device_Library/Class/msc/inc
-  -ILibraries/STM32_USB_HOST_Library/Core/inc
-  -ILibraries/STM32_USB_HOST_Library/Class/MSC/inc
-  -ILibraries/STM32_USB_HOST_Library/Class/HID/inc
-  -ILibraries/STM32_USB_OTG_Driver/inc
-  -IMiddleWare/uCOS_II/uC-CPU
-  -IMiddleWare/uCOS_II/uC-CPU/ARM-Cortex-M4/GNU
-  -IMiddleWare/uCOS_II/uC-LIB
-  -IMiddleWare/uCOS_II/uCOS-BSP
-  -IMiddleWare/uCOS_II/uCOS-II/Source
-  -IMiddleWare/uCOS_II/uCOS-II/Ports/ARM-Cortex-M4/Generic/GNU
-  -IMiddleWare/lwip-2.0.2/src/include
-  -IMiddleWare/lwip-2.0.2/src/include/lwip
-  -IMiddleWare/lwip-2.0.2/src/include/netif
-  -IMiddleWare/lwip-2.0.2/src/include/posix
-  -IMiddleWare/lwip-2.0.2/src/include/posix/sys
-  -IMiddleWare/FATFS/src
-  -IMiddleWare/cJSON
   ; 定义全局宏
-  -D STM32F40_41xxx
-  -D USE_STDPERIPH_DRIVER
+  -DSTM32F40_41xxx
+  -DUSE_STDPERIPH_DRIVER
+  -DUSE_USB_OTG_HS
+  -DUSE_EMBEDDED_PHY
   # 去除编译警告
   -Wno-unused-variable
   -Wno-pointer-sign
@@ -89,6 +84,7 @@ build_unflags =
   -Os
 
 build_src_filter = +<User/> 
+            -<User/bsp/usbh>
             +<Project/GCC/> 
             +<Libraries/STM32F4xx_StdPeriph_Driver/src/>
             -<Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_qspi.c>
@@ -97,10 +93,14 @@ build_src_filter = +<User/>
             -<Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_cec.c>
             -<Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spdifrx.c>
             +<Libraries/STM32F4x7_ETH_Driver/src/>
-            ; +<ILibraries/STM32_USB_Device_Library/>
             ; +<ILibraries/STM32_USB_HOST_Library/>
+            ; -<ILibraries/STM32_USB_HOST_Library/Class/HID>
             ; -<ILibraries/STM32_USB_HOST_Library/Class/MSC/src/usbh_msc_fatfs.c>
             ; +<Libraries/STM32_USB_OTG_Driver/>
+            ; -<Libraries/STM32_USB_OTG_Driver/src/usb_bsp_template.c>
+            ; -<Libraries/STM32_USB_OTG_Driver/src/usb_dcd_int.c>
+            ; -<Libraries/STM32_USB_OTG_Driver/src/usb_dcd.c>
+            ; -<Libraries/STM32_USB_OTG_Driver/src/usb_otg.c>
             +<MiddleWare/lwip-2.0.2/src/api>
             +<MiddleWare/lwip-2.0.2/src/core>
             +<MiddleWare/lwip-2.0.2/src/core/ipv4>