你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发

[复制链接]
mon51 发布时间:2014-3-31 18:51
STM32F207的HOST usb 非常有用,可以接U盘,也可以接USB接口的键盘。非常方便,但ST的U库V2.0,V2.1实际使用都有问题。
经过修改,现在可以很好第读写U盘和连接HID键盘。
程序代码的核心是V2.1的库。
通过U盘D读写,可以方便地进行IAP-BOOT操作。以及用户键盘操作!
收藏 2 评论23 发布时间:2014-3-31 18:51

举报

23个回答
mon51 回答时间:2014-3-31 18:52:44

RE:STM32F207的host USB的MSC和HID的合并开发

吃完饭来发电路和源程序。
mon51 回答时间:2014-4-1 10:51:07

RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发

继续发图:
电路很简单,占用F207的HS-usb:
mon51 回答时间:2014-4-1 10:53:18

回复:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发

回复第 3 楼 于2014-04-01 10:51:07发表:
继续发图:
电路很简单,占用F207的HS-usb:
 

 
USB.png
mon51 回答时间:2014-4-1 10:55:01

RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发

初始化端口程序:
void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev){
  GPIO_InitTypeDef GPIO_InitStructure;
  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) ;
        //-----------------------------------------------------------------
        //Vbus_EN   根据实际电路修改!
  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOE , ENABLE) ;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
         
        GPIO_ResetBits(GPIOE, GPIO_Pin_0);
        //-------------------------------------------------------------------
  USB_OTG_BSP_TimeInit();
        USB_OTG_BSP_mDelay(500);   //Delay is need for stabilising the Vbus Low
}
mon51 回答时间:2014-4-1 10:55:30

RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发

采用内部的:USE_EMBEDDED_PHY  ,所以直接用IO连接U端口。
mon51 回答时间:2014-4-1 10:57:01

RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发

以下是ST原来的库函数定义文件头:
#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 */
mon51 回答时间:2014-4-1 10:59:15

RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发

主程序非常简单:(这里是用U盘iap—BOOT为例):
int main(void){
   
  /*!< At this stage the microcontroller clock setting is already configured,
  this is done through SystemInit() function which is called from startup
  file (startup_stm32fxxx_xx.s) before to branch to application main.
  To reconfigure the default setting of SystemInit() function, refer to
  system_stm32fxxx.c file
  */  
  
  /* Init Host Library */
  USBH_Init(&USB_OTG_Core,
            USB_OTG_HS_CORE_ID,
            &USB_Host,
            &USBH_MSC_cb,
            &USR_cb);
  
  while (1){
    /* Host Task handler */
    USBH_Process(&USB_OTG_Core, &USB_Host);
                if(iapRun==0){
                        IAP_URN_UserProgrammer();
                }
  }
}
mon51 回答时间:2014-4-1 11:01:59

RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发

由于HOST-MSC和HID的初始化全部相同,这里先不介绍,不同点是从枚举后分析数据协议开始,所以我们在这里添加设备识别代码,然后再根据不同的设备,做相应的事情,这里主要是MSC的处理和HID设备的处理,同样也可以添加其他的设备.
  case HOST_USR_INPUT:   
    /*The function should return user response true to move to class state */
          //修改:添加MSC-HID自动判别,然后再初始化CALSS-CB
                if(JB_MSC_HID(phost,0)){ //0:判别MSC,1:判别HID
                        SetHOST_USB_MODE(0);
                        phost->class_cb=&USBH_MSC_cb;
                        if((phost->class_cb->Init(pdev, phost)) == USBH_OK){
                                phost->gState  = HOST_CLASS_REQUEST;     
                        }     
                }else{
                        if(JB_MSC_HID(phost,1)){ //0:判别MSC,1:判别HID
                                SetHOST_USB_MODE(1);
                                phost->class_cb=&HID_cb;
                                //phost->usr_cb=&USR_Callbacks;//???????
                                if((phost->class_cb->Init(pdev, phost)) == USBH_OK){
                                        phost->gState  = HOST_CLASS_REQUEST;     
                                }
                        }
    }   
    break;
mon51 回答时间:2014-4-1 11:02:43

RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发

case HOST_ENUMERATION:     
    /* Check for enumeration status */  
    if ( USBH_HandleEnum(pdev , phost) == USBH_OK){
      /* The function shall return USBH_OK when full enumeration is complete */
      
      /* user callback for end of device basic enumeration */
      phost->usr_cb->EnumerationDone();
      
      phost->gState  = HOST_USR_INPUT;   
    }
    break;
123下一页

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版