Merge pull request #93 from zyp/master
[fw/stlink] / example / stm32f4 / STM32_USB_OTG_Driver / src / usb_hcd.c
1 /**
2   ******************************************************************************
3   * @file    usb_hcd.c
4   * @author  MCD Application Team
5   * @version V2.0.0
6   * @date    22-July-2011
7   * @brief   Host Interface Layer
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>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
19   ******************************************************************************
20   */
21
22 /* Includes ------------------------------------------------------------------*/
23 #include "usb_core.h"
24 #include "usb_hcd.h"
25 #include "usb_conf.h"
26 #include "usb_bsp.h"
27
28
29 /** @addtogroup USB_OTG_DRIVER
30   * @{
31   */
32   
33 /** @defgroup USB_HCD 
34   * @brief This file is the interface between EFSL ans Host mass-storage class
35   * @{
36   */
37
38
39 /** @defgroup USB_HCD_Private_Defines
40   * @{
41   */ 
42 /**
43   * @}
44   */ 
45  
46
47 /** @defgroup USB_HCD_Private_TypesDefinitions
48   * @{
49   */ 
50 /**
51   * @}
52   */ 
53
54
55
56 /** @defgroup USB_HCD_Private_Macros
57   * @{
58   */ 
59 /**
60   * @}
61   */ 
62
63
64 /** @defgroup USB_HCD_Private_Variables
65   * @{
66   */ 
67 /**
68   * @}
69   */ 
70
71
72 /** @defgroup USB_HCD_Private_FunctionPrototypes
73   * @{
74   */ 
75 /**
76   * @}
77   */ 
78
79
80 /** @defgroup USB_HCD_Private_Functions
81   * @{
82   */ 
83
84 /**
85   * @brief  HCD_Init 
86   *         Initialize the HOST portion of the driver.
87   * @param  pdev: Selected device
88   * @param  base_address: OTG base address
89   * @retval Status
90   */
91 uint32_t HCD_Init(USB_OTG_CORE_HANDLE *pdev , 
92                   USB_OTG_CORE_ID_TypeDef coreID)
93 {
94   uint8_t i = 0;
95   pdev->host.ConnSts = 0;
96   
97   for (i= 0; i< USB_OTG_MAX_TX_FIFOS; i++)
98   {
99   pdev->host.ErrCnt[i]  = 0;
100   pdev->host.XferCnt[i]   = 0;
101   pdev->host.HC_Status[i]   = HC_IDLE;
102   }
103   pdev->host.hc[0].max_packet  = 8; 
104
105   USB_OTG_SelectCore(pdev, coreID);
106 #ifndef DUAL_ROLE_MODE_ENABLED
107   USB_OTG_DisableGlobalInt(pdev);
108   USB_OTG_CoreInit(pdev);
109
110   /* Force Host Mode*/
111   USB_OTG_SetCurrentMode(pdev , HOST_MODE);
112   USB_OTG_CoreInitHost(pdev);
113   USB_OTG_EnableGlobalInt(pdev);
114 #endif
115    
116   return 0;
117 }
118
119
120 /**
121   * @brief  HCD_GetCurrentSpeed
122   *         Get Current device Speed.
123   * @param  pdev : Selected device
124   * @retval Status
125   */
126
127 uint32_t HCD_GetCurrentSpeed (USB_OTG_CORE_HANDLE *pdev)
128 {    
129     USB_OTG_HPRT0_TypeDef  HPRT0;
130     HPRT0.d32 = USB_OTG_READ_REG32(pdev->regs.HPRT0);
131     
132     return HPRT0.b.prtspd;
133 }
134
135 /**
136   * @brief  HCD_ResetPort
137   *         Issues the reset command to device
138   * @param  pdev : Selected device
139   * @retval Status
140   */
141 uint32_t HCD_ResetPort(USB_OTG_CORE_HANDLE *pdev)
142 {
143   /*
144   Before starting to drive a USB reset, the application waits for the OTG 
145   interrupt triggered by the debounce done bit (DBCDNE bit in OTG_FS_GOTGINT), 
146   which indicates that the bus is stable again after the electrical debounce 
147   caused by the attachment of a pull-up resistor on DP (FS) or DM (LS).
148   */
149   
150   USB_OTG_ResetPort(pdev); 
151   return 0;
152 }
153
154 /**
155   * @brief  HCD_IsDeviceConnected
156   *         Check if the device is connected.
157   * @param  pdev : Selected device
158   * @retval Device connection status. 1 -> connected and 0 -> disconnected
159   * 
160   */
161 uint32_t HCD_IsDeviceConnected(USB_OTG_CORE_HANDLE *pdev)
162 {
163   return (pdev->host.ConnSts);
164 }
165
166 /**
167   * @brief  HCD_GetCurrentFrame 
168   *         This function returns the frame number for sof packet
169   * @param  pdev : Selected device
170   * @retval Frame number
171   * 
172   */
173 uint32_t HCD_GetCurrentFrame (USB_OTG_CORE_HANDLE *pdev) 
174 {
175  return (USB_OTG_READ_REG32(&pdev->regs.HREGS->HFNUM) & 0xFFFF) ;
176 }
177
178 /**
179   * @brief  HCD_GetURB_State 
180   *         This function returns the last URBstate
181   * @param  pdev: Selected device
182   * @retval URB_STATE
183   * 
184   */
185 URB_STATE HCD_GetURB_State (USB_OTG_CORE_HANDLE *pdev , uint8_t ch_num) 
186 {
187   return pdev->host.URB_State[ch_num] ;
188 }
189
190 /**
191   * @brief  HCD_GetXferCnt 
192   *         This function returns the last URBstate
193   * @param  pdev: Selected device
194   * @retval No. of data bytes transferred
195   * 
196   */
197 uint32_t HCD_GetXferCnt (USB_OTG_CORE_HANDLE *pdev, uint8_t ch_num) 
198 {
199   return pdev->host.XferCnt[ch_num] ;
200 }
201
202
203
204 /**
205   * @brief  HCD_GetHCState 
206   *         This function returns the HC Status 
207   * @param  pdev: Selected device
208   * @retval HC_STATUS
209   * 
210   */
211 HC_STATUS HCD_GetHCState (USB_OTG_CORE_HANDLE *pdev ,  uint8_t ch_num) 
212 {
213   return pdev->host.HC_Status[ch_num] ;
214 }
215
216 /**
217   * @brief  HCD_HC_Init 
218   *         This function prepare a HC and start a transfer
219   * @param  pdev: Selected device
220   * @param  hc_num: Channel number 
221   * @retval status 
222   */
223 uint32_t HCD_HC_Init (USB_OTG_CORE_HANDLE *pdev , uint8_t hc_num) 
224 {
225   return USB_OTG_HC_Init(pdev, hc_num);  
226 }
227
228 /**
229   * @brief  HCD_SubmitRequest 
230   *         This function prepare a HC and start a transfer
231   * @param  pdev: Selected device
232   * @param  hc_num: Channel number 
233   * @retval status
234   */
235 uint32_t HCD_SubmitRequest (USB_OTG_CORE_HANDLE *pdev , uint8_t hc_num) 
236 {
237   
238   pdev->host.URB_State[hc_num] =   URB_IDLE;  
239   pdev->host.hc[hc_num].xfer_count = 0 ;
240   return USB_OTG_HC_StartXfer(pdev, hc_num);
241 }
242
243
244 /**
245 * @}
246 */ 
247
248 /**
249 * @}
250 */ 
251
252 /**
253 * @}
254 */
255
256 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/