STM32f429开发中USB读写文件涉及到的库移植

第一步 

USB_HID移植(原创http://blog.csdn.net/xbl1986/article/details/17577685#comments

├── STM32_USB_Device_Library                                 USB从设备库

│   │   ├── Class
│   │   │   └── hid
│   │   │       ├── inc
│   │   │       │   └── usbd_hid_core.h
│   │   │       └── src
│   │   │           └── usbd_hid_core.c
│   │   └── Core
│   │       ├── inc
│   │       │   ├── usbd_core.h
│   │       │   ├── usbd_def.h
│   │       │   ├── usbd_ioreq.h
│   │       │   ├── usbd_req.h
│   │       │   └── usbd_usr.h
│   │       └── src
│   │           ├── usbd_core.c
│   │           ├── usbd_ioreq.c
│   │           └── usbd_req.c
│   ├── STM32_USB_OTG_Driver                                 USB OTG 库
│   │   ├── inc
│   │   │   ├── usb_bsp.h
│   │   │   ├── usb_core.h
│   │   │   ├── usb_dcd.h
│   │   │   ├── usb_dcd_int.h
│   │   │   ├── usb_defines.h
│   │   │   └── usb_regs.h
│   │   └── src
│   │       ├── usb_core.c
│   │       ├── usb_dcd.c

│   │       └── usb_dcd_int.c


从层级上分析,OTG是更加底层的,USBD的文件依赖于USB的文件,从对库的使用来讲,这些文件我们都不需要改动。

 我们需要改动的有可能是下面的文件

    ├── usb_bsp.c
    ├── usb_conf.h
    ├── usbd_conf.h
    ├── usbd_desc.c
    ├── usbd_desc.h
    └── usbd_usr.c

一些逻辑在main.c中操作,考虑如何发数据到主机端


对于一个工程来讲

    ├── stm32f4xx_conf.h
    ├── stm32f4xx_it.c
    ├── stm32f4xx_it.h
    ├── system_stm32f4xx.c

这几个文件也是从库里提取出来的,有可能会改动的


STM32F4xx_StdPeriph_Driver 这部分的内容基本上从来没有动过,是相当底层的驱动文件了


│   ├── CMSIS
│   │   ├── Include
│   │   │   ├── core_cm4.h
│   │   │   ├── core_cm4_simd.h
│   │   │   ├── core_cmFunc.h
│   │   │   └── core_cmInstr.h
│   │   └── ST
│   │       └── STM32F4xx
│   │           ├── Include
│   │           │   ├── stm32f4xx.h
│   │           │   └── system_stm32f4xx.h
│   │           └── Source
│   │               └── Templates
│   │                   └── arm
│   │                       └── startup_stm32f4xx.s

这些文件也是一般不会去动的,.s文件的名字可能有些区别


下面给出一个修改过的main.c 内容很精简了

[cpp]  view plain  copy
  1. /** 
  2.   ****************************************************************************** 
  3.   * @file    main.c  
  4.   * @author  MCD Application Team 
  5.   * @version V1.0.0 
  6.   * @date    19-September-2011 
  7.   * @brief   Main program body 
  8.   ****************************************************************************** 
  9.   * @attention 
  10.   * 
  11.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 
  12.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 
  13.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 
  14.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 
  15.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 
  16.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 
  17.   * 
  18.   * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> 
  19.   ****************************************************************************** 
  20.   */   
  21.   
  22. /* Includes ------------------------------------------------------------------*/  
  23. #include "main.h"  
  24. #include "usbd_hid_core.h"  
  25. #include "usbd_usr.h"  
  26. #include "usbd_desc.h"  
  27.   
  28. /** @addtogroup STM32F4-Discovery_Demo 
  29.   * @{ 
  30.   */  
  31.   
  32. /* Private typedef -----------------------------------------------------------*/  
  33. /* Private define ------------------------------------------------------------*/  
  34. /* Private macro -------------------------------------------------------------*/  
  35. /* Private variables ---------------------------------------------------------*/  
  36. #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED  
  37.   #if defined ( __ICCARM__ ) /*!< IAR Compiler */  
  38.     #pragma data_alignment = 4     
  39.   #endif  
  40. #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */  
  41. __ALIGN_BEGIN USB_OTG_CORE_HANDLE  USB_OTG_dev __ALIGN_END;  
  42. __IO uint32_t TimingDelay;  
  43. /* Private function prototypes -----------------------------------------------*/  
  44. static uint32_t Demo_USBConfig(void);  
  45. static void Demo_Exec(void);  
  46. /* Private functions ---------------------------------------------------------*/  
  47.   
  48. /** 
  49.   * @brief  Main program. 
  50.   * @param  None 
  51.   * @retval None 
  52.   */  
  53. int main(void)  
  54. {  
  55.   RCC_ClocksTypeDef RCC_Clocks;  
  56.   /* SysTick end of count event each 10ms */  
  57.   RCC_GetClocksFreq(&RCC_Clocks);  
  58.   SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);  
  59.   Demo_Exec();  
  60. }  
  61.   
  62. /** 
  63.   * @brief  Execute the demo application. 
  64.   * @param  None 
  65.   * @retval None 
  66.   */  
  67. static void Demo_Exec(void)  
  68. {  
  69.       uint8_t buf[4];  
  70.         buf[0]=0;  
  71.         buf[1]=7;  
  72.         buf[2]=7;  
  73.         buf[3]=0;  
  74.     /* USB configuration */  
  75.     Demo_USBConfig();  
  76.     while(1) {  
  77.         Delay(5);  
  78.         USBD_HID_SendReport (&USB_OTG_dev,   
  79.                                                  buf,  
  80.                                                  4);  
  81.     }  
  82. }  
  83.   
  84. /** 
  85.   * @brief  Initializes the USB for the demonstration application. 
  86.   * @param  None 
  87.   * @retval None 
  88.   */  
  89. static uint32_t Demo_USBConfig(void)  
  90. {  
  91.   USBD_Init(&USB_OTG_dev,  
  92.             USB_OTG_FS_CORE_ID,  
  93.             &USR_desc,   
  94.             &USBD_HID_cb,   
  95.             &USR_cb);  
  96.     
  97.   return 0;  
  98. }  
  99.   
  100. /** 
  101.   * @brief  Inserts a delay time. 
  102.   * @param  nTime: specifies the delay time length, in 10 ms. 
  103.   * @retval None 
  104.   */  
  105. void Delay(__IO uint32_t nTime)  
  106. {  
  107.   TimingDelay = nTime;  
  108.   
  109.   while(TimingDelay != 0);  
  110. }  
  111.   
  112. /** 
  113.   * @brief  Decrements the TimingDelay variable. 
  114.   * @param  None 
  115.   * @retval None 
  116.   */  
  117. void TimingDelay_Decrement(void)  
  118. {  
  119.   if (TimingDelay != 0x00)  
  120.   {   
  121.     TimingDelay--;  
  122.   }  
  123. }  
  124.   
  125. /** 
  126.   * @brief  This function handles the test program fail. 
  127.   * @param  None 
  128.   * @retval None 
  129.   */  
  130. void Fail_Handler(void)  
  131. {  
  132.   while(1)  
  133.   {  
  134.     Delay(5);  
  135.   }  
  136. }  
  137.   
  138. #ifdef  USE_FULL_ASSERT  
  139.   
  140. /** 
  141.   * @brief  Reports the name of the source file and the source line number 
  142.   *   where the assert_param error has occurred. 
  143.   * @param  file: pointer to the source file name 
  144.   * @param  line: assert_param error line source number 
  145.   * @retval None 
  146.   */  
  147. void assert_failed(uint8_t* file, uint32_t line)  
  148. {   
  149.   /* User can add his own implementation to report the file name and line number, 
  150.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */  
  151.   
  152.   /* Infinite loop */  
  153.   while (1)  
  154.   {  
  155.   }  
  156. }  
  157. #endif  
  158.   
  159. /** 
  160.   * @} 
  161.   */  
  162.   
  163.   
  164. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/  

