stm32g4xx_hal_pcd.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_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) 2019 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. (##) Initialize the related GPIO clocks
  38. (##) Configure PCD pin-out
  39. (##) Configure PCD NVIC interrupt
  40. (#)Associate the Upper USB device stack to the HAL PCD Driver:
  41. (##) hpcd.pData = pdev;
  42. (#)Enable PCD transmission and reception:
  43. (##) HAL_PCD_Start();
  44. @endverbatim
  45. ******************************************************************************
  46. */
  47. /* Includes ------------------------------------------------------------------*/
  48. #include "stm32g4xx_hal.h"
  49. /** @addtogroup STM32G4xx_HAL_Driver
  50. * @{
  51. */
  52. /** @defgroup PCD PCD
  53. * @brief PCD HAL module driver
  54. * @{
  55. */
  56. #ifdef HAL_PCD_MODULE_ENABLED
  57. #if defined (USB)
  58. /* Private types -------------------------------------------------------------*/
  59. /* Private variables ---------------------------------------------------------*/
  60. /* Private constants ---------------------------------------------------------*/
  61. /* Private macros ------------------------------------------------------------*/
  62. /** @defgroup PCD_Private_Macros PCD Private Macros
  63. * @{
  64. */
  65. #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
  66. #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
  67. /**
  68. * @}
  69. */
  70. /* Private functions prototypes ----------------------------------------------*/
  71. /** @defgroup PCD_Private_Functions PCD Private Functions
  72. * @{
  73. */
  74. static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd);
  75. #if (USE_USB_DOUBLE_BUFFER == 1U)
  76. static HAL_StatusTypeDef HAL_PCD_EP_DB_Transmit(PCD_HandleTypeDef *hpcd, PCD_EPTypeDef *ep, uint16_t wEPVal);
  77. static uint16_t HAL_PCD_EP_DB_Receive(PCD_HandleTypeDef *hpcd, PCD_EPTypeDef *ep, uint16_t wEPVal);
  78. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  79. /**
  80. * @}
  81. */
  82. /* Exported functions --------------------------------------------------------*/
  83. /** @defgroup PCD_Exported_Functions PCD Exported Functions
  84. * @{
  85. */
  86. /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
  87. * @brief Initialization and Configuration functions
  88. *
  89. @verbatim
  90. ===============================================================================
  91. ##### Initialization and de-initialization functions #####
  92. ===============================================================================
  93. [..] This section provides functions allowing to:
  94. @endverbatim
  95. * @{
  96. */
  97. /**
  98. * @brief Initializes the PCD according to the specified
  99. * parameters in the PCD_InitTypeDef and initialize the associated handle.
  100. * @param hpcd PCD handle
  101. * @retval HAL status
  102. */
  103. HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
  104. {
  105. uint8_t i;
  106. /* Check the PCD handle allocation */
  107. if (hpcd == NULL)
  108. {
  109. return HAL_ERROR;
  110. }
  111. /* Check the parameters */
  112. assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
  113. if (hpcd->State == HAL_PCD_STATE_RESET)
  114. {
  115. /* Allocate lock resource and initialize it */
  116. hpcd->Lock = HAL_UNLOCKED;
  117. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  118. hpcd->SOFCallback = HAL_PCD_SOFCallback;
  119. hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
  120. hpcd->ResetCallback = HAL_PCD_ResetCallback;
  121. hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
  122. hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
  123. hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
  124. hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
  125. hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback;
  126. hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback;
  127. hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback;
  128. hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback;
  129. hpcd->LPMCallback = HAL_PCDEx_LPM_Callback;
  130. hpcd->BCDCallback = HAL_PCDEx_BCD_Callback;
  131. if (hpcd->MspInitCallback == NULL)
  132. {
  133. hpcd->MspInitCallback = HAL_PCD_MspInit;
  134. }
  135. /* Init the low level hardware */
  136. hpcd->MspInitCallback(hpcd);
  137. #else
  138. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  139. HAL_PCD_MspInit(hpcd);
  140. #endif /* (USE_HAL_PCD_REGISTER_CALLBACKS) */
  141. }
  142. hpcd->State = HAL_PCD_STATE_BUSY;
  143. /* Disable the Interrupts */
  144. __HAL_PCD_DISABLE(hpcd);
  145. /* Init endpoints structures */
  146. for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
  147. {
  148. /* Init ep structure */
  149. hpcd->IN_ep[i].is_in = 1U;
  150. hpcd->IN_ep[i].num = i;
  151. /* Control until ep is activated */
  152. hpcd->IN_ep[i].type = EP_TYPE_CTRL;
  153. hpcd->IN_ep[i].maxpacket = 0U;
  154. hpcd->IN_ep[i].xfer_buff = 0U;
  155. hpcd->IN_ep[i].xfer_len = 0U;
  156. }
  157. for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
  158. {
  159. hpcd->OUT_ep[i].is_in = 0U;
  160. hpcd->OUT_ep[i].num = i;
  161. /* Control until ep is activated */
  162. hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
  163. hpcd->OUT_ep[i].maxpacket = 0U;
  164. hpcd->OUT_ep[i].xfer_buff = 0U;
  165. hpcd->OUT_ep[i].xfer_len = 0U;
  166. }
  167. /* Init Device */
  168. (void)USB_DevInit(hpcd->Instance, hpcd->Init);
  169. hpcd->USB_Address = 0U;
  170. hpcd->State = HAL_PCD_STATE_READY;
  171. /* Activate LPM */
  172. if (hpcd->Init.lpm_enable == 1U)
  173. {
  174. (void)HAL_PCDEx_ActivateLPM(hpcd);
  175. }
  176. return HAL_OK;
  177. }
  178. /**
  179. * @brief DeInitializes the PCD peripheral.
  180. * @param hpcd PCD handle
  181. * @retval HAL status
  182. */
  183. HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
  184. {
  185. /* Check the PCD handle allocation */
  186. if (hpcd == NULL)
  187. {
  188. return HAL_ERROR;
  189. }
  190. hpcd->State = HAL_PCD_STATE_BUSY;
  191. /* Stop Device */
  192. if (USB_StopDevice(hpcd->Instance) != HAL_OK)
  193. {
  194. return HAL_ERROR;
  195. }
  196. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  197. if (hpcd->MspDeInitCallback == NULL)
  198. {
  199. hpcd->MspDeInitCallback = HAL_PCD_MspDeInit; /* Legacy weak MspDeInit */
  200. }
  201. /* DeInit the low level hardware */
  202. hpcd->MspDeInitCallback(hpcd);
  203. #else
  204. /* DeInit the low level hardware: CLOCK, NVIC.*/
  205. HAL_PCD_MspDeInit(hpcd);
  206. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  207. hpcd->State = HAL_PCD_STATE_RESET;
  208. return HAL_OK;
  209. }
  210. /**
  211. * @brief Initializes the PCD MSP.
  212. * @param hpcd PCD handle
  213. * @retval None
  214. */
  215. __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
  216. {
  217. /* Prevent unused argument(s) compilation warning */
  218. UNUSED(hpcd);
  219. /* NOTE : This function should not be modified, when the callback is needed,
  220. the HAL_PCD_MspInit could be implemented in the user file
  221. */
  222. }
  223. /**
  224. * @brief DeInitializes PCD MSP.
  225. * @param hpcd PCD handle
  226. * @retval None
  227. */
  228. __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
  229. {
  230. /* Prevent unused argument(s) compilation warning */
  231. UNUSED(hpcd);
  232. /* NOTE : This function should not be modified, when the callback is needed,
  233. the HAL_PCD_MspDeInit could be implemented in the user file
  234. */
  235. }
  236. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  237. /**
  238. * @brief Register a User USB PCD Callback
  239. * To be used instead of the weak predefined callback
  240. * @param hpcd USB PCD handle
  241. * @param CallbackID ID of the callback to be registered
  242. * This parameter can be one of the following values:
  243. * @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
  244. * @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
  245. * @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
  246. * @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
  247. * @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
  248. * @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
  249. * @arg @ref HAL_PCD_DISCONNECT_CB_ID USB PCD Disconnect callback ID
  250. * @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
  251. * @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
  252. * @param pCallback pointer to the Callback function
  253. * @retval HAL status
  254. */
  255. HAL_StatusTypeDef HAL_PCD_RegisterCallback(PCD_HandleTypeDef *hpcd,
  256. HAL_PCD_CallbackIDTypeDef CallbackID,
  257. pPCD_CallbackTypeDef pCallback)
  258. {
  259. HAL_StatusTypeDef status = HAL_OK;
  260. if (pCallback == NULL)
  261. {
  262. /* Update the error code */
  263. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  264. return HAL_ERROR;
  265. }
  266. /* Process locked */
  267. __HAL_LOCK(hpcd);
  268. if (hpcd->State == HAL_PCD_STATE_READY)
  269. {
  270. switch (CallbackID)
  271. {
  272. case HAL_PCD_SOF_CB_ID :
  273. hpcd->SOFCallback = pCallback;
  274. break;
  275. case HAL_PCD_SETUPSTAGE_CB_ID :
  276. hpcd->SetupStageCallback = pCallback;
  277. break;
  278. case HAL_PCD_RESET_CB_ID :
  279. hpcd->ResetCallback = pCallback;
  280. break;
  281. case HAL_PCD_SUSPEND_CB_ID :
  282. hpcd->SuspendCallback = pCallback;
  283. break;
  284. case HAL_PCD_RESUME_CB_ID :
  285. hpcd->ResumeCallback = pCallback;
  286. break;
  287. case HAL_PCD_CONNECT_CB_ID :
  288. hpcd->ConnectCallback = pCallback;
  289. break;
  290. case HAL_PCD_DISCONNECT_CB_ID :
  291. hpcd->DisconnectCallback = pCallback;
  292. break;
  293. case HAL_PCD_MSPINIT_CB_ID :
  294. hpcd->MspInitCallback = pCallback;
  295. break;
  296. case HAL_PCD_MSPDEINIT_CB_ID :
  297. hpcd->MspDeInitCallback = pCallback;
  298. break;
  299. default :
  300. /* Update the error code */
  301. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  302. /* Return error status */
  303. status = HAL_ERROR;
  304. break;
  305. }
  306. }
  307. else if (hpcd->State == HAL_PCD_STATE_RESET)
  308. {
  309. switch (CallbackID)
  310. {
  311. case HAL_PCD_MSPINIT_CB_ID :
  312. hpcd->MspInitCallback = pCallback;
  313. break;
  314. case HAL_PCD_MSPDEINIT_CB_ID :
  315. hpcd->MspDeInitCallback = pCallback;
  316. break;
  317. default :
  318. /* Update the error code */
  319. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  320. /* Return error status */
  321. status = HAL_ERROR;
  322. break;
  323. }
  324. }
  325. else
  326. {
  327. /* Update the error code */
  328. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  329. /* Return error status */
  330. status = HAL_ERROR;
  331. }
  332. /* Release Lock */
  333. __HAL_UNLOCK(hpcd);
  334. return status;
  335. }
  336. /**
  337. * @brief Unregister an USB PCD Callback
  338. * USB PCD callback is redirected to the weak predefined callback
  339. * @param hpcd USB PCD handle
  340. * @param CallbackID ID of the callback to be unregistered
  341. * This parameter can be one of the following values:
  342. * @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
  343. * @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
  344. * @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
  345. * @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
  346. * @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
  347. * @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
  348. * @arg @ref HAL_PCD_DISCONNECT_CB_ID USB PCD Disconnect callback ID
  349. * @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
  350. * @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
  351. * @retval HAL status
  352. */
  353. HAL_StatusTypeDef HAL_PCD_UnRegisterCallback(PCD_HandleTypeDef *hpcd, HAL_PCD_CallbackIDTypeDef CallbackID)
  354. {
  355. HAL_StatusTypeDef status = HAL_OK;
  356. /* Process locked */
  357. __HAL_LOCK(hpcd);
  358. /* Setup Legacy weak Callbacks */
  359. if (hpcd->State == HAL_PCD_STATE_READY)
  360. {
  361. switch (CallbackID)
  362. {
  363. case HAL_PCD_SOF_CB_ID :
  364. hpcd->SOFCallback = HAL_PCD_SOFCallback;
  365. break;
  366. case HAL_PCD_SETUPSTAGE_CB_ID :
  367. hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
  368. break;
  369. case HAL_PCD_RESET_CB_ID :
  370. hpcd->ResetCallback = HAL_PCD_ResetCallback;
  371. break;
  372. case HAL_PCD_SUSPEND_CB_ID :
  373. hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
  374. break;
  375. case HAL_PCD_RESUME_CB_ID :
  376. hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
  377. break;
  378. case HAL_PCD_CONNECT_CB_ID :
  379. hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
  380. break;
  381. case HAL_PCD_DISCONNECT_CB_ID :
  382. hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
  383. break;
  384. case HAL_PCD_MSPINIT_CB_ID :
  385. hpcd->MspInitCallback = HAL_PCD_MspInit;
  386. break;
  387. case HAL_PCD_MSPDEINIT_CB_ID :
  388. hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
  389. break;
  390. default :
  391. /* Update the error code */
  392. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  393. /* Return error status */
  394. status = HAL_ERROR;
  395. break;
  396. }
  397. }
  398. else if (hpcd->State == HAL_PCD_STATE_RESET)
  399. {
  400. switch (CallbackID)
  401. {
  402. case HAL_PCD_MSPINIT_CB_ID :
  403. hpcd->MspInitCallback = HAL_PCD_MspInit;
  404. break;
  405. case HAL_PCD_MSPDEINIT_CB_ID :
  406. hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
  407. break;
  408. default :
  409. /* Update the error code */
  410. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  411. /* Return error status */
  412. status = HAL_ERROR;
  413. break;
  414. }
  415. }
  416. else
  417. {
  418. /* Update the error code */
  419. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  420. /* Return error status */
  421. status = HAL_ERROR;
  422. }
  423. /* Release Lock */
  424. __HAL_UNLOCK(hpcd);
  425. return status;
  426. }
  427. /**
  428. * @brief Register USB PCD Data OUT Stage Callback
  429. * To be used instead of the weak HAL_PCD_DataOutStageCallback() predefined callback
  430. * @param hpcd PCD handle
  431. * @param pCallback pointer to the USB PCD Data OUT Stage Callback function
  432. * @retval HAL status
  433. */
  434. HAL_StatusTypeDef HAL_PCD_RegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd,
  435. pPCD_DataOutStageCallbackTypeDef pCallback)
  436. {
  437. HAL_StatusTypeDef status = HAL_OK;
  438. if (pCallback == NULL)
  439. {
  440. /* Update the error code */
  441. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  442. return HAL_ERROR;
  443. }
  444. /* Process locked */
  445. __HAL_LOCK(hpcd);
  446. if (hpcd->State == HAL_PCD_STATE_READY)
  447. {
  448. hpcd->DataOutStageCallback = pCallback;
  449. }
  450. else
  451. {
  452. /* Update the error code */
  453. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  454. /* Return error status */
  455. status = HAL_ERROR;
  456. }
  457. /* Release Lock */
  458. __HAL_UNLOCK(hpcd);
  459. return status;
  460. }
  461. /**
  462. * @brief Unregister the USB PCD Data OUT Stage Callback
  463. * USB PCD Data OUT Stage Callback is redirected to the weak HAL_PCD_DataOutStageCallback() predefined callback
  464. * @param hpcd PCD handle
  465. * @retval HAL status
  466. */
  467. HAL_StatusTypeDef HAL_PCD_UnRegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd)
  468. {
  469. HAL_StatusTypeDef status = HAL_OK;
  470. /* Process locked */
  471. __HAL_LOCK(hpcd);
  472. if (hpcd->State == HAL_PCD_STATE_READY)
  473. {
  474. hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback; /* Legacy weak DataOutStageCallback */
  475. }
  476. else
  477. {
  478. /* Update the error code */
  479. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  480. /* Return error status */
  481. status = HAL_ERROR;
  482. }
  483. /* Release Lock */
  484. __HAL_UNLOCK(hpcd);
  485. return status;
  486. }
  487. /**
  488. * @brief Register USB PCD Data IN Stage Callback
  489. * To be used instead of the weak HAL_PCD_DataInStageCallback() predefined callback
  490. * @param hpcd PCD handle
  491. * @param pCallback pointer to the USB PCD Data IN Stage Callback function
  492. * @retval HAL status
  493. */
  494. HAL_StatusTypeDef HAL_PCD_RegisterDataInStageCallback(PCD_HandleTypeDef *hpcd,
  495. pPCD_DataInStageCallbackTypeDef pCallback)
  496. {
  497. HAL_StatusTypeDef status = HAL_OK;
  498. if (pCallback == NULL)
  499. {
  500. /* Update the error code */
  501. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  502. return HAL_ERROR;
  503. }
  504. /* Process locked */
  505. __HAL_LOCK(hpcd);
  506. if (hpcd->State == HAL_PCD_STATE_READY)
  507. {
  508. hpcd->DataInStageCallback = pCallback;
  509. }
  510. else
  511. {
  512. /* Update the error code */
  513. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  514. /* Return error status */
  515. status = HAL_ERROR;
  516. }
  517. /* Release Lock */
  518. __HAL_UNLOCK(hpcd);
  519. return status;
  520. }
  521. /**
  522. * @brief Unregister the USB PCD Data IN Stage Callback
  523. * USB PCD Data OUT Stage Callback is redirected to the weak HAL_PCD_DataInStageCallback() predefined callback
  524. * @param hpcd PCD handle
  525. * @retval HAL status
  526. */
  527. HAL_StatusTypeDef HAL_PCD_UnRegisterDataInStageCallback(PCD_HandleTypeDef *hpcd)
  528. {
  529. HAL_StatusTypeDef status = HAL_OK;
  530. /* Process locked */
  531. __HAL_LOCK(hpcd);
  532. if (hpcd->State == HAL_PCD_STATE_READY)
  533. {
  534. hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback; /* Legacy weak DataInStageCallback */
  535. }
  536. else
  537. {
  538. /* Update the error code */
  539. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  540. /* Return error status */
  541. status = HAL_ERROR;
  542. }
  543. /* Release Lock */
  544. __HAL_UNLOCK(hpcd);
  545. return status;
  546. }
  547. /**
  548. * @brief Register USB PCD Iso OUT incomplete Callback
  549. * To be used instead of the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
  550. * @param hpcd PCD handle
  551. * @param pCallback pointer to the USB PCD Iso OUT incomplete Callback function
  552. * @retval HAL status
  553. */
  554. HAL_StatusTypeDef HAL_PCD_RegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd,
  555. pPCD_IsoOutIncpltCallbackTypeDef pCallback)
  556. {
  557. HAL_StatusTypeDef status = HAL_OK;
  558. if (pCallback == NULL)
  559. {
  560. /* Update the error code */
  561. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  562. return HAL_ERROR;
  563. }
  564. /* Process locked */
  565. __HAL_LOCK(hpcd);
  566. if (hpcd->State == HAL_PCD_STATE_READY)
  567. {
  568. hpcd->ISOOUTIncompleteCallback = pCallback;
  569. }
  570. else
  571. {
  572. /* Update the error code */
  573. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  574. /* Return error status */
  575. status = HAL_ERROR;
  576. }
  577. /* Release Lock */
  578. __HAL_UNLOCK(hpcd);
  579. return status;
  580. }
  581. /**
  582. * @brief Unregister the USB PCD Iso OUT incomplete Callback
  583. * USB PCD Iso OUT incomplete Callback is redirected
  584. * to the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
  585. * @param hpcd PCD handle
  586. * @retval HAL status
  587. */
  588. HAL_StatusTypeDef HAL_PCD_UnRegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd)
  589. {
  590. HAL_StatusTypeDef status = HAL_OK;
  591. /* Process locked */
  592. __HAL_LOCK(hpcd);
  593. if (hpcd->State == HAL_PCD_STATE_READY)
  594. {
  595. hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback; /* Legacy weak ISOOUTIncompleteCallback */
  596. }
  597. else
  598. {
  599. /* Update the error code */
  600. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  601. /* Return error status */
  602. status = HAL_ERROR;
  603. }
  604. /* Release Lock */
  605. __HAL_UNLOCK(hpcd);
  606. return status;
  607. }
  608. /**
  609. * @brief Register USB PCD Iso IN incomplete Callback
  610. * To be used instead of the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
  611. * @param hpcd PCD handle
  612. * @param pCallback pointer to the USB PCD Iso IN incomplete Callback function
  613. * @retval HAL status
  614. */
  615. HAL_StatusTypeDef HAL_PCD_RegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd,
  616. pPCD_IsoInIncpltCallbackTypeDef pCallback)
  617. {
  618. HAL_StatusTypeDef status = HAL_OK;
  619. if (pCallback == NULL)
  620. {
  621. /* Update the error code */
  622. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  623. return HAL_ERROR;
  624. }
  625. /* Process locked */
  626. __HAL_LOCK(hpcd);
  627. if (hpcd->State == HAL_PCD_STATE_READY)
  628. {
  629. hpcd->ISOINIncompleteCallback = pCallback;
  630. }
  631. else
  632. {
  633. /* Update the error code */
  634. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  635. /* Return error status */
  636. status = HAL_ERROR;
  637. }
  638. /* Release Lock */
  639. __HAL_UNLOCK(hpcd);
  640. return status;
  641. }
  642. /**
  643. * @brief Unregister the USB PCD Iso IN incomplete Callback
  644. * USB PCD Iso IN incomplete Callback is redirected
  645. * to the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
  646. * @param hpcd PCD handle
  647. * @retval HAL status
  648. */
  649. HAL_StatusTypeDef HAL_PCD_UnRegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd)
  650. {
  651. HAL_StatusTypeDef status = HAL_OK;
  652. /* Process locked */
  653. __HAL_LOCK(hpcd);
  654. if (hpcd->State == HAL_PCD_STATE_READY)
  655. {
  656. hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback; /* Legacy weak ISOINIncompleteCallback */
  657. }
  658. else
  659. {
  660. /* Update the error code */
  661. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  662. /* Return error status */
  663. status = HAL_ERROR;
  664. }
  665. /* Release Lock */
  666. __HAL_UNLOCK(hpcd);
  667. return status;
  668. }
  669. /**
  670. * @brief Register USB PCD BCD Callback
  671. * To be used instead of the weak HAL_PCDEx_BCD_Callback() predefined callback
  672. * @param hpcd PCD handle
  673. * @param pCallback pointer to the USB PCD BCD Callback function
  674. * @retval HAL status
  675. */
  676. HAL_StatusTypeDef HAL_PCD_RegisterBcdCallback(PCD_HandleTypeDef *hpcd, pPCD_BcdCallbackTypeDef pCallback)
  677. {
  678. HAL_StatusTypeDef status = HAL_OK;
  679. if (pCallback == NULL)
  680. {
  681. /* Update the error code */
  682. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  683. return HAL_ERROR;
  684. }
  685. /* Process locked */
  686. __HAL_LOCK(hpcd);
  687. if (hpcd->State == HAL_PCD_STATE_READY)
  688. {
  689. hpcd->BCDCallback = pCallback;
  690. }
  691. else
  692. {
  693. /* Update the error code */
  694. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  695. /* Return error status */
  696. status = HAL_ERROR;
  697. }
  698. /* Release Lock */
  699. __HAL_UNLOCK(hpcd);
  700. return status;
  701. }
  702. /**
  703. * @brief Unregister the USB PCD BCD Callback
  704. * USB BCD Callback is redirected to the weak HAL_PCDEx_BCD_Callback() predefined callback
  705. * @param hpcd PCD handle
  706. * @retval HAL status
  707. */
  708. HAL_StatusTypeDef HAL_PCD_UnRegisterBcdCallback(PCD_HandleTypeDef *hpcd)
  709. {
  710. HAL_StatusTypeDef status = HAL_OK;
  711. /* Process locked */
  712. __HAL_LOCK(hpcd);
  713. if (hpcd->State == HAL_PCD_STATE_READY)
  714. {
  715. hpcd->BCDCallback = HAL_PCDEx_BCD_Callback; /* Legacy weak HAL_PCDEx_BCD_Callback */
  716. }
  717. else
  718. {
  719. /* Update the error code */
  720. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  721. /* Return error status */
  722. status = HAL_ERROR;
  723. }
  724. /* Release Lock */
  725. __HAL_UNLOCK(hpcd);
  726. return status;
  727. }
  728. /**
  729. * @brief Register USB PCD LPM Callback
  730. * To be used instead of the weak HAL_PCDEx_LPM_Callback() predefined callback
  731. * @param hpcd PCD handle
  732. * @param pCallback pointer to the USB PCD LPM Callback function
  733. * @retval HAL status
  734. */
  735. HAL_StatusTypeDef HAL_PCD_RegisterLpmCallback(PCD_HandleTypeDef *hpcd, pPCD_LpmCallbackTypeDef pCallback)
  736. {
  737. HAL_StatusTypeDef status = HAL_OK;
  738. if (pCallback == NULL)
  739. {
  740. /* Update the error code */
  741. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  742. return HAL_ERROR;
  743. }
  744. /* Process locked */
  745. __HAL_LOCK(hpcd);
  746. if (hpcd->State == HAL_PCD_STATE_READY)
  747. {
  748. hpcd->LPMCallback = pCallback;
  749. }
  750. else
  751. {
  752. /* Update the error code */
  753. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  754. /* Return error status */
  755. status = HAL_ERROR;
  756. }
  757. /* Release Lock */
  758. __HAL_UNLOCK(hpcd);
  759. return status;
  760. }
  761. /**
  762. * @brief Unregister the USB PCD LPM Callback
  763. * USB LPM Callback is redirected to the weak HAL_PCDEx_LPM_Callback() predefined callback
  764. * @param hpcd PCD handle
  765. * @retval HAL status
  766. */
  767. HAL_StatusTypeDef HAL_PCD_UnRegisterLpmCallback(PCD_HandleTypeDef *hpcd)
  768. {
  769. HAL_StatusTypeDef status = HAL_OK;
  770. /* Process locked */
  771. __HAL_LOCK(hpcd);
  772. if (hpcd->State == HAL_PCD_STATE_READY)
  773. {
  774. hpcd->LPMCallback = HAL_PCDEx_LPM_Callback; /* Legacy weak HAL_PCDEx_LPM_Callback */
  775. }
  776. else
  777. {
  778. /* Update the error code */
  779. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  780. /* Return error status */
  781. status = HAL_ERROR;
  782. }
  783. /* Release Lock */
  784. __HAL_UNLOCK(hpcd);
  785. return status;
  786. }
  787. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  788. /**
  789. * @}
  790. */
  791. /** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
  792. * @brief Data transfers functions
  793. *
  794. @verbatim
  795. ===============================================================================
  796. ##### IO operation functions #####
  797. ===============================================================================
  798. [..]
  799. This subsection provides a set of functions allowing to manage the PCD data
  800. transfers.
  801. @endverbatim
  802. * @{
  803. */
  804. /**
  805. * @brief Start the USB device
  806. * @param hpcd PCD handle
  807. * @retval HAL status
  808. */
  809. HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
  810. {
  811. __HAL_LOCK(hpcd);
  812. __HAL_PCD_ENABLE(hpcd);
  813. (void)USB_DevConnect(hpcd->Instance);
  814. __HAL_UNLOCK(hpcd);
  815. return HAL_OK;
  816. }
  817. /**
  818. * @brief Stop the USB device.
  819. * @param hpcd PCD handle
  820. * @retval HAL status
  821. */
  822. HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
  823. {
  824. __HAL_LOCK(hpcd);
  825. __HAL_PCD_DISABLE(hpcd);
  826. (void)USB_DevDisconnect(hpcd->Instance);
  827. __HAL_UNLOCK(hpcd);
  828. return HAL_OK;
  829. }
  830. /**
  831. * @brief This function handles PCD interrupt request.
  832. * @param hpcd PCD handle
  833. * @retval HAL status
  834. */
  835. void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
  836. {
  837. uint32_t wIstr = USB_ReadInterrupts(hpcd->Instance);
  838. if ((wIstr & USB_ISTR_CTR) == USB_ISTR_CTR)
  839. {
  840. /* servicing of the endpoint correct transfer interrupt */
  841. /* clear of the CTR flag into the sub */
  842. (void)PCD_EP_ISR_Handler(hpcd);
  843. return;
  844. }
  845. if ((wIstr & USB_ISTR_RESET) == USB_ISTR_RESET)
  846. {
  847. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_RESET);
  848. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  849. hpcd->ResetCallback(hpcd);
  850. #else
  851. HAL_PCD_ResetCallback(hpcd);
  852. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  853. (void)HAL_PCD_SetAddress(hpcd, 0U);
  854. return;
  855. }
  856. if ((wIstr & USB_ISTR_PMAOVR) == USB_ISTR_PMAOVR)
  857. {
  858. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_PMAOVR);
  859. return;
  860. }
  861. if ((wIstr & USB_ISTR_ERR) == USB_ISTR_ERR)
  862. {
  863. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ERR);
  864. return;
  865. }
  866. if ((wIstr & USB_ISTR_WKUP) == USB_ISTR_WKUP)
  867. {
  868. hpcd->Instance->CNTR &= (uint16_t) ~(USB_CNTR_LPMODE);
  869. hpcd->Instance->CNTR &= (uint16_t) ~(USB_CNTR_FSUSP);
  870. if (hpcd->LPM_State == LPM_L1)
  871. {
  872. hpcd->LPM_State = LPM_L0;
  873. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  874. hpcd->LPMCallback(hpcd, PCD_LPM_L0_ACTIVE);
  875. #else
  876. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
  877. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  878. }
  879. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  880. hpcd->ResumeCallback(hpcd);
  881. #else
  882. HAL_PCD_ResumeCallback(hpcd);
  883. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  884. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_WKUP);
  885. return;
  886. }
  887. if ((wIstr & USB_ISTR_SUSP) == USB_ISTR_SUSP)
  888. {
  889. /* Force low-power mode in the macrocell */
  890. hpcd->Instance->CNTR |= (uint16_t)USB_CNTR_FSUSP;
  891. /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
  892. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SUSP);
  893. hpcd->Instance->CNTR |= (uint16_t)USB_CNTR_LPMODE;
  894. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  895. hpcd->SuspendCallback(hpcd);
  896. #else
  897. HAL_PCD_SuspendCallback(hpcd);
  898. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  899. return;
  900. }
  901. /* Handle LPM Interrupt */
  902. if ((wIstr & USB_ISTR_L1REQ) == USB_ISTR_L1REQ)
  903. {
  904. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_L1REQ);
  905. if (hpcd->LPM_State == LPM_L0)
  906. {
  907. /* Force suspend and low-power mode before going to L1 state*/
  908. hpcd->Instance->CNTR |= (uint16_t)USB_CNTR_LPMODE;
  909. hpcd->Instance->CNTR |= (uint16_t)USB_CNTR_FSUSP;
  910. hpcd->LPM_State = LPM_L1;
  911. hpcd->BESL = ((uint32_t)hpcd->Instance->LPMCSR & USB_LPMCSR_BESL) >> 2;
  912. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  913. hpcd->LPMCallback(hpcd, PCD_LPM_L1_ACTIVE);
  914. #else
  915. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
  916. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  917. }
  918. else
  919. {
  920. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  921. hpcd->SuspendCallback(hpcd);
  922. #else
  923. HAL_PCD_SuspendCallback(hpcd);
  924. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  925. }
  926. return;
  927. }
  928. if ((wIstr & USB_ISTR_SOF) == USB_ISTR_SOF)
  929. {
  930. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SOF);
  931. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  932. hpcd->SOFCallback(hpcd);
  933. #else
  934. HAL_PCD_SOFCallback(hpcd);
  935. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  936. return;
  937. }
  938. if ((wIstr & USB_ISTR_ESOF) == USB_ISTR_ESOF)
  939. {
  940. /* clear ESOF flag in ISTR */
  941. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ESOF);
  942. return;
  943. }
  944. }
  945. /**
  946. * @brief Data OUT stage callback.
  947. * @param hpcd PCD handle
  948. * @param epnum endpoint number
  949. * @retval None
  950. */
  951. __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  952. {
  953. /* Prevent unused argument(s) compilation warning */
  954. UNUSED(hpcd);
  955. UNUSED(epnum);
  956. /* NOTE : This function should not be modified, when the callback is needed,
  957. the HAL_PCD_DataOutStageCallback could be implemented in the user file
  958. */
  959. }
  960. /**
  961. * @brief Data IN stage callback
  962. * @param hpcd PCD handle
  963. * @param epnum endpoint number
  964. * @retval None
  965. */
  966. __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  967. {
  968. /* Prevent unused argument(s) compilation warning */
  969. UNUSED(hpcd);
  970. UNUSED(epnum);
  971. /* NOTE : This function should not be modified, when the callback is needed,
  972. the HAL_PCD_DataInStageCallback could be implemented in the user file
  973. */
  974. }
  975. /**
  976. * @brief Setup stage callback
  977. * @param hpcd PCD handle
  978. * @retval None
  979. */
  980. __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  981. {
  982. /* Prevent unused argument(s) compilation warning */
  983. UNUSED(hpcd);
  984. /* NOTE : This function should not be modified, when the callback is needed,
  985. the HAL_PCD_SetupStageCallback could be implemented in the user file
  986. */
  987. }
  988. /**
  989. * @brief USB Start Of Frame callback.
  990. * @param hpcd PCD handle
  991. * @retval None
  992. */
  993. __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  994. {
  995. /* Prevent unused argument(s) compilation warning */
  996. UNUSED(hpcd);
  997. /* NOTE : This function should not be modified, when the callback is needed,
  998. the HAL_PCD_SOFCallback could be implemented in the user file
  999. */
  1000. }
  1001. /**
  1002. * @brief USB Reset callback.
  1003. * @param hpcd PCD handle
  1004. * @retval None
  1005. */
  1006. __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  1007. {
  1008. /* Prevent unused argument(s) compilation warning */
  1009. UNUSED(hpcd);
  1010. /* NOTE : This function should not be modified, when the callback is needed,
  1011. the HAL_PCD_ResetCallback could be implemented in the user file
  1012. */
  1013. }
  1014. /**
  1015. * @brief Suspend event callback.
  1016. * @param hpcd PCD handle
  1017. * @retval None
  1018. */
  1019. __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  1020. {
  1021. /* Prevent unused argument(s) compilation warning */
  1022. UNUSED(hpcd);
  1023. /* NOTE : This function should not be modified, when the callback is needed,
  1024. the HAL_PCD_SuspendCallback could be implemented in the user file
  1025. */
  1026. }
  1027. /**
  1028. * @brief Resume event callback.
  1029. * @param hpcd PCD handle
  1030. * @retval None
  1031. */
  1032. __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  1033. {
  1034. /* Prevent unused argument(s) compilation warning */
  1035. UNUSED(hpcd);
  1036. /* NOTE : This function should not be modified, when the callback is needed,
  1037. the HAL_PCD_ResumeCallback could be implemented in the user file
  1038. */
  1039. }
  1040. /**
  1041. * @brief Incomplete ISO OUT callback.
  1042. * @param hpcd PCD handle
  1043. * @param epnum endpoint number
  1044. * @retval None
  1045. */
  1046. __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1047. {
  1048. /* Prevent unused argument(s) compilation warning */
  1049. UNUSED(hpcd);
  1050. UNUSED(epnum);
  1051. /* NOTE : This function should not be modified, when the callback is needed,
  1052. the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
  1053. */
  1054. }
  1055. /**
  1056. * @brief Incomplete ISO IN callback.
  1057. * @param hpcd PCD handle
  1058. * @param epnum endpoint number
  1059. * @retval None
  1060. */
  1061. __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1062. {
  1063. /* Prevent unused argument(s) compilation warning */
  1064. UNUSED(hpcd);
  1065. UNUSED(epnum);
  1066. /* NOTE : This function should not be modified, when the callback is needed,
  1067. the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
  1068. */
  1069. }
  1070. /**
  1071. * @brief Connection event callback.
  1072. * @param hpcd PCD handle
  1073. * @retval None
  1074. */
  1075. __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  1076. {
  1077. /* Prevent unused argument(s) compilation warning */
  1078. UNUSED(hpcd);
  1079. /* NOTE : This function should not be modified, when the callback is needed,
  1080. the HAL_PCD_ConnectCallback could be implemented in the user file
  1081. */
  1082. }
  1083. /**
  1084. * @brief Disconnection event callback.
  1085. * @param hpcd PCD handle
  1086. * @retval None
  1087. */
  1088. __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  1089. {
  1090. /* Prevent unused argument(s) compilation warning */
  1091. UNUSED(hpcd);
  1092. /* NOTE : This function should not be modified, when the callback is needed,
  1093. the HAL_PCD_DisconnectCallback could be implemented in the user file
  1094. */
  1095. }
  1096. /**
  1097. * @}
  1098. */
  1099. /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
  1100. * @brief management functions
  1101. *
  1102. @verbatim
  1103. ===============================================================================
  1104. ##### Peripheral Control functions #####
  1105. ===============================================================================
  1106. [..]
  1107. This subsection provides a set of functions allowing to control the PCD data
  1108. transfers.
  1109. @endverbatim
  1110. * @{
  1111. */
  1112. /**
  1113. * @brief Connect the USB device
  1114. * @param hpcd PCD handle
  1115. * @retval HAL status
  1116. */
  1117. HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
  1118. {
  1119. __HAL_LOCK(hpcd);
  1120. (void)USB_DevConnect(hpcd->Instance);
  1121. __HAL_UNLOCK(hpcd);
  1122. return HAL_OK;
  1123. }
  1124. /**
  1125. * @brief Disconnect the USB device.
  1126. * @param hpcd PCD handle
  1127. * @retval HAL status
  1128. */
  1129. HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
  1130. {
  1131. __HAL_LOCK(hpcd);
  1132. (void)USB_DevDisconnect(hpcd->Instance);
  1133. __HAL_UNLOCK(hpcd);
  1134. return HAL_OK;
  1135. }
  1136. /**
  1137. * @brief Set the USB Device address.
  1138. * @param hpcd PCD handle
  1139. * @param address new device address
  1140. * @retval HAL status
  1141. */
  1142. HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
  1143. {
  1144. __HAL_LOCK(hpcd);
  1145. hpcd->USB_Address = address;
  1146. (void)USB_SetDevAddress(hpcd->Instance, address);
  1147. __HAL_UNLOCK(hpcd);
  1148. return HAL_OK;
  1149. }
  1150. /**
  1151. * @brief Open and configure an endpoint.
  1152. * @param hpcd PCD handle
  1153. * @param ep_addr endpoint address
  1154. * @param ep_mps endpoint max packet size
  1155. * @param ep_type endpoint type
  1156. * @retval HAL status
  1157. */
  1158. HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
  1159. uint16_t ep_mps, uint8_t ep_type)
  1160. {
  1161. HAL_StatusTypeDef ret = HAL_OK;
  1162. PCD_EPTypeDef *ep;
  1163. if ((ep_addr & 0x80U) == 0x80U)
  1164. {
  1165. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1166. ep->is_in = 1U;
  1167. }
  1168. else
  1169. {
  1170. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1171. ep->is_in = 0U;
  1172. }
  1173. ep->num = ep_addr & EP_ADDR_MSK;
  1174. ep->maxpacket = (uint32_t)ep_mps & 0x7FFU;
  1175. ep->type = ep_type;
  1176. /* Set initial data PID. */
  1177. if (ep_type == EP_TYPE_BULK)
  1178. {
  1179. ep->data_pid_start = 0U;
  1180. }
  1181. __HAL_LOCK(hpcd);
  1182. (void)USB_ActivateEndpoint(hpcd->Instance, ep);
  1183. __HAL_UNLOCK(hpcd);
  1184. return ret;
  1185. }
  1186. /**
  1187. * @brief Deactivate an endpoint.
  1188. * @param hpcd PCD handle
  1189. * @param ep_addr endpoint address
  1190. * @retval HAL status
  1191. */
  1192. HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1193. {
  1194. PCD_EPTypeDef *ep;
  1195. if ((ep_addr & 0x80U) == 0x80U)
  1196. {
  1197. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1198. ep->is_in = 1U;
  1199. }
  1200. else
  1201. {
  1202. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1203. ep->is_in = 0U;
  1204. }
  1205. ep->num = ep_addr & EP_ADDR_MSK;
  1206. __HAL_LOCK(hpcd);
  1207. (void)USB_DeactivateEndpoint(hpcd->Instance, ep);
  1208. __HAL_UNLOCK(hpcd);
  1209. return HAL_OK;
  1210. }
  1211. /**
  1212. * @brief Receive an amount of data.
  1213. * @param hpcd PCD handle
  1214. * @param ep_addr endpoint address
  1215. * @param pBuf pointer to the reception buffer
  1216. * @param len amount of data to be received
  1217. * @retval HAL status
  1218. */
  1219. HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  1220. {
  1221. PCD_EPTypeDef *ep;
  1222. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1223. /*setup and start the Xfer */
  1224. ep->xfer_buff = pBuf;
  1225. ep->xfer_len = len;
  1226. ep->xfer_count = 0U;
  1227. ep->is_in = 0U;
  1228. ep->num = ep_addr & EP_ADDR_MSK;
  1229. (void)USB_EPStartXfer(hpcd->Instance, ep);
  1230. return HAL_OK;
  1231. }
  1232. /**
  1233. * @brief Get Received Data Size
  1234. * @param hpcd PCD handle
  1235. * @param ep_addr endpoint address
  1236. * @retval Data Size
  1237. */
  1238. uint32_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef const *hpcd, uint8_t ep_addr)
  1239. {
  1240. return hpcd->OUT_ep[ep_addr & EP_ADDR_MSK].xfer_count;
  1241. }
  1242. /**
  1243. * @brief Send an amount of data
  1244. * @param hpcd PCD handle
  1245. * @param ep_addr endpoint address
  1246. * @param pBuf pointer to the transmission buffer
  1247. * @param len amount of data to be sent
  1248. * @retval HAL status
  1249. */
  1250. HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  1251. {
  1252. PCD_EPTypeDef *ep;
  1253. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1254. /*setup and start the Xfer */
  1255. ep->xfer_buff = pBuf;
  1256. ep->xfer_len = len;
  1257. ep->xfer_fill_db = 1U;
  1258. ep->xfer_len_db = len;
  1259. ep->xfer_count = 0U;
  1260. ep->is_in = 1U;
  1261. ep->num = ep_addr & EP_ADDR_MSK;
  1262. (void)USB_EPStartXfer(hpcd->Instance, ep);
  1263. return HAL_OK;
  1264. }
  1265. /**
  1266. * @brief Set a STALL condition over an endpoint
  1267. * @param hpcd PCD handle
  1268. * @param ep_addr endpoint address
  1269. * @retval HAL status
  1270. */
  1271. HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1272. {
  1273. PCD_EPTypeDef *ep;
  1274. if (((uint32_t)ep_addr & EP_ADDR_MSK) > hpcd->Init.dev_endpoints)
  1275. {
  1276. return HAL_ERROR;
  1277. }
  1278. if ((0x80U & ep_addr) == 0x80U)
  1279. {
  1280. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1281. ep->is_in = 1U;
  1282. }
  1283. else
  1284. {
  1285. ep = &hpcd->OUT_ep[ep_addr];
  1286. ep->is_in = 0U;
  1287. }
  1288. ep->is_stall = 1U;
  1289. ep->num = ep_addr & EP_ADDR_MSK;
  1290. __HAL_LOCK(hpcd);
  1291. (void)USB_EPSetStall(hpcd->Instance, ep);
  1292. __HAL_UNLOCK(hpcd);
  1293. return HAL_OK;
  1294. }
  1295. /**
  1296. * @brief Clear a STALL condition over in an endpoint
  1297. * @param hpcd PCD handle
  1298. * @param ep_addr endpoint address
  1299. * @retval HAL status
  1300. */
  1301. HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1302. {
  1303. PCD_EPTypeDef *ep;
  1304. if (((uint32_t)ep_addr & 0x0FU) > hpcd->Init.dev_endpoints)
  1305. {
  1306. return HAL_ERROR;
  1307. }
  1308. if ((0x80U & ep_addr) == 0x80U)
  1309. {
  1310. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1311. ep->is_in = 1U;
  1312. }
  1313. else
  1314. {
  1315. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1316. ep->is_in = 0U;
  1317. }
  1318. ep->is_stall = 0U;
  1319. ep->num = ep_addr & EP_ADDR_MSK;
  1320. __HAL_LOCK(hpcd);
  1321. (void)USB_EPClearStall(hpcd->Instance, ep);
  1322. __HAL_UNLOCK(hpcd);
  1323. return HAL_OK;
  1324. }
  1325. /**
  1326. * @brief Abort an USB EP transaction.
  1327. * @param hpcd PCD handle
  1328. * @param ep_addr endpoint address
  1329. * @retval HAL status
  1330. */
  1331. HAL_StatusTypeDef HAL_PCD_EP_Abort(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1332. {
  1333. HAL_StatusTypeDef ret;
  1334. PCD_EPTypeDef *ep;
  1335. if ((0x80U & ep_addr) == 0x80U)
  1336. {
  1337. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1338. }
  1339. else
  1340. {
  1341. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1342. }
  1343. /* Stop Xfer */
  1344. ret = USB_EPStopXfer(hpcd->Instance, ep);
  1345. return ret;
  1346. }
  1347. /**
  1348. * @brief Flush an endpoint
  1349. * @param hpcd PCD handle
  1350. * @param ep_addr endpoint address
  1351. * @retval HAL status
  1352. */
  1353. HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1354. {
  1355. __HAL_LOCK(hpcd);
  1356. if ((ep_addr & 0x80U) == 0x80U)
  1357. {
  1358. (void)USB_FlushTxFifo(hpcd->Instance, (uint32_t)ep_addr & EP_ADDR_MSK);
  1359. }
  1360. else
  1361. {
  1362. (void)USB_FlushRxFifo(hpcd->Instance);
  1363. }
  1364. __HAL_UNLOCK(hpcd);
  1365. return HAL_OK;
  1366. }
  1367. /**
  1368. * @brief Activate remote wakeup signalling
  1369. * @param hpcd PCD handle
  1370. * @retval HAL status
  1371. */
  1372. HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1373. {
  1374. return (USB_ActivateRemoteWakeup(hpcd->Instance));
  1375. }
  1376. /**
  1377. * @brief De-activate remote wakeup signalling.
  1378. * @param hpcd PCD handle
  1379. * @retval HAL status
  1380. */
  1381. HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1382. {
  1383. return (USB_DeActivateRemoteWakeup(hpcd->Instance));
  1384. }
  1385. /**
  1386. * @}
  1387. */
  1388. /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
  1389. * @brief Peripheral State functions
  1390. *
  1391. @verbatim
  1392. ===============================================================================
  1393. ##### Peripheral State functions #####
  1394. ===============================================================================
  1395. [..]
  1396. This subsection permits to get in run-time the status of the peripheral
  1397. and the data flow.
  1398. @endverbatim
  1399. * @{
  1400. */
  1401. /**
  1402. * @brief Return the PCD handle state.
  1403. * @param hpcd PCD handle
  1404. * @retval HAL state
  1405. */
  1406. PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef const *hpcd)
  1407. {
  1408. return hpcd->State;
  1409. }
  1410. /**
  1411. * @}
  1412. */
  1413. /**
  1414. * @}
  1415. */
  1416. /* Private functions ---------------------------------------------------------*/
  1417. /** @addtogroup PCD_Private_Functions
  1418. * @{
  1419. */
  1420. /**
  1421. * @brief This function handles PCD Endpoint interrupt request.
  1422. * @param hpcd PCD handle
  1423. * @retval HAL status
  1424. */
  1425. static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
  1426. {
  1427. PCD_EPTypeDef *ep;
  1428. uint16_t count;
  1429. uint16_t wIstr;
  1430. uint16_t wEPVal;
  1431. uint16_t TxPctSize;
  1432. uint8_t epindex;
  1433. #if (USE_USB_DOUBLE_BUFFER != 1U)
  1434. count = 0U;
  1435. #endif /* USE_USB_DOUBLE_BUFFER */
  1436. /* stay in loop while pending interrupts */
  1437. while ((hpcd->Instance->ISTR & USB_ISTR_CTR) != 0U)
  1438. {
  1439. wIstr = hpcd->Instance->ISTR;
  1440. /* extract highest priority endpoint number */
  1441. epindex = (uint8_t)(wIstr & USB_ISTR_EP_ID);
  1442. if (epindex == 0U)
  1443. {
  1444. /* Decode and service control endpoint interrupt */
  1445. /* DIR bit = origin of the interrupt */
  1446. if ((wIstr & USB_ISTR_DIR) == 0U)
  1447. {
  1448. /* DIR = 0 */
  1449. /* DIR = 0 => IN int */
  1450. /* DIR = 0 implies that (EP_CTR_TX = 1) always */
  1451. PCD_CLEAR_TX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  1452. ep = &hpcd->IN_ep[0];
  1453. ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
  1454. ep->xfer_buff += ep->xfer_count;
  1455. /* TX COMPLETE */
  1456. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1457. hpcd->DataInStageCallback(hpcd, 0U);
  1458. #else
  1459. HAL_PCD_DataInStageCallback(hpcd, 0U);
  1460. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1461. if ((hpcd->USB_Address > 0U) && (ep->xfer_len == 0U))
  1462. {
  1463. hpcd->Instance->DADDR = ((uint16_t)hpcd->USB_Address | USB_DADDR_EF);
  1464. hpcd->USB_Address = 0U;
  1465. }
  1466. }
  1467. else
  1468. {
  1469. /* DIR = 1 */
  1470. /* DIR = 1 & CTR_RX => SETUP or OUT int */
  1471. /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
  1472. ep = &hpcd->OUT_ep[0];
  1473. wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
  1474. if ((wEPVal & USB_EP_SETUP) != 0U)
  1475. {
  1476. /* Get SETUP Packet */
  1477. ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  1478. USB_ReadPMA(hpcd->Instance, (uint8_t *)hpcd->Setup,
  1479. ep->pmaadress, (uint16_t)ep->xfer_count);
  1480. /* SETUP bit kept frozen while CTR_RX = 1 */
  1481. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  1482. /* Process SETUP Packet*/
  1483. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1484. hpcd->SetupStageCallback(hpcd);
  1485. #else
  1486. HAL_PCD_SetupStageCallback(hpcd);
  1487. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1488. }
  1489. else if ((wEPVal & USB_EP_CTR_RX) != 0U)
  1490. {
  1491. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  1492. /* Get Control Data OUT Packet */
  1493. ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  1494. if ((ep->xfer_count != 0U) && (ep->xfer_buff != 0U))
  1495. {
  1496. USB_ReadPMA(hpcd->Instance, ep->xfer_buff,
  1497. ep->pmaadress, (uint16_t)ep->xfer_count);
  1498. ep->xfer_buff += ep->xfer_count;
  1499. /* Process Control Data OUT Packet */
  1500. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1501. hpcd->DataOutStageCallback(hpcd, 0U);
  1502. #else
  1503. HAL_PCD_DataOutStageCallback(hpcd, 0U);
  1504. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1505. }
  1506. wEPVal = (uint16_t)PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
  1507. if (((wEPVal & USB_EP_SETUP) == 0U) && ((wEPVal & USB_EP_RX_STRX) != USB_EP_RX_VALID))
  1508. {
  1509. PCD_SET_EP_RX_STATUS(hpcd->Instance, PCD_ENDP0, USB_EP_RX_VALID);
  1510. }
  1511. }
  1512. }
  1513. }
  1514. else
  1515. {
  1516. /* Decode and service non control endpoints interrupt */
  1517. /* process related endpoint register */
  1518. wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, epindex);
  1519. if ((wEPVal & USB_EP_CTR_RX) != 0U)
  1520. {
  1521. /* clear int flag */
  1522. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, epindex);
  1523. ep = &hpcd->OUT_ep[epindex];
  1524. /* OUT Single Buffering */
  1525. if (ep->doublebuffer == 0U)
  1526. {
  1527. count = (uint16_t)PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  1528. if (count != 0U)
  1529. {
  1530. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, count);
  1531. }
  1532. }
  1533. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1534. else
  1535. {
  1536. /* manage double buffer bulk out */
  1537. if (ep->type == EP_TYPE_BULK)
  1538. {
  1539. count = HAL_PCD_EP_DB_Receive(hpcd, ep, wEPVal);
  1540. }
  1541. else /* manage double buffer iso out */
  1542. {
  1543. /* free EP OUT Buffer */
  1544. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
  1545. if ((PCD_GET_ENDPOINT(hpcd->Instance, ep->num) & USB_EP_DTOG_RX) != 0U)
  1546. {
  1547. /* read from endpoint BUF0Addr buffer */
  1548. count = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  1549. if (count != 0U)
  1550. {
  1551. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
  1552. }
  1553. }
  1554. else
  1555. {
  1556. /* read from endpoint BUF1Addr buffer */
  1557. count = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
  1558. if (count != 0U)
  1559. {
  1560. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
  1561. }
  1562. }
  1563. }
  1564. }
  1565. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1566. /* multi-packet on the NON control OUT endpoint */
  1567. ep->xfer_count += count;
  1568. ep->xfer_buff += count;
  1569. if ((ep->xfer_len == 0U) || (count < ep->maxpacket))
  1570. {
  1571. /* RX COMPLETE */
  1572. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1573. hpcd->DataOutStageCallback(hpcd, ep->num);
  1574. #else
  1575. HAL_PCD_DataOutStageCallback(hpcd, ep->num);
  1576. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1577. }
  1578. else
  1579. {
  1580. (void)USB_EPStartXfer(hpcd->Instance, ep);
  1581. }
  1582. }
  1583. if ((wEPVal & USB_EP_CTR_TX) != 0U)
  1584. {
  1585. ep = &hpcd->IN_ep[epindex];
  1586. /* clear int flag */
  1587. PCD_CLEAR_TX_EP_CTR(hpcd->Instance, epindex);
  1588. if (ep->type == EP_TYPE_ISOC)
  1589. {
  1590. ep->xfer_len = 0U;
  1591. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1592. if (ep->doublebuffer != 0U)
  1593. {
  1594. if ((wEPVal & USB_EP_DTOG_TX) != 0U)
  1595. {
  1596. PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1597. }
  1598. else
  1599. {
  1600. PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1601. }
  1602. }
  1603. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1604. /* TX COMPLETE */
  1605. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1606. hpcd->DataInStageCallback(hpcd, ep->num);
  1607. #else
  1608. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  1609. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1610. }
  1611. else
  1612. {
  1613. /* Manage Single Buffer Transaction */
  1614. if ((wEPVal & USB_EP_KIND) == 0U)
  1615. {
  1616. /* Multi-packet on the NON control IN endpoint */
  1617. TxPctSize = (uint16_t)PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
  1618. if (ep->xfer_len > TxPctSize)
  1619. {
  1620. ep->xfer_len -= TxPctSize;
  1621. }
  1622. else
  1623. {
  1624. ep->xfer_len = 0U;
  1625. }
  1626. /* Zero Length Packet? */
  1627. if (ep->xfer_len == 0U)
  1628. {
  1629. /* TX COMPLETE */
  1630. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1631. hpcd->DataInStageCallback(hpcd, ep->num);
  1632. #else
  1633. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  1634. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1635. }
  1636. else
  1637. {
  1638. /* Transfer is not yet Done */
  1639. ep->xfer_buff += TxPctSize;
  1640. ep->xfer_count += TxPctSize;
  1641. (void)USB_EPStartXfer(hpcd->Instance, ep);
  1642. }
  1643. }
  1644. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1645. /* Double Buffer bulk IN (bulk transfer Len > Ep_Mps) */
  1646. else
  1647. {
  1648. (void)HAL_PCD_EP_DB_Transmit(hpcd, ep, wEPVal);
  1649. }
  1650. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1651. }
  1652. }
  1653. }
  1654. }
  1655. return HAL_OK;
  1656. }
  1657. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1658. /**
  1659. * @brief Manage double buffer bulk out transaction from ISR
  1660. * @param hpcd PCD handle
  1661. * @param ep current endpoint handle
  1662. * @param wEPVal Last snapshot of EPRx register value taken in ISR
  1663. * @retval HAL status
  1664. */
  1665. static uint16_t HAL_PCD_EP_DB_Receive(PCD_HandleTypeDef *hpcd,
  1666. PCD_EPTypeDef *ep, uint16_t wEPVal)
  1667. {
  1668. uint16_t count;
  1669. /* Manage Buffer0 OUT */
  1670. if ((wEPVal & USB_EP_DTOG_RX) != 0U)
  1671. {
  1672. /* Get count of received Data on buffer0 */
  1673. count = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  1674. if (ep->xfer_len >= count)
  1675. {
  1676. ep->xfer_len -= count;
  1677. }
  1678. else
  1679. {
  1680. ep->xfer_len = 0U;
  1681. }
  1682. if (ep->xfer_len == 0U)
  1683. {
  1684. /* Set NAK to OUT endpoint since double buffer is enabled */
  1685. PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_NAK);
  1686. }
  1687. /* Check if Buffer1 is in blocked state which requires to toggle */
  1688. if ((wEPVal & USB_EP_DTOG_TX) != 0U)
  1689. {
  1690. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
  1691. }
  1692. if (count != 0U)
  1693. {
  1694. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
  1695. }
  1696. }
  1697. /* Manage Buffer 1 DTOG_RX=0 */
  1698. else
  1699. {
  1700. /* Get count of received data */
  1701. count = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
  1702. if (ep->xfer_len >= count)
  1703. {
  1704. ep->xfer_len -= count;
  1705. }
  1706. else
  1707. {
  1708. ep->xfer_len = 0U;
  1709. }
  1710. if (ep->xfer_len == 0U)
  1711. {
  1712. /* Set NAK on the current endpoint */
  1713. PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_NAK);
  1714. }
  1715. /* Need to FreeUser Buffer */
  1716. if ((wEPVal & USB_EP_DTOG_TX) == 0U)
  1717. {
  1718. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
  1719. }
  1720. if (count != 0U)
  1721. {
  1722. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
  1723. }
  1724. }
  1725. return count;
  1726. }
  1727. /**
  1728. * @brief Manage double buffer bulk IN transaction from ISR
  1729. * @param hpcd PCD handle
  1730. * @param ep current endpoint handle
  1731. * @param wEPVal Last snapshot of EPRx register value taken in ISR
  1732. * @retval HAL status
  1733. */
  1734. static HAL_StatusTypeDef HAL_PCD_EP_DB_Transmit(PCD_HandleTypeDef *hpcd,
  1735. PCD_EPTypeDef *ep, uint16_t wEPVal)
  1736. {
  1737. uint32_t len;
  1738. uint16_t TxPctSize;
  1739. /* Data Buffer0 ACK received */
  1740. if ((wEPVal & USB_EP_DTOG_TX) != 0U)
  1741. {
  1742. /* multi-packet on the NON control IN endpoint */
  1743. TxPctSize = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  1744. if (ep->xfer_len > TxPctSize)
  1745. {
  1746. ep->xfer_len -= TxPctSize;
  1747. }
  1748. else
  1749. {
  1750. ep->xfer_len = 0U;
  1751. }
  1752. /* Transfer is completed */
  1753. if (ep->xfer_len == 0U)
  1754. {
  1755. PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1756. PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1757. if (ep->type == EP_TYPE_BULK)
  1758. {
  1759. /* Set Bulk endpoint in NAK state */
  1760. PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_NAK);
  1761. }
  1762. /* TX COMPLETE */
  1763. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1764. hpcd->DataInStageCallback(hpcd, ep->num);
  1765. #else
  1766. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  1767. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1768. if ((wEPVal & USB_EP_DTOG_RX) != 0U)
  1769. {
  1770. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
  1771. }
  1772. return HAL_OK;
  1773. }
  1774. else /* Transfer is not yet Done */
  1775. {
  1776. /* Need to Free USB Buffer */
  1777. if ((wEPVal & USB_EP_DTOG_RX) != 0U)
  1778. {
  1779. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
  1780. }
  1781. /* Still there is data to Fill in the next Buffer */
  1782. if (ep->xfer_fill_db == 1U)
  1783. {
  1784. ep->xfer_buff += TxPctSize;
  1785. ep->xfer_count += TxPctSize;
  1786. /* Calculate the len of the new buffer to fill */
  1787. if (ep->xfer_len_db >= ep->maxpacket)
  1788. {
  1789. len = ep->maxpacket;
  1790. ep->xfer_len_db -= len;
  1791. }
  1792. else if (ep->xfer_len_db == 0U)
  1793. {
  1794. len = TxPctSize;
  1795. ep->xfer_fill_db = 0U;
  1796. }
  1797. else
  1798. {
  1799. ep->xfer_fill_db = 0U;
  1800. len = ep->xfer_len_db;
  1801. ep->xfer_len_db = 0U;
  1802. }
  1803. /* Write remaining Data to Buffer */
  1804. /* Set the Double buffer counter for pma buffer0 */
  1805. PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, len);
  1806. /* Copy user buffer to USB PMA */
  1807. USB_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, (uint16_t)len);
  1808. }
  1809. }
  1810. }
  1811. else /* Data Buffer1 ACK received */
  1812. {
  1813. /* multi-packet on the NON control IN endpoint */
  1814. TxPctSize = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
  1815. if (ep->xfer_len >= TxPctSize)
  1816. {
  1817. ep->xfer_len -= TxPctSize;
  1818. }
  1819. else
  1820. {
  1821. ep->xfer_len = 0U;
  1822. }
  1823. /* Transfer is completed */
  1824. if (ep->xfer_len == 0U)
  1825. {
  1826. PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1827. PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1828. if (ep->type == EP_TYPE_BULK)
  1829. {
  1830. /* Set Bulk endpoint in NAK state */
  1831. PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_NAK);
  1832. }
  1833. /* TX COMPLETE */
  1834. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1835. hpcd->DataInStageCallback(hpcd, ep->num);
  1836. #else
  1837. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  1838. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1839. /* need to Free USB Buff */
  1840. if ((wEPVal & USB_EP_DTOG_RX) == 0U)
  1841. {
  1842. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
  1843. }
  1844. return HAL_OK;
  1845. }
  1846. else /* Transfer is not yet Done */
  1847. {
  1848. /* Need to Free USB Buffer */
  1849. if ((wEPVal & USB_EP_DTOG_RX) == 0U)
  1850. {
  1851. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
  1852. }
  1853. /* Still there is data to Fill in the next Buffer */
  1854. if (ep->xfer_fill_db == 1U)
  1855. {
  1856. ep->xfer_buff += TxPctSize;
  1857. ep->xfer_count += TxPctSize;
  1858. /* Calculate the len of the new buffer to fill */
  1859. if (ep->xfer_len_db >= ep->maxpacket)
  1860. {
  1861. len = ep->maxpacket;
  1862. ep->xfer_len_db -= len;
  1863. }
  1864. else if (ep->xfer_len_db == 0U)
  1865. {
  1866. len = TxPctSize;
  1867. ep->xfer_fill_db = 0U;
  1868. }
  1869. else
  1870. {
  1871. len = ep->xfer_len_db;
  1872. ep->xfer_len_db = 0U;
  1873. ep->xfer_fill_db = 0;
  1874. }
  1875. /* Set the Double buffer counter for pma buffer1 */
  1876. PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, len);
  1877. /* Copy the user buffer to USB PMA */
  1878. USB_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, (uint16_t)len);
  1879. }
  1880. }
  1881. }
  1882. /* Enable endpoint IN */
  1883. PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_VALID);
  1884. return HAL_OK;
  1885. }
  1886. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1887. /**
  1888. * @}
  1889. */
  1890. #endif /* defined (USB) */
  1891. #endif /* HAL_PCD_MODULE_ENABLED */
  1892. /**
  1893. * @}
  1894. */
  1895. /**
  1896. * @}
  1897. */