Browse Source

USB驱动完成

樊春春 1 year ago
parent
commit
e51d3324b7

+ 14 - 5
CMakeLists.txt

@@ -18,8 +18,8 @@ set(CMAKE_CXX_EXTENSIONS ON)
 set(MCU_PARAMS
         -mthumb
         -mcpu=cortex-m4
-        -mfpu=fpv5-d16
-        -mfloat-abi=hard
+#        -mfpu=fpv5-d16
+#        -mfloat-abi=hard
         )
 
 # C 源文件
@@ -49,6 +49,11 @@ file(GLOB_RECURSE C_SRCS
         ${CMAKE_CURRENT_SOURCE_DIR}/MiddleWare/uCOS_II/uCOS-II/Source/*.c
         ${CMAKE_CURRENT_SOURCE_DIR}/MiddleWare/uCOS_II/uCOS-II/Ports/ARM-Cortex-M4/Generic/GNU/*.c
         ${CMAKE_CURRENT_SOURCE_DIR}/MiddleWare/uCOS_II/uCOS-II/Ports/ARM-Cortex-M4/Generic/GNU/*.s
+        ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Class/MSC/src/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Core/src/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/src/usb_core.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/src/usb_hcd.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/src/usb_hcd_int.c
         )
 file(GLOB_RECURSE R_SRCS
         ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_qspi.c
@@ -56,7 +61,7 @@ file(GLOB_RECURSE R_SRCS
         ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmc.c
         ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_cec.c
         ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spdifrx.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/usbh/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Class/MSC/src/usbh_msc_fatfs.c
         )
 
 list(REMOVE_ITEM C_SRCS ${R_SRCS})
@@ -101,12 +106,12 @@ set(INC_C_DIRS
         ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/spi
         ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/timer
         ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/uart
+        ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/usbh
         ${CMAKE_CURRENT_SOURCE_DIR}/User/app
         ${CMAKE_CURRENT_SOURCE_DIR}/User/app/esp8266
         ${CMAKE_CURRENT_SOURCE_DIR}/User/app/iec104
         ${CMAKE_CURRENT_SOURCE_DIR}/User/app/key
         ${CMAKE_CURRENT_SOURCE_DIR}/User/app/queue
-        ${CMAKE_CURRENT_SOURCE_DIR}/User/app/led
         ${CMAKE_CURRENT_SOURCE_DIR}/User/app/memory
         ${CMAKE_CURRENT_SOURCE_DIR}/User/app/modbus
         ${CMAKE_CURRENT_SOURCE_DIR}/User/app/net
@@ -115,6 +120,9 @@ set(INC_C_DIRS
         ${CMAKE_CURRENT_SOURCE_DIR}/User/conf/lwip/port
         ${CMAKE_CURRENT_SOURCE_DIR}/User/conf/lwip/port/arch
         ${CMAKE_CURRENT_SOURCE_DIR}/User/conf/ucos
+        ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Core/inc
+        ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Class/MSC/inc
+        ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/inc
         )
 
 set(INC_CXX_DIRS)
@@ -185,6 +193,7 @@ target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
         -Wno-pointer-sign
         -Wno-unused-but-set-variable
         -Wno-unused-function
+        -Wno-unused-parameter
         # -Wno-volatile
         # -Wold-style-cast
         # -Wuseless-cast
@@ -201,7 +210,7 @@ target_link_options(${CMAKE_PROJECT_NAME} PRIVATE
         ${MCU_PARAMS}
         ${LINK_OPTS}
         -Wl,-Map=${CMAKE_PROJECT_NAME}.map
-        -u _printf_float
+        # -u _printf_float
         --specs=nosys.specs
         -Wl,--start-group
         -lc

+ 15 - 0
User/Bsp/armfly_bsp.c

@@ -23,6 +23,21 @@ void bsp_init(void)
     key_init();
     uart6_init();
     nor_flash_init();
+
+/* Init Host Library */
+#ifdef USE_USB_OTG_FS
+    USBH_Init(&USB_OTG_Core,
+              USB_OTG_FS_CORE_ID,
+              &USB_Host,
+              &USBH_MSC_cb,
+              &USR_cb);
+#else
+    USBH_Init(&USB_OTG_Core,
+              USB_OTG_HS_CORE_ID,
+              &USB_Host,
+              &USBH_MSC_cb,
+              &USR_cb);
+#endif
     fatfs_init();
     lwip_setup();
     // can初始化

+ 1 - 0
User/Bsp/armfly_bsp.h

@@ -19,5 +19,6 @@
 #include "uart1.h"
 #include "uart3.h"
 #include "uart6.h"
+#include "usbh_bsp_msc.h"
 
 #endif

+ 12 - 3
User/Bsp/interface/interface.c

@@ -28,7 +28,8 @@ Gpio_Clock clock_info[] = {
     {.type = kEthernet_dm9101, .RCC_AXBPeriph = RCC_AHB1Periph, .AF_Clock = RCC_AHB1Periph_ETH_MAC_Rx},
     {.type = kEthernet_dm9101, .RCC_AXBPeriph = RCC_AHB1Periph, .AF_Clock = RCC_AHB1Periph_ETH_MAC_PTP},
     {.type = kFSMC, .RCC_AXBPeriph = RCC_AHB3Periph, .AF_Clock = RCC_AHB3Periph_FSMC},
-    {.type = kUSB, .RCC_AXBPeriph = RCC_AHB1Periph, .AF_Clock = RCC_AHB1Periph_OTG_HS},
+    //    {.type = kUSB, .RCC_AXBPeriph = RCC_AHB1Periph, .AF_Clock = RCC_AHB1Periph_OTG_HS},
+    //    {.type = kSDIO, .RCC_AXBPeriph = RCC_APB2Periph, .AF_Clock = RCC_APB2Periph_SDIO},
     // {.type = kPMU, .AXBPeriph_Clock = RCU_PMU},
     {.type = kDMA1, .RCC_AXBPeriph = RCC_AHB1Periph, .AF_Clock = RCC_AHB1Periph_DMA1},
     // //    {.type = kTIMER1,   .AXBPeriph_Clock = RCU_TIMER1},
@@ -145,8 +146,16 @@ Interface_struct interface_info[] = {
     {.type = kOutput, .Out_Type = kAM2303, .GPIOx = AM2303_PORT, .GPIO_Pin = AM2303_PIN, .GPIO_Mode = GPIO_Mode_OUT, .GPIO_OType = GPIO_OType_OD, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_NOPULL},
 
     // USB U盘
-    {.type = kUSB, .GPIOx = GPIOB, .GPIO_Pin = GPIO_Pin_14, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOB, .GPIO_PinSource = GPIO_PinSource14, .GPIO_AF = GPIO_AF_OTG2_FS}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_NOPULL},
-    {.type = kUSB, .GPIOx = GPIOB, .GPIO_Pin = GPIO_Pin_15, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOB, .GPIO_PinSource = GPIO_PinSource15, .GPIO_AF = GPIO_AF_OTG2_FS}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_NOPULL},
+    //    {.type = kUSB, .GPIOx = GPIOB, .GPIO_Pin = GPIO_Pin_14, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOB, .GPIO_PinSource = GPIO_PinSource14, .GPIO_AF = GPIO_AF_OTG2_FS}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_NOPULL},
+    //    {.type = kUSB, .GPIOx = GPIOB, .GPIO_Pin = GPIO_Pin_15, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOB, .GPIO_PinSource = GPIO_PinSource15, .GPIO_AF = GPIO_AF_OTG2_FS}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_NOPULL},
+
+    // sdio
+    //    {.type = kSDIO, .GPIOx = GPIOC, .GPIO_Pin = GPIO_Pin_8,  .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOC, .GPIO_PinSource = GPIO_PinSource8,  .GPIO_AF = GPIO_AF_SDIO}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    //    {.type = kSDIO, .GPIOx = GPIOC, .GPIO_Pin = GPIO_Pin_9,  .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOC, .GPIO_PinSource = GPIO_PinSource9,  .GPIO_AF = GPIO_AF_SDIO}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    //    {.type = kSDIO, .GPIOx = GPIOC, .GPIO_Pin = GPIO_Pin_10, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOC, .GPIO_PinSource = GPIO_PinSource10, .GPIO_AF = GPIO_AF_SDIO}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    //    {.type = kSDIO, .GPIOx = GPIOC, .GPIO_Pin = GPIO_Pin_11, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOC, .GPIO_PinSource = GPIO_PinSource11, .GPIO_AF = GPIO_AF_SDIO}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    //    {.type = kSDIO, .GPIOx = GPIOC, .GPIO_Pin = GPIO_Pin_12, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOC, .GPIO_PinSource = GPIO_PinSource12, .GPIO_AF = GPIO_AF_SDIO}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_NOPULL},
+    //    {.type = kSDIO, .GPIOx = GPIOD, .GPIO_Pin = GPIO_Pin_2,  .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOD, .GPIO_PinSource = GPIO_PinSource2,  .GPIO_AF = GPIO_AF_SDIO}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
 
     // input
     {.type = kInput, .In_Type = DM9K_INIT, .GPIOx = DM9K_RESET_PORT, .GPIO_Pin = DM9K_RESET_PIN, .GPIO_Mode = GPIO_Mode_IN, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_NOPULL},

+ 1 - 0
User/Bsp/interface/interface.h

@@ -26,6 +26,7 @@ typedef enum
     kDMA1,
     kAM2303,
     kUSB,
+    kSDIO,
     //    kTIMER1,
     kInterfaceEnd,
 } interface_type;