第二步

移植并修改底层文件

先上个移植完的工程下载地址:

http://download.csdn.net/detail/u013352158/8212365


刚开始打算在discovery板子上移植USB官方库时候是想用全速模式的,折腾了半天没反应,后来看原理图时发现全速模式的两引脚接到仿真器上去了,没办法只能采用在高速口下配置成全速模式的方法,不过移植起来也算方便,总的来说就三点吧:时钟,IO最后就是把该删的都删了。

文件结构可以看下面这一篇文章,讲的很清楚了,毕竟f4和f1系列USB库差别挺大的,一开始可能无从下手

http://blog.csdn.net/xbl1986/article/details/17577685#comments


首先是预编译指令,有4个:USE_STDPERIPH_DRIVER,STM32F4XX,USE_USB_OTG_HS,USE_EMBEDDED_PHY,前三个不说了应该都看的懂,第四个有两个指令可以选择,USE_ULPI_PHY是正常的高速模式,stm32要使用高速模式必须外扩个USB3300芯片作为高速USB的物理层这里我们使用片上内置的物理层,所以用的是USE_EMBEDDED_PHY,在高速口下配置成全速模式。

接着是时钟,USB时钟必须为48M,不同板子外接的晶振不一样,官方库里用的是25M的外部晶振,而我们的discovery板子上用的是8M的晶振,需要去system_stm32f4xx.c文件里改下分频系数,下面就是配置成168M主频48M的USB时钟的分频系数,具体的时钟树可以去看数据手册。

