stm32f0xx_hal_pcd.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_pcd.c
  4. * @author MCD Application Team
  5. * @brief PCD HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the USB Peripheral Controller:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2016 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. The PCD HAL driver can be used as follows:
  30. (#) Declare a PCD_HandleTypeDef handle structure, for example:
  31. PCD_HandleTypeDef hpcd;
  32. (#) Fill parameters of Init structure in HCD handle
  33. (#) Call HAL_PCD_Init() API to initialize the PCD peripheral (Core, Device core, ...)
  34. (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
  35. (##) Enable the PCD/USB Low Level interface clock using
  36. (+++) __HAL_RCC_USB_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 "stm32f0xx_hal.h"
  49. /** @addtogroup STM32F0xx_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 = ep_mps;
  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 *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. /* Prevent unused argument(s) compilation warning */
  1356. UNUSED(hpcd);
  1357. UNUSED(ep_addr);
  1358. return HAL_OK;
  1359. }
  1360. /**
  1361. * @brief Activate remote wakeup signalling
  1362. * @param hpcd PCD handle
  1363. * @retval HAL status
  1364. */
  1365. HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1366. {
  1367. return (USB_ActivateRemoteWakeup(hpcd->Instance));
  1368. }
  1369. /**
  1370. * @brief De-activate remote wakeup signalling.
  1371. * @param hpcd PCD handle
  1372. * @retval HAL status
  1373. */
  1374. HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1375. {
  1376. return (USB_DeActivateRemoteWakeup(hpcd->Instance));
  1377. }
  1378. /**
  1379. * @}
  1380. */
  1381. /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
  1382. * @brief Peripheral State functions
  1383. *
  1384. @verbatim
  1385. ===============================================================================
  1386. ##### Peripheral State functions #####
  1387. ===============================================================================
  1388. [..]
  1389. This subsection permits to get in run-time the status of the peripheral
  1390. and the data flow.
  1391. @endverbatim
  1392. * @{
  1393. */
  1394. /**
  1395. * @brief Return the PCD handle state.
  1396. * @param hpcd PCD handle
  1397. * @retval HAL state
  1398. */
  1399. PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
  1400. {
  1401. return hpcd->State;
  1402. }
  1403. /**
  1404. * @}
  1405. */
  1406. /**
  1407. * @}
  1408. */
  1409. /* Private functions ---------------------------------------------------------*/
  1410. /** @addtogroup PCD_Private_Functions
  1411. * @{
  1412. */
  1413. /**
  1414. * @brief This function handles PCD Endpoint interrupt request.
  1415. * @param hpcd PCD handle
  1416. * @retval HAL status
  1417. */
  1418. static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
  1419. {
  1420. PCD_EPTypeDef *ep;
  1421. uint16_t count;
  1422. uint16_t wIstr;
  1423. uint16_t wEPVal;
  1424. uint16_t TxPctSize;
  1425. uint8_t epindex;
  1426. #if (USE_USB_DOUBLE_BUFFER != 1U)
  1427. count = 0U;
  1428. #endif /* USE_USB_DOUBLE_BUFFER */
  1429. /* stay in loop while pending interrupts */
  1430. while ((hpcd->Instance->ISTR & USB_ISTR_CTR) != 0U)
  1431. {
  1432. wIstr = hpcd->Instance->ISTR;
  1433. /* extract highest priority endpoint number */
  1434. epindex = (uint8_t)(wIstr & USB_ISTR_EP_ID);
  1435. if (epindex == 0U)
  1436. {
  1437. /* Decode and service control endpoint interrupt */
  1438. /* DIR bit = origin of the interrupt */
  1439. if ((wIstr & USB_ISTR_DIR) == 0U)
  1440. {
  1441. /* DIR = 0 */
  1442. /* DIR = 0 => IN int */
  1443. /* DIR = 0 implies that (EP_CTR_TX = 1) always */
  1444. PCD_CLEAR_TX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  1445. ep = &hpcd->IN_ep[0];
  1446. ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
  1447. ep->xfer_buff += ep->xfer_count;
  1448. /* TX COMPLETE */
  1449. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1450. hpcd->DataInStageCallback(hpcd, 0U);
  1451. #else
  1452. HAL_PCD_DataInStageCallback(hpcd, 0U);
  1453. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1454. if ((hpcd->USB_Address > 0U) && (ep->xfer_len == 0U))
  1455. {
  1456. hpcd->Instance->DADDR = ((uint16_t)hpcd->USB_Address | USB_DADDR_EF);
  1457. hpcd->USB_Address = 0U;
  1458. }
  1459. }
  1460. else
  1461. {
  1462. /* DIR = 1 */
  1463. /* DIR = 1 & CTR_RX => SETUP or OUT int */
  1464. /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
  1465. ep = &hpcd->OUT_ep[0];
  1466. wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
  1467. if ((wEPVal & USB_EP_SETUP) != 0U)
  1468. {
  1469. /* Get SETUP Packet */
  1470. ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  1471. USB_ReadPMA(hpcd->Instance, (uint8_t *)hpcd->Setup,
  1472. ep->pmaadress, (uint16_t)ep->xfer_count);
  1473. /* SETUP bit kept frozen while CTR_RX = 1 */
  1474. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  1475. /* Process SETUP Packet*/
  1476. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1477. hpcd->SetupStageCallback(hpcd);
  1478. #else
  1479. HAL_PCD_SetupStageCallback(hpcd);
  1480. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1481. }
  1482. else if ((wEPVal & USB_EP_CTR_RX) != 0U)
  1483. {
  1484. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  1485. /* Get Control Data OUT Packet */
  1486. ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  1487. if ((ep->xfer_count != 0U) && (ep->xfer_buff != 0U))
  1488. {
  1489. USB_ReadPMA(hpcd->Instance, ep->xfer_buff,
  1490. ep->pmaadress, (uint16_t)ep->xfer_count);
  1491. ep->xfer_buff += ep->xfer_count;
  1492. /* Process Control Data OUT Packet */
  1493. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1494. hpcd->DataOutStageCallback(hpcd, 0U);
  1495. #else
  1496. HAL_PCD_DataOutStageCallback(hpcd, 0U);
  1497. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1498. }
  1499. wEPVal = (uint16_t)PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
  1500. if (((wEPVal & USB_EP_SETUP) == 0U) && ((wEPVal & USB_EP_RX_STRX) != USB_EP_RX_VALID))
  1501. {
  1502. PCD_SET_EP_RX_CNT(hpcd->Instance, PCD_ENDP0, ep->maxpacket);
  1503. PCD_SET_EP_RX_STATUS(hpcd->Instance, PCD_ENDP0, USB_EP_RX_VALID);
  1504. }
  1505. }
  1506. }
  1507. }
  1508. else
  1509. {
  1510. /* Decode and service non control endpoints interrupt */
  1511. /* process related endpoint register */
  1512. wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, epindex);
  1513. if ((wEPVal & USB_EP_CTR_RX) != 0U)
  1514. {
  1515. /* clear int flag */
  1516. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, epindex);
  1517. ep = &hpcd->OUT_ep[epindex];
  1518. /* OUT Single Buffering */
  1519. if (ep->doublebuffer == 0U)
  1520. {
  1521. count = (uint16_t)PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  1522. if (count != 0U)
  1523. {
  1524. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, count);
  1525. }
  1526. }
  1527. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1528. else
  1529. {
  1530. /* manage double buffer bulk out */
  1531. if (ep->type == EP_TYPE_BULK)
  1532. {
  1533. count = HAL_PCD_EP_DB_Receive(hpcd, ep, wEPVal);
  1534. }
  1535. else /* manage double buffer iso out */
  1536. {
  1537. /* free EP OUT Buffer */
  1538. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
  1539. if ((PCD_GET_ENDPOINT(hpcd->Instance, ep->num) & USB_EP_DTOG_RX) != 0U)
  1540. {
  1541. /* read from endpoint BUF0Addr buffer */
  1542. count = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  1543. if (count != 0U)
  1544. {
  1545. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
  1546. }
  1547. }
  1548. else
  1549. {
  1550. /* read from endpoint BUF1Addr buffer */
  1551. count = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
  1552. if (count != 0U)
  1553. {
  1554. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
  1555. }
  1556. }
  1557. }
  1558. }
  1559. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1560. /* multi-packet on the NON control OUT endpoint */
  1561. ep->xfer_count += count;
  1562. ep->xfer_buff += count;
  1563. if ((ep->xfer_len == 0U) || (count < ep->maxpacket))
  1564. {
  1565. /* RX COMPLETE */
  1566. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1567. hpcd->DataOutStageCallback(hpcd, ep->num);
  1568. #else
  1569. HAL_PCD_DataOutStageCallback(hpcd, ep->num);
  1570. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1571. }
  1572. else
  1573. {
  1574. (void)USB_EPStartXfer(hpcd->Instance, ep);
  1575. }
  1576. }
  1577. if ((wEPVal & USB_EP_CTR_TX) != 0U)
  1578. {
  1579. ep = &hpcd->IN_ep[epindex];
  1580. /* clear int flag */
  1581. PCD_CLEAR_TX_EP_CTR(hpcd->Instance, epindex);
  1582. if (ep->type == EP_TYPE_ISOC)
  1583. {
  1584. ep->xfer_len = 0U;
  1585. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1586. if (ep->doublebuffer != 0U)
  1587. {
  1588. if ((wEPVal & USB_EP_DTOG_TX) != 0U)
  1589. {
  1590. PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1591. }
  1592. else
  1593. {
  1594. PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1595. }
  1596. }
  1597. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1598. /* TX COMPLETE */
  1599. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1600. hpcd->DataInStageCallback(hpcd, ep->num);
  1601. #else
  1602. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  1603. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1604. }
  1605. else
  1606. {
  1607. /* Manage Single Buffer Transaction */
  1608. if ((wEPVal & USB_EP_KIND) == 0U)
  1609. {
  1610. /* multi-packet on the NON control IN endpoint */
  1611. TxPctSize = (uint16_t)PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
  1612. if (ep->xfer_len > TxPctSize)
  1613. {
  1614. ep->xfer_len -= TxPctSize;
  1615. }
  1616. else
  1617. {
  1618. ep->xfer_len = 0U;
  1619. }
  1620. /* Zero Length Packet? */
  1621. if (ep->xfer_len == 0U)
  1622. {
  1623. /* TX COMPLETE */
  1624. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1625. hpcd->DataInStageCallback(hpcd, ep->num);
  1626. #else
  1627. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  1628. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1629. }
  1630. else
  1631. {
  1632. /* Transfer is not yet Done */
  1633. ep->xfer_buff += TxPctSize;
  1634. ep->xfer_count += TxPctSize;
  1635. (void)USB_EPStartXfer(hpcd->Instance, ep);
  1636. }
  1637. }
  1638. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1639. /* Double Buffer bulk IN (bulk transfer Len > Ep_Mps) */
  1640. else
  1641. {
  1642. (void)HAL_PCD_EP_DB_Transmit(hpcd, ep, wEPVal);
  1643. }
  1644. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1645. }
  1646. }
  1647. }
  1648. }
  1649. return HAL_OK;
  1650. }
  1651. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1652. /**
  1653. * @brief Manage double buffer bulk out transaction from ISR
  1654. * @param hpcd PCD handle
  1655. * @param ep current endpoint handle
  1656. * @param wEPVal Last snapshot of EPRx register value taken in ISR
  1657. * @retval HAL status
  1658. */
  1659. static uint16_t HAL_PCD_EP_DB_Receive(PCD_HandleTypeDef *hpcd,
  1660. PCD_EPTypeDef *ep, uint16_t wEPVal)
  1661. {
  1662. uint16_t count;
  1663. /* Manage Buffer0 OUT */
  1664. if ((wEPVal & USB_EP_DTOG_RX) != 0U)
  1665. {
  1666. /* Get count of received Data on buffer0 */
  1667. count = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  1668. if (ep->xfer_len >= count)
  1669. {
  1670. ep->xfer_len -= count;
  1671. }
  1672. else
  1673. {
  1674. ep->xfer_len = 0U;
  1675. }
  1676. if (ep->xfer_len == 0U)
  1677. {
  1678. /* set NAK to OUT endpoint since double buffer is enabled */
  1679. PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_NAK);
  1680. }
  1681. /* Check if Buffer1 is in blocked state which requires to toggle */
  1682. if ((wEPVal & USB_EP_DTOG_TX) != 0U)
  1683. {
  1684. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
  1685. }
  1686. if (count != 0U)
  1687. {
  1688. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
  1689. }
  1690. }
  1691. /* Manage Buffer 1 DTOG_RX=0 */
  1692. else
  1693. {
  1694. /* Get count of received data */
  1695. count = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
  1696. if (ep->xfer_len >= count)
  1697. {
  1698. ep->xfer_len -= count;
  1699. }
  1700. else
  1701. {
  1702. ep->xfer_len = 0U;
  1703. }
  1704. if (ep->xfer_len == 0U)
  1705. {
  1706. /* set NAK on the current endpoint */
  1707. PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_NAK);
  1708. }
  1709. /*Need to FreeUser Buffer*/
  1710. if ((wEPVal & USB_EP_DTOG_TX) == 0U)
  1711. {
  1712. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
  1713. }
  1714. if (count != 0U)
  1715. {
  1716. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
  1717. }
  1718. }
  1719. return count;
  1720. }
  1721. /**
  1722. * @brief Manage double buffer bulk IN transaction from ISR
  1723. * @param hpcd PCD handle
  1724. * @param ep current endpoint handle
  1725. * @param wEPVal Last snapshot of EPRx register value taken in ISR
  1726. * @retval HAL status
  1727. */
  1728. static HAL_StatusTypeDef HAL_PCD_EP_DB_Transmit(PCD_HandleTypeDef *hpcd,
  1729. PCD_EPTypeDef *ep, uint16_t wEPVal)
  1730. {
  1731. uint32_t len;
  1732. uint16_t TxPctSize;
  1733. /* Data Buffer0 ACK received */
  1734. if ((wEPVal & USB_EP_DTOG_TX) != 0U)
  1735. {
  1736. /* multi-packet on the NON control IN endpoint */
  1737. TxPctSize = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  1738. if (ep->xfer_len > TxPctSize)
  1739. {
  1740. ep->xfer_len -= TxPctSize;
  1741. }
  1742. else
  1743. {
  1744. ep->xfer_len = 0U;
  1745. }
  1746. /* Transfer is completed */
  1747. if (ep->xfer_len == 0U)
  1748. {
  1749. PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1750. PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1751. /* TX COMPLETE */
  1752. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1753. hpcd->DataInStageCallback(hpcd, ep->num);
  1754. #else
  1755. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  1756. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1757. if ((wEPVal & USB_EP_DTOG_RX) != 0U)
  1758. {
  1759. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
  1760. }
  1761. }
  1762. else /* Transfer is not yet Done */
  1763. {
  1764. /* need to Free USB Buff */
  1765. if ((wEPVal & USB_EP_DTOG_RX) != 0U)
  1766. {
  1767. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
  1768. }
  1769. /* Still there is data to Fill in the next Buffer */
  1770. if (ep->xfer_fill_db == 1U)
  1771. {
  1772. ep->xfer_buff += TxPctSize;
  1773. ep->xfer_count += TxPctSize;
  1774. /* Calculate the len of the new buffer to fill */
  1775. if (ep->xfer_len_db >= ep->maxpacket)
  1776. {
  1777. len = ep->maxpacket;
  1778. ep->xfer_len_db -= len;
  1779. }
  1780. else if (ep->xfer_len_db == 0U)
  1781. {
  1782. len = TxPctSize;
  1783. ep->xfer_fill_db = 0U;
  1784. }
  1785. else
  1786. {
  1787. ep->xfer_fill_db = 0U;
  1788. len = ep->xfer_len_db;
  1789. ep->xfer_len_db = 0U;
  1790. }
  1791. /* Write remaining Data to Buffer */
  1792. /* Set the Double buffer counter for pma buffer1 */
  1793. PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, len);
  1794. /* Copy user buffer to USB PMA */
  1795. USB_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, (uint16_t)len);
  1796. }
  1797. }
  1798. }
  1799. else /* Data Buffer1 ACK received */
  1800. {
  1801. /* multi-packet on the NON control IN endpoint */
  1802. TxPctSize = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
  1803. if (ep->xfer_len >= TxPctSize)
  1804. {
  1805. ep->xfer_len -= TxPctSize;
  1806. }
  1807. else
  1808. {
  1809. ep->xfer_len = 0U;
  1810. }
  1811. /* Transfer is completed */
  1812. if (ep->xfer_len == 0U)
  1813. {
  1814. PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1815. PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
  1816. /* TX COMPLETE */
  1817. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1818. hpcd->DataInStageCallback(hpcd, ep->num);
  1819. #else
  1820. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  1821. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1822. /* need to Free USB Buff */
  1823. if ((wEPVal & USB_EP_DTOG_RX) == 0U)
  1824. {
  1825. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
  1826. }
  1827. }
  1828. else /* Transfer is not yet Done */
  1829. {
  1830. /* need to Free USB Buff */
  1831. if ((wEPVal & USB_EP_DTOG_RX) == 0U)
  1832. {
  1833. PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
  1834. }
  1835. /* Still there is data to Fill in the next Buffer */
  1836. if (ep->xfer_fill_db == 1U)
  1837. {
  1838. ep->xfer_buff += TxPctSize;
  1839. ep->xfer_count += TxPctSize;
  1840. /* Calculate the len of the new buffer to fill */
  1841. if (ep->xfer_len_db >= ep->maxpacket)
  1842. {
  1843. len = ep->maxpacket;
  1844. ep->xfer_len_db -= len;
  1845. }
  1846. else if (ep->xfer_len_db == 0U)
  1847. {
  1848. len = TxPctSize;
  1849. ep->xfer_fill_db = 0U;
  1850. }
  1851. else
  1852. {
  1853. len = ep->xfer_len_db;
  1854. ep->xfer_len_db = 0U;
  1855. ep->xfer_fill_db = 0;
  1856. }
  1857. /* Set the Double buffer counter for pmabuffer1 */
  1858. PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, len);
  1859. /* Copy the user buffer to USB PMA */
  1860. USB_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, (uint16_t)len);
  1861. }
  1862. }
  1863. }
  1864. /*enable endpoint IN*/
  1865. PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_VALID);
  1866. return HAL_OK;
  1867. }
  1868. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1869. /**
  1870. * @}
  1871. */
  1872. #endif /* defined (USB) */
  1873. #endif /* HAL_PCD_MODULE_ENABLED */
  1874. /**
  1875. * @}
  1876. */
  1877. /**
  1878. * @}
  1879. */