Merge pull request #10558 from iNavFlight/MrD_Correct-comments-on-OSD-symbols
[inav.git] / lib / main / STM32_USB_OTG_Driver / src / usb_dcd.c
blob6940076c302f296c9f7f2e929439a2dfca8d01a5
1 /**
2 ******************************************************************************
3 * @file usb_dcd.c
4 * @author MCD Application Team
5 * @version V2.2.0
6 * @date 09-November-2015
7 * @brief Peripheral Device Interface Layer
8 ******************************************************************************
9 * @attention
11 * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
13 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
14 * You may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at:
17 * http://www.st.com/software_license_agreement_liberty_v2
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
25 ******************************************************************************
28 /* Includes ------------------------------------------------------------------*/
29 #include "usb_dcd.h"
30 #include "usb_bsp.h"
33 /** @addtogroup USB_OTG_DRIVER
34 * @{
37 /** @defgroup USB_DCD
38 * @brief This file is the interface between EFSL ans Host mass-storage class
39 * @{
43 /** @defgroup USB_DCD_Private_Defines
44 * @{
45 */
46 /**
47 * @}
48 */
51 /** @defgroup USB_DCD_Private_TypesDefinitions
52 * @{
53 */
54 /**
55 * @}
56 */
60 /** @defgroup USB_DCD_Private_Macros
61 * @{
62 */
63 /**
64 * @}
65 */
68 /** @defgroup USB_DCD_Private_Variables
69 * @{
70 */
71 /**
72 * @}
73 */
76 /** @defgroup USB_DCD_Private_FunctionPrototypes
77 * @{
78 */
80 /**
81 * @}
82 */
85 /** @defgroup USB_DCD_Private_Functions
86 * @{
87 */
91 void DCD_Init(USB_OTG_CORE_HANDLE *pdev ,
92 USB_OTG_CORE_ID_TypeDef coreID)
94 uint32_t i;
95 USB_OTG_EP *ep;
97 USB_OTG_SelectCore (pdev , coreID);
99 pdev->dev.device_status = USB_OTG_DEFAULT;
100 pdev->dev.device_address = 0;
102 /* Init ep structure */
103 for (i = 0; i < pdev->cfg.dev_endpoints ; i++)
105 ep = &pdev->dev.in_ep[i];
106 /* Init ep structure */
107 ep->is_in = 1;
108 ep->num = i;
109 ep->tx_fifo_num = i;
110 /* Control until ep is activated */
111 ep->type = EP_TYPE_CTRL;
112 ep->maxpacket = USB_OTG_MAX_EP0_SIZE;
113 ep->xfer_buff = 0;
114 ep->xfer_len = 0;
117 for (i = 0; i < pdev->cfg.dev_endpoints; i++)
119 ep = &pdev->dev.out_ep[i];
120 /* Init ep structure */
121 ep->is_in = 0;
122 ep->num = i;
123 ep->tx_fifo_num = i;
124 /* Control until ep is activated */
125 ep->type = EP_TYPE_CTRL;
126 ep->maxpacket = USB_OTG_MAX_EP0_SIZE;
127 ep->xfer_buff = 0;
128 ep->xfer_len = 0;
131 USB_OTG_DisableGlobalInt(pdev);
133 #if defined (STM32F446xx) || defined (STM32F469_479xx)
135 /* Force Device Mode*/
136 USB_OTG_SetCurrentMode(pdev, DEVICE_MODE);
138 /*Init the Core (common init.) */
139 USB_OTG_CoreInit(pdev);
141 #else
143 /*Init the Core (common init.) */
144 USB_OTG_CoreInit(pdev);
146 /* Force Device Mode*/
147 USB_OTG_SetCurrentMode(pdev, DEVICE_MODE);
149 #endif
151 /* Init Device */
152 USB_OTG_CoreInitDev(pdev);
154 /* Enable USB Global interrupt */
155 USB_OTG_EnableGlobalInt(pdev);
160 * @brief Configure an EP
161 * @param pdev : Device instance
162 * @param epdesc : Endpoint Descriptor
163 * @retval : status
165 uint32_t DCD_EP_Open(USB_OTG_CORE_HANDLE *pdev ,
166 uint8_t ep_addr,
167 uint16_t ep_mps,
168 uint8_t ep_type)
170 USB_OTG_EP *ep;
172 if ((ep_addr & 0x80) == 0x80)
174 ep = &pdev->dev.in_ep[ep_addr & 0x7F];
176 else
178 ep = &pdev->dev.out_ep[ep_addr & 0x7F];
180 ep->num = ep_addr & 0x7F;
182 ep->is_in = (0x80 & ep_addr) != 0;
183 ep->maxpacket = ep_mps;
184 ep->type = ep_type;
185 if (ep->is_in)
187 /* Assign a Tx FIFO */
188 ep->tx_fifo_num = ep->num;
190 /* Set initial data PID. */
191 if (ep_type == USB_OTG_EP_BULK )
193 ep->data_pid_start = 0;
195 USB_OTG_EPActivate(pdev , ep );
196 return 0;
199 * @brief called when an EP is disabled
200 * @param pdev: device instance
201 * @param ep_addr: endpoint address
202 * @retval : status
204 uint32_t DCD_EP_Close(USB_OTG_CORE_HANDLE *pdev , uint8_t ep_addr)
206 USB_OTG_EP *ep;
208 if ((ep_addr&0x80) == 0x80)
210 ep = &pdev->dev.in_ep[ep_addr & 0x7F];
212 else
214 ep = &pdev->dev.out_ep[ep_addr & 0x7F];
216 ep->num = ep_addr & 0x7F;
217 ep->is_in = (0x80 & ep_addr) != 0;
218 USB_OTG_EPDeactivate(pdev , ep );
219 return 0;
224 * @brief DCD_EP_PrepareRx
225 * @param pdev: device instance
226 * @param ep_addr: endpoint address
227 * @param pbuf: pointer to Rx buffer
228 * @param buf_len: data length
229 * @retval : status
231 uint32_t DCD_EP_PrepareRx( USB_OTG_CORE_HANDLE *pdev,
232 uint8_t ep_addr,
233 uint8_t *pbuf,
234 uint16_t buf_len)
236 USB_OTG_EP *ep;
238 ep = &pdev->dev.out_ep[ep_addr & 0x7F];
240 /*setup and start the Xfer */
241 ep->xfer_buff = pbuf;
242 ep->xfer_len = buf_len;
243 ep->xfer_count = 0;
244 ep->is_in = 0;
245 ep->num = ep_addr & 0x7F;
247 if (pdev->cfg.dma_enable == 1)
249 ep->dma_addr = (uint32_t)pbuf;
252 if ( ep->num == 0 )
254 USB_OTG_EP0StartXfer(pdev , ep);
256 else
258 USB_OTG_EPStartXfer(pdev, ep );
260 return 0;
264 * @brief Transmit data over USB
265 * @param pdev: device instance
266 * @param ep_addr: endpoint address
267 * @param pbuf: pointer to Tx buffer
268 * @param buf_len: data length
269 * @retval : status
271 uint32_t DCD_EP_Tx ( USB_OTG_CORE_HANDLE *pdev,
272 uint8_t ep_addr,
273 uint8_t *pbuf,
274 uint32_t buf_len)
276 USB_OTG_EP *ep;
278 ep = &pdev->dev.in_ep[ep_addr & 0x7F];
280 /* Setup and start the Transfer */
281 ep->is_in = 1;
282 ep->num = ep_addr & 0x7F;
283 ep->xfer_buff = pbuf;
284 ep->dma_addr = (uint32_t)pbuf;
285 ep->xfer_count = 0;
286 ep->xfer_len = buf_len;
288 if ( ep->num == 0 )
290 USB_OTG_EP0StartXfer(pdev , ep);
292 else
294 USB_OTG_EPStartXfer(pdev, ep );
296 return 0;
301 * @brief Stall an endpoint.
302 * @param pdev: device instance
303 * @param epnum: endpoint address
304 * @retval : status
306 uint32_t DCD_EP_Stall (USB_OTG_CORE_HANDLE *pdev, uint8_t epnum)
308 USB_OTG_EP *ep;
309 if ((0x80 & epnum) == 0x80)
311 ep = &pdev->dev.in_ep[epnum & 0x7F];
313 else
315 ep = &pdev->dev.out_ep[epnum];
318 ep->is_stall = 1;
319 ep->num = epnum & 0x7F;
320 ep->is_in = ((epnum & 0x80) == 0x80);
322 USB_OTG_EPSetStall(pdev , ep);
323 return (0);
328 * @brief Clear stall condition on endpoints.
329 * @param pdev: device instance
330 * @param epnum: endpoint address
331 * @retval : status
333 uint32_t DCD_EP_ClrStall (USB_OTG_CORE_HANDLE *pdev, uint8_t epnum)
335 USB_OTG_EP *ep;
336 if ((0x80 & epnum) == 0x80)
338 ep = &pdev->dev.in_ep[epnum & 0x7F];
340 else
342 ep = &pdev->dev.out_ep[epnum];
345 ep->is_stall = 0;
346 ep->num = epnum & 0x7F;
347 ep->is_in = ((epnum & 0x80) == 0x80);
349 USB_OTG_EPClearStall(pdev , ep);
350 return (0);
355 * @brief This Function flushes the FIFOs.
356 * @param pdev: device instance
357 * @param epnum: endpoint address
358 * @retval : status
360 uint32_t DCD_EP_Flush (USB_OTG_CORE_HANDLE *pdev , uint8_t epnum)
363 if ((epnum & 0x80) == 0x80)
365 USB_OTG_FlushTxFifo(pdev, epnum & 0x7F);
367 else
369 USB_OTG_FlushRxFifo(pdev);
372 return (0);
377 * @brief This Function set USB device address
378 * @param pdev: device instance
379 * @param address: new device address
380 * @retval : status
382 void DCD_EP_SetAddress (USB_OTG_CORE_HANDLE *pdev, uint8_t address)
384 USB_OTG_DCFG_TypeDef dcfg;
385 dcfg.d32 = 0;
386 dcfg.b.devaddr = address;
387 USB_OTG_MODIFY_REG32( &pdev->regs.DREGS->DCFG, 0, dcfg.d32);
391 * @brief Connect device (enable internal pull-up)
392 * @param pdev: device instance
393 * @retval : None
395 void DCD_DevConnect (USB_OTG_CORE_HANDLE *pdev)
397 #ifndef USE_OTG_MODE
398 USB_OTG_DCTL_TypeDef dctl;
399 dctl.d32 = USB_OTG_READ_REG32(&pdev->regs.DREGS->DCTL);
400 /* Connect device */
401 dctl.b.sftdiscon = 0;
402 USB_OTG_WRITE_REG32(&pdev->regs.DREGS->DCTL, dctl.d32);
403 USB_OTG_BSP_mDelay(3);
404 #endif
409 * @brief Disconnect device (disable internal pull-up)
410 * @param pdev: device instance
411 * @retval : None
413 void DCD_DevDisconnect (USB_OTG_CORE_HANDLE *pdev)
415 #ifndef USE_OTG_MODE
416 USB_OTG_DCTL_TypeDef dctl;
417 dctl.d32 = USB_OTG_READ_REG32(&pdev->regs.DREGS->DCTL);
418 /* Disconnect device for 3ms */
419 dctl.b.sftdiscon = 1;
420 USB_OTG_WRITE_REG32(&pdev->regs.DREGS->DCTL, dctl.d32);
421 USB_OTG_BSP_mDelay(3);
422 #endif
427 * @brief returns the EP Status
428 * @param pdev : Selected device
429 * epnum : endpoint address
430 * @retval : EP status
433 uint32_t DCD_GetEPStatus(USB_OTG_CORE_HANDLE *pdev ,uint8_t epnum)
435 USB_OTG_EP *ep;
436 uint32_t Status = 0;
438 if ((0x80 & epnum) == 0x80)
440 ep = &pdev->dev.in_ep[epnum & 0x7F];
442 else
444 ep = &pdev->dev.out_ep[epnum];
447 Status = USB_OTG_GetEPStatus(pdev ,ep);
449 /* Return the current status */
450 return Status;
454 * @brief Set the EP Status
455 * @param pdev : Selected device
456 * Status : new Status
457 * epnum : EP address
458 * @retval : None
460 void DCD_SetEPStatus (USB_OTG_CORE_HANDLE *pdev , uint8_t epnum , uint32_t Status)
462 USB_OTG_EP *ep;
464 if ((0x80 & epnum) == 0x80)
466 ep = &pdev->dev.in_ep[epnum & 0x7F];
468 else
470 ep = &pdev->dev.out_ep[epnum];
473 USB_OTG_SetEPStatus(pdev ,ep , Status);
477 * @}
481 * @}
485 * @}
488 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/