[cpp]  view plain  copy
  1. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */  
  2. #define PLL_M      8  
  3. #define PLL_N      336  
  4.   
  5. /* SYSCLK = PLL_VCO / PLL_P */  
  6. #define PLL_P      2  
  7.   
  8. /* USB OTG FS, SDIO and RNG Clock =  PLL_VCO / PLLQ */  
  9. #define PLL_Q      7  

之后去app.c里找主函数,主函数很简单,只调用了一个USBD_Init()初始化函数,跟着这个函数去usb_bsp.c文件里把底层IO配置改下,说白了就是配置下DM,DP,VBUS三个引脚,顺便把USB和GPIOB外设时钟打开。

[cpp]  view plain  copy
  1. void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev)  
  2. {  
  3.   GPIO_InitTypeDef GPIO_InitStructure;      
  4.     
  5.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE) ;  
  6.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_OTG_HS, ENABLE) ;  
  7.     
  8.   GPIO_InitStructure.GPIO_Pin = //GPIO_Pin_12 | //ID  
  9.                                 GPIO_Pin_14 | //DM  
  10.                                 GPIO_Pin_15;  //DP  
  11.     
  12.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;  
  13.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
  14.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
  15.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;   
  16.   GPIO_Init(GPIOB, &GPIO_InitStructure);    
  17.     
  18. //  GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_OTG_HS_FS) ;   
  19.   GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_OTG_HS_FS) ;   
  20.   GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_OTG_HS_FS) ;  
  21.     
  22.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;  
  23.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;  
  24.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;  
  25.   GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;  
  26.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;  
  27.   GPIO_Init(GPIOB, &GPIO_InitStructure);    
  28. }  

到这里就接近尾声了,编译一下开始删吧,中断文件里几个外部中断和唤醒中断都可以删了,usbd_usr.c里各种函数内容都删了吧,留着个接口当备胎以后再说,删的没错误了之后把usb_conf.h里下面两条注释掉就算完成了。

// #define USB_OTG_HS_DEDICATED_EP1_ENABLED
// #define USB_OTG_HS_LOW_PWR_MGMT_SUPPORT


最后贴个usb的软断开代码,以后应该会用到的吧:

[cpp]  view plain  copy
  1. USB_OTG_DCTL_TypeDef   dctl;  
  2.    /*软断开*/  
  3. dctl.d32 = USB_OTG_READ_REG32(&pdev->regs.DREGS->DCTL);  
  4. dctl.b.sftdiscon = 1;         //0:正常工作;1:软断开  
  5. USB_OTG_WRITE_REG32(&pdev->regs.DREGS->DCTL, dctl.d32); 

猜你喜欢

转载自blog.csdn.net/xiaomei2010280/article/details/78811172