2 ******************************************************************************
4 * @author MCD Application Team
6 * @date 09-November-2015
7 * @brief Peripheral Device Interface Layer
8 ******************************************************************************
11 * <h2><center>© 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 ------------------------------------------------------------------*/
33 /** @addtogroup USB_OTG_DRIVER
38 * @brief This file is the interface between EFSL ans Host mass-storage class
43 /** @defgroup USB_DCD_Private_Defines
51 /** @defgroup USB_DCD_Private_TypesDefinitions
60 /** @defgroup USB_DCD_Private_Macros
68 /** @defgroup USB_DCD_Private_Variables
76 /** @defgroup USB_DCD_Private_FunctionPrototypes
85 /** @defgroup USB_DCD_Private_Functions
91 void DCD_Init(USB_OTG_CORE_HANDLE
*pdev
,
92 USB_OTG_CORE_ID_TypeDef coreID
)
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 */
110 /* Control until ep is activated */
111 ep
->type
= EP_TYPE_CTRL
;
112 ep
->maxpacket
= USB_OTG_MAX_EP0_SIZE
;
117 for (i
= 0; i
< pdev
->cfg
.dev_endpoints
; i
++)
119 ep
= &pdev
->dev
.out_ep
[i
];
120 /* Init ep structure */
124 /* Control until ep is activated */
125 ep
->type
= EP_TYPE_CTRL
;
126 ep
->maxpacket
= USB_OTG_MAX_EP0_SIZE
;
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
);
143 /*Init the Core (common init.) */
144 USB_OTG_CoreInit(pdev
);
146 /* Force Device Mode*/
147 USB_OTG_SetCurrentMode(pdev
, DEVICE_MODE
);
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
165 uint32_t DCD_EP_Open(USB_OTG_CORE_HANDLE
*pdev
,
172 if ((ep_addr
& 0x80) == 0x80)
174 ep
= &pdev
->dev
.in_ep
[ep_addr
& 0x7F];
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
;
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
);
199 * @brief called when an EP is disabled
200 * @param pdev: device instance
201 * @param ep_addr: endpoint address
204 uint32_t DCD_EP_Close(USB_OTG_CORE_HANDLE
*pdev
, uint8_t ep_addr
)
208 if ((ep_addr
&0x80) == 0x80)
210 ep
= &pdev
->dev
.in_ep
[ep_addr
& 0x7F];
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
);
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
231 uint32_t DCD_EP_PrepareRx( USB_OTG_CORE_HANDLE
*pdev
,
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
;
245 ep
->num
= ep_addr
& 0x7F;
247 if (pdev
->cfg
.dma_enable
== 1)
249 ep
->dma_addr
= (uint32_t)pbuf
;
254 USB_OTG_EP0StartXfer(pdev
, ep
);
258 USB_OTG_EPStartXfer(pdev
, ep
);
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
271 uint32_t DCD_EP_Tx ( USB_OTG_CORE_HANDLE
*pdev
,
278 ep
= &pdev
->dev
.in_ep
[ep_addr
& 0x7F];
280 /* Setup and start the Transfer */
282 ep
->num
= ep_addr
& 0x7F;
283 ep
->xfer_buff
= pbuf
;
284 ep
->dma_addr
= (uint32_t)pbuf
;
286 ep
->xfer_len
= buf_len
;
290 USB_OTG_EP0StartXfer(pdev
, ep
);
294 USB_OTG_EPStartXfer(pdev
, ep
);
301 * @brief Stall an endpoint.
302 * @param pdev: device instance
303 * @param epnum: endpoint address
306 uint32_t DCD_EP_Stall (USB_OTG_CORE_HANDLE
*pdev
, uint8_t epnum
)
309 if ((0x80 & epnum
) == 0x80)
311 ep
= &pdev
->dev
.in_ep
[epnum
& 0x7F];
315 ep
= &pdev
->dev
.out_ep
[epnum
];
319 ep
->num
= epnum
& 0x7F;
320 ep
->is_in
= ((epnum
& 0x80) == 0x80);
322 USB_OTG_EPSetStall(pdev
, ep
);
328 * @brief Clear stall condition on endpoints.
329 * @param pdev: device instance
330 * @param epnum: endpoint address
333 uint32_t DCD_EP_ClrStall (USB_OTG_CORE_HANDLE
*pdev
, uint8_t epnum
)
336 if ((0x80 & epnum
) == 0x80)
338 ep
= &pdev
->dev
.in_ep
[epnum
& 0x7F];
342 ep
= &pdev
->dev
.out_ep
[epnum
];
346 ep
->num
= epnum
& 0x7F;
347 ep
->is_in
= ((epnum
& 0x80) == 0x80);
349 USB_OTG_EPClearStall(pdev
, ep
);
355 * @brief This Function flushes the FIFOs.
356 * @param pdev: device instance
357 * @param epnum: endpoint address
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);
369 USB_OTG_FlushRxFifo(pdev
);
377 * @brief This Function set USB device address
378 * @param pdev: device instance
379 * @param address: new device address
382 void DCD_EP_SetAddress (USB_OTG_CORE_HANDLE
*pdev
, uint8_t address
)
384 USB_OTG_DCFG_TypeDef dcfg
;
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
395 void DCD_DevConnect (USB_OTG_CORE_HANDLE
*pdev
)
398 USB_OTG_DCTL_TypeDef dctl
;
399 dctl
.d32
= USB_OTG_READ_REG32(&pdev
->regs
.DREGS
->DCTL
);
401 dctl
.b
.sftdiscon
= 0;
402 USB_OTG_WRITE_REG32(&pdev
->regs
.DREGS
->DCTL
, dctl
.d32
);
403 USB_OTG_BSP_mDelay(3);
409 * @brief Disconnect device (disable internal pull-up)
410 * @param pdev: device instance
413 void DCD_DevDisconnect (USB_OTG_CORE_HANDLE
*pdev
)
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);
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
)
438 if ((0x80 & epnum
) == 0x80)
440 ep
= &pdev
->dev
.in_ep
[epnum
& 0x7F];
444 ep
= &pdev
->dev
.out_ep
[epnum
];
447 Status
= USB_OTG_GetEPStatus(pdev
,ep
);
449 /* Return the current status */
454 * @brief Set the EP Status
455 * @param pdev : Selected device
456 * Status : new Status
460 void DCD_SetEPStatus (USB_OTG_CORE_HANDLE
*pdev
, uint8_t epnum
, uint32_t Status
)
464 if ((0x80 & epnum
) == 0x80)
466 ep
= &pdev
->dev
.in_ep
[epnum
& 0x7F];
470 ep
= &pdev
->dev
.out_ep
[epnum
];
473 USB_OTG_SetEPStatus(pdev
,ep
, Status
);
488 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/