stm32l4xx_hal_pcd.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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) 2017 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_CLK_ENABLE(); For USB Device only FS peripheral
  37. (##) Initialize the related GPIO clocks
  38. (##) Configure PCD pin-out
  39. (##) Configure PCD NVIC interrupt
  40. (#)Associate the Upper USB device stack to the HAL PCD Driver:
  41. (##) hpcd.pData = pdev;
  42. (#)Enable PCD transmission and reception:
  43. (##) HAL_PCD_Start();
  44. @endverbatim
  45. ******************************************************************************
  46. */
  47. /* Includes ------------------------------------------------------------------*/
  48. #include "stm32l4xx_hal.h"
  49. /** @addtogroup STM32L4xx_HAL_Driver
  50. * @{
  51. */
  52. /** @defgroup PCD PCD
  53. * @brief PCD HAL module driver
  54. * @{
  55. */
  56. #ifdef HAL_PCD_MODULE_ENABLED
  57. #if defined (USB) || defined (USB_OTG_FS)
  58. /* Private types -------------------------------------------------------------*/
  59. /* Private variables ---------------------------------------------------------*/
  60. /* Private constants ---------------------------------------------------------*/
  61. /* Private macros ------------------------------------------------------------*/
  62. /** @defgroup PCD_Private_Macros PCD Private Macros
  63. * @{
  64. */
  65. #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
  66. #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
  67. /**
  68. * @}
  69. */
  70. /* Private functions prototypes ----------------------------------------------*/
  71. /** @defgroup PCD_Private_Functions PCD Private Functions
  72. * @{
  73. */
  74. #if defined (USB_OTG_FS)
  75. static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
  76. static HAL_StatusTypeDef PCD_EP_OutXfrComplete_int(PCD_HandleTypeDef *hpcd, uint32_t epnum);
  77. static HAL_StatusTypeDef PCD_EP_OutSetupPacket_int(PCD_HandleTypeDef *hpcd, uint32_t epnum);
  78. #endif /* defined (USB_OTG_FS) */
  79. #if defined (USB)
  80. static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd);
  81. #if (USE_USB_DOUBLE_BUFFER == 1U)
  82. static HAL_StatusTypeDef HAL_PCD_EP_DB_Transmit(PCD_HandleTypeDef *hpcd, PCD_EPTypeDef *ep, uint16_t wEPVal);
  83. static uint16_t HAL_PCD_EP_DB_Receive(PCD_HandleTypeDef *hpcd, PCD_EPTypeDef *ep, uint16_t wEPVal);
  84. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  85. #endif /* defined (USB) */
  86. /**
  87. * @}
  88. */
  89. /* Exported functions --------------------------------------------------------*/
  90. /** @defgroup PCD_Exported_Functions PCD Exported Functions
  91. * @{
  92. */
  93. /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
  94. * @brief Initialization and Configuration functions
  95. *
  96. @verbatim
  97. ===============================================================================
  98. ##### Initialization and de-initialization functions #####
  99. ===============================================================================
  100. [..] This section provides functions allowing to:
  101. @endverbatim
  102. * @{
  103. */
  104. /**
  105. * @brief Initializes the PCD according to the specified
  106. * parameters in the PCD_InitTypeDef and initialize the associated handle.
  107. * @param hpcd PCD handle
  108. * @retval HAL status
  109. */
  110. HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
  111. {
  112. #if defined (USB_OTG_FS)
  113. USB_OTG_GlobalTypeDef *USBx;
  114. #endif /* defined (USB_OTG_FS) */
  115. uint8_t i;
  116. /* Check the PCD handle allocation */
  117. if (hpcd == NULL)
  118. {
  119. return HAL_ERROR;
  120. }
  121. /* Check the parameters */
  122. assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
  123. #if defined (USB_OTG_FS)
  124. USBx = hpcd->Instance;
  125. #endif /* defined (USB_OTG_FS) */
  126. if (hpcd->State == HAL_PCD_STATE_RESET)
  127. {
  128. /* Allocate lock resource and initialize it */
  129. hpcd->Lock = HAL_UNLOCKED;
  130. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  131. hpcd->SOFCallback = HAL_PCD_SOFCallback;
  132. hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
  133. hpcd->ResetCallback = HAL_PCD_ResetCallback;
  134. hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
  135. hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
  136. hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
  137. hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
  138. hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback;
  139. hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback;
  140. hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback;
  141. hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback;
  142. hpcd->LPMCallback = HAL_PCDEx_LPM_Callback;
  143. hpcd->BCDCallback = HAL_PCDEx_BCD_Callback;
  144. if (hpcd->MspInitCallback == NULL)
  145. {
  146. hpcd->MspInitCallback = HAL_PCD_MspInit;
  147. }
  148. /* Init the low level hardware */
  149. hpcd->MspInitCallback(hpcd);
  150. #else
  151. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  152. HAL_PCD_MspInit(hpcd);
  153. #endif /* (USE_HAL_PCD_REGISTER_CALLBACKS) */
  154. }
  155. hpcd->State = HAL_PCD_STATE_BUSY;
  156. #if defined (USB_OTG_FS)
  157. /* Disable DMA mode for FS instance */
  158. if ((USBx->CID & (0x1U << 8)) == 0U)
  159. {
  160. hpcd->Init.dma_enable = 0U;
  161. }
  162. #endif /* defined (USB_OTG_FS) */
  163. /* Disable the Interrupts */
  164. __HAL_PCD_DISABLE(hpcd);
  165. /*Init the Core (common init.) */
  166. if (USB_CoreInit(hpcd->Instance, hpcd->Init) != HAL_OK)
  167. {
  168. hpcd->State = HAL_PCD_STATE_ERROR;
  169. return HAL_ERROR;
  170. }
  171. /* Force Device Mode*/
  172. (void)USB_SetCurrentMode(hpcd->Instance, USB_DEVICE_MODE);
  173. /* Init endpoints structures */
  174. for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
  175. {
  176. /* Init ep structure */
  177. hpcd->IN_ep[i].is_in = 1U;
  178. hpcd->IN_ep[i].num = i;
  179. hpcd->IN_ep[i].tx_fifo_num = i;
  180. /* Control until ep is activated */
  181. hpcd->IN_ep[i].type = EP_TYPE_CTRL;
  182. hpcd->IN_ep[i].maxpacket = 0U;
  183. hpcd->IN_ep[i].xfer_buff = 0U;
  184. hpcd->IN_ep[i].xfer_len = 0U;
  185. }
  186. for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
  187. {
  188. hpcd->OUT_ep[i].is_in = 0U;
  189. hpcd->OUT_ep[i].num = i;
  190. /* Control until ep is activated */
  191. hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
  192. hpcd->OUT_ep[i].maxpacket = 0U;
  193. hpcd->OUT_ep[i].xfer_buff = 0U;
  194. hpcd->OUT_ep[i].xfer_len = 0U;
  195. }
  196. /* Init Device */
  197. if (USB_DevInit(hpcd->Instance, hpcd->Init) != HAL_OK)
  198. {
  199. hpcd->State = HAL_PCD_STATE_ERROR;
  200. return HAL_ERROR;
  201. }
  202. hpcd->USB_Address = 0U;
  203. hpcd->State = HAL_PCD_STATE_READY;
  204. /* Activate LPM */
  205. if (hpcd->Init.lpm_enable == 1U)
  206. {
  207. (void)HAL_PCDEx_ActivateLPM(hpcd);
  208. }
  209. (void)USB_DevDisconnect(hpcd->Instance);
  210. return HAL_OK;
  211. }
  212. /**
  213. * @brief DeInitializes the PCD peripheral.
  214. * @param hpcd PCD handle
  215. * @retval HAL status
  216. */
  217. HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
  218. {
  219. /* Check the PCD handle allocation */
  220. if (hpcd == NULL)
  221. {
  222. return HAL_ERROR;
  223. }
  224. hpcd->State = HAL_PCD_STATE_BUSY;
  225. /* Stop Device */
  226. if (USB_StopDevice(hpcd->Instance) != HAL_OK)
  227. {
  228. return HAL_ERROR;
  229. }
  230. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  231. if (hpcd->MspDeInitCallback == NULL)
  232. {
  233. hpcd->MspDeInitCallback = HAL_PCD_MspDeInit; /* Legacy weak MspDeInit */
  234. }
  235. /* DeInit the low level hardware */
  236. hpcd->MspDeInitCallback(hpcd);
  237. #else
  238. /* DeInit the low level hardware: CLOCK, NVIC.*/
  239. HAL_PCD_MspDeInit(hpcd);
  240. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  241. hpcd->State = HAL_PCD_STATE_RESET;
  242. return HAL_OK;
  243. }
  244. /**
  245. * @brief Initializes the PCD MSP.
  246. * @param hpcd PCD handle
  247. * @retval None
  248. */
  249. __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
  250. {
  251. /* Prevent unused argument(s) compilation warning */
  252. UNUSED(hpcd);
  253. /* NOTE : This function should not be modified, when the callback is needed,
  254. the HAL_PCD_MspInit could be implemented in the user file
  255. */
  256. }
  257. /**
  258. * @brief DeInitializes PCD MSP.
  259. * @param hpcd PCD handle
  260. * @retval None
  261. */
  262. __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
  263. {
  264. /* Prevent unused argument(s) compilation warning */
  265. UNUSED(hpcd);
  266. /* NOTE : This function should not be modified, when the callback is needed,
  267. the HAL_PCD_MspDeInit could be implemented in the user file
  268. */
  269. }
  270. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  271. /**
  272. * @brief Register a User USB PCD Callback
  273. * To be used instead of the weak predefined callback
  274. * @param hpcd USB PCD handle
  275. * @param CallbackID ID of the callback to be registered
  276. * This parameter can be one of the following values:
  277. * @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
  278. * @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
  279. * @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
  280. * @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
  281. * @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
  282. * @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
  283. * @arg @ref HAL_PCD_DISCONNECT_CB_ID OTG PCD Disconnect callback ID
  284. * @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
  285. * @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
  286. * @param pCallback pointer to the Callback function
  287. * @retval HAL status
  288. */
  289. HAL_StatusTypeDef HAL_PCD_RegisterCallback(PCD_HandleTypeDef *hpcd,
  290. HAL_PCD_CallbackIDTypeDef CallbackID,
  291. pPCD_CallbackTypeDef pCallback)
  292. {
  293. HAL_StatusTypeDef status = HAL_OK;
  294. if (pCallback == NULL)
  295. {
  296. /* Update the error code */
  297. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  298. return HAL_ERROR;
  299. }
  300. /* Process locked */
  301. __HAL_LOCK(hpcd);
  302. if (hpcd->State == HAL_PCD_STATE_READY)
  303. {
  304. switch (CallbackID)
  305. {
  306. case HAL_PCD_SOF_CB_ID :
  307. hpcd->SOFCallback = pCallback;
  308. break;
  309. case HAL_PCD_SETUPSTAGE_CB_ID :
  310. hpcd->SetupStageCallback = pCallback;
  311. break;
  312. case HAL_PCD_RESET_CB_ID :
  313. hpcd->ResetCallback = pCallback;
  314. break;
  315. case HAL_PCD_SUSPEND_CB_ID :
  316. hpcd->SuspendCallback = pCallback;
  317. break;
  318. case HAL_PCD_RESUME_CB_ID :
  319. hpcd->ResumeCallback = pCallback;
  320. break;
  321. case HAL_PCD_CONNECT_CB_ID :
  322. hpcd->ConnectCallback = pCallback;
  323. break;
  324. case HAL_PCD_DISCONNECT_CB_ID :
  325. hpcd->DisconnectCallback = pCallback;
  326. break;
  327. case HAL_PCD_MSPINIT_CB_ID :
  328. hpcd->MspInitCallback = pCallback;
  329. break;
  330. case HAL_PCD_MSPDEINIT_CB_ID :
  331. hpcd->MspDeInitCallback = pCallback;
  332. break;
  333. default :
  334. /* Update the error code */
  335. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  336. /* Return error status */
  337. status = HAL_ERROR;
  338. break;
  339. }
  340. }
  341. else if (hpcd->State == HAL_PCD_STATE_RESET)
  342. {
  343. switch (CallbackID)
  344. {
  345. case HAL_PCD_MSPINIT_CB_ID :
  346. hpcd->MspInitCallback = pCallback;
  347. break;
  348. case HAL_PCD_MSPDEINIT_CB_ID :
  349. hpcd->MspDeInitCallback = pCallback;
  350. break;
  351. default :
  352. /* Update the error code */
  353. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  354. /* Return error status */
  355. status = HAL_ERROR;
  356. break;
  357. }
  358. }
  359. else
  360. {
  361. /* Update the error code */
  362. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  363. /* Return error status */
  364. status = HAL_ERROR;
  365. }
  366. /* Release Lock */
  367. __HAL_UNLOCK(hpcd);
  368. return status;
  369. }
  370. /**
  371. * @brief Unregister an USB PCD Callback
  372. * USB PCD callback is redirected to the weak predefined callback
  373. * @param hpcd USB PCD handle
  374. * @param CallbackID ID of the callback to be unregistered
  375. * This parameter can be one of the following values:
  376. * @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
  377. * @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
  378. * @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
  379. * @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
  380. * @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
  381. * @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
  382. * @arg @ref HAL_PCD_DISCONNECT_CB_ID OTG PCD Disconnect callback ID
  383. * @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
  384. * @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
  385. * @retval HAL status
  386. */
  387. HAL_StatusTypeDef HAL_PCD_UnRegisterCallback(PCD_HandleTypeDef *hpcd, HAL_PCD_CallbackIDTypeDef CallbackID)
  388. {
  389. HAL_StatusTypeDef status = HAL_OK;
  390. /* Process locked */
  391. __HAL_LOCK(hpcd);
  392. /* Setup Legacy weak Callbacks */
  393. if (hpcd->State == HAL_PCD_STATE_READY)
  394. {
  395. switch (CallbackID)
  396. {
  397. case HAL_PCD_SOF_CB_ID :
  398. hpcd->SOFCallback = HAL_PCD_SOFCallback;
  399. break;
  400. case HAL_PCD_SETUPSTAGE_CB_ID :
  401. hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
  402. break;
  403. case HAL_PCD_RESET_CB_ID :
  404. hpcd->ResetCallback = HAL_PCD_ResetCallback;
  405. break;
  406. case HAL_PCD_SUSPEND_CB_ID :
  407. hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
  408. break;
  409. case HAL_PCD_RESUME_CB_ID :
  410. hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
  411. break;
  412. case HAL_PCD_CONNECT_CB_ID :
  413. hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
  414. break;
  415. case HAL_PCD_DISCONNECT_CB_ID :
  416. hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
  417. break;
  418. case HAL_PCD_MSPINIT_CB_ID :
  419. hpcd->MspInitCallback = HAL_PCD_MspInit;
  420. break;
  421. case HAL_PCD_MSPDEINIT_CB_ID :
  422. hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
  423. break;
  424. default :
  425. /* Update the error code */
  426. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  427. /* Return error status */
  428. status = HAL_ERROR;
  429. break;
  430. }
  431. }
  432. else if (hpcd->State == HAL_PCD_STATE_RESET)
  433. {
  434. switch (CallbackID)
  435. {
  436. case HAL_PCD_MSPINIT_CB_ID :
  437. hpcd->MspInitCallback = HAL_PCD_MspInit;
  438. break;
  439. case HAL_PCD_MSPDEINIT_CB_ID :
  440. hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
  441. break;
  442. default :
  443. /* Update the error code */
  444. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  445. /* Return error status */
  446. status = HAL_ERROR;
  447. break;
  448. }
  449. }
  450. else
  451. {
  452. /* Update the error code */
  453. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  454. /* Return error status */
  455. status = HAL_ERROR;
  456. }
  457. /* Release Lock */
  458. __HAL_UNLOCK(hpcd);
  459. return status;
  460. }
  461. /**
  462. * @brief Register USB PCD Data OUT Stage Callback
  463. * To be used instead of the weak HAL_PCD_DataOutStageCallback() predefined callback
  464. * @param hpcd PCD handle
  465. * @param pCallback pointer to the USB PCD Data OUT Stage Callback function
  466. * @retval HAL status
  467. */
  468. HAL_StatusTypeDef HAL_PCD_RegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd,
  469. pPCD_DataOutStageCallbackTypeDef pCallback)
  470. {
  471. HAL_StatusTypeDef status = HAL_OK;
  472. if (pCallback == NULL)
  473. {
  474. /* Update the error code */
  475. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  476. return HAL_ERROR;
  477. }
  478. /* Process locked */
  479. __HAL_LOCK(hpcd);
  480. if (hpcd->State == HAL_PCD_STATE_READY)
  481. {
  482. hpcd->DataOutStageCallback = pCallback;
  483. }
  484. else
  485. {
  486. /* Update the error code */
  487. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  488. /* Return error status */
  489. status = HAL_ERROR;
  490. }
  491. /* Release Lock */
  492. __HAL_UNLOCK(hpcd);
  493. return status;
  494. }
  495. /**
  496. * @brief Unregister the USB PCD Data OUT Stage Callback
  497. * USB PCD Data OUT Stage Callback is redirected to the weak HAL_PCD_DataOutStageCallback() predefined callback
  498. * @param hpcd PCD handle
  499. * @retval HAL status
  500. */
  501. HAL_StatusTypeDef HAL_PCD_UnRegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd)
  502. {
  503. HAL_StatusTypeDef status = HAL_OK;
  504. /* Process locked */
  505. __HAL_LOCK(hpcd);
  506. if (hpcd->State == HAL_PCD_STATE_READY)
  507. {
  508. hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback; /* Legacy weak DataOutStageCallback */
  509. }
  510. else
  511. {
  512. /* Update the error code */
  513. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  514. /* Return error status */
  515. status = HAL_ERROR;
  516. }
  517. /* Release Lock */
  518. __HAL_UNLOCK(hpcd);
  519. return status;
  520. }
  521. /**
  522. * @brief Register USB PCD Data IN Stage Callback
  523. * To be used instead of the weak HAL_PCD_DataInStageCallback() predefined callback
  524. * @param hpcd PCD handle
  525. * @param pCallback pointer to the USB PCD Data IN Stage Callback function
  526. * @retval HAL status
  527. */
  528. HAL_StatusTypeDef HAL_PCD_RegisterDataInStageCallback(PCD_HandleTypeDef *hpcd,
  529. pPCD_DataInStageCallbackTypeDef pCallback)
  530. {
  531. HAL_StatusTypeDef status = HAL_OK;
  532. if (pCallback == NULL)
  533. {
  534. /* Update the error code */
  535. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  536. return HAL_ERROR;
  537. }
  538. /* Process locked */
  539. __HAL_LOCK(hpcd);
  540. if (hpcd->State == HAL_PCD_STATE_READY)
  541. {
  542. hpcd->DataInStageCallback = pCallback;
  543. }
  544. else
  545. {
  546. /* Update the error code */
  547. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  548. /* Return error status */
  549. status = HAL_ERROR;
  550. }
  551. /* Release Lock */
  552. __HAL_UNLOCK(hpcd);
  553. return status;
  554. }
  555. /**
  556. * @brief Unregister the USB PCD Data IN Stage Callback
  557. * USB PCD Data OUT Stage Callback is redirected to the weak HAL_PCD_DataInStageCallback() predefined callback
  558. * @param hpcd PCD handle
  559. * @retval HAL status
  560. */
  561. HAL_StatusTypeDef HAL_PCD_UnRegisterDataInStageCallback(PCD_HandleTypeDef *hpcd)
  562. {
  563. HAL_StatusTypeDef status = HAL_OK;
  564. /* Process locked */
  565. __HAL_LOCK(hpcd);
  566. if (hpcd->State == HAL_PCD_STATE_READY)
  567. {
  568. hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback; /* Legacy weak DataInStageCallback */
  569. }
  570. else
  571. {
  572. /* Update the error code */
  573. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  574. /* Return error status */
  575. status = HAL_ERROR;
  576. }
  577. /* Release Lock */
  578. __HAL_UNLOCK(hpcd);
  579. return status;
  580. }
  581. /**
  582. * @brief Register USB PCD Iso OUT incomplete Callback
  583. * To be used instead of the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
  584. * @param hpcd PCD handle
  585. * @param pCallback pointer to the USB PCD Iso OUT incomplete Callback function
  586. * @retval HAL status
  587. */
  588. HAL_StatusTypeDef HAL_PCD_RegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd,
  589. pPCD_IsoOutIncpltCallbackTypeDef pCallback)
  590. {
  591. HAL_StatusTypeDef status = HAL_OK;
  592. if (pCallback == NULL)
  593. {
  594. /* Update the error code */
  595. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  596. return HAL_ERROR;
  597. }
  598. /* Process locked */
  599. __HAL_LOCK(hpcd);
  600. if (hpcd->State == HAL_PCD_STATE_READY)
  601. {
  602. hpcd->ISOOUTIncompleteCallback = pCallback;
  603. }
  604. else
  605. {
  606. /* Update the error code */
  607. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  608. /* Return error status */
  609. status = HAL_ERROR;
  610. }
  611. /* Release Lock */
  612. __HAL_UNLOCK(hpcd);
  613. return status;
  614. }
  615. /**
  616. * @brief Unregister the USB PCD Iso OUT incomplete Callback
  617. * USB PCD Iso OUT incomplete Callback is redirected
  618. * to the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
  619. * @param hpcd PCD handle
  620. * @retval HAL status
  621. */
  622. HAL_StatusTypeDef HAL_PCD_UnRegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd)
  623. {
  624. HAL_StatusTypeDef status = HAL_OK;
  625. /* Process locked */
  626. __HAL_LOCK(hpcd);
  627. if (hpcd->State == HAL_PCD_STATE_READY)
  628. {
  629. hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback; /* Legacy weak ISOOUTIncompleteCallback */
  630. }
  631. else
  632. {
  633. /* Update the error code */
  634. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  635. /* Return error status */
  636. status = HAL_ERROR;
  637. }
  638. /* Release Lock */
  639. __HAL_UNLOCK(hpcd);
  640. return status;
  641. }
  642. /**
  643. * @brief Register USB PCD Iso IN incomplete Callback
  644. * To be used instead of the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
  645. * @param hpcd PCD handle
  646. * @param pCallback pointer to the USB PCD Iso IN incomplete Callback function
  647. * @retval HAL status
  648. */
  649. HAL_StatusTypeDef HAL_PCD_RegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd,
  650. pPCD_IsoInIncpltCallbackTypeDef pCallback)
  651. {
  652. HAL_StatusTypeDef status = HAL_OK;
  653. if (pCallback == NULL)
  654. {
  655. /* Update the error code */
  656. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  657. return HAL_ERROR;
  658. }
  659. /* Process locked */
  660. __HAL_LOCK(hpcd);
  661. if (hpcd->State == HAL_PCD_STATE_READY)
  662. {
  663. hpcd->ISOINIncompleteCallback = pCallback;
  664. }
  665. else
  666. {
  667. /* Update the error code */
  668. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  669. /* Return error status */
  670. status = HAL_ERROR;
  671. }
  672. /* Release Lock */
  673. __HAL_UNLOCK(hpcd);
  674. return status;
  675. }
  676. /**
  677. * @brief Unregister the USB PCD Iso IN incomplete Callback
  678. * USB PCD Iso IN incomplete Callback is redirected
  679. * to the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
  680. * @param hpcd PCD handle
  681. * @retval HAL status
  682. */
  683. HAL_StatusTypeDef HAL_PCD_UnRegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd)
  684. {
  685. HAL_StatusTypeDef status = HAL_OK;
  686. /* Process locked */
  687. __HAL_LOCK(hpcd);
  688. if (hpcd->State == HAL_PCD_STATE_READY)
  689. {
  690. hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback; /* Legacy weak ISOINIncompleteCallback */
  691. }
  692. else
  693. {
  694. /* Update the error code */
  695. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  696. /* Return error status */
  697. status = HAL_ERROR;
  698. }
  699. /* Release Lock */
  700. __HAL_UNLOCK(hpcd);
  701. return status;
  702. }
  703. /**
  704. * @brief Register USB PCD BCD Callback
  705. * To be used instead of the weak HAL_PCDEx_BCD_Callback() predefined callback
  706. * @param hpcd PCD handle
  707. * @param pCallback pointer to the USB PCD BCD Callback function
  708. * @retval HAL status
  709. */
  710. HAL_StatusTypeDef HAL_PCD_RegisterBcdCallback(PCD_HandleTypeDef *hpcd, pPCD_BcdCallbackTypeDef pCallback)
  711. {
  712. HAL_StatusTypeDef status = HAL_OK;
  713. if (pCallback == NULL)
  714. {
  715. /* Update the error code */
  716. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  717. return HAL_ERROR;
  718. }
  719. /* Process locked */
  720. __HAL_LOCK(hpcd);
  721. if (hpcd->State == HAL_PCD_STATE_READY)
  722. {
  723. hpcd->BCDCallback = pCallback;
  724. }
  725. else
  726. {
  727. /* Update the error code */
  728. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  729. /* Return error status */
  730. status = HAL_ERROR;
  731. }
  732. /* Release Lock */
  733. __HAL_UNLOCK(hpcd);
  734. return status;
  735. }
  736. /**
  737. * @brief Unregister the USB PCD BCD Callback
  738. * USB BCD Callback is redirected to the weak HAL_PCDEx_BCD_Callback() predefined callback
  739. * @param hpcd PCD handle
  740. * @retval HAL status
  741. */
  742. HAL_StatusTypeDef HAL_PCD_UnRegisterBcdCallback(PCD_HandleTypeDef *hpcd)
  743. {
  744. HAL_StatusTypeDef status = HAL_OK;
  745. /* Process locked */
  746. __HAL_LOCK(hpcd);
  747. if (hpcd->State == HAL_PCD_STATE_READY)
  748. {
  749. hpcd->BCDCallback = HAL_PCDEx_BCD_Callback; /* Legacy weak HAL_PCDEx_BCD_Callback */
  750. }
  751. else
  752. {
  753. /* Update the error code */
  754. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  755. /* Return error status */
  756. status = HAL_ERROR;
  757. }
  758. /* Release Lock */
  759. __HAL_UNLOCK(hpcd);
  760. return status;
  761. }
  762. /**
  763. * @brief Register USB PCD LPM Callback
  764. * To be used instead of the weak HAL_PCDEx_LPM_Callback() predefined callback
  765. * @param hpcd PCD handle
  766. * @param pCallback pointer to the USB PCD LPM Callback function
  767. * @retval HAL status
  768. */
  769. HAL_StatusTypeDef HAL_PCD_RegisterLpmCallback(PCD_HandleTypeDef *hpcd, pPCD_LpmCallbackTypeDef pCallback)
  770. {
  771. HAL_StatusTypeDef status = HAL_OK;
  772. if (pCallback == NULL)
  773. {
  774. /* Update the error code */
  775. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  776. return HAL_ERROR;
  777. }
  778. /* Process locked */
  779. __HAL_LOCK(hpcd);
  780. if (hpcd->State == HAL_PCD_STATE_READY)
  781. {
  782. hpcd->LPMCallback = pCallback;
  783. }
  784. else
  785. {
  786. /* Update the error code */
  787. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  788. /* Return error status */
  789. status = HAL_ERROR;
  790. }
  791. /* Release Lock */
  792. __HAL_UNLOCK(hpcd);
  793. return status;
  794. }
  795. /**
  796. * @brief Unregister the USB PCD LPM Callback
  797. * USB LPM Callback is redirected to the weak HAL_PCDEx_LPM_Callback() predefined callback
  798. * @param hpcd PCD handle
  799. * @retval HAL status
  800. */
  801. HAL_StatusTypeDef HAL_PCD_UnRegisterLpmCallback(PCD_HandleTypeDef *hpcd)
  802. {
  803. HAL_StatusTypeDef status = HAL_OK;
  804. /* Process locked */
  805. __HAL_LOCK(hpcd);
  806. if (hpcd->State == HAL_PCD_STATE_READY)
  807. {
  808. hpcd->LPMCallback = HAL_PCDEx_LPM_Callback; /* Legacy weak HAL_PCDEx_LPM_Callback */
  809. }
  810. else
  811. {
  812. /* Update the error code */
  813. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  814. /* Return error status */
  815. status = HAL_ERROR;
  816. }
  817. /* Release Lock */
  818. __HAL_UNLOCK(hpcd);
  819. return status;
  820. }
  821. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  822. /**
  823. * @}
  824. */
  825. /** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
  826. * @brief Data transfers functions
  827. *
  828. @verbatim
  829. ===============================================================================
  830. ##### IO operation functions #####
  831. ===============================================================================
  832. [..]
  833. This subsection provides a set of functions allowing to manage the PCD data
  834. transfers.
  835. @endverbatim
  836. * @{
  837. */
  838. /**
  839. * @brief Start the USB device
  840. * @param hpcd PCD handle
  841. * @retval HAL status
  842. */
  843. HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
  844. {
  845. #if defined (USB_OTG_FS)
  846. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  847. #endif /* defined (USB_OTG_FS) */
  848. __HAL_LOCK(hpcd);
  849. #if defined (USB_OTG_FS)
  850. if (hpcd->Init.battery_charging_enable == 1U)
  851. {
  852. /* Enable USB Transceiver */
  853. USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
  854. }
  855. #endif /* defined (USB_OTG_FS) */
  856. __HAL_PCD_ENABLE(hpcd);
  857. (void)USB_DevConnect(hpcd->Instance);
  858. __HAL_UNLOCK(hpcd);
  859. return HAL_OK;
  860. }
  861. /**
  862. * @brief Stop the USB device.
  863. * @param hpcd PCD handle
  864. * @retval HAL status
  865. */
  866. HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
  867. {
  868. #if defined (USB_OTG_FS)
  869. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  870. #endif /* defined (USB_OTG_FS) */
  871. __HAL_LOCK(hpcd);
  872. __HAL_PCD_DISABLE(hpcd);
  873. (void)USB_DevDisconnect(hpcd->Instance);
  874. #if defined (USB_OTG_FS)
  875. (void)USB_FlushTxFifo(hpcd->Instance, 0x10U);
  876. if (hpcd->Init.battery_charging_enable == 1U)
  877. {
  878. /* Disable USB Transceiver */
  879. USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
  880. }
  881. #endif /* defined (USB_OTG_FS) */
  882. __HAL_UNLOCK(hpcd);
  883. return HAL_OK;
  884. }
  885. #if defined (USB_OTG_FS)
  886. /**
  887. * @brief Handles PCD interrupt request.
  888. * @param hpcd PCD handle
  889. * @retval HAL status
  890. */
  891. void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
  892. {
  893. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  894. uint32_t USBx_BASE = (uint32_t)USBx;
  895. USB_OTG_EPTypeDef *ep;
  896. uint32_t i;
  897. uint32_t ep_intr;
  898. uint32_t epint;
  899. uint32_t epnum;
  900. uint32_t fifoemptymsk;
  901. uint32_t RegVal;
  902. /* ensure that we are in device mode */
  903. if (USB_GetMode(hpcd->Instance) == USB_OTG_MODE_DEVICE)
  904. {
  905. /* avoid spurious interrupt */
  906. if (__HAL_PCD_IS_INVALID_INTERRUPT(hpcd))
  907. {
  908. return;
  909. }
  910. /* store current frame number */
  911. hpcd->FrameNumber = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF_Msk) >> USB_OTG_DSTS_FNSOF_Pos;
  912. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_MMIS))
  913. {
  914. /* incorrect mode, acknowledge the interrupt */
  915. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_MMIS);
  916. }
  917. /* Handle RxQLevel Interrupt */
  918. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL))
  919. {
  920. USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  921. RegVal = USBx->GRXSTSP;
  922. ep = &hpcd->OUT_ep[RegVal & USB_OTG_GRXSTSP_EPNUM];
  923. if (((RegVal & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT)
  924. {
  925. if ((RegVal & USB_OTG_GRXSTSP_BCNT) != 0U)
  926. {
  927. (void)USB_ReadPacket(USBx, ep->xfer_buff,
  928. (uint16_t)((RegVal & USB_OTG_GRXSTSP_BCNT) >> 4));
  929. ep->xfer_buff += (RegVal & USB_OTG_GRXSTSP_BCNT) >> 4;
  930. ep->xfer_count += (RegVal & USB_OTG_GRXSTSP_BCNT) >> 4;
  931. }
  932. }
  933. else if (((RegVal & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT)
  934. {
  935. (void)USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8U);
  936. ep->xfer_count += (RegVal & USB_OTG_GRXSTSP_BCNT) >> 4;
  937. }
  938. else
  939. {
  940. /* ... */
  941. }
  942. USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  943. }
  944. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OEPINT))
  945. {
  946. epnum = 0U;
  947. /* Read in the device interrupt bits */
  948. ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
  949. while (ep_intr != 0U)
  950. {
  951. if ((ep_intr & 0x1U) != 0U)
  952. {
  953. epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, (uint8_t)epnum);
  954. if ((epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC)
  955. {
  956. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
  957. (void)PCD_EP_OutXfrComplete_int(hpcd, epnum);
  958. }
  959. if ((epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP)
  960. {
  961. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
  962. /* Class B setup phase done for previous decoded setup */
  963. (void)PCD_EP_OutSetupPacket_int(hpcd, epnum);
  964. }
  965. if ((epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
  966. {
  967. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
  968. }
  969. /* Clear OUT Endpoint disable interrupt */
  970. if ((epint & USB_OTG_DOEPINT_EPDISD) == USB_OTG_DOEPINT_EPDISD)
  971. {
  972. if ((USBx->GINTSTS & USB_OTG_GINTSTS_BOUTNAKEFF) == USB_OTG_GINTSTS_BOUTNAKEFF)
  973. {
  974. USBx_DEVICE->DCTL |= USB_OTG_DCTL_CGONAK;
  975. }
  976. ep = &hpcd->OUT_ep[epnum];
  977. if (ep->is_iso_incomplete == 1U)
  978. {
  979. ep->is_iso_incomplete = 0U;
  980. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  981. hpcd->ISOOUTIncompleteCallback(hpcd, (uint8_t)epnum);
  982. #else
  983. HAL_PCD_ISOOUTIncompleteCallback(hpcd, (uint8_t)epnum);
  984. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  985. }
  986. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_EPDISD);
  987. }
  988. /* Clear Status Phase Received interrupt */
  989. if ((epint & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
  990. {
  991. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
  992. }
  993. /* Clear OUT NAK interrupt */
  994. if ((epint & USB_OTG_DOEPINT_NAK) == USB_OTG_DOEPINT_NAK)
  995. {
  996. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_NAK);
  997. }
  998. }
  999. epnum++;
  1000. ep_intr >>= 1U;
  1001. }
  1002. }
  1003. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT))
  1004. {
  1005. /* Read in the device interrupt bits */
  1006. ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
  1007. epnum = 0U;
  1008. while (ep_intr != 0U)
  1009. {
  1010. if ((ep_intr & 0x1U) != 0U) /* In ITR */
  1011. {
  1012. epint = USB_ReadDevInEPInterrupt(hpcd->Instance, (uint8_t)epnum);
  1013. if ((epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
  1014. {
  1015. fifoemptymsk = (uint32_t)(0x1UL << (epnum & EP_ADDR_MSK));
  1016. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  1017. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
  1018. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1019. hpcd->DataInStageCallback(hpcd, (uint8_t)epnum);
  1020. #else
  1021. HAL_PCD_DataInStageCallback(hpcd, (uint8_t)epnum);
  1022. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1023. }
  1024. if ((epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
  1025. {
  1026. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
  1027. }
  1028. if ((epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
  1029. {
  1030. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
  1031. }
  1032. if ((epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
  1033. {
  1034. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
  1035. }
  1036. if ((epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
  1037. {
  1038. (void)USB_FlushTxFifo(USBx, epnum);
  1039. ep = &hpcd->IN_ep[epnum];
  1040. if (ep->is_iso_incomplete == 1U)
  1041. {
  1042. ep->is_iso_incomplete = 0U;
  1043. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1044. hpcd->ISOINIncompleteCallback(hpcd, (uint8_t)epnum);
  1045. #else
  1046. HAL_PCD_ISOINIncompleteCallback(hpcd, (uint8_t)epnum);
  1047. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1048. }
  1049. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
  1050. }
  1051. if ((epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE)
  1052. {
  1053. (void)PCD_WriteEmptyTxFifo(hpcd, epnum);
  1054. }
  1055. }
  1056. epnum++;
  1057. ep_intr >>= 1U;
  1058. }
  1059. }
  1060. /* Handle Resume Interrupt */
  1061. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT))
  1062. {
  1063. /* Clear the Remote Wake-up Signaling */
  1064. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  1065. if (hpcd->LPM_State == LPM_L1)
  1066. {
  1067. hpcd->LPM_State = LPM_L0;
  1068. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1069. hpcd->LPMCallback(hpcd, PCD_LPM_L0_ACTIVE);
  1070. #else
  1071. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
  1072. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1073. }
  1074. else
  1075. {
  1076. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1077. hpcd->ResumeCallback(hpcd);
  1078. #else
  1079. HAL_PCD_ResumeCallback(hpcd);
  1080. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1081. }
  1082. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
  1083. }
  1084. /* Handle Suspend Interrupt */
  1085. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP))
  1086. {
  1087. if ((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
  1088. {
  1089. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1090. hpcd->SuspendCallback(hpcd);
  1091. #else
  1092. HAL_PCD_SuspendCallback(hpcd);
  1093. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1094. }
  1095. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
  1096. }
  1097. /* Handle LPM Interrupt */
  1098. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT))
  1099. {
  1100. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT);
  1101. if (hpcd->LPM_State == LPM_L0)
  1102. {
  1103. hpcd->LPM_State = LPM_L1;
  1104. hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >> 2U;
  1105. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1106. hpcd->LPMCallback(hpcd, PCD_LPM_L1_ACTIVE);
  1107. #else
  1108. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
  1109. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1110. }
  1111. else
  1112. {
  1113. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1114. hpcd->SuspendCallback(hpcd);
  1115. #else
  1116. HAL_PCD_SuspendCallback(hpcd);
  1117. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1118. }
  1119. }
  1120. /* Handle Reset Interrupt */
  1121. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST))
  1122. {
  1123. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  1124. (void)USB_FlushTxFifo(hpcd->Instance, 0x10U);
  1125. for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
  1126. {
  1127. USBx_INEP(i)->DIEPINT = 0xFB7FU;
  1128. USBx_INEP(i)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
  1129. USBx_OUTEP(i)->DOEPINT = 0xFB7FU;
  1130. USBx_OUTEP(i)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
  1131. USBx_OUTEP(i)->DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
  1132. }
  1133. USBx_DEVICE->DAINTMSK |= 0x10001U;
  1134. if (hpcd->Init.use_dedicated_ep1 != 0U)
  1135. {
  1136. USBx_DEVICE->DOUTEP1MSK |= USB_OTG_DOEPMSK_STUPM |
  1137. USB_OTG_DOEPMSK_XFRCM |
  1138. USB_OTG_DOEPMSK_EPDM;
  1139. USBx_DEVICE->DINEP1MSK |= USB_OTG_DIEPMSK_TOM |
  1140. USB_OTG_DIEPMSK_XFRCM |
  1141. USB_OTG_DIEPMSK_EPDM;
  1142. }
  1143. else
  1144. {
  1145. USBx_DEVICE->DOEPMSK |= USB_OTG_DOEPMSK_STUPM |
  1146. USB_OTG_DOEPMSK_XFRCM |
  1147. USB_OTG_DOEPMSK_EPDM |
  1148. USB_OTG_DOEPMSK_OTEPSPRM |
  1149. USB_OTG_DOEPMSK_NAKM;
  1150. USBx_DEVICE->DIEPMSK |= USB_OTG_DIEPMSK_TOM |
  1151. USB_OTG_DIEPMSK_XFRCM |
  1152. USB_OTG_DIEPMSK_EPDM;
  1153. }
  1154. /* Set Default Address to 0 */
  1155. USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
  1156. /* setup EP0 to receive SETUP packets */
  1157. (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t *)hpcd->Setup);
  1158. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
  1159. }
  1160. /* Handle Enumeration done Interrupt */
  1161. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE))
  1162. {
  1163. (void)USB_ActivateSetup(hpcd->Instance);
  1164. hpcd->Init.speed = USB_GetDevSpeed(hpcd->Instance);
  1165. /* Set USB Turnaround time */
  1166. (void)USB_SetTurnaroundTime(hpcd->Instance,
  1167. HAL_RCC_GetHCLKFreq(),
  1168. (uint8_t)hpcd->Init.speed);
  1169. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1170. hpcd->ResetCallback(hpcd);
  1171. #else
  1172. HAL_PCD_ResetCallback(hpcd);
  1173. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1174. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE);
  1175. }
  1176. /* Handle SOF Interrupt */
  1177. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SOF))
  1178. {
  1179. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1180. hpcd->SOFCallback(hpcd);
  1181. #else
  1182. HAL_PCD_SOFCallback(hpcd);
  1183. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1184. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SOF);
  1185. }
  1186. /* Handle Global OUT NAK effective Interrupt */
  1187. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_BOUTNAKEFF))
  1188. {
  1189. USBx->GINTMSK &= ~USB_OTG_GINTMSK_GONAKEFFM;
  1190. for (epnum = 1U; epnum < hpcd->Init.dev_endpoints; epnum++)
  1191. {
  1192. if (hpcd->OUT_ep[epnum].is_iso_incomplete == 1U)
  1193. {
  1194. /* Abort current transaction and disable the EP */
  1195. (void)HAL_PCD_EP_Abort(hpcd, (uint8_t)epnum);
  1196. }
  1197. }
  1198. }
  1199. /* Handle Incomplete ISO IN Interrupt */
  1200. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR))
  1201. {
  1202. for (epnum = 1U; epnum < hpcd->Init.dev_endpoints; epnum++)
  1203. {
  1204. RegVal = USBx_INEP(epnum)->DIEPCTL;
  1205. if ((hpcd->IN_ep[epnum].type == EP_TYPE_ISOC) &&
  1206. ((RegVal & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA))
  1207. {
  1208. hpcd->IN_ep[epnum].is_iso_incomplete = 1U;
  1209. /* Abort current transaction and disable the EP */
  1210. (void)HAL_PCD_EP_Abort(hpcd, (uint8_t)(epnum | 0x80U));
  1211. }
  1212. }
  1213. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR);
  1214. }
  1215. /* Handle Incomplete ISO OUT Interrupt */
  1216. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
  1217. {
  1218. for (epnum = 1U; epnum < hpcd->Init.dev_endpoints; epnum++)
  1219. {
  1220. RegVal = USBx_OUTEP(epnum)->DOEPCTL;
  1221. if ((hpcd->OUT_ep[epnum].type == EP_TYPE_ISOC) &&
  1222. ((RegVal & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA) &&
  1223. ((RegVal & (0x1U << 16)) == (hpcd->FrameNumber & 0x1U)))
  1224. {
  1225. hpcd->OUT_ep[epnum].is_iso_incomplete = 1U;
  1226. USBx->GINTMSK |= USB_OTG_GINTMSK_GONAKEFFM;
  1227. if ((USBx->GINTSTS & USB_OTG_GINTSTS_BOUTNAKEFF) == 0U)
  1228. {
  1229. USBx_DEVICE->DCTL |= USB_OTG_DCTL_SGONAK;
  1230. break;
  1231. }
  1232. }
  1233. }
  1234. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
  1235. }
  1236. /* Handle Connection event Interrupt */
  1237. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT))
  1238. {
  1239. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1240. hpcd->ConnectCallback(hpcd);
  1241. #else
  1242. HAL_PCD_ConnectCallback(hpcd);
  1243. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1244. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT);
  1245. }
  1246. /* Handle Disconnection event Interrupt */
  1247. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OTGINT))
  1248. {
  1249. RegVal = hpcd->Instance->GOTGINT;
  1250. if ((RegVal & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET)
  1251. {
  1252. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1253. hpcd->DisconnectCallback(hpcd);
  1254. #else
  1255. HAL_PCD_DisconnectCallback(hpcd);
  1256. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1257. }
  1258. hpcd->Instance->GOTGINT |= RegVal;
  1259. }
  1260. }
  1261. }
  1262. #endif /* defined (USB_OTG_FS) */
  1263. #if defined (USB)
  1264. /**
  1265. * @brief This function handles PCD interrupt request.
  1266. * @param hpcd PCD handle
  1267. * @retval HAL status
  1268. */
  1269. void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
  1270. {
  1271. uint32_t wIstr = USB_ReadInterrupts(hpcd->Instance);
  1272. if ((wIstr & USB_ISTR_CTR) == USB_ISTR_CTR)
  1273. {
  1274. /* servicing of the endpoint correct transfer interrupt */
  1275. /* clear of the CTR flag into the sub */
  1276. (void)PCD_EP_ISR_Handler(hpcd);
  1277. return;
  1278. }
  1279. if ((wIstr & USB_ISTR_RESET) == USB_ISTR_RESET)
  1280. {
  1281. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_RESET);
  1282. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1283. hpcd->ResetCallback(hpcd);
  1284. #else
  1285. HAL_PCD_ResetCallback(hpcd);
  1286. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1287. (void)HAL_PCD_SetAddress(hpcd, 0U);
  1288. return;
  1289. }
  1290. if ((wIstr & USB_ISTR_PMAOVR) == USB_ISTR_PMAOVR)
  1291. {
  1292. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_PMAOVR);
  1293. return;
  1294. }
  1295. if ((wIstr & USB_ISTR_ERR) == USB_ISTR_ERR)
  1296. {
  1297. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ERR);
  1298. return;
  1299. }
  1300. if ((wIstr & USB_ISTR_WKUP) == USB_ISTR_WKUP)
  1301. {
  1302. hpcd->Instance->CNTR &= (uint16_t) ~(USB_CNTR_LPMODE);
  1303. hpcd->Instance->CNTR &= (uint16_t) ~(USB_CNTR_FSUSP);
  1304. if (hpcd->LPM_State == LPM_L1)
  1305. {
  1306. hpcd->LPM_State = LPM_L0;
  1307. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1308. hpcd->LPMCallback(hpcd, PCD_LPM_L0_ACTIVE);
  1309. #else
  1310. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
  1311. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1312. }
  1313. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1314. hpcd->ResumeCallback(hpcd);
  1315. #else
  1316. HAL_PCD_ResumeCallback(hpcd);
  1317. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1318. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_WKUP);
  1319. return;
  1320. }
  1321. if ((wIstr & USB_ISTR_SUSP) == USB_ISTR_SUSP)
  1322. {
  1323. /* Force low-power mode in the macrocell */
  1324. hpcd->Instance->CNTR |= (uint16_t)USB_CNTR_FSUSP;
  1325. /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
  1326. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SUSP);
  1327. hpcd->Instance->CNTR |= (uint16_t)USB_CNTR_LPMODE;
  1328. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1329. hpcd->SuspendCallback(hpcd);
  1330. #else
  1331. HAL_PCD_SuspendCallback(hpcd);
  1332. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1333. return;
  1334. }
  1335. /* Handle LPM Interrupt */
  1336. if ((wIstr & USB_ISTR_L1REQ) == USB_ISTR_L1REQ)
  1337. {
  1338. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_L1REQ);
  1339. if (hpcd->LPM_State == LPM_L0)
  1340. {
  1341. /* Force suspend and low-power mode before going to L1 state*/
  1342. hpcd->Instance->CNTR |= (uint16_t)USB_CNTR_LPMODE;
  1343. hpcd->Instance->CNTR |= (uint16_t)USB_CNTR_FSUSP;
  1344. hpcd->LPM_State = LPM_L1;
  1345. hpcd->BESL = ((uint32_t)hpcd->Instance->LPMCSR & USB_LPMCSR_BESL) >> 2;
  1346. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1347. hpcd->LPMCallback(hpcd, PCD_LPM_L1_ACTIVE);
  1348. #else
  1349. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
  1350. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1351. }
  1352. else
  1353. {
  1354. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1355. hpcd->SuspendCallback(hpcd);
  1356. #else
  1357. HAL_PCD_SuspendCallback(hpcd);
  1358. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1359. }
  1360. return;
  1361. }
  1362. if ((wIstr & USB_ISTR_SOF) == USB_ISTR_SOF)
  1363. {
  1364. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SOF);
  1365. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1366. hpcd->SOFCallback(hpcd);
  1367. #else
  1368. HAL_PCD_SOFCallback(hpcd);
  1369. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1370. return;
  1371. }
  1372. if ((wIstr & USB_ISTR_ESOF) == USB_ISTR_ESOF)
  1373. {
  1374. /* clear ESOF flag in ISTR */
  1375. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ESOF);
  1376. return;
  1377. }
  1378. }
  1379. #endif /* defined (USB) */
  1380. /**
  1381. * @brief Data OUT stage callback.
  1382. * @param hpcd PCD handle
  1383. * @param epnum endpoint number
  1384. * @retval None
  1385. */
  1386. __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1387. {
  1388. /* Prevent unused argument(s) compilation warning */
  1389. UNUSED(hpcd);
  1390. UNUSED(epnum);
  1391. /* NOTE : This function should not be modified, when the callback is needed,
  1392. the HAL_PCD_DataOutStageCallback could be implemented in the user file
  1393. */
  1394. }
  1395. /**
  1396. * @brief Data IN stage callback
  1397. * @param hpcd PCD handle
  1398. * @param epnum endpoint number
  1399. * @retval None
  1400. */
  1401. __weak void HAL_PCD_DataInStageCallback(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_DataInStageCallback could be implemented in the user file
  1408. */
  1409. }
  1410. /**
  1411. * @brief Setup stage callback
  1412. * @param hpcd PCD handle
  1413. * @retval None
  1414. */
  1415. __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  1416. {
  1417. /* Prevent unused argument(s) compilation warning */
  1418. UNUSED(hpcd);
  1419. /* NOTE : This function should not be modified, when the callback is needed,
  1420. the HAL_PCD_SetupStageCallback could be implemented in the user file
  1421. */
  1422. }
  1423. /**
  1424. * @brief USB Start Of Frame callback.
  1425. * @param hpcd PCD handle
  1426. * @retval None
  1427. */
  1428. __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  1429. {
  1430. /* Prevent unused argument(s) compilation warning */
  1431. UNUSED(hpcd);
  1432. /* NOTE : This function should not be modified, when the callback is needed,
  1433. the HAL_PCD_SOFCallback could be implemented in the user file
  1434. */
  1435. }
  1436. /**
  1437. * @brief USB Reset callback.
  1438. * @param hpcd PCD handle
  1439. * @retval None
  1440. */
  1441. __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  1442. {
  1443. /* Prevent unused argument(s) compilation warning */
  1444. UNUSED(hpcd);
  1445. /* NOTE : This function should not be modified, when the callback is needed,
  1446. the HAL_PCD_ResetCallback could be implemented in the user file
  1447. */
  1448. }
  1449. /**
  1450. * @brief Suspend event callback.
  1451. * @param hpcd PCD handle
  1452. * @retval None
  1453. */
  1454. __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  1455. {
  1456. /* Prevent unused argument(s) compilation warning */
  1457. UNUSED(hpcd);
  1458. /* NOTE : This function should not be modified, when the callback is needed,
  1459. the HAL_PCD_SuspendCallback could be implemented in the user file
  1460. */
  1461. }
  1462. /**
  1463. * @brief Resume event callback.
  1464. * @param hpcd PCD handle
  1465. * @retval None
  1466. */
  1467. __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  1468. {
  1469. /* Prevent unused argument(s) compilation warning */
  1470. UNUSED(hpcd);
  1471. /* NOTE : This function should not be modified, when the callback is needed,
  1472. the HAL_PCD_ResumeCallback could be implemented in the user file
  1473. */
  1474. }
  1475. /**
  1476. * @brief Incomplete ISO OUT callback.
  1477. * @param hpcd PCD handle
  1478. * @param epnum endpoint number
  1479. * @retval None
  1480. */
  1481. __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1482. {
  1483. /* Prevent unused argument(s) compilation warning */
  1484. UNUSED(hpcd);
  1485. UNUSED(epnum);
  1486. /* NOTE : This function should not be modified, when the callback is needed,
  1487. the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
  1488. */
  1489. }
  1490. /**
  1491. * @brief Incomplete ISO IN callback.
  1492. * @param hpcd PCD handle
  1493. * @param epnum endpoint number
  1494. * @retval None
  1495. */
  1496. __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1497. {
  1498. /* Prevent unused argument(s) compilation warning */
  1499. UNUSED(hpcd);
  1500. UNUSED(epnum);
  1501. /* NOTE : This function should not be modified, when the callback is needed,
  1502. the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
  1503. */
  1504. }
  1505. /**
  1506. * @brief Connection event callback.
  1507. * @param hpcd PCD handle
  1508. * @retval None
  1509. */
  1510. __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  1511. {
  1512. /* Prevent unused argument(s) compilation warning */
  1513. UNUSED(hpcd);
  1514. /* NOTE : This function should not be modified, when the callback is needed,
  1515. the HAL_PCD_ConnectCallback could be implemented in the user file
  1516. */
  1517. }
  1518. /**
  1519. * @brief Disconnection event callback.
  1520. * @param hpcd PCD handle
  1521. * @retval None
  1522. */
  1523. __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  1524. {
  1525. /* Prevent unused argument(s) compilation warning */
  1526. UNUSED(hpcd);
  1527. /* NOTE : This function should not be modified, when the callback is needed,
  1528. the HAL_PCD_DisconnectCallback could be implemented in the user file
  1529. */
  1530. }
  1531. /**
  1532. * @}
  1533. */
  1534. /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
  1535. * @brief management functions
  1536. *
  1537. @verbatim
  1538. ===============================================================================
  1539. ##### Peripheral Control functions #####
  1540. ===============================================================================
  1541. [..]
  1542. This subsection provides a set of functions allowing to control the PCD data
  1543. transfers.
  1544. @endverbatim
  1545. * @{
  1546. */
  1547. /**
  1548. * @brief Connect the USB device
  1549. * @param hpcd PCD handle
  1550. * @retval HAL status
  1551. */
  1552. HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
  1553. {
  1554. #if defined (USB_OTG_FS)
  1555. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1556. #endif /* defined (USB_OTG_FS) */
  1557. __HAL_LOCK(hpcd);
  1558. #if defined (USB_OTG_FS)
  1559. if (hpcd->Init.battery_charging_enable == 1U)
  1560. {
  1561. /* Enable USB Transceiver */
  1562. USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
  1563. }
  1564. #endif /* defined (USB_OTG_FS) */
  1565. (void)USB_DevConnect(hpcd->Instance);
  1566. __HAL_UNLOCK(hpcd);
  1567. return HAL_OK;
  1568. }
  1569. /**
  1570. * @brief Disconnect the USB device.
  1571. * @param hpcd PCD handle
  1572. * @retval HAL status
  1573. */
  1574. HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
  1575. {
  1576. #if defined (USB_OTG_FS)
  1577. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1578. #endif /* defined (USB_OTG_FS) */
  1579. __HAL_LOCK(hpcd);
  1580. (void)USB_DevDisconnect(hpcd->Instance);
  1581. #if defined (USB_OTG_FS)
  1582. if (hpcd->Init.battery_charging_enable == 1U)
  1583. {
  1584. /* Disable USB Transceiver */
  1585. USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
  1586. }
  1587. #endif /* defined (USB_OTG_FS) */
  1588. __HAL_UNLOCK(hpcd);
  1589. return HAL_OK;
  1590. }
  1591. /**
  1592. * @brief Set the USB Device address.
  1593. * @param hpcd PCD handle
  1594. * @param address new device address
  1595. * @retval HAL status
  1596. */
  1597. HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
  1598. {
  1599. __HAL_LOCK(hpcd);
  1600. hpcd->USB_Address = address;
  1601. (void)USB_SetDevAddress(hpcd->Instance, address);
  1602. __HAL_UNLOCK(hpcd);
  1603. return HAL_OK;
  1604. }
  1605. /**
  1606. * @brief Open and configure an endpoint.
  1607. * @param hpcd PCD handle
  1608. * @param ep_addr endpoint address
  1609. * @param ep_mps endpoint max packet size
  1610. * @param ep_type endpoint type
  1611. * @retval HAL status
  1612. */
  1613. HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
  1614. uint16_t ep_mps, uint8_t ep_type)
  1615. {
  1616. HAL_StatusTypeDef ret = HAL_OK;
  1617. PCD_EPTypeDef *ep;
  1618. if ((ep_addr & 0x80U) == 0x80U)
  1619. {
  1620. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1621. ep->is_in = 1U;
  1622. }
  1623. else
  1624. {
  1625. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1626. ep->is_in = 0U;
  1627. }
  1628. ep->num = ep_addr & EP_ADDR_MSK;
  1629. ep->maxpacket = ep_mps;
  1630. ep->type = ep_type;
  1631. if (ep->is_in != 0U)
  1632. {
  1633. /* Assign a Tx FIFO */
  1634. ep->tx_fifo_num = ep->num;
  1635. }
  1636. /* Set initial data PID. */
  1637. if (ep_type == EP_TYPE_BULK)
  1638. {
  1639. ep->data_pid_start = 0U;
  1640. }
  1641. __HAL_LOCK(hpcd);
  1642. (void)USB_ActivateEndpoint(hpcd->Instance, ep);
  1643. __HAL_UNLOCK(hpcd);
  1644. return ret;
  1645. }
  1646. /**
  1647. * @brief Deactivate an endpoint.
  1648. * @param hpcd PCD handle
  1649. * @param ep_addr endpoint address
  1650. * @retval HAL status
  1651. */
  1652. HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1653. {
  1654. PCD_EPTypeDef *ep;
  1655. if ((ep_addr & 0x80U) == 0x80U)
  1656. {
  1657. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1658. ep->is_in = 1U;
  1659. }
  1660. else
  1661. {
  1662. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1663. ep->is_in = 0U;
  1664. }
  1665. ep->num = ep_addr & EP_ADDR_MSK;
  1666. __HAL_LOCK(hpcd);
  1667. (void)USB_DeactivateEndpoint(hpcd->Instance, ep);
  1668. __HAL_UNLOCK(hpcd);
  1669. return HAL_OK;
  1670. }
  1671. /**
  1672. * @brief Receive an amount of data.
  1673. * @param hpcd PCD handle
  1674. * @param ep_addr endpoint address
  1675. * @param pBuf pointer to the reception buffer
  1676. * @param len amount of data to be received
  1677. * @retval HAL status
  1678. */
  1679. HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  1680. {
  1681. PCD_EPTypeDef *ep;
  1682. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1683. /*setup and start the Xfer */
  1684. ep->xfer_buff = pBuf;
  1685. ep->xfer_len = len;
  1686. ep->xfer_count = 0U;
  1687. ep->is_in = 0U;
  1688. ep->num = ep_addr & EP_ADDR_MSK;
  1689. if ((ep_addr & EP_ADDR_MSK) == 0U)
  1690. {
  1691. (void)USB_EP0StartXfer(hpcd->Instance, ep);
  1692. }
  1693. else
  1694. {
  1695. (void)USB_EPStartXfer(hpcd->Instance, ep);
  1696. }
  1697. return HAL_OK;
  1698. }
  1699. /**
  1700. * @brief Get Received Data Size
  1701. * @param hpcd PCD handle
  1702. * @param ep_addr endpoint address
  1703. * @retval Data Size
  1704. */
  1705. uint32_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1706. {
  1707. return hpcd->OUT_ep[ep_addr & EP_ADDR_MSK].xfer_count;
  1708. }
  1709. /**
  1710. * @brief Send an amount of data
  1711. * @param hpcd PCD handle
  1712. * @param ep_addr endpoint address
  1713. * @param pBuf pointer to the transmission buffer
  1714. * @param len amount of data to be sent
  1715. * @retval HAL status
  1716. */
  1717. HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  1718. {
  1719. PCD_EPTypeDef *ep;
  1720. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1721. /*setup and start the Xfer */
  1722. ep->xfer_buff = pBuf;
  1723. ep->xfer_len = len;
  1724. #if defined (USB)
  1725. ep->xfer_fill_db = 1U;
  1726. ep->xfer_len_db = len;
  1727. #endif /* defined (USB) */
  1728. ep->xfer_count = 0U;
  1729. ep->is_in = 1U;
  1730. ep->num = ep_addr & EP_ADDR_MSK;
  1731. if ((ep_addr & EP_ADDR_MSK) == 0U)
  1732. {
  1733. (void)USB_EP0StartXfer(hpcd->Instance, ep);
  1734. }
  1735. else
  1736. {
  1737. (void)USB_EPStartXfer(hpcd->Instance, ep);
  1738. }
  1739. return HAL_OK;
  1740. }
  1741. /**
  1742. * @brief Set a STALL condition over an endpoint
  1743. * @param hpcd PCD handle
  1744. * @param ep_addr endpoint address
  1745. * @retval HAL status
  1746. */
  1747. HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1748. {
  1749. PCD_EPTypeDef *ep;
  1750. if (((uint32_t)ep_addr & EP_ADDR_MSK) > hpcd->Init.dev_endpoints)
  1751. {
  1752. return HAL_ERROR;
  1753. }
  1754. if ((0x80U & ep_addr) == 0x80U)
  1755. {
  1756. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1757. ep->is_in = 1U;
  1758. }
  1759. else
  1760. {
  1761. ep = &hpcd->OUT_ep[ep_addr];
  1762. ep->is_in = 0U;
  1763. }
  1764. ep->is_stall = 1U;
  1765. ep->num = ep_addr & EP_ADDR_MSK;
  1766. __HAL_LOCK(hpcd);
  1767. (void)USB_EPSetStall(hpcd->Instance, ep);
  1768. if ((ep_addr & EP_ADDR_MSK) == 0U)
  1769. {
  1770. (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t *)hpcd->Setup);
  1771. }
  1772. __HAL_UNLOCK(hpcd);
  1773. return HAL_OK;
  1774. }
  1775. /**
  1776. * @brief Clear a STALL condition over in an endpoint
  1777. * @param hpcd PCD handle
  1778. * @param ep_addr endpoint address
  1779. * @retval HAL status
  1780. */
  1781. HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1782. {
  1783. PCD_EPTypeDef *ep;
  1784. if (((uint32_t)ep_addr & 0x0FU) > hpcd->Init.dev_endpoints)
  1785. {
  1786. return HAL_ERROR;
  1787. }
  1788. if ((0x80U & ep_addr) == 0x80U)
  1789. {
  1790. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1791. ep->is_in = 1U;
  1792. }
  1793. else
  1794. {
  1795. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1796. ep->is_in = 0U;
  1797. }
  1798. ep->is_stall = 0U;
  1799. ep->num = ep_addr & EP_ADDR_MSK;
  1800. __HAL_LOCK(hpcd);
  1801. (void)USB_EPClearStall(hpcd->Instance, ep);
  1802. __HAL_UNLOCK(hpcd);
  1803. return HAL_OK;
  1804. }
  1805. /**
  1806. * @brief Abort an USB EP transaction.
  1807. * @param hpcd PCD handle
  1808. * @param ep_addr endpoint address
  1809. * @retval HAL status
  1810. */
  1811. HAL_StatusTypeDef HAL_PCD_EP_Abort(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1812. {
  1813. HAL_StatusTypeDef ret;
  1814. PCD_EPTypeDef *ep;
  1815. if ((0x80U & ep_addr) == 0x80U)
  1816. {
  1817. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1818. }
  1819. else
  1820. {
  1821. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1822. }
  1823. /* Stop Xfer */
  1824. ret = USB_EPStopXfer(hpcd->Instance, ep);
  1825. return ret;
  1826. }
  1827. /**
  1828. * @brief Flush an endpoint
  1829. * @param hpcd PCD handle
  1830. * @param ep_addr endpoint address
  1831. * @retval HAL status
  1832. */
  1833. HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1834. {
  1835. __HAL_LOCK(hpcd);
  1836. if ((ep_addr & 0x80U) == 0x80U)
  1837. {
  1838. (void)USB_FlushTxFifo(hpcd->Instance, (uint32_t)ep_addr & EP_ADDR_MSK);
  1839. }
  1840. else
  1841. {
  1842. (void)USB_FlushRxFifo(hpcd->Instance);
  1843. }
  1844. __HAL_UNLOCK(hpcd);
  1845. return HAL_OK;
  1846. }
  1847. /**
  1848. * @brief Activate remote wakeup signalling
  1849. * @param hpcd PCD handle
  1850. * @retval HAL status
  1851. */
  1852. HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1853. {
  1854. return (USB_ActivateRemoteWakeup(hpcd->Instance));
  1855. }
  1856. /**
  1857. * @brief De-activate remote wakeup signalling.
  1858. * @param hpcd PCD handle
  1859. * @retval HAL status
  1860. */
  1861. HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1862. {
  1863. return (USB_DeActivateRemoteWakeup(hpcd->Instance));
  1864. }
  1865. /**
  1866. * @}
  1867. */
  1868. /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
  1869. * @brief Peripheral State functions
  1870. *
  1871. @verbatim
  1872. ===============================================================================
  1873. ##### Peripheral State functions #####
  1874. ===============================================================================
  1875. [..]
  1876. This subsection permits to get in run-time the status of the peripheral
  1877. and the data flow.
  1878. @endverbatim
  1879. * @{
  1880. */
  1881. /**
  1882. * @brief Return the PCD handle state.
  1883. * @param hpcd PCD handle
  1884. * @retval HAL state
  1885. */
  1886. PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
  1887. {
  1888. return hpcd->State;
  1889. }
  1890. /**
  1891. * @}
  1892. */
  1893. /**
  1894. * @}
  1895. */
  1896. /* Private functions ---------------------------------------------------------*/
  1897. /** @addtogroup PCD_Private_Functions
  1898. * @{
  1899. */
  1900. #if defined (USB_OTG_FS)
  1901. /**
  1902. * @brief Check FIFO for the next packet to be loaded.
  1903. * @param hpcd PCD handle
  1904. * @param epnum endpoint number
  1905. * @retval HAL status
  1906. */
  1907. static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1908. {
  1909. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1910. uint32_t USBx_BASE = (uint32_t)USBx;
  1911. USB_OTG_EPTypeDef *ep;
  1912. uint32_t len;
  1913. uint32_t len32b;
  1914. uint32_t fifoemptymsk;
  1915. ep = &hpcd->IN_ep[epnum];
  1916. if (ep->xfer_count > ep->xfer_len)
  1917. {
  1918. return HAL_ERROR;
  1919. }
  1920. len = ep->xfer_len - ep->xfer_count;
  1921. if (len > ep->maxpacket)
  1922. {
  1923. len = ep->maxpacket;
  1924. }
  1925. len32b = (len + 3U) / 4U;
  1926. while (((USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) >= len32b) &&
  1927. (ep->xfer_count < ep->xfer_len) && (ep->xfer_len != 0U))
  1928. {
  1929. /* Write the FIFO */
  1930. len = ep->xfer_len - ep->xfer_count;
  1931. if (len > ep->maxpacket)
  1932. {
  1933. len = ep->maxpacket;
  1934. }
  1935. len32b = (len + 3U) / 4U;
  1936. (void)USB_WritePacket(USBx, ep->xfer_buff, (uint8_t)epnum, (uint16_t)len);
  1937. ep->xfer_buff += len;
  1938. ep->xfer_count += len;
  1939. }
  1940. if (ep->xfer_len <= ep->xfer_count)
  1941. {
  1942. fifoemptymsk = (uint32_t)(0x1UL << (epnum & EP_ADDR_MSK));
  1943. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  1944. }
  1945. return HAL_OK;
  1946. }
  1947. /**
  1948. * @brief process EP OUT transfer complete interrupt.
  1949. * @param hpcd PCD handle
  1950. * @param epnum endpoint number
  1951. * @retval HAL status
  1952. */
  1953. static HAL_StatusTypeDef PCD_EP_OutXfrComplete_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1954. {
  1955. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1956. uint32_t USBx_BASE = (uint32_t)USBx;
  1957. uint32_t gSNPSiD = *(__IO uint32_t *)(&USBx->CID + 0x1U);
  1958. uint32_t DoepintReg = USBx_OUTEP(epnum)->DOEPINT;
  1959. if (gSNPSiD == USB_OTG_CORE_ID_310A)
  1960. {
  1961. /* StupPktRcvd = 1 this is a setup packet */
  1962. if ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX)
  1963. {
  1964. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
  1965. }
  1966. else
  1967. {
  1968. if ((DoepintReg & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
  1969. {
  1970. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
  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. else
  1980. {
  1981. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1982. hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
  1983. #else
  1984. HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
  1985. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1986. }
  1987. return HAL_OK;
  1988. }
  1989. /**
  1990. * @brief process EP OUT setup packet received interrupt.
  1991. * @param hpcd PCD handle
  1992. * @param epnum endpoint number
  1993. * @retval HAL status
  1994. */
  1995. static HAL_StatusTypeDef PCD_EP_OutSetupPacket_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1996. {
  1997. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1998. uint32_t USBx_BASE = (uint32_t)USBx;
  1999. uint32_t gSNPSiD = *(__IO uint32_t *)(&USBx->CID + 0x1U);
  2000. uint32_t DoepintReg = USBx_OUTEP(epnum)->DOEPINT;
  2001. if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
  2002. ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX))
  2003. {
  2004. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
  2005. }
  2006. /* Inform the upper layer that a setup packet is available */
  2007. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  2008. hpcd->SetupStageCallback(hpcd);
  2009. #else
  2010. HAL_PCD_SetupStageCallback(hpcd);
  2011. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  2012. return HAL_OK;
  2013. }
  2014. #endif /* defined (USB_OTG_FS) */
  2015. #if defined (USB)
  2016. /**
  2017. * @brief This function handles PCD Endpoint interrupt request.
  2018. * @param hpcd PCD handle
  2019. * @retval HAL status
  2020. */
  2021. static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
  2022. {
  2023. PCD_EPTypeDef *ep;
  2024. uint16_t count;
  2025. uint16_t wIstr;
  2026. uint16_t wEPVal;
  2027. uint16_t TxPctSize;
  2028. uint8_t epindex;
  2029. #if (USE_USB_DOUBLE_BUFFER != 1U)
  2030. count = 0U;
  2031. #endif /* USE_USB_DOUBLE_BUFFER */
  2032. /* stay in loop while pending interrupts */
  2033. while ((hpcd->Instance->ISTR & USB_ISTR_CTR) != 0U)
  2034. {
  2035. wIstr = hpcd->Instance->ISTR;
  2036. /* extract highest priority endpoint number */
  2037. epindex = (uint8_t)(wIstr & USB_ISTR_EP_ID);
  2038. if (epindex == 0U)
  2039. {
  2040. /* Decode and service control endpoint interrupt */
  2041. /* DIR bit = origin of the interrupt */
  2042. if ((wIstr & USB_ISTR_DIR) == 0U)
  2043. {
  2044. /* DIR = 0 */
  2045. /* DIR = 0 => IN int */
  2046. /* DIR = 0 implies that (EP_CTR_TX = 1) always */
  2047. PCD_CLEAR_TX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  2048. ep = &hpcd->IN_ep[0];
  2049. ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
  2050. ep->xfer_buff += ep->xfer_count;
  2051. /* TX COMPLETE */
  2052. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  2053. hpcd->DataInStageCallback(hpcd, 0U);
  2054. #else
  2055. HAL_PCD_DataInStageCallback(hpcd, 0U);
  2056. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  2057. if ((hpcd->USB_Address > 0U) && (ep->xfer_len == 0U))
  2058. {
  2059. hpcd->Instance->DADDR = ((uint16_t)hpcd->USB_Address | USB_DADDR_EF);
  2060. hpcd->USB_Address = 0U;
  2061. }
  2062. }
  2063. else
  2064. {
  2065. /* DIR = 1 */
  2066. /* DIR = 1 & CTR_RX => SETUP or OUT int */
  2067. /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
  2068. ep = &hpcd->OUT_ep[0];
  2069. wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
  2070. if ((wEPVal & USB_EP_SETUP) != 0U)
  2071. {
  2072. /* Get SETUP Packet */
  2073. ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  2074. USB_ReadPMA(hpcd->Instance, (uint8_t *)hpcd->Setup,
  2075. ep->pmaadress, (uint16_t)ep->xfer_count);
  2076. /* SETUP bit kept frozen while CTR_RX = 1 */
  2077. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  2078. /* Process SETUP Packet*/
  2079. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  2080. hpcd->SetupStageCallback(hpcd);
  2081. #else
  2082. HAL_PCD_SetupStageCallback(hpcd);
  2083. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  2084. }
  2085. else if ((wEPVal & USB_EP_CTR_RX) != 0U)
  2086. {
  2087. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  2088. /* Get Control Data OUT Packet */
  2089. ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  2090. if ((ep->xfer_count != 0U) && (ep->xfer_buff != 0U))
  2091. {
  2092. USB_ReadPMA(hpcd->Instance, ep->xfer_buff,
  2093. ep->pmaadress, (uint16_t)ep->xfer_count);
  2094. ep->xfer_buff += ep->xfer_count;
  2095. /* Process Control Data OUT Packet */
  2096. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  2097. hpcd->DataOutStageCallback(hpcd, 0U);
  2098. #else
  2099. HAL_PCD_DataOutStageCallback(hpcd, 0U);
  2100. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  2101. }
  2102. wEPVal = (uint16_t)PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
  2103. if (((wEPVal & USB_EP_SETUP) == 0U) && ((wEPVal & USB_EP_RX_STRX) != USB_EP_RX_VALID))
  2104. {
  2105. PCD_SET_EP_RX_CNT(hpcd->Instance, PCD_ENDP0, ep->maxpacket);
  2106. PCD_SET_EP_RX_STATUS(hpcd->Instance, PCD_ENDP0, USB_EP_RX_VALID);
  2107. }
  2108. }
  2109. }
  2110. }
  2111. else
  2112. {
  2113. /* Decode and service non control endpoints interrupt */
  2114. /* process related endpoint register */
  2115. wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, epindex);
  2116. if ((wEPVal & USB_EP_CTR_RX) != 0U)
  2117. {
  2118. /* clear int flag */
  2119. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, epindex);
  2120. ep = &hpcd->OUT_ep[epindex];
  2121. /* OUT Single Buffering */
  2122. if (ep->doublebuffer == 0U)
  2123. {
  2124. count = (uint16_t)PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  2125. if (count != 0U)
  2126. {
  2127. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, count);
  2128. }
  2129. }
  2130. #if (USE_USB_DOUBLE_BUFFER == 1U)
  2131. else
  2132. {
  2133. /* manage double buffer bulk out */
  2134. if (ep->type == EP_TYPE_BULK)
  2135. {
  2136. count = HAL_PCD_EP_DB_Receive(hpcd, ep, wEPVal);
  2137. }
  2138. else /* manage double buffer iso out */
  2139. {
  2140. /* free EP OUT Buffer */
  2141. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
  2142. if ((PCD_GET_ENDPOINT(hpcd->Instance, ep->num) & USB_EP_DTOG_RX) != 0U)
  2143. {
  2144. /* read from endpoint BUF0Addr buffer */
  2145. count = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  2146. if (count != 0U)
  2147. {
  2148. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
  2149. }
  2150. }
  2151. else
  2152. {
  2153. /* read from endpoint BUF1Addr buffer */
  2154. count = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
  2155. if (count != 0U)
  2156. {
  2157. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
  2158. }
  2159. }
  2160. }
  2161. }
  2162. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  2163. /* multi-packet on the NON control OUT endpoint */
  2164. ep->xfer_count += count;
  2165. ep->xfer_buff += count;
  2166. if ((ep->xfer_len == 0U) || (count < ep->maxpacket))
  2167. {
  2168. /* RX COMPLETE */
  2169. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  2170. hpcd->DataOutStageCallback(hpcd, ep->num);
  2171. #else
  2172. HAL_PCD_DataOutStageCallback(hpcd, ep->num);
  2173. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  2174. }
  2175. else
  2176. {
  2177. (void) USB_EPStartXfer(hpcd->Instance, ep);
  2178. }
  2179. }
  2180. if ((wEPVal & USB_EP_CTR_TX) != 0U)
  2181. {
  2182. ep = &hpcd->IN_ep[epindex];
  2183. /* clear int flag */
  2184. PCD_CLEAR_TX_EP_CTR(hpcd->Instance, epindex);
  2185. if (ep->type == EP_TYPE_ISOC)
  2186. {
  2187. ep->xfer_len = 0U;
  2188. #if (USE_USB_DOUBLE_BUFFER == 1U)
  2189. if (ep->doublebuffer != 0U)
  2190. {
  2191. if ((wEPVal & USB_EP_DTOG_TX) != 0U)
  2192. {
  2193. PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  2194. }
  2195. else
  2196. {
  2197. PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  2198. }
  2199. }
  2200. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  2201. /* TX COMPLETE */
  2202. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  2203. hpcd->DataInStageCallback(hpcd, ep->num);
  2204. #else
  2205. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  2206. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  2207. }
  2208. else
  2209. {
  2210. /* Manage Single Buffer Transaction */
  2211. if ((wEPVal & USB_EP_KIND) == 0U)
  2212. {
  2213. /* multi-packet on the NON control IN endpoint */
  2214. TxPctSize = (uint16_t)PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
  2215. if (ep->xfer_len > TxPctSize)
  2216. {
  2217. ep->xfer_len -= TxPctSize;
  2218. }
  2219. else
  2220. {
  2221. ep->xfer_len = 0U;
  2222. }
  2223. /* Zero Length Packet? */
  2224. if (ep->xfer_len == 0U)
  2225. {
  2226. /* TX COMPLETE */
  2227. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  2228. hpcd->DataInStageCallback(hpcd, ep->num);
  2229. #else
  2230. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  2231. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  2232. }
  2233. else
  2234. {
  2235. /* Transfer is not yet Done */
  2236. ep->xfer_buff += TxPctSize;
  2237. ep->xfer_count += TxPctSize;
  2238. (void)USB_EPStartXfer(hpcd->Instance, ep);
  2239. }
  2240. }
  2241. #if (USE_USB_DOUBLE_BUFFER == 1U)
  2242. /* Double Buffer bulk IN (bulk transfer Len > Ep_Mps) */
  2243. else
  2244. {
  2245. (void)HAL_PCD_EP_DB_Transmit(hpcd, ep, wEPVal);
  2246. }
  2247. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  2248. }
  2249. }
  2250. }
  2251. }
  2252. return HAL_OK;
  2253. }
  2254. #if (USE_USB_DOUBLE_BUFFER == 1U)
  2255. /**
  2256. * @brief Manage double buffer bulk out transaction from ISR
  2257. * @param hpcd PCD handle
  2258. * @param ep current endpoint handle
  2259. * @param wEPVal Last snapshot of EPRx register value taken in ISR
  2260. * @retval HAL status
  2261. */
  2262. static uint16_t HAL_PCD_EP_DB_Receive(PCD_HandleTypeDef *hpcd,
  2263. PCD_EPTypeDef *ep, uint16_t wEPVal)
  2264. {
  2265. uint16_t count;
  2266. /* Manage Buffer0 OUT */
  2267. if ((wEPVal & USB_EP_DTOG_RX) != 0U)
  2268. {
  2269. /* Get count of received Data on buffer0 */
  2270. count = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  2271. if (ep->xfer_len >= count)
  2272. {
  2273. ep->xfer_len -= count;
  2274. }
  2275. else
  2276. {
  2277. ep->xfer_len = 0U;
  2278. }
  2279. if (ep->xfer_len == 0U)
  2280. {
  2281. /* set NAK to OUT endpoint since double buffer is enabled */
  2282. PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_NAK);
  2283. }
  2284. /* Check if Buffer1 is in blocked state which requires to toggle */
  2285. if ((wEPVal & USB_EP_DTOG_TX) != 0U)
  2286. {
  2287. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
  2288. }
  2289. if (count != 0U)
  2290. {
  2291. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
  2292. }
  2293. }
  2294. /* Manage Buffer 1 DTOG_RX=0 */
  2295. else
  2296. {
  2297. /* Get count of received data */
  2298. count = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
  2299. if (ep->xfer_len >= count)
  2300. {
  2301. ep->xfer_len -= count;
  2302. }
  2303. else
  2304. {
  2305. ep->xfer_len = 0U;
  2306. }
  2307. if (ep->xfer_len == 0U)
  2308. {
  2309. /* set NAK on the current endpoint */
  2310. PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_NAK);
  2311. }
  2312. /*Need to FreeUser Buffer*/
  2313. if ((wEPVal & USB_EP_DTOG_TX) == 0U)
  2314. {
  2315. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
  2316. }
  2317. if (count != 0U)
  2318. {
  2319. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
  2320. }
  2321. }
  2322. return count;
  2323. }
  2324. /**
  2325. * @brief Manage double buffer bulk IN transaction from ISR
  2326. * @param hpcd PCD handle
  2327. * @param ep current endpoint handle
  2328. * @param wEPVal Last snapshot of EPRx register value taken in ISR
  2329. * @retval HAL status
  2330. */
  2331. static HAL_StatusTypeDef HAL_PCD_EP_DB_Transmit(PCD_HandleTypeDef *hpcd,
  2332. PCD_EPTypeDef *ep, uint16_t wEPVal)
  2333. {
  2334. uint32_t len;
  2335. uint16_t TxPctSize;
  2336. /* Data Buffer0 ACK received */
  2337. if ((wEPVal & USB_EP_DTOG_TX) != 0U)
  2338. {
  2339. /* multi-packet on the NON control IN endpoint */
  2340. TxPctSize = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  2341. if (ep->xfer_len > TxPctSize)
  2342. {
  2343. ep->xfer_len -= TxPctSize;
  2344. }
  2345. else
  2346. {
  2347. ep->xfer_len = 0U;
  2348. }
  2349. /* Transfer is completed */
  2350. if (ep->xfer_len == 0U)
  2351. {
  2352. PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  2353. PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  2354. /* TX COMPLETE */
  2355. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  2356. hpcd->DataInStageCallback(hpcd, ep->num);
  2357. #else
  2358. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  2359. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  2360. if ((wEPVal & USB_EP_DTOG_RX) != 0U)
  2361. {
  2362. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
  2363. }
  2364. }
  2365. else /* Transfer is not yet Done */
  2366. {
  2367. /* need to Free USB Buff */
  2368. if ((wEPVal & USB_EP_DTOG_RX) != 0U)
  2369. {
  2370. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
  2371. }
  2372. /* Still there is data to Fill in the next Buffer */
  2373. if (ep->xfer_fill_db == 1U)
  2374. {
  2375. ep->xfer_buff += TxPctSize;
  2376. ep->xfer_count += TxPctSize;
  2377. /* Calculate the len of the new buffer to fill */
  2378. if (ep->xfer_len_db >= ep->maxpacket)
  2379. {
  2380. len = ep->maxpacket;
  2381. ep->xfer_len_db -= len;
  2382. }
  2383. else if (ep->xfer_len_db == 0U)
  2384. {
  2385. len = TxPctSize;
  2386. ep->xfer_fill_db = 0U;
  2387. }
  2388. else
  2389. {
  2390. ep->xfer_fill_db = 0U;
  2391. len = ep->xfer_len_db;
  2392. ep->xfer_len_db = 0U;
  2393. }
  2394. /* Write remaining Data to Buffer */
  2395. /* Set the Double buffer counter for pma buffer1 */
  2396. PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, len);
  2397. /* Copy user buffer to USB PMA */
  2398. USB_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, (uint16_t)len);
  2399. }
  2400. }
  2401. }
  2402. else /* Data Buffer1 ACK received */
  2403. {
  2404. /* multi-packet on the NON control IN endpoint */
  2405. TxPctSize = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
  2406. if (ep->xfer_len >= TxPctSize)
  2407. {
  2408. ep->xfer_len -= TxPctSize;
  2409. }
  2410. else
  2411. {
  2412. ep->xfer_len = 0U;
  2413. }
  2414. /* Transfer is completed */
  2415. if (ep->xfer_len == 0U)
  2416. {
  2417. PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  2418. PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  2419. /* TX COMPLETE */
  2420. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  2421. hpcd->DataInStageCallback(hpcd, ep->num);
  2422. #else
  2423. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  2424. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  2425. /* need to Free USB Buff */
  2426. if ((wEPVal & USB_EP_DTOG_RX) == 0U)
  2427. {
  2428. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
  2429. }
  2430. }
  2431. else /* Transfer is not yet Done */
  2432. {
  2433. /* need to Free USB Buff */
  2434. if ((wEPVal & USB_EP_DTOG_RX) == 0U)
  2435. {
  2436. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
  2437. }
  2438. /* Still there is data to Fill in the next Buffer */
  2439. if (ep->xfer_fill_db == 1U)
  2440. {
  2441. ep->xfer_buff += TxPctSize;
  2442. ep->xfer_count += TxPctSize;
  2443. /* Calculate the len of the new buffer to fill */
  2444. if (ep->xfer_len_db >= ep->maxpacket)
  2445. {
  2446. len = ep->maxpacket;
  2447. ep->xfer_len_db -= len;
  2448. }
  2449. else if (ep->xfer_len_db == 0U)
  2450. {
  2451. len = TxPctSize;
  2452. ep->xfer_fill_db = 0U;
  2453. }
  2454. else
  2455. {
  2456. len = ep->xfer_len_db;
  2457. ep->xfer_len_db = 0U;
  2458. ep->xfer_fill_db = 0;
  2459. }
  2460. /* Set the Double buffer counter for pmabuffer1 */
  2461. PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, len);
  2462. /* Copy the user buffer to USB PMA */
  2463. USB_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, (uint16_t)len);
  2464. }
  2465. }
  2466. }
  2467. /*enable endpoint IN*/
  2468. PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_VALID);
  2469. return HAL_OK;
  2470. }
  2471. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  2472. #endif /* defined (USB) */
  2473. /**
  2474. * @}
  2475. */
  2476. #endif /* defined (USB) || defined (USB_OTG_FS) */
  2477. #endif /* HAL_PCD_MODULE_ENABLED */
  2478. /**
  2479. * @}
  2480. */
  2481. /**
  2482. * @}
  2483. */