stm32l4xx_hal_hcd.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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. // Added for MBED PR #3432
  180. /* reset to 0 */
  181. hhcd->hc[ch_num].toggle_out = 0;
  182. hhcd->hc[ch_num].toggle_in = 0;
  183. status = USB_HC_Init(hhcd->Instance,
  184. ch_num,
  185. epnum,
  186. dev_address,
  187. speed,
  188. ep_type,
  189. mps);
  190. __HAL_UNLOCK(hhcd);
  191. return status;
  192. }
  193. /**
  194. * @brief Halt a Host channel.
  195. * @param hhcd: HCD handle
  196. * @param ch_num: Channel number.
  197. * This parameter can be a value from 1 to 15
  198. * @retval HAL status
  199. */
  200. HAL_StatusTypeDef HAL_HCD_HC_Halt(HCD_HandleTypeDef *hhcd, uint8_t ch_num)
  201. {
  202. HAL_StatusTypeDef status = HAL_OK;
  203. __HAL_LOCK(hhcd);
  204. USB_HC_Halt(hhcd->Instance, ch_num);
  205. __HAL_UNLOCK(hhcd);
  206. return status;
  207. }
  208. /**
  209. * @brief DeInitialize the Host driver.
  210. * @param hhcd: HCD handle
  211. * @retval HAL status
  212. */
  213. HAL_StatusTypeDef HAL_HCD_DeInit(HCD_HandleTypeDef *hhcd)
  214. {
  215. /* Check the HCD handle allocation */
  216. if(hhcd == NULL)
  217. {
  218. return HAL_ERROR;
  219. }
  220. hhcd->State = HAL_HCD_STATE_BUSY;
  221. /* DeInit the low level hardware */
  222. HAL_HCD_MspDeInit(hhcd);
  223. __HAL_HCD_DISABLE(hhcd);
  224. hhcd->State = HAL_HCD_STATE_RESET;
  225. return HAL_OK;
  226. }
  227. /**
  228. * @brief Initialize the HCD MSP.
  229. * @param hhcd: HCD handle
  230. * @retval None
  231. */
  232. __weak void HAL_HCD_MspInit(HCD_HandleTypeDef *hhcd)
  233. {
  234. /* Prevent unused argument(s) compilation warning */
  235. UNUSED(hhcd);
  236. /* NOTE : This function should not be modified, when the callback is needed,
  237. the HAL_PCD_MspInit could be implemented in the user file
  238. */
  239. }
  240. /**
  241. * @brief DeInitialize the HCD MSP.
  242. * @param hhcd: HCD handle
  243. * @retval None
  244. */
  245. __weak void HAL_HCD_MspDeInit(HCD_HandleTypeDef *hhcd)
  246. {
  247. /* Prevent unused argument(s) compilation warning */
  248. UNUSED(hhcd);
  249. /* NOTE : This function should not be modified, when the callback is needed,
  250. the HAL_PCD_MspDeInit could be implemented in the user file
  251. */
  252. }
  253. /**
  254. * @}
  255. */
  256. /** @defgroup HCD_Exported_Functions_Group2 Input and Output operation functions
  257. * @brief HCD IO operation functions
  258. *
  259. @verbatim
  260. ===============================================================================
  261. ##### IO operation functions #####
  262. ===============================================================================
  263. [..] This subsection provides a set of functions allowing to manage the USB Host Data
  264. Transfer
  265. @endverbatim
  266. * @{
  267. */
  268. /**
  269. * @brief Submit a new URB for processing.
  270. * @param hhcd: HCD handle
  271. * @param ch_num: Channel number.
  272. * This parameter can be a value from 1 to 15
  273. * @param direction: Channel number.
  274. * This parameter can be one of these values:
  275. * 0 : Output / 1 : Input
  276. * @param ep_type: Endpoint Type.
  277. * This parameter can be one of these values:
  278. * EP_TYPE_CTRL: Control type/
  279. * EP_TYPE_ISOC: Isochronous type/
  280. * EP_TYPE_BULK: Bulk type/
  281. * EP_TYPE_INTR: Interrupt type/
  282. * @param token: Endpoint Type.
  283. * This parameter can be one of these values:
  284. * 0: HC_PID_SETUP / 1: HC_PID_DATA1
  285. * @param pbuff: pointer to URB data
  286. * @param length: Length of URB data
  287. * @param do_ping: activate do ping protocol (for high speed only).
  288. * This parameter can be one of these values:
  289. * 0 : do ping inactive / 1 : do ping active
  290. * @retval HAL status
  291. */
  292. HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
  293. uint8_t ch_num,
  294. uint8_t direction ,
  295. uint8_t ep_type,
  296. uint8_t token,
  297. uint8_t* pbuff,
  298. uint16_t length,
  299. uint8_t do_ping)
  300. {
  301. // Added for MBED PR #3432
  302. if ((hhcd->hc[ch_num].ep_is_in != direction)) {
  303. if ((hhcd->hc[ch_num].ep_type == EP_TYPE_CTRL)){
  304. /* reconfigure the endpoint !!! from tx -> rx, and rx ->tx */
  305. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  306. if (direction)
  307. {
  308. USBx_HC(ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
  309. USBx_HC(ch_num)->HCCHAR |= 1 << 15;
  310. }
  311. else
  312. {
  313. USBx_HC(ch_num)->HCINTMSK &= ~USB_OTG_HCINTMSK_BBERRM;
  314. USBx_HC(ch_num)->HCCHAR &= ~(1 << 15);
  315. }
  316. hhcd->hc[ch_num].ep_is_in = direction;
  317. /* if reception put toggle_in to 1 */
  318. if (direction == 1) hhcd->hc[ch_num].toggle_in=1;
  319. }
  320. }
  321. hhcd->hc[ch_num].ep_type = ep_type;
  322. if(token == 0)
  323. {
  324. hhcd->hc[ch_num].data_pid = HC_PID_SETUP;
  325. }
  326. else
  327. {
  328. hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
  329. }
  330. /* Manage Data Toggle */
  331. switch(ep_type)
  332. {
  333. case EP_TYPE_CTRL:
  334. if((token == 1) && (direction == 0)) /*send data */
  335. {
  336. if ( length == 0 )
  337. { /* For Status OUT stage, Length==0, Status Out PID = 1 */
  338. hhcd->hc[ch_num].toggle_out = 1;
  339. }
  340. /* Set the Data Toggle bit as per the Flag */
  341. if ( hhcd->hc[ch_num].toggle_out == 0)
  342. { /* Put the PID 0 */
  343. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  344. }
  345. else
  346. { /* Put the PID 1 */
  347. hhcd->hc[ch_num].data_pid = HC_PID_DATA1 ;
  348. }
  349. if(hhcd->hc[ch_num].urb_state != URB_NOTREADY)
  350. {
  351. hhcd->hc[ch_num].do_ping = do_ping;
  352. }
  353. }
  354. // Added for MBED PR #3432
  355. else if ((token == 1) && (direction == 1))
  356. {
  357. if( hhcd->hc[ch_num].toggle_in == 0)
  358. {
  359. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  360. }
  361. else
  362. {
  363. hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
  364. }
  365. }
  366. break;
  367. case EP_TYPE_BULK:
  368. if(direction == 0)
  369. {
  370. /* Set the Data Toggle bit as per the Flag */
  371. if ( hhcd->hc[ch_num].toggle_out == 0)
  372. { /* Put the PID 0 */
  373. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  374. }
  375. else
  376. { /* Put the PID 1 */
  377. hhcd->hc[ch_num].data_pid = HC_PID_DATA1 ;
  378. }
  379. if(hhcd->hc[ch_num].urb_state != URB_NOTREADY)
  380. {
  381. hhcd->hc[ch_num].do_ping = do_ping;
  382. }
  383. }
  384. else
  385. {
  386. if( hhcd->hc[ch_num].toggle_in == 0)
  387. {
  388. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  389. }
  390. else
  391. {
  392. hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
  393. }
  394. }
  395. break;
  396. case EP_TYPE_INTR:
  397. if(direction == 0)
  398. {
  399. /* Set the Data Toggle bit as per the Flag */
  400. if ( hhcd->hc[ch_num].toggle_out == 0)
  401. { /* Put the PID 0 */
  402. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  403. }
  404. else
  405. { /* Put the PID 1 */
  406. hhcd->hc[ch_num].data_pid = HC_PID_DATA1 ;
  407. }
  408. }
  409. else
  410. {
  411. if( hhcd->hc[ch_num].toggle_in == 0)
  412. {
  413. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  414. }
  415. else
  416. {
  417. hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
  418. }
  419. }
  420. break;
  421. case EP_TYPE_ISOC:
  422. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  423. break;
  424. }
  425. hhcd->hc[ch_num].xfer_buff = pbuff;
  426. hhcd->hc[ch_num].xfer_len = length;
  427. hhcd->hc[ch_num].urb_state = URB_IDLE;
  428. hhcd->hc[ch_num].xfer_count = 0 ;
  429. hhcd->hc[ch_num].ch_num = ch_num;
  430. hhcd->hc[ch_num].state = HC_IDLE;
  431. return USB_HC_StartXfer(hhcd->Instance, &(hhcd->hc[ch_num]), hhcd->Init.dma_enable);
  432. }
  433. /**
  434. * @brief Handle HCD interrupt request.
  435. * @param hhcd: HCD handle
  436. * @retval None
  437. */
  438. void HAL_HCD_IRQHandler(HCD_HandleTypeDef *hhcd)
  439. {
  440. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  441. uint32_t i = 0 , interrupt = 0;
  442. /* ensure that we are in device mode */
  443. if (USB_GetMode(hhcd->Instance) == USB_OTG_MODE_HOST)
  444. {
  445. /* avoid spurious interrupt */
  446. if(__HAL_HCD_IS_INVALID_INTERRUPT(hhcd))
  447. {
  448. return;
  449. }
  450. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
  451. {
  452. /* incorrect mode, acknowledge the interrupt */
  453. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
  454. }
  455. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_IISOIXFR))
  456. {
  457. /* incorrect mode, acknowledge the interrupt */
  458. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_IISOIXFR);
  459. }
  460. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_PTXFE))
  461. {
  462. /* incorrect mode, acknowledge the interrupt */
  463. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_PTXFE);
  464. }
  465. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_MMIS))
  466. {
  467. /* incorrect mode, acknowledge the interrupt */
  468. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_MMIS);
  469. }
  470. /* Handle Host Disconnect Interrupts */
  471. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_DISCINT))
  472. {
  473. /* Cleanup HPRT */
  474. USBx_HPRT0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
  475. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
  476. /* Handle Host Port Interrupts */
  477. HAL_HCD_Disconnect_Callback(hhcd);
  478. USB_InitFSLSPClkSel(hhcd->Instance ,HCFG_48_MHZ );
  479. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_DISCINT);
  480. }
  481. /* Handle Host Port Interrupts */
  482. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_HPRTINT))
  483. {
  484. HCD_Port_IRQHandler (hhcd);
  485. }
  486. /* Handle Host SOF Interrupts */
  487. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_SOF))
  488. {
  489. HAL_HCD_SOF_Callback(hhcd);
  490. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_SOF);
  491. }
  492. /* Handle Host channel Interrupts */
  493. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_HCINT))
  494. {
  495. interrupt = USB_HC_ReadInterrupt(hhcd->Instance);
  496. for (i = 0; i < hhcd->Init.Host_channels ; i++)
  497. {
  498. if (interrupt & (1 << i))
  499. {
  500. if ((USBx_HC(i)->HCCHAR) & USB_OTG_HCCHAR_EPDIR)
  501. {
  502. HCD_HC_IN_IRQHandler (hhcd, i);
  503. }
  504. else
  505. {
  506. HCD_HC_OUT_IRQHandler (hhcd, i);
  507. }
  508. }
  509. }
  510. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_HCINT);
  511. }
  512. /* Handle Rx Queue Level Interrupts */
  513. if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_RXFLVL))
  514. {
  515. USB_MASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  516. HCD_RXQLVL_IRQHandler (hhcd);
  517. USB_UNMASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  518. }
  519. }
  520. }
  521. /**
  522. * @brief SOF callback.
  523. * @param hhcd: HCD handle
  524. * @retval None
  525. */
  526. __weak void HAL_HCD_SOF_Callback(HCD_HandleTypeDef *hhcd)
  527. {
  528. /* Prevent unused argument(s) compilation warning */
  529. UNUSED(hhcd);
  530. /* NOTE : This function should not be modified, when the callback is needed,
  531. the HAL_HCD_SOF_Callback could be implemented in the user file
  532. */
  533. }
  534. /**
  535. * @brief Connection Event callback.
  536. * @param hhcd: HCD handle
  537. * @retval None
  538. */
  539. __weak void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd)
  540. {
  541. /* Prevent unused argument(s) compilation warning */
  542. UNUSED(hhcd);
  543. /* NOTE : This function should not be modified, when the callback is needed,
  544. the HAL_HCD_Connect_Callback could be implemented in the user file
  545. */
  546. }
  547. /**
  548. * @brief Disconnection Event callback.
  549. * @param hhcd: HCD handle
  550. * @retval None
  551. */
  552. __weak void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd)
  553. {
  554. /* Prevent unused argument(s) compilation warning */
  555. UNUSED(hhcd);
  556. /* NOTE : This function should not be modified, when the callback is needed,
  557. the HAL_HCD_Disconnect_Callback could be implemented in the user file
  558. */
  559. }
  560. /**
  561. * @brief Notify URB state change callback.
  562. * @param hhcd: HCD handle
  563. * @param chnum: Channel number.
  564. * This parameter can be a value from 1 to 15
  565. * @param urb_state:
  566. * This parameter can be one of these values:
  567. * URB_IDLE/
  568. * URB_DONE/
  569. * URB_NOTREADY/
  570. * URB_NYET/
  571. * URB_ERROR/
  572. * URB_STALL/
  573. * @retval None
  574. */
  575. __weak void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum, HCD_URBStateTypeDef urb_state)
  576. {
  577. /* Prevent unused argument(s) compilation warning */
  578. UNUSED(hhcd);
  579. UNUSED(chnum);
  580. UNUSED(urb_state);
  581. /* NOTE : This function should not be modified, when the callback is needed,
  582. the HAL_HCD_HC_NotifyURBChange_Callback could be implemented in the user file
  583. */
  584. }
  585. /**
  586. * @}
  587. */
  588. /** @defgroup HCD_Exported_Functions_Group3 Peripheral Control functions
  589. * @brief Management functions
  590. *
  591. @verbatim
  592. ===============================================================================
  593. ##### Peripheral Control functions #####
  594. ===============================================================================
  595. [..]
  596. This subsection provides a set of functions allowing to control the HCD data
  597. transfers.
  598. @endverbatim
  599. * @{
  600. */
  601. /**
  602. * @brief Start the Host driver.
  603. * @param hhcd: HCD handle
  604. * @retval HAL status
  605. */
  606. HAL_StatusTypeDef HAL_HCD_Start(HCD_HandleTypeDef *hhcd)
  607. {
  608. __HAL_LOCK(hhcd);
  609. __HAL_HCD_ENABLE(hhcd);
  610. USB_DriveVbus(hhcd->Instance, 1);
  611. __HAL_UNLOCK(hhcd);
  612. return HAL_OK;
  613. }
  614. /**
  615. * @brief Stop the Host driver.
  616. * @param hhcd: HCD handle
  617. * @retval HAL status
  618. */
  619. HAL_StatusTypeDef HAL_HCD_Stop(HCD_HandleTypeDef *hhcd)
  620. {
  621. __HAL_LOCK(hhcd);
  622. USB_StopHost(hhcd->Instance);
  623. __HAL_UNLOCK(hhcd);
  624. return HAL_OK;
  625. }
  626. /**
  627. * @brief Reset the Host port.
  628. * @param hhcd: HCD handle
  629. * @retval HAL status
  630. */
  631. HAL_StatusTypeDef HAL_HCD_ResetPort(HCD_HandleTypeDef *hhcd)
  632. {
  633. return (USB_ResetPort(hhcd->Instance));
  634. }
  635. /**
  636. * @}
  637. */
  638. /** @defgroup HCD_Exported_Functions_Group4 Peripheral State functions
  639. * @brief Peripheral State functions
  640. *
  641. @verbatim
  642. ===============================================================================
  643. ##### Peripheral State functions #####
  644. ===============================================================================
  645. [..]
  646. This subsection permits to get in run-time the status of the peripheral
  647. and the data flow.
  648. @endverbatim
  649. * @{
  650. */
  651. /**
  652. * @brief Return the HCD handle state.
  653. * @param hhcd: HCD handle
  654. * @retval HAL state
  655. */
  656. HCD_StateTypeDef HAL_HCD_GetState(HCD_HandleTypeDef *hhcd)
  657. {
  658. return hhcd->State;
  659. }
  660. /**
  661. * @brief Return URB state for a channel.
  662. * @param hhcd: HCD handle
  663. * @param chnum: Channel number.
  664. * This parameter can be a value from 1 to 15
  665. * @retval URB state.
  666. * This parameter can be one of these values:
  667. * URB_IDLE/
  668. * URB_DONE/
  669. * URB_NOTREADY/
  670. * URB_NYET/
  671. * URB_ERROR/
  672. * URB_STALL
  673. */
  674. HCD_URBStateTypeDef HAL_HCD_HC_GetURBState(HCD_HandleTypeDef *hhcd, uint8_t chnum)
  675. {
  676. return hhcd->hc[chnum].urb_state;
  677. }
  678. /**
  679. * @brief Return the last Host transfer size.
  680. * @param hhcd: HCD handle
  681. * @param chnum: Channel number.
  682. * This parameter can be a value from 1 to 15
  683. * @retval last transfer size in byte
  684. */
  685. uint32_t HAL_HCD_HC_GetXferCount(HCD_HandleTypeDef *hhcd, uint8_t chnum)
  686. {
  687. return hhcd->hc[chnum].xfer_count;
  688. }
  689. /**
  690. * @brief Return the Host Channel state.
  691. * @param hhcd: HCD handle
  692. * @param chnum: Channel number.
  693. * This parameter can be a value from 1 to 15
  694. * @retval Host channel state
  695. * This parameter can be one of these values:
  696. * HC_IDLE/
  697. * HC_XFRC/
  698. * HC_HALTED/
  699. * HC_NYET/
  700. * HC_NAK/
  701. * HC_STALL/
  702. * HC_XACTERR/
  703. * HC_BBLERR/
  704. * HC_DATATGLERR
  705. */
  706. HCD_HCStateTypeDef HAL_HCD_HC_GetState(HCD_HandleTypeDef *hhcd, uint8_t chnum)
  707. {
  708. return hhcd->hc[chnum].state;
  709. }
  710. /**
  711. * @brief Return the current Host frame number.
  712. * @param hhcd: HCD handle
  713. * @retval Current Host frame number
  714. */
  715. uint32_t HAL_HCD_GetCurrentFrame(HCD_HandleTypeDef *hhcd)
  716. {
  717. return (USB_GetCurrentFrame(hhcd->Instance));
  718. }
  719. /**
  720. * @brief Return the Host enumeration speed.
  721. * @param hhcd: HCD handle
  722. * @retval Enumeration speed
  723. */
  724. uint32_t HAL_HCD_GetCurrentSpeed(HCD_HandleTypeDef *hhcd)
  725. {
  726. return (USB_GetHostSpeed(hhcd->Instance));
  727. }
  728. /**
  729. * @}
  730. */
  731. /**
  732. * @}
  733. */
  734. /** @addtogroup HCD_Private_Functions
  735. * @{
  736. */
  737. /**
  738. * @brief Handle Host Channel IN interrupt requests.
  739. * @param hhcd: HCD handle
  740. * @param chnum: Channel number.
  741. * This parameter can be a value from 1 to 15
  742. * @retval none
  743. */
  744. static void HCD_HC_IN_IRQHandler (HCD_HandleTypeDef *hhcd, uint8_t chnum)
  745. {
  746. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  747. uint32_t tmpreg = 0;
  748. if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_AHBERR)
  749. {
  750. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_AHBERR);
  751. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  752. }
  753. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_ACK)
  754. {
  755. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_ACK);
  756. }
  757. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_STALL)
  758. {
  759. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  760. hhcd->hc[chnum].state = HC_STALL;
  761. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
  762. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_STALL);
  763. USB_HC_Halt(hhcd->Instance, chnum);
  764. }
  765. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_DTERR)
  766. {
  767. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  768. USB_HC_Halt(hhcd->Instance, chnum);
  769. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
  770. hhcd->hc[chnum].state = HC_DATATGLERR;
  771. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_DTERR);
  772. }
  773. if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_FRMOR)
  774. {
  775. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  776. USB_HC_Halt(hhcd->Instance, chnum);
  777. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_FRMOR);
  778. }
  779. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_XFRC)
  780. {
  781. if (hhcd->Init.dma_enable)
  782. {
  783. hhcd->hc[chnum].xfer_count = hhcd->hc[chnum].xfer_len - \
  784. (USBx_HC(chnum)->HCTSIZ & USB_OTG_HCTSIZ_XFRSIZ);
  785. }
  786. hhcd->hc[chnum].state = HC_XFRC;
  787. hhcd->hc[chnum].ErrCnt = 0;
  788. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_XFRC);
  789. if ((hhcd->hc[chnum].ep_type == EP_TYPE_CTRL)||
  790. (hhcd->hc[chnum].ep_type == EP_TYPE_BULK))
  791. {
  792. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  793. USB_HC_Halt(hhcd->Instance, chnum);
  794. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
  795. }
  796. else if(hhcd->hc[chnum].ep_type == EP_TYPE_INTR)
  797. {
  798. USBx_HC(chnum)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM;
  799. hhcd->hc[chnum].urb_state = URB_DONE;
  800. HAL_HCD_HC_NotifyURBChange_Callback(hhcd, chnum, hhcd->hc[chnum].urb_state);
  801. }
  802. hhcd->hc[chnum].toggle_in ^= 1;
  803. }
  804. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_CHH)
  805. {
  806. // Added for MBED PR #3432
  807. int reactivate = 0;
  808. __HAL_HCD_MASK_HALT_HC_INT(chnum);
  809. if(hhcd->hc[chnum].state == HC_XFRC)
  810. {
  811. hhcd->hc[chnum].urb_state = URB_DONE;
  812. }
  813. else if (hhcd->hc[chnum].state == HC_STALL)
  814. {
  815. hhcd->hc[chnum].urb_state = URB_STALL;
  816. }
  817. else if((hhcd->hc[chnum].state == HC_XACTERR) ||
  818. (hhcd->hc[chnum].state == HC_DATATGLERR))
  819. {
  820. if(hhcd->hc[chnum].ErrCnt++ > 3)
  821. {
  822. hhcd->hc[chnum].ErrCnt = 0;
  823. hhcd->hc[chnum].urb_state = URB_ERROR;
  824. }
  825. else
  826. {
  827. hhcd->hc[chnum].urb_state = URB_NOTREADY;
  828. }
  829. /* re-activate the channel */
  830. tmpreg = USBx_HC(chnum)->HCCHAR;
  831. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  832. // Added for MBED PR #3432 #4231
  833. if ( hhcd->hc[chnum].urb_state != URB_ERROR) {
  834. tmpreg |= USB_OTG_HCCHAR_CHENA;
  835. reactivate = 1;
  836. }
  837. USBx_HC(chnum)->HCCHAR = tmpreg;
  838. }
  839. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_CHH);
  840. // Added for MBED PR #3432 #4231
  841. if (hhcd->hc[chnum].state == 0) reactivate = 1;
  842. if (reactivate == 0) HAL_HCD_HC_NotifyURBChange_Callback(hhcd, chnum, hhcd->hc[chnum].urb_state);
  843. }
  844. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_TXERR)
  845. {
  846. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  847. hhcd->hc[chnum].ErrCnt++;
  848. hhcd->hc[chnum].state = HC_XACTERR;
  849. USB_HC_Halt(hhcd->Instance, chnum);
  850. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_TXERR);
  851. }
  852. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NAK)
  853. {
  854. if(hhcd->hc[chnum].ep_type == EP_TYPE_INTR)
  855. {
  856. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  857. USB_HC_Halt(hhcd->Instance, chnum);
  858. }
  859. /* Clear the NAK flag before re-enabling the channel for new IN request */
  860. hhcd->hc[chnum].state = HC_NAK;
  861. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
  862. if ((hhcd->hc[chnum].ep_type == EP_TYPE_CTRL)||
  863. (hhcd->hc[chnum].ep_type == EP_TYPE_BULK))
  864. {
  865. /* re-activate the channel */
  866. USBx_HC(chnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHDIS;
  867. USBx_HC(chnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  868. }
  869. }
  870. }
  871. /**
  872. * @brief Handle Host Channel OUT interrupt requests.
  873. * @param hhcd: HCD handle
  874. * @param chnum: Channel number.
  875. * This parameter can be a value from 1 to 15
  876. * @retval none
  877. */
  878. static void HCD_HC_OUT_IRQHandler (HCD_HandleTypeDef *hhcd, uint8_t chnum)
  879. {
  880. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  881. uint32_t tmpreg = 0;
  882. if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_AHBERR)
  883. {
  884. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_AHBERR);
  885. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  886. }
  887. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_ACK)
  888. {
  889. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_ACK);
  890. if( hhcd->hc[chnum].do_ping == 1)
  891. {
  892. hhcd->hc[chnum].state = HC_NYET;
  893. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  894. USB_HC_Halt(hhcd->Instance, chnum);
  895. hhcd->hc[chnum].urb_state = URB_NOTREADY;
  896. }
  897. }
  898. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NYET)
  899. {
  900. hhcd->hc[chnum].state = HC_NYET;
  901. hhcd->hc[chnum].ErrCnt= 0;
  902. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  903. USB_HC_Halt(hhcd->Instance, chnum);
  904. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NYET);
  905. }
  906. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_FRMOR)
  907. {
  908. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  909. USB_HC_Halt(hhcd->Instance, chnum);
  910. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_FRMOR);
  911. }
  912. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_XFRC)
  913. {
  914. hhcd->hc[chnum].ErrCnt = 0;
  915. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  916. USB_HC_Halt(hhcd->Instance, chnum);
  917. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_XFRC);
  918. hhcd->hc[chnum].state = HC_XFRC;
  919. }
  920. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_STALL)
  921. {
  922. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_STALL);
  923. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  924. USB_HC_Halt(hhcd->Instance, chnum);
  925. hhcd->hc[chnum].state = HC_STALL;
  926. }
  927. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NAK)
  928. {
  929. hhcd->hc[chnum].ErrCnt = 0;
  930. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  931. USB_HC_Halt(hhcd->Instance, chnum);
  932. hhcd->hc[chnum].state = HC_NAK;
  933. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
  934. }
  935. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_TXERR)
  936. {
  937. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  938. USB_HC_Halt(hhcd->Instance, chnum);
  939. hhcd->hc[chnum].state = HC_XACTERR;
  940. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_TXERR);
  941. }
  942. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_DTERR)
  943. {
  944. __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
  945. USB_HC_Halt(hhcd->Instance, chnum);
  946. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
  947. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_DTERR);
  948. hhcd->hc[chnum].state = HC_DATATGLERR;
  949. }
  950. else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_CHH)
  951. {
  952. __HAL_HCD_MASK_HALT_HC_INT(chnum);
  953. if(hhcd->hc[chnum].state == HC_XFRC)
  954. {
  955. hhcd->hc[chnum].urb_state = URB_DONE;
  956. if (hhcd->hc[chnum].ep_type == EP_TYPE_BULK)
  957. {
  958. hhcd->hc[chnum].toggle_out ^= 1;
  959. }
  960. }
  961. else if (hhcd->hc[chnum].state == HC_NAK)
  962. {
  963. hhcd->hc[chnum].urb_state = URB_NOTREADY;
  964. }
  965. else if (hhcd->hc[chnum].state == HC_NYET)
  966. {
  967. hhcd->hc[chnum].urb_state = URB_NOTREADY;
  968. hhcd->hc[chnum].do_ping = 0;
  969. }
  970. else if (hhcd->hc[chnum].state == HC_STALL)
  971. {
  972. hhcd->hc[chnum].urb_state = URB_STALL;
  973. }
  974. else if((hhcd->hc[chnum].state == HC_XACTERR) ||
  975. (hhcd->hc[chnum].state == HC_DATATGLERR))
  976. {
  977. if(hhcd->hc[chnum].ErrCnt++ > 3)
  978. {
  979. hhcd->hc[chnum].ErrCnt = 0;
  980. hhcd->hc[chnum].urb_state = URB_ERROR;
  981. }
  982. else
  983. {
  984. hhcd->hc[chnum].urb_state = URB_NOTREADY;
  985. }
  986. /* re-activate the channel */
  987. tmpreg = USBx_HC(chnum)->HCCHAR;
  988. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  989. tmpreg |= USB_OTG_HCCHAR_CHENA;
  990. USBx_HC(chnum)->HCCHAR = tmpreg;
  991. }
  992. __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_CHH);
  993. HAL_HCD_HC_NotifyURBChange_Callback(hhcd, chnum, hhcd->hc[chnum].urb_state);
  994. }
  995. }
  996. /**
  997. * @brief Handle Rx Queue Level interrupt requests.
  998. * @param hhcd: HCD handle
  999. * @retval none
  1000. */
  1001. static void HCD_RXQLVL_IRQHandler (HCD_HandleTypeDef *hhcd)
  1002. {
  1003. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  1004. uint8_t channelnum =0;
  1005. uint32_t pktsts;
  1006. uint32_t pktcnt;
  1007. uint32_t temp = 0;
  1008. uint32_t tmpreg = 0;
  1009. temp = hhcd->Instance->GRXSTSP ;
  1010. channelnum = temp & USB_OTG_GRXSTSP_EPNUM;
  1011. pktsts = (temp & USB_OTG_GRXSTSP_PKTSTS) >> 17;
  1012. pktcnt = (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  1013. switch (pktsts)
  1014. {
  1015. case GRXSTS_PKTSTS_IN:
  1016. /* Read the data into the Host buffer. */
  1017. if ((pktcnt > 0) && (hhcd->hc[channelnum].xfer_buff != (void *)0))
  1018. {
  1019. USB_ReadPacket(hhcd->Instance, hhcd->hc[channelnum].xfer_buff, pktcnt);
  1020. /*manage multiple Xfer */
  1021. hhcd->hc[channelnum].xfer_buff += pktcnt;
  1022. hhcd->hc[channelnum].xfer_count += pktcnt;
  1023. if((USBx_HC(channelnum)->HCTSIZ & USB_OTG_HCTSIZ_PKTCNT) > 0)
  1024. {
  1025. /* re-activate the channel when more packets are expected */
  1026. tmpreg = USBx_HC(channelnum)->HCCHAR;
  1027. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  1028. tmpreg |= USB_OTG_HCCHAR_CHENA;
  1029. USBx_HC(channelnum)->HCCHAR = tmpreg;
  1030. hhcd->hc[channelnum].toggle_in ^= 1;
  1031. }
  1032. }
  1033. break;
  1034. case GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
  1035. break;
  1036. case GRXSTS_PKTSTS_IN_XFER_COMP:
  1037. case GRXSTS_PKTSTS_CH_HALTED:
  1038. default:
  1039. break;
  1040. }
  1041. }
  1042. /**
  1043. * @brief Handle Host Port interrupt requests.
  1044. * @param hhcd: HCD handle
  1045. * @retval None
  1046. */
  1047. static void HCD_Port_IRQHandler (HCD_HandleTypeDef *hhcd)
  1048. {
  1049. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  1050. __IO uint32_t hprt0, hprt0_dup;
  1051. /* Handle Host Port Interrupts */
  1052. hprt0 = USBx_HPRT0;
  1053. hprt0_dup = USBx_HPRT0;
  1054. hprt0_dup &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
  1055. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
  1056. /* Check whether Port Connect Detected */
  1057. if((hprt0 & USB_OTG_HPRT_PCDET) == USB_OTG_HPRT_PCDET)
  1058. {
  1059. if((hprt0 & USB_OTG_HPRT_PCSTS) == USB_OTG_HPRT_PCSTS)
  1060. {
  1061. USB_MASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_DISCINT);
  1062. HAL_HCD_Connect_Callback(hhcd);
  1063. }
  1064. hprt0_dup |= USB_OTG_HPRT_PCDET;
  1065. }
  1066. /* Check whether Port Enable Changed */
  1067. if((hprt0 & USB_OTG_HPRT_PENCHNG) == USB_OTG_HPRT_PENCHNG)
  1068. {
  1069. hprt0_dup |= USB_OTG_HPRT_PENCHNG;
  1070. if((hprt0 & USB_OTG_HPRT_PENA) == USB_OTG_HPRT_PENA)
  1071. {
  1072. if(hhcd->Init.phy_itface == USB_OTG_EMBEDDED_PHY)
  1073. {
  1074. if ((hprt0 & USB_OTG_HPRT_PSPD) == (HPRT0_PRTSPD_LOW_SPEED << 17))
  1075. {
  1076. USB_InitFSLSPClkSel(hhcd->Instance ,HCFG_6_MHZ );
  1077. }
  1078. else
  1079. {
  1080. USB_InitFSLSPClkSel(hhcd->Instance ,HCFG_48_MHZ );
  1081. }
  1082. }
  1083. else
  1084. {
  1085. if(hhcd->Init.speed == HCD_SPEED_FULL)
  1086. {
  1087. USBx_HOST->HFIR = (uint32_t)60000;
  1088. }
  1089. }
  1090. HAL_HCD_Connect_Callback(hhcd);
  1091. if(hhcd->Init.speed == HCD_SPEED_HIGH)
  1092. {
  1093. USB_UNMASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_DISCINT);
  1094. }
  1095. }
  1096. else
  1097. {
  1098. /* Cleanup HPRT */
  1099. USBx_HPRT0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
  1100. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
  1101. USB_UNMASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_DISCINT);
  1102. }
  1103. }
  1104. /* Check For an overcurrent */
  1105. if((hprt0 & USB_OTG_HPRT_POCCHNG) == USB_OTG_HPRT_POCCHNG)
  1106. {
  1107. hprt0_dup |= USB_OTG_HPRT_POCCHNG;
  1108. }
  1109. /* Clear Port Interrupts */
  1110. USBx_HPRT0 = hprt0_dup;
  1111. }
  1112. /**
  1113. * @}
  1114. */
  1115. /**
  1116. * @}
  1117. */
  1118. #endif /* HAL_HCD_MODULE_ENABLED */
  1119. /**
  1120. * @}
  1121. */
  1122. /**
  1123. * @}
  1124. */
  1125. #endif /* STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || */
  1126. /* STM32L496xx || STM32L4A6xx || */
  1127. /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1128. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/