stm32f4xx_hal_pcd.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_pcd.c
  4. * @author MCD Application Team
  5. * @brief PCD HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the USB Peripheral Controller:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2016 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. The PCD HAL driver can be used as follows:
  30. (#) Declare a PCD_HandleTypeDef handle structure, for example:
  31. PCD_HandleTypeDef hpcd;
  32. (#) Fill parameters of Init structure in HCD handle
  33. (#) Call HAL_PCD_Init() API to initialize the PCD peripheral (Core, Device core, ...)
  34. (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
  35. (##) Enable the PCD/USB Low Level interface clock using
  36. (+++) __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
  37. (+++) __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); (For High Speed Mode)
  38. (##) Initialize the related GPIO clocks
  39. (##) Configure PCD pin-out
  40. (##) Configure PCD NVIC interrupt
  41. (#)Associate the Upper USB device stack to the HAL PCD Driver:
  42. (##) hpcd.pData = pdev;
  43. (#)Enable PCD transmission and reception:
  44. (##) HAL_PCD_Start();
  45. @endverbatim
  46. ******************************************************************************
  47. */
  48. /* Includes ------------------------------------------------------------------*/
  49. #include "stm32f4xx_hal.h"
  50. /** @addtogroup STM32F4xx_HAL_Driver
  51. * @{
  52. */
  53. /** @defgroup PCD PCD
  54. * @brief PCD HAL module driver
  55. * @{
  56. */
  57. #ifdef HAL_PCD_MODULE_ENABLED
  58. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  59. /* Private types -------------------------------------------------------------*/
  60. /* Private variables ---------------------------------------------------------*/
  61. /* Private constants ---------------------------------------------------------*/
  62. /* Private macros ------------------------------------------------------------*/
  63. /** @defgroup PCD_Private_Macros PCD Private Macros
  64. * @{
  65. */
  66. #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
  67. #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
  68. /**
  69. * @}
  70. */
  71. /* Private functions prototypes ----------------------------------------------*/
  72. /** @defgroup PCD_Private_Functions PCD Private Functions
  73. * @{
  74. */
  75. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  76. static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
  77. static HAL_StatusTypeDef PCD_EP_OutXfrComplete_int(PCD_HandleTypeDef *hpcd, uint32_t epnum);
  78. static HAL_StatusTypeDef PCD_EP_OutSetupPacket_int(PCD_HandleTypeDef *hpcd, uint32_t epnum);
  79. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  80. /**
  81. * @}
  82. */
  83. /* Exported functions --------------------------------------------------------*/
  84. /** @defgroup PCD_Exported_Functions PCD Exported Functions
  85. * @{
  86. */
  87. /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
  88. * @brief Initialization and Configuration functions
  89. *
  90. @verbatim
  91. ===============================================================================
  92. ##### Initialization and de-initialization functions #####
  93. ===============================================================================
  94. [..] This section provides functions allowing to:
  95. @endverbatim
  96. * @{
  97. */
  98. /**
  99. * @brief Initializes the PCD according to the specified
  100. * parameters in the PCD_InitTypeDef and initialize the associated handle.
  101. * @param hpcd PCD handle
  102. * @retval HAL status
  103. */
  104. HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
  105. {
  106. #if defined (USB_OTG_FS)
  107. const USB_OTG_GlobalTypeDef *USBx;
  108. #endif /* defined (USB_OTG_FS) */
  109. uint8_t i;
  110. /* Check the PCD handle allocation */
  111. if (hpcd == NULL)
  112. {
  113. return HAL_ERROR;
  114. }
  115. /* Check the parameters */
  116. assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
  117. #if defined (USB_OTG_FS)
  118. USBx = hpcd->Instance;
  119. #endif /* defined (USB_OTG_FS) */
  120. if (hpcd->State == HAL_PCD_STATE_RESET)
  121. {
  122. /* Allocate lock resource and initialize it */
  123. hpcd->Lock = HAL_UNLOCKED;
  124. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  125. hpcd->SOFCallback = HAL_PCD_SOFCallback;
  126. hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
  127. hpcd->ResetCallback = HAL_PCD_ResetCallback;
  128. hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
  129. hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
  130. hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
  131. hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
  132. hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback;
  133. hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback;
  134. hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback;
  135. hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback;
  136. hpcd->LPMCallback = HAL_PCDEx_LPM_Callback;
  137. hpcd->BCDCallback = HAL_PCDEx_BCD_Callback;
  138. if (hpcd->MspInitCallback == NULL)
  139. {
  140. hpcd->MspInitCallback = HAL_PCD_MspInit;
  141. }
  142. /* Init the low level hardware */
  143. hpcd->MspInitCallback(hpcd);
  144. #else
  145. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  146. HAL_PCD_MspInit(hpcd);
  147. #endif /* (USE_HAL_PCD_REGISTER_CALLBACKS) */
  148. }
  149. hpcd->State = HAL_PCD_STATE_BUSY;
  150. #if defined (USB_OTG_FS)
  151. /* Disable DMA mode for FS instance */
  152. if (USBx == USB_OTG_FS)
  153. {
  154. hpcd->Init.dma_enable = 0U;
  155. }
  156. #endif /* defined (USB_OTG_FS) */
  157. /* Disable the Interrupts */
  158. __HAL_PCD_DISABLE(hpcd);
  159. /*Init the Core (common init.) */
  160. if (USB_CoreInit(hpcd->Instance, hpcd->Init) != HAL_OK)
  161. {
  162. hpcd->State = HAL_PCD_STATE_ERROR;
  163. return HAL_ERROR;
  164. }
  165. /* Force Device Mode */
  166. if (USB_SetCurrentMode(hpcd->Instance, USB_DEVICE_MODE) != HAL_OK)
  167. {
  168. hpcd->State = HAL_PCD_STATE_ERROR;
  169. return HAL_ERROR;
  170. }
  171. /* Init endpoints structures */
  172. for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
  173. {
  174. /* Init ep structure */
  175. hpcd->IN_ep[i].is_in = 1U;
  176. hpcd->IN_ep[i].num = i;
  177. hpcd->IN_ep[i].tx_fifo_num = i;
  178. /* Control until ep is activated */
  179. hpcd->IN_ep[i].type = EP_TYPE_CTRL;
  180. hpcd->IN_ep[i].maxpacket = 0U;
  181. hpcd->IN_ep[i].xfer_buff = 0U;
  182. hpcd->IN_ep[i].xfer_len = 0U;
  183. }
  184. for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
  185. {
  186. hpcd->OUT_ep[i].is_in = 0U;
  187. hpcd->OUT_ep[i].num = i;
  188. /* Control until ep is activated */
  189. hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
  190. hpcd->OUT_ep[i].maxpacket = 0U;
  191. hpcd->OUT_ep[i].xfer_buff = 0U;
  192. hpcd->OUT_ep[i].xfer_len = 0U;
  193. }
  194. /* Init Device */
  195. if (USB_DevInit(hpcd->Instance, hpcd->Init) != HAL_OK)
  196. {
  197. hpcd->State = HAL_PCD_STATE_ERROR;
  198. return HAL_ERROR;
  199. }
  200. hpcd->USB_Address = 0U;
  201. hpcd->State = HAL_PCD_STATE_READY;
  202. #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) \
  203. || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) \
  204. || defined(STM32F423xx)
  205. /* Activate LPM */
  206. if (hpcd->Init.lpm_enable == 1U)
  207. {
  208. (void)HAL_PCDEx_ActivateLPM(hpcd);
  209. }
  210. #endif /* defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) ||
  211. defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) ||
  212. defined(STM32F423xx) */
  213. (void)USB_DevDisconnect(hpcd->Instance);
  214. return HAL_OK;
  215. }
  216. /**
  217. * @brief DeInitializes the PCD peripheral.
  218. * @param hpcd PCD handle
  219. * @retval HAL status
  220. */
  221. HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
  222. {
  223. /* Check the PCD handle allocation */
  224. if (hpcd == NULL)
  225. {
  226. return HAL_ERROR;
  227. }
  228. hpcd->State = HAL_PCD_STATE_BUSY;
  229. /* Stop Device */
  230. if (USB_StopDevice(hpcd->Instance) != HAL_OK)
  231. {
  232. return HAL_ERROR;
  233. }
  234. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  235. if (hpcd->MspDeInitCallback == NULL)
  236. {
  237. hpcd->MspDeInitCallback = HAL_PCD_MspDeInit; /* Legacy weak MspDeInit */
  238. }
  239. /* DeInit the low level hardware */
  240. hpcd->MspDeInitCallback(hpcd);
  241. #else
  242. /* DeInit the low level hardware: CLOCK, NVIC.*/
  243. HAL_PCD_MspDeInit(hpcd);
  244. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  245. hpcd->State = HAL_PCD_STATE_RESET;
  246. return HAL_OK;
  247. }
  248. /**
  249. * @brief Initializes the PCD MSP.
  250. * @param hpcd PCD handle
  251. * @retval None
  252. */
  253. __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
  254. {
  255. /* Prevent unused argument(s) compilation warning */
  256. UNUSED(hpcd);
  257. /* NOTE : This function should not be modified, when the callback is needed,
  258. the HAL_PCD_MspInit could be implemented in the user file
  259. */
  260. }
  261. /**
  262. * @brief DeInitializes PCD MSP.
  263. * @param hpcd PCD handle
  264. * @retval None
  265. */
  266. __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
  267. {
  268. /* Prevent unused argument(s) compilation warning */
  269. UNUSED(hpcd);
  270. /* NOTE : This function should not be modified, when the callback is needed,
  271. the HAL_PCD_MspDeInit could be implemented in the user file
  272. */
  273. }
  274. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  275. /**
  276. * @brief Register a User USB PCD Callback
  277. * To be used instead of the weak predefined callback
  278. * @param hpcd USB PCD handle
  279. * @param CallbackID ID of the callback to be registered
  280. * This parameter can be one of the following values:
  281. * @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
  282. * @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
  283. * @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
  284. * @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
  285. * @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
  286. * @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
  287. * @arg @ref HAL_PCD_DISCONNECT_CB_ID USB PCD Disconnect callback ID
  288. * @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
  289. * @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
  290. * @param pCallback pointer to the Callback function
  291. * @retval HAL status
  292. */
  293. HAL_StatusTypeDef HAL_PCD_RegisterCallback(PCD_HandleTypeDef *hpcd,
  294. HAL_PCD_CallbackIDTypeDef CallbackID,
  295. pPCD_CallbackTypeDef pCallback)
  296. {
  297. HAL_StatusTypeDef status = HAL_OK;
  298. if (pCallback == NULL)
  299. {
  300. /* Update the error code */
  301. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  302. return HAL_ERROR;
  303. }
  304. /* Process locked */
  305. __HAL_LOCK(hpcd);
  306. if (hpcd->State == HAL_PCD_STATE_READY)
  307. {
  308. switch (CallbackID)
  309. {
  310. case HAL_PCD_SOF_CB_ID :
  311. hpcd->SOFCallback = pCallback;
  312. break;
  313. case HAL_PCD_SETUPSTAGE_CB_ID :
  314. hpcd->SetupStageCallback = pCallback;
  315. break;
  316. case HAL_PCD_RESET_CB_ID :
  317. hpcd->ResetCallback = pCallback;
  318. break;
  319. case HAL_PCD_SUSPEND_CB_ID :
  320. hpcd->SuspendCallback = pCallback;
  321. break;
  322. case HAL_PCD_RESUME_CB_ID :
  323. hpcd->ResumeCallback = pCallback;
  324. break;
  325. case HAL_PCD_CONNECT_CB_ID :
  326. hpcd->ConnectCallback = pCallback;
  327. break;
  328. case HAL_PCD_DISCONNECT_CB_ID :
  329. hpcd->DisconnectCallback = pCallback;
  330. break;
  331. case HAL_PCD_MSPINIT_CB_ID :
  332. hpcd->MspInitCallback = pCallback;
  333. break;
  334. case HAL_PCD_MSPDEINIT_CB_ID :
  335. hpcd->MspDeInitCallback = pCallback;
  336. break;
  337. default :
  338. /* Update the error code */
  339. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  340. /* Return error status */
  341. status = HAL_ERROR;
  342. break;
  343. }
  344. }
  345. else if (hpcd->State == HAL_PCD_STATE_RESET)
  346. {
  347. switch (CallbackID)
  348. {
  349. case HAL_PCD_MSPINIT_CB_ID :
  350. hpcd->MspInitCallback = pCallback;
  351. break;
  352. case HAL_PCD_MSPDEINIT_CB_ID :
  353. hpcd->MspDeInitCallback = pCallback;
  354. break;
  355. default :
  356. /* Update the error code */
  357. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  358. /* Return error status */
  359. status = HAL_ERROR;
  360. break;
  361. }
  362. }
  363. else
  364. {
  365. /* Update the error code */
  366. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  367. /* Return error status */
  368. status = HAL_ERROR;
  369. }
  370. /* Release Lock */
  371. __HAL_UNLOCK(hpcd);
  372. return status;
  373. }
  374. /**
  375. * @brief Unregister an USB PCD Callback
  376. * USB PCD callback is redirected to the weak predefined callback
  377. * @param hpcd USB PCD handle
  378. * @param CallbackID ID of the callback to be unregistered
  379. * This parameter can be one of the following values:
  380. * @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
  381. * @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
  382. * @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
  383. * @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
  384. * @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
  385. * @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
  386. * @arg @ref HAL_PCD_DISCONNECT_CB_ID USB PCD Disconnect callback ID
  387. * @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
  388. * @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
  389. * @retval HAL status
  390. */
  391. HAL_StatusTypeDef HAL_PCD_UnRegisterCallback(PCD_HandleTypeDef *hpcd, HAL_PCD_CallbackIDTypeDef CallbackID)
  392. {
  393. HAL_StatusTypeDef status = HAL_OK;
  394. /* Process locked */
  395. __HAL_LOCK(hpcd);
  396. /* Setup Legacy weak Callbacks */
  397. if (hpcd->State == HAL_PCD_STATE_READY)
  398. {
  399. switch (CallbackID)
  400. {
  401. case HAL_PCD_SOF_CB_ID :
  402. hpcd->SOFCallback = HAL_PCD_SOFCallback;
  403. break;
  404. case HAL_PCD_SETUPSTAGE_CB_ID :
  405. hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
  406. break;
  407. case HAL_PCD_RESET_CB_ID :
  408. hpcd->ResetCallback = HAL_PCD_ResetCallback;
  409. break;
  410. case HAL_PCD_SUSPEND_CB_ID :
  411. hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
  412. break;
  413. case HAL_PCD_RESUME_CB_ID :
  414. hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
  415. break;
  416. case HAL_PCD_CONNECT_CB_ID :
  417. hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
  418. break;
  419. case HAL_PCD_DISCONNECT_CB_ID :
  420. hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
  421. break;
  422. case HAL_PCD_MSPINIT_CB_ID :
  423. hpcd->MspInitCallback = HAL_PCD_MspInit;
  424. break;
  425. case HAL_PCD_MSPDEINIT_CB_ID :
  426. hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
  427. break;
  428. default :
  429. /* Update the error code */
  430. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  431. /* Return error status */
  432. status = HAL_ERROR;
  433. break;
  434. }
  435. }
  436. else if (hpcd->State == HAL_PCD_STATE_RESET)
  437. {
  438. switch (CallbackID)
  439. {
  440. case HAL_PCD_MSPINIT_CB_ID :
  441. hpcd->MspInitCallback = HAL_PCD_MspInit;
  442. break;
  443. case HAL_PCD_MSPDEINIT_CB_ID :
  444. hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
  445. break;
  446. default :
  447. /* Update the error code */
  448. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  449. /* Return error status */
  450. status = HAL_ERROR;
  451. break;
  452. }
  453. }
  454. else
  455. {
  456. /* Update the error code */
  457. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  458. /* Return error status */
  459. status = HAL_ERROR;
  460. }
  461. /* Release Lock */
  462. __HAL_UNLOCK(hpcd);
  463. return status;
  464. }
  465. /**
  466. * @brief Register USB PCD Data OUT Stage Callback
  467. * To be used instead of the weak HAL_PCD_DataOutStageCallback() predefined callback
  468. * @param hpcd PCD handle
  469. * @param pCallback pointer to the USB PCD Data OUT Stage Callback function
  470. * @retval HAL status
  471. */
  472. HAL_StatusTypeDef HAL_PCD_RegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd,
  473. pPCD_DataOutStageCallbackTypeDef pCallback)
  474. {
  475. HAL_StatusTypeDef status = HAL_OK;
  476. if (pCallback == NULL)
  477. {
  478. /* Update the error code */
  479. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  480. return HAL_ERROR;
  481. }
  482. /* Process locked */
  483. __HAL_LOCK(hpcd);
  484. if (hpcd->State == HAL_PCD_STATE_READY)
  485. {
  486. hpcd->DataOutStageCallback = pCallback;
  487. }
  488. else
  489. {
  490. /* Update the error code */
  491. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  492. /* Return error status */
  493. status = HAL_ERROR;
  494. }
  495. /* Release Lock */
  496. __HAL_UNLOCK(hpcd);
  497. return status;
  498. }
  499. /**
  500. * @brief Unregister the USB PCD Data OUT Stage Callback
  501. * USB PCD Data OUT Stage Callback is redirected to the weak HAL_PCD_DataOutStageCallback() predefined callback
  502. * @param hpcd PCD handle
  503. * @retval HAL status
  504. */
  505. HAL_StatusTypeDef HAL_PCD_UnRegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd)
  506. {
  507. HAL_StatusTypeDef status = HAL_OK;
  508. /* Process locked */
  509. __HAL_LOCK(hpcd);
  510. if (hpcd->State == HAL_PCD_STATE_READY)
  511. {
  512. hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback; /* Legacy weak DataOutStageCallback */
  513. }
  514. else
  515. {
  516. /* Update the error code */
  517. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  518. /* Return error status */
  519. status = HAL_ERROR;
  520. }
  521. /* Release Lock */
  522. __HAL_UNLOCK(hpcd);
  523. return status;
  524. }
  525. /**
  526. * @brief Register USB PCD Data IN Stage Callback
  527. * To be used instead of the weak HAL_PCD_DataInStageCallback() predefined callback
  528. * @param hpcd PCD handle
  529. * @param pCallback pointer to the USB PCD Data IN Stage Callback function
  530. * @retval HAL status
  531. */
  532. HAL_StatusTypeDef HAL_PCD_RegisterDataInStageCallback(PCD_HandleTypeDef *hpcd,
  533. pPCD_DataInStageCallbackTypeDef pCallback)
  534. {
  535. HAL_StatusTypeDef status = HAL_OK;
  536. if (pCallback == NULL)
  537. {
  538. /* Update the error code */
  539. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  540. return HAL_ERROR;
  541. }
  542. /* Process locked */
  543. __HAL_LOCK(hpcd);
  544. if (hpcd->State == HAL_PCD_STATE_READY)
  545. {
  546. hpcd->DataInStageCallback = pCallback;
  547. }
  548. else
  549. {
  550. /* Update the error code */
  551. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  552. /* Return error status */
  553. status = HAL_ERROR;
  554. }
  555. /* Release Lock */
  556. __HAL_UNLOCK(hpcd);
  557. return status;
  558. }
  559. /**
  560. * @brief Unregister the USB PCD Data IN Stage Callback
  561. * USB PCD Data OUT Stage Callback is redirected to the weak HAL_PCD_DataInStageCallback() predefined callback
  562. * @param hpcd PCD handle
  563. * @retval HAL status
  564. */
  565. HAL_StatusTypeDef HAL_PCD_UnRegisterDataInStageCallback(PCD_HandleTypeDef *hpcd)
  566. {
  567. HAL_StatusTypeDef status = HAL_OK;
  568. /* Process locked */
  569. __HAL_LOCK(hpcd);
  570. if (hpcd->State == HAL_PCD_STATE_READY)
  571. {
  572. hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback; /* Legacy weak DataInStageCallback */
  573. }
  574. else
  575. {
  576. /* Update the error code */
  577. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  578. /* Return error status */
  579. status = HAL_ERROR;
  580. }
  581. /* Release Lock */
  582. __HAL_UNLOCK(hpcd);
  583. return status;
  584. }
  585. /**
  586. * @brief Register USB PCD Iso OUT incomplete Callback
  587. * To be used instead of the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
  588. * @param hpcd PCD handle
  589. * @param pCallback pointer to the USB PCD Iso OUT incomplete Callback function
  590. * @retval HAL status
  591. */
  592. HAL_StatusTypeDef HAL_PCD_RegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd,
  593. pPCD_IsoOutIncpltCallbackTypeDef pCallback)
  594. {
  595. HAL_StatusTypeDef status = HAL_OK;
  596. if (pCallback == NULL)
  597. {
  598. /* Update the error code */
  599. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  600. return HAL_ERROR;
  601. }
  602. /* Process locked */
  603. __HAL_LOCK(hpcd);
  604. if (hpcd->State == HAL_PCD_STATE_READY)
  605. {
  606. hpcd->ISOOUTIncompleteCallback = pCallback;
  607. }
  608. else
  609. {
  610. /* Update the error code */
  611. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  612. /* Return error status */
  613. status = HAL_ERROR;
  614. }
  615. /* Release Lock */
  616. __HAL_UNLOCK(hpcd);
  617. return status;
  618. }
  619. /**
  620. * @brief Unregister the USB PCD Iso OUT incomplete Callback
  621. * USB PCD Iso OUT incomplete Callback is redirected
  622. * to the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
  623. * @param hpcd PCD handle
  624. * @retval HAL status
  625. */
  626. HAL_StatusTypeDef HAL_PCD_UnRegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd)
  627. {
  628. HAL_StatusTypeDef status = HAL_OK;
  629. /* Process locked */
  630. __HAL_LOCK(hpcd);
  631. if (hpcd->State == HAL_PCD_STATE_READY)
  632. {
  633. hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback; /* Legacy weak ISOOUTIncompleteCallback */
  634. }
  635. else
  636. {
  637. /* Update the error code */
  638. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  639. /* Return error status */
  640. status = HAL_ERROR;
  641. }
  642. /* Release Lock */
  643. __HAL_UNLOCK(hpcd);
  644. return status;
  645. }
  646. /**
  647. * @brief Register USB PCD Iso IN incomplete Callback
  648. * To be used instead of the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
  649. * @param hpcd PCD handle
  650. * @param pCallback pointer to the USB PCD Iso IN incomplete Callback function
  651. * @retval HAL status
  652. */
  653. HAL_StatusTypeDef HAL_PCD_RegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd,
  654. pPCD_IsoInIncpltCallbackTypeDef pCallback)
  655. {
  656. HAL_StatusTypeDef status = HAL_OK;
  657. if (pCallback == NULL)
  658. {
  659. /* Update the error code */
  660. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  661. return HAL_ERROR;
  662. }
  663. /* Process locked */
  664. __HAL_LOCK(hpcd);
  665. if (hpcd->State == HAL_PCD_STATE_READY)
  666. {
  667. hpcd->ISOINIncompleteCallback = pCallback;
  668. }
  669. else
  670. {
  671. /* Update the error code */
  672. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  673. /* Return error status */
  674. status = HAL_ERROR;
  675. }
  676. /* Release Lock */
  677. __HAL_UNLOCK(hpcd);
  678. return status;
  679. }
  680. /**
  681. * @brief Unregister the USB PCD Iso IN incomplete Callback
  682. * USB PCD Iso IN incomplete Callback is redirected
  683. * to the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
  684. * @param hpcd PCD handle
  685. * @retval HAL status
  686. */
  687. HAL_StatusTypeDef HAL_PCD_UnRegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd)
  688. {
  689. HAL_StatusTypeDef status = HAL_OK;
  690. /* Process locked */
  691. __HAL_LOCK(hpcd);
  692. if (hpcd->State == HAL_PCD_STATE_READY)
  693. {
  694. hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback; /* Legacy weak ISOINIncompleteCallback */
  695. }
  696. else
  697. {
  698. /* Update the error code */
  699. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  700. /* Return error status */
  701. status = HAL_ERROR;
  702. }
  703. /* Release Lock */
  704. __HAL_UNLOCK(hpcd);
  705. return status;
  706. }
  707. /**
  708. * @brief Register USB PCD BCD Callback
  709. * To be used instead of the weak HAL_PCDEx_BCD_Callback() predefined callback
  710. * @param hpcd PCD handle
  711. * @param pCallback pointer to the USB PCD BCD Callback function
  712. * @retval HAL status
  713. */
  714. HAL_StatusTypeDef HAL_PCD_RegisterBcdCallback(PCD_HandleTypeDef *hpcd, pPCD_BcdCallbackTypeDef pCallback)
  715. {
  716. HAL_StatusTypeDef status = HAL_OK;
  717. if (pCallback == NULL)
  718. {
  719. /* Update the error code */
  720. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  721. return HAL_ERROR;
  722. }
  723. /* Process locked */
  724. __HAL_LOCK(hpcd);
  725. if (hpcd->State == HAL_PCD_STATE_READY)
  726. {
  727. hpcd->BCDCallback = pCallback;
  728. }
  729. else
  730. {
  731. /* Update the error code */
  732. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  733. /* Return error status */
  734. status = HAL_ERROR;
  735. }
  736. /* Release Lock */
  737. __HAL_UNLOCK(hpcd);
  738. return status;
  739. }
  740. /**
  741. * @brief Unregister the USB PCD BCD Callback
  742. * USB BCD Callback is redirected to the weak HAL_PCDEx_BCD_Callback() predefined callback
  743. * @param hpcd PCD handle
  744. * @retval HAL status
  745. */
  746. HAL_StatusTypeDef HAL_PCD_UnRegisterBcdCallback(PCD_HandleTypeDef *hpcd)
  747. {
  748. HAL_StatusTypeDef status = HAL_OK;
  749. /* Process locked */
  750. __HAL_LOCK(hpcd);
  751. if (hpcd->State == HAL_PCD_STATE_READY)
  752. {
  753. hpcd->BCDCallback = HAL_PCDEx_BCD_Callback; /* Legacy weak HAL_PCDEx_BCD_Callback */
  754. }
  755. else
  756. {
  757. /* Update the error code */
  758. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  759. /* Return error status */
  760. status = HAL_ERROR;
  761. }
  762. /* Release Lock */
  763. __HAL_UNLOCK(hpcd);
  764. return status;
  765. }
  766. /**
  767. * @brief Register USB PCD LPM Callback
  768. * To be used instead of the weak HAL_PCDEx_LPM_Callback() predefined callback
  769. * @param hpcd PCD handle
  770. * @param pCallback pointer to the USB PCD LPM Callback function
  771. * @retval HAL status
  772. */
  773. HAL_StatusTypeDef HAL_PCD_RegisterLpmCallback(PCD_HandleTypeDef *hpcd, pPCD_LpmCallbackTypeDef pCallback)
  774. {
  775. HAL_StatusTypeDef status = HAL_OK;
  776. if (pCallback == NULL)
  777. {
  778. /* Update the error code */
  779. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  780. return HAL_ERROR;
  781. }
  782. /* Process locked */
  783. __HAL_LOCK(hpcd);
  784. if (hpcd->State == HAL_PCD_STATE_READY)
  785. {
  786. hpcd->LPMCallback = pCallback;
  787. }
  788. else
  789. {
  790. /* Update the error code */
  791. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  792. /* Return error status */
  793. status = HAL_ERROR;
  794. }
  795. /* Release Lock */
  796. __HAL_UNLOCK(hpcd);
  797. return status;
  798. }
  799. /**
  800. * @brief Unregister the USB PCD LPM Callback
  801. * USB LPM Callback is redirected to the weak HAL_PCDEx_LPM_Callback() predefined callback
  802. * @param hpcd PCD handle
  803. * @retval HAL status
  804. */
  805. HAL_StatusTypeDef HAL_PCD_UnRegisterLpmCallback(PCD_HandleTypeDef *hpcd)
  806. {
  807. HAL_StatusTypeDef status = HAL_OK;
  808. /* Process locked */
  809. __HAL_LOCK(hpcd);
  810. if (hpcd->State == HAL_PCD_STATE_READY)
  811. {
  812. hpcd->LPMCallback = HAL_PCDEx_LPM_Callback; /* Legacy weak HAL_PCDEx_LPM_Callback */
  813. }
  814. else
  815. {
  816. /* Update the error code */
  817. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  818. /* Return error status */
  819. status = HAL_ERROR;
  820. }
  821. /* Release Lock */
  822. __HAL_UNLOCK(hpcd);
  823. return status;
  824. }
  825. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  826. /**
  827. * @}
  828. */
  829. /** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
  830. * @brief Data transfers functions
  831. *
  832. @verbatim
  833. ===============================================================================
  834. ##### IO operation functions #####
  835. ===============================================================================
  836. [..]
  837. This subsection provides a set of functions allowing to manage the PCD data
  838. transfers.
  839. @endverbatim
  840. * @{
  841. */
  842. /**
  843. * @brief Start the USB device
  844. * @param hpcd PCD handle
  845. * @retval HAL status
  846. */
  847. HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
  848. {
  849. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  850. __HAL_LOCK(hpcd);
  851. if (((USBx->GUSBCFG & USB_OTG_GUSBCFG_PHYSEL) != 0U) &&
  852. (hpcd->Init.battery_charging_enable == 1U))
  853. {
  854. /* Enable USB Transceiver */
  855. USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
  856. }
  857. __HAL_PCD_ENABLE(hpcd);
  858. (void)USB_DevConnect(hpcd->Instance);
  859. __HAL_UNLOCK(hpcd);
  860. return HAL_OK;
  861. }
  862. /**
  863. * @brief Stop the USB device.
  864. * @param hpcd PCD handle
  865. * @retval HAL status
  866. */
  867. HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
  868. {
  869. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  870. __HAL_LOCK(hpcd);
  871. __HAL_PCD_DISABLE(hpcd);
  872. (void)USB_DevDisconnect(hpcd->Instance);
  873. (void)USB_FlushTxFifo(hpcd->Instance, 0x10U);
  874. if (((USBx->GUSBCFG & USB_OTG_GUSBCFG_PHYSEL) != 0U) &&
  875. (hpcd->Init.battery_charging_enable == 1U))
  876. {
  877. /* Disable USB Transceiver */
  878. USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
  879. }
  880. __HAL_UNLOCK(hpcd);
  881. return HAL_OK;
  882. }
  883. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  884. /**
  885. * @brief Handles PCD interrupt request.
  886. * @param hpcd PCD handle
  887. * @retval HAL status
  888. */
  889. void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
  890. {
  891. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  892. uint32_t USBx_BASE = (uint32_t)USBx;
  893. USB_OTG_EPTypeDef *ep;
  894. uint32_t i;
  895. uint32_t ep_intr;
  896. uint32_t epint;
  897. uint32_t epnum;
  898. uint32_t fifoemptymsk;
  899. uint32_t RegVal;
  900. /* ensure that we are in device mode */
  901. if (USB_GetMode(hpcd->Instance) == USB_OTG_MODE_DEVICE)
  902. {
  903. /* avoid spurious interrupt */
  904. if (__HAL_PCD_IS_INVALID_INTERRUPT(hpcd))
  905. {
  906. return;
  907. }
  908. /* store current frame number */
  909. hpcd->FrameNumber = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF_Msk) >> USB_OTG_DSTS_FNSOF_Pos;
  910. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_MMIS))
  911. {
  912. /* incorrect mode, acknowledge the interrupt */
  913. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_MMIS);
  914. }
  915. /* Handle RxQLevel Interrupt */
  916. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL))
  917. {
  918. USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  919. RegVal = USBx->GRXSTSP;
  920. ep = &hpcd->OUT_ep[RegVal & USB_OTG_GRXSTSP_EPNUM];
  921. if (((RegVal & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT)
  922. {
  923. if ((RegVal & USB_OTG_GRXSTSP_BCNT) != 0U)
  924. {
  925. (void)USB_ReadPacket(USBx, ep->xfer_buff,
  926. (uint16_t)((RegVal & USB_OTG_GRXSTSP_BCNT) >> 4));
  927. ep->xfer_buff += (RegVal & USB_OTG_GRXSTSP_BCNT) >> 4;
  928. ep->xfer_count += (RegVal & USB_OTG_GRXSTSP_BCNT) >> 4;
  929. }
  930. }
  931. else if (((RegVal & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT)
  932. {
  933. (void)USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8U);
  934. ep->xfer_count += (RegVal & USB_OTG_GRXSTSP_BCNT) >> 4;
  935. }
  936. else
  937. {
  938. /* ... */
  939. }
  940. USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  941. }
  942. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OEPINT))
  943. {
  944. epnum = 0U;
  945. /* Read in the device interrupt bits */
  946. ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
  947. while (ep_intr != 0U)
  948. {
  949. if ((ep_intr & 0x1U) != 0U)
  950. {
  951. epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, (uint8_t)epnum);
  952. if ((epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC)
  953. {
  954. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
  955. (void)PCD_EP_OutXfrComplete_int(hpcd, epnum);
  956. }
  957. if ((epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP)
  958. {
  959. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
  960. /* Class B setup phase done for previous decoded setup */
  961. (void)PCD_EP_OutSetupPacket_int(hpcd, epnum);
  962. }
  963. if ((epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
  964. {
  965. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
  966. }
  967. /* Clear OUT Endpoint disable interrupt */
  968. if ((epint & USB_OTG_DOEPINT_EPDISD) == USB_OTG_DOEPINT_EPDISD)
  969. {
  970. if ((USBx->GINTSTS & USB_OTG_GINTSTS_BOUTNAKEFF) == USB_OTG_GINTSTS_BOUTNAKEFF)
  971. {
  972. USBx_DEVICE->DCTL |= USB_OTG_DCTL_CGONAK;
  973. }
  974. ep = &hpcd->OUT_ep[epnum];
  975. if (ep->is_iso_incomplete == 1U)
  976. {
  977. ep->is_iso_incomplete = 0U;
  978. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  979. hpcd->ISOOUTIncompleteCallback(hpcd, (uint8_t)epnum);
  980. #else
  981. HAL_PCD_ISOOUTIncompleteCallback(hpcd, (uint8_t)epnum);
  982. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  983. }
  984. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_EPDISD);
  985. }
  986. /* Clear Status Phase Received interrupt */
  987. if ((epint & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
  988. {
  989. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
  990. }
  991. /* Clear OUT NAK interrupt */
  992. if ((epint & USB_OTG_DOEPINT_NAK) == USB_OTG_DOEPINT_NAK)
  993. {
  994. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_NAK);
  995. }
  996. }
  997. epnum++;
  998. ep_intr >>= 1U;
  999. }
  1000. }
  1001. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT))
  1002. {
  1003. /* Read in the device interrupt bits */
  1004. ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
  1005. epnum = 0U;
  1006. while (ep_intr != 0U)
  1007. {
  1008. if ((ep_intr & 0x1U) != 0U) /* In ITR */
  1009. {
  1010. epint = USB_ReadDevInEPInterrupt(hpcd->Instance, (uint8_t)epnum);
  1011. if ((epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
  1012. {
  1013. fifoemptymsk = (uint32_t)(0x1UL << (epnum & EP_ADDR_MSK));
  1014. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  1015. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
  1016. if (hpcd->Init.dma_enable == 1U)
  1017. {
  1018. hpcd->IN_ep[epnum].xfer_buff += hpcd->IN_ep[epnum].maxpacket;
  1019. /* this is ZLP, so prepare EP0 for next setup */
  1020. if ((epnum == 0U) && (hpcd->IN_ep[epnum].xfer_len == 0U))
  1021. {
  1022. /* prepare to rx more setup packets */
  1023. (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
  1024. }
  1025. }
  1026. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1027. hpcd->DataInStageCallback(hpcd, (uint8_t)epnum);
  1028. #else
  1029. HAL_PCD_DataInStageCallback(hpcd, (uint8_t)epnum);
  1030. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1031. }
  1032. if ((epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
  1033. {
  1034. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
  1035. }
  1036. if ((epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
  1037. {
  1038. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
  1039. }
  1040. if ((epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
  1041. {
  1042. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
  1043. }
  1044. if ((epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
  1045. {
  1046. (void)USB_FlushTxFifo(USBx, epnum);
  1047. ep = &hpcd->IN_ep[epnum];
  1048. if (ep->is_iso_incomplete == 1U)
  1049. {
  1050. ep->is_iso_incomplete = 0U;
  1051. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1052. hpcd->ISOINIncompleteCallback(hpcd, (uint8_t)epnum);
  1053. #else
  1054. HAL_PCD_ISOINIncompleteCallback(hpcd, (uint8_t)epnum);
  1055. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1056. }
  1057. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
  1058. }
  1059. if ((epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE)
  1060. {
  1061. (void)PCD_WriteEmptyTxFifo(hpcd, epnum);
  1062. }
  1063. }
  1064. epnum++;
  1065. ep_intr >>= 1U;
  1066. }
  1067. }
  1068. /* Handle Resume Interrupt */
  1069. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT))
  1070. {
  1071. /* Clear the Remote Wake-up Signaling */
  1072. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  1073. if (hpcd->LPM_State == LPM_L1)
  1074. {
  1075. hpcd->LPM_State = LPM_L0;
  1076. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1077. hpcd->LPMCallback(hpcd, PCD_LPM_L0_ACTIVE);
  1078. #else
  1079. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
  1080. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1081. }
  1082. else
  1083. {
  1084. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1085. hpcd->ResumeCallback(hpcd);
  1086. #else
  1087. HAL_PCD_ResumeCallback(hpcd);
  1088. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1089. }
  1090. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
  1091. }
  1092. /* Handle Suspend Interrupt */
  1093. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP))
  1094. {
  1095. if ((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
  1096. {
  1097. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1098. hpcd->SuspendCallback(hpcd);
  1099. #else
  1100. HAL_PCD_SuspendCallback(hpcd);
  1101. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1102. }
  1103. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
  1104. }
  1105. #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) \
  1106. || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) \
  1107. || defined(STM32F423xx)
  1108. /* Handle LPM Interrupt */
  1109. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT))
  1110. {
  1111. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT);
  1112. if (hpcd->LPM_State == LPM_L0)
  1113. {
  1114. hpcd->LPM_State = LPM_L1;
  1115. hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >> 2U;
  1116. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1117. hpcd->LPMCallback(hpcd, PCD_LPM_L1_ACTIVE);
  1118. #else
  1119. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
  1120. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1121. }
  1122. else
  1123. {
  1124. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1125. hpcd->SuspendCallback(hpcd);
  1126. #else
  1127. HAL_PCD_SuspendCallback(hpcd);
  1128. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1129. }
  1130. }
  1131. #endif /* defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) ||
  1132. defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) ||
  1133. defined(STM32F423xx) */
  1134. /* Handle Reset Interrupt */
  1135. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST))
  1136. {
  1137. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  1138. (void)USB_FlushTxFifo(hpcd->Instance, 0x10U);
  1139. for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
  1140. {
  1141. USBx_INEP(i)->DIEPINT = 0xFB7FU;
  1142. USBx_INEP(i)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
  1143. USBx_OUTEP(i)->DOEPINT = 0xFB7FU;
  1144. USBx_OUTEP(i)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
  1145. USBx_OUTEP(i)->DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
  1146. }
  1147. USBx_DEVICE->DAINTMSK |= 0x10001U;
  1148. if (hpcd->Init.use_dedicated_ep1 != 0U)
  1149. {
  1150. USBx_DEVICE->DOUTEP1MSK |= USB_OTG_DOEPMSK_STUPM |
  1151. USB_OTG_DOEPMSK_XFRCM |
  1152. USB_OTG_DOEPMSK_EPDM;
  1153. USBx_DEVICE->DINEP1MSK |= USB_OTG_DIEPMSK_TOM |
  1154. USB_OTG_DIEPMSK_XFRCM |
  1155. USB_OTG_DIEPMSK_EPDM;
  1156. }
  1157. else
  1158. {
  1159. USBx_DEVICE->DOEPMSK |= USB_OTG_DOEPMSK_STUPM |
  1160. USB_OTG_DOEPMSK_XFRCM |
  1161. USB_OTG_DOEPMSK_EPDM |
  1162. USB_OTG_DOEPMSK_OTEPSPRM |
  1163. USB_OTG_DOEPMSK_NAKM;
  1164. USBx_DEVICE->DIEPMSK |= USB_OTG_DIEPMSK_TOM |
  1165. USB_OTG_DIEPMSK_XFRCM |
  1166. USB_OTG_DIEPMSK_EPDM;
  1167. }
  1168. /* Set Default Address to 0 */
  1169. USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
  1170. /* setup EP0 to receive SETUP packets */
  1171. (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t)hpcd->Init.dma_enable,
  1172. (uint8_t *)hpcd->Setup);
  1173. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
  1174. }
  1175. /* Handle Enumeration done Interrupt */
  1176. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE))
  1177. {
  1178. (void)USB_ActivateSetup(hpcd->Instance);
  1179. hpcd->Init.speed = USB_GetDevSpeed(hpcd->Instance);
  1180. /* Set USB Turnaround time */
  1181. (void)USB_SetTurnaroundTime(hpcd->Instance,
  1182. HAL_RCC_GetHCLKFreq(),
  1183. (uint8_t)hpcd->Init.speed);
  1184. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1185. hpcd->ResetCallback(hpcd);
  1186. #else
  1187. HAL_PCD_ResetCallback(hpcd);
  1188. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1189. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE);
  1190. }
  1191. /* Handle SOF Interrupt */
  1192. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SOF))
  1193. {
  1194. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1195. hpcd->SOFCallback(hpcd);
  1196. #else
  1197. HAL_PCD_SOFCallback(hpcd);
  1198. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1199. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SOF);
  1200. }
  1201. /* Handle Global OUT NAK effective Interrupt */
  1202. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_BOUTNAKEFF))
  1203. {
  1204. USBx->GINTMSK &= ~USB_OTG_GINTMSK_GONAKEFFM;
  1205. for (epnum = 1U; epnum < hpcd->Init.dev_endpoints; epnum++)
  1206. {
  1207. if (hpcd->OUT_ep[epnum].is_iso_incomplete == 1U)
  1208. {
  1209. /* Abort current transaction and disable the EP */
  1210. (void)HAL_PCD_EP_Abort(hpcd, (uint8_t)epnum);
  1211. }
  1212. }
  1213. }
  1214. /* Handle Incomplete ISO IN Interrupt */
  1215. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR))
  1216. {
  1217. for (epnum = 1U; epnum < hpcd->Init.dev_endpoints; epnum++)
  1218. {
  1219. RegVal = USBx_INEP(epnum)->DIEPCTL;
  1220. if ((hpcd->IN_ep[epnum].type == EP_TYPE_ISOC) &&
  1221. ((RegVal & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA))
  1222. {
  1223. hpcd->IN_ep[epnum].is_iso_incomplete = 1U;
  1224. /* Abort current transaction and disable the EP */
  1225. (void)HAL_PCD_EP_Abort(hpcd, (uint8_t)(epnum | 0x80U));
  1226. }
  1227. }
  1228. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR);
  1229. }
  1230. /* Handle Incomplete ISO OUT Interrupt */
  1231. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
  1232. {
  1233. for (epnum = 1U; epnum < hpcd->Init.dev_endpoints; epnum++)
  1234. {
  1235. RegVal = USBx_OUTEP(epnum)->DOEPCTL;
  1236. if ((hpcd->OUT_ep[epnum].type == EP_TYPE_ISOC) &&
  1237. ((RegVal & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA) &&
  1238. ((RegVal & (0x1U << 16)) == (hpcd->FrameNumber & 0x1U)))
  1239. {
  1240. hpcd->OUT_ep[epnum].is_iso_incomplete = 1U;
  1241. USBx->GINTMSK |= USB_OTG_GINTMSK_GONAKEFFM;
  1242. if ((USBx->GINTSTS & USB_OTG_GINTSTS_BOUTNAKEFF) == 0U)
  1243. {
  1244. USBx_DEVICE->DCTL |= USB_OTG_DCTL_SGONAK;
  1245. break;
  1246. }
  1247. }
  1248. }
  1249. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
  1250. }
  1251. /* Handle Connection event Interrupt */
  1252. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT))
  1253. {
  1254. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1255. hpcd->ConnectCallback(hpcd);
  1256. #else
  1257. HAL_PCD_ConnectCallback(hpcd);
  1258. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1259. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT);
  1260. }
  1261. /* Handle Disconnection event Interrupt */
  1262. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OTGINT))
  1263. {
  1264. RegVal = hpcd->Instance->GOTGINT;
  1265. if ((RegVal & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET)
  1266. {
  1267. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1268. hpcd->DisconnectCallback(hpcd);
  1269. #else
  1270. HAL_PCD_DisconnectCallback(hpcd);
  1271. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1272. }
  1273. hpcd->Instance->GOTGINT |= RegVal;
  1274. }
  1275. }
  1276. }
  1277. /**
  1278. * @brief Handles PCD Wakeup interrupt request.
  1279. * @param hpcd PCD handle
  1280. * @retval HAL status
  1281. */
  1282. void HAL_PCD_WKUP_IRQHandler(PCD_HandleTypeDef *hpcd)
  1283. {
  1284. #if defined (USB_OTG_FS)
  1285. USB_OTG_GlobalTypeDef *USBx;
  1286. USBx = hpcd->Instance;
  1287. if (USBx == USB_OTG_FS)
  1288. {
  1289. /* Clear EXTI pending Bit */
  1290. __HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG();
  1291. }
  1292. else
  1293. #endif /* defined (USB_OTG_FS) */
  1294. {
  1295. /* Clear EXTI pending Bit */
  1296. __HAL_USB_OTG_HS_WAKEUP_EXTI_CLEAR_FLAG();
  1297. }
  1298. }
  1299. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  1300. /**
  1301. * @brief Data OUT stage callback.
  1302. * @param hpcd PCD handle
  1303. * @param epnum endpoint number
  1304. * @retval None
  1305. */
  1306. __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1307. {
  1308. /* Prevent unused argument(s) compilation warning */
  1309. UNUSED(hpcd);
  1310. UNUSED(epnum);
  1311. /* NOTE : This function should not be modified, when the callback is needed,
  1312. the HAL_PCD_DataOutStageCallback could be implemented in the user file
  1313. */
  1314. }
  1315. /**
  1316. * @brief Data IN stage callback
  1317. * @param hpcd PCD handle
  1318. * @param epnum endpoint number
  1319. * @retval None
  1320. */
  1321. __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1322. {
  1323. /* Prevent unused argument(s) compilation warning */
  1324. UNUSED(hpcd);
  1325. UNUSED(epnum);
  1326. /* NOTE : This function should not be modified, when the callback is needed,
  1327. the HAL_PCD_DataInStageCallback could be implemented in the user file
  1328. */
  1329. }
  1330. /**
  1331. * @brief Setup stage callback
  1332. * @param hpcd PCD handle
  1333. * @retval None
  1334. */
  1335. __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  1336. {
  1337. /* Prevent unused argument(s) compilation warning */
  1338. UNUSED(hpcd);
  1339. /* NOTE : This function should not be modified, when the callback is needed,
  1340. the HAL_PCD_SetupStageCallback could be implemented in the user file
  1341. */
  1342. }
  1343. /**
  1344. * @brief USB Start Of Frame callback.
  1345. * @param hpcd PCD handle
  1346. * @retval None
  1347. */
  1348. __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  1349. {
  1350. /* Prevent unused argument(s) compilation warning */
  1351. UNUSED(hpcd);
  1352. /* NOTE : This function should not be modified, when the callback is needed,
  1353. the HAL_PCD_SOFCallback could be implemented in the user file
  1354. */
  1355. }
  1356. /**
  1357. * @brief USB Reset callback.
  1358. * @param hpcd PCD handle
  1359. * @retval None
  1360. */
  1361. __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  1362. {
  1363. /* Prevent unused argument(s) compilation warning */
  1364. UNUSED(hpcd);
  1365. /* NOTE : This function should not be modified, when the callback is needed,
  1366. the HAL_PCD_ResetCallback could be implemented in the user file
  1367. */
  1368. }
  1369. /**
  1370. * @brief Suspend event callback.
  1371. * @param hpcd PCD handle
  1372. * @retval None
  1373. */
  1374. __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  1375. {
  1376. /* Prevent unused argument(s) compilation warning */
  1377. UNUSED(hpcd);
  1378. /* NOTE : This function should not be modified, when the callback is needed,
  1379. the HAL_PCD_SuspendCallback could be implemented in the user file
  1380. */
  1381. }
  1382. /**
  1383. * @brief Resume event callback.
  1384. * @param hpcd PCD handle
  1385. * @retval None
  1386. */
  1387. __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  1388. {
  1389. /* Prevent unused argument(s) compilation warning */
  1390. UNUSED(hpcd);
  1391. /* NOTE : This function should not be modified, when the callback is needed,
  1392. the HAL_PCD_ResumeCallback could be implemented in the user file
  1393. */
  1394. }
  1395. /**
  1396. * @brief Incomplete ISO OUT callback.
  1397. * @param hpcd PCD handle
  1398. * @param epnum endpoint number
  1399. * @retval None
  1400. */
  1401. __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1402. {
  1403. /* Prevent unused argument(s) compilation warning */
  1404. UNUSED(hpcd);
  1405. UNUSED(epnum);
  1406. /* NOTE : This function should not be modified, when the callback is needed,
  1407. the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
  1408. */
  1409. }
  1410. /**
  1411. * @brief Incomplete ISO IN callback.
  1412. * @param hpcd PCD handle
  1413. * @param epnum endpoint number
  1414. * @retval None
  1415. */
  1416. __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1417. {
  1418. /* Prevent unused argument(s) compilation warning */
  1419. UNUSED(hpcd);
  1420. UNUSED(epnum);
  1421. /* NOTE : This function should not be modified, when the callback is needed,
  1422. the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
  1423. */
  1424. }
  1425. /**
  1426. * @brief Connection event callback.
  1427. * @param hpcd PCD handle
  1428. * @retval None
  1429. */
  1430. __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  1431. {
  1432. /* Prevent unused argument(s) compilation warning */
  1433. UNUSED(hpcd);
  1434. /* NOTE : This function should not be modified, when the callback is needed,
  1435. the HAL_PCD_ConnectCallback could be implemented in the user file
  1436. */
  1437. }
  1438. /**
  1439. * @brief Disconnection event callback.
  1440. * @param hpcd PCD handle
  1441. * @retval None
  1442. */
  1443. __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  1444. {
  1445. /* Prevent unused argument(s) compilation warning */
  1446. UNUSED(hpcd);
  1447. /* NOTE : This function should not be modified, when the callback is needed,
  1448. the HAL_PCD_DisconnectCallback could be implemented in the user file
  1449. */
  1450. }
  1451. /**
  1452. * @}
  1453. */
  1454. /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
  1455. * @brief management functions
  1456. *
  1457. @verbatim
  1458. ===============================================================================
  1459. ##### Peripheral Control functions #####
  1460. ===============================================================================
  1461. [..]
  1462. This subsection provides a set of functions allowing to control the PCD data
  1463. transfers.
  1464. @endverbatim
  1465. * @{
  1466. */
  1467. /**
  1468. * @brief Connect the USB device
  1469. * @param hpcd PCD handle
  1470. * @retval HAL status
  1471. */
  1472. HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
  1473. {
  1474. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1475. __HAL_LOCK(hpcd);
  1476. if (((USBx->GUSBCFG & USB_OTG_GUSBCFG_PHYSEL) != 0U) &&
  1477. (hpcd->Init.battery_charging_enable == 1U))
  1478. {
  1479. /* Enable USB Transceiver */
  1480. USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
  1481. }
  1482. (void)USB_DevConnect(hpcd->Instance);
  1483. __HAL_UNLOCK(hpcd);
  1484. return HAL_OK;
  1485. }
  1486. /**
  1487. * @brief Disconnect the USB device.
  1488. * @param hpcd PCD handle
  1489. * @retval HAL status
  1490. */
  1491. HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
  1492. {
  1493. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1494. __HAL_LOCK(hpcd);
  1495. (void)USB_DevDisconnect(hpcd->Instance);
  1496. if (((USBx->GUSBCFG & USB_OTG_GUSBCFG_PHYSEL) != 0U) &&
  1497. (hpcd->Init.battery_charging_enable == 1U))
  1498. {
  1499. /* Disable USB Transceiver */
  1500. USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
  1501. }
  1502. __HAL_UNLOCK(hpcd);
  1503. return HAL_OK;
  1504. }
  1505. /**
  1506. * @brief Set the USB Device address.
  1507. * @param hpcd PCD handle
  1508. * @param address new device address
  1509. * @retval HAL status
  1510. */
  1511. HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
  1512. {
  1513. __HAL_LOCK(hpcd);
  1514. hpcd->USB_Address = address;
  1515. (void)USB_SetDevAddress(hpcd->Instance, address);
  1516. __HAL_UNLOCK(hpcd);
  1517. return HAL_OK;
  1518. }
  1519. /**
  1520. * @brief Open and configure an endpoint.
  1521. * @param hpcd PCD handle
  1522. * @param ep_addr endpoint address
  1523. * @param ep_mps endpoint max packet size
  1524. * @param ep_type endpoint type
  1525. * @retval HAL status
  1526. */
  1527. HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
  1528. uint16_t ep_mps, uint8_t ep_type)
  1529. {
  1530. HAL_StatusTypeDef ret = HAL_OK;
  1531. PCD_EPTypeDef *ep;
  1532. if ((ep_addr & 0x80U) == 0x80U)
  1533. {
  1534. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1535. ep->is_in = 1U;
  1536. }
  1537. else
  1538. {
  1539. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1540. ep->is_in = 0U;
  1541. }
  1542. ep->num = ep_addr & EP_ADDR_MSK;
  1543. ep->maxpacket = ep_mps;
  1544. ep->type = ep_type;
  1545. if (ep->is_in != 0U)
  1546. {
  1547. /* Assign a Tx FIFO */
  1548. ep->tx_fifo_num = ep->num;
  1549. }
  1550. /* Set initial data PID. */
  1551. if (ep_type == EP_TYPE_BULK)
  1552. {
  1553. ep->data_pid_start = 0U;
  1554. }
  1555. __HAL_LOCK(hpcd);
  1556. (void)USB_ActivateEndpoint(hpcd->Instance, ep);
  1557. __HAL_UNLOCK(hpcd);
  1558. return ret;
  1559. }
  1560. /**
  1561. * @brief Deactivate an endpoint.
  1562. * @param hpcd PCD handle
  1563. * @param ep_addr endpoint address
  1564. * @retval HAL status
  1565. */
  1566. HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1567. {
  1568. PCD_EPTypeDef *ep;
  1569. if ((ep_addr & 0x80U) == 0x80U)
  1570. {
  1571. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1572. ep->is_in = 1U;
  1573. }
  1574. else
  1575. {
  1576. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1577. ep->is_in = 0U;
  1578. }
  1579. ep->num = ep_addr & EP_ADDR_MSK;
  1580. __HAL_LOCK(hpcd);
  1581. (void)USB_DeactivateEndpoint(hpcd->Instance, ep);
  1582. __HAL_UNLOCK(hpcd);
  1583. return HAL_OK;
  1584. }
  1585. /**
  1586. * @brief Receive an amount of data.
  1587. * @param hpcd PCD handle
  1588. * @param ep_addr endpoint address
  1589. * @param pBuf pointer to the reception buffer
  1590. * @param len amount of data to be received
  1591. * @retval HAL status
  1592. */
  1593. HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  1594. {
  1595. PCD_EPTypeDef *ep;
  1596. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1597. /*setup and start the Xfer */
  1598. ep->xfer_buff = pBuf;
  1599. ep->xfer_len = len;
  1600. ep->xfer_count = 0U;
  1601. ep->is_in = 0U;
  1602. ep->num = ep_addr & EP_ADDR_MSK;
  1603. if (hpcd->Init.dma_enable == 1U)
  1604. {
  1605. ep->dma_addr = (uint32_t)pBuf;
  1606. }
  1607. (void)USB_EPStartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
  1608. return HAL_OK;
  1609. }
  1610. /**
  1611. * @brief Get Received Data Size
  1612. * @param hpcd PCD handle
  1613. * @param ep_addr endpoint address
  1614. * @retval Data Size
  1615. */
  1616. uint32_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef const *hpcd, uint8_t ep_addr)
  1617. {
  1618. return hpcd->OUT_ep[ep_addr & EP_ADDR_MSK].xfer_count;
  1619. }
  1620. /**
  1621. * @brief Send an amount of data
  1622. * @param hpcd PCD handle
  1623. * @param ep_addr endpoint address
  1624. * @param pBuf pointer to the transmission buffer
  1625. * @param len amount of data to be sent
  1626. * @retval HAL status
  1627. */
  1628. HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  1629. {
  1630. PCD_EPTypeDef *ep;
  1631. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1632. /*setup and start the Xfer */
  1633. ep->xfer_buff = pBuf;
  1634. ep->xfer_len = len;
  1635. ep->xfer_count = 0U;
  1636. ep->is_in = 1U;
  1637. ep->num = ep_addr & EP_ADDR_MSK;
  1638. if (hpcd->Init.dma_enable == 1U)
  1639. {
  1640. ep->dma_addr = (uint32_t)pBuf;
  1641. }
  1642. (void)USB_EPStartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
  1643. return HAL_OK;
  1644. }
  1645. /**
  1646. * @brief Set a STALL condition over an endpoint
  1647. * @param hpcd PCD handle
  1648. * @param ep_addr endpoint address
  1649. * @retval HAL status
  1650. */
  1651. HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1652. {
  1653. PCD_EPTypeDef *ep;
  1654. if (((uint32_t)ep_addr & EP_ADDR_MSK) > hpcd->Init.dev_endpoints)
  1655. {
  1656. return HAL_ERROR;
  1657. }
  1658. if ((0x80U & ep_addr) == 0x80U)
  1659. {
  1660. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1661. ep->is_in = 1U;
  1662. }
  1663. else
  1664. {
  1665. ep = &hpcd->OUT_ep[ep_addr];
  1666. ep->is_in = 0U;
  1667. }
  1668. ep->is_stall = 1U;
  1669. ep->num = ep_addr & EP_ADDR_MSK;
  1670. __HAL_LOCK(hpcd);
  1671. (void)USB_EPSetStall(hpcd->Instance, ep);
  1672. if ((ep_addr & EP_ADDR_MSK) == 0U)
  1673. {
  1674. (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t)hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
  1675. }
  1676. __HAL_UNLOCK(hpcd);
  1677. return HAL_OK;
  1678. }
  1679. /**
  1680. * @brief Clear a STALL condition over in an endpoint
  1681. * @param hpcd PCD handle
  1682. * @param ep_addr endpoint address
  1683. * @retval HAL status
  1684. */
  1685. HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1686. {
  1687. PCD_EPTypeDef *ep;
  1688. if (((uint32_t)ep_addr & 0x0FU) > hpcd->Init.dev_endpoints)
  1689. {
  1690. return HAL_ERROR;
  1691. }
  1692. if ((0x80U & ep_addr) == 0x80U)
  1693. {
  1694. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1695. ep->is_in = 1U;
  1696. }
  1697. else
  1698. {
  1699. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1700. ep->is_in = 0U;
  1701. }
  1702. ep->is_stall = 0U;
  1703. ep->num = ep_addr & EP_ADDR_MSK;
  1704. __HAL_LOCK(hpcd);
  1705. (void)USB_EPClearStall(hpcd->Instance, ep);
  1706. __HAL_UNLOCK(hpcd);
  1707. return HAL_OK;
  1708. }
  1709. /**
  1710. * @brief Abort an USB EP transaction.
  1711. * @param hpcd PCD handle
  1712. * @param ep_addr endpoint address
  1713. * @retval HAL status
  1714. */
  1715. HAL_StatusTypeDef HAL_PCD_EP_Abort(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1716. {
  1717. HAL_StatusTypeDef ret;
  1718. PCD_EPTypeDef *ep;
  1719. if ((0x80U & ep_addr) == 0x80U)
  1720. {
  1721. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1722. }
  1723. else
  1724. {
  1725. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1726. }
  1727. /* Stop Xfer */
  1728. ret = USB_EPStopXfer(hpcd->Instance, ep);
  1729. return ret;
  1730. }
  1731. /**
  1732. * @brief Flush an endpoint
  1733. * @param hpcd PCD handle
  1734. * @param ep_addr endpoint address
  1735. * @retval HAL status
  1736. */
  1737. HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1738. {
  1739. __HAL_LOCK(hpcd);
  1740. if ((ep_addr & 0x80U) == 0x80U)
  1741. {
  1742. (void)USB_FlushTxFifo(hpcd->Instance, (uint32_t)ep_addr & EP_ADDR_MSK);
  1743. }
  1744. else
  1745. {
  1746. (void)USB_FlushRxFifo(hpcd->Instance);
  1747. }
  1748. __HAL_UNLOCK(hpcd);
  1749. return HAL_OK;
  1750. }
  1751. /**
  1752. * @brief Activate remote wakeup signalling
  1753. * @param hpcd PCD handle
  1754. * @retval HAL status
  1755. */
  1756. HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1757. {
  1758. return (USB_ActivateRemoteWakeup(hpcd->Instance));
  1759. }
  1760. /**
  1761. * @brief De-activate remote wakeup signalling.
  1762. * @param hpcd PCD handle
  1763. * @retval HAL status
  1764. */
  1765. HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1766. {
  1767. return (USB_DeActivateRemoteWakeup(hpcd->Instance));
  1768. }
  1769. /**
  1770. * @}
  1771. */
  1772. /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
  1773. * @brief Peripheral State functions
  1774. *
  1775. @verbatim
  1776. ===============================================================================
  1777. ##### Peripheral State functions #####
  1778. ===============================================================================
  1779. [..]
  1780. This subsection permits to get in run-time the status of the peripheral
  1781. and the data flow.
  1782. @endverbatim
  1783. * @{
  1784. */
  1785. /**
  1786. * @brief Return the PCD handle state.
  1787. * @param hpcd PCD handle
  1788. * @retval HAL state
  1789. */
  1790. PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef const *hpcd)
  1791. {
  1792. return hpcd->State;
  1793. }
  1794. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  1795. /**
  1796. * @brief Set the USB Device high speed test mode.
  1797. * @param hpcd PCD handle
  1798. * @param testmode USB Device high speed test mode
  1799. * @retval HAL status
  1800. */
  1801. HAL_StatusTypeDef HAL_PCD_SetTestMode(const PCD_HandleTypeDef *hpcd, uint8_t testmode)
  1802. {
  1803. const USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1804. uint32_t USBx_BASE = (uint32_t)USBx;
  1805. switch (testmode)
  1806. {
  1807. case TEST_J:
  1808. case TEST_K:
  1809. case TEST_SE0_NAK:
  1810. case TEST_PACKET:
  1811. case TEST_FORCE_EN:
  1812. USBx_DEVICE->DCTL |= (uint32_t)testmode << 4;
  1813. break;
  1814. default:
  1815. break;
  1816. }
  1817. return HAL_OK;
  1818. }
  1819. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  1820. /**
  1821. * @}
  1822. */
  1823. /**
  1824. * @}
  1825. */
  1826. /* Private functions ---------------------------------------------------------*/
  1827. /** @addtogroup PCD_Private_Functions
  1828. * @{
  1829. */
  1830. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  1831. /**
  1832. * @brief Check FIFO for the next packet to be loaded.
  1833. * @param hpcd PCD handle
  1834. * @param epnum endpoint number
  1835. * @retval HAL status
  1836. */
  1837. static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1838. {
  1839. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1840. uint32_t USBx_BASE = (uint32_t)USBx;
  1841. USB_OTG_EPTypeDef *ep;
  1842. uint32_t len;
  1843. uint32_t len32b;
  1844. uint32_t fifoemptymsk;
  1845. ep = &hpcd->IN_ep[epnum];
  1846. if (ep->xfer_count > ep->xfer_len)
  1847. {
  1848. return HAL_ERROR;
  1849. }
  1850. len = ep->xfer_len - ep->xfer_count;
  1851. if (len > ep->maxpacket)
  1852. {
  1853. len = ep->maxpacket;
  1854. }
  1855. len32b = (len + 3U) / 4U;
  1856. while (((USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) >= len32b) &&
  1857. (ep->xfer_count < ep->xfer_len) && (ep->xfer_len != 0U))
  1858. {
  1859. /* Write the FIFO */
  1860. len = ep->xfer_len - ep->xfer_count;
  1861. if (len > ep->maxpacket)
  1862. {
  1863. len = ep->maxpacket;
  1864. }
  1865. len32b = (len + 3U) / 4U;
  1866. (void)USB_WritePacket(USBx, ep->xfer_buff, (uint8_t)epnum, (uint16_t)len,
  1867. (uint8_t)hpcd->Init.dma_enable);
  1868. ep->xfer_buff += len;
  1869. ep->xfer_count += len;
  1870. }
  1871. if (ep->xfer_len <= ep->xfer_count)
  1872. {
  1873. fifoemptymsk = (uint32_t)(0x1UL << (epnum & EP_ADDR_MSK));
  1874. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  1875. }
  1876. return HAL_OK;
  1877. }
  1878. /**
  1879. * @brief process EP OUT transfer complete interrupt.
  1880. * @param hpcd PCD handle
  1881. * @param epnum endpoint number
  1882. * @retval HAL status
  1883. */
  1884. static HAL_StatusTypeDef PCD_EP_OutXfrComplete_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1885. {
  1886. USB_OTG_EPTypeDef *ep;
  1887. const USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1888. uint32_t USBx_BASE = (uint32_t)USBx;
  1889. uint32_t gSNPSiD = *(__IO const uint32_t *)(&USBx->CID + 0x1U);
  1890. uint32_t DoepintReg = USBx_OUTEP(epnum)->DOEPINT;
  1891. if (hpcd->Init.dma_enable == 1U)
  1892. {
  1893. if ((DoepintReg & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP) /* Class C */
  1894. {
  1895. /* StupPktRcvd = 1 this is a setup packet */
  1896. if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
  1897. ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX))
  1898. {
  1899. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
  1900. }
  1901. }
  1902. else if ((DoepintReg & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR) /* Class E */
  1903. {
  1904. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
  1905. }
  1906. else if ((DoepintReg & (USB_OTG_DOEPINT_STUP | USB_OTG_DOEPINT_OTEPSPR)) == 0U)
  1907. {
  1908. /* StupPktRcvd = 1 this is a setup packet */
  1909. if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
  1910. ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX))
  1911. {
  1912. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
  1913. }
  1914. else
  1915. {
  1916. ep = &hpcd->OUT_ep[epnum];
  1917. /* out data packet received over EP */
  1918. ep->xfer_count = ep->xfer_size - (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
  1919. if (epnum == 0U)
  1920. {
  1921. if (ep->xfer_len == 0U)
  1922. {
  1923. /* this is ZLP, so prepare EP0 for next setup */
  1924. (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
  1925. }
  1926. else
  1927. {
  1928. ep->xfer_buff += ep->xfer_count;
  1929. }
  1930. }
  1931. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1932. hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
  1933. #else
  1934. HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
  1935. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1936. }
  1937. }
  1938. else
  1939. {
  1940. /* ... */
  1941. }
  1942. }
  1943. else
  1944. {
  1945. if (gSNPSiD == USB_OTG_CORE_ID_310A)
  1946. {
  1947. /* StupPktRcvd = 1 this is a setup packet */
  1948. if ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX)
  1949. {
  1950. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
  1951. }
  1952. else
  1953. {
  1954. if ((DoepintReg & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
  1955. {
  1956. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
  1957. }
  1958. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1959. hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
  1960. #else
  1961. HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
  1962. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1963. }
  1964. }
  1965. else
  1966. {
  1967. if ((epnum == 0U) && (hpcd->OUT_ep[epnum].xfer_len == 0U))
  1968. {
  1969. /* this is ZLP, so prepare EP0 for next setup */
  1970. (void)USB_EP0_OutStart(hpcd->Instance, 0U, (uint8_t *)hpcd->Setup);
  1971. }
  1972. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1973. hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
  1974. #else
  1975. HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
  1976. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1977. }
  1978. }
  1979. return HAL_OK;
  1980. }
  1981. /**
  1982. * @brief process EP OUT setup packet received interrupt.
  1983. * @param hpcd PCD handle
  1984. * @param epnum endpoint number
  1985. * @retval HAL status
  1986. */
  1987. static HAL_StatusTypeDef PCD_EP_OutSetupPacket_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1988. {
  1989. const USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1990. uint32_t USBx_BASE = (uint32_t)USBx;
  1991. uint32_t gSNPSiD = *(__IO const uint32_t *)(&USBx->CID + 0x1U);
  1992. uint32_t DoepintReg = USBx_OUTEP(epnum)->DOEPINT;
  1993. if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
  1994. ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX))
  1995. {
  1996. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
  1997. }
  1998. /* Inform the upper layer that a setup packet is available */
  1999. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  2000. hpcd->SetupStageCallback(hpcd);
  2001. #else
  2002. HAL_PCD_SetupStageCallback(hpcd);
  2003. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  2004. if ((gSNPSiD > USB_OTG_CORE_ID_300A) && (hpcd->Init.dma_enable == 1U))
  2005. {
  2006. (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
  2007. }
  2008. return HAL_OK;
  2009. }
  2010. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  2011. /**
  2012. * @}
  2013. */
  2014. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  2015. #endif /* HAL_PCD_MODULE_ENABLED */
  2016. /**
  2017. * @}
  2018. */
  2019. /**
  2020. * @}
  2021. */