+ 2 - 2
User/Bsp/sdio/sdcard.c

@@ -2978,10 +2978,10 @@ void SDIO_Interrupts_Config(void)
     NVIC_InitTypeDef NVIC_InitStructure;
 
     /* Configure the NVIC Preemption Priority Bits */
-    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
+//    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
 
     NVIC_InitStructure.NVIC_IRQChannel                   = SDIO_IRQn;
-    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
+    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
     NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
     NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
     NVIC_Init(&NVIC_InitStructure);

+ 5 - 5
User/Bsp/timer/timer.c

@@ -4,7 +4,7 @@ void timer_nvic_config(void)
 {
     NVIC_InitTypeDef NVIC_InitStructure;
     // Usart1 NVIC 配置
-    NVIC_InitStructure.NVIC_IRQChannel                   = TIM2_IRQn;
+    NVIC_InitStructure.NVIC_IRQChannel                   = TIM3_IRQn;
     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
     NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
     NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
@@ -22,11 +22,11 @@ void timer_config(void)
     TIM_TimeBaseInitStructure.TIM_CounterMode   = TIM_CounterMode_Up; //向上计数模式
     TIM_TimeBaseInitStructure.TIM_Prescaler     = 10000;              // 7999;
     TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
-    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure); //初始化TIM2
+    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure); //初始化TIM2
 
