Added STM32F4 discovery demo sources and make file
[fw/stlink] / example / stm32f4 / Projects / discovery_demo / usb_bsp.c
1 /**
2   ******************************************************************************
3   * @file    usb_bsp.c
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    19-September-2011
7   * @brief   This file is responsible to offer board support package and is 
8   *          configurable by user.
9   ******************************************************************************
10   * @attention
11   *
12   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
14   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
15   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
16   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
17   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 
18   *
19   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
20   ******************************************************************************
21   */ 
22
23 /* Includes ------------------------------------------------------------------*/
24 #include "usb_bsp.h"
25 #include "usbd_conf.h"
26 #include "stm32f4_discovery.h"
27
28 //Library config for this project!!!!!!!!!!!
29 #include "stm32f4xx_conf.h"
30
31
32 /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
33 * @{
34 */
35
36 /** @defgroup USB_BSP
37 * @brief This file is responsible to offer board support package
38 * @{
39 */ 
40
41 /** @defgroup USB_BSP_Private_Defines
42 * @{
43 */ 
44 /**
45 * @}
46 */ 
47
48
49 /** @defgroup USB_BSP_Private_TypesDefinitions
50 * @{
51 */ 
52 /**
53 * @}
54 */ 
55
56
57
58
59
60 /** @defgroup USB_BSP_Private_Macros
61 * @{
62 */ 
63 /**
64 * @}
65 */ 
66
67 /** @defgroup USBH_BSP_Private_Variables
68 * @{
69 */ 
70
71 /**
72 * @}
73 */ 
74
75 /** @defgroup USBH_BSP_Private_FunctionPrototypes
76 * @{
77 */ 
78 /**
79 * @}
80 */ 
81
82 /** @defgroup USB_BSP_Private_Functions
83 * @{
84 */ 
85
86
87 /**
88 * @brief  USB_OTG_BSP_Init
89 *         Initilizes BSP configurations
90 * @param  None
91 * @retval None
92 */
93
94 void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev)
95 {
96   GPIO_InitTypeDef GPIO_InitStructure;
97
98 #ifndef USE_ULPI_PHY 
99 #ifdef USB_OTG_FS_LOW_PWR_MGMT_SUPPORT
100   EXTI_InitTypeDef EXTI_InitStructure;
101   NVIC_InitTypeDef NVIC_InitStructure; 
102 #endif  
103 #endif
104   
105  
106  #ifdef USE_USB_OTG_FS 
107
108   RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA , ENABLE);  
109   
110   /* Configure SOF VBUS ID DM DP Pins */
111   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8  | 
112     GPIO_Pin_9  | 
113       GPIO_Pin_11 | 
114         GPIO_Pin_12;
115   
116   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
117   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
118   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
119   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
120   GPIO_Init(GPIOA, &GPIO_InitStructure);  
121   
122   GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_OTG1_FS) ;
123   GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_OTG1_FS) ; 
124   GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_OTG1_FS) ; 
125   GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_OTG1_FS) ;
126   
127   /* this for ID line debug */
128   
129   
130   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_10;
131   GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
132   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;  
133   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
134   GPIO_Init(GPIOA, &GPIO_InitStructure);  
135   GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_OTG1_FS) ;   
136
137   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
138   RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE) ; 
139  #else // USE_USB_OTG_HS 
140
141   #ifdef USE_ULPI_PHY // ULPI
142   RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | 
143                          RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOH | 
144                            RCC_AHB1Periph_GPIOI, ENABLE);    
145   
146   
147   GPIO_PinAFConfig(GPIOA,GPIO_PinSource3, GPIO_AF_OTG2_HS) ; // D0
148   GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // CLK
149   GPIO_PinAFConfig(GPIOB,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // D1
150   GPIO_PinAFConfig(GPIOB,GPIO_PinSource1, GPIO_AF_OTG2_HS) ; // D2
151   GPIO_PinAFConfig(GPIOB,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // D7
152   GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_OTG2_HS) ; // D3
153   GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // D4
154   GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_OTG2_HS) ; // D5
155   GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_HS) ; // D6
156   GPIO_PinAFConfig(GPIOH,GPIO_PinSource4, GPIO_AF_OTG2_HS) ; // NXT
157   GPIO_PinAFConfig(GPIOI,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // DIR
158   GPIO_PinAFConfig(GPIOC,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // STP
159   
160   // CLK
161   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ; 
162   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
163   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
164   GPIO_Init(GPIOA, &GPIO_InitStructure);  
165   
166   // D0
167   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3  ; 
168   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
169   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
170   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
171   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
172   GPIO_Init(GPIOA, &GPIO_InitStructure);  
173   
174   
175   
176   // D1 D2 D3 D4 D5 D6 D7
177   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1  |
178     GPIO_Pin_5 | GPIO_Pin_10 | 
179       GPIO_Pin_11| GPIO_Pin_12 | 
180         GPIO_Pin_13 ;
181   
182   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
183   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
184   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
185   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
186   GPIO_Init(GPIOB, &GPIO_InitStructure);  
187   
188   
189   // STP
190   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0  ;
191   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
192   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
193   GPIO_Init(GPIOC, &GPIO_InitStructure);  
194   
195   //NXT  
196   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
197   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
198   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
199   GPIO_Init(GPIOH, &GPIO_InitStructure);  
200   
201   
202   //DIR
203   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ; 
204   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
205   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
206   GPIO_Init(GPIOI, &GPIO_InitStructure);  
207   
208   
209   RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS | 
210                          RCC_AHB1Periph_OTG_HS_ULPI, ENABLE) ;    
211    
212   #else
213    #ifdef USE_I2C_PHY    
214   RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB , ENABLE);  
215   /* Configure RESET INTN SCL SDA (Phy/I2C) Pins */
216   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | 
217     GPIO_Pin_1 | 
218       GPIO_Pin_10 | 
219         GPIO_Pin_11;
220   
221   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
222   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
223   GPIO_Init(GPIOB, &GPIO_InitStructure);  
224   
225   GPIO_PinAFConfig(GPIOB,GPIO_PinSource0,GPIO_AF_OTG2_FS) ; 
226   GPIO_PinAFConfig(GPIOB,GPIO_PinSource1,GPIO_AF_OTG2_FS) ; 
227   GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_OTG2_FS) ; 
228   GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_OTG2_FS);
229   RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ;  
230   
231    #else
232
233   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE);
234   
235   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12  | 
236     GPIO_Pin_13 |
237       GPIO_Pin_14 | 
238         GPIO_Pin_15;
239   
240   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
241   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
242   GPIO_Init(GPIOB, &GPIO_InitStructure);  
243   
244   GPIO_PinAFConfig(GPIOB,GPIO_PinSource12, GPIO_AF_OTG2_FS) ; 
245   GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_FS) ; 
246   GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_OTG2_FS) ; 
247   GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_OTG2_FS) ;
248   RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ;  
249    #endif  
250   #endif // USE_ULPI_PHY
251   
252  #endif //USB_OTG_HS
253
254   
255   /* enable the PWR clock */
256   RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, ENABLE);   
257   
258   /* Configure the Key button in EXTI mode */
259   STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);
260   
261 #ifdef USB_OTG_FS_LOW_PWR_MGMT_SUPPORT
262   EXTI_ClearITPendingBit(EXTI_Line18);
263   
264   EXTI_InitStructure.EXTI_Line = EXTI_Line18; 
265   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
266   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
267   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
268   EXTI_Init(&EXTI_InitStructure);
269
270   EXTI_ClearITPendingBit(EXTI_Line18);   
271
272   NVIC_InitStructure.NVIC_IRQChannel = OTG_FS_WKUP_IRQn;
273   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
274   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
275   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
276   NVIC_Init(&NVIC_InitStructure);
277
278   EXTI_ClearITPendingBit(EXTI_Line18);    
279 #endif 
280   
281 #ifdef USB_OTG_HS_LOW_PWR_MGMT_SUPPORT
282   EXTI_ClearITPendingBit(EXTI_Line20);
283   
284   EXTI_InitStructure.EXTI_Line = EXTI_Line20; 
285   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
286   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
287   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
288   EXTI_Init(&EXTI_InitStructure);
289
290   EXTI_ClearITPendingBit(EXTI_Line20);   
291
292   NVIC_InitStructure.NVIC_IRQChannel = OTG_HS_WKUP_IRQn;
293   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
294   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
295   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
296   NVIC_Init(&NVIC_InitStructure);
297
298   EXTI_ClearITPendingBit(EXTI_Line20);    
299 #endif   
300
301   EXTI_ClearITPendingBit(USER_BUTTON_EXTI_LINE);  
302 }
303 /**
304 * @brief  USB_OTG_BSP_EnableInterrupt
305 *         Enabele USB Global interrupt
306 * @param  None
307 * @retval None
308 */
309 void USB_OTG_BSP_EnableInterrupt(USB_OTG_CORE_HANDLE *pdev)
310 {
311   NVIC_InitTypeDef NVIC_InitStructure; 
312   
313   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
314 #ifdef USE_USB_OTG_HS   
315   NVIC_InitStructure.NVIC_IRQChannel = OTG_HS_IRQn;
316 #else
317   NVIC_InitStructure.NVIC_IRQChannel = OTG_FS_IRQn;  
318 #endif
319   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
320   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
321   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
322   NVIC_Init(&NVIC_InitStructure);  
323 #ifdef USB_OTG_HS_DEDICATED_EP1_ENABLED
324   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
325   NVIC_InitStructure.NVIC_IRQChannel = OTG_HS_EP1_OUT_IRQn;
326   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
327   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
328   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
329   NVIC_Init(&NVIC_InitStructure);  
330   
331   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
332   NVIC_InitStructure.NVIC_IRQChannel = OTG_HS_EP1_IN_IRQn;
333   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
334   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
335   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
336   NVIC_Init(&NVIC_InitStructure);   
337 #endif
338 }
339 /**
340 * @brief  USB_OTG_BSP_uDelay
341 *         This function provides delay time in micro sec
342 * @param  usec : Value of delay required in micro sec
343 * @retval None
344 */
345 void USB_OTG_BSP_uDelay (const uint32_t usec)
346 {
347   uint32_t count = 0;
348   const uint32_t utime = (120 * usec / 7);
349   do
350   {
351     if ( ++count > utime )
352     {
353       return ;
354     }
355   }
356   while (1);
357 }
358
359
360 /**
361 * @brief  USB_OTG_BSP_mDelay
362 *          This function provides delay time in milli sec
363 * @param  msec : Value of delay required in milli sec
364 * @retval None
365 */
366 void USB_OTG_BSP_mDelay (const uint32_t msec)
367 {
368   USB_OTG_BSP_uDelay(msec * 1000);   
369 }
370 /**
371 * @}
372 */ 
373
374 /**
375 * @}
376 */ 
377
378 /**
379 * @}
380 */
381
382 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/