stm32l4xx_hal_hcd.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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. @verbatim
  14. ==============================================================================
  15. ##### How to use this driver #####
  16. ==============================================================================
  17. [..]
  18. (#)Declare a HCD_HandleTypeDef handle structure, for example:
  19. HCD_HandleTypeDef hhcd;
  20. (#)Fill parameters of Init structure in HCD handle
  21. (#)Call HAL_HCD_Init() API to initialize the HCD peripheral (Core, Host core, ...)
  22. (#)Initialize the HCD low level resources through the HAL_HCD_MspInit() API:
  23. (##) Enable the HCD/USB Low Level interface clock using the following macro
  24. (+++) __HAL_RCC_USB_OTG_FS_CLK_ENABLE()
  25. (##) Initialize the related GPIO clocks
  26. (##) Configure HCD pin-out
  27. (##) Configure HCD NVIC interrupt
  28. (#)Associate the Upper USB Host stack to the HAL HCD Driver:
  29. (##) hhcd.pData = phost;
  30. (#)Enable HCD transmission and reception:
  31. (##) HAL_HCD_Start();
  32. @endverbatim
  33. ******************************************************************************
  34. * @attention
  35. *
  36. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  37. *
  38. * Redistribution and use in source and binary forms, with or without modification,
  39. * are permitted provided that the following conditions are met:
  40. * 1. Redistributions of source code must retain the above copyright notice,
  41. * this list of conditions and the following disclaimer.
  42. * 2. Redistributions in binary form must reproduce the above copyright notice,
  43. * this list of conditions and the following disclaimer in the documentation
  44. * and/or other materials provided with the distribution.
  45. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  46. * may be used to endorse or promote products derived from this software
  47. * without specific prior written permission.
  48. *
  49. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  50. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  52. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  53. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  54. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  55. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  56. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  58. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59. *
  60. ******************************************************************************
  61. */
  62. /* Includes ------------------------------------------------------------------*/
  63. #include "stm32l4xx_hal.h"
  64. #if defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || \
  65. defined(STM32L496xx) || defined(STM32L4A6xx) || \
  66. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  67. /** @addtogroup STM32L4xx_HAL_Driver
  68. * @{
  69. */
  70. /** @defgroup HCD HCD
  71. * @brief HCD HAL module driver
  72. * @{
  73. */
  74. #ifdef HAL_HCD_MODULE_ENABLED
  75. /* Private typedef -----------------------------------------------------------*/
  76. /* Private define ------------------------------------------------------------*/
  77. /* Private macro -------------------------------------------------------------*/
  78. /* Private variables ---------------------------------------------------------*/
  79. /* Private function prototypes -----------------------------------------------*/
  80. /** @defgroup HCD_Private_Functions HCD Private Functions
  81. * @{
  82. */
  83. static void HCD_HC_IN_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum);
  84. static void HCD_HC_OUT_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum);
  85. static void HCD_RXQLVL_IRQHandler(HCD_HandleTypeDef *hhcd);
  86. static void HCD_Port_IRQHandler(HCD_HandleTypeDef *hhcd);
  87. /**
  88. * @}
  89. */
  90. /* Exported functions --------------------------------------------------------*/
  91. /** @defgroup HCD_Exported_Functions HCD Exported Functions
  92. * @{
  93. */
  94. /** @defgroup HCD_Exported_Functions_Group1 Initialization and de-initialization functions
  95. * @brief Initialization and Configuration functions
  96. *
  97. @verbatim
  98. ===============================================================================
  99. ##### Initialization and de-initialization functions #####
  100. ===============================================================================
  101. [..] This section provides functions allowing to:
  102. @endverbatim
  103. * @{
  104. */
  105. /**
  106. * @brief Initialize the Host driver.
  107. * @param hhcd: HCD handle
  108. * @retval HAL status
  109. */
  110. HAL_StatusTypeDef HAL_HCD_Init(HCD_HandleTypeDef *hhcd)
  111. {
  112. /* Check the HCD handle allocation */
  113. if(hhcd == NULL)
  114. {
  115. return HAL_ERROR;
  116. }
  117. /* Check the parameters */
  118. assert_param(IS_HCD_ALL_INSTANCE(hhcd->Instance));
  119. if(hhcd->State == HAL_HCD_STATE_RESET)
  120. {
  121. /* Allocate lock resource and initialize it */
  122. hhcd->Lock = HAL_UNLOCKED;
  123. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  124. HAL_HCD_MspInit(hhcd);
  125. }
  126. hhcd->State = HAL_HCD_STATE_BUSY;
  127. /* Disable the Interrupts */
  128. __HAL_HCD_DISABLE(hhcd);
  129. /*Init the Core (common init.) */
  130. USB_CoreInit(hhcd->Instance, hhcd->Init);
  131. /* Force Host Mode*/
  132. USB_SetCurrentMode(hhcd->Instance , USB_HOST_MODE);
  133. /* Init Host */
  134. USB_HostInit(hhcd->Instance, hhcd->Init);
  135. hhcd->State= HAL_HCD_STATE_READY;
  136. return HAL_OK;
  137. }
  138. /**
  139. * @brief Initialize a Host channel.
  140. * @param hhcd: HCD handle
  141. * @param ch_num: Channel number.
  142. * This parameter can be a value from 1 to 15
  143. * @param epnum: Endpoint number.
  144. * This parameter can be a value from 1 to 15
  145. * @param dev_address : Current device address
  146. * This parameter can be a value from 0 to 255
  147. * @param speed: Current device speed.
  148. * This parameter can be one of these values:
  149. * HCD_SPEED_HIGH: High speed mode,
  150. * HCD_SPEED_FULL: Full speed mode,
  151. * HCD_SPEED_LOW: Low speed mode
  152. * @param ep_type: Endpoint Type.
  153. * This parameter can be one of these values:
  154. * EP_TYPE_CTRL: Control type,
  155. * EP_TYPE_ISOC: Isochronous type,
  156. * EP_TYPE_BULK: Bulk type,
  157. * EP_TYPE_INTR: Interrupt type
  158. * @param mps: Max Packet Size.
  159. * This parameter can be a value from 0 to32K
  160. * @retval HAL status
  161. */
  162. HAL_StatusTypeDef HAL_HCD_HC_Init(HCD_HandleTypeDef *hhcd,
  163. uint8_t ch_num,
  164. uint8_t epnum,
  165. uint8_t dev_address,
  166. uint8_t speed,
  167. uint8_t ep_type,
  168. uint16_t mps)
  169. {
  170. HAL_StatusTypeDef status = HAL_OK;
  171. __HAL_LOCK(hhcd);
  172. hhcd->hc[ch_num].dev_addr = dev_address;
  173. hhcd->hc[ch_num].max_packet = mps;
  174. hhcd->hc[ch_num].ch_num = ch_num;
  175. hhcd->hc[ch_num].ep_type = ep_type;
  176. hhcd->hc[ch_num].ep_num = epnum & 0x7F;
  177. hhcd->hc[ch_num].ep_is_in = ((epnum & 0x80) == 0x80);
  178. hhcd->hc[ch_num].speed = speed;
  179. status = USB_HC_Init(hhcd->Instance,
  180. ch_num,
  181. epnum,
  182. dev_address,
  183. speed,
  184. ep_type,
  185. mps);
  186. __HAL_UNLOCK(hhcd);
  187. return status;
  188. }
  189. /**
  190. * @brief Halt a Host channel.
  191. * @param hhcd: HCD handle
  192. * @param ch_num: Channel number.
  193. * This parameter can be a value from 1 to 15
  194. * @retval HAL status
  195. */
  196. HAL_StatusTypeDef HAL_HCD_HC_Halt(HCD_HandleTypeDef *hhcd, uint8_t ch_num)
  197. {
  198. HAL_StatusTypeDef status = HAL_OK;
  199. __HAL_LOCK(hhcd);
  200. USB_HC_Halt(hhcd->Instance, ch_num);
  201. __HAL_UNLOCK(hhcd);
  202. return status;
  203. }
  204. /**
  205. * @brief DeInitialize the Host driver.
  206. * @param hhcd: HCD handle
  207. * @retval HAL status
  208. */
  209. HAL_StatusTypeDef HAL_HCD_DeInit(HCD_HandleTypeDef *hhcd)
  210. {
  211. /* Check the HCD handle allocation */
  212. if(hhcd == NULL)
  213. {
  214. return HAL_ERROR;
  215. }
  216. hhcd->State = HAL_HCD_STATE_BUSY;
  217. /* DeInit the low level hardware */
  218. HAL_HCD_MspDeInit(hhcd);
  219. __HAL_HCD_DISABLE(hhcd);
  220. hhcd->State = HAL_HCD_STATE_RESET;
  221. return HAL_OK;
  222. }
  223. /**
  224. * @brief Initialize the HCD MSP.
  225. * @param hhcd: HCD handle
  226. * @retval None
  227. */
  228. __weak void HAL_HCD_MspInit(HCD_HandleTypeDef *hhcd)
  229. {
  230. /* Prevent unused argument(s) compilation warning */
  231. UNUSED(hhcd);
  232. /* NOTE : This function should not be modified, when the callback is needed,
  233. the HAL_PCD_MspInit could be implemented in the user file
  234. */
  235. }
  236. /**
  237. * @brief DeInitialize the HCD MSP.
  238. * @param hhcd: HCD handle
  239. * @retval None
  240. */
  241. __weak void HAL_HCD_MspDeInit(HCD_HandleTypeDef *hhcd)
  242. {
  243. /* Prevent unused argument(s) compilation warning */
  244. UNUSED(hhcd);
  245. /* NOTE : This function should not be modified, when the callback is needed,
  246. the HAL_PCD_MspDeInit could be implemented in the user file
  247. */
  248. }
  249. /**
  250. * @}
  251. */
  252. /** @defgroup HCD_Exported_Functions_Group2 Input and Output operation functions
  253. * @brief HCD IO operation functions
  254. *
  255. @verbatim
  256. ===============================================================================
  257. ##### IO operation functions #####
  258. ===============================================================================
  259. [..] This subsection provides a set of functions allowing to manage the USB Host Data
  260. Transfer
  261. @endverbatim
  262. * @{
  263. */
  264. /**
  265. * @brief Submit a new URB for processing.
  266. * @param hhcd: HCD handle
  267. * @param ch_num: Channel number.
  268. * This parameter can be a value from 1 to 15
  269. * @param direction: Channel number.
  270. * This parameter can be one of these values:
  271. * 0 : Output / 1 : Input
  272. * @param ep_type: Endpoint Type.
  273. * This parameter can be one of these values:
  274. * EP_TYPE_CTRL: Control type/
  275. * EP_TYPE_ISOC: Isochronous type/
  276. * EP_TYPE_BULK: Bulk type/
  277. * EP_TYPE_INTR: Interrupt type/
  278. * @param token: Endpoint Type.
  279. * This parameter can be one of these values:
  280. * 0: HC_PID_SETUP / 1: HC_PID_DATA1
  281. * @param pbuff: pointer to URB data
  282. * @param length: Length of URB data
  283. * @param do_ping: activate do ping protocol (for high speed only).
  284. * This parameter can be one of these values:
  285. * 0 : do ping inactive / 1 : do ping active
  286. * @retval HAL status
  287. */
  288. HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
  289. uint8_t ch_num,
  290. uint8_t direction ,
  291. uint8_t ep_type,
  292. uint8_t token,
  293. uint8_t* pbuff,
  294. uint16_t length,
  295. uint8_t do_ping)
  296. {
  297. hhcd->hc[ch_num].ep_is_in = direction;
  298. hhcd->hc[ch_num].ep_type = ep_type;
  299. if(token == 0)
  300. {
  301. hhcd->hc[ch_num].data_pid = HC_PID_SETUP;
  302. }
  303. else
  304. {
  305. hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
  306. }
  307. /* Manage Data Toggle */
  308. switch(ep_type)
  309. {
  310. case EP_TYPE_CTRL:
  311. if((token == 1) && (direction == 0)) /*send data */
  312. {
  313. if ( length == 0 )
  314. { /* For Status OUT stage, Length==0, Status Out PID = 1 */
  315. hhcd->hc[ch_num].toggle_out = 1;
  316. }
  317. /* Set the Data Toggle bit as per the Flag */
  318. if ( hhcd->hc[ch_num].toggle_out == 0)
  319. { /* Put the PID 0 */
  320. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  321. }
  322. else
  323. { /* Put the PID 1 */
  324. hhcd->hc[ch_num].data_pid = HC_PID_DATA1 ;
  325. }
  326. if(hhcd->hc[ch_num].urb_state != URB_NOTREADY)
  327. {
  328. hhcd->hc[ch_num].do_ping = do_ping;
  329. }
  330. }
  331. break;
  332. case EP_TYPE_BULK:
  333. if(direction == 0)
  334. {
  335. /* Set the Data Toggle bit as per the Flag */
  336. if ( hhcd->hc[ch_num].toggle_out == 0)
  337. { /* Put the PID 0 */
  338. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  339. }
  340. else
  341. { /* Put the PID 1 */
  342. hhcd->hc[ch_num].data_pid = HC_PID_DATA1 ;
  343. }
  344. if(hhcd->hc[ch_num].urb_state != URB_NOTREADY)
  345. {
  346. hhcd->hc[ch_num].do_ping = do_ping;
  347. }
  348. }
  349. else
  350. {
  351. if( hhcd->hc[ch_num].toggle_in == 0)
  352. {
  353. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  354. }
  355. else
  356. {
  357. hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
  358. }
  359. }
  360. break;
  361. case EP_TYPE_INTR:
  362. if(direction == 0)
  363. {
  364. /* Set the Data Toggle bit as per the Flag */
  365. if ( hhcd->hc[ch_num].toggle_out == 0)
  366. { /* Put the PID 0 */
  367. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  368. }
  369. else
  370. { /* Put the PID 1 */
  371. hhcd->hc[ch_num].data_pid = HC_PID_DATA1 ;
  372. }
  373. }
  374. else
  375. {
  376. if( hhcd->hc[ch_num].toggle_in == 0)
  377. {
  378. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  379. }
  380. else
  381. {
  382. hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
  383. }
  384. }
  385. break;
  386. case EP_TYPE_ISOC:
  387. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  388. break;
  389. }
  390. hhcd->hc[ch_num].xfer_buff = pbuff;
  391. hhcd->hc[ch_num].xfer_len = length;
  392. hhcd->hc[ch_num].urb_state = URB_IDLE;
  393. hhcd->hc[ch_num].xfer_count = 0 ;
  394. hhcd->hc[ch_num].ch_num = ch_num;
  395. hhcd->hc[ch_num].state = HC_IDLE;
  396. return USB_HC_StartXfer(hhcd->Instance, &(hhcd->hc[ch_num]), hhcd->Init.dma_enable);
  397. }
  398. /**
  399. * @brief Handle HCD interrupt request.
  400. * @param hhcd: HCD handle
  401. * @retval None
  402. */
  403. void HAL_HCD_IRQHandler(HCD_HandleTypeDef *hhcd)
  404. {
  405. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  406. uint32_t i = 0 , interrupt = 0;
  407. /* ensure that we are in device mode */
  408. if (USB_GetMode(hhcd->Instance) == USB_OTG_MODE_HOST)
  409. {
  410. /* avoid spurious interrupt */
  411. if(__HAL_HCD_IS_INVALID_INTERRUPT(hhcd))
  412. {
  413. return;
  414. }
  415. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
  416. {
  417. /* incorrect mode, acknowledge the interrupt */
  418. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
  419. }
  420. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_IISOIXFR))
  421. {
  422. /* incorrect mode, acknowledge the interrupt */
  423. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_IISOIXFR);
  424. }
  425. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_PTXFE))
  426. {
  427. /* incorrect mode, acknowledge the interrupt */
  428. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_PTXFE);
  429. }
  430. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_MMIS))
  431. {
  432. /* incorrect mode, acknowledge the interrupt */
  433. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_MMIS);
  434. }
  435. /* Handle Host Disconnect Interrupts */
  436. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_DISCINT))
  437. {
  438. /* Cleanup HPRT */
  439. USBx_HPRT0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
  440. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
  441. /* Handle Host Port Interrupts */
  442. HAL_HCD_Disconnect_Callback(hhcd);
  443. USB_InitFSLSPClkSel(hhcd->Instance ,HCFG_48_MHZ );
  444. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_DISCINT);
  445. }
  446. /* Handle Host Port Interrupts */
  447. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_HPRTINT))
  448. {
  449. HCD_Port_IRQHandler (hhcd);
  450. }
  451. /* Handle Host SOF Interrupts */
  452. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_SOF))
  453. {
  454. HAL_HCD_SOF_Callback(hhcd);
  455. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_SOF);
  456. }
  457. /* Handle Host channel Interrupts */
  458. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_HCINT))
  459. {
  460. interrupt = USB_HC_ReadInterrupt(hhcd->Instance);
  461. for (i = 0; i < hhcd->Init.Host_channels ; i++)
  462. {
  463. if (interrupt & (1 << i))
  464. {
  465. if ((USBx_HC(i)->HCCHAR) & USB_OTG_HCCHAR_EPDIR)
  466. {
  467. HCD_HC_IN_IRQHandler (hhcd, i);
  468. }
  469. else
  470. {
  471. HCD_HC_OUT_IRQHandler (hhcd, i);
  472. }
  473. }
  474. }
  475. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_HCINT);
  476. }
  477. /* Handle Rx Queue Level Interrupts */
  478. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_RXFLVL))
  479. {
  480. USB_MASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  481. HCD_RXQLVL_IRQHandler (hhcd);
  482. USB_UNMASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  483. }
  484. }
  485. }
  486. /**
  487. * @brief SOF callback.
  488. * @param hhcd: HCD handle
  489. * @retval None
  490. */
  491. __weak void HAL_HCD_SOF_Callback(HCD_HandleTypeDef *hhcd)
  492. {
  493. /* Prevent unused argument(s) compilation warning */
  494. UNUSED(hhcd);
  495. /* NOTE : This function should not be modified, when the callback is needed,
  496. the HAL_HCD_SOF_Callback could be implemented in the user file
  497. */
  498. }
  499. /**
  500. * @brief Connection Event callback.
  501. * @param hhcd: HCD handle
  502. * @retval None
  503. */
  504. __weak void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd)
  505. {
  506. /* Prevent unused argument(s) compilation warning */
  507. UNUSED(hhcd);
  508. /* NOTE : This function should not be modified, when the callback is needed,
  509. the HAL_HCD_Connect_Callback could be implemented in the user file
  510. */
  511. }
  512. /**
  513. * @brief Disconnection Event callback.
  514. * @param hhcd: HCD handle
  515. * @retval None
  516. */
  517. __weak void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd)
  518. {
  519. /* Prevent unused argument(s) compilation warning */
  520. UNUSED(hhcd);
  521. /* NOTE : This function should not be modified, when the callback is needed,
  522. the HAL_HCD_Disconnect_Callback could be implemented in the user file
  523. */
  524. }
  525. /**
  526. * @brief Notify URB state change callback.
  527. * @param hhcd: HCD handle
  528. * @param chnum: Channel number.
  529. * This parameter can be a value from 1 to 15
  530. * @param urb_state:
  531. * This parameter can be one of these values:
  532. * URB_IDLE/
  533. * URB_DONE/
  534. * URB_NOTREADY/
  535. * URB_NYET/
  536. * URB_ERROR/
  537. * URB_STALL/
  538. * @retval None
  539. */
  540. __weak void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum, HCD_URBStateTypeDef urb_state)
  541. {
  542. /* Prevent unused argument(s) compilation warning */
  543. UNUSED(hhcd);
  544. UNUSED(chnum);
  545. UNUSED(urb_state);
  546. /* NOTE : This function should not be modified, when the callback is needed,
  547. the HAL_HCD_HC_NotifyURBChange_Callback could be implemented in the user file
  548. */
  549. }
  550. /**
  551. * @}
  552. */
  553. /** @defgroup HCD_Exported_Functions_Group3 Peripheral Control functions
  554. * @brief Management functions
  555. *
  556. @verbatim
  557. ===============================================================================
  558. ##### Peripheral Control functions #####
  559. ===============================================================================
  560. [..]
  561. This subsection provides a set of functions allowing to control the HCD data
  562. transfers.
  563. @endverbatim
  564. * @{
  565. */
  566. /**
  567. * @brief Start the Host driver.
  568. * @param hhcd: HCD handle
  569. * @retval HAL status
  570. */
  571. HAL_StatusTypeDef HAL_HCD_Start(HCD_HandleTypeDef *hhcd)
  572. {
  573. __HAL_LOCK(hhcd);
  574. __HAL_HCD_ENABLE(hhcd);
  575. USB_DriveVbus(hhcd->Instance, 1);
  576. __HAL_UNLOCK(hhcd);
  577. return HAL_OK;
  578. }
  579. /**
  580. * @brief Stop the Host driver.
  581. * @param hhcd: HCD handle
  582. * @retval HAL status
  583. */
  584. HAL_StatusTypeDef HAL_HCD_Stop(HCD_HandleTypeDef *hhcd)
  585. {
  586. __HAL_LOCK(hhcd);
  587. USB_StopHost(hhcd->Instance);
  588. __HAL_UNLOCK(hhcd);
  589. return HAL_OK;
  590. }
  591. /**
  592. * @brief Reset the Host port.
  593. * @param hhcd: HCD handle
  594. * @retval HAL status
  595. */
  596. HAL_StatusTypeDef HAL_HCD_ResetPort(HCD_HandleTypeDef *hhcd)
  597. {
  598. return (USB_ResetPort(hhcd->Instance));
  599. }
  600. /**
  601. * @}
  602. */
  603. /** @defgroup HCD_Exported_Functions_Group4 Peripheral State functions
  604. * @brief Peripheral State functions
  605. *
  606. @verbatim
  607. ===============================================================================
  608. ##### Peripheral State functions #####
  609. ===============================================================================
  610. [..]
  611. This subsection permits to get in run-time the status of the peripheral
  612. and the data flow.
  613. @endverbatim
  614. * @{
  615. */
  616. /**
  617. * @brief Return the HCD handle state.
  618. * @param hhcd: HCD handle
  619. * @retval HAL state
  620. */
  621. HCD_StateTypeDef HAL_HCD_GetState(HCD_HandleTypeDef *hhcd)
  622. {
  623. return hhcd->State;
  624. }
  625. /**
  626. * @brief Return URB state for a channel.
  627. * @param hhcd: HCD handle
  628. * @param chnum: Channel number.
  629. * This parameter can be a value from 1 to 15
  630. * @retval URB state.
  631. * This parameter can be one of these values:
  632. * URB_IDLE/
  633. * URB_DONE/
  634. * URB_NOTREADY/
  635. * URB_NYET/
  636. * URB_ERROR/
  637. * URB_STALL
  638. */
  639. HCD_URBStateTypeDef HAL_HCD_HC_GetURBState(HCD_HandleTypeDef *hhcd, uint8_t chnum)
  640. {
  641. return hhcd->hc[chnum].urb_state;
  642. }
  643. /**
  644. * @brief Return the last Host transfer size.
  645. * @param hhcd: HCD handle
  646. * @param chnum: Channel number.
  647. * This parameter can be a value from 1 to 15
  648. * @retval last transfer size in byte
  649. */
  650. uint32_t HAL_HCD_HC_GetXferCount(HCD_HandleTypeDef *hhcd, uint8_t chnum)
  651. {
  652. return hhcd->hc[chnum].xfer_count;
  653. }
  654. /**
  655. * @brief Return the Host Channel state.
  656. * @param hhcd: HCD handle
  657. * @param chnum: Channel number.
  658. * This parameter can be a value from 1 to 15
  659. * @retval Host channel state
  660. * This parameter can be one of these values:
  661. * HC_IDLE/
  662. * HC_XFRC/
  663. * HC_HALTED/
  664. * HC_NYET/
  665. * HC_NAK/
  666. * HC_STALL/
  667. * HC_XACTERR/
  668. * HC_BBLERR/
  669. * HC_DATATGLERR
  670. */
  671. HCD_HCStateTypeDef HAL_HCD_HC_GetState(HCD_HandleTypeDef *hhcd, uint8_t chnum)
  672. {
  673. return hhcd->hc[chnum].state;
  674. }
  675. /**
  676. * @brief Return the current Host frame number.
  677. * @param hhcd: HCD handle
  678. * @retval Current Host frame number
  679. */
  680. uint32_t HAL_HCD_GetCurrentFrame(HCD_HandleTypeDef *hhcd)
  681. {
  682. return (USB_GetCurrentFrame(hhcd->Instance));
  683. }
  684. /**
  685. * @brief Return the Host enumeration speed.
  686. * @param hhcd: HCD handle
  687. * @retval Enumeration speed
  688. */
  689. uint32_t HAL_HCD_GetCurrentSpeed(HCD_HandleTypeDef *hhcd)
  690. {
  691. return (USB_GetHostSpeed(hhcd->Instance));
  692. }
  693. /**
  694. * @}
  695. */
  696. /**
  697. * @}
  698. */
  699. /** @addtogroup HCD_Private_Functions
  700. * @{
  701. */
  702. /**
  703. * @brief Handle Host Channel IN interrupt requests.
  704. * @param hhcd: HCD handle
  705. * @param chnum: Channel number.
  706. * This parameter can be a value from 1 to 15
  707. * @retval none
  708. */
  709. static void HCD_HC_IN_IRQHandler (HCD_HandleTypeDef *hhcd, uint8_t chnum)
  710. {
  711. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  712. uint32_t tmpreg = 0;
  713. if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_AHBERR)
  714. {
  715. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_AHBERR);
  716. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  717. }
  718. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_ACK)
  719. {
  720. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_ACK);
  721. }
  722. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_STALL)
  723. {
  724. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  725. hhcd->hc[chnum].state = HC_STALL;
  726. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
  727. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_STALL);
  728. USB_HC_Halt(hhcd->Instance, chnum);
  729. }
  730. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_DTERR)
  731. {
  732. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  733. USB_HC_Halt(hhcd->Instance, chnum);
  734. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
  735. hhcd->hc[chnum].state = HC_DATATGLERR;
  736. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_DTERR);
  737. }
  738. if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_FRMOR)
  739. {
  740. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  741. USB_HC_Halt(hhcd->Instance, chnum);
  742. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_FRMOR);
  743. }
  744. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_XFRC)
  745. {
  746. if (hhcd->Init.dma_enable)
  747. {
  748. hhcd->hc[chnum].xfer_count = hhcd->hc[chnum].xfer_len - \
  749. (USBx_HC(chnum)->HCTSIZ & USB_OTG_HCTSIZ_XFRSIZ);
  750. }
  751. hhcd->hc[chnum].state = HC_XFRC;
  752. hhcd->hc[chnum].ErrCnt = 0;
  753. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_XFRC);
  754. if ((hhcd->hc[chnum].ep_type == EP_TYPE_CTRL)||
  755. (hhcd->hc[chnum].ep_type == EP_TYPE_BULK))
  756. {
  757. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  758. USB_HC_Halt(hhcd->Instance, chnum);
  759. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
  760. }
  761. else if(hhcd->hc[chnum].ep_type == EP_TYPE_INTR)
  762. {
  763. USBx_HC(chnum)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM;
  764. hhcd->hc[chnum].urb_state = URB_DONE;
  765. HAL_HCD_HC_NotifyURBChange_Callback(hhcd, chnum, hhcd->hc[chnum].urb_state);
  766. }
  767. hhcd->hc[chnum].toggle_in ^= 1;
  768. }
  769. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_CHH)
  770. {
  771. __HAL_HCD_MASK_HALT_HC_INT(chnum);
  772. if(hhcd->hc[chnum].state == HC_XFRC)
  773. {
  774. hhcd->hc[chnum].urb_state = URB_DONE;
  775. }
  776. else if (hhcd->hc[chnum].state == HC_STALL)
  777. {
  778. hhcd->hc[chnum].urb_state = URB_STALL;
  779. }
  780. else if((hhcd->hc[chnum].state == HC_XACTERR) ||
  781. (hhcd->hc[chnum].state == HC_DATATGLERR))
  782. {
  783. if(hhcd->hc[chnum].ErrCnt++ > 3)
  784. {
  785. hhcd->hc[chnum].ErrCnt = 0;
  786. hhcd->hc[chnum].urb_state = URB_ERROR;
  787. }
  788. else
  789. {
  790. hhcd->hc[chnum].urb_state = URB_NOTREADY;
  791. }
  792. /* re-activate the channel */
  793. tmpreg = USBx_HC(chnum)->HCCHAR;
  794. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  795. tmpreg |= USB_OTG_HCCHAR_CHENA;
  796. USBx_HC(chnum)->HCCHAR = tmpreg;
  797. }
  798. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_CHH);
  799. HAL_HCD_HC_NotifyURBChange_Callback(hhcd, chnum, hhcd->hc[chnum].urb_state);
  800. }
  801. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_TXERR)
  802. {
  803. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  804. hhcd->hc[chnum].ErrCnt++;
  805. hhcd->hc[chnum].state = HC_XACTERR;
  806. USB_HC_Halt(hhcd->Instance, chnum);
  807. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_TXERR);
  808. }
  809. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NAK)
  810. {
  811. if(hhcd->hc[chnum].ep_type == EP_TYPE_INTR)
  812. {
  813. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  814. USB_HC_Halt(hhcd->Instance, chnum);
  815. }
  816. /* Clear the NAK flag before re-enabling the channel for new IN request */
  817. hhcd->hc[chnum].state = HC_NAK;
  818. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
  819. if ((hhcd->hc[chnum].ep_type == EP_TYPE_CTRL)||
  820. (hhcd->hc[chnum].ep_type == EP_TYPE_BULK))
  821. {
  822. /* re-activate the channel */
  823. USBx_HC(chnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHDIS;
  824. USBx_HC(chnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  825. }
  826. }
  827. }
  828. /**
  829. * @brief Handle Host Channel OUT interrupt requests.
  830. * @param hhcd: HCD handle
  831. * @param chnum: Channel number.
  832. * This parameter can be a value from 1 to 15
  833. * @retval none
  834. */
  835. static void HCD_HC_OUT_IRQHandler (HCD_HandleTypeDef *hhcd, uint8_t chnum)
  836. {
  837. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  838. uint32_t tmpreg = 0;
  839. if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_AHBERR)
  840. {
  841. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_AHBERR);
  842. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  843. }
  844. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_ACK)
  845. {
  846. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_ACK);
  847. if( hhcd->hc[chnum].do_ping == 1)
  848. {
  849. hhcd->hc[chnum].state = HC_NYET;
  850. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  851. USB_HC_Halt(hhcd->Instance, chnum);
  852. hhcd->hc[chnum].urb_state = URB_NOTREADY;
  853. }
  854. }
  855. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NYET)
  856. {
  857. hhcd->hc[chnum].state = HC_NYET;
  858. hhcd->hc[chnum].ErrCnt= 0;
  859. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  860. USB_HC_Halt(hhcd->Instance, chnum);
  861. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NYET);
  862. }
  863. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_FRMOR)
  864. {
  865. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  866. USB_HC_Halt(hhcd->Instance, chnum);
  867. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_FRMOR);
  868. }
  869. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_XFRC)
  870. {
  871. hhcd->hc[chnum].ErrCnt = 0;
  872. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  873. USB_HC_Halt(hhcd->Instance, chnum);
  874. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_XFRC);
  875. hhcd->hc[chnum].state = HC_XFRC;
  876. }
  877. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_STALL)
  878. {
  879. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_STALL);
  880. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  881. USB_HC_Halt(hhcd->Instance, chnum);
  882. hhcd->hc[chnum].state = HC_STALL;
  883. }
  884. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NAK)
  885. {
  886. hhcd->hc[chnum].ErrCnt = 0;
  887. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  888. USB_HC_Halt(hhcd->Instance, chnum);
  889. hhcd->hc[chnum].state = HC_NAK;
  890. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
  891. }
  892. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_TXERR)
  893. {
  894. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  895. USB_HC_Halt(hhcd->Instance, chnum);
  896. hhcd->hc[chnum].state = HC_XACTERR;
  897. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_TXERR);
  898. }
  899. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_DTERR)
  900. {
  901. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  902. USB_HC_Halt(hhcd->Instance, chnum);
  903. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
  904. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_DTERR);
  905. hhcd->hc[chnum].state = HC_DATATGLERR;
  906. }
  907. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_CHH)
  908. {
  909. __HAL_HCD_MASK_HALT_HC_INT(chnum);
  910. if(hhcd->hc[chnum].state == HC_XFRC)
  911. {
  912. hhcd->hc[chnum].urb_state = URB_DONE;
  913. if (hhcd->hc[chnum].ep_type == EP_TYPE_BULK)
  914. {
  915. hhcd->hc[chnum].toggle_out ^= 1;
  916. }
  917. }
  918. else if (hhcd->hc[chnum].state == HC_NAK)
  919. {
  920. hhcd->hc[chnum].urb_state = URB_NOTREADY;
  921. }
  922. else if (hhcd->hc[chnum].state == HC_NYET)
  923. {
  924. hhcd->hc[chnum].urb_state = URB_NOTREADY;
  925. hhcd->hc[chnum].do_ping = 0;
  926. }
  927. else if (hhcd->hc[chnum].state == HC_STALL)
  928. {
  929. hhcd->hc[chnum].urb_state = URB_STALL;
  930. }
  931. else if((hhcd->hc[chnum].state == HC_XACTERR) ||
  932. (hhcd->hc[chnum].state == HC_DATATGLERR))
  933. {
  934. if(hhcd->hc[chnum].ErrCnt++ > 3)
  935. {
  936. hhcd->hc[chnum].ErrCnt = 0;
  937. hhcd->hc[chnum].urb_state = URB_ERROR;
  938. }
  939. else
  940. {
  941. hhcd->hc[chnum].urb_state = URB_NOTREADY;
  942. }
  943. /* re-activate the channel */
  944. tmpreg = USBx_HC(chnum)->HCCHAR;
  945. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  946. tmpreg |= USB_OTG_HCCHAR_CHENA;
  947. USBx_HC(chnum)->HCCHAR = tmpreg;
  948. }
  949. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_CHH);
  950. HAL_HCD_HC_NotifyURBChange_Callback(hhcd, chnum, hhcd->hc[chnum].urb_state);
  951. }
  952. }
  953. /**
  954. * @brief Handle Rx Queue Level interrupt requests.
  955. * @param hhcd: HCD handle
  956. * @retval none
  957. */
  958. static void HCD_RXQLVL_IRQHandler (HCD_HandleTypeDef *hhcd)
  959. {
  960. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  961. uint8_t channelnum =0;
  962. uint32_t pktsts;
  963. uint32_t pktcnt;
  964. uint32_t temp = 0;
  965. uint32_t tmpreg = 0;
  966. temp = hhcd->Instance->GRXSTSP ;
  967. channelnum = temp & USB_OTG_GRXSTSP_EPNUM;
  968. pktsts = (temp & USB_OTG_GRXSTSP_PKTSTS) >> 17;
  969. pktcnt = (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  970. switch (pktsts)
  971. {
  972. case GRXSTS_PKTSTS_IN:
  973. /* Read the data into the Host buffer. */
  974. if ((pktcnt > 0) && (hhcd->hc[channelnum].xfer_buff != (void *)0))
  975. {
  976. USB_ReadPacket(hhcd->Instance, hhcd->hc[channelnum].xfer_buff, pktcnt);
  977. /*manage multiple Xfer */
  978. hhcd->hc[channelnum].xfer_buff += pktcnt;
  979. hhcd->hc[channelnum].xfer_count += pktcnt;
  980. if((USBx_HC(channelnum)->HCTSIZ & USB_OTG_HCTSIZ_PKTCNT) > 0)
  981. {
  982. /* re-activate the channel when more packets are expected */
  983. tmpreg = USBx_HC(channelnum)->HCCHAR;
  984. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  985. tmpreg |= USB_OTG_HCCHAR_CHENA;
  986. USBx_HC(channelnum)->HCCHAR = tmpreg;
  987. hhcd->hc[channelnum].toggle_in ^= 1;
  988. }
  989. }
  990. break;
  991. case GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
  992. break;
  993. case GRXSTS_PKTSTS_IN_XFER_COMP:
  994. case GRXSTS_PKTSTS_CH_HALTED:
  995. default:
  996. break;
  997. }
  998. }
  999. /**
  1000. * @brief Handle Host Port interrupt requests.
  1001. * @param hhcd: HCD handle
  1002. * @retval None
  1003. */
  1004. static void HCD_Port_IRQHandler (HCD_HandleTypeDef *hhcd)
  1005. {
  1006. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  1007. __IO uint32_t hprt0, hprt0_dup;
  1008. /* Handle Host Port Interrupts */
  1009. hprt0 = USBx_HPRT0;
  1010. hprt0_dup = USBx_HPRT0;
  1011. hprt0_dup &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
  1012. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
  1013. /* Check whether Port Connect Detected */
  1014. if((hprt0 & USB_OTG_HPRT_PCDET) == USB_OTG_HPRT_PCDET)
  1015. {
  1016. if((hprt0 & USB_OTG_HPRT_PCSTS) == USB_OTG_HPRT_PCSTS)
  1017. {
  1018. USB_MASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_DISCINT);
  1019. HAL_HCD_Connect_Callback(hhcd);
  1020. }
  1021. hprt0_dup |= USB_OTG_HPRT_PCDET;
  1022. }
  1023. /* Check whether Port Enable Changed */
  1024. if((hprt0 & USB_OTG_HPRT_PENCHNG) == USB_OTG_HPRT_PENCHNG)
  1025. {
  1026. hprt0_dup |= USB_OTG_HPRT_PENCHNG;
  1027. if((hprt0 & USB_OTG_HPRT_PENA) == USB_OTG_HPRT_PENA)
  1028. {
  1029. if(hhcd->Init.phy_itface == USB_OTG_EMBEDDED_PHY)
  1030. {
  1031. if ((hprt0 & USB_OTG_HPRT_PSPD) == (HPRT0_PRTSPD_LOW_SPEED << 17))
  1032. {
  1033. USB_InitFSLSPClkSel(hhcd->Instance ,HCFG_6_MHZ );
  1034. }
  1035. else
  1036. {
  1037. USB_InitFSLSPClkSel(hhcd->Instance ,HCFG_48_MHZ );
  1038. }
  1039. }
  1040. else
  1041. {
  1042. if(hhcd->Init.speed == HCD_SPEED_FULL)
  1043. {
  1044. USBx_HOST->HFIR = (uint32_t)60000;
  1045. }
  1046. }
  1047. HAL_HCD_Connect_Callback(hhcd);
  1048. if(hhcd->Init.speed == HCD_SPEED_HIGH)
  1049. {
  1050. USB_UNMASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_DISCINT);
  1051. }
  1052. }
  1053. else
  1054. {
  1055. /* Cleanup HPRT */
  1056. USBx_HPRT0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
  1057. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
  1058. USB_UNMASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_DISCINT);
  1059. }
  1060. }
  1061. /* Check For an overcurrent */
  1062. if((hprt0 & USB_OTG_HPRT_POCCHNG) == USB_OTG_HPRT_POCCHNG)
  1063. {
  1064. hprt0_dup |= USB_OTG_HPRT_POCCHNG;
  1065. }
  1066. /* Clear Port Interrupts */
  1067. USBx_HPRT0 = hprt0_dup;
  1068. }
  1069. /**
  1070. * @}
  1071. */
  1072. /**
  1073. * @}
  1074. */
  1075. #endif /* HAL_HCD_MODULE_ENABLED */
  1076. /**
  1077. * @}
  1078. */
  1079. /**
  1080. * @}
  1081. */
  1082. #endif /* STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || */
  1083. /* STM32L496xx || STM32L4A6xx || */
  1084. /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1085. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/