-    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //允许定时器2更新中断
+    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //允许定时器2更新中断
 
-    TIM_Cmd(TIM2, ENABLE); //使能定时器3
+    TIM_Cmd(TIM3, ENABLE); //使能定时器3
 }
 
 void user_timer_init(void)
@@ -35,7 +35,7 @@ void user_timer_init(void)
     timer_config();
 }
 
-void TIM2_IRQHandler(void)
+void TIM3_IRQHandler(void)
 {
     static uint16_t cnt = 0;
     if (cnt >= 100)

+ 76 - 83
User/Bsp/usbh/usb_bsp.c

@@ -1,29 +1,29 @@
 /**
- ******************************************************************************
- * @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.
- *
- ******************************************************************************
- */
+******************************************************************************
+* @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 ------------------------------------------------------------------*/
 
@@ -293,14 +293,13 @@ 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_IRQChannelPreemptionPriority = 3;
+    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
     NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
     NVIC_Init(&NVIC_InitStructure);
 }
@@ -315,30 +314,30 @@ void USB_OTG_BSP_EnableInterrupt(USB_OTG_CORE_HANDLE *pdev)
 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);
-  }
+/*
+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
 }
@@ -353,36 +352,36 @@ void USB_OTG_BSP_DriveVBUS(USB_OTG_CORE_HANDLE *pdev, uint8_t state)
 void USB_OTG_BSP_ConfigVBUS(USB_OTG_CORE_HANDLE *pdev)
 {
 #if 0 /* 安富莱STM32-F4 不支持VBUS */
