stm32f1xx_hal_pcd.c 58 KB

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