1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_pcd.c
4 * @author MCD Application Team
5 * @version V1.0.1
6 * @date 25-June-2015
7 * @brief PCD HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the USB Peripheral Controller:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral Control functions
13 * + Peripheral State functions
14 *
15 @verbatim
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
19 [..]
20 The PCD HAL driver can be used as follows:
21
22 (#) Declare a PCD_HandleTypeDef handle structure, for example:
23 PCD_HandleTypeDef hpcd;
24
25 (#) Fill parameters of Init structure in HCD handle
26
27 (#) Call HAL_PCD_Init() API to initialize the HCD peripheral (Core, Device core, ...)
28
29 (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
30 (##) Enable the PCD/USB Low Level interface clock using
31 (+++) __OTGFS-OTG_CLK_ENABLE()/__OTGHS-OTG_CLK_ENABLE();
32 (+++) __OTGHSULPI_CLK_ENABLE(); (For High Speed Mode)
33
34 (##) Initialize the related GPIO clocks
35 (##) Configure PCD pin-out
36 (##) Configure PCD NVIC interrupt
37
38 (#)Associate the Upper USB device stack to the HAL PCD Driver:
39 (##) hpcd.pData = pdev;
40
41 (#)Enable HCD transmission and reception:
42 (##) HAL_PCD_Start();
43
44 @endverbatim
45 ******************************************************************************
46 * @attention
47 *
48 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
49 *
50 * Redistribution and use in source and binary forms, with or without modification,
51 * are permitted provided that the following conditions are met:
52 * 1. Redistributions of source code must retain the above copyright notice,
53 * this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright notice,
55 * this list of conditions and the following disclaimer in the documentation
56 * and/or other materials provided with the distribution.
57 * 3. Neither the name of STMicroelectronics nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
62 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
64 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
67 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
68 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
69 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
70 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71 *
72 ******************************************************************************
73 */
74
75 /* Includes ------------------------------------------------------------------*/
76 #include "stm32f7xx_hal.h"
77
78 /** @addtogroup STM32F7xx_HAL_Driver
79 * @{
80 */
81
82 /** @defgroup PCD PCD
83 * @brief PCD HAL module driver
84 * @{
85 */
86
87 #ifdef HAL_PCD_MODULE_ENABLED
88
89 /* Private types -------------------------------------------------------------*/
90 /* Private variables ---------------------------------------------------------*/
91 /* Private constants ---------------------------------------------------------*/
92 /* Private macros ------------------------------------------------------------*/
93 /** @defgroup PCD_Private_Macros PCD Private Macros
94 * @{
95 */
96 #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
97 #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
98 /**
99 * @}
100 */
101
102 /* Private functions prototypes ----------------------------------------------*/
103 /** @defgroup PCD_Private_Functions PCD Private Functions
104 * @{
105 */
106 static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
107 /**
108 * @}
109 */
110
111 /* Exported functions --------------------------------------------------------*/
112 /** @defgroup PCD_Exported_Functions PCD Exported Functions
113 * @{
114 */
115
116 /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
117 * @brief Initialization and Configuration functions
118 *
119 @verbatim
120 ===============================================================================
121 ##### Initialization and de-initialization functions #####
122 ===============================================================================
123 [..] This section provides functions allowing to:
124
125 @endverbatim
126 * @{
127 */
128
129 /**
130 * @brief Initializes the PCD according to the specified
131 * parameters in the PCD_InitTypeDef and create the associated handle.
132 * @param hpcd: PCD handle
133 * @retval HAL status
134 */
HAL_PCD_Init(PCD_HandleTypeDef * hpcd)135 HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
136 {
137 uint32_t i = 0;
138
139 /* Check the PCD handle allocation */
140 if (hpcd == NULL) {
141 return HAL_ERROR;
142 }
143
144 /* Check the parameters */
145 assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
146
147 hpcd->State = HAL_PCD_STATE_BUSY;
148
149 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
150 HAL_PCD_MspInit(hpcd);
151
152 /* Disable the Interrupts */
153 __HAL_PCD_DISABLE(hpcd);
154
155 /*Init the Core (common init.) */
156 USB_CoreInit(hpcd->Instance, hpcd->Init);
157
158 /* Force Device Mode*/
159 USB_SetCurrentMode(hpcd->Instance , USB_OTG_DEVICE_MODE);
160
161 /* Init endpoints structures */
162 for (i = 0; i < 15 ; i++) {
163 /* Init ep structure */
164 hpcd->IN_ep[i].is_in = 1;
165 hpcd->IN_ep[i].num = i;
166 hpcd->IN_ep[i].tx_fifo_num = i;
167 /* Control until ep is activated */
168 hpcd->IN_ep[i].type = EP_TYPE_CTRL;
169 hpcd->IN_ep[i].maxpacket = 0;
170 hpcd->IN_ep[i].xfer_buff = 0;
171 hpcd->IN_ep[i].xfer_len = 0;
172 }
173
174 for (i = 0; i < 15 ; i++) {
175 hpcd->OUT_ep[i].is_in = 0;
176 hpcd->OUT_ep[i].num = i;
177 hpcd->IN_ep[i].tx_fifo_num = i;
178 /* Control until ep is activated */
179 hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
180 hpcd->OUT_ep[i].maxpacket = 0;
181 hpcd->OUT_ep[i].xfer_buff = 0;
182 hpcd->OUT_ep[i].xfer_len = 0;
183
184 hpcd->Instance->DIEPTXF[i] = 0;
185 }
186
187 /* Init Device */
188 USB_DevInit(hpcd->Instance, hpcd->Init);
189
190 hpcd->State= HAL_PCD_STATE_READY;
191
192 /* Activate LPM */
193 if (hpcd->Init.lpm_enable == 1) {
194 HAL_PCDEx_ActivateLPM(hpcd);
195 }
196
197 USB_DevDisconnect (hpcd->Instance);
198 return HAL_OK;
199 }
200
201 /**
202 * @brief DeInitializes the PCD peripheral
203 * @param hpcd: PCD handle
204 * @retval HAL status
205 */
HAL_PCD_DeInit(PCD_HandleTypeDef * hpcd)206 HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
207 {
208 /* Check the PCD handle allocation */
209 if (hpcd == NULL) {
210 return HAL_ERROR;
211 }
212
213 hpcd->State = HAL_PCD_STATE_BUSY;
214
215 /* Stop Device */
216 HAL_PCD_Stop(hpcd);
217
218 /* DeInit the low level hardware */
219 HAL_PCD_MspDeInit(hpcd);
220
221 hpcd->State = HAL_PCD_STATE_RESET;
222
223 return HAL_OK;
224 }
225
226 /**
227 * @brief Initializes the PCD MSP.
228 * @param hpcd: PCD handle
229 * @retval None
230 */
HAL_PCD_MspInit(PCD_HandleTypeDef * hpcd)231 __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
232 {
233 /* NOTE : This function Should not be modified, when the callback is needed,
234 the HAL_PCD_MspInit could be implemented in the user file
235 */
236 }
237
238 /**
239 * @brief DeInitializes PCD MSP.
240 * @param hpcd: PCD handle
241 * @retval None
242 */
HAL_PCD_MspDeInit(PCD_HandleTypeDef * hpcd)243 __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
244 {
245 /* NOTE : This function Should not be modified, when the callback is needed,
246 the HAL_PCD_MspDeInit could be implemented in the user file
247 */
248 }
249
250 /**
251 * @}
252 */
253
254 /** @defgroup PCD_Exported_Functions_Group2 IO operation functions
255 * @brief Data transfers functions
256 *
257 @verbatim
258 ===============================================================================
259 ##### IO operation functions #####
260 ===============================================================================
261 [..]
262 This subsection provides a set of functions allowing to manage the PCD data
263 transfers.
264
265 @endverbatim
266 * @{
267 */
268
269 /**
270 * @brief Start The USB OTG Device.
271 * @param hpcd: PCD handle
272 * @retval HAL status
273 */
HAL_PCD_Start(PCD_HandleTypeDef * hpcd)274 HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
275 {
276 __HAL_LOCK(hpcd);
277 USB_DevConnect (hpcd->Instance);
278 __HAL_PCD_ENABLE(hpcd);
279 __HAL_UNLOCK(hpcd);
280 return HAL_OK;
281 }
282
283 /**
284 * @brief Stop The USB OTG Device.
285 * @param hpcd: PCD handle
286 * @retval HAL status
287 */
HAL_PCD_Stop(PCD_HandleTypeDef * hpcd)288 HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
289 {
290 __HAL_LOCK(hpcd);
291 __HAL_PCD_DISABLE(hpcd);
292 USB_StopDevice(hpcd->Instance);
293 USB_DevDisconnect (hpcd->Instance);
294 __HAL_UNLOCK(hpcd);
295 return HAL_OK;
296 }
297
298 /**
299 * @brief This function handles PCD interrupt request.
300 * @param hpcd: PCD handle
301 * @retval HAL status
302 */
HAL_PCD_IRQHandler(PCD_HandleTypeDef * hpcd)303 void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
304 {
305 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
306 uint32_t i = 0, ep_intr = 0, epint = 0, epnum = 0;
307 uint32_t fifoemptymsk = 0, temp = 0;
308 USB_OTG_EPTypeDef *ep;
309
310 /* ensure that we are in device mode */
311 if (USB_GetMode(hpcd->Instance) == USB_OTG_MODE_DEVICE) {
312 /* avoid spurious interrupt */
313 if (__HAL_PCD_IS_INVALID_INTERRUPT(hpcd)) {
314 return;
315 }
316
317 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_MMIS)) {
318 /* incorrect mode, acknowledge the interrupt */
319 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_MMIS);
320 }
321
322 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OEPINT)) {
323 epnum = 0;
324
325 /* Read in the device interrupt bits */
326 ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
327
328 while ( ep_intr ) {
329 if (ep_intr & 0x1) {
330 epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, epnum);
331
332 if (( epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC) {
333 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
334
335 if (hpcd->Init.dma_enable == 1) {
336 hpcd->OUT_ep[epnum].xfer_count = hpcd->OUT_ep[epnum].maxpacket- (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
337 hpcd->OUT_ep[epnum].xfer_buff += hpcd->OUT_ep[epnum].maxpacket;
338 }
339
340 HAL_PCD_DataOutStageCallback(hpcd, epnum);
341 if (hpcd->Init.dma_enable == 1) {
342 if ((epnum == 0) && (hpcd->OUT_ep[epnum].xfer_len == 0)) {
343 /* this is ZLP, so prepare EP0 for next setup */
344 USB_EP0_OutStart(hpcd->Instance, 1, (uint8_t *)hpcd->Setup);
345 }
346 }
347 }
348
349 if (( epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP) {
350 /* Inform the upper layer that a setup packet is available */
351 HAL_PCD_SetupStageCallback(hpcd);
352 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
353 }
354
355 if (( epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS) {
356 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
357 }
358 }
359 epnum++;
360 ep_intr >>= 1;
361 }
362 }
363
364 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT)) {
365 /* Read in the device interrupt bits */
366 ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
367
368 epnum = 0;
369
370 while ( ep_intr ) {
371 if (ep_intr & 0x1) { /* In ITR */
372 epint = USB_ReadDevInEPInterrupt(hpcd->Instance, epnum);
373
374 if (( epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC) {
375 fifoemptymsk = 0x1 << epnum;
376 USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
377
378 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
379
380 if (hpcd->Init.dma_enable == 1) {
381 hpcd->IN_ep[epnum].xfer_buff += hpcd->IN_ep[epnum].maxpacket;
382 }
383
384 HAL_PCD_DataInStageCallback(hpcd, epnum);
385
386 if (hpcd->Init.dma_enable == 1) {
387 /* this is ZLP, so prepare EP0 for next setup */
388 if ((epnum == 0) && (hpcd->IN_ep[epnum].xfer_len == 0)) {
389 /* prepare to rx more setup packets */
390 USB_EP0_OutStart(hpcd->Instance, 1, (uint8_t *)hpcd->Setup);
391 }
392 }
393 }
394 if (( epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC) {
395 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
396 }
397 if (( epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE) {
398 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
399 }
400 if (( epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE) {
401 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
402 }
403 if (( epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD) {
404 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
405 }
406 if (( epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE) {
407 PCD_WriteEmptyTxFifo(hpcd , epnum);
408 }
409 }
410 epnum++;
411 ep_intr >>= 1;
412 }
413 }
414
415 /* Handle Resume Interrupt */
416 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT)) {
417 /* Clear the Remote Wake-up Signaling */
418 USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
419
420 if (hpcd->LPM_State == LPM_L1) {
421 hpcd->LPM_State = LPM_L0;
422 HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
423 } else {
424 HAL_PCD_ResumeCallback(hpcd);
425 }
426 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
427 }
428
429 /* Handle Suspend Interrupt */
430 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP)) {
431
432 if ((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS) {
433
434 HAL_PCD_SuspendCallback(hpcd);
435 }
436 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
437 }
438
439 /* Handle LPM Interrupt */
440 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT)) {
441 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT);
442 if ( hpcd->LPM_State == LPM_L0) {
443 hpcd->LPM_State = LPM_L1;
444 hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >>2 ;
445 HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
446 } else {
447 HAL_PCD_SuspendCallback(hpcd);
448 }
449 }
450
451 /* Handle Reset Interrupt */
452 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST)) {
453 USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
454 USB_FlushTxFifo(hpcd->Instance , 0 );
455
456 for (i = 0; i < hpcd->Init.dev_endpoints ; i++) {
457 USBx_INEP(i)->DIEPINT = 0xFF;
458 USBx_OUTEP(i)->DOEPINT = 0xFF;
459 }
460 USBx_DEVICE->DAINT = 0xFFFFFFFF;
461 USBx_DEVICE->DAINTMSK |= 0x10001;
462
463 if (hpcd->Init.use_dedicated_ep1) {
464 USBx_DEVICE->DOUTEP1MSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
465 USBx_DEVICE->DINEP1MSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
466 } else {
467 USBx_DEVICE->DOEPMSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
468 USBx_DEVICE->DIEPMSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
469 }
470
471 /* Set Default Address to 0 */
472 USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
473
474 /* setup EP0 to receive SETUP packets */
475 USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
476
477 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
478 }
479
480 /* Handle Enumeration done Interrupt */
481 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE)) {
482 USB_ActivateSetup(hpcd->Instance);
483 hpcd->Instance->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT;
484
485 if ( USB_GetDevSpeed(hpcd->Instance) == USB_OTG_SPEED_HIGH) {
486 hpcd->Init.speed = USB_OTG_SPEED_HIGH;
487 hpcd->Init.ep0_mps = USB_OTG_HS_MAX_PACKET_SIZE ;
488 hpcd->Instance->GUSBCFG |= (uint32_t)((USBD_HS_TRDT_VALUE << 10) & USB_OTG_GUSBCFG_TRDT);
489 } else {
490 hpcd->Init.speed = USB_OTG_SPEED_FULL;
491 hpcd->Init.ep0_mps = USB_OTG_FS_MAX_PACKET_SIZE ;
492 hpcd->Instance->GUSBCFG |= (uint32_t)((USBD_FS_TRDT_VALUE << 10) & USB_OTG_GUSBCFG_TRDT);
493 }
494
495 HAL_PCD_ResetCallback(hpcd);
496
497 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE);
498 }
499
500 /* Handle RxQLevel Interrupt */
501 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL)) {
502 USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
503 temp = USBx->GRXSTSP;
504 ep = &hpcd->OUT_ep[temp & USB_OTG_GRXSTSP_EPNUM];
505
506 if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT) {
507 if ((temp & USB_OTG_GRXSTSP_BCNT) != 0) {
508 USB_ReadPacket(USBx, ep->xfer_buff, (temp & USB_OTG_GRXSTSP_BCNT) >> 4);
509 ep->xfer_buff += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
510 ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
511 }
512 } else if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT) {
513 USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8);
514 ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
515 }
516 USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
517 }
518
519 /* Handle SOF Interrupt */
520 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SOF)) {
521 HAL_PCD_SOFCallback(hpcd);
522 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SOF);
523 }
524
525 /* Handle Incomplete ISO IN Interrupt */
526 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR)) {
527 HAL_PCD_ISOINIncompleteCallback(hpcd, epnum);
528 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR);
529 }
530
531 /* Handle Incomplete ISO OUT Interrupt */
532 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT)) {
533 HAL_PCD_ISOOUTIncompleteCallback(hpcd, epnum);
534 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
535 }
536
537 /* Handle Connection event Interrupt */
538 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT)) {
539 HAL_PCD_ConnectCallback(hpcd);
540 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT);
541 }
542
543 /* Handle Disconnection event Interrupt */
544 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OTGINT)) {
545 temp = hpcd->Instance->GOTGINT;
546
547 if ((temp & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET) {
548 HAL_PCD_DisconnectCallback(hpcd);
549 }
550 hpcd->Instance->GOTGINT |= temp;
551 }
552 }
553 }
554
555 /**
556 * @brief Data out stage callbacks
557 * @param hpcd: PCD handle
558 * @param epnum: endpoint number
559 * @retval None
560 */
HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef * hpcd,uint8_t epnum)561 __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
562 {
563 /* NOTE : This function Should not be modified, when the callback is needed,
564 the HAL_PCD_DataOutStageCallback could be implemented in the user file
565 */
566 }
567
568 /**
569 * @brief Data IN stage callbacks
570 * @param hpcd: PCD handle
571 * @param epnum: endpoint number
572 * @retval None
573 */
HAL_PCD_DataInStageCallback(PCD_HandleTypeDef * hpcd,uint8_t epnum)574 __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
575 {
576 /* NOTE : This function Should not be modified, when the callback is needed,
577 the HAL_PCD_DataInStageCallback could be implemented in the user file
578 */
579 }
580 /**
581 * @brief Setup stage callback
582 * @param hpcd: PCD handle
583 * @retval None
584 */
HAL_PCD_SetupStageCallback(PCD_HandleTypeDef * hpcd)585 __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
586 {
587 /* NOTE : This function Should not be modified, when the callback is needed,
588 the HAL_PCD_SetupStageCallback could be implemented in the user file
589 */
590 }
591
592 /**
593 * @brief USB Start Of Frame callbacks
594 * @param hpcd: PCD handle
595 * @retval None
596 */
HAL_PCD_SOFCallback(PCD_HandleTypeDef * hpcd)597 __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
598 {
599 /* NOTE : This function Should not be modified, when the callback is needed,
600 the HAL_PCD_SOFCallback could be implemented in the user file
601 */
602 }
603
604 /**
605 * @brief USB Reset callbacks
606 * @param hpcd: PCD handle
607 * @retval None
608 */
HAL_PCD_ResetCallback(PCD_HandleTypeDef * hpcd)609 __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
610 {
611 /* NOTE : This function Should not be modified, when the callback is needed,
612 the HAL_PCD_ResetCallback could be implemented in the user file
613 */
614 }
615
616
617 /**
618 * @brief Suspend event callbacks
619 * @param hpcd: PCD handle
620 * @retval None
621 */
HAL_PCD_SuspendCallback(PCD_HandleTypeDef * hpcd)622 __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
623 {
624 /* NOTE : This function Should not be modified, when the callback is needed,
625 the HAL_PCD_SuspendCallback could be implemented in the user file
626 */
627 }
628
629 /**
630 * @brief Resume event callbacks
631 * @param hpcd: PCD handle
632 * @retval None
633 */
HAL_PCD_ResumeCallback(PCD_HandleTypeDef * hpcd)634 __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
635 {
636 /* NOTE : This function Should not be modified, when the callback is needed,
637 the HAL_PCD_ResumeCallback could be implemented in the user file
638 */
639 }
640
641 /**
642 * @brief Incomplete ISO OUT callbacks
643 * @param hpcd: PCD handle
644 * @param epnum: endpoint number
645 * @retval None
646 */
HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef * hpcd,uint8_t epnum)647 __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
648 {
649 /* NOTE : This function Should not be modified, when the callback is needed,
650 the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
651 */
652 }
653
654 /**
655 * @brief Incomplete ISO IN callbacks
656 * @param hpcd: PCD handle
657 * @param epnum: endpoint number
658 * @retval None
659 */
HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef * hpcd,uint8_t epnum)660 __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
661 {
662 /* NOTE : This function Should not be modified, when the callback is needed,
663 the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
664 */
665 }
666
667 /**
668 * @brief Connection event callbacks
669 * @param hpcd: PCD handle
670 * @retval None
671 */
HAL_PCD_ConnectCallback(PCD_HandleTypeDef * hpcd)672 __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
673 {
674 /* NOTE : This function Should not be modified, when the callback is needed,
675 the HAL_PCD_ConnectCallback could be implemented in the user file
676 */
677 }
678
679 /**
680 * @brief Disconnection event callbacks
681 * @param hpcd: PCD handle
682 * @retval None
683 */
HAL_PCD_DisconnectCallback(PCD_HandleTypeDef * hpcd)684 __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
685 {
686 /* NOTE : This function Should not be modified, when the callback is needed,
687 the HAL_PCD_DisconnectCallback could be implemented in the user file
688 */
689 }
690
691 /**
692 * @}
693 */
694
695 /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
696 * @brief management functions
697 *
698 @verbatim
699 ===============================================================================
700 ##### Peripheral Control functions #####
701 ===============================================================================
702 [..]
703 This subsection provides a set of functions allowing to control the PCD data
704 transfers.
705
706 @endverbatim
707 * @{
708 */
709
710 /**
711 * @brief Connect the USB device
712 * @param hpcd: PCD handle
713 * @retval HAL status
714 */
HAL_PCD_DevConnect(PCD_HandleTypeDef * hpcd)715 HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
716 {
717 __HAL_LOCK(hpcd);
718 USB_DevConnect(hpcd->Instance);
719 __HAL_UNLOCK(hpcd);
720 return HAL_OK;
721 }
722
723 /**
724 * @brief Disconnect the USB device
725 * @param hpcd: PCD handle
726 * @retval HAL status
727 */
HAL_PCD_DevDisconnect(PCD_HandleTypeDef * hpcd)728 HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
729 {
730 __HAL_LOCK(hpcd);
731 USB_DevDisconnect(hpcd->Instance);
732 __HAL_UNLOCK(hpcd);
733 return HAL_OK;
734 }
735
736 /**
737 * @brief Set the USB Device address
738 * @param hpcd: PCD handle
739 * @param address: new device address
740 * @retval HAL status
741 */
HAL_PCD_SetAddress(PCD_HandleTypeDef * hpcd,uint8_t address)742 HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
743 {
744 __HAL_LOCK(hpcd);
745 USB_SetDevAddress(hpcd->Instance, address);
746 __HAL_UNLOCK(hpcd);
747 return HAL_OK;
748 }
749 /**
750 * @brief Open and configure an endpoint
751 * @param hpcd: PCD handle
752 * @param ep_addr: endpoint address
753 * @param ep_mps: endpoint max packet size
754 * @param ep_type: endpoint type
755 * @retval HAL status
756 */
HAL_PCD_EP_Open(PCD_HandleTypeDef * hpcd,uint8_t ep_addr,uint16_t ep_mps,uint8_t ep_type)757 HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type)
758 {
759 HAL_StatusTypeDef ret = HAL_OK;
760 USB_OTG_EPTypeDef *ep;
761
762 if ((ep_addr & 0x80) == 0x80) {
763 ep = &hpcd->IN_ep[ep_addr & 0x7F];
764 } else {
765 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
766 }
767 ep->num = ep_addr & 0x7F;
768
769 ep->is_in = (0x80 & ep_addr) != 0;
770 ep->maxpacket = ep_mps;
771 ep->type = ep_type;
772 if (ep->is_in) {
773 /* Assign a Tx FIFO */
774 ep->tx_fifo_num = ep->num;
775 }
776 /* Set initial data PID. */
777 if (ep_type == EP_TYPE_BULK ) {
778 ep->data_pid_start = 0;
779 }
780
781 __HAL_LOCK(hpcd);
782 USB_ActivateEndpoint(hpcd->Instance , ep);
783 __HAL_UNLOCK(hpcd);
784 return ret;
785 }
786
787
788 /**
789 * @brief Deactivate an endpoint
790 * @param hpcd: PCD handle
791 * @param ep_addr: endpoint address
792 * @retval HAL status
793 */
HAL_PCD_EP_Close(PCD_HandleTypeDef * hpcd,uint8_t ep_addr)794 HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
795 {
796 USB_OTG_EPTypeDef *ep;
797
798 if ((ep_addr & 0x80) == 0x80) {
799 ep = &hpcd->IN_ep[ep_addr & 0x7F];
800 } else {
801 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
802 }
803 ep->num = ep_addr & 0x7F;
804
805 ep->is_in = (0x80 & ep_addr) != 0;
806
807 __HAL_LOCK(hpcd);
808 USB_DeactivateEndpoint(hpcd->Instance , ep);
809 __HAL_UNLOCK(hpcd);
810 return HAL_OK;
811 }
812
813
814 /**
815 * @brief Receive an amount of data
816 * @param hpcd: PCD handle
817 * @param ep_addr: endpoint address
818 * @param pBuf: pointer to the reception buffer
819 * @param len: amount of data to be received
820 * @retval HAL status
821 */
HAL_PCD_EP_Receive(PCD_HandleTypeDef * hpcd,uint8_t ep_addr,uint8_t * pBuf,uint32_t len)822 HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
823 {
824 USB_OTG_EPTypeDef *ep;
825
826 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
827
828 /*setup and start the Xfer */
829 ep->xfer_buff = pBuf;
830 ep->xfer_len = len;
831 ep->xfer_count = 0;
832 ep->is_in = 0;
833 ep->num = ep_addr & 0x7F;
834
835 if (hpcd->Init.dma_enable == 1) {
836 ep->dma_addr = (uint32_t)pBuf;
837 }
838
839 __HAL_LOCK(hpcd);
840
841 if ((ep_addr & 0x7F) == 0 ) {
842 USB_EP0StartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
843 } else {
844 USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
845 }
846 __HAL_UNLOCK(hpcd);
847
848 return HAL_OK;
849 }
850
851 /**
852 * @brief Get Received Data Size
853 * @param hpcd: PCD handle
854 * @param ep_addr: endpoint address
855 * @retval Data Size
856 */
HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef * hpcd,uint8_t ep_addr)857 uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
858 {
859 return hpcd->OUT_ep[ep_addr & 0x7F].xfer_count;
860 }
861 /**
862 * @brief Send an amount of data
863 * @param hpcd: PCD handle
864 * @param ep_addr: endpoint address
865 * @param pBuf: pointer to the transmission buffer
866 * @param len: amount of data to be sent
867 * @retval HAL status
868 */
HAL_PCD_EP_Transmit(PCD_HandleTypeDef * hpcd,uint8_t ep_addr,uint8_t * pBuf,uint32_t len)869 HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
870 {
871 USB_OTG_EPTypeDef *ep;
872
873 ep = &hpcd->IN_ep[ep_addr & 0x7F];
874
875 /*setup and start the Xfer */
876 ep->xfer_buff = pBuf;
877 ep->xfer_len = len;
878 ep->xfer_count = 0;
879 ep->is_in = 1;
880 ep->num = ep_addr & 0x7F;
881
882 if (hpcd->Init.dma_enable == 1) {
883 ep->dma_addr = (uint32_t)pBuf;
884 }
885
886 __HAL_LOCK(hpcd);
887
888 if ((ep_addr & 0x7F) == 0 ) {
889 USB_EP0StartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
890 } else {
891 USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
892 }
893
894 __HAL_UNLOCK(hpcd);
895
896 return HAL_OK;
897 }
898
899 /**
900 * @brief Set a STALL condition over an endpoint
901 * @param hpcd: PCD handle
902 * @param ep_addr: endpoint address
903 * @retval HAL status
904 */
HAL_PCD_EP_SetStall(PCD_HandleTypeDef * hpcd,uint8_t ep_addr)905 HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
906 {
907 USB_OTG_EPTypeDef *ep;
908
909 if ((0x80 & ep_addr) == 0x80) {
910 ep = &hpcd->IN_ep[ep_addr & 0x7F];
911 } else {
912 ep = &hpcd->OUT_ep[ep_addr];
913 }
914
915 ep->is_stall = 1;
916 ep->num = ep_addr & 0x7F;
917 ep->is_in = ((ep_addr & 0x80) == 0x80);
918
919
920 __HAL_LOCK(hpcd);
921 USB_EPSetStall(hpcd->Instance , ep);
922 if ((ep_addr & 0x7F) == 0) {
923 USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
924 }
925 __HAL_UNLOCK(hpcd);
926
927 return HAL_OK;
928 }
929
930 /**
931 * @brief Clear a STALL condition over in an endpoint
932 * @param hpcd: PCD handle
933 * @param ep_addr: endpoint address
934 * @retval HAL status
935 */
HAL_PCD_EP_ClrStall(PCD_HandleTypeDef * hpcd,uint8_t ep_addr)936 HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
937 {
938 USB_OTG_EPTypeDef *ep;
939
940 if ((0x80 & ep_addr) == 0x80) {
941 ep = &hpcd->IN_ep[ep_addr & 0x7F];
942 } else {
943 ep = &hpcd->OUT_ep[ep_addr];
944 }
945
946 ep->is_stall = 0;
947 ep->num = ep_addr & 0x7F;
948 ep->is_in = ((ep_addr & 0x80) == 0x80);
949
950 __HAL_LOCK(hpcd);
951 USB_EPClearStall(hpcd->Instance , ep);
952 __HAL_UNLOCK(hpcd);
953
954 return HAL_OK;
955 }
956
957 /**
958 * @brief Flush an endpoint
959 * @param hpcd: PCD handle
960 * @param ep_addr: endpoint address
961 * @retval HAL status
962 */
HAL_PCD_EP_Flush(PCD_HandleTypeDef * hpcd,uint8_t ep_addr)963 HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
964 {
965 __HAL_LOCK(hpcd);
966
967 if ((ep_addr & 0x80) == 0x80) {
968 USB_FlushTxFifo(hpcd->Instance, ep_addr & 0x7F);
969 } else {
970 USB_FlushRxFifo(hpcd->Instance);
971 }
972
973 __HAL_UNLOCK(hpcd);
974
975 return HAL_OK;
976 }
977
978 /**
979 * @brief HAL_PCD_ActivateRemoteWakeup : Active remote wake-up signalling
980 * @param hpcd: PCD handle
981 * @retval HAL status
982 */
HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef * hpcd)983 HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
984 {
985 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
986
987 if ((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS) {
988 /* Activate Remote wake-up signaling */
989 USBx_DEVICE->DCTL |= USB_OTG_DCTL_RWUSIG;
990 }
991 return HAL_OK;
992 }
993
994 /**
995 * @brief HAL_PCD_DeActivateRemoteWakeup : de-active remote wake-up signalling
996 * @param hpcd: PCD handle
997 * @retval HAL status
998 */
HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef * hpcd)999 HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
1000 {
1001 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1002
1003 /* De-activate Remote wake-up signaling */
1004 USBx_DEVICE->DCTL &= ~(USB_OTG_DCTL_RWUSIG);
1005 return HAL_OK;
1006 }
1007 /**
1008 * @}
1009 */
1010
1011 /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
1012 * @brief Peripheral State functions
1013 *
1014 @verbatim
1015 ===============================================================================
1016 ##### Peripheral State functions #####
1017 ===============================================================================
1018 [..]
1019 This subsection permits to get in run-time the status of the peripheral
1020 and the data flow.
1021
1022 @endverbatim
1023 * @{
1024 */
1025
1026 /**
1027 * @brief Return the PCD state
1028 * @param hpcd: PCD handle
1029 * @retval HAL state
1030 */
HAL_PCD_GetState(PCD_HandleTypeDef * hpcd)1031 PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
1032 {
1033 return hpcd->State;
1034 }
1035 /**
1036 * @}
1037 */
1038
1039 /**
1040 * @}
1041 */
1042
1043 /* Private functions ---------------------------------------------------------*/
1044 /** @addtogroup PCD_Private_Functions
1045 * @{
1046 */
1047
1048 /**
1049 * @brief DCD_WriteEmptyTxFifo
1050 * check FIFO for the next packet to be loaded
1051 * @param hpcd: PCD handle
1052 * @param epnum : endpoint number
1053 * @retval HAL status
1054 */
PCD_WriteEmptyTxFifo(PCD_HandleTypeDef * hpcd,uint32_t epnum)1055 static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
1056 {
1057 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1058 USB_OTG_EPTypeDef *ep;
1059 int32_t len = 0;
1060 uint32_t len32b;
1061 uint32_t fifoemptymsk = 0;
1062
1063 ep = &hpcd->IN_ep[epnum];
1064 len = ep->xfer_len - ep->xfer_count;
1065
1066 if (len > (int32_t)ep->maxpacket) {
1067 len = ep->maxpacket;
1068 }
1069
1070
1071 len32b = (len + 3) / 4;
1072
1073 while ( (USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) > len32b &&
1074 ep->xfer_count < ep->xfer_len &&
1075 ep->xfer_len != 0) {
1076 /* Write the FIFO */
1077 len = ep->xfer_len - ep->xfer_count;
1078
1079 if (len > (int32_t)ep->maxpacket) {
1080 len = ep->maxpacket;
1081 }
1082 len32b = (len + 3) / 4;
1083
1084 USB_WritePacket(USBx, ep->xfer_buff, epnum, len, hpcd->Init.dma_enable);
1085
1086 ep->xfer_buff += len;
1087 ep->xfer_count += len;
1088 }
1089
1090 if (len <= 0) {
1091 fifoemptymsk = 0x1 << epnum;
1092 USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
1093
1094 }
1095
1096 return HAL_OK;
1097 }
1098
1099 /**
1100 * @}
1101 */
1102
1103 #endif /* HAL_PCD_MODULE_ENABLED */
1104 /**
1105 * @}
1106 */
1107
1108 /**
1109 * @}
1110 */
1111
1112 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
1113