-#ifndef USE_USB_OTG_HS 
-  GPIO_InitTypeDef GPIO_InitStructure;
+#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);
+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);
+#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 */
+/* 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
 }
@@ -398,16 +397,10 @@ 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_IRQChannelSubPriority        = 0;
     NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
 
     NVIC_Init(&NVIC_InitStructure);

+ 4 - 4
User/Bsp/usbh/usbh_it.c

@@ -48,10 +48,10 @@ void EXTI1_IRQHandler(void)
  * @param  None
  * @retval None
  */
-// void TIM2_IRQHandler(void)
-// {
-//     USB_OTG_BSP_TimerIRQ();
-// }
+void TIM2_IRQHandler(void)
+{
+    USB_OTG_BSP_TimerIRQ();
+}
 
 /**
  * @brief  OTG_FS_IRQHandler

+ 47 - 4
User/Bsp/usbh/usbh_usr.c

@@ -27,18 +27,18 @@
 
 /* Includes ------------------------------------------------------------------*/
 #include "usbh_usr.h"
+#include "bsp_fatfs.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;
-
+uint8_t             USBH_USR_ApplicationState = USH_USR_FS_INIT;
 /*  Points to the DEVICE_PROP structure of current device */
 /*  The purpose of this register is to speed up the execution */
 
@@ -350,7 +350,50 @@ void USBH_USR_OverCurrentDetected(void)
  */
 int USBH_USR_MSC_Application(void)
 {
-    return 0;
+    FRESULT res; /* 文件操作结果 */
+    switch (USBH_USR_ApplicationState)
+    {
+    case USH_USR_FS_INIT:
+        res = f_mount(&FatFs[2], "2:", 1); // 文件系统挂载时会对NAND初始化
+        if (res == FR_NO_FILESYSTEM)       // TF 卡没有文件系统
+        {
+            res = f_mkfs("2:", 0, 0); // 格式化
+            if (res == FR_OK)
+            {
+                usb_printf("USB format ok\r\n");
+                res = f_mount(NULL, "2:", 1);      /* 格式化后,先取消挂载 */
+                res = f_mount(&FatFs[2], "2:", 1); /* 重新挂载	*/
+            }
+            else
+            {
+                usb_printf("USB format fail\r\n");
+            }
+        }
+        if (res != FR_OK)
+        {
+            usb_printf("!!!USB mount file system fail (%d)\r\n", res);
+            return (-1);
+        }
+        usb_printf("USB file system mount ok !\r\n");
+
+        USBH_USR_ApplicationState = USH_USR_FS_READLIST;
+        break;
+    case USH_USR_FS_READLIST:
+        while (USB_OTG_Core.host.ConnSts == 0)
+            ;
+        USBH_USR_ApplicationState = USH_USR_FS_WRITEFILE;
+        break;
+    case USH_USR_FS_WRITEFILE:
+        while (USB_OTG_Core.host.ConnSts == 0)
+            ;
+        USBH_USR_ApplicationState = USH_USR_FS_DRAW;
+        break;
+    case USH_USR_FS_DRAW:
+        break;
+    default:
+        break;
+    }
+    return (0);
 }
 
 /**
@@ -361,7 +404,7 @@ int USBH_USR_MSC_Application(void)
  */
 void USBH_USR_DeInit(void)
 {
-    // USBH_USR_ApplicationState = USH_USR_FS_INIT;
+    USBH_USR_ApplicationState = USH_USR_FS_INIT;
 }
 
 /**

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

@@ -67,6 +67,7 @@ extern USBH_Usr_cb_TypeDef USR_cb;
 #define USH_USR_FS_READLIST  1
 #define USH_USR_FS_WRITEFILE 2
 #define USH_USR_FS_DRAW      3
+#define USH_USR_FS_TEST      4
 /**
  * @}
  */

+ 1 - 1
User/conf/fatfs/bsp_fatfs.c

