c703d399803476a46eee05057baff9b50454cde1
[fw/stlink] / example / stm32f4 / STM32_USB_HOST_Library / Core / src / usbh_hcs.c
1 /**
2   ******************************************************************************
3   * @file    usbh_hcs.c
4   * @author  MCD Application Team
5   * @version V2.0.0
6   * @date    22-July-2011
7   * @brief   This file implements functions for opening and closing host channels
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 "usbh_hcs.h"
24
25 /** @addtogroup USBH_LIB
26   * @{
27   */
28
29 /** @addtogroup USBH_LIB_CORE
30 * @{
31 */
32   
33 /** @defgroup USBH_HCS
34   * @brief This file includes opening and closing host channels
35   * @{
36   */ 
37
38 /** @defgroup USBH_HCS_Private_Defines
39   * @{
40   */ 
41 /**
42   * @}
43   */ 
44
45 /** @defgroup USBH_HCS_Private_TypesDefinitions
46   * @{
47   */ 
48 /**
49   * @}
50   */ 
51
52
53 /** @defgroup USBH_HCS_Private_Macros
54   * @{
55   */ 
56 /**
57   * @}
58   */ 
59
60
61 /** @defgroup USBH_HCS_Private_Variables
62   * @{
63   */ 
64
65 /**
66   * @}
67   */ 
68
69
70 /** @defgroup USBH_HCS_Private_FunctionPrototypes
71   * @{
72   */ 
73 static uint16_t USBH_GetFreeChannel (USB_OTG_CORE_HANDLE *pdev);
74 /**
75   * @}
76   */ 
77
78
79 /** @defgroup USBH_HCS_Private_Functions
80   * @{
81   */ 
82
83
84
85 /**
86   * @brief  USBH_Open_Channel
87   *         Open a  pipe
88   * @param  pdev : Selected device
89   * @param  hc_num: Host channel Number
90   * @param  dev_address: USB Device address allocated to attached device
91   * @param  speed : USB device speed (Full/Low)
92   * @param  ep_type: end point type (Bulk/int/ctl)
93   * @param  mps: max pkt size
94   * @retval Status
95   */
96 uint8_t USBH_Open_Channel  (USB_OTG_CORE_HANDLE *pdev,
97                             uint8_t hc_num,
98                             uint8_t dev_address,
99                             uint8_t speed,
100                             uint8_t ep_type,
101                             uint16_t mps)
102 {
103
104   pdev->host.hc[hc_num].ep_num = pdev->host.channel[hc_num]& 0x7F;
105   pdev->host.hc[hc_num].ep_is_in = (pdev->host.channel[hc_num] & 0x80 ) == 0x80;  
106   pdev->host.hc[hc_num].dev_addr = dev_address;  
107   pdev->host.hc[hc_num].ep_type = ep_type;  
108   pdev->host.hc[hc_num].max_packet = mps; 
109   pdev->host.hc[hc_num].speed = speed; 
110   pdev->host.hc[hc_num].toggle_in = 0; 
111   pdev->host.hc[hc_num].toggle_out = 0;   
112   if(speed == HPRT0_PRTSPD_HIGH_SPEED)
113   {
114     pdev->host.hc[hc_num].do_ping = 1;
115   }
116   
117   USB_OTG_HC_Init(pdev, hc_num) ;
118   
119   return HC_OK; 
120
121 }
122
123 /**
124   * @brief  USBH_Modify_Channel
125   *         Modify a  pipe
126   * @param  pdev : Selected device
127   * @param  hc_num: Host channel Number
128   * @param  dev_address: USB Device address allocated to attached device
129   * @param  speed : USB device speed (Full/Low)
130   * @param  ep_type: end point type (Bulk/int/ctl)
131   * @param  mps: max pkt size
132   * @retval Status
133   */
134 uint8_t USBH_Modify_Channel (USB_OTG_CORE_HANDLE *pdev,
135                             uint8_t hc_num,
136                             uint8_t dev_address,
137                             uint8_t speed,
138                             uint8_t ep_type,
139                             uint16_t mps)
140 {
141   
142   if(dev_address != 0)
143   {
144     pdev->host.hc[hc_num].dev_addr = dev_address;  
145   }
146   
147   if((pdev->host.hc[hc_num].max_packet != mps) && (mps != 0))
148   {
149     pdev->host.hc[hc_num].max_packet = mps; 
150   }
151   
152   if((pdev->host.hc[hc_num].speed != speed ) && (speed != 0 )) 
153   {
154     pdev->host.hc[hc_num].speed = speed; 
155   }
156   
157   USB_OTG_HC_Init(pdev, hc_num);
158   return HC_OK; 
159
160 }
161
162 /**
163   * @brief  USBH_Alloc_Channel
164   *         Allocate a new channel for the pipe
165   * @param  ep_addr: End point for which the channel to be allocated
166   * @retval hc_num: Host channel number
167   */
168 uint8_t USBH_Alloc_Channel  (USB_OTG_CORE_HANDLE *pdev, uint8_t ep_addr)
169 {
170   uint16_t hc_num;
171   
172   hc_num =  USBH_GetFreeChannel(pdev);
173
174   if (hc_num != HC_ERROR)
175   {
176         pdev->host.channel[hc_num] = HC_USED | ep_addr;
177   }
178   return hc_num;
179 }
180
181 /**
182   * @brief  USBH_Free_Pipe
183   *         Free the USB host channel
184   * @param  idx: Channel number to be freed 
185   * @retval Status
186   */
187 uint8_t USBH_Free_Channel  (USB_OTG_CORE_HANDLE *pdev, uint8_t idx)
188 {
189    if(idx < HC_MAX)
190    {
191          pdev->host.channel[idx] &= HC_USED_MASK;
192    }
193    return USBH_OK;
194 }
195
196
197 /**
198   * @brief  USBH_DeAllocate_AllChannel
199   *         Free all USB host channel
200 * @param  pdev : core instance
201   * @retval Status
202   */
203 uint8_t USBH_DeAllocate_AllChannel  (USB_OTG_CORE_HANDLE *pdev)
204 {
205    uint8_t idx;
206    
207    for (idx = 2; idx < HC_MAX ; idx ++)
208    {
209          pdev->host.channel[idx] = 0;
210    }
211    return USBH_OK;
212 }
213
214 /**
215   * @brief  USBH_GetFreeChannel
216   *         Get a free channel number for allocation to a device endpoint
217   * @param  None
218   * @retval idx: Free Channel number
219   */
220 static uint16_t USBH_GetFreeChannel (USB_OTG_CORE_HANDLE *pdev)
221 {
222   uint8_t idx = 0;
223   
224   for (idx = 0 ; idx < HC_MAX ; idx++)
225   {
226         if ((pdev->host.channel[idx] & HC_USED) == 0)
227         {
228            return idx;
229         } 
230   }
231   return HC_ERROR;
232 }
233
234
235 /**
236 * @}
237 */ 
238
239 /**
240 * @}
241 */ 
242
243 /**
244 * @}
245 */
246
247 /**
248 * @}
249 */ 
250
251 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
252
253