stm32g0xx_hal_pcd.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_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) 2018 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 "stm32g0xx_hal.h"
  49. /** @addtogroup STM32G0xx_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_DRD_FS)
  58. /* Private types -------------------------------------------------------------*/
  59. /* Private variables ---------------------------------------------------------*/
  60. /* Private constants ---------------------------------------------------------*/
  61. /* Private macros ------------------------------------------------------------*/
  62. /** @defgroup PCD_Private_Macros PCD Private Macros
  63. * @{
  64. */
  65. #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
  66. #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
  67. /**
  68. * @}
  69. */
  70. /* Private functions prototypes ----------------------------------------------*/
  71. /** @defgroup PCD_Private_Functions PCD Private Functions
  72. * @{
  73. */
  74. 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. /* check if this is an USB pending IT */
  839. if ((SYSCFG->IT_LINE_SR[8] & (0x1U << 2)) == 0U)
  840. {
  841. return;
  842. }
  843. if ((wIstr & USB_ISTR_CTR) == USB_ISTR_CTR)
  844. {
  845. /* servicing of the endpoint correct transfer interrupt */
  846. /* clear of the CTR flag into the sub */
  847. (void)PCD_EP_ISR_Handler(hpcd);
  848. return;
  849. }
  850. if ((wIstr & USB_ISTR_RESET) == USB_ISTR_RESET)
  851. {
  852. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_RESET);
  853. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  854. hpcd->ResetCallback(hpcd);
  855. #else
  856. HAL_PCD_ResetCallback(hpcd);
  857. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  858. (void)HAL_PCD_SetAddress(hpcd, 0U);
  859. return;
  860. }
  861. if ((wIstr & USB_ISTR_PMAOVR) == USB_ISTR_PMAOVR)
  862. {
  863. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_PMAOVR);
  864. return;
  865. }
  866. if ((wIstr & USB_ISTR_ERR) == USB_ISTR_ERR)
  867. {
  868. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ERR);
  869. return;
  870. }
  871. if ((wIstr & USB_ISTR_WKUP) == USB_ISTR_WKUP)
  872. {
  873. hpcd->Instance->CNTR &= ~(USB_CNTR_SUSPRDY);
  874. hpcd->Instance->CNTR &= ~(USB_CNTR_SUSPEN);
  875. if (hpcd->LPM_State == LPM_L1)
  876. {
  877. hpcd->LPM_State = LPM_L0;
  878. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  879. hpcd->LPMCallback(hpcd, PCD_LPM_L0_ACTIVE);
  880. #else
  881. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
  882. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  883. }
  884. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  885. hpcd->ResumeCallback(hpcd);
  886. #else
  887. HAL_PCD_ResumeCallback(hpcd);
  888. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  889. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_WKUP);
  890. return;
  891. }
  892. if ((wIstr & USB_ISTR_SUSP) == USB_ISTR_SUSP)
  893. {
  894. /* Force low-power mode in the macrocell */
  895. hpcd->Instance->CNTR |= USB_CNTR_SUSPEN;
  896. /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
  897. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SUSP);
  898. hpcd->Instance->CNTR |= USB_CNTR_SUSPRDY;
  899. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  900. hpcd->SuspendCallback(hpcd);
  901. #else
  902. HAL_PCD_SuspendCallback(hpcd);
  903. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  904. return;
  905. }
  906. /* Handle LPM Interrupt */
  907. if ((wIstr & USB_ISTR_L1REQ) == USB_ISTR_L1REQ)
  908. {
  909. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_L1REQ);
  910. if (hpcd->LPM_State == LPM_L0)
  911. {
  912. /* Force suspend and low-power mode before going to L1 state*/
  913. hpcd->Instance->CNTR |= USB_CNTR_SUSPRDY;
  914. hpcd->Instance->CNTR |= USB_CNTR_SUSPEN;
  915. hpcd->LPM_State = LPM_L1;
  916. hpcd->BESL = ((uint32_t)hpcd->Instance->LPMCSR & USB_LPMCSR_BESL) >> 2;
  917. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  918. hpcd->LPMCallback(hpcd, PCD_LPM_L1_ACTIVE);
  919. #else
  920. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
  921. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  922. }
  923. else
  924. {
  925. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  926. hpcd->SuspendCallback(hpcd);
  927. #else
  928. HAL_PCD_SuspendCallback(hpcd);
  929. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  930. }
  931. return;
  932. }
  933. if ((wIstr & USB_ISTR_SOF) == USB_ISTR_SOF)
  934. {
  935. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SOF);
  936. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  937. hpcd->SOFCallback(hpcd);
  938. #else
  939. HAL_PCD_SOFCallback(hpcd);
  940. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  941. return;
  942. }
  943. if ((wIstr & USB_ISTR_ESOF) == USB_ISTR_ESOF)
  944. {
  945. /* clear ESOF flag in ISTR */
  946. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ESOF);
  947. return;
  948. }
  949. }
  950. /**
  951. * @brief Data OUT stage callback.
  952. * @param hpcd PCD handle
  953. * @param epnum endpoint number
  954. * @retval None
  955. */
  956. __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  957. {
  958. /* Prevent unused argument(s) compilation warning */
  959. UNUSED(hpcd);
  960. UNUSED(epnum);
  961. /* NOTE : This function should not be modified, when the callback is needed,
  962. the HAL_PCD_DataOutStageCallback could be implemented in the user file
  963. */
  964. }
  965. /**
  966. * @brief Data IN stage callback
  967. * @param hpcd PCD handle
  968. * @param epnum endpoint number
  969. * @retval None
  970. */
  971. __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  972. {
  973. /* Prevent unused argument(s) compilation warning */
  974. UNUSED(hpcd);
  975. UNUSED(epnum);
  976. /* NOTE : This function should not be modified, when the callback is needed,
  977. the HAL_PCD_DataInStageCallback could be implemented in the user file
  978. */
  979. }
  980. /**
  981. * @brief Setup stage callback
  982. * @param hpcd PCD handle
  983. * @retval None
  984. */
  985. __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  986. {
  987. /* Prevent unused argument(s) compilation warning */
  988. UNUSED(hpcd);
  989. /* NOTE : This function should not be modified, when the callback is needed,
  990. the HAL_PCD_SetupStageCallback could be implemented in the user file
  991. */
  992. }
  993. /**
  994. * @brief USB Start Of Frame callback.
  995. * @param hpcd PCD handle
  996. * @retval None
  997. */
  998. __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  999. {
  1000. /* Prevent unused argument(s) compilation warning */
  1001. UNUSED(hpcd);
  1002. /* NOTE : This function should not be modified, when the callback is needed,
  1003. the HAL_PCD_SOFCallback could be implemented in the user file
  1004. */
  1005. }
  1006. /**
  1007. * @brief USB Reset callback.
  1008. * @param hpcd PCD handle
  1009. * @retval None
  1010. */
  1011. __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  1012. {
  1013. /* Prevent unused argument(s) compilation warning */
  1014. UNUSED(hpcd);
  1015. /* NOTE : This function should not be modified, when the callback is needed,
  1016. the HAL_PCD_ResetCallback could be implemented in the user file
  1017. */
  1018. }
  1019. /**
  1020. * @brief Suspend event callback.
  1021. * @param hpcd PCD handle
  1022. * @retval None
  1023. */
  1024. __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  1025. {
  1026. /* Prevent unused argument(s) compilation warning */
  1027. UNUSED(hpcd);
  1028. /* NOTE : This function should not be modified, when the callback is needed,
  1029. the HAL_PCD_SuspendCallback could be implemented in the user file
  1030. */
  1031. }
  1032. /**
  1033. * @brief Resume event callback.
  1034. * @param hpcd PCD handle
  1035. * @retval None
  1036. */
  1037. __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  1038. {
  1039. /* Prevent unused argument(s) compilation warning */
  1040. UNUSED(hpcd);
  1041. /* NOTE : This function should not be modified, when the callback is needed,
  1042. the HAL_PCD_ResumeCallback could be implemented in the user file
  1043. */
  1044. }
  1045. /**
  1046. * @brief Incomplete ISO OUT callback.
  1047. * @param hpcd PCD handle
  1048. * @param epnum endpoint number
  1049. * @retval None
  1050. */
  1051. __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1052. {
  1053. /* Prevent unused argument(s) compilation warning */
  1054. UNUSED(hpcd);
  1055. UNUSED(epnum);
  1056. /* NOTE : This function should not be modified, when the callback is needed,
  1057. the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
  1058. */
  1059. }
  1060. /**
  1061. * @brief Incomplete ISO IN callback.
  1062. * @param hpcd PCD handle
  1063. * @param epnum endpoint number
  1064. * @retval None
  1065. */
  1066. __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1067. {
  1068. /* Prevent unused argument(s) compilation warning */
  1069. UNUSED(hpcd);
  1070. UNUSED(epnum);
  1071. /* NOTE : This function should not be modified, when the callback is needed,
  1072. the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
  1073. */
  1074. }
  1075. /**
  1076. * @brief Connection event callback.
  1077. * @param hpcd PCD handle
  1078. * @retval None
  1079. */
  1080. __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  1081. {
  1082. /* Prevent unused argument(s) compilation warning */
  1083. UNUSED(hpcd);
  1084. /* NOTE : This function should not be modified, when the callback is needed,
  1085. the HAL_PCD_ConnectCallback could be implemented in the user file
  1086. */
  1087. }
  1088. /**
  1089. * @brief Disconnection event callback.
  1090. * @param hpcd PCD handle
  1091. * @retval None
  1092. */
  1093. __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  1094. {
  1095. /* Prevent unused argument(s) compilation warning */
  1096. UNUSED(hpcd);
  1097. /* NOTE : This function should not be modified, when the callback is needed,
  1098. the HAL_PCD_DisconnectCallback could be implemented in the user file
  1099. */
  1100. }
  1101. /**
  1102. * @}
  1103. */
  1104. /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
  1105. * @brief management functions
  1106. *
  1107. @verbatim
  1108. ===============================================================================
  1109. ##### Peripheral Control functions #####
  1110. ===============================================================================
  1111. [..]
  1112. This subsection provides a set of functions allowing to control the PCD data
  1113. transfers.
  1114. @endverbatim
  1115. * @{
  1116. */
  1117. /**
  1118. * @brief Connect the USB device
  1119. * @param hpcd PCD handle
  1120. * @retval HAL status
  1121. */
  1122. HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
  1123. {
  1124. __HAL_LOCK(hpcd);
  1125. (void)USB_DevConnect(hpcd->Instance);
  1126. __HAL_UNLOCK(hpcd);
  1127. return HAL_OK;
  1128. }
  1129. /**
  1130. * @brief Disconnect the USB device.
  1131. * @param hpcd PCD handle
  1132. * @retval HAL status
  1133. */
  1134. HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
  1135. {
  1136. __HAL_LOCK(hpcd);
  1137. (void)USB_DevDisconnect(hpcd->Instance);
  1138. __HAL_UNLOCK(hpcd);
  1139. return HAL_OK;
  1140. }
  1141. /**
  1142. * @brief Set the USB Device address.
  1143. * @param hpcd PCD handle
  1144. * @param address new device address
  1145. * @retval HAL status
  1146. */
  1147. HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
  1148. {
  1149. __HAL_LOCK(hpcd);
  1150. hpcd->USB_Address = address;
  1151. (void)USB_SetDevAddress(hpcd->Instance, address);
  1152. __HAL_UNLOCK(hpcd);
  1153. return HAL_OK;
  1154. }
  1155. /**
  1156. * @brief Open and configure an endpoint.
  1157. * @param hpcd PCD handle
  1158. * @param ep_addr endpoint address
  1159. * @param ep_mps endpoint max packet size
  1160. * @param ep_type endpoint type
  1161. * @retval HAL status
  1162. */
  1163. HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
  1164. uint16_t ep_mps, uint8_t ep_type)
  1165. {
  1166. HAL_StatusTypeDef ret = HAL_OK;
  1167. PCD_EPTypeDef *ep;
  1168. if ((ep_addr & 0x80U) == 0x80U)
  1169. {
  1170. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1171. ep->is_in = 1U;
  1172. }
  1173. else
  1174. {
  1175. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1176. ep->is_in = 0U;
  1177. }
  1178. ep->num = ep_addr & EP_ADDR_MSK;
  1179. ep->maxpacket = ep_mps;
  1180. ep->type = ep_type;
  1181. /* Set initial data PID. */
  1182. if (ep_type == EP_TYPE_BULK)
  1183. {
  1184. ep->data_pid_start = 0U;
  1185. }
  1186. __HAL_LOCK(hpcd);
  1187. (void)USB_ActivateEndpoint(hpcd->Instance, ep);
  1188. __HAL_UNLOCK(hpcd);
  1189. return ret;
  1190. }
  1191. /**
  1192. * @brief Deactivate an endpoint.
  1193. * @param hpcd PCD handle
  1194. * @param ep_addr endpoint address
  1195. * @retval HAL status
  1196. */
  1197. HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1198. {
  1199. PCD_EPTypeDef *ep;
  1200. if ((ep_addr & 0x80U) == 0x80U)
  1201. {
  1202. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1203. ep->is_in = 1U;
  1204. }
  1205. else
  1206. {
  1207. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1208. ep->is_in = 0U;
  1209. }
  1210. ep->num = ep_addr & EP_ADDR_MSK;
  1211. __HAL_LOCK(hpcd);
  1212. (void)USB_DeactivateEndpoint(hpcd->Instance, ep);
  1213. __HAL_UNLOCK(hpcd);
  1214. return HAL_OK;
  1215. }
  1216. /**
  1217. * @brief Receive an amount of data.
  1218. * @param hpcd PCD handle
  1219. * @param ep_addr endpoint address
  1220. * @param pBuf pointer to the reception buffer
  1221. * @param len amount of data to be received
  1222. * @retval HAL status
  1223. */
  1224. HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  1225. {
  1226. PCD_EPTypeDef *ep;
  1227. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1228. /*setup and start the Xfer */
  1229. ep->xfer_buff = pBuf;
  1230. ep->xfer_len = len;
  1231. ep->xfer_count = 0U;
  1232. ep->is_in = 0U;
  1233. ep->num = ep_addr & EP_ADDR_MSK;
  1234. (void)USB_EPStartXfer(hpcd->Instance, ep);
  1235. return HAL_OK;
  1236. }
  1237. /**
  1238. * @brief Get Received Data Size
  1239. * @param hpcd PCD handle
  1240. * @param ep_addr endpoint address
  1241. * @retval Data Size
  1242. */
  1243. uint32_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef const *hpcd, uint8_t ep_addr)
  1244. {
  1245. return hpcd->OUT_ep[ep_addr & EP_ADDR_MSK].xfer_count;
  1246. }
  1247. /**
  1248. * @brief Send an amount of data
  1249. * @param hpcd PCD handle
  1250. * @param ep_addr endpoint address
  1251. * @param pBuf pointer to the transmission buffer
  1252. * @param len amount of data to be sent
  1253. * @retval HAL status
  1254. */
  1255. HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  1256. {
  1257. PCD_EPTypeDef *ep;
  1258. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1259. /*setup and start the Xfer */
  1260. ep->xfer_buff = pBuf;
  1261. ep->xfer_len = len;
  1262. ep->xfer_fill_db = 1U;
  1263. ep->xfer_len_db = len;
  1264. ep->xfer_count = 0U;
  1265. ep->is_in = 1U;
  1266. ep->num = ep_addr & EP_ADDR_MSK;
  1267. (void)USB_EPStartXfer(hpcd->Instance, ep);
  1268. return HAL_OK;
  1269. }
  1270. /**
  1271. * @brief Set a STALL condition over an endpoint
  1272. * @param hpcd PCD handle
  1273. * @param ep_addr endpoint address
  1274. * @retval HAL status
  1275. */
  1276. HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1277. {
  1278. PCD_EPTypeDef *ep;
  1279. if (((uint32_t)ep_addr & EP_ADDR_MSK) > hpcd->Init.dev_endpoints)
  1280. {
  1281. return HAL_ERROR;
  1282. }
  1283. if ((0x80U & ep_addr) == 0x80U)
  1284. {
  1285. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1286. ep->is_in = 1U;
  1287. }
  1288. else
  1289. {
  1290. ep = &hpcd->OUT_ep[ep_addr];
  1291. ep->is_in = 0U;
  1292. }
  1293. ep->is_stall = 1U;
  1294. ep->num = ep_addr & EP_ADDR_MSK;
  1295. __HAL_LOCK(hpcd);
  1296. (void)USB_EPSetStall(hpcd->Instance, ep);
  1297. __HAL_UNLOCK(hpcd);
  1298. return HAL_OK;
  1299. }
  1300. /**
  1301. * @brief Clear a STALL condition over in an endpoint
  1302. * @param hpcd PCD handle
  1303. * @param ep_addr endpoint address
  1304. * @retval HAL status
  1305. */
  1306. HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1307. {
  1308. PCD_EPTypeDef *ep;
  1309. if (((uint32_t)ep_addr & 0x0FU) > hpcd->Init.dev_endpoints)
  1310. {
  1311. return HAL_ERROR;
  1312. }
  1313. if ((0x80U & ep_addr) == 0x80U)
  1314. {
  1315. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1316. ep->is_in = 1U;
  1317. }
  1318. else
  1319. {
  1320. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1321. ep->is_in = 0U;
  1322. }
  1323. ep->is_stall = 0U;
  1324. ep->num = ep_addr & EP_ADDR_MSK;
  1325. __HAL_LOCK(hpcd);
  1326. (void)USB_EPClearStall(hpcd->Instance, ep);
  1327. __HAL_UNLOCK(hpcd);
  1328. return HAL_OK;
  1329. }
  1330. /**
  1331. * @brief Abort an USB EP transaction.
  1332. * @param hpcd PCD handle
  1333. * @param ep_addr endpoint address
  1334. * @retval HAL status
  1335. */
  1336. HAL_StatusTypeDef HAL_PCD_EP_Abort(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1337. {
  1338. HAL_StatusTypeDef ret;
  1339. PCD_EPTypeDef *ep;
  1340. if ((0x80U & ep_addr) == 0x80U)
  1341. {
  1342. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1343. }
  1344. else
  1345. {
  1346. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1347. }
  1348. /* Stop Xfer */
  1349. ret = USB_EPStopXfer(hpcd->Instance, ep);
  1350. return ret;
  1351. }
  1352. /**
  1353. * @brief Flush an endpoint
  1354. * @param hpcd PCD handle
  1355. * @param ep_addr endpoint address
  1356. * @retval HAL status
  1357. */
  1358. HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1359. {
  1360. __HAL_LOCK(hpcd);
  1361. if ((ep_addr & 0x80U) == 0x80U)
  1362. {
  1363. (void)USB_FlushTxFifo(hpcd->Instance, (uint32_t)ep_addr & EP_ADDR_MSK);
  1364. }
  1365. else
  1366. {
  1367. (void)USB_FlushRxFifo(hpcd->Instance);
  1368. }
  1369. __HAL_UNLOCK(hpcd);
  1370. return HAL_OK;
  1371. }
  1372. /**
  1373. * @brief Activate remote wakeup signalling
  1374. * @param hpcd PCD handle
  1375. * @retval HAL status
  1376. */
  1377. HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1378. {
  1379. return (USB_ActivateRemoteWakeup(hpcd->Instance));
  1380. }
  1381. /**
  1382. * @brief De-activate remote wakeup signalling.
  1383. * @param hpcd PCD handle
  1384. * @retval HAL status
  1385. */
  1386. HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1387. {
  1388. return (USB_DeActivateRemoteWakeup(hpcd->Instance));
  1389. }
  1390. /**
  1391. * @}
  1392. */
  1393. /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
  1394. * @brief Peripheral State functions
  1395. *
  1396. @verbatim
  1397. ===============================================================================
  1398. ##### Peripheral State functions #####
  1399. ===============================================================================
  1400. [..]
  1401. This subsection permits to get in run-time the status of the peripheral
  1402. and the data flow.
  1403. @endverbatim
  1404. * @{
  1405. */
  1406. /**
  1407. * @brief Return the PCD handle state.
  1408. * @param hpcd PCD handle
  1409. * @retval HAL state
  1410. */
  1411. PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef const *hpcd)
  1412. {
  1413. return hpcd->State;
  1414. }
  1415. /**
  1416. * @}
  1417. */
  1418. /**
  1419. * @}
  1420. */
  1421. /* Private functions ---------------------------------------------------------*/
  1422. /** @addtogroup PCD_Private_Functions
  1423. * @{
  1424. */
  1425. /**
  1426. * @brief This function handles PCD Endpoint interrupt request.
  1427. * @param hpcd PCD handle
  1428. * @retval HAL status
  1429. */
  1430. static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
  1431. {
  1432. PCD_EPTypeDef *ep;
  1433. uint16_t count;
  1434. uint16_t wIstr;
  1435. uint16_t wEPVal;
  1436. uint16_t TxPctSize;
  1437. uint8_t epindex;
  1438. #if (USE_USB_DOUBLE_BUFFER != 1U)
  1439. count = 0U;
  1440. #endif /* USE_USB_DOUBLE_BUFFER */
  1441. /* stay in loop while pending interrupts */
  1442. while ((hpcd->Instance->ISTR & USB_ISTR_CTR) != 0U)
  1443. {
  1444. wIstr = (uint16_t)hpcd->Instance->ISTR;
  1445. /* extract highest priority endpoint number */
  1446. epindex = (uint8_t)(wIstr & USB_ISTR_IDN);
  1447. if (epindex == 0U)
  1448. {
  1449. /* Decode and service control endpoint interrupt */
  1450. /* DIR bit = origin of the interrupt */
  1451. if ((wIstr & USB_ISTR_DIR) == 0U)
  1452. {
  1453. /* DIR = 0 */
  1454. /* DIR = 0 => IN int */
  1455. /* DIR = 0 implies that (EP_CTR_TX = 1) always */
  1456. PCD_CLEAR_TX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  1457. ep = &hpcd->IN_ep[0];
  1458. ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
  1459. ep->xfer_buff += ep->xfer_count;
  1460. /* TX COMPLETE */
  1461. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1462. hpcd->DataInStageCallback(hpcd, 0U);
  1463. #else
  1464. HAL_PCD_DataInStageCallback(hpcd, 0U);
  1465. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1466. if ((hpcd->USB_Address > 0U) && (ep->xfer_len == 0U))
  1467. {
  1468. hpcd->Instance->DADDR = ((uint16_t)hpcd->USB_Address | USB_DADDR_EF);
  1469. hpcd->USB_Address = 0U;
  1470. }
  1471. }
  1472. else
  1473. {
  1474. /* DIR = 1 */
  1475. /* DIR = 1 & CTR_RX => SETUP or OUT int */
  1476. /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
  1477. ep = &hpcd->OUT_ep[0];
  1478. wEPVal = (uint16_t)PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
  1479. if ((wEPVal & USB_EP_SETUP) != 0U)
  1480. {
  1481. /* Get SETUP Packet */
  1482. ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  1483. USB_ReadPMA(hpcd->Instance, (uint8_t *)hpcd->Setup,
  1484. ep->pmaadress, (uint16_t)ep->xfer_count);
  1485. /* SETUP bit kept frozen while CTR_RX = 1 */
  1486. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  1487. /* Process SETUP Packet*/
  1488. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1489. hpcd->SetupStageCallback(hpcd);
  1490. #else
  1491. HAL_PCD_SetupStageCallback(hpcd);
  1492. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1493. }
  1494. else if ((wEPVal & USB_EP_VTRX) != 0U)
  1495. {
  1496. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  1497. /* Get Control Data OUT Packet */
  1498. ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  1499. if ((ep->xfer_count != 0U) && (ep->xfer_buff != 0U))
  1500. {
  1501. USB_ReadPMA(hpcd->Instance, ep->xfer_buff,
  1502. ep->pmaadress, (uint16_t)ep->xfer_count);
  1503. ep->xfer_buff += ep->xfer_count;
  1504. /* Process Control Data OUT Packet */
  1505. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1506. hpcd->DataOutStageCallback(hpcd, 0U);
  1507. #else
  1508. HAL_PCD_DataOutStageCallback(hpcd, 0U);
  1509. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1510. }
  1511. wEPVal = (uint16_t)PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
  1512. if (((wEPVal & USB_EP_SETUP) == 0U) && ((wEPVal & USB_EP_RX_STRX) != USB_EP_RX_VALID))
  1513. {
  1514. PCD_SET_EP_RX_CNT(hpcd->Instance, PCD_ENDP0, ep->maxpacket);
  1515. PCD_SET_EP_RX_STATUS(hpcd->Instance, PCD_ENDP0, USB_EP_RX_VALID);
  1516. }
  1517. }
  1518. }
  1519. }
  1520. else
  1521. {
  1522. /* Decode and service non control endpoints interrupt */
  1523. /* process related endpoint register */
  1524. wEPVal = (uint16_t)PCD_GET_ENDPOINT(hpcd->Instance, epindex);
  1525. if ((wEPVal & USB_EP_VTRX) != 0U)
  1526. {
  1527. /* clear int flag */
  1528. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, epindex);
  1529. ep = &hpcd->OUT_ep[epindex];
  1530. /* OUT Single Buffering */
  1531. if (ep->doublebuffer == 0U)
  1532. {
  1533. count = (uint16_t)PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  1534. if (count != 0U)
  1535. {
  1536. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, count);
  1537. }
  1538. }
  1539. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1540. else
  1541. {
  1542. /* manage double buffer bulk out */
  1543. if (ep->type == EP_TYPE_BULK)
  1544. {
  1545. count = HAL_PCD_EP_DB_Receive(hpcd, ep, wEPVal);
  1546. }
  1547. else /* manage double buffer iso out */
  1548. {
  1549. /* free EP OUT Buffer */
  1550. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
  1551. if ((PCD_GET_ENDPOINT(hpcd->Instance, ep->num) & USB_EP_DTOG_RX) != 0U)
  1552. {
  1553. /* read from endpoint BUF0Addr buffer */
  1554. count = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  1555. if (count != 0U)
  1556. {
  1557. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
  1558. }
  1559. }
  1560. else
  1561. {
  1562. /* read from endpoint BUF1Addr buffer */
  1563. count = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
  1564. if (count != 0U)
  1565. {
  1566. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
  1567. }
  1568. }
  1569. }
  1570. }
  1571. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1572. /* multi-packet on the NON control OUT endpoint */
  1573. ep->xfer_count += count;
  1574. ep->xfer_buff += count;
  1575. if ((ep->xfer_len == 0U) || (count < ep->maxpacket))
  1576. {
  1577. /* RX COMPLETE */
  1578. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1579. hpcd->DataOutStageCallback(hpcd, ep->num);
  1580. #else
  1581. HAL_PCD_DataOutStageCallback(hpcd, ep->num);
  1582. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1583. }
  1584. else
  1585. {
  1586. (void)USB_EPStartXfer(hpcd->Instance, ep);
  1587. }
  1588. }
  1589. if ((wEPVal & USB_EP_VTTX) != 0U)
  1590. {
  1591. ep = &hpcd->IN_ep[epindex];
  1592. /* clear int flag */
  1593. PCD_CLEAR_TX_EP_CTR(hpcd->Instance, epindex);
  1594. if (ep->type == EP_TYPE_ISOC)
  1595. {
  1596. ep->xfer_len = 0U;
  1597. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1598. if (ep->doublebuffer != 0U)
  1599. {
  1600. if ((wEPVal & USB_EP_DTOG_TX) != 0U)
  1601. {
  1602. PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1603. }
  1604. else
  1605. {
  1606. PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1607. }
  1608. }
  1609. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1610. /* TX COMPLETE */
  1611. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1612. hpcd->DataInStageCallback(hpcd, ep->num);
  1613. #else
  1614. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  1615. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1616. }
  1617. else
  1618. {
  1619. /* Manage Single Buffer Transaction */
  1620. if ((wEPVal & USB_EP_KIND) == 0U)
  1621. {
  1622. /* multi-packet on the NON control IN endpoint */
  1623. TxPctSize = (uint16_t)PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
  1624. if (ep->xfer_len > TxPctSize)
  1625. {
  1626. ep->xfer_len -= TxPctSize;
  1627. }
  1628. else
  1629. {
  1630. ep->xfer_len = 0U;
  1631. }
  1632. /* Zero Length Packet? */
  1633. if (ep->xfer_len == 0U)
  1634. {
  1635. /* TX COMPLETE */
  1636. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1637. hpcd->DataInStageCallback(hpcd, ep->num);
  1638. #else
  1639. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  1640. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1641. }
  1642. else
  1643. {
  1644. /* Transfer is not yet Done */
  1645. ep->xfer_buff += TxPctSize;
  1646. ep->xfer_count += TxPctSize;
  1647. (void)USB_EPStartXfer(hpcd->Instance, ep);
  1648. }
  1649. }
  1650. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1651. /* Double Buffer bulk IN (bulk transfer Len > Ep_Mps) */
  1652. else
  1653. {
  1654. (void)HAL_PCD_EP_DB_Transmit(hpcd, ep, wEPVal);
  1655. }
  1656. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1657. }
  1658. }
  1659. }
  1660. }
  1661. return HAL_OK;
  1662. }
  1663. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1664. /**
  1665. * @brief Manage double buffer bulk out transaction from ISR
  1666. * @param hpcd PCD handle
  1667. * @param ep current endpoint handle
  1668. * @param wEPVal Last snapshot of EPRx register value taken in ISR
  1669. * @retval HAL status
  1670. */
  1671. static uint16_t HAL_PCD_EP_DB_Receive(PCD_HandleTypeDef *hpcd,
  1672. PCD_EPTypeDef *ep, uint16_t wEPVal)
  1673. {
  1674. uint16_t count;
  1675. /* Manage Buffer0 OUT */
  1676. if ((wEPVal & USB_EP_DTOG_RX) != 0U)
  1677. {
  1678. /* Get count of received Data on buffer0 */
  1679. count = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  1680. if (ep->xfer_len >= count)
  1681. {
  1682. ep->xfer_len -= count;
  1683. }
  1684. else
  1685. {
  1686. ep->xfer_len = 0U;
  1687. }
  1688. if (ep->xfer_len == 0U)
  1689. {
  1690. /* set NAK to OUT endpoint since double buffer is enabled */
  1691. PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_NAK);
  1692. }
  1693. /* Check if Buffer1 is in blocked state which requires to toggle */
  1694. if ((wEPVal & USB_EP_DTOG_TX) != 0U)
  1695. {
  1696. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
  1697. }
  1698. if (count != 0U)
  1699. {
  1700. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
  1701. }
  1702. }
  1703. /* Manage Buffer 1 DTOG_RX=0 */
  1704. else
  1705. {
  1706. /* Get count of received data */
  1707. count = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
  1708. if (ep->xfer_len >= count)
  1709. {
  1710. ep->xfer_len -= count;
  1711. }
  1712. else
  1713. {
  1714. ep->xfer_len = 0U;
  1715. }
  1716. if (ep->xfer_len == 0U)
  1717. {
  1718. /* set NAK on the current endpoint */
  1719. PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_NAK);
  1720. }
  1721. /*Need to FreeUser Buffer*/
  1722. if ((wEPVal & USB_EP_DTOG_TX) == 0U)
  1723. {
  1724. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
  1725. }
  1726. if (count != 0U)
  1727. {
  1728. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
  1729. }
  1730. }
  1731. return count;
  1732. }
  1733. /**
  1734. * @brief Manage double buffer bulk IN transaction from ISR
  1735. * @param hpcd PCD handle
  1736. * @param ep current endpoint handle
  1737. * @param wEPVal Last snapshot of EPRx register value taken in ISR
  1738. * @retval HAL status
  1739. */
  1740. static HAL_StatusTypeDef HAL_PCD_EP_DB_Transmit(PCD_HandleTypeDef *hpcd,
  1741. PCD_EPTypeDef *ep, uint16_t wEPVal)
  1742. {
  1743. uint32_t len;
  1744. uint16_t TxPctSize;
  1745. /* Data Buffer0 ACK received */
  1746. if ((wEPVal & USB_EP_DTOG_TX) != 0U)
  1747. {
  1748. /* multi-packet on the NON control IN endpoint */
  1749. TxPctSize = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  1750. if (ep->xfer_len > TxPctSize)
  1751. {
  1752. ep->xfer_len -= TxPctSize;
  1753. }
  1754. else
  1755. {
  1756. ep->xfer_len = 0U;
  1757. }
  1758. /* Transfer is completed */
  1759. if (ep->xfer_len == 0U)
  1760. {
  1761. PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1762. PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1763. /* TX COMPLETE */
  1764. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1765. hpcd->DataInStageCallback(hpcd, ep->num);
  1766. #else
  1767. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  1768. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1769. if ((wEPVal & USB_EP_DTOG_RX) != 0U)
  1770. {
  1771. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
  1772. }
  1773. }
  1774. else /* Transfer is not yet Done */
  1775. {
  1776. /* need to Free USB Buff */
  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 buffer1 */
  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. /* TX COMPLETE */
  1829. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1830. hpcd->DataInStageCallback(hpcd, ep->num);
  1831. #else
  1832. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  1833. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1834. /* need to Free USB Buff */
  1835. if ((wEPVal & USB_EP_DTOG_RX) == 0U)
  1836. {
  1837. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
  1838. }
  1839. }
  1840. else /* Transfer is not yet Done */
  1841. {
  1842. /* need to Free USB Buff */
  1843. if ((wEPVal & USB_EP_DTOG_RX) == 0U)
  1844. {
  1845. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
  1846. }
  1847. /* Still there is data to Fill in the next Buffer */
  1848. if (ep->xfer_fill_db == 1U)
  1849. {
  1850. ep->xfer_buff += TxPctSize;
  1851. ep->xfer_count += TxPctSize;
  1852. /* Calculate the len of the new buffer to fill */
  1853. if (ep->xfer_len_db >= ep->maxpacket)
  1854. {
  1855. len = ep->maxpacket;
  1856. ep->xfer_len_db -= len;
  1857. }
  1858. else if (ep->xfer_len_db == 0U)
  1859. {
  1860. len = TxPctSize;
  1861. ep->xfer_fill_db = 0U;
  1862. }
  1863. else
  1864. {
  1865. len = ep->xfer_len_db;
  1866. ep->xfer_len_db = 0U;
  1867. ep->xfer_fill_db = 0;
  1868. }
  1869. /* Set the Double buffer counter for pmabuffer1 */
  1870. PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, len);
  1871. /* Copy the user buffer to USB PMA */
  1872. USB_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, (uint16_t)len);
  1873. }
  1874. }
  1875. }
  1876. /*enable endpoint IN*/
  1877. PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_VALID);
  1878. return HAL_OK;
  1879. }
  1880. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1881. /**
  1882. * @}
  1883. */
  1884. #endif /* defined (USB_DRD_FS) */
  1885. #endif /* HAL_PCD_MODULE_ENABLED */
  1886. /**
  1887. * @}
  1888. */
  1889. /**
  1890. * @}
  1891. */