STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
stm32h7xx_hal_pcd.c
Go to the documentation of this file.
1/**
2 ******************************************************************************
3 * @file stm32h7xx_hal_pcd.c
4 * @author MCD Application Team
5 * @brief PCD HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the USB Peripheral Controller:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 * + Peripheral Control functions
11 * + Peripheral State functions
12 *
13 ******************************************************************************
14 * @attention
15 *
16 * Copyright (c) 2017 STMicroelectronics.
17 * All rights reserved.
18 *
19 * This software is licensed under terms that can be found in the LICENSE file
20 * in the root directory of this software component.
21 * If no LICENSE file comes with this software, it is provided AS-IS.
22 *
23 ******************************************************************************
24 @verbatim
25 ==============================================================================
26 ##### How to use this driver #####
27 ==============================================================================
28 [..]
29 The PCD HAL driver can be used as follows:
30
31 (#) Declare a PCD_HandleTypeDef handle structure, for example:
32 PCD_HandleTypeDef hpcd;
33
34 (#) Fill parameters of Init structure in HCD handle
35
36 (#) Call HAL_PCD_Init() API to initialize the PCD peripheral (Core, Device core, ...)
37
38 (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
39 (##) Enable the PCD/USB Low Level interface clock using
40 (+++) __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
41 (+++) __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); (For High Speed Mode)
42
43 (##) Initialize the related GPIO clocks
44 (##) Configure PCD pin-out
45 (##) Configure PCD NVIC interrupt
46
47 (#)Associate the Upper USB device stack to the HAL PCD Driver:
48 (##) hpcd.pData = pdev;
49
50 (#)Enable PCD transmission and reception:
51 (##) HAL_PCD_Start();
52
53 @endverbatim
54 ******************************************************************************
55 */
56
57/* Includes ------------------------------------------------------------------*/
58#include "stm32h7xx_hal.h"
59
60/** @addtogroup STM32H7xx_HAL_Driver
61 * @{
62 */
63
64/** @defgroup PCD PCD
65 * @brief PCD HAL module driver
66 * @{
67 */
68
69#ifdef HAL_PCD_MODULE_ENABLED
70
71#if defined (USB_OTG_FS) || defined (USB_OTG_HS)
72
73/* Private types -------------------------------------------------------------*/
74/* Private variables ---------------------------------------------------------*/
75/* Private constants ---------------------------------------------------------*/
76/* Private macros ------------------------------------------------------------*/
77/** @defgroup PCD_Private_Macros PCD Private Macros
78 * @{
79 */
80#define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
81#define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
82/**
83 * @}
84 */
85
86/* Private functions prototypes ----------------------------------------------*/
87/** @defgroup PCD_Private_Functions PCD Private Functions
88 * @{
89 */
90#if defined (USB_OTG_FS) || defined (USB_OTG_HS)
91static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
92static HAL_StatusTypeDef PCD_EP_OutXfrComplete_int(PCD_HandleTypeDef *hpcd, uint32_t epnum);
93static HAL_StatusTypeDef PCD_EP_OutSetupPacket_int(PCD_HandleTypeDef *hpcd, uint32_t epnum);
94#endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
95/**
96 * @}
97 */
98
99/* Exported functions --------------------------------------------------------*/
100/** @defgroup PCD_Exported_Functions PCD Exported Functions
101 * @{
102 */
103
104/** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
105 * @brief Initialization and Configuration functions
106 *
107@verbatim
108 ===============================================================================
109 ##### Initialization and de-initialization functions #####
110 ===============================================================================
111 [..] This section provides functions allowing to:
112
113@endverbatim
114 * @{
115 */
116
117/**
118 * @brief Initializes the PCD according to the specified
119 * parameters in the PCD_InitTypeDef and initialize the associated handle.
120 * @param hpcd PCD handle
121 * @retval HAL status
122 */
123HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
124{
125 uint8_t i;
126
127 /* Check the PCD handle allocation */
128 if (hpcd == NULL)
129 {
130 return HAL_ERROR;
131 }
132
133 /* Check the parameters */
134 assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
135
136 if (hpcd->State == HAL_PCD_STATE_RESET)
137 {
138 /* Allocate lock resource and initialize it */
139 hpcd->Lock = HAL_UNLOCKED;
140
141#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
142 hpcd->SOFCallback = HAL_PCD_SOFCallback;
143 hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
144 hpcd->ResetCallback = HAL_PCD_ResetCallback;
145 hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
146 hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
147 hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
148 hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
149 hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback;
150 hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback;
151 hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback;
152 hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback;
153 hpcd->LPMCallback = HAL_PCDEx_LPM_Callback;
154 hpcd->BCDCallback = HAL_PCDEx_BCD_Callback;
155
156 if (hpcd->MspInitCallback == NULL)
157 {
158 hpcd->MspInitCallback = HAL_PCD_MspInit;
159 }
160
161 /* Init the low level hardware */
162 hpcd->MspInitCallback(hpcd);
163#else
164 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
165 HAL_PCD_MspInit(hpcd);
166#endif /* (USE_HAL_PCD_REGISTER_CALLBACKS) */
167 }
168
169 hpcd->State = HAL_PCD_STATE_BUSY;
170
171 /* Disable the Interrupts */
172 __HAL_PCD_DISABLE(hpcd);
173
174 /*Init the Core (common init.) */
175 if (USB_CoreInit(hpcd->Instance, hpcd->Init) != HAL_OK)
176 {
177 hpcd->State = HAL_PCD_STATE_ERROR;
178 return HAL_ERROR;
179 }
180
181 /* Force Device Mode */
182 if (USB_SetCurrentMode(hpcd->Instance, USB_DEVICE_MODE) != HAL_OK)
183 {
184 hpcd->State = HAL_PCD_STATE_ERROR;
185 return HAL_ERROR;
186 }
187
188 /* Init endpoints structures */
189 for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
190 {
191 /* Init ep structure */
192 hpcd->IN_ep[i].is_in = 1U;
193 hpcd->IN_ep[i].num = i;
194 hpcd->IN_ep[i].tx_fifo_num = i;
195 /* Control until ep is activated */
196 hpcd->IN_ep[i].type = EP_TYPE_CTRL;
197 hpcd->IN_ep[i].maxpacket = 0U;
198 hpcd->IN_ep[i].xfer_buff = 0U;
199 hpcd->IN_ep[i].xfer_len = 0U;
200 }
201
202 for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
203 {
204 hpcd->OUT_ep[i].is_in = 0U;
205 hpcd->OUT_ep[i].num = i;
206 /* Control until ep is activated */
207 hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
208 hpcd->OUT_ep[i].maxpacket = 0U;
209 hpcd->OUT_ep[i].xfer_buff = 0U;
210 hpcd->OUT_ep[i].xfer_len = 0U;
211 }
212
213 /* Init Device */
214 if (USB_DevInit(hpcd->Instance, hpcd->Init) != HAL_OK)
215 {
216 hpcd->State = HAL_PCD_STATE_ERROR;
217 return HAL_ERROR;
218 }
219
220 hpcd->USB_Address = 0U;
221 hpcd->State = HAL_PCD_STATE_READY;
222
223 /* Activate LPM */
224 if (hpcd->Init.lpm_enable == 1U)
225 {
226 (void)HAL_PCDEx_ActivateLPM(hpcd);
227 }
228
229 (void)USB_DevDisconnect(hpcd->Instance);
230
231 return HAL_OK;
232}
233
234/**
235 * @brief DeInitializes the PCD peripheral.
236 * @param hpcd PCD handle
237 * @retval HAL status
238 */
239HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
240{
241 /* Check the PCD handle allocation */
242 if (hpcd == NULL)
243 {
244 return HAL_ERROR;
245 }
246
247 hpcd->State = HAL_PCD_STATE_BUSY;
248
249 /* Stop Device */
250 if (USB_StopDevice(hpcd->Instance) != HAL_OK)
251 {
252 return HAL_ERROR;
253 }
254
255#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
256 if (hpcd->MspDeInitCallback == NULL)
257 {
258 hpcd->MspDeInitCallback = HAL_PCD_MspDeInit; /* Legacy weak MspDeInit */
259 }
260
261 /* DeInit the low level hardware */
262 hpcd->MspDeInitCallback(hpcd);
263#else
264 /* DeInit the low level hardware: CLOCK, NVIC.*/
265 HAL_PCD_MspDeInit(hpcd);
266#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
267
268 hpcd->State = HAL_PCD_STATE_RESET;
269
270 return HAL_OK;
271}
272
273/**
274 * @brief Initializes the PCD MSP.
275 * @param hpcd PCD handle
276 * @retval None
277 */
278__weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
279{
280 /* Prevent unused argument(s) compilation warning */
281 UNUSED(hpcd);
282
283 /* NOTE : This function should not be modified, when the callback is needed,
284 the HAL_PCD_MspInit could be implemented in the user file
285 */
286}
287
288/**
289 * @brief DeInitializes PCD MSP.
290 * @param hpcd PCD handle
291 * @retval None
292 */
293__weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
294{
295 /* Prevent unused argument(s) compilation warning */
296 UNUSED(hpcd);
297
298 /* NOTE : This function should not be modified, when the callback is needed,
299 the HAL_PCD_MspDeInit could be implemented in the user file
300 */
301}
302
303#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
304/**
305 * @brief Register a User USB PCD Callback
306 * To be used instead of the weak predefined callback
307 * @param hpcd USB PCD handle
308 * @param CallbackID ID of the callback to be registered
309 * This parameter can be one of the following values:
310 * @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
311 * @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
312 * @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
313 * @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
314 * @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
315 * @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
316 * @arg @ref HAL_PCD_DISCONNECT_CB_ID USB PCD Disconnect callback ID
317 * @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
318 * @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
319 * @param pCallback pointer to the Callback function
320 * @retval HAL status
321 */
322HAL_StatusTypeDef HAL_PCD_RegisterCallback(PCD_HandleTypeDef *hpcd,
323 HAL_PCD_CallbackIDTypeDef CallbackID,
324 pPCD_CallbackTypeDef pCallback)
325{
327
328 if (pCallback == NULL)
329 {
330 /* Update the error code */
331 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
332 return HAL_ERROR;
333 }
334 /* Process locked */
335 __HAL_LOCK(hpcd);
336
337 if (hpcd->State == HAL_PCD_STATE_READY)
338 {
339 switch (CallbackID)
340 {
341 case HAL_PCD_SOF_CB_ID :
342 hpcd->SOFCallback = pCallback;
343 break;
344
345 case HAL_PCD_SETUPSTAGE_CB_ID :
346 hpcd->SetupStageCallback = pCallback;
347 break;
348
349 case HAL_PCD_RESET_CB_ID :
350 hpcd->ResetCallback = pCallback;
351 break;
352
353 case HAL_PCD_SUSPEND_CB_ID :
354 hpcd->SuspendCallback = pCallback;
355 break;
356
357 case HAL_PCD_RESUME_CB_ID :
358 hpcd->ResumeCallback = pCallback;
359 break;
360
361 case HAL_PCD_CONNECT_CB_ID :
362 hpcd->ConnectCallback = pCallback;
363 break;
364
365 case HAL_PCD_DISCONNECT_CB_ID :
366 hpcd->DisconnectCallback = pCallback;
367 break;
368
369 case HAL_PCD_MSPINIT_CB_ID :
370 hpcd->MspInitCallback = pCallback;
371 break;
372
373 case HAL_PCD_MSPDEINIT_CB_ID :
374 hpcd->MspDeInitCallback = pCallback;
375 break;
376
377 default :
378 /* Update the error code */
379 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
380 /* Return error status */
382 break;
383 }
384 }
385 else if (hpcd->State == HAL_PCD_STATE_RESET)
386 {
387 switch (CallbackID)
388 {
389 case HAL_PCD_MSPINIT_CB_ID :
390 hpcd->MspInitCallback = pCallback;
391 break;
392
393 case HAL_PCD_MSPDEINIT_CB_ID :
394 hpcd->MspDeInitCallback = pCallback;
395 break;
396
397 default :
398 /* Update the error code */
399 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
400 /* Return error status */
402 break;
403 }
404 }
405 else
406 {
407 /* Update the error code */
408 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
409 /* Return error status */
411 }
412
413 /* Release Lock */
414 __HAL_UNLOCK(hpcd);
415 return status;
416}
417
418/**
419 * @brief Unregister an USB PCD Callback
420 * USB PCD callback is redirected to the weak predefined callback
421 * @param hpcd USB PCD handle
422 * @param CallbackID ID of the callback to be unregistered
423 * This parameter can be one of the following values:
424 * @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
425 * @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
426 * @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
427 * @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
428 * @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
429 * @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
430 * @arg @ref HAL_PCD_DISCONNECT_CB_ID USB PCD Disconnect callback ID
431 * @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
432 * @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
433 * @retval HAL status
434 */
435HAL_StatusTypeDef HAL_PCD_UnRegisterCallback(PCD_HandleTypeDef *hpcd, HAL_PCD_CallbackIDTypeDef CallbackID)
436{
438
439 /* Process locked */
440 __HAL_LOCK(hpcd);
441
442 /* Setup Legacy weak Callbacks */
443 if (hpcd->State == HAL_PCD_STATE_READY)
444 {
445 switch (CallbackID)
446 {
447 case HAL_PCD_SOF_CB_ID :
448 hpcd->SOFCallback = HAL_PCD_SOFCallback;
449 break;
450
451 case HAL_PCD_SETUPSTAGE_CB_ID :
452 hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
453 break;
454
455 case HAL_PCD_RESET_CB_ID :
456 hpcd->ResetCallback = HAL_PCD_ResetCallback;
457 break;
458
459 case HAL_PCD_SUSPEND_CB_ID :
460 hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
461 break;
462
463 case HAL_PCD_RESUME_CB_ID :
464 hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
465 break;
466
467 case HAL_PCD_CONNECT_CB_ID :
468 hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
469 break;
470
471 case HAL_PCD_DISCONNECT_CB_ID :
472 hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
473 break;
474
475 case HAL_PCD_MSPINIT_CB_ID :
476 hpcd->MspInitCallback = HAL_PCD_MspInit;
477 break;
478
479 case HAL_PCD_MSPDEINIT_CB_ID :
480 hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
481 break;
482
483 default :
484 /* Update the error code */
485 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
486
487 /* Return error status */
489 break;
490 }
491 }
492 else if (hpcd->State == HAL_PCD_STATE_RESET)
493 {
494 switch (CallbackID)
495 {
496 case HAL_PCD_MSPINIT_CB_ID :
497 hpcd->MspInitCallback = HAL_PCD_MspInit;
498 break;
499
500 case HAL_PCD_MSPDEINIT_CB_ID :
501 hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
502 break;
503
504 default :
505 /* Update the error code */
506 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
507
508 /* Return error status */
510 break;
511 }
512 }
513 else
514 {
515 /* Update the error code */
516 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
517
518 /* Return error status */
520 }
521
522 /* Release Lock */
523 __HAL_UNLOCK(hpcd);
524 return status;
525}
526
527/**
528 * @brief Register USB PCD Data OUT Stage Callback
529 * To be used instead of the weak HAL_PCD_DataOutStageCallback() predefined callback
530 * @param hpcd PCD handle
531 * @param pCallback pointer to the USB PCD Data OUT Stage Callback function
532 * @retval HAL status
533 */
534HAL_StatusTypeDef HAL_PCD_RegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd,
535 pPCD_DataOutStageCallbackTypeDef pCallback)
536{
538
539 if (pCallback == NULL)
540 {
541 /* Update the error code */
542 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
543
544 return HAL_ERROR;
545 }
546
547 /* Process locked */
548 __HAL_LOCK(hpcd);
549
550 if (hpcd->State == HAL_PCD_STATE_READY)
551 {
552 hpcd->DataOutStageCallback = pCallback;
553 }
554 else
555 {
556 /* Update the error code */
557 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
558
559 /* Return error status */
561 }
562
563 /* Release Lock */
564 __HAL_UNLOCK(hpcd);
565
566 return status;
567}
568
569/**
570 * @brief Unregister the USB PCD Data OUT Stage Callback
571 * USB PCD Data OUT Stage Callback is redirected to the weak HAL_PCD_DataOutStageCallback() predefined callback
572 * @param hpcd PCD handle
573 * @retval HAL status
574 */
575HAL_StatusTypeDef HAL_PCD_UnRegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd)
576{
578
579 /* Process locked */
580 __HAL_LOCK(hpcd);
581
582 if (hpcd->State == HAL_PCD_STATE_READY)
583 {
584 hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback; /* Legacy weak DataOutStageCallback */
585 }
586 else
587 {
588 /* Update the error code */
589 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
590
591 /* Return error status */
593 }
594
595 /* Release Lock */
596 __HAL_UNLOCK(hpcd);
597
598 return status;
599}
600
601/**
602 * @brief Register USB PCD Data IN Stage Callback
603 * To be used instead of the weak HAL_PCD_DataInStageCallback() predefined callback
604 * @param hpcd PCD handle
605 * @param pCallback pointer to the USB PCD Data IN Stage Callback function
606 * @retval HAL status
607 */
608HAL_StatusTypeDef HAL_PCD_RegisterDataInStageCallback(PCD_HandleTypeDef *hpcd,
609 pPCD_DataInStageCallbackTypeDef pCallback)
610{
612
613 if (pCallback == NULL)
614 {
615 /* Update the error code */
616 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
617
618 return HAL_ERROR;
619 }
620
621 /* Process locked */
622 __HAL_LOCK(hpcd);
623
624 if (hpcd->State == HAL_PCD_STATE_READY)
625 {
626 hpcd->DataInStageCallback = pCallback;
627 }
628 else
629 {
630 /* Update the error code */
631 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
632
633 /* Return error status */
635 }
636
637 /* Release Lock */
638 __HAL_UNLOCK(hpcd);
639
640 return status;
641}
642
643/**
644 * @brief Unregister the USB PCD Data IN Stage Callback
645 * USB PCD Data OUT Stage Callback is redirected to the weak HAL_PCD_DataInStageCallback() predefined callback
646 * @param hpcd PCD handle
647 * @retval HAL status
648 */
649HAL_StatusTypeDef HAL_PCD_UnRegisterDataInStageCallback(PCD_HandleTypeDef *hpcd)
650{
652
653 /* Process locked */
654 __HAL_LOCK(hpcd);
655
656 if (hpcd->State == HAL_PCD_STATE_READY)
657 {
658 hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback; /* Legacy weak DataInStageCallback */
659 }
660 else
661 {
662 /* Update the error code */
663 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
664
665 /* Return error status */
667 }
668
669 /* Release Lock */
670 __HAL_UNLOCK(hpcd);
671
672 return status;
673}
674
675/**
676 * @brief Register USB PCD Iso OUT incomplete Callback
677 * To be used instead of the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
678 * @param hpcd PCD handle
679 * @param pCallback pointer to the USB PCD Iso OUT incomplete Callback function
680 * @retval HAL status
681 */
682HAL_StatusTypeDef HAL_PCD_RegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd,
683 pPCD_IsoOutIncpltCallbackTypeDef pCallback)
684{
686
687 if (pCallback == NULL)
688 {
689 /* Update the error code */
690 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
691
692 return HAL_ERROR;
693 }
694
695 /* Process locked */
696 __HAL_LOCK(hpcd);
697
698 if (hpcd->State == HAL_PCD_STATE_READY)
699 {
700 hpcd->ISOOUTIncompleteCallback = pCallback;
701 }
702 else
703 {
704 /* Update the error code */
705 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
706
707 /* Return error status */
709 }
710
711 /* Release Lock */
712 __HAL_UNLOCK(hpcd);
713
714 return status;
715}
716
717/**
718 * @brief Unregister the USB PCD Iso OUT incomplete Callback
719 * USB PCD Iso OUT incomplete Callback is redirected
720 * to the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
721 * @param hpcd PCD handle
722 * @retval HAL status
723 */
724HAL_StatusTypeDef HAL_PCD_UnRegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd)
725{
727
728 /* Process locked */
729 __HAL_LOCK(hpcd);
730
731 if (hpcd->State == HAL_PCD_STATE_READY)
732 {
733 hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback; /* Legacy weak ISOOUTIncompleteCallback */
734 }
735 else
736 {
737 /* Update the error code */
738 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
739
740 /* Return error status */
742 }
743
744 /* Release Lock */
745 __HAL_UNLOCK(hpcd);
746
747 return status;
748}
749
750/**
751 * @brief Register USB PCD Iso IN incomplete Callback
752 * To be used instead of the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
753 * @param hpcd PCD handle
754 * @param pCallback pointer to the USB PCD Iso IN incomplete Callback function
755 * @retval HAL status
756 */
757HAL_StatusTypeDef HAL_PCD_RegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd,
758 pPCD_IsoInIncpltCallbackTypeDef pCallback)
759{
761
762 if (pCallback == NULL)
763 {
764 /* Update the error code */
765 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
766
767 return HAL_ERROR;
768 }
769
770 /* Process locked */
771 __HAL_LOCK(hpcd);
772
773 if (hpcd->State == HAL_PCD_STATE_READY)
774 {
775 hpcd->ISOINIncompleteCallback = pCallback;
776 }
777 else
778 {
779 /* Update the error code */
780 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
781
782 /* Return error status */
784 }
785
786 /* Release Lock */
787 __HAL_UNLOCK(hpcd);
788
789 return status;
790}
791
792/**
793 * @brief Unregister the USB PCD Iso IN incomplete Callback
794 * USB PCD Iso IN incomplete Callback is redirected
795 * to the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
796 * @param hpcd PCD handle
797 * @retval HAL status
798 */
799HAL_StatusTypeDef HAL_PCD_UnRegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd)
800{
802
803 /* Process locked */
804 __HAL_LOCK(hpcd);
805
806 if (hpcd->State == HAL_PCD_STATE_READY)
807 {
808 hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback; /* Legacy weak ISOINIncompleteCallback */
809 }
810 else
811 {
812 /* Update the error code */
813 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
814
815 /* Return error status */
817 }
818
819 /* Release Lock */
820 __HAL_UNLOCK(hpcd);
821
822 return status;
823}
824
825/**
826 * @brief Register USB PCD BCD Callback
827 * To be used instead of the weak HAL_PCDEx_BCD_Callback() predefined callback
828 * @param hpcd PCD handle
829 * @param pCallback pointer to the USB PCD BCD Callback function
830 * @retval HAL status
831 */
832HAL_StatusTypeDef HAL_PCD_RegisterBcdCallback(PCD_HandleTypeDef *hpcd, pPCD_BcdCallbackTypeDef pCallback)
833{
835
836 if (pCallback == NULL)
837 {
838 /* Update the error code */
839 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
840
841 return HAL_ERROR;
842 }
843
844 /* Process locked */
845 __HAL_LOCK(hpcd);
846
847 if (hpcd->State == HAL_PCD_STATE_READY)
848 {
849 hpcd->BCDCallback = pCallback;
850 }
851 else
852 {
853 /* Update the error code */
854 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
855
856 /* Return error status */
858 }
859
860 /* Release Lock */
861 __HAL_UNLOCK(hpcd);
862
863 return status;
864}
865
866/**
867 * @brief Unregister the USB PCD BCD Callback
868 * USB BCD Callback is redirected to the weak HAL_PCDEx_BCD_Callback() predefined callback
869 * @param hpcd PCD handle
870 * @retval HAL status
871 */
872HAL_StatusTypeDef HAL_PCD_UnRegisterBcdCallback(PCD_HandleTypeDef *hpcd)
873{
875
876 /* Process locked */
877 __HAL_LOCK(hpcd);
878
879 if (hpcd->State == HAL_PCD_STATE_READY)
880 {
881 hpcd->BCDCallback = HAL_PCDEx_BCD_Callback; /* Legacy weak HAL_PCDEx_BCD_Callback */
882 }
883 else
884 {
885 /* Update the error code */
886 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
887
888 /* Return error status */
890 }
891
892 /* Release Lock */
893 __HAL_UNLOCK(hpcd);
894
895 return status;
896}
897
898/**
899 * @brief Register USB PCD LPM Callback
900 * To be used instead of the weak HAL_PCDEx_LPM_Callback() predefined callback
901 * @param hpcd PCD handle
902 * @param pCallback pointer to the USB PCD LPM Callback function
903 * @retval HAL status
904 */
905HAL_StatusTypeDef HAL_PCD_RegisterLpmCallback(PCD_HandleTypeDef *hpcd, pPCD_LpmCallbackTypeDef pCallback)
906{
908
909 if (pCallback == NULL)
910 {
911 /* Update the error code */
912 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
913
914 return HAL_ERROR;
915 }
916
917 /* Process locked */
918 __HAL_LOCK(hpcd);
919
920 if (hpcd->State == HAL_PCD_STATE_READY)
921 {
922 hpcd->LPMCallback = pCallback;
923 }
924 else
925 {
926 /* Update the error code */
927 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
928
929 /* Return error status */
931 }
932
933 /* Release Lock */
934 __HAL_UNLOCK(hpcd);
935
936 return status;
937}
938
939/**
940 * @brief Unregister the USB PCD LPM Callback
941 * USB LPM Callback is redirected to the weak HAL_PCDEx_LPM_Callback() predefined callback
942 * @param hpcd PCD handle
943 * @retval HAL status
944 */
945HAL_StatusTypeDef HAL_PCD_UnRegisterLpmCallback(PCD_HandleTypeDef *hpcd)
946{
948
949 /* Process locked */
950 __HAL_LOCK(hpcd);
951
952 if (hpcd->State == HAL_PCD_STATE_READY)
953 {
954 hpcd->LPMCallback = HAL_PCDEx_LPM_Callback; /* Legacy weak HAL_PCDEx_LPM_Callback */
955 }
956 else
957 {
958 /* Update the error code */
959 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
960
961 /* Return error status */
963 }
964
965 /* Release Lock */
966 __HAL_UNLOCK(hpcd);
967
968 return status;
969}
970#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
971
972/**
973 * @}
974 */
975
976/** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
977 * @brief Data transfers functions
978 *
979@verbatim
980 ===============================================================================
981 ##### IO operation functions #####
982 ===============================================================================
983 [..]
984 This subsection provides a set of functions allowing to manage the PCD data
985 transfers.
986
987@endverbatim
988 * @{
989 */
990
991/**
992 * @brief Start the USB device
993 * @param hpcd PCD handle
994 * @retval HAL status
995 */
996HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
997{
998 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
999
1000 __HAL_LOCK(hpcd);
1001
1002 if (((USBx->GUSBCFG & USB_OTG_GUSBCFG_PHYSEL) != 0U) &&
1003 (hpcd->Init.battery_charging_enable == 1U))
1004 {
1005 /* Enable USB Transceiver */
1006 USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
1007 }
1008
1009 __HAL_PCD_ENABLE(hpcd);
1010 (void)USB_DevConnect(hpcd->Instance);
1011 __HAL_UNLOCK(hpcd);
1012
1013 return HAL_OK;
1014}
1015
1016/**
1017 * @brief Stop the USB device.
1018 * @param hpcd PCD handle
1019 * @retval HAL status
1020 */
1021HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
1022{
1023 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1024
1025 __HAL_LOCK(hpcd);
1026 __HAL_PCD_DISABLE(hpcd);
1027 (void)USB_DevDisconnect(hpcd->Instance);
1028
1029 (void)USB_FlushTxFifo(hpcd->Instance, 0x10U);
1030
1031 if (((USBx->GUSBCFG & USB_OTG_GUSBCFG_PHYSEL) != 0U) &&
1032 (hpcd->Init.battery_charging_enable == 1U))
1033 {
1034 /* Disable USB Transceiver */
1035 USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
1036 }
1037
1038 __HAL_UNLOCK(hpcd);
1039
1040 return HAL_OK;
1041}
1042
1043#if defined (USB_OTG_FS) || defined (USB_OTG_HS)
1044/**
1045 * @brief Handles PCD interrupt request.
1046 * @param hpcd PCD handle
1047 * @retval HAL status
1048 */
1049void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
1050{
1051 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1052 uint32_t USBx_BASE = (uint32_t)USBx;
1053 USB_OTG_EPTypeDef *ep;
1054 uint32_t i;
1055 uint32_t ep_intr;
1056 uint32_t epint;
1057 uint32_t epnum;
1058 uint32_t fifoemptymsk;
1059 uint32_t RegVal;
1060
1061 /* ensure that we are in device mode */
1062 if (USB_GetMode(hpcd->Instance) == USB_OTG_MODE_DEVICE)
1063 {
1064 /* avoid spurious interrupt */
1065 if (__HAL_PCD_IS_INVALID_INTERRUPT(hpcd))
1066 {
1067 return;
1068 }
1069
1070 /* store current frame number */
1071 hpcd->FrameNumber = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF_Msk) >> USB_OTG_DSTS_FNSOF_Pos;
1072
1073 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_MMIS))
1074 {
1075 /* incorrect mode, acknowledge the interrupt */
1076 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_MMIS);
1077 }
1078
1079 /* Handle RxQLevel Interrupt */
1080 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL))
1081 {
1082 USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
1083
1084 RegVal = USBx->GRXSTSP;
1085
1086 ep = &hpcd->OUT_ep[RegVal & USB_OTG_GRXSTSP_EPNUM];
1087
1088 if (((RegVal & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT)
1089 {
1090 if ((RegVal & USB_OTG_GRXSTSP_BCNT) != 0U)
1091 {
1092 (void)USB_ReadPacket(USBx, ep->xfer_buff,
1093 (uint16_t)((RegVal & USB_OTG_GRXSTSP_BCNT) >> 4));
1094
1095 ep->xfer_buff += (RegVal & USB_OTG_GRXSTSP_BCNT) >> 4;
1096 ep->xfer_count += (RegVal & USB_OTG_GRXSTSP_BCNT) >> 4;
1097 }
1098 }
1099 else if (((RegVal & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT)
1100 {
1101 (void)USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8U);
1102 ep->xfer_count += (RegVal & USB_OTG_GRXSTSP_BCNT) >> 4;
1103 }
1104 else
1105 {
1106 /* ... */
1107 }
1108
1109 USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
1110 }
1111
1112 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OEPINT))
1113 {
1114 epnum = 0U;
1115
1116 /* Read in the device interrupt bits */
1117 ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
1118
1119 while (ep_intr != 0U)
1120 {
1121 if ((ep_intr & 0x1U) != 0U)
1122 {
1123 epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, (uint8_t)epnum);
1124
1126 {
1127 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
1128 (void)PCD_EP_OutXfrComplete_int(hpcd, epnum);
1129 }
1130
1132 {
1133 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
1134 /* Class B setup phase done for previous decoded setup */
1135 (void)PCD_EP_OutSetupPacket_int(hpcd, epnum);
1136 }
1137
1139 {
1140 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
1141 }
1142
1143 /* Clear OUT Endpoint disable interrupt */
1145 {
1147 {
1148 USBx_DEVICE->DCTL |= USB_OTG_DCTL_CGONAK;
1149 }
1150
1151 ep = &hpcd->OUT_ep[epnum];
1152
1153 if (ep->is_iso_incomplete == 1U)
1154 {
1155 ep->is_iso_incomplete = 0U;
1156
1157#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1158 hpcd->ISOOUTIncompleteCallback(hpcd, (uint8_t)epnum);
1159#else
1160 HAL_PCD_ISOOUTIncompleteCallback(hpcd, (uint8_t)epnum);
1161#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1162 }
1163
1164 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_EPDISD);
1165 }
1166
1167 /* Clear Status Phase Received interrupt */
1169 {
1170 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
1171 }
1172
1173 /* Clear OUT NAK interrupt */
1175 {
1176 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_NAK);
1177 }
1178 }
1179 epnum++;
1180 ep_intr >>= 1U;
1181 }
1182 }
1183
1184 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT))
1185 {
1186 /* Read in the device interrupt bits */
1187 ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
1188
1189 epnum = 0U;
1190
1191 while (ep_intr != 0U)
1192 {
1193 if ((ep_intr & 0x1U) != 0U) /* In ITR */
1194 {
1195 epint = USB_ReadDevInEPInterrupt(hpcd->Instance, (uint8_t)epnum);
1196
1198 {
1199 fifoemptymsk = (uint32_t)(0x1UL << (epnum & EP_ADDR_MSK));
1200 USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
1201
1202 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
1203
1204 if (hpcd->Init.dma_enable == 1U)
1205 {
1206 hpcd->IN_ep[epnum].xfer_buff += hpcd->IN_ep[epnum].maxpacket;
1207
1208 /* this is ZLP, so prepare EP0 for next setup */
1209 if ((epnum == 0U) && (hpcd->IN_ep[epnum].xfer_len == 0U))
1210 {
1211 /* prepare to rx more setup packets */
1212 (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
1213 }
1214 }
1215
1216#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1217 hpcd->DataInStageCallback(hpcd, (uint8_t)epnum);
1218#else
1219 HAL_PCD_DataInStageCallback(hpcd, (uint8_t)epnum);
1220#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1221 }
1223 {
1224 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
1225 }
1227 {
1228 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
1229 }
1231 {
1232 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
1233 }
1235 {
1236 (void)USB_FlushTxFifo(USBx, epnum);
1237
1238 ep = &hpcd->IN_ep[epnum];
1239
1240 if (ep->is_iso_incomplete == 1U)
1241 {
1242 ep->is_iso_incomplete = 0U;
1243
1244#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1245 hpcd->ISOINIncompleteCallback(hpcd, (uint8_t)epnum);
1246#else
1247 HAL_PCD_ISOINIncompleteCallback(hpcd, (uint8_t)epnum);
1248#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1249 }
1250
1251 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
1252 }
1254 {
1255 (void)PCD_WriteEmptyTxFifo(hpcd, epnum);
1256 }
1257 }
1258 epnum++;
1259 ep_intr >>= 1U;
1260 }
1261 }
1262
1263 /* Handle Resume Interrupt */
1264 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT))
1265 {
1266 /* Clear the Remote Wake-up Signaling */
1267 USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
1268
1269 if (hpcd->LPM_State == LPM_L1)
1270 {
1271 hpcd->LPM_State = LPM_L0;
1272
1273#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1274 hpcd->LPMCallback(hpcd, PCD_LPM_L0_ACTIVE);
1275#else
1276 HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
1277#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1278 }
1279 else
1280 {
1281#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1282 hpcd->ResumeCallback(hpcd);
1283#else
1285#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1286 }
1287
1288 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
1289 }
1290
1291 /* Handle Suspend Interrupt */
1292 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP))
1293 {
1294 if ((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
1295 {
1296#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1297 hpcd->SuspendCallback(hpcd);
1298#else
1300#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1301 }
1302 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
1303 }
1304
1305 /* Handle LPM Interrupt */
1306 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT))
1307 {
1308 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT);
1309
1310 if (hpcd->LPM_State == LPM_L0)
1311 {
1312 hpcd->LPM_State = LPM_L1;
1313 hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >> 2U;
1314
1315#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1316 hpcd->LPMCallback(hpcd, PCD_LPM_L1_ACTIVE);
1317#else
1318 HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
1319#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1320 }
1321 else
1322 {
1323#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1324 hpcd->SuspendCallback(hpcd);
1325#else
1327#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1328 }
1329 }
1330
1331 /* Handle Reset Interrupt */
1332 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST))
1333 {
1334 USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
1335 (void)USB_FlushTxFifo(hpcd->Instance, 0x10U);
1336
1337 for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
1338 {
1339 USBx_INEP(i)->DIEPINT = 0xFB7FU;
1340 USBx_INEP(i)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
1341 USBx_OUTEP(i)->DOEPINT = 0xFB7FU;
1342 USBx_OUTEP(i)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
1343 USBx_OUTEP(i)->DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
1344 }
1345 USBx_DEVICE->DAINTMSK |= 0x10001U;
1346
1347 if (hpcd->Init.use_dedicated_ep1 != 0U)
1348 {
1349 USBx_DEVICE->DOUTEP1MSK |= USB_OTG_DOEPMSK_STUPM |
1352
1353 USBx_DEVICE->DINEP1MSK |= USB_OTG_DIEPMSK_TOM |
1356 }
1357 else
1358 {
1359 USBx_DEVICE->DOEPMSK |= USB_OTG_DOEPMSK_STUPM |
1364
1365 USBx_DEVICE->DIEPMSK |= USB_OTG_DIEPMSK_TOM |
1368 }
1369
1370 /* Set Default Address to 0 */
1371 USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
1372
1373 /* setup EP0 to receive SETUP packets */
1374 (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t)hpcd->Init.dma_enable,
1375 (uint8_t *)hpcd->Setup);
1376
1377 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
1378 }
1379
1380 /* Handle Enumeration done Interrupt */
1381 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE))
1382 {
1383 (void)USB_ActivateSetup(hpcd->Instance);
1384 hpcd->Init.speed = USB_GetDevSpeed(hpcd->Instance);
1385
1386 /* Set USB Turnaround time */
1387 (void)USB_SetTurnaroundTime(hpcd->Instance,
1389 (uint8_t)hpcd->Init.speed);
1390
1391#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1392 hpcd->ResetCallback(hpcd);
1393#else
1395#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1396
1397 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE);
1398 }
1399
1400 /* Handle SOF Interrupt */
1401 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SOF))
1402 {
1403#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1404 hpcd->SOFCallback(hpcd);
1405#else
1406 HAL_PCD_SOFCallback(hpcd);
1407#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1408
1409 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SOF);
1410 }
1411
1412 /* Handle Global OUT NAK effective Interrupt */
1413 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_BOUTNAKEFF))
1414 {
1416
1417 for (epnum = 1U; epnum < hpcd->Init.dev_endpoints; epnum++)
1418 {
1419 if (hpcd->OUT_ep[epnum].is_iso_incomplete == 1U)
1420 {
1421 /* disable the EP */
1422 USBx_OUTEP(epnum)->DOEPCTL |= (USB_OTG_DOEPCTL_SNAK);
1423 USBx_OUTEP(epnum)->DOEPCTL |= (USB_OTG_DOEPCTL_EPDIS);
1424 }
1425 }
1426 }
1427
1428 /* Handle Incomplete ISO IN Interrupt */
1429 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR))
1430 {
1431 for (epnum = 1U; epnum < hpcd->Init.dev_endpoints; epnum++)
1432 {
1433 RegVal = USBx_INEP(epnum)->DIEPCTL;
1434
1435 if ((hpcd->IN_ep[epnum].type == EP_TYPE_ISOC) &&
1437 {
1438 hpcd->IN_ep[epnum].is_iso_incomplete = 1U;
1439
1440 /* Abort current transaction and disable the EP */
1441 (void)HAL_PCD_EP_Abort(hpcd, (uint8_t)(epnum | 0x80U));
1442 }
1443 }
1444
1445 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR);
1446 }
1447
1448 /* Handle Incomplete ISO OUT Interrupt */
1449 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
1450 {
1451 for (epnum = 1U; epnum < hpcd->Init.dev_endpoints; epnum++)
1452 {
1453 RegVal = USBx_OUTEP(epnum)->DOEPCTL;
1454
1455 if ((hpcd->OUT_ep[epnum].type == EP_TYPE_ISOC) &&
1457 (((RegVal & (0x1UL << 16)) >> 16U) == (hpcd->FrameNumber & 0x1U)))
1458 {
1459 hpcd->OUT_ep[epnum].is_iso_incomplete = 1U;
1460
1462
1463 if ((USBx->GINTSTS & USB_OTG_GINTSTS_BOUTNAKEFF) == 0U)
1464 {
1465 USBx_DEVICE->DCTL |= USB_OTG_DCTL_SGONAK;
1466 break;
1467 }
1468 }
1469 }
1470
1471 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
1472 }
1473
1474 /* Handle Connection event Interrupt */
1475 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT))
1476 {
1477#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1478 hpcd->ConnectCallback(hpcd);
1479#else
1481#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1482
1483 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT);
1484 }
1485
1486 /* Handle Disconnection event Interrupt */
1487 if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OTGINT))
1488 {
1489 RegVal = hpcd->Instance->GOTGINT;
1490
1492 {
1493#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1494 hpcd->DisconnectCallback(hpcd);
1495#else
1497#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1498 }
1499 hpcd->Instance->GOTGINT |= RegVal;
1500 }
1501 }
1502}
1503#endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
1504
1505
1506/**
1507 * @brief Data OUT stage callback.
1508 * @param hpcd PCD handle
1509 * @param epnum endpoint number
1510 * @retval None
1511 */
1512__weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
1513{
1514 /* Prevent unused argument(s) compilation warning */
1515 UNUSED(hpcd);
1516 UNUSED(epnum);
1517
1518 /* NOTE : This function should not be modified, when the callback is needed,
1519 the HAL_PCD_DataOutStageCallback could be implemented in the user file
1520 */
1521}
1522
1523/**
1524 * @brief Data IN stage callback
1525 * @param hpcd PCD handle
1526 * @param epnum endpoint number
1527 * @retval None
1528 */
1529__weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
1530{
1531 /* Prevent unused argument(s) compilation warning */
1532 UNUSED(hpcd);
1533 UNUSED(epnum);
1534
1535 /* NOTE : This function should not be modified, when the callback is needed,
1536 the HAL_PCD_DataInStageCallback could be implemented in the user file
1537 */
1538}
1539/**
1540 * @brief Setup stage callback
1541 * @param hpcd PCD handle
1542 * @retval None
1543 */
1544__weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
1545{
1546 /* Prevent unused argument(s) compilation warning */
1547 UNUSED(hpcd);
1548
1549 /* NOTE : This function should not be modified, when the callback is needed,
1550 the HAL_PCD_SetupStageCallback could be implemented in the user file
1551 */
1552}
1553
1554/**
1555 * @brief USB Start Of Frame callback.
1556 * @param hpcd PCD handle
1557 * @retval None
1558 */
1559__weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
1560{
1561 /* Prevent unused argument(s) compilation warning */
1562 UNUSED(hpcd);
1563
1564 /* NOTE : This function should not be modified, when the callback is needed,
1565 the HAL_PCD_SOFCallback could be implemented in the user file
1566 */
1567}
1568
1569/**
1570 * @brief USB Reset callback.
1571 * @param hpcd PCD handle
1572 * @retval None
1573 */
1574__weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
1575{
1576 /* Prevent unused argument(s) compilation warning */
1577 UNUSED(hpcd);
1578
1579 /* NOTE : This function should not be modified, when the callback is needed,
1580 the HAL_PCD_ResetCallback could be implemented in the user file
1581 */
1582}
1583
1584/**
1585 * @brief Suspend event callback.
1586 * @param hpcd PCD handle
1587 * @retval None
1588 */
1589__weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
1590{
1591 /* Prevent unused argument(s) compilation warning */
1592 UNUSED(hpcd);
1593
1594 /* NOTE : This function should not be modified, when the callback is needed,
1595 the HAL_PCD_SuspendCallback could be implemented in the user file
1596 */
1597}
1598
1599/**
1600 * @brief Resume event callback.
1601 * @param hpcd PCD handle
1602 * @retval None
1603 */
1604__weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
1605{
1606 /* Prevent unused argument(s) compilation warning */
1607 UNUSED(hpcd);
1608
1609 /* NOTE : This function should not be modified, when the callback is needed,
1610 the HAL_PCD_ResumeCallback could be implemented in the user file
1611 */
1612}
1613
1614/**
1615 * @brief Incomplete ISO OUT callback.
1616 * @param hpcd PCD handle
1617 * @param epnum endpoint number
1618 * @retval None
1619 */
1620__weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
1621{
1622 /* Prevent unused argument(s) compilation warning */
1623 UNUSED(hpcd);
1624 UNUSED(epnum);
1625
1626 /* NOTE : This function should not be modified, when the callback is needed,
1627 the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
1628 */
1629}
1630
1631/**
1632 * @brief Incomplete ISO IN callback.
1633 * @param hpcd PCD handle
1634 * @param epnum endpoint number
1635 * @retval None
1636 */
1637__weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
1638{
1639 /* Prevent unused argument(s) compilation warning */
1640 UNUSED(hpcd);
1641 UNUSED(epnum);
1642
1643 /* NOTE : This function should not be modified, when the callback is needed,
1644 the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
1645 */
1646}
1647
1648/**
1649 * @brief Connection event callback.
1650 * @param hpcd PCD handle
1651 * @retval None
1652 */
1653__weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
1654{
1655 /* Prevent unused argument(s) compilation warning */
1656 UNUSED(hpcd);
1657
1658 /* NOTE : This function should not be modified, when the callback is needed,
1659 the HAL_PCD_ConnectCallback could be implemented in the user file
1660 */
1661}
1662
1663/**
1664 * @brief Disconnection event callback.
1665 * @param hpcd PCD handle
1666 * @retval None
1667 */
1668__weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
1669{
1670 /* Prevent unused argument(s) compilation warning */
1671 UNUSED(hpcd);
1672
1673 /* NOTE : This function should not be modified, when the callback is needed,
1674 the HAL_PCD_DisconnectCallback could be implemented in the user file
1675 */
1676}
1677
1678/**
1679 * @}
1680 */
1681
1682/** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
1683 * @brief management functions
1684 *
1685@verbatim
1686 ===============================================================================
1687 ##### Peripheral Control functions #####
1688 ===============================================================================
1689 [..]
1690 This subsection provides a set of functions allowing to control the PCD data
1691 transfers.
1692
1693@endverbatim
1694 * @{
1695 */
1696
1697/**
1698 * @brief Connect the USB device
1699 * @param hpcd PCD handle
1700 * @retval HAL status
1701 */
1702HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
1703{
1704 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1705
1706 __HAL_LOCK(hpcd);
1707
1708 if (((USBx->GUSBCFG & USB_OTG_GUSBCFG_PHYSEL) != 0U) &&
1709 (hpcd->Init.battery_charging_enable == 1U))
1710 {
1711 /* Enable USB Transceiver */
1712 USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
1713 }
1714 (void)USB_DevConnect(hpcd->Instance);
1715 __HAL_UNLOCK(hpcd);
1716
1717 return HAL_OK;
1718}
1719
1720/**
1721 * @brief Disconnect the USB device.
1722 * @param hpcd PCD handle
1723 * @retval HAL status
1724 */
1725HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
1726{
1727 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1728
1729 __HAL_LOCK(hpcd);
1730 (void)USB_DevDisconnect(hpcd->Instance);
1731
1732 if (((USBx->GUSBCFG & USB_OTG_GUSBCFG_PHYSEL) != 0U) &&
1733 (hpcd->Init.battery_charging_enable == 1U))
1734 {
1735 /* Disable USB Transceiver */
1736 USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
1737 }
1738
1739 __HAL_UNLOCK(hpcd);
1740
1741 return HAL_OK;
1742}
1743
1744/**
1745 * @brief Set the USB Device address.
1746 * @param hpcd PCD handle
1747 * @param address new device address
1748 * @retval HAL status
1749 */
1750HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
1751{
1752 __HAL_LOCK(hpcd);
1753 hpcd->USB_Address = address;
1754 (void)USB_SetDevAddress(hpcd->Instance, address);
1755 __HAL_UNLOCK(hpcd);
1756
1757 return HAL_OK;
1758}
1759/**
1760 * @brief Open and configure an endpoint.
1761 * @param hpcd PCD handle
1762 * @param ep_addr endpoint address
1763 * @param ep_mps endpoint max packet size
1764 * @param ep_type endpoint type
1765 * @retval HAL status
1766 */
1767HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
1768 uint16_t ep_mps, uint8_t ep_type)
1769{
1771 PCD_EPTypeDef *ep;
1772
1773 if ((ep_addr & 0x80U) == 0x80U)
1774 {
1775 ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1776 ep->is_in = 1U;
1777 }
1778 else
1779 {
1780 ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1781 ep->is_in = 0U;
1782 }
1783
1784 ep->num = ep_addr & EP_ADDR_MSK;
1785 ep->maxpacket = (uint32_t)ep_mps & 0x7FFU;
1786 ep->type = ep_type;
1787
1788 if (ep->is_in != 0U)
1789 {
1790 /* Assign a Tx FIFO */
1791 ep->tx_fifo_num = ep->num;
1792 }
1793
1794 /* Set initial data PID. */
1795 if (ep_type == EP_TYPE_BULK)
1796 {
1797 ep->data_pid_start = 0U;
1798 }
1799
1800 __HAL_LOCK(hpcd);
1801 (void)USB_ActivateEndpoint(hpcd->Instance, ep);
1802 __HAL_UNLOCK(hpcd);
1803
1804 return ret;
1805}
1806
1807/**
1808 * @brief Deactivate an endpoint.
1809 * @param hpcd PCD handle
1810 * @param ep_addr endpoint address
1811 * @retval HAL status
1812 */
1813HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1814{
1815 PCD_EPTypeDef *ep;
1816
1817 if ((ep_addr & 0x80U) == 0x80U)
1818 {
1819 ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1820 ep->is_in = 1U;
1821 }
1822 else
1823 {
1824 ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1825 ep->is_in = 0U;
1826 }
1827 ep->num = ep_addr & EP_ADDR_MSK;
1828
1829 __HAL_LOCK(hpcd);
1830 (void)USB_DeactivateEndpoint(hpcd->Instance, ep);
1831 __HAL_UNLOCK(hpcd);
1832 return HAL_OK;
1833}
1834
1835
1836/**
1837 * @brief Receive an amount of data.
1838 * @param hpcd PCD handle
1839 * @param ep_addr endpoint address
1840 * @param pBuf pointer to the reception buffer
1841 * @param len amount of data to be received
1842 * @retval HAL status
1843 */
1844HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
1845{
1846 PCD_EPTypeDef *ep;
1847
1848 ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1849
1850 /*setup and start the Xfer */
1851 ep->xfer_buff = pBuf;
1852 ep->xfer_len = len;
1853 ep->xfer_count = 0U;
1854 ep->is_in = 0U;
1855 ep->num = ep_addr & EP_ADDR_MSK;
1856
1857 if (hpcd->Init.dma_enable == 1U)
1858 {
1859 ep->dma_addr = (uint32_t)pBuf;
1860 }
1861
1862 (void)USB_EPStartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
1863
1864 return HAL_OK;
1865}
1866
1867/**
1868 * @brief Get Received Data Size
1869 * @param hpcd PCD handle
1870 * @param ep_addr endpoint address
1871 * @retval Data Size
1872 */
1873uint32_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef const *hpcd, uint8_t ep_addr)
1874{
1875 return hpcd->OUT_ep[ep_addr & EP_ADDR_MSK].xfer_count;
1876}
1877/**
1878 * @brief Send an amount of data
1879 * @param hpcd PCD handle
1880 * @param ep_addr endpoint address
1881 * @param pBuf pointer to the transmission buffer
1882 * @param len amount of data to be sent
1883 * @retval HAL status
1884 */
1885HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
1886{
1887 PCD_EPTypeDef *ep;
1888
1889 ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1890
1891 /*setup and start the Xfer */
1892 ep->xfer_buff = pBuf;
1893 ep->xfer_len = len;
1894 ep->xfer_count = 0U;
1895 ep->is_in = 1U;
1896 ep->num = ep_addr & EP_ADDR_MSK;
1897
1898 if (hpcd->Init.dma_enable == 1U)
1899 {
1900 ep->dma_addr = (uint32_t)pBuf;
1901 }
1902
1903 (void)USB_EPStartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
1904
1905 return HAL_OK;
1906}
1907
1908/**
1909 * @brief Set a STALL condition over an endpoint
1910 * @param hpcd PCD handle
1911 * @param ep_addr endpoint address
1912 * @retval HAL status
1913 */
1914HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1915{
1916 PCD_EPTypeDef *ep;
1917
1918 if (((uint32_t)ep_addr & EP_ADDR_MSK) > hpcd->Init.dev_endpoints)
1919 {
1920 return HAL_ERROR;
1921 }
1922
1923 if ((0x80U & ep_addr) == 0x80U)
1924 {
1925 ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1926 ep->is_in = 1U;
1927 }
1928 else
1929 {
1930 ep = &hpcd->OUT_ep[ep_addr];
1931 ep->is_in = 0U;
1932 }
1933
1934 ep->is_stall = 1U;
1935 ep->num = ep_addr & EP_ADDR_MSK;
1936
1937 __HAL_LOCK(hpcd);
1938
1939 (void)USB_EPSetStall(hpcd->Instance, ep);
1940
1941 if ((ep_addr & EP_ADDR_MSK) == 0U)
1942 {
1943 (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t)hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
1944 }
1945
1946 __HAL_UNLOCK(hpcd);
1947
1948 return HAL_OK;
1949}
1950
1951/**
1952 * @brief Clear a STALL condition over in an endpoint
1953 * @param hpcd PCD handle
1954 * @param ep_addr endpoint address
1955 * @retval HAL status
1956 */
1957HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1958{
1959 PCD_EPTypeDef *ep;
1960
1961 if (((uint32_t)ep_addr & 0x0FU) > hpcd->Init.dev_endpoints)
1962 {
1963 return HAL_ERROR;
1964 }
1965
1966 if ((0x80U & ep_addr) == 0x80U)
1967 {
1968 ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1969 ep->is_in = 1U;
1970 }
1971 else
1972 {
1973 ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1974 ep->is_in = 0U;
1975 }
1976
1977 ep->is_stall = 0U;
1978 ep->num = ep_addr & EP_ADDR_MSK;
1979
1980 __HAL_LOCK(hpcd);
1981 (void)USB_EPClearStall(hpcd->Instance, ep);
1982 __HAL_UNLOCK(hpcd);
1983
1984 return HAL_OK;
1985}
1986
1987/**
1988 * @brief Abort an USB EP transaction.
1989 * @param hpcd PCD handle
1990 * @param ep_addr endpoint address
1991 * @retval HAL status
1992 */
1993HAL_StatusTypeDef HAL_PCD_EP_Abort(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1994{
1996 PCD_EPTypeDef *ep;
1997
1998 if ((0x80U & ep_addr) == 0x80U)
1999 {
2000 ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
2001 }
2002 else
2003 {
2004 ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
2005 }
2006
2007 /* Stop Xfer */
2008 ret = USB_EPStopXfer(hpcd->Instance, ep);
2009
2010 return ret;
2011}
2012
2013/**
2014 * @brief Flush an endpoint
2015 * @param hpcd PCD handle
2016 * @param ep_addr endpoint address
2017 * @retval HAL status
2018 */
2019HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
2020{
2021 __HAL_LOCK(hpcd);
2022
2023 if ((ep_addr & 0x80U) == 0x80U)
2024 {
2025 (void)USB_FlushTxFifo(hpcd->Instance, (uint32_t)ep_addr & EP_ADDR_MSK);
2026 }
2027 else
2028 {
2029 (void)USB_FlushRxFifo(hpcd->Instance);
2030 }
2031
2032 __HAL_UNLOCK(hpcd);
2033
2034 return HAL_OK;
2035}
2036
2037/**
2038 * @brief Activate remote wakeup signalling
2039 * @param hpcd PCD handle
2040 * @retval HAL status
2041 */
2042HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
2043{
2044 return (USB_ActivateRemoteWakeup(hpcd->Instance));
2045}
2046
2047/**
2048 * @brief De-activate remote wakeup signalling.
2049 * @param hpcd PCD handle
2050 * @retval HAL status
2051 */
2052HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
2053{
2054 return (USB_DeActivateRemoteWakeup(hpcd->Instance));
2055}
2056
2057/**
2058 * @}
2059 */
2060
2061/** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
2062 * @brief Peripheral State functions
2063 *
2064@verbatim
2065 ===============================================================================
2066 ##### Peripheral State functions #####
2067 ===============================================================================
2068 [..]
2069 This subsection permits to get in run-time the status of the peripheral
2070 and the data flow.
2071
2072@endverbatim
2073 * @{
2074 */
2075
2076/**
2077 * @brief Return the PCD handle state.
2078 * @param hpcd PCD handle
2079 * @retval HAL state
2080 */
2081PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef const *hpcd)
2082{
2083 return hpcd->State;
2084}
2085
2086#if defined (USB_OTG_FS) || defined (USB_OTG_HS)
2087/**
2088 * @brief Set the USB Device high speed test mode.
2089 * @param hpcd PCD handle
2090 * @param testmode USB Device high speed test mode
2091 * @retval HAL status
2092 */
2093HAL_StatusTypeDef HAL_PCD_SetTestMode(const PCD_HandleTypeDef *hpcd, uint8_t testmode)
2094{
2095 const USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
2096 uint32_t USBx_BASE = (uint32_t)USBx;
2097
2098 switch (testmode)
2099 {
2100 case TEST_J:
2101 case TEST_K:
2102 case TEST_SE0_NAK:
2103 case TEST_PACKET:
2104 case TEST_FORCE_EN:
2105 USBx_DEVICE->DCTL &= ~(0x7UL << 4);
2106 USBx_DEVICE->DCTL |= (uint32_t)testmode << 4;
2107 break;
2108
2109 default:
2110 break;
2111 }
2112
2113 return HAL_OK;
2114}
2115#endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
2116/**
2117 * @}
2118 */
2119
2120/**
2121 * @}
2122 */
2123
2124/* Private functions ---------------------------------------------------------*/
2125/** @addtogroup PCD_Private_Functions
2126 * @{
2127 */
2128#if defined (USB_OTG_FS) || defined (USB_OTG_HS)
2129/**
2130 * @brief Check FIFO for the next packet to be loaded.
2131 * @param hpcd PCD handle
2132 * @param epnum endpoint number
2133 * @retval HAL status
2134 */
2135static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
2136{
2137 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
2138 uint32_t USBx_BASE = (uint32_t)USBx;
2139 USB_OTG_EPTypeDef *ep;
2140 uint32_t len;
2141 uint32_t len32b;
2142 uint32_t fifoemptymsk;
2143
2144 ep = &hpcd->IN_ep[epnum];
2145
2146 if (ep->xfer_count > ep->xfer_len)
2147 {
2148 return HAL_ERROR;
2149 }
2150
2151 len = ep->xfer_len - ep->xfer_count;
2152
2153 if (len > ep->maxpacket)
2154 {
2155 len = ep->maxpacket;
2156 }
2157
2158 len32b = (len + 3U) / 4U;
2159
2160 while (((USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) >= len32b) &&
2161 (ep->xfer_count < ep->xfer_len) && (ep->xfer_len != 0U))
2162 {
2163 /* Write the FIFO */
2164 len = ep->xfer_len - ep->xfer_count;
2165
2166 if (len > ep->maxpacket)
2167 {
2168 len = ep->maxpacket;
2169 }
2170 len32b = (len + 3U) / 4U;
2171
2172 (void)USB_WritePacket(USBx, ep->xfer_buff, (uint8_t)epnum, (uint16_t)len,
2173 (uint8_t)hpcd->Init.dma_enable);
2174
2175 ep->xfer_buff += len;
2176 ep->xfer_count += len;
2177 }
2178
2179 if (ep->xfer_len <= ep->xfer_count)
2180 {
2181 fifoemptymsk = (uint32_t)(0x1UL << (epnum & EP_ADDR_MSK));
2182 USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
2183 }
2184
2185 return HAL_OK;
2186}
2187
2188
2189/**
2190 * @brief process EP OUT transfer complete interrupt.
2191 * @param hpcd PCD handle
2192 * @param epnum endpoint number
2193 * @retval HAL status
2194 */
2195static HAL_StatusTypeDef PCD_EP_OutXfrComplete_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
2196{
2197 USB_OTG_EPTypeDef *ep;
2198 const USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
2199 uint32_t USBx_BASE = (uint32_t)USBx;
2200 uint32_t gSNPSiD = *(__IO const uint32_t *)(&USBx->CID + 0x1U);
2201 uint32_t DoepintReg = USBx_OUTEP(epnum)->DOEPINT;
2202
2203 if (hpcd->Init.dma_enable == 1U)
2204 {
2205 if ((DoepintReg & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP) /* Class C */
2206 {
2207 /* StupPktRcvd = 1 this is a setup packet */
2208 if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
2210 {
2211 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
2212 }
2213 }
2214 else if ((DoepintReg & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR) /* Class E */
2215 {
2216 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
2217 }
2218 else if ((DoepintReg & (USB_OTG_DOEPINT_STUP | USB_OTG_DOEPINT_OTEPSPR)) == 0U)
2219 {
2220 /* StupPktRcvd = 1 this is a setup packet */
2221 if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
2223 {
2224 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
2225 }
2226 else
2227 {
2228 ep = &hpcd->OUT_ep[epnum];
2229
2230 /* out data packet received over EP */
2231 ep->xfer_count = ep->xfer_size - (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
2232
2233 if (epnum == 0U)
2234 {
2235 if (ep->xfer_len == 0U)
2236 {
2237 /* this is ZLP, so prepare EP0 for next setup */
2238 (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
2239 }
2240 else
2241 {
2242 ep->xfer_buff += ep->xfer_count;
2243 }
2244 }
2245
2246#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
2247 hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
2248#else
2249 HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
2250#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
2251 }
2252 }
2253 else
2254 {
2255 /* ... */
2256 }
2257 }
2258 else
2259 {
2260 if (gSNPSiD == USB_OTG_CORE_ID_310A)
2261 {
2262 /* StupPktRcvd = 1 this is a setup packet */
2264 {
2265 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
2266 }
2267 else
2268 {
2270 {
2271 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
2272 }
2273
2274#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
2275 hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
2276#else
2277 HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
2278#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
2279 }
2280 }
2281 else
2282 {
2283 if ((epnum == 0U) && (hpcd->OUT_ep[epnum].xfer_len == 0U))
2284 {
2285 /* this is ZLP, so prepare EP0 for next setup */
2286 (void)USB_EP0_OutStart(hpcd->Instance, 0U, (uint8_t *)hpcd->Setup);
2287 }
2288
2289#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
2290 hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
2291#else
2292 HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
2293#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
2294 }
2295 }
2296
2297 return HAL_OK;
2298}
2299
2300
2301/**
2302 * @brief process EP OUT setup packet received interrupt.
2303 * @param hpcd PCD handle
2304 * @param epnum endpoint number
2305 * @retval HAL status
2306 */
2307static HAL_StatusTypeDef PCD_EP_OutSetupPacket_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
2308{
2309 const USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
2310 uint32_t USBx_BASE = (uint32_t)USBx;
2311 uint32_t gSNPSiD = *(__IO const uint32_t *)(&USBx->CID + 0x1U);
2312 uint32_t DoepintReg = USBx_OUTEP(epnum)->DOEPINT;
2313
2314 if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
2316 {
2317 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
2318 }
2319
2320 /* Inform the upper layer that a setup packet is available */
2321#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
2322 hpcd->SetupStageCallback(hpcd);
2323#else
2325#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
2326
2327 if ((gSNPSiD > USB_OTG_CORE_ID_300A) && (hpcd->Init.dma_enable == 1U))
2328 {
2329 (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
2330 }
2331
2332 return HAL_OK;
2333}
2334#endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
2335
2336/**
2337 * @}
2338 */
2339#endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
2340#endif /* HAL_PCD_MODULE_ENABLED */
2341/**
2342 * @}
2343 */
2344
2345/**
2346 * @}
2347 */
#define __IO
Defines 'read / write' permissions.
#define IS_PCD_ALL_INSTANCE(INSTANCE)
#define USB_OTG_DOEPCTL_SNAK
Set NAK.
#define USB_OTG_GRXSTSP_EPNUM
IN EP interrupt mask bits.
#define USB_OTG_GLPMCFG_BESL
BESL value received with last ACKed LPM Token.
#define USB_OTG_DOEPINT_XFRC
Transfer completed interrupt.
#define USB_OTG_DOEPINT_NAK
NAK Packet is transmitted by the device.
#define USB_OTG_GUSBCFG_PHYSEL
USB 2.0 high-speed ULPI PHY or USB 1.1 full-speed serial transceiver select.
#define USB_OTG_DOEPMSK_OTEPSPRM
Status Phase Received mask.
#define USB_OTG_DCTL_CGONAK
Clear global OUT NAK.
#define USB_OTG_DOEPINT_OTEPSPR
OUT Status Phase Received interrupt.
#define USB_OTG_DIEPINT_TOC
Timeout condition.
#define USB_OTG_DSTS_FNSOF_Pos
#define USB_OTG_GINTSTS_BOUTNAKEFF
Global OUT NAK effective.
#define USB_OTG_DOEPINT_EPDISD
Endpoint disabled interrupt.
#define USB_OTG_DOEPMSK_NAKM
OUT Packet NAK interrupt mask.
#define USB_OTG_GINTSTS_LPMINT
LPM interrupt.
#define USB_OTG_DOEPINT_OTEPDIS
OUT token received when endpoint disabled.
#define USB_OTG_DIEPINT_INEPNE
IN endpoint NAK effective.
#define USB_OTG_GINTSTS_USBRST
USB reset.
#define USB_OTG_GOTGINT_SEDET
Session end detected.
#define USB_OTG_GINTSTS_SOF
Start of frame.
#define USB_OTG_GINTSTS_OTGINT
OTG interrupt.
#define USB_OTG_GCCFG_PWRDWN
Power down.
#define USB_OTG_GINTSTS_PXFR_INCOMPISOOUT
Incomplete periodic transfer.
#define USB_OTG_DOEPCTL_STALL
STALL handshake.
#define USB_OTG_DOEPMSK_EPDM
Endpoint disabled interrupt mask.
#define USB_OTG_GINTSTS_WKUINT
Resume/remote wakeup detected interrupt.
#define USB_OTG_GINTSTS_IISOIXFR
Incomplete isochronous IN transfer.
#define USB_OTG_DIEPINT_EPDISD
Endpoint disabled interrupt.
#define USB_OTG_DIEPMSK_XFRCM
Transfer completed interrupt mask.
#define USB_OTG_DCTL_SGONAK
Set global OUT NAK.
#define USB_OTG_GINTSTS_OEPINT
OUT endpoint interrupt.
#define USB_OTG_DIEPMSK_TOM
Timeout condition mask (nonisochronous endpoints).
#define USB_OTG_DOEPINT_STUP
SETUP phase done.
#define USB_OTG_DOEPCTL_EPENA
Endpoint enable.
#define USB_OTG_GINTSTS_ENUMDNE
Enumeration done.
#define USB_OTG_DCTL_RWUSIG
Remote wakeup signaling.
#define USB_OTG_DSTS_SUSPSTS
Suspend status.
#define USB_OTG_DOEPINT_STPKTRX
Setup Packet Received.
#define USB_OTG_GRXSTSP_PKTSTS
OUT EP interrupt mask bits.
#define USB_OTG_DSTS_FNSOF_Msk
0x003FFF00
#define USB_OTG_DOEPMSK_XFRCM
Transfer completed interrupt mask.
#define USB_OTG_GRXSTSP_BCNT
OUT EP interrupt mask bits.
#define USB_OTG_DIEPINT_XFRC
Transfer completed interrupt.
#define USB_OTG_GINTSTS_MMIS
Mode mismatch interrupt.
#define USB_OTG_DIEPCTL_STALL
STALL handshake.
#define USB_OTG_DOEPTSIZ_XFRSIZ
Transfer size.
#define USB_OTG_GINTSTS_IEPINT
IN endpoint interrupt.
#define USB_OTG_GINTMSK_GONAKEFFM
Global OUT NAK effective mask.
#define USB_OTG_DOEPMSK_STUPM
SETUP phase done mask.
#define USB_OTG_GINTSTS_RXFLVL
RxFIFO nonempty.
#define USB_OTG_DIEPINT_ITTXFE
IN token received when TxFIFO is empty.
#define USB_OTG_DIEPCTL_EPENA
Endpoint enable.
#define USB_OTG_GINTSTS_SRQINT
Session request/new session detected interrupt.
#define USB_OTG_GINTSTS_USBSUSP
USB suspend.
#define USB_OTG_DTXFSTS_INEPTFSAV
IN endpoint TxFIFO space available.
#define USB_OTG_DIEPINT_TXFE
Transmit FIFO empty.
#define USB_OTG_DOEPCTL_EPDIS
Endpoint disable.
#define USB_OTG_DCFG_DAD
Device address.
#define USB_OTG_DIEPMSK_EPDM
Endpoint disabled interrupt mask.
uint32_t HAL_RCC_GetHCLKFreq(void)
#define NULL
Definition usbd_def.h:44
This file contains all the functions prototypes for the HAL module driver.
#define assert_param(expr)
Uncomment the line below to expanse the "assert_param" macro in the HAL drivers code.
HAL_StatusTypeDef
HAL Status structures definition.
@ HAL_ERROR
@ HAL_OK
#define __HAL_UNLOCK(__HANDLE__)
#define UNUSED(x)
@ HAL_UNLOCKED
#define __HAL_LOCK(__HANDLE__)
USB_OTG_Core_Registers.
__IO uint32_t GRXSTSP
Receive Sts Q Read & POP Register 020h.
__IO uint32_t GINTSTS
Core Interrupt Register 014h.
__IO uint32_t GUSBCFG
Core USB Configuration Register 00Ch.
__IO uint32_t GINTMSK
Core Interrupt Mask Register 018h.
__IO uint32_t CID
User ID Register 03Ch.
__IO uint32_t GCCFG
General Purpose IO Register 038h.
void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
Resume callback.
Definition usbd_conf.c:247
void HAL_PCD_MspDeInit(PCD_HandleTypeDef *pcdHandle)
Definition usbd_conf.c:102
void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
Connect callback.
Definition usbd_conf.c:294
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
Suspend callback.
Definition usbd_conf.c:222
void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
Data In stage callback.
Definition usbd_conf.c:161
void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
Disconnect callback.
Definition usbd_conf.c:308
void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
Data Out stage callback.
Definition usbd_conf.c:146
void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
ISOOUTIncomplete callback.
Definition usbd_conf.c:265
void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
SOF callback.
Definition usbd_conf.c:175
void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
ISOINIncomplete callback.
Definition usbd_conf.c:280
void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
Setup stage callback.
Definition usbd_conf.c:131
void HAL_PCD_MspInit(PCD_HandleTypeDef *pcdHandle)
Definition usbd_conf.c:66
void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
Reset callback.
Definition usbd_conf.c:189