@@ -66,7 +66,7 @@ void fatfs_init(void)
     else
     {
         LED1_RUN_ON;
-        DEBUG("File system mount ok !\r\n");
+        DEBUG("TF File system mount ok !\r\n");
     }
 
     res = f_mount(&FatFs[1], "1:", 1); // 文件系统挂载时会对NAND初始化

+ 1 - 2
User/conf/fatfs/bsp_fatfs.h

@@ -3,7 +3,6 @@
 
 #include "ff.h"
 #include "stm32f4xx.h"
-
+extern FATFS FatFs[_VOLUMES];
 void fatfs_init(void); // 文件系统初始化
-
 #endif

+ 4 - 15
User/conf/fatfs/diskio.c

@@ -13,17 +13,15 @@
 #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 */
 /* 为每个设备定义一个物理编号 */
 #define FS_SD   0 // SD卡
 #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 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 SD_BLOCKSIZE 512
 
@@ -47,11 +45,9 @@ DSTATUS disk_status(
     case FS_NAND:
         status = 0;
         break;
-#ifdef FS_USB
     case FS_USB:
         status = 0;
         break;
-#endif
     }
     return status;
 }
@@ -92,14 +88,13 @@ DSTATUS disk_initialize(
             status = RES_ERROR;
         }
         break;
-#ifdef FS_USB
     case FS_USB:
+        /* STM32 USB Host 口外接U盘 */
         if (HCD_IsDeviceConnected(&USB_OTG_Core))
         {
             status &= ~STA_NOINIT;
         }
         break;
-#endif
     }
     return status;
 }
@@ -165,7 +160,6 @@ DRESULT disk_read(
             status = RES_ERROR;
         }
         break;
-#ifdef FS_USB
     case FS_USB:
     {
         BYTE res = USBH_MSC_OK;
@@ -193,7 +187,6 @@ DRESULT disk_read(
             status = RES_ERROR;
         }
     }
-#endif
     break;
     }
 
@@ -268,7 +261,6 @@ DRESULT disk_write(
             status = RES_ERROR;
         }
         break;
-#ifdef FS_USB
     case FS_USB:
     {
         BYTE res = USBH_MSC_OK;
@@ -296,7 +288,6 @@ DRESULT disk_write(
             status = RES_ERROR;
         }
     }
-#endif
     break;
     }
 
@@ -343,7 +334,6 @@ 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)
@@ -371,7 +361,6 @@ DRESULT disk_ioctl(
             status = RES_PARERR;
             break;
         }
-#endif
         break;
     }
 

+ 2 - 0
User/main.c

@@ -8,6 +8,7 @@
 */
 
 #include "main.h"
+#include "bsp_fatfs.h"
 
 void DumpHEX(uint8_t *buffer, uint32_t len)
 {
@@ -174,6 +175,7 @@ void misc_task(void)
     {
         iwdg_feed(MISC_DOG);
         key_scan();
+        USBH_Process(&USB_OTG_Core, &USB_Host);
         key_state = key_get();
         if (key_state != KEY_NONE)
         {

+ 1 - 0
User/main.h

@@ -16,6 +16,7 @@
 #include "stm32f4x7_phy.h"
 #include "stm32f4xx.h"
 #include <stdio.h>
+#include "usbh_bsp_msc.h"
 
 void bsp_init(void);
 void init_task(void *pvParameters);

+ 4 - 4
User/system_stm32f4xx.c

@@ -348,9 +348,9 @@
      through STLINK MCO pin of STM32F103 microcontroller. The frequency cannot be changed
      and is fixed at 8 MHz.
      Hardware configuration needed for Nucleo Board:
-      SB54, SB55 OFF
-      R35 removed
-      SB16, SB50 ON */
+     � SB54, SB55 OFF
+     � R35 removed
+     � SB16, SB50 ON */
 /* #define USE_HSE_BYPASS */
 
 #if defined(USE_HSE_BYPASS)
@@ -390,7 +390,7 @@
 #endif /* STM32F446xx */
 
 #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx)
-#define PLL_N 360
+#define PLL_N 336
 /* SYSCLK = PLL_VCO / PLL_P */
 #define PLL_P 2
 #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx || STM32F446xx */