1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_hal_hcd.c
4   * @author  MCD Application Team
5   * @brief   HCD 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     (#)Declare a HCD_HandleTypeDef handle structure, for example:
30        HCD_HandleTypeDef  hhcd;
31 
32     (#)Fill parameters of Init structure in HCD handle
33 
34     (#)Call HAL_HCD_Init() API to initialize the HCD peripheral (Core, Host core, ...)
35 
36     (#)Initialize the HCD low level resources through the HAL_HCD_MspInit() API:
37         (##) Enable the HCD/USB Low Level interface clock using the following macros
38              (+++) __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
39         (##) Initialize the related GPIO clocks
40         (##) Configure HCD pin-out
41         (##) Configure HCD NVIC interrupt
42 
43     (#)Associate the Upper USB Host stack to the HAL HCD Driver:
44         (##) hhcd.pData = phost;
45 
46     (#)Enable HCD transmission and reception:
47         (##) HAL_HCD_Start();
48 
49   @endverbatim
50   ******************************************************************************
51   */
52 
53 /* Includes ------------------------------------------------------------------*/
54 #include "stm32l4xx_hal.h"
55 
56 /** @addtogroup STM32L4xx_HAL_Driver
57   * @{
58   */
59 
60 #ifdef HAL_HCD_MODULE_ENABLED
61 #if defined (USB_OTG_FS)
62 
63 /** @defgroup HCD HCD
64   * @brief HCD HAL module driver
65   * @{
66   */
67 
68 /* Private typedef -----------------------------------------------------------*/
69 /* Private define ------------------------------------------------------------*/
70 /* Private macro -------------------------------------------------------------*/
71 /* Private variables ---------------------------------------------------------*/
72 /* Private function prototypes -----------------------------------------------*/
73 /** @defgroup HCD_Private_Functions HCD Private Functions
74   * @{
75   */
76 static void HCD_HC_IN_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum);
77 static void HCD_HC_OUT_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum);
78 static void HCD_RXQLVL_IRQHandler(HCD_HandleTypeDef *hhcd);
79 static void HCD_Port_IRQHandler(HCD_HandleTypeDef *hhcd);
80 /**
81   * @}
82   */
83 
84 /* Exported functions --------------------------------------------------------*/
85 /** @defgroup HCD_Exported_Functions HCD Exported Functions
86   * @{
87   */
88 
89 /** @defgroup HCD_Exported_Functions_Group1 Initialization and de-initialization functions
90   *  @brief    Initialization and Configuration functions
91   *
92 @verbatim
93  ===============================================================================
94           ##### Initialization and de-initialization functions #####
95  ===============================================================================
96     [..]  This section provides functions allowing to:
97 
98 @endverbatim
99   * @{
100   */
101 
102 /**
103   * @brief  Initialize the host driver.
104   * @param  hhcd HCD handle
105   * @retval HAL status
106   */
HAL_HCD_Init(HCD_HandleTypeDef * hhcd)107 HAL_StatusTypeDef HAL_HCD_Init(HCD_HandleTypeDef *hhcd)
108 {
109   USB_OTG_GlobalTypeDef *USBx;
110 
111   /* Check the HCD handle allocation */
112   if (hhcd == NULL)
113   {
114     return HAL_ERROR;
115   }
116 
117   /* Check the parameters */
118   assert_param(IS_HCD_ALL_INSTANCE(hhcd->Instance));
119 
120   USBx = hhcd->Instance;
121 
122   if (hhcd->State == HAL_HCD_STATE_RESET)
123   {
124     /* Allocate lock resource and initialize it */
125     hhcd->Lock = HAL_UNLOCKED;
126 
127 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
128     hhcd->SOFCallback = HAL_HCD_SOF_Callback;
129     hhcd->ConnectCallback = HAL_HCD_Connect_Callback;
130     hhcd->DisconnectCallback = HAL_HCD_Disconnect_Callback;
131     hhcd->PortEnabledCallback = HAL_HCD_PortEnabled_Callback;
132     hhcd->PortDisabledCallback = HAL_HCD_PortDisabled_Callback;
133     hhcd->HC_NotifyURBChangeCallback = HAL_HCD_HC_NotifyURBChange_Callback;
134 
135     if (hhcd->MspInitCallback == NULL)
136     {
137       hhcd->MspInitCallback = HAL_HCD_MspInit;
138     }
139 
140     /* Init the low level hardware */
141     hhcd->MspInitCallback(hhcd);
142 #else
143     /* Init the low level hardware : GPIO, CLOCK, NVIC... */
144     HAL_HCD_MspInit(hhcd);
145 #endif /* (USE_HAL_HCD_REGISTER_CALLBACKS) */
146   }
147 
148   hhcd->State = HAL_HCD_STATE_BUSY;
149 
150   /* Disable DMA mode for FS instance */
151   if ((USBx->CID & (0x1U << 8)) == 0U)
152   {
153     hhcd->Init.dma_enable = 0U;
154   }
155 
156   /* Disable the Interrupts */
157   __HAL_HCD_DISABLE(hhcd);
158 
159   /* Init the Core (common init.) */
160   (void)USB_CoreInit(hhcd->Instance, hhcd->Init);
161 
162   /* Force Host Mode*/
163   (void)USB_SetCurrentMode(hhcd->Instance, USB_HOST_MODE);
164 
165   /* Init Host */
166   (void)USB_HostInit(hhcd->Instance, hhcd->Init);
167 
168   hhcd->State = HAL_HCD_STATE_READY;
169 
170   return HAL_OK;
171 }
172 
173 /**
174   * @brief  Initialize a host channel.
175   * @param  hhcd HCD handle
176   * @param  ch_num Channel number.
177   *         This parameter can be a value from 1 to 15
178   * @param  epnum Endpoint number.
179   *          This parameter can be a value from 1 to 15
180   * @param  dev_address Current device address
181   *          This parameter can be a value from 0 to 255
182   * @param  speed Current device speed.
183   *          This parameter can be one of these values:
184   *            HCD_DEVICE_SPEED_FULL: Full speed mode,
185   *            HCD_DEVICE_SPEED_LOW: Low speed mode
186   * @param  ep_type Endpoint Type.
187   *          This parameter can be one of these values:
188   *            EP_TYPE_CTRL: Control type,
189   *            EP_TYPE_ISOC: Isochronous type,
190   *            EP_TYPE_BULK: Bulk type,
191   *            EP_TYPE_INTR: Interrupt type
192   * @param  mps Max Packet Size.
193   *          This parameter can be a value from 0 to32K
194   * @retval HAL status
195   */
HAL_HCD_HC_Init(HCD_HandleTypeDef * hhcd,uint8_t ch_num,uint8_t epnum,uint8_t dev_address,uint8_t speed,uint8_t ep_type,uint16_t mps)196 HAL_StatusTypeDef HAL_HCD_HC_Init(HCD_HandleTypeDef *hhcd,
197                                   uint8_t ch_num,
198                                   uint8_t epnum,
199                                   uint8_t dev_address,
200                                   uint8_t speed,
201                                   uint8_t ep_type,
202                                   uint16_t mps)
203 {
204   HAL_StatusTypeDef status;
205 
206   __HAL_LOCK(hhcd);
207   hhcd->hc[ch_num].do_ping = 0U;
208   hhcd->hc[ch_num].dev_addr = dev_address;
209   hhcd->hc[ch_num].max_packet = mps;
210   hhcd->hc[ch_num].ch_num = ch_num;
211   hhcd->hc[ch_num].ep_type = ep_type;
212   hhcd->hc[ch_num].ep_num = epnum & 0x7FU;
213 
214   if ((epnum & 0x80U) == 0x80U)
215   {
216     hhcd->hc[ch_num].ep_is_in = 1U;
217   }
218   else
219   {
220     hhcd->hc[ch_num].ep_is_in = 0U;
221   }
222 
223   hhcd->hc[ch_num].speed = speed;
224 
225   status =  USB_HC_Init(hhcd->Instance,
226                         ch_num,
227                         epnum,
228                         dev_address,
229                         speed,
230                         ep_type,
231                         mps);
232   __HAL_UNLOCK(hhcd);
233 
234   return status;
235 }
236 
237 /**
238   * @brief  Halt a host channel.
239   * @param  hhcd HCD handle
240   * @param  ch_num Channel number.
241   *         This parameter can be a value from 1 to 15
242   * @retval HAL status
243   */
HAL_HCD_HC_Halt(HCD_HandleTypeDef * hhcd,uint8_t ch_num)244 HAL_StatusTypeDef HAL_HCD_HC_Halt(HCD_HandleTypeDef *hhcd, uint8_t ch_num)
245 {
246   HAL_StatusTypeDef status = HAL_OK;
247 
248   __HAL_LOCK(hhcd);
249   (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
250   __HAL_UNLOCK(hhcd);
251 
252   return status;
253 }
254 
255 /**
256   * @brief  DeInitialize the host driver.
257   * @param  hhcd HCD handle
258   * @retval HAL status
259   */
HAL_HCD_DeInit(HCD_HandleTypeDef * hhcd)260 HAL_StatusTypeDef HAL_HCD_DeInit(HCD_HandleTypeDef *hhcd)
261 {
262   /* Check the HCD handle allocation */
263   if (hhcd == NULL)
264   {
265     return HAL_ERROR;
266   }
267 
268   hhcd->State = HAL_HCD_STATE_BUSY;
269 
270 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
271   if (hhcd->MspDeInitCallback == NULL)
272   {
273     hhcd->MspDeInitCallback = HAL_HCD_MspDeInit; /* Legacy weak MspDeInit  */
274   }
275 
276   /* DeInit the low level hardware */
277   hhcd->MspDeInitCallback(hhcd);
278 #else
279   /* DeInit the low level hardware: CLOCK, NVIC.*/
280   HAL_HCD_MspDeInit(hhcd);
281 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
282 
283   __HAL_HCD_DISABLE(hhcd);
284 
285   hhcd->State = HAL_HCD_STATE_RESET;
286 
287   return HAL_OK;
288 }
289 
290 /**
291   * @brief  Initialize the HCD MSP.
292   * @param  hhcd HCD handle
293   * @retval None
294   */
HAL_HCD_MspInit(HCD_HandleTypeDef * hhcd)295 __weak void  HAL_HCD_MspInit(HCD_HandleTypeDef *hhcd)
296 {
297   /* Prevent unused argument(s) compilation warning */
298   UNUSED(hhcd);
299 
300   /* NOTE : This function should not be modified, when the callback is needed,
301             the HAL_HCD_MspInit could be implemented in the user file
302    */
303 }
304 
305 /**
306   * @brief  DeInitialize the HCD MSP.
307   * @param  hhcd HCD handle
308   * @retval None
309   */
HAL_HCD_MspDeInit(HCD_HandleTypeDef * hhcd)310 __weak void  HAL_HCD_MspDeInit(HCD_HandleTypeDef *hhcd)
311 {
312   /* Prevent unused argument(s) compilation warning */
313   UNUSED(hhcd);
314 
315   /* NOTE : This function should not be modified, when the callback is needed,
316             the HAL_HCD_MspDeInit could be implemented in the user file
317    */
318 }
319 
320 /**
321   * @}
322   */
323 
324 /** @defgroup HCD_Exported_Functions_Group2 Input and Output operation functions
325   *  @brief   HCD IO operation functions
326   *
327 @verbatim
328  ===============================================================================
329                       ##### IO operation functions #####
330  ===============================================================================
331  [..] This subsection provides a set of functions allowing to manage the USB Host Data
332     Transfer
333 
334 @endverbatim
335   * @{
336   */
337 
338 /**
339   * @brief  Submit a new URB for processing.
340   * @param  hhcd HCD handle
341   * @param  ch_num Channel number.
342   *         This parameter can be a value from 1 to 15
343   * @param  direction Channel number.
344   *          This parameter can be one of these values:
345   *           0 : Output / 1 : Input
346   * @param  ep_type Endpoint Type.
347   *          This parameter can be one of these values:
348   *            EP_TYPE_CTRL: Control type/
349   *            EP_TYPE_ISOC: Isochronous type/
350   *            EP_TYPE_BULK: Bulk type/
351   *            EP_TYPE_INTR: Interrupt type/
352   * @param  token Endpoint Type.
353   *          This parameter can be one of these values:
354   *            0: HC_PID_SETUP / 1: HC_PID_DATA1
355   * @param  pbuff pointer to URB data
356   * @param  length Length of URB data
357   * @param  do_ping activate do ping protocol (for high speed only).
358   *          This parameter can be one of these values:
359   *           0 : do ping inactive / 1 : do ping active
360   * @retval HAL status
361   */
HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef * hhcd,uint8_t ch_num,uint8_t direction,uint8_t ep_type,uint8_t token,uint8_t * pbuff,uint16_t length,uint8_t do_ping)362 HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
363                                            uint8_t ch_num,
364                                            uint8_t direction,
365                                            uint8_t ep_type,
366                                            uint8_t token,
367                                            uint8_t *pbuff,
368                                            uint16_t length,
369                                            uint8_t do_ping)
370 {
371   hhcd->hc[ch_num].ep_is_in = direction;
372   hhcd->hc[ch_num].ep_type  = ep_type;
373 
374   if (token == 0U)
375   {
376     hhcd->hc[ch_num].data_pid = HC_PID_SETUP;
377     hhcd->hc[ch_num].do_ping = do_ping;
378   }
379   else
380   {
381     hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
382   }
383 
384   /* Manage Data Toggle */
385   switch (ep_type)
386   {
387     case EP_TYPE_CTRL:
388       if ((token == 1U) && (direction == 0U)) /*send data */
389       {
390         if (length == 0U)
391         {
392           /* For Status OUT stage, Length==0, Status Out PID = 1 */
393           hhcd->hc[ch_num].toggle_out = 1U;
394         }
395 
396         /* Set the Data Toggle bit as per the Flag */
397         if (hhcd->hc[ch_num].toggle_out == 0U)
398         {
399           /* Put the PID 0 */
400           hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
401         }
402         else
403         {
404           /* Put the PID 1 */
405           hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
406         }
407       }
408       break;
409 
410     case EP_TYPE_BULK:
411       if (direction == 0U)
412       {
413         /* Set the Data Toggle bit as per the Flag */
414         if (hhcd->hc[ch_num].toggle_out == 0U)
415         {
416           /* Put the PID 0 */
417           hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
418         }
419         else
420         {
421           /* Put the PID 1 */
422           hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
423         }
424       }
425       else
426       {
427         if (hhcd->hc[ch_num].toggle_in == 0U)
428         {
429           hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
430         }
431         else
432         {
433           hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
434         }
435       }
436 
437       break;
438     case EP_TYPE_INTR:
439       if (direction == 0U)
440       {
441         /* Set the Data Toggle bit as per the Flag */
442         if (hhcd->hc[ch_num].toggle_out == 0U)
443         {
444           /* Put the PID 0 */
445           hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
446         }
447         else
448         {
449           /* Put the PID 1 */
450           hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
451         }
452       }
453       else
454       {
455         if (hhcd->hc[ch_num].toggle_in == 0U)
456         {
457           hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
458         }
459         else
460         {
461           hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
462         }
463       }
464       break;
465 
466     case EP_TYPE_ISOC:
467       hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
468       break;
469 
470     default:
471       break;
472   }
473 
474   hhcd->hc[ch_num].xfer_buff = pbuff;
475   hhcd->hc[ch_num].xfer_len  = length;
476   hhcd->hc[ch_num].urb_state = URB_IDLE;
477   hhcd->hc[ch_num].xfer_count = 0U;
478   hhcd->hc[ch_num].ch_num = ch_num;
479   hhcd->hc[ch_num].state = HC_IDLE;
480 
481   return USB_HC_StartXfer(hhcd->Instance, &hhcd->hc[ch_num]);
482 }
483 
484 /**
485   * @brief  Handle HCD interrupt request.
486   * @param  hhcd HCD handle
487   * @retval None
488   */
HAL_HCD_IRQHandler(HCD_HandleTypeDef * hhcd)489 void HAL_HCD_IRQHandler(HCD_HandleTypeDef *hhcd)
490 {
491   USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
492   uint32_t USBx_BASE = (uint32_t)USBx;
493   uint32_t i;
494   uint32_t interrupt;
495 
496   /* Ensure that we are in device mode */
497   if (USB_GetMode(hhcd->Instance) == USB_OTG_MODE_HOST)
498   {
499     /* Avoid spurious interrupt */
500     if (__HAL_HCD_IS_INVALID_INTERRUPT(hhcd))
501     {
502       return;
503     }
504 
505     if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
506     {
507       /* Incorrect mode, acknowledge the interrupt */
508       __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
509     }
510 
511     if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_IISOIXFR))
512     {
513       /* Incorrect mode, acknowledge the interrupt */
514       __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_IISOIXFR);
515     }
516 
517     if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_PTXFE))
518     {
519       /* Incorrect mode, acknowledge the interrupt */
520       __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_PTXFE);
521     }
522 
523     if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_MMIS))
524     {
525       /* Incorrect mode, acknowledge the interrupt */
526       __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_MMIS);
527     }
528 
529     /* Handle Host Disconnect Interrupts */
530     if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_DISCINT))
531     {
532       __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_DISCINT);
533 
534       if ((USBx_HPRT0 & USB_OTG_HPRT_PCSTS) == 0U)
535       {
536         /* Flush USB Fifo */
537         (void)USB_FlushTxFifo(USBx, 0x10U);
538         (void)USB_FlushRxFifo(USBx);
539 
540         /* Restore FS Clock */
541         (void)USB_InitFSLSPClkSel(hhcd->Instance, HCFG_48_MHZ);
542 
543         /* Handle Host Port Disconnect Interrupt */
544 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
545         hhcd->DisconnectCallback(hhcd);
546 #else
547         HAL_HCD_Disconnect_Callback(hhcd);
548 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
549       }
550     }
551 
552     /* Handle Host Port Interrupts */
553     if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_HPRTINT))
554     {
555       HCD_Port_IRQHandler(hhcd);
556     }
557 
558     /* Handle Host SOF Interrupt */
559     if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_SOF))
560     {
561 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
562       hhcd->SOFCallback(hhcd);
563 #else
564       HAL_HCD_SOF_Callback(hhcd);
565 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
566 
567       __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_SOF);
568     }
569 
570     /* Handle Rx Queue Level Interrupts */
571     if ((__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_RXFLVL)) != 0U)
572     {
573       USB_MASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_RXFLVL);
574 
575       HCD_RXQLVL_IRQHandler(hhcd);
576 
577       USB_UNMASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_RXFLVL);
578     }
579 
580     /* Handle Host channel Interrupt */
581     if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_HCINT))
582     {
583       interrupt = USB_HC_ReadInterrupt(hhcd->Instance);
584       for (i = 0U; i < hhcd->Init.Host_channels; i++)
585       {
586         if ((interrupt & (1UL << (i & 0xFU))) != 0U)
587         {
588           if ((USBx_HC(i)->HCCHAR & USB_OTG_HCCHAR_EPDIR) == USB_OTG_HCCHAR_EPDIR)
589           {
590             HCD_HC_IN_IRQHandler(hhcd, (uint8_t)i);
591           }
592           else
593           {
594             HCD_HC_OUT_IRQHandler(hhcd, (uint8_t)i);
595           }
596         }
597       }
598       __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_HCINT);
599     }
600   }
601 }
602 
603 
604 /**
605   * @brief  SOF callback.
606   * @param  hhcd HCD handle
607   * @retval None
608   */
HAL_HCD_SOF_Callback(HCD_HandleTypeDef * hhcd)609 __weak void HAL_HCD_SOF_Callback(HCD_HandleTypeDef *hhcd)
610 {
611   /* Prevent unused argument(s) compilation warning */
612   UNUSED(hhcd);
613 
614   /* NOTE : This function should not be modified, when the callback is needed,
615             the HAL_HCD_SOF_Callback could be implemented in the user file
616    */
617 }
618 
619 /**
620   * @brief Connection Event callback.
621   * @param  hhcd HCD handle
622   * @retval None
623   */
HAL_HCD_Connect_Callback(HCD_HandleTypeDef * hhcd)624 __weak void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd)
625 {
626   /* Prevent unused argument(s) compilation warning */
627   UNUSED(hhcd);
628 
629   /* NOTE : This function should not be modified, when the callback is needed,
630             the HAL_HCD_Connect_Callback could be implemented in the user file
631    */
632 }
633 
634 /**
635   * @brief  Disconnection Event callback.
636   * @param  hhcd HCD handle
637   * @retval None
638   */
HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef * hhcd)639 __weak void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd)
640 {
641   /* Prevent unused argument(s) compilation warning */
642   UNUSED(hhcd);
643 
644   /* NOTE : This function should not be modified, when the callback is needed,
645             the HAL_HCD_Disconnect_Callback could be implemented in the user file
646    */
647 }
648 
649 /**
650   * @brief  Port Enabled  Event callback.
651   * @param  hhcd HCD handle
652   * @retval None
653   */
HAL_HCD_PortEnabled_Callback(HCD_HandleTypeDef * hhcd)654 __weak void HAL_HCD_PortEnabled_Callback(HCD_HandleTypeDef *hhcd)
655 {
656   /* Prevent unused argument(s) compilation warning */
657   UNUSED(hhcd);
658 
659   /* NOTE : This function should not be modified, when the callback is needed,
660             the HAL_HCD_Disconnect_Callback could be implemented in the user file
661    */
662 }
663 
664 /**
665   * @brief  Port Disabled  Event callback.
666   * @param  hhcd HCD handle
667   * @retval None
668   */
HAL_HCD_PortDisabled_Callback(HCD_HandleTypeDef * hhcd)669 __weak void HAL_HCD_PortDisabled_Callback(HCD_HandleTypeDef *hhcd)
670 {
671   /* Prevent unused argument(s) compilation warning */
672   UNUSED(hhcd);
673 
674   /* NOTE : This function should not be modified, when the callback is needed,
675             the HAL_HCD_Disconnect_Callback could be implemented in the user file
676    */
677 }
678 
679 /**
680   * @brief  Notify URB state change callback.
681   * @param  hhcd HCD handle
682   * @param  chnum Channel number.
683   *         This parameter can be a value from 1 to 15
684   * @param  urb_state:
685   *          This parameter can be one of these values:
686   *            URB_IDLE/
687   *            URB_DONE/
688   *            URB_NOTREADY/
689   *            URB_NYET/
690   *            URB_ERROR/
691   *            URB_STALL/
692   * @retval None
693   */
HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef * hhcd,uint8_t chnum,HCD_URBStateTypeDef urb_state)694 __weak void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum, HCD_URBStateTypeDef urb_state)
695 {
696   /* Prevent unused argument(s) compilation warning */
697   UNUSED(hhcd);
698   UNUSED(chnum);
699   UNUSED(urb_state);
700 
701   /* NOTE : This function should not be modified, when the callback is needed,
702             the HAL_HCD_HC_NotifyURBChange_Callback could be implemented in the user file
703    */
704 }
705 
706 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
707 /**
708   * @brief  Register a User USB HCD Callback
709   *         To be used instead of the weak predefined callback
710   * @param  hhcd USB HCD handle
711   * @param  CallbackID ID of the callback to be registered
712   *         This parameter can be one of the following values:
713   *          @arg @ref HAL_HCD_SOF_CB_ID USB HCD SOF callback ID
714   *          @arg @ref HAL_HCD_CONNECT_CB_ID USB HCD Connect callback ID
715   *          @arg @ref HAL_HCD_DISCONNECT_CB_ID OTG HCD Disconnect callback ID
716   *          @arg @ref HAL_HCD_PORT_ENABLED_CB_ID USB HCD Port Enable callback ID
717   *          @arg @ref HAL_HCD_PORT_DISABLED_CB_ID USB HCD Port Disable callback ID
718   *          @arg @ref HAL_HCD_MSPINIT_CB_ID MspDeInit callback ID
719   *          @arg @ref HAL_HCD_MSPDEINIT_CB_ID MspDeInit callback ID
720   * @param  pCallback pointer to the Callback function
721   * @retval HAL status
722   */
HAL_HCD_RegisterCallback(HCD_HandleTypeDef * hhcd,HAL_HCD_CallbackIDTypeDef CallbackID,pHCD_CallbackTypeDef pCallback)723 HAL_StatusTypeDef HAL_HCD_RegisterCallback(HCD_HandleTypeDef *hhcd,
724                                            HAL_HCD_CallbackIDTypeDef CallbackID,
725                                            pHCD_CallbackTypeDef pCallback)
726 {
727   HAL_StatusTypeDef status = HAL_OK;
728 
729   if (pCallback == NULL)
730   {
731     /* Update the error code */
732     hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
733     return HAL_ERROR;
734   }
735   /* Process locked */
736   __HAL_LOCK(hhcd);
737 
738   if (hhcd->State == HAL_HCD_STATE_READY)
739   {
740     switch (CallbackID)
741     {
742       case HAL_HCD_SOF_CB_ID :
743         hhcd->SOFCallback = pCallback;
744         break;
745 
746       case HAL_HCD_CONNECT_CB_ID :
747         hhcd->ConnectCallback = pCallback;
748         break;
749 
750       case HAL_HCD_DISCONNECT_CB_ID :
751         hhcd->DisconnectCallback = pCallback;
752         break;
753 
754       case HAL_HCD_PORT_ENABLED_CB_ID :
755         hhcd->PortEnabledCallback = pCallback;
756         break;
757 
758       case HAL_HCD_PORT_DISABLED_CB_ID :
759         hhcd->PortDisabledCallback = pCallback;
760         break;
761 
762       case HAL_HCD_MSPINIT_CB_ID :
763         hhcd->MspInitCallback = pCallback;
764         break;
765 
766       case HAL_HCD_MSPDEINIT_CB_ID :
767         hhcd->MspDeInitCallback = pCallback;
768         break;
769 
770       default :
771         /* Update the error code */
772         hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
773         /* Return error status */
774         status =  HAL_ERROR;
775         break;
776     }
777   }
778   else if (hhcd->State == HAL_HCD_STATE_RESET)
779   {
780     switch (CallbackID)
781     {
782       case HAL_HCD_MSPINIT_CB_ID :
783         hhcd->MspInitCallback = pCallback;
784         break;
785 
786       case HAL_HCD_MSPDEINIT_CB_ID :
787         hhcd->MspDeInitCallback = pCallback;
788         break;
789 
790       default :
791         /* Update the error code */
792         hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
793         /* Return error status */
794         status =  HAL_ERROR;
795         break;
796     }
797   }
798   else
799   {
800     /* Update the error code */
801     hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
802     /* Return error status */
803     status =  HAL_ERROR;
804   }
805 
806   /* Release Lock */
807   __HAL_UNLOCK(hhcd);
808   return status;
809 }
810 
811 /**
812   * @brief  Unregister an USB HCD Callback
813   *         USB HCD callback is redirected to the weak predefined callback
814   * @param  hhcd USB HCD handle
815   * @param  CallbackID ID of the callback to be unregistered
816   *         This parameter can be one of the following values:
817   *          @arg @ref HAL_HCD_SOF_CB_ID USB HCD SOF callback ID
818   *          @arg @ref HAL_HCD_CONNECT_CB_ID USB HCD Connect callback ID
819   *          @arg @ref HAL_HCD_DISCONNECT_CB_ID OTG HCD Disconnect callback ID
820   *          @arg @ref HAL_HCD_PORT_ENABLED_CB_ID USB HCD Port Enabled callback ID
821   *          @arg @ref HAL_HCD_PORT_DISABLED_CB_ID USB HCD Port Disabled callback ID
822   *          @arg @ref HAL_HCD_MSPINIT_CB_ID MspDeInit callback ID
823   *          @arg @ref HAL_HCD_MSPDEINIT_CB_ID MspDeInit callback ID
824   * @retval HAL status
825   */
HAL_HCD_UnRegisterCallback(HCD_HandleTypeDef * hhcd,HAL_HCD_CallbackIDTypeDef CallbackID)826 HAL_StatusTypeDef HAL_HCD_UnRegisterCallback(HCD_HandleTypeDef *hhcd, HAL_HCD_CallbackIDTypeDef CallbackID)
827 {
828   HAL_StatusTypeDef status = HAL_OK;
829 
830   /* Process locked */
831   __HAL_LOCK(hhcd);
832 
833   /* Setup Legacy weak Callbacks  */
834   if (hhcd->State == HAL_HCD_STATE_READY)
835   {
836     switch (CallbackID)
837     {
838       case HAL_HCD_SOF_CB_ID :
839         hhcd->SOFCallback = HAL_HCD_SOF_Callback;
840         break;
841 
842       case HAL_HCD_CONNECT_CB_ID :
843         hhcd->ConnectCallback = HAL_HCD_Connect_Callback;
844         break;
845 
846       case HAL_HCD_DISCONNECT_CB_ID :
847         hhcd->DisconnectCallback = HAL_HCD_Disconnect_Callback;
848         break;
849 
850       case HAL_HCD_PORT_ENABLED_CB_ID :
851         hhcd->PortEnabledCallback = HAL_HCD_PortEnabled_Callback;
852         break;
853 
854       case HAL_HCD_PORT_DISABLED_CB_ID :
855         hhcd->PortDisabledCallback = HAL_HCD_PortDisabled_Callback;
856         break;
857 
858       case HAL_HCD_MSPINIT_CB_ID :
859         hhcd->MspInitCallback = HAL_HCD_MspInit;
860         break;
861 
862       case HAL_HCD_MSPDEINIT_CB_ID :
863         hhcd->MspDeInitCallback = HAL_HCD_MspDeInit;
864         break;
865 
866       default :
867         /* Update the error code */
868         hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
869 
870         /* Return error status */
871         status =  HAL_ERROR;
872         break;
873     }
874   }
875   else if (hhcd->State == HAL_HCD_STATE_RESET)
876   {
877     switch (CallbackID)
878     {
879       case HAL_HCD_MSPINIT_CB_ID :
880         hhcd->MspInitCallback = HAL_HCD_MspInit;
881         break;
882 
883       case HAL_HCD_MSPDEINIT_CB_ID :
884         hhcd->MspDeInitCallback = HAL_HCD_MspDeInit;
885         break;
886 
887       default :
888         /* Update the error code */
889         hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
890 
891         /* Return error status */
892         status =  HAL_ERROR;
893         break;
894     }
895   }
896   else
897   {
898     /* Update the error code */
899     hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
900 
901     /* Return error status */
902     status =  HAL_ERROR;
903   }
904 
905   /* Release Lock */
906   __HAL_UNLOCK(hhcd);
907   return status;
908 }
909 
910 /**
911   * @brief  Register USB HCD Host Channel Notify URB Change Callback
912   *         To be used instead of the weak HAL_HCD_HC_NotifyURBChange_Callback() predefined callback
913   * @param  hhcd HCD handle
914   * @param  pCallback pointer to the USB HCD Host Channel Notify URB Change Callback function
915   * @retval HAL status
916   */
HAL_HCD_RegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef * hhcd,pHCD_HC_NotifyURBChangeCallbackTypeDef pCallback)917 HAL_StatusTypeDef HAL_HCD_RegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef *hhcd,
918                                                              pHCD_HC_NotifyURBChangeCallbackTypeDef pCallback)
919 {
920   HAL_StatusTypeDef status = HAL_OK;
921 
922   if (pCallback == NULL)
923   {
924     /* Update the error code */
925     hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
926 
927     return HAL_ERROR;
928   }
929 
930   /* Process locked */
931   __HAL_LOCK(hhcd);
932 
933   if (hhcd->State == HAL_HCD_STATE_READY)
934   {
935     hhcd->HC_NotifyURBChangeCallback = pCallback;
936   }
937   else
938   {
939     /* Update the error code */
940     hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
941 
942     /* Return error status */
943     status =  HAL_ERROR;
944   }
945 
946   /* Release Lock */
947   __HAL_UNLOCK(hhcd);
948 
949   return status;
950 }
951 
952 /**
953   * @brief  Unregister the USB HCD Host Channel Notify URB Change Callback
954   *         USB HCD Host Channel Notify URB Change Callback is redirected
955   *         to the weak HAL_HCD_HC_NotifyURBChange_Callback() predefined callback
956   * @param  hhcd HCD handle
957   * @retval HAL status
958   */
HAL_HCD_UnRegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef * hhcd)959 HAL_StatusTypeDef HAL_HCD_UnRegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef *hhcd)
960 {
961   HAL_StatusTypeDef status = HAL_OK;
962 
963   /* Process locked */
964   __HAL_LOCK(hhcd);
965 
966   if (hhcd->State == HAL_HCD_STATE_READY)
967   {
968     hhcd->HC_NotifyURBChangeCallback = HAL_HCD_HC_NotifyURBChange_Callback; /* Legacy weak DataOutStageCallback  */
969   }
970   else
971   {
972     /* Update the error code */
973     hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
974 
975     /* Return error status */
976     status =  HAL_ERROR;
977   }
978 
979   /* Release Lock */
980   __HAL_UNLOCK(hhcd);
981 
982   return status;
983 }
984 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
985 
986 /**
987   * @}
988   */
989 
990 /** @defgroup HCD_Exported_Functions_Group3 Peripheral Control functions
991   *  @brief   Management functions
992   *
993 @verbatim
994  ===============================================================================
995                       ##### Peripheral Control functions #####
996  ===============================================================================
997     [..]
998     This subsection provides a set of functions allowing to control the HCD data
999     transfers.
1000 
1001 @endverbatim
1002   * @{
1003   */
1004 
1005 /**
1006   * @brief  Start the host driver.
1007   * @param  hhcd HCD handle
1008   * @retval HAL status
1009   */
HAL_HCD_Start(HCD_HandleTypeDef * hhcd)1010 HAL_StatusTypeDef HAL_HCD_Start(HCD_HandleTypeDef *hhcd)
1011 {
1012   __HAL_LOCK(hhcd);
1013   /* Enable port power */
1014   (void)USB_DriveVbus(hhcd->Instance, 1U);
1015 
1016   /* Enable global interrupt */
1017   __HAL_HCD_ENABLE(hhcd);
1018   __HAL_UNLOCK(hhcd);
1019 
1020   return HAL_OK;
1021 }
1022 
1023 /**
1024   * @brief  Stop the host driver.
1025   * @param  hhcd HCD handle
1026   * @retval HAL status
1027   */
1028 
HAL_HCD_Stop(HCD_HandleTypeDef * hhcd)1029 HAL_StatusTypeDef HAL_HCD_Stop(HCD_HandleTypeDef *hhcd)
1030 {
1031   __HAL_LOCK(hhcd);
1032   (void)USB_StopHost(hhcd->Instance);
1033   __HAL_UNLOCK(hhcd);
1034 
1035   return HAL_OK;
1036 }
1037 
1038 /**
1039   * @brief  Reset the host port.
1040   * @param  hhcd HCD handle
1041   * @retval HAL status
1042   */
HAL_HCD_ResetPort(HCD_HandleTypeDef * hhcd)1043 HAL_StatusTypeDef HAL_HCD_ResetPort(HCD_HandleTypeDef *hhcd)
1044 {
1045   return (USB_ResetPort(hhcd->Instance));
1046 }
1047 
1048 /**
1049   * @}
1050   */
1051 
1052 /** @defgroup HCD_Exported_Functions_Group4 Peripheral State functions
1053   *  @brief   Peripheral State functions
1054   *
1055 @verbatim
1056  ===============================================================================
1057                       ##### Peripheral State functions #####
1058  ===============================================================================
1059     [..]
1060     This subsection permits to get in run-time the status of the peripheral
1061     and the data flow.
1062 
1063 @endverbatim
1064   * @{
1065   */
1066 
1067 /**
1068   * @brief  Return the HCD handle state.
1069   * @param  hhcd HCD handle
1070   * @retval HAL state
1071   */
HAL_HCD_GetState(HCD_HandleTypeDef * hhcd)1072 HCD_StateTypeDef HAL_HCD_GetState(HCD_HandleTypeDef *hhcd)
1073 {
1074   return hhcd->State;
1075 }
1076 
1077 /**
1078   * @brief  Return  URB state for a channel.
1079   * @param  hhcd HCD handle
1080   * @param  chnum Channel number.
1081   *         This parameter can be a value from 1 to 15
1082   * @retval URB state.
1083   *          This parameter can be one of these values:
1084   *            URB_IDLE/
1085   *            URB_DONE/
1086   *            URB_NOTREADY/
1087   *            URB_NYET/
1088   *            URB_ERROR/
1089   *            URB_STALL
1090   */
HAL_HCD_HC_GetURBState(HCD_HandleTypeDef * hhcd,uint8_t chnum)1091 HCD_URBStateTypeDef HAL_HCD_HC_GetURBState(HCD_HandleTypeDef *hhcd, uint8_t chnum)
1092 {
1093   return hhcd->hc[chnum].urb_state;
1094 }
1095 
1096 
1097 /**
1098   * @brief  Return the last host transfer size.
1099   * @param  hhcd HCD handle
1100   * @param  chnum Channel number.
1101   *         This parameter can be a value from 1 to 15
1102   * @retval last transfer size in byte
1103   */
HAL_HCD_HC_GetXferCount(HCD_HandleTypeDef * hhcd,uint8_t chnum)1104 uint32_t HAL_HCD_HC_GetXferCount(HCD_HandleTypeDef *hhcd, uint8_t chnum)
1105 {
1106   return hhcd->hc[chnum].xfer_count;
1107 }
1108 
1109 /**
1110   * @brief  Return the Host Channel state.
1111   * @param  hhcd HCD handle
1112   * @param  chnum Channel number.
1113   *         This parameter can be a value from 1 to 15
1114   * @retval Host channel state
1115   *          This parameter can be one of these values:
1116   *            HC_IDLE/
1117   *            HC_XFRC/
1118   *            HC_HALTED/
1119   *            HC_NYET/
1120   *            HC_NAK/
1121   *            HC_STALL/
1122   *            HC_XACTERR/
1123   *            HC_BBLERR/
1124   *            HC_DATATGLERR
1125   */
HAL_HCD_HC_GetState(HCD_HandleTypeDef * hhcd,uint8_t chnum)1126 HCD_HCStateTypeDef  HAL_HCD_HC_GetState(HCD_HandleTypeDef *hhcd, uint8_t chnum)
1127 {
1128   return hhcd->hc[chnum].state;
1129 }
1130 
1131 /**
1132   * @brief  Return the current Host frame number.
1133   * @param  hhcd HCD handle
1134   * @retval Current Host frame number
1135   */
HAL_HCD_GetCurrentFrame(HCD_HandleTypeDef * hhcd)1136 uint32_t HAL_HCD_GetCurrentFrame(HCD_HandleTypeDef *hhcd)
1137 {
1138   return (USB_GetCurrentFrame(hhcd->Instance));
1139 }
1140 
1141 /**
1142   * @brief  Return the Host enumeration speed.
1143   * @param  hhcd HCD handle
1144   * @retval Enumeration speed
1145   */
HAL_HCD_GetCurrentSpeed(HCD_HandleTypeDef * hhcd)1146 uint32_t HAL_HCD_GetCurrentSpeed(HCD_HandleTypeDef *hhcd)
1147 {
1148   return (USB_GetHostSpeed(hhcd->Instance));
1149 }
1150 
1151 /**
1152   * @}
1153   */
1154 
1155 /**
1156   * @}
1157   */
1158 
1159 /** @addtogroup HCD_Private_Functions
1160   * @{
1161   */
1162 /**
1163   * @brief  Handle Host Channel IN interrupt requests.
1164   * @param  hhcd HCD handle
1165   * @param  chnum Channel number.
1166   *         This parameter can be a value from 1 to 15
1167   * @retval none
1168   */
HCD_HC_IN_IRQHandler(HCD_HandleTypeDef * hhcd,uint8_t chnum)1169 static void HCD_HC_IN_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum)
1170 {
1171   USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
1172   uint32_t USBx_BASE = (uint32_t)USBx;
1173   uint32_t ch_num = (uint32_t)chnum;
1174 
1175   uint32_t tmpreg;
1176 
1177   if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_AHBERR) == USB_OTG_HCINT_AHBERR)
1178   {
1179     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_AHBERR);
1180     hhcd->hc[ch_num].state = HC_XACTERR;
1181     (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1182   }
1183   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_BBERR) == USB_OTG_HCINT_BBERR)
1184   {
1185     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_BBERR);
1186     hhcd->hc[ch_num].state = HC_BBLERR;
1187     (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1188   }
1189   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_ACK) == USB_OTG_HCINT_ACK)
1190   {
1191     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_ACK);
1192   }
1193   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_STALL) == USB_OTG_HCINT_STALL)
1194   {
1195     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_STALL);
1196     hhcd->hc[ch_num].state = HC_STALL;
1197     (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1198   }
1199   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_DTERR) == USB_OTG_HCINT_DTERR)
1200   {
1201     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_DTERR);
1202     hhcd->hc[ch_num].state = HC_DATATGLERR;
1203     (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1204   }
1205   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_TXERR) == USB_OTG_HCINT_TXERR)
1206   {
1207     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_TXERR);
1208     hhcd->hc[ch_num].state = HC_XACTERR;
1209     (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1210   }
1211   else
1212   {
1213     /* ... */
1214   }
1215 
1216   if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_FRMOR) == USB_OTG_HCINT_FRMOR)
1217   {
1218     (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1219     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_FRMOR);
1220   }
1221   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_XFRC) == USB_OTG_HCINT_XFRC)
1222   {
1223     hhcd->hc[ch_num].state = HC_XFRC;
1224     hhcd->hc[ch_num].ErrCnt = 0U;
1225     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_XFRC);
1226 
1227     if ((hhcd->hc[ch_num].ep_type == EP_TYPE_CTRL) ||
1228         (hhcd->hc[ch_num].ep_type == EP_TYPE_BULK))
1229     {
1230       (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1231       __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_NAK);
1232     }
1233     else if ((hhcd->hc[ch_num].ep_type == EP_TYPE_INTR) ||
1234              (hhcd->hc[ch_num].ep_type == EP_TYPE_ISOC))
1235     {
1236       USBx_HC(ch_num)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM;
1237       hhcd->hc[ch_num].urb_state = URB_DONE;
1238 
1239 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
1240       hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
1241 #else
1242       HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
1243 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
1244     }
1245     else
1246     {
1247       /* ... */
1248     }
1249 
1250     if (hhcd->Init.dma_enable == 1U)
1251     {
1252       if (((hhcd->hc[ch_num].XferSize / hhcd->hc[ch_num].max_packet) & 1U) != 0U)
1253       {
1254         hhcd->hc[ch_num].toggle_in ^= 1U;
1255       }
1256     }
1257     else
1258     {
1259       hhcd->hc[ch_num].toggle_in ^= 1U;
1260     }
1261   }
1262   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_CHH) == USB_OTG_HCINT_CHH)
1263   {
1264     if (hhcd->hc[ch_num].state == HC_XFRC)
1265     {
1266       hhcd->hc[ch_num].urb_state = URB_DONE;
1267     }
1268     else if (hhcd->hc[ch_num].state == HC_STALL)
1269     {
1270       hhcd->hc[ch_num].urb_state = URB_STALL;
1271     }
1272     else if ((hhcd->hc[ch_num].state == HC_XACTERR) ||
1273              (hhcd->hc[ch_num].state == HC_DATATGLERR))
1274     {
1275       hhcd->hc[ch_num].ErrCnt++;
1276       if (hhcd->hc[ch_num].ErrCnt > 2U)
1277       {
1278         hhcd->hc[ch_num].ErrCnt = 0U;
1279         hhcd->hc[ch_num].urb_state = URB_ERROR;
1280       }
1281       else
1282       {
1283         hhcd->hc[ch_num].urb_state = URB_NOTREADY;
1284 
1285         /* re-activate the channel */
1286         tmpreg = USBx_HC(ch_num)->HCCHAR;
1287         tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
1288         tmpreg |= USB_OTG_HCCHAR_CHENA;
1289         USBx_HC(ch_num)->HCCHAR = tmpreg;
1290       }
1291     }
1292     else if (hhcd->hc[ch_num].state == HC_NAK)
1293     {
1294       hhcd->hc[ch_num].urb_state  = URB_NOTREADY;
1295 
1296       /* re-activate the channel */
1297       tmpreg = USBx_HC(ch_num)->HCCHAR;
1298       tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
1299       tmpreg |= USB_OTG_HCCHAR_CHENA;
1300       USBx_HC(ch_num)->HCCHAR = tmpreg;
1301     }
1302     else if (hhcd->hc[ch_num].state == HC_BBLERR)
1303     {
1304       hhcd->hc[ch_num].ErrCnt++;
1305       hhcd->hc[ch_num].urb_state = URB_ERROR;
1306     }
1307     else
1308     {
1309       /* ... */
1310     }
1311     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_CHH);
1312 
1313 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
1314     hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
1315 #else
1316     HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
1317 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
1318   }
1319   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_NAK) == USB_OTG_HCINT_NAK)
1320   {
1321     if (hhcd->hc[ch_num].ep_type == EP_TYPE_INTR)
1322     {
1323       hhcd->hc[ch_num].ErrCnt = 0U;
1324       (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1325     }
1326     else if ((hhcd->hc[ch_num].ep_type == EP_TYPE_CTRL) ||
1327              (hhcd->hc[ch_num].ep_type == EP_TYPE_BULK))
1328     {
1329       hhcd->hc[ch_num].ErrCnt = 0U;
1330       hhcd->hc[ch_num].state = HC_NAK;
1331       (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1332     }
1333     else
1334     {
1335       /* ... */
1336     }
1337     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_NAK);
1338   }
1339   else
1340   {
1341     /* ... */
1342   }
1343 }
1344 
1345 /**
1346   * @brief  Handle Host Channel OUT interrupt requests.
1347   * @param  hhcd HCD handle
1348   * @param  chnum Channel number.
1349   *         This parameter can be a value from 1 to 15
1350   * @retval none
1351   */
HCD_HC_OUT_IRQHandler(HCD_HandleTypeDef * hhcd,uint8_t chnum)1352 static void HCD_HC_OUT_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum)
1353 {
1354   USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
1355   uint32_t USBx_BASE = (uint32_t)USBx;
1356   uint32_t ch_num = (uint32_t)chnum;
1357   uint32_t tmpreg;
1358   uint32_t num_packets;
1359 
1360   if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_AHBERR) == USB_OTG_HCINT_AHBERR)
1361   {
1362     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_AHBERR);
1363     hhcd->hc[ch_num].state = HC_XACTERR;
1364     (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1365   }
1366   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_ACK) == USB_OTG_HCINT_ACK)
1367   {
1368     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_ACK);
1369 
1370     if (hhcd->hc[ch_num].do_ping == 1U)
1371     {
1372       hhcd->hc[ch_num].do_ping = 0U;
1373       hhcd->hc[ch_num].urb_state = URB_NOTREADY;
1374       (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1375     }
1376   }
1377   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_FRMOR) == USB_OTG_HCINT_FRMOR)
1378   {
1379     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_FRMOR);
1380     (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1381   }
1382   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_XFRC) == USB_OTG_HCINT_XFRC)
1383   {
1384     hhcd->hc[ch_num].ErrCnt = 0U;
1385 
1386     /* transaction completed with NYET state, update do ping state */
1387     if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_NYET) == USB_OTG_HCINT_NYET)
1388     {
1389       hhcd->hc[ch_num].do_ping = 1U;
1390       __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_NYET);
1391     }
1392     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_XFRC);
1393     hhcd->hc[ch_num].state = HC_XFRC;
1394     (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1395   }
1396   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_NYET) == USB_OTG_HCINT_NYET)
1397   {
1398     hhcd->hc[ch_num].state = HC_NYET;
1399     hhcd->hc[ch_num].do_ping = 1U;
1400     hhcd->hc[ch_num].ErrCnt = 0U;
1401     (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1402     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_NYET);
1403   }
1404   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_STALL) == USB_OTG_HCINT_STALL)
1405   {
1406     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_STALL);
1407     hhcd->hc[ch_num].state = HC_STALL;
1408     (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1409   }
1410   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_NAK) == USB_OTG_HCINT_NAK)
1411   {
1412     hhcd->hc[ch_num].ErrCnt = 0U;
1413     hhcd->hc[ch_num].state = HC_NAK;
1414 
1415     (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1416     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_NAK);
1417   }
1418   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_TXERR) == USB_OTG_HCINT_TXERR)
1419   {
1420     hhcd->hc[ch_num].state = HC_XACTERR;
1421     (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1422     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_TXERR);
1423   }
1424   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_DTERR) == USB_OTG_HCINT_DTERR)
1425   {
1426     hhcd->hc[ch_num].state = HC_DATATGLERR;
1427     (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1428     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_DTERR);
1429   }
1430   else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_CHH) == USB_OTG_HCINT_CHH)
1431   {
1432     if (hhcd->hc[ch_num].state == HC_XFRC)
1433     {
1434       hhcd->hc[ch_num].urb_state  = URB_DONE;
1435       if ((hhcd->hc[ch_num].ep_type == EP_TYPE_BULK) ||
1436           (hhcd->hc[ch_num].ep_type == EP_TYPE_INTR))
1437       {
1438         if (hhcd->Init.dma_enable == 0U)
1439         {
1440           hhcd->hc[ch_num].toggle_out ^= 1U;
1441         }
1442 
1443         if ((hhcd->Init.dma_enable == 1U) && (hhcd->hc[ch_num].xfer_len > 0U))
1444         {
1445           num_packets = (hhcd->hc[ch_num].xfer_len + hhcd->hc[ch_num].max_packet - 1U) / hhcd->hc[ch_num].max_packet;
1446 
1447           if ((num_packets & 1U) != 0U)
1448           {
1449             hhcd->hc[ch_num].toggle_out ^= 1U;
1450           }
1451         }
1452       }
1453     }
1454     else if (hhcd->hc[ch_num].state == HC_NAK)
1455     {
1456       hhcd->hc[ch_num].urb_state = URB_NOTREADY;
1457     }
1458     else if (hhcd->hc[ch_num].state == HC_NYET)
1459     {
1460       hhcd->hc[ch_num].urb_state  = URB_NOTREADY;
1461     }
1462     else if (hhcd->hc[ch_num].state == HC_STALL)
1463     {
1464       hhcd->hc[ch_num].urb_state  = URB_STALL;
1465     }
1466     else if ((hhcd->hc[ch_num].state == HC_XACTERR) ||
1467              (hhcd->hc[ch_num].state == HC_DATATGLERR))
1468     {
1469       hhcd->hc[ch_num].ErrCnt++;
1470       if (hhcd->hc[ch_num].ErrCnt > 2U)
1471       {
1472         hhcd->hc[ch_num].ErrCnt = 0U;
1473         hhcd->hc[ch_num].urb_state = URB_ERROR;
1474       }
1475       else
1476       {
1477         hhcd->hc[ch_num].urb_state = URB_NOTREADY;
1478 
1479         /* re-activate the channel  */
1480         tmpreg = USBx_HC(ch_num)->HCCHAR;
1481         tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
1482         tmpreg |= USB_OTG_HCCHAR_CHENA;
1483         USBx_HC(ch_num)->HCCHAR = tmpreg;
1484       }
1485     }
1486     else
1487     {
1488       /* ... */
1489     }
1490 
1491     __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_CHH);
1492 
1493 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
1494     hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
1495 #else
1496     HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
1497 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
1498   }
1499   else
1500   {
1501     /* ... */
1502   }
1503 }
1504 
1505 /**
1506   * @brief  Handle Rx Queue Level interrupt requests.
1507   * @param  hhcd HCD handle
1508   * @retval none
1509   */
HCD_RXQLVL_IRQHandler(HCD_HandleTypeDef * hhcd)1510 static void HCD_RXQLVL_IRQHandler(HCD_HandleTypeDef *hhcd)
1511 {
1512   USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
1513   uint32_t USBx_BASE = (uint32_t)USBx;
1514   uint32_t pktsts;
1515   uint32_t pktcnt;
1516   uint32_t GrxstspReg;
1517   uint32_t xferSizePktCnt;
1518   uint32_t tmpreg;
1519   uint32_t ch_num;
1520 
1521   GrxstspReg = hhcd->Instance->GRXSTSP;
1522   ch_num = GrxstspReg & USB_OTG_GRXSTSP_EPNUM;
1523   pktsts = (GrxstspReg & USB_OTG_GRXSTSP_PKTSTS) >> 17;
1524   pktcnt = (GrxstspReg & USB_OTG_GRXSTSP_BCNT) >> 4;
1525 
1526   switch (pktsts)
1527   {
1528     case GRXSTS_PKTSTS_IN:
1529       /* Read the data into the host buffer. */
1530       if ((pktcnt > 0U) && (hhcd->hc[ch_num].xfer_buff != (void *)0))
1531       {
1532         if ((hhcd->hc[ch_num].xfer_count + pktcnt) <= hhcd->hc[ch_num].xfer_len)
1533         {
1534           (void)USB_ReadPacket(hhcd->Instance,
1535                                hhcd->hc[ch_num].xfer_buff, (uint16_t)pktcnt);
1536 
1537           /* manage multiple Xfer */
1538           hhcd->hc[ch_num].xfer_buff += pktcnt;
1539           hhcd->hc[ch_num].xfer_count += pktcnt;
1540 
1541           /* get transfer size packet count */
1542           xferSizePktCnt = (USBx_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_PKTCNT) >> 19;
1543 
1544           if ((hhcd->hc[ch_num].max_packet == pktcnt) && (xferSizePktCnt > 0U))
1545           {
1546             /* re-activate the channel when more packets are expected */
1547             tmpreg = USBx_HC(ch_num)->HCCHAR;
1548             tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
1549             tmpreg |= USB_OTG_HCCHAR_CHENA;
1550             USBx_HC(ch_num)->HCCHAR = tmpreg;
1551             hhcd->hc[ch_num].toggle_in ^= 1U;
1552           }
1553         }
1554         else
1555         {
1556           hhcd->hc[ch_num].urb_state = URB_ERROR;
1557         }
1558       }
1559       break;
1560 
1561     case GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
1562       break;
1563 
1564     case GRXSTS_PKTSTS_IN_XFER_COMP:
1565     case GRXSTS_PKTSTS_CH_HALTED:
1566     default:
1567       break;
1568   }
1569 }
1570 
1571 /**
1572   * @brief  Handle Host Port interrupt requests.
1573   * @param  hhcd HCD handle
1574   * @retval None
1575   */
HCD_Port_IRQHandler(HCD_HandleTypeDef * hhcd)1576 static void HCD_Port_IRQHandler(HCD_HandleTypeDef *hhcd)
1577 {
1578   USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
1579   uint32_t USBx_BASE = (uint32_t)USBx;
1580   __IO uint32_t hprt0;
1581   __IO uint32_t hprt0_dup;
1582 
1583   /* Handle Host Port Interrupts */
1584   hprt0 = USBx_HPRT0;
1585   hprt0_dup = USBx_HPRT0;
1586 
1587   hprt0_dup &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET | \
1588                  USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
1589 
1590   /* Check whether Port Connect detected */
1591   if ((hprt0 & USB_OTG_HPRT_PCDET) == USB_OTG_HPRT_PCDET)
1592   {
1593     if ((hprt0 & USB_OTG_HPRT_PCSTS) == USB_OTG_HPRT_PCSTS)
1594     {
1595 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
1596       hhcd->ConnectCallback(hhcd);
1597 #else
1598       HAL_HCD_Connect_Callback(hhcd);
1599 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
1600     }
1601     hprt0_dup |= USB_OTG_HPRT_PCDET;
1602   }
1603 
1604   /* Check whether Port Enable Changed */
1605   if ((hprt0 & USB_OTG_HPRT_PENCHNG) == USB_OTG_HPRT_PENCHNG)
1606   {
1607     hprt0_dup |= USB_OTG_HPRT_PENCHNG;
1608 
1609     if ((hprt0 & USB_OTG_HPRT_PENA) == USB_OTG_HPRT_PENA)
1610     {
1611       if (hhcd->Init.phy_itface  == USB_OTG_EMBEDDED_PHY)
1612       {
1613         if ((hprt0 & USB_OTG_HPRT_PSPD) == (HPRT0_PRTSPD_LOW_SPEED << 17))
1614         {
1615           (void)USB_InitFSLSPClkSel(hhcd->Instance, HCFG_6_MHZ);
1616         }
1617         else
1618         {
1619           (void)USB_InitFSLSPClkSel(hhcd->Instance, HCFG_48_MHZ);
1620         }
1621       }
1622       else
1623       {
1624         if (hhcd->Init.speed == HCD_SPEED_FULL)
1625         {
1626           USBx_HOST->HFIR = 60000U;
1627         }
1628       }
1629 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
1630       hhcd->PortEnabledCallback(hhcd);
1631 #else
1632       HAL_HCD_PortEnabled_Callback(hhcd);
1633 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
1634 
1635     }
1636     else
1637     {
1638 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
1639       hhcd->PortDisabledCallback(hhcd);
1640 #else
1641       HAL_HCD_PortDisabled_Callback(hhcd);
1642 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
1643     }
1644   }
1645 
1646   /* Check for an overcurrent */
1647   if ((hprt0 & USB_OTG_HPRT_POCCHNG) == USB_OTG_HPRT_POCCHNG)
1648   {
1649     hprt0_dup |= USB_OTG_HPRT_POCCHNG;
1650   }
1651 
1652   /* Clear Port Interrupts */
1653   USBx_HPRT0 = hprt0_dup;
1654 }
1655 
1656 /**
1657   * @}
1658   */
1659 
1660 /**
1661   * @}
1662   */
1663 
1664 #endif /* defined (USB_OTG_FS) */
1665 #endif /* HAL_HCD_MODULE_ENABLED */
1666 
1667 /**
1668   * @}
1669   */
1670 
1671 /**
1672   * @}
1673   */
1674