xref: /btstack/port/stm32-f4discovery-usb/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_pipes.c (revision a8f7f3fcbcd51f8d2e92aca076b6a9f812db358c)
1 /**
2   ******************************************************************************
3   * @file    usbh_pipes.c
4   * @author  MCD Application Team
5   * @brief   This file implements functions for opening and closing Pipes
6   ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; Copyright (c) 2015 STMicroelectronics.
10   * All rights reserved.</center></h2>
11   *
12   * This software component is licensed by ST under Ultimate Liberty license
13   * SLA0044, the "License"; You may not use this file except in compliance with
14   * the License. You may obtain a copy of the License at:
15   *                      www.st.com/SLA0044
16   *
17   ******************************************************************************
18   */
19 
20 /* Includes ------------------------------------------------------------------*/
21 #include "usbh_pipes.h"
22 
23 /** @addtogroup USBH_LIB
24   * @{
25   */
26 
27 /** @addtogroup USBH_LIB_CORE
28 * @{
29 */
30 
31 /** @defgroup USBH_PIPES
32   * @brief This file includes opening and closing Pipes
33   * @{
34   */
35 
36 /** @defgroup USBH_PIPES_Private_Defines
37   * @{
38   */
39 /**
40   * @}
41   */
42 
43 /** @defgroup USBH_PIPES_Private_TypesDefinitions
44   * @{
45   */
46 /**
47   * @}
48   */
49 
50 
51 /** @defgroup USBH_PIPES_Private_Macros
52   * @{
53   */
54 /**
55   * @}
56   */
57 
58 
59 /** @defgroup USBH_PIPES_Private_Variables
60   * @{
61   */
62 
63 /**
64   * @}
65   */
66 
67 
68 /** @defgroup USBH_PIPES_Private_Functions
69   * @{
70   */
71 static uint16_t USBH_GetFreePipe(USBH_HandleTypeDef *phost);
72 
73 
74 /**
75   * @brief  USBH_Open_Pipe
76   *         Open a  pipe
77   * @param  phost: Host Handle
78   * @param  pipe_num: Pipe Number
79   * @param  dev_address: USB Device address allocated to attached device
80   * @param  speed : USB device speed (Full/Low)
81   * @param  ep_type: end point type (Bulk/int/ctl)
82   * @param  mps: max pkt size
83   * @retval USBH Status
84   */
USBH_OpenPipe(USBH_HandleTypeDef * phost,uint8_t pipe_num,uint8_t epnum,uint8_t dev_address,uint8_t speed,uint8_t ep_type,uint16_t mps)85 USBH_StatusTypeDef USBH_OpenPipe(USBH_HandleTypeDef *phost, uint8_t pipe_num,
86                                  uint8_t epnum, uint8_t dev_address,
87                                  uint8_t speed, uint8_t ep_type, uint16_t mps)
88 {
89   USBH_LL_OpenPipe(phost, pipe_num, epnum, dev_address, speed, ep_type, mps);
90 
91   return USBH_OK;
92 }
93 
94 
95 /**
96   * @brief  USBH_ClosePipe
97   *         Close a  pipe
98   * @param  phost: Host Handle
99   * @param  pipe_num: Pipe Number
100   * @retval USBH Status
101   */
USBH_ClosePipe(USBH_HandleTypeDef * phost,uint8_t pipe_num)102 USBH_StatusTypeDef USBH_ClosePipe(USBH_HandleTypeDef *phost, uint8_t pipe_num)
103 {
104   USBH_LL_ClosePipe(phost, pipe_num);
105 
106   return USBH_OK;
107 }
108 
109 
110 /**
111   * @brief  USBH_Alloc_Pipe
112   *         Allocate a new Pipe
113   * @param  phost: Host Handle
114   * @param  ep_addr: End point for which the Pipe to be allocated
115   * @retval Pipe number
116   */
USBH_AllocPipe(USBH_HandleTypeDef * phost,uint8_t ep_addr)117 uint8_t USBH_AllocPipe(USBH_HandleTypeDef *phost, uint8_t ep_addr)
118 {
119   uint16_t pipe;
120 
121   pipe =  USBH_GetFreePipe(phost);
122 
123   if (pipe != 0xFFFFU)
124   {
125     phost->Pipes[pipe & 0xFU] = 0x8000U | ep_addr;
126   }
127 
128   return (uint8_t)pipe;
129 }
130 
131 
132 /**
133   * @brief  USBH_Free_Pipe
134   *         Free the USB Pipe
135   * @param  phost: Host Handle
136   * @param  idx: Pipe number to be freed
137   * @retval USBH Status
138   */
USBH_FreePipe(USBH_HandleTypeDef * phost,uint8_t idx)139 USBH_StatusTypeDef USBH_FreePipe(USBH_HandleTypeDef *phost, uint8_t idx)
140 {
141   if (idx < 11U)
142   {
143     phost->Pipes[idx] &= 0x7FFFU;
144   }
145 
146   return USBH_OK;
147 }
148 
149 
150 /**
151   * @brief  USBH_GetFreePipe
152   * @param  phost: Host Handle
153   *         Get a free Pipe number for allocation to a device endpoint
154   * @retval idx: Free Pipe number
155   */
USBH_GetFreePipe(USBH_HandleTypeDef * phost)156 static uint16_t USBH_GetFreePipe(USBH_HandleTypeDef *phost)
157 {
158   uint8_t idx = 0U;
159 
160   for (idx = 0U ; idx < 11U ; idx++)
161   {
162     if ((phost->Pipes[idx] & 0x8000U) == 0U)
163     {
164       return (uint16_t)idx;
165     }
166   }
167 
168   return 0xFFFFU;
169 }
170 /**
171 * @}
172 */
173 
174 /**
175 * @}
176 */
177 
178 /**
179 * @}
180 */
181 
182 /**
183 * @}
184 */
185 
186 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
187 
188 
189