stm32f1xx_hal_pcd.c 71 KB

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