stm32f3xx_hal_pcd.c 52 KB

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