2 ******************************************************************************
3 * @file stm32f1xx_hal_hcd.c
4 * @author MCD Application Team
6 * @date 15-December-2014
7 * @brief HCD HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the USB Peripheral Controller:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral Control functions
13 * + Peripheral State functions
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
20 (#)Declare a HCD_HandleTypeDef handle structure, for example:
21 HCD_HandleTypeDef hhcd;
23 (#)Fill parameters of Init structure in HCD handle
25 (#)Call HAL_HCD_Init() API to initialize the HCD peripheral (Core, Host core, ...)
27 (#)Initialize the HCD low level resources through the HAL_HCD_MspInit() API:
28 (##) Enable the HCD/USB Low Level interface clock using the following macro
29 (+++) __HAL_RCC_USB_OTG_FS_CLK_ENABLE()
31 (##) Initialize the related GPIO clocks
32 (##) Configure HCD pin-out
33 (##) Configure HCD NVIC interrupt
35 (#)Associate the Upper USB Host stack to the HAL HCD Driver:
36 (##) hhcd.pData = phost;
38 (#)Enable HCD transmission and reception:
42 ******************************************************************************
45 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
47 * Redistribution and use in source and binary forms, with or without modification,
48 * are permitted provided that the following conditions are met:
49 * 1. Redistributions of source code must retain the above copyright notice,
50 * this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright notice,
52 * this list of conditions and the following disclaimer in the documentation
53 * and/or other materials provided with the distribution.
54 * 3. Neither the name of STMicroelectronics nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
59 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
61 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
64 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
65 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
66 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
67 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 ******************************************************************************
72 /* Includes ------------------------------------------------------------------*/
73 #include "stm32f1xx_hal.h"
74 /** @addtogroup STM32F1xx_HAL_Driver
79 #ifdef HAL_HCD_MODULE_ENABLED
81 #if defined(STM32F105xC) || defined(STM32F107xC)
84 * @brief HCD HAL module driver
88 /* Private types -------------------------------------------------------------*/
89 /* Private variables ---------------------------------------------------------*/
90 /* Private constants ---------------------------------------------------------*/
91 /* Private macros ------------------------------------------------------------*/
92 /* Private function ----------------------------------------------------------*/
93 /** @defgroup HCD_Private_Functions HCD Private Functions
96 static void HCD_HC_IN_IRQHandler(HCD_HandleTypeDef
*hhcd
, uint8_t chnum
);
97 static void HCD_HC_OUT_IRQHandler(HCD_HandleTypeDef
*hhcd
, uint8_t chnum
);
98 static void HCD_RXQLVL_IRQHandler(HCD_HandleTypeDef
*hhcd
);
99 static void HCD_Port_IRQHandler(HCD_HandleTypeDef
*hhcd
);
104 /* Exported functions --------------------------------------------------------*/
105 /** @defgroup HCD_Exported_Functions HCD Exported Functions
109 /** @defgroup HCD_Exported_Functions_Group1 Initialization and de-initialization functions
110 * @brief Initialization and Configuration functions
113 ===============================================================================
114 ##### Initialization and de-initialization functions #####
115 ===============================================================================
116 [..] This section provides functions allowing to:
123 * @brief Initialize the host driver
124 * @param hhcd: HCD handle
127 HAL_StatusTypeDef
HAL_HCD_Init(HCD_HandleTypeDef
*hhcd
)
129 /* Check the HCD handle allocation */
135 /* Check the parameters */
136 assert_param(IS_HCD_ALL_INSTANCE(hhcd
->Instance
));
138 if(hhcd
->State
== HAL_HCD_STATE_RESET
)
140 /* Allocate lock resource and initialize it */
141 hhcd
-> Lock
= HAL_UNLOCKED
;
143 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
144 HAL_HCD_MspInit(hhcd
);
147 hhcd
->State
= HAL_HCD_STATE_BUSY
;
149 /* Disable the Interrupts */
150 __HAL_HCD_DISABLE(hhcd
);
152 /*Init the Core (common init.) */
153 USB_CoreInit(hhcd
->Instance
, hhcd
->Init
);
156 USB_SetCurrentMode(hhcd
->Instance
, USB_HOST_MODE
);
159 USB_HostInit(hhcd
->Instance
, hhcd
->Init
);
161 hhcd
->State
= HAL_HCD_STATE_READY
;
167 * @brief Initialize a host channel
168 * @param hhcd: HCD handle
169 * @param ch_num: Channel number.
170 * This parameter can be a value from 1 to 15
171 * @param epnum: Endpoint number.
172 * This parameter can be a value from 1 to 15
173 * @param dev_address : Current device address
174 * This parameter can be a value from 0 to 255
175 * @param speed: Current device speed.
176 * This parameter can be one of these values:
177 * HCD_SPEED_FULL: Full speed mode,
178 * HCD_SPEED_LOW: Low speed mode
179 * @param ep_type: Endpoint Type.
180 * This parameter can be one of these values:
181 * EP_TYPE_CTRL: Control type,
182 * EP_TYPE_ISOC: Isochronous type,
183 * EP_TYPE_BULK: Bulk type,
184 * EP_TYPE_INTR: Interrupt type
185 * @param mps: Max Packet Size.
186 * This parameter can be a value from 0 to32K
189 HAL_StatusTypeDef
HAL_HCD_HC_Init(HCD_HandleTypeDef
*hhcd
,
197 HAL_StatusTypeDef status
= HAL_OK
;
201 hhcd
->hc
[ch_num
].dev_addr
= dev_address
;
202 hhcd
->hc
[ch_num
].max_packet
= mps
;
203 hhcd
->hc
[ch_num
].ch_num
= ch_num
;
204 hhcd
->hc
[ch_num
].ep_type
= ep_type
;
205 hhcd
->hc
[ch_num
].ep_num
= epnum
& 0x7F;
206 hhcd
->hc
[ch_num
].ep_is_in
= ((epnum
& 0x80) == 0x80);
207 hhcd
->hc
[ch_num
].speed
= speed
;
209 status
= USB_HC_Init(hhcd
->Instance
,
222 * @brief Halt a host channel
223 * @param hhcd: HCD handle
224 * @param ch_num: Channel number.
225 * This parameter can be a value from 1 to 15
228 HAL_StatusTypeDef
HAL_HCD_HC_Halt(HCD_HandleTypeDef
*hhcd
,
232 USB_HC_Halt(hhcd
->Instance
, ch_num
);
239 * @brief DeInitialize the host driver
240 * @param hhcd: HCD handle
243 HAL_StatusTypeDef
HAL_HCD_DeInit(HCD_HandleTypeDef
*hhcd
)
245 /* Check the HCD handle allocation */
251 hhcd
->State
= HAL_HCD_STATE_BUSY
;
253 /* DeInit the low level hardware */
254 HAL_HCD_MspDeInit(hhcd
);
256 __HAL_HCD_DISABLE(hhcd
);
258 hhcd
->State
= HAL_HCD_STATE_RESET
;
264 * @brief Initializes the HCD MSP.
265 * @param hhcd: HCD handle
268 __weak
void HAL_HCD_MspInit(HCD_HandleTypeDef
*hhcd
)
270 /* NOTE : This function Should not be modified, when the callback is needed,
271 the HAL_HCD_MspInit could be implemented in the user file
276 * @brief DeInitializes HCD MSP.
277 * @param hhcd: HCD handle
280 __weak
void HAL_HCD_MspDeInit(HCD_HandleTypeDef
*hhcd
)
282 /* NOTE : This function Should not be modified, when the callback is needed,
283 the HAL_HCD_MspDeInit could be implemented in the user file
291 /** @defgroup HCD_Exported_Functions_Group2 IO operation functions
292 * @brief HCD IO operation functions
295 ===============================================================================
296 ##### IO operation functions #####
297 ===============================================================================
298 This subsection provides a set of functions allowing to manage the USB Host Data
306 * @brief Submit a new URB for processing
307 * @param hhcd: HCD handle
308 * @param ch_num: Channel number.
309 * This parameter can be a value from 1 to 15
310 * @param direction: Channel number.
311 * This parameter can be one of these values:
312 * 0 : Output / 1 : Input
313 * @param ep_type: Endpoint Type.
314 * This parameter can be one of these values:
315 * EP_TYPE_CTRL: Control type/
316 * EP_TYPE_ISOC: Isochronous type/
317 * EP_TYPE_BULK: Bulk type/
318 * EP_TYPE_INTR: Interrupt type/
319 * @param token: Endpoint Type.
320 * This parameter can be one of these values:
321 * 0: HC_PID_SETUP / 1: HC_PID_DATA1
322 * @param pbuff: pointer to URB data
323 * @param length: Length of URB data
324 * @param do_ping: activate do ping protocol (for high speed only).
325 * This parameter can be one of these values:
326 * 0 : do ping inactive / 1 : do ping active
329 HAL_StatusTypeDef
HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef
*hhcd
,
338 hhcd
->hc
[ch_num
].ep_is_in
= direction
;
339 hhcd
->hc
[ch_num
].ep_type
= ep_type
;
343 hhcd
->hc
[ch_num
].data_pid
= HC_PID_SETUP
;
347 hhcd
->hc
[ch_num
].data_pid
= HC_PID_DATA1
;
350 /* Manage Data Toggle */
354 if((token
== 1) && (direction
== 0)) /*send data */
357 { /* For Status OUT stage, Length==0, Status Out PID = 1 */
358 hhcd
->hc
[ch_num
].toggle_out
= 1;
361 /* Set the Data Toggle bit as per the Flag */
362 if ( hhcd
->hc
[ch_num
].toggle_out
== 0)
363 { /* Put the PID 0 */
364 hhcd
->hc
[ch_num
].data_pid
= HC_PID_DATA0
;
367 { /* Put the PID 1 */
368 hhcd
->hc
[ch_num
].data_pid
= HC_PID_DATA1
;
370 if(hhcd
->hc
[ch_num
].urb_state
!= URB_NOTREADY
)
372 hhcd
->hc
[ch_num
].do_ping
= do_ping
;
380 /* Set the Data Toggle bit as per the Flag */
381 if ( hhcd
->hc
[ch_num
].toggle_out
== 0)
382 { /* Put the PID 0 */
383 hhcd
->hc
[ch_num
].data_pid
= HC_PID_DATA0
;
386 { /* Put the PID 1 */
387 hhcd
->hc
[ch_num
].data_pid
= HC_PID_DATA1
;
389 if(hhcd
->hc
[ch_num
].urb_state
!= URB_NOTREADY
)
391 hhcd
->hc
[ch_num
].do_ping
= do_ping
;
396 if( hhcd
->hc
[ch_num
].toggle_in
== 0)
398 hhcd
->hc
[ch_num
].data_pid
= HC_PID_DATA0
;
402 hhcd
->hc
[ch_num
].data_pid
= HC_PID_DATA1
;
410 /* Set the Data Toggle bit as per the Flag */
411 if ( hhcd
->hc
[ch_num
].toggle_out
== 0)
412 { /* Put the PID 0 */
413 hhcd
->hc
[ch_num
].data_pid
= HC_PID_DATA0
;
416 { /* Put the PID 1 */
417 hhcd
->hc
[ch_num
].data_pid
= HC_PID_DATA1
;
422 if( hhcd
->hc
[ch_num
].toggle_in
== 0)
424 hhcd
->hc
[ch_num
].data_pid
= HC_PID_DATA0
;
428 hhcd
->hc
[ch_num
].data_pid
= HC_PID_DATA1
;
434 hhcd
->hc
[ch_num
].data_pid
= HC_PID_DATA0
;
438 hhcd
->hc
[ch_num
].xfer_buff
= pbuff
;
439 hhcd
->hc
[ch_num
].xfer_len
= length
;
440 hhcd
->hc
[ch_num
].urb_state
= URB_IDLE
;
441 hhcd
->hc
[ch_num
].xfer_count
= 0 ;
442 hhcd
->hc
[ch_num
].ch_num
= ch_num
;
443 hhcd
->hc
[ch_num
].state
= HC_IDLE
;
445 return USB_HC_StartXfer(hhcd
->Instance
, &(hhcd
->hc
[ch_num
]));
449 * @brief This function handles HCD interrupt request.
450 * @param hhcd: HCD handle
453 void HAL_HCD_IRQHandler(HCD_HandleTypeDef
*hhcd
)
455 USB_OTG_GlobalTypeDef
*USBx
= hhcd
->Instance
;
457 uint32_t index
= 0 , interrupt
= 0;
459 /* ensure that we are in device mode */
460 if (USB_GetMode(hhcd
->Instance
) == USB_OTG_MODE_HOST
)
462 /* avoid spurious interrupt */
463 if(__HAL_HCD_IS_INVALID_INTERRUPT(hhcd
))
468 if(__HAL_HCD_GET_FLAG(hhcd
, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT
))
470 /* incorrect mode, acknowledge the interrupt */
471 __HAL_HCD_CLEAR_FLAG(hhcd
, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT
);
474 if(__HAL_HCD_GET_FLAG(hhcd
, USB_OTG_GINTSTS_IISOIXFR
))
476 /* incorrect mode, acknowledge the interrupt */
477 __HAL_HCD_CLEAR_FLAG(hhcd
, USB_OTG_GINTSTS_IISOIXFR
);
480 if(__HAL_HCD_GET_FLAG(hhcd
, USB_OTG_GINTSTS_PTXFE
))
482 /* incorrect mode, acknowledge the interrupt */
483 __HAL_HCD_CLEAR_FLAG(hhcd
, USB_OTG_GINTSTS_PTXFE
);
486 if(__HAL_HCD_GET_FLAG(hhcd
, USB_OTG_GINTSTS_MMIS
))
488 /* incorrect mode, acknowledge the interrupt */
489 __HAL_HCD_CLEAR_FLAG(hhcd
, USB_OTG_GINTSTS_MMIS
);
492 /* Handle Host Disconnect Interrupts */
493 if(__HAL_HCD_GET_FLAG(hhcd
, USB_OTG_GINTSTS_DISCINT
))
497 USBx_HPRT0
&= ~(USB_OTG_HPRT_PENA
| USB_OTG_HPRT_PCDET
|\
498 USB_OTG_HPRT_PENCHNG
| USB_OTG_HPRT_POCCHNG
);
500 /* Handle Host Port Interrupts */
501 HAL_HCD_Disconnect_Callback(hhcd
);
502 USB_InitFSLSPClkSel(hhcd
->Instance
,HCFG_48_MHZ
);
503 __HAL_HCD_CLEAR_FLAG(hhcd
, USB_OTG_GINTSTS_DISCINT
);
506 /* Handle Host Port Interrupts */
507 if(__HAL_HCD_GET_FLAG(hhcd
, USB_OTG_GINTSTS_HPRTINT
))
509 HCD_Port_IRQHandler (hhcd
);
512 /* Handle Host SOF Interrupts */
513 if(__HAL_HCD_GET_FLAG(hhcd
, USB_OTG_GINTSTS_SOF
))
515 HAL_HCD_SOF_Callback(hhcd
);
516 __HAL_HCD_CLEAR_FLAG(hhcd
, USB_OTG_GINTSTS_SOF
);
519 /* Handle Host channel Interrupts */
520 if(__HAL_HCD_GET_FLAG(hhcd
, USB_OTG_GINTSTS_HCINT
))
522 interrupt
= USB_HC_ReadInterrupt(hhcd
->Instance
);
523 for (index
= 0; index
< hhcd
->Init
.Host_channels
; index
++)
525 if (interrupt
& (1 << index
))
527 if ((USBx_HC(index
)->HCCHAR
) & USB_OTG_HCCHAR_EPDIR
)
529 HCD_HC_IN_IRQHandler (hhcd
, index
);
533 HCD_HC_OUT_IRQHandler (hhcd
, index
);
537 __HAL_HCD_CLEAR_FLAG(hhcd
, USB_OTG_GINTSTS_HCINT
);
540 /* Handle Rx Queue Level Interrupts */
541 if(__HAL_HCD_GET_FLAG(hhcd
, USB_OTG_GINTSTS_RXFLVL
))
543 USB_MASK_INTERRUPT(hhcd
->Instance
, USB_OTG_GINTSTS_RXFLVL
);
545 HCD_RXQLVL_IRQHandler (hhcd
);
547 USB_UNMASK_INTERRUPT(hhcd
->Instance
, USB_OTG_GINTSTS_RXFLVL
);
554 * @brief SOF callback.
555 * @param hhcd: HCD handle
558 __weak
void HAL_HCD_SOF_Callback(HCD_HandleTypeDef
*hhcd
)
560 /* NOTE : This function Should not be modified, when the callback is needed,
561 the HAL_HCD_SOF_Callback could be implemented in the user file
566 * @brief Connexion Event callback.
567 * @param hhcd: HCD handle
570 __weak
void HAL_HCD_Connect_Callback(HCD_HandleTypeDef
*hhcd
)
572 /* NOTE : This function Should not be modified, when the callback is needed,
573 the HAL_HCD_Connect_Callback could be implemented in the user file
578 * @brief Disonnexion Event callback.
579 * @param hhcd: HCD handle
582 __weak
void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef
*hhcd
)
584 /* NOTE : This function Should not be modified, when the callback is needed,
585 the HAL_HCD_Disconnect_Callback could be implemented in the user file
590 * @brief Notify URB state change callback.
591 * @param hhcd: HCD handle
592 * @param chnum: Channel number.
593 * This parameter can be a value from 1 to 15
595 * This parameter can be one of these values:
604 __weak
void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef
*hhcd
, uint8_t chnum
, HCD_URBStateTypeDef urb_state
)
606 /* NOTE : This function Should not be modified, when the callback is needed,
607 the HAL_HCD_HC_NotifyURBChange_Callback could be implemented in the user file
615 /** @defgroup HCD_Exported_Functions_Group3 Peripheral Control functions
616 * @brief management functions
619 ===============================================================================
620 ##### Peripheral Control functions #####
621 ===============================================================================
623 This subsection provides a set of functions allowing to control the HCD data
631 * @brief Start the host driver
632 * @param hhcd: HCD handle
635 HAL_StatusTypeDef
HAL_HCD_Start(HCD_HandleTypeDef
*hhcd
)
638 __HAL_HCD_ENABLE(hhcd
);
639 USB_DriveVbus(hhcd
->Instance
, 1);
645 * @brief Stop the host driver
646 * @param hhcd: HCD handle
650 HAL_StatusTypeDef
HAL_HCD_Stop(HCD_HandleTypeDef
*hhcd
)
653 USB_StopHost(hhcd
->Instance
);
659 * @brief Reset the host port
660 * @param hhcd: HCD handle
663 HAL_StatusTypeDef
HAL_HCD_ResetPort(HCD_HandleTypeDef
*hhcd
)
665 return (USB_ResetPort(hhcd
->Instance
));
672 /** @defgroup HCD_Exported_Functions_Group4 Peripheral State functions
673 * @brief Peripheral State functions
676 ===============================================================================
677 ##### Peripheral State functions #####
678 ===============================================================================
680 This subsection permits to get in run-time the status of the peripheral
688 * @brief Return the HCD state
689 * @param hhcd: HCD handle
692 HCD_StateTypeDef
HAL_HCD_GetState(HCD_HandleTypeDef
*hhcd
)
698 * @brief Return URB state for a channel
699 * @param hhcd: HCD handle
700 * @param chnum: Channel number.
701 * This parameter can be a value from 1 to 15
703 * This parameter can be one of these values:
711 HCD_URBStateTypeDef
HAL_HCD_HC_GetURBState(HCD_HandleTypeDef
*hhcd
, uint8_t chnum
)
713 return hhcd
->hc
[chnum
].urb_state
;
718 * @brief Return the last host transfer size
719 * @param hhcd: HCD handle
720 * @param chnum: Channel number.
721 * This parameter can be a value from 1 to 15
722 * @retval last transfer size in byte
724 uint32_t HAL_HCD_HC_GetXferCount(HCD_HandleTypeDef
*hhcd
, uint8_t chnum
)
726 return hhcd
->hc
[chnum
].xfer_count
;
730 * @brief Return the Host Channel state
731 * @param hhcd: HCD handle
732 * @param chnum: Channel number.
733 * This parameter can be a value from 1 to 15
734 * @retval Host channel state
735 * This parameter can be one of the these values:
746 HCD_HCStateTypeDef
HAL_HCD_HC_GetState(HCD_HandleTypeDef
*hhcd
, uint8_t chnum
)
748 return hhcd
->hc
[chnum
].state
;
752 * @brief Return the current Host frame number
753 * @param hhcd: HCD handle
754 * @retval Current Host frame number
756 uint32_t HAL_HCD_GetCurrentFrame(HCD_HandleTypeDef
*hhcd
)
758 return (USB_GetCurrentFrame(hhcd
->Instance
));
762 * @brief Return the Host enumeration speed
763 * @param hhcd: HCD handle
764 * @retval Enumeration speed
766 uint32_t HAL_HCD_GetCurrentSpeed(HCD_HandleTypeDef
*hhcd
)
768 return (USB_GetHostSpeed(hhcd
->Instance
));
778 /** @addtogroup HCD_Private_Functions
782 * @brief This function handles Host Channel IN interrupt requests.
783 * @param hhcd: HCD handle
784 * @param chnum: Channel number.
785 * This parameter can be a value from 1 to 15
788 static void HCD_HC_IN_IRQHandler (HCD_HandleTypeDef
*hhcd
, uint8_t chnum
)
790 USB_OTG_GlobalTypeDef
*USBx
= hhcd
->Instance
;
792 if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_AHBERR
)
794 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_AHBERR
);
795 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
797 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_ACK
)
799 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_ACK
);
802 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_STALL
)
804 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
805 hhcd
->hc
[chnum
].state
= HC_STALL
;
806 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_NAK
);
807 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_STALL
);
808 USB_HC_Halt(hhcd
->Instance
, chnum
);
810 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_DTERR
)
812 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
813 USB_HC_Halt(hhcd
->Instance
, chnum
);
814 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_NAK
);
815 hhcd
->hc
[chnum
].state
= HC_DATATGLERR
;
816 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_DTERR
);
819 if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_FRMOR
)
821 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
822 USB_HC_Halt(hhcd
->Instance
, chnum
);
823 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_FRMOR
);
826 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_XFRC
)
828 hhcd
->hc
[chnum
].state
= HC_XFRC
;
829 hhcd
->hc
[chnum
].ErrCnt
= 0;
830 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_XFRC
);
832 if ((hhcd
->hc
[chnum
].ep_type
== EP_TYPE_CTRL
)||
833 (hhcd
->hc
[chnum
].ep_type
== EP_TYPE_BULK
))
835 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
836 USB_HC_Halt(hhcd
->Instance
, chnum
);
837 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_NAK
);
840 else if(hhcd
->hc
[chnum
].ep_type
== EP_TYPE_INTR
)
842 USBx_HC(chnum
)->HCCHAR
|= USB_OTG_HCCHAR_ODDFRM
;
843 hhcd
->hc
[chnum
].urb_state
= URB_DONE
;
844 HAL_HCD_HC_NotifyURBChange_Callback(hhcd
, chnum
, hhcd
->hc
[chnum
].urb_state
);
846 hhcd
->hc
[chnum
].toggle_in
^= 1;
849 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_CHH
)
851 __HAL_HCD_MASK_HALT_HC_INT(chnum
);
853 if(hhcd
->hc
[chnum
].state
== HC_XFRC
)
855 hhcd
->hc
[chnum
].urb_state
= URB_DONE
;
857 else if (hhcd
->hc
[chnum
].state
== HC_STALL
)
859 hhcd
->hc
[chnum
].urb_state
= URB_STALL
;
861 else if((hhcd
->hc
[chnum
].state
== HC_XACTERR
) ||
862 (hhcd
->hc
[chnum
].state
== HC_DATATGLERR
))
864 if(hhcd
->hc
[chnum
].ErrCnt
++ > 3)
866 hhcd
->hc
[chnum
].ErrCnt
= 0;
867 hhcd
->hc
[chnum
].urb_state
= URB_ERROR
;
871 hhcd
->hc
[chnum
].urb_state
= URB_NOTREADY
;
874 /* re-activate the channel */
875 USBx_HC(chnum
)->HCCHAR
&= ~USB_OTG_HCCHAR_CHDIS
;
876 USBx_HC(chnum
)->HCCHAR
|= USB_OTG_HCCHAR_CHENA
;
878 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_CHH
);
879 HAL_HCD_HC_NotifyURBChange_Callback(hhcd
, chnum
, hhcd
->hc
[chnum
].urb_state
);
882 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_TXERR
)
884 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
885 hhcd
->hc
[chnum
].ErrCnt
++;
886 hhcd
->hc
[chnum
].state
= HC_XACTERR
;
887 USB_HC_Halt(hhcd
->Instance
, chnum
);
888 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_TXERR
);
890 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_NAK
)
892 if(hhcd
->hc
[chnum
].ep_type
== EP_TYPE_INTR
)
894 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
895 USB_HC_Halt(hhcd
->Instance
, chnum
);
897 else if ((hhcd
->hc
[chnum
].ep_type
== EP_TYPE_CTRL
)||
898 (hhcd
->hc
[chnum
].ep_type
== EP_TYPE_BULK
))
900 /* re-activate the channel */
901 USBx_HC(chnum
)->HCCHAR
&= ~USB_OTG_HCCHAR_CHDIS
;
902 USBx_HC(chnum
)->HCCHAR
|= USB_OTG_HCCHAR_CHENA
;
905 hhcd
->hc
[chnum
].state
= HC_NAK
;
906 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_NAK
);
911 * @brief This function handles Host Channel OUT interrupt requests.
912 * @param hhcd: HCD handle
913 * @param chnum: Channel number.
914 * This parameter can be a value from 1 to 15
917 static void HCD_HC_OUT_IRQHandler (HCD_HandleTypeDef
*hhcd
, uint8_t chnum
)
919 USB_OTG_GlobalTypeDef
*USBx
= hhcd
->Instance
;
921 if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_AHBERR
)
923 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_AHBERR
);
924 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
926 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_ACK
)
928 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_ACK
);
930 if( hhcd
->hc
[chnum
].do_ping
== 1)
932 hhcd
->hc
[chnum
].state
= HC_NYET
;
933 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
934 USB_HC_Halt(hhcd
->Instance
, chnum
);
935 hhcd
->hc
[chnum
].urb_state
= URB_NOTREADY
;
939 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_NYET
)
941 hhcd
->hc
[chnum
].state
= HC_NYET
;
942 hhcd
->hc
[chnum
].ErrCnt
= 0;
943 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
944 USB_HC_Halt(hhcd
->Instance
, chnum
);
945 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_NYET
);
949 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_FRMOR
)
951 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
952 USB_HC_Halt(hhcd
->Instance
, chnum
);
953 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_FRMOR
);
956 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_XFRC
)
958 hhcd
->hc
[chnum
].ErrCnt
= 0;
959 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
960 USB_HC_Halt(hhcd
->Instance
, chnum
);
961 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_XFRC
);
962 hhcd
->hc
[chnum
].state
= HC_XFRC
;
964 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_STALL
)
966 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_STALL
);
967 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
968 USB_HC_Halt(hhcd
->Instance
, chnum
);
969 hhcd
->hc
[chnum
].state
= HC_STALL
;
971 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_NAK
)
973 hhcd
->hc
[chnum
].ErrCnt
= 0;
974 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
975 USB_HC_Halt(hhcd
->Instance
, chnum
);
976 hhcd
->hc
[chnum
].state
= HC_NAK
;
977 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_NAK
);
979 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_TXERR
)
981 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
982 USB_HC_Halt(hhcd
->Instance
, chnum
);
983 hhcd
->hc
[chnum
].state
= HC_XACTERR
;
984 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_TXERR
);
986 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_DTERR
)
988 __HAL_HCD_UNMASK_HALT_HC_INT(chnum
);
989 USB_HC_Halt(hhcd
->Instance
, chnum
);
990 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_NAK
);
991 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_DTERR
);
992 hhcd
->hc
[chnum
].state
= HC_DATATGLERR
;
994 else if ((USBx_HC(chnum
)->HCINT
) & USB_OTG_HCINT_CHH
)
996 __HAL_HCD_MASK_HALT_HC_INT(chnum
);
998 if(hhcd
->hc
[chnum
].state
== HC_XFRC
)
1000 hhcd
->hc
[chnum
].urb_state
= URB_DONE
;
1001 if (hhcd
->hc
[chnum
].ep_type
== EP_TYPE_BULK
)
1003 hhcd
->hc
[chnum
].toggle_out
^= 1;
1006 else if (hhcd
->hc
[chnum
].state
== HC_NAK
)
1008 hhcd
->hc
[chnum
].urb_state
= URB_NOTREADY
;
1010 else if (hhcd
->hc
[chnum
].state
== HC_NYET
)
1012 hhcd
->hc
[chnum
].urb_state
= URB_NOTREADY
;
1013 hhcd
->hc
[chnum
].do_ping
= 0;
1015 else if (hhcd
->hc
[chnum
].state
== HC_STALL
)
1017 hhcd
->hc
[chnum
].urb_state
= URB_STALL
;
1019 else if((hhcd
->hc
[chnum
].state
== HC_XACTERR
) ||
1020 (hhcd
->hc
[chnum
].state
== HC_DATATGLERR
))
1022 if(hhcd
->hc
[chnum
].ErrCnt
++ > 3)
1024 hhcd
->hc
[chnum
].ErrCnt
= 0;
1025 hhcd
->hc
[chnum
].urb_state
= URB_ERROR
;
1029 hhcd
->hc
[chnum
].urb_state
= URB_NOTREADY
;
1032 /* re-activate the channel */
1033 USBx_HC(chnum
)->HCCHAR
&= ~USB_OTG_HCCHAR_CHDIS
;
1034 USBx_HC(chnum
)->HCCHAR
|= USB_OTG_HCCHAR_CHENA
;
1037 __HAL_HCD_CLEAR_HC_INT(chnum
, USB_OTG_HCINT_CHH
);
1038 HAL_HCD_HC_NotifyURBChange_Callback(hhcd
, chnum
, hhcd
->hc
[chnum
].urb_state
);
1043 * @brief This function handles Rx Queue Level interrupt requests.
1044 * @param hhcd: HCD handle
1047 static void HCD_RXQLVL_IRQHandler (HCD_HandleTypeDef
*hhcd
)
1049 USB_OTG_GlobalTypeDef
*USBx
= hhcd
->Instance
;
1050 uint8_t channelnum
=0;
1055 temp
= hhcd
->Instance
->GRXSTSP
;
1056 channelnum
= temp
& USB_OTG_GRXSTSP_EPNUM
;
1057 pktsts
= (temp
& USB_OTG_GRXSTSP_PKTSTS
) >> 17;
1058 pktcnt
= (temp
& USB_OTG_GRXSTSP_BCNT
) >> 4;
1062 case GRXSTS_PKTSTS_IN
:
1063 /* Read the data into the host buffer. */
1064 if ((pktcnt
> 0) && (hhcd
->hc
[channelnum
].xfer_buff
!= (void *)0))
1066 USB_ReadPacket(hhcd
->Instance
, hhcd
->hc
[channelnum
].xfer_buff
, pktcnt
);
1068 /*manage multiple Xfer */
1069 hhcd
->hc
[channelnum
].xfer_buff
+= pktcnt
;
1070 hhcd
->hc
[channelnum
].xfer_count
+= pktcnt
;
1072 if((USBx_HC(channelnum
)->HCTSIZ
& USB_OTG_HCTSIZ_PKTCNT
) > 0)
1074 /* re-activate the channel when more packets are expected */
1075 USBx_HC(channelnum
)->HCCHAR
&= ~USB_OTG_HCCHAR_CHDIS
;
1076 USBx_HC(channelnum
)->HCCHAR
|= USB_OTG_HCCHAR_CHENA
;
1077 hhcd
->hc
[channelnum
].toggle_in
^= 1;
1082 case GRXSTS_PKTSTS_DATA_TOGGLE_ERR
:
1085 case GRXSTS_PKTSTS_IN_XFER_COMP
:
1086 case GRXSTS_PKTSTS_CH_HALTED
:
1093 * @brief This function handles Host Port interrupt requests.
1094 * @param hhcd: HCD handle
1097 static void HCD_Port_IRQHandler (HCD_HandleTypeDef
*hhcd
)
1099 USB_OTG_GlobalTypeDef
*USBx
= hhcd
->Instance
;
1100 __IO
uint32_t hprt0
= 0, hprt0_dup
= 0;
1102 /* Handle Host Port Interrupts */
1104 hprt0_dup
= USBx_HPRT0
;
1106 hprt0_dup
&= ~(USB_OTG_HPRT_PENA
| USB_OTG_HPRT_PCDET
|\
1107 USB_OTG_HPRT_PENCHNG
| USB_OTG_HPRT_POCCHNG
);
1109 /* Check whether Port Connect Detected */
1110 if((hprt0
& USB_OTG_HPRT_PCDET
) == USB_OTG_HPRT_PCDET
)
1112 if((hprt0
& USB_OTG_HPRT_PCSTS
) == USB_OTG_HPRT_PCSTS
)
1114 USB_MASK_INTERRUPT(hhcd
->Instance
, USB_OTG_GINTSTS_DISCINT
);
1115 HAL_HCD_Connect_Callback(hhcd
);
1117 hprt0_dup
|= USB_OTG_HPRT_PCDET
;
1120 /* Check whether Port Enable Changed */
1121 if((hprt0
& USB_OTG_HPRT_PENCHNG
) == USB_OTG_HPRT_PENCHNG
)
1123 hprt0_dup
|= USB_OTG_HPRT_PENCHNG
;
1125 if((hprt0
& USB_OTG_HPRT_PENA
) == USB_OTG_HPRT_PENA
)
1127 if ((hprt0
& USB_OTG_HPRT_PSPD
) == (HPRT0_PRTSPD_LOW_SPEED
<< 17))
1129 USB_InitFSLSPClkSel(hhcd
->Instance
,HCFG_6_MHZ
);
1133 USB_InitFSLSPClkSel(hhcd
->Instance
,HCFG_48_MHZ
);
1135 HAL_HCD_Connect_Callback(hhcd
);
1140 USBx_HPRT0
&= ~(USB_OTG_HPRT_PENA
| USB_OTG_HPRT_PCDET
|\
1141 USB_OTG_HPRT_PENCHNG
| USB_OTG_HPRT_POCCHNG
);
1143 USB_UNMASK_INTERRUPT(hhcd
->Instance
, USB_OTG_GINTSTS_DISCINT
);
1147 /* Check For an over current */
1148 if((hprt0
& USB_OTG_HPRT_POCCHNG
) == USB_OTG_HPRT_POCCHNG
)
1150 hprt0_dup
|= USB_OTG_HPRT_POCCHNG
;
1153 /* Clear Port Interrupts */
1154 USBx_HPRT0
= hprt0_dup
;
1165 #endif /* STM32F105xC || STM32F107xC */
1167 #endif /* HAL_HCD_MODULE_ENABLED */
1173 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/