source: S-port/trunk/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hcd.c

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