stm32l4xx_hal_pcd.c 76 KB

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