stm32f1xx_hal_pcd.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_pcd.c
  4. * @author MCD Application Team
  5. * @version V1.0.4
  6. * @date 29-April-2016
  7. * @brief PCD HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the USB Peripheral Controller:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral Control functions
  13. * + Peripheral State functions
  14. *
  15. @verbatim
  16. ==============================================================================
  17. ##### How to use this driver #####
  18. ==============================================================================
  19. [..]
  20. The PCD HAL driver can be used as follows:
  21. (#) Declare a PCD_HandleTypeDef handle structure, for example:
  22. PCD_HandleTypeDef hpcd;
  23. (#) Fill parameters of Init structure in HCD handle
  24. (#) Call HAL_PCD_Init() API to initialize the HCD peripheral (Core, Device core, ...)
  25. (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
  26. (##) Enable the PCD/USB Low Level interface clock using the following macro
  27. (+++) __HAL_RCC_USB_CLK_ENABLE(); For USB Device FS peripheral available
  28. on STM32F102xx and STM32F103xx devices
  29. (+++) __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); For USB OTG FS peripheral available
  30. on STM32F105xx and STM32F107xx devices
  31. (##) Initialize the related GPIO clocks
  32. (##) Configure PCD pin-out
  33. (##) Configure PCD NVIC interrupt
  34. (#)Associate the Upper USB device stack to the HAL PCD Driver:
  35. (##) hpcd.pData = pdev;
  36. (#)Enable HCD transmission and reception:
  37. (##) HAL_PCD_Start();
  38. @endverbatim
  39. ******************************************************************************
  40. * @attention
  41. *
  42. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  43. *
  44. * Redistribution and use in source and binary forms, with or without modification,
  45. * are permitted provided that the following conditions are met:
  46. * 1. Redistributions of source code must retain the above copyright notice,
  47. * this list of conditions and the following disclaimer.
  48. * 2. Redistributions in binary form must reproduce the above copyright notice,
  49. * this list of conditions and the following disclaimer in the documentation
  50. * and/or other materials provided with the distribution.
  51. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  52. * may be used to endorse or promote products derived from this software
  53. * without specific prior written permission.
  54. *
  55. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  56. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  57. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  58. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  59. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  60. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  61. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  62. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  63. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. *
  66. ******************************************************************************
  67. */
  68. /* Includes ------------------------------------------------------------------*/
  69. #include "stm32f1xx_hal.h"
  70. /** @addtogroup STM32F1xx_HAL_Driver
  71. * @{
  72. */
  73. #ifdef HAL_PCD_MODULE_ENABLED
  74. #if defined(STM32F102x6) || defined(STM32F102xB) || \
  75. defined(STM32F103x6) || defined(STM32F103xB) || \
  76. defined(STM32F103xE) || defined(STM32F103xG) || \
  77. defined(STM32F105xC) || defined(STM32F107xC)
  78. /** @defgroup PCD PCD
  79. * @brief PCD HAL module driver
  80. * @{
  81. */
  82. /* Private types -------------------------------------------------------------*/
  83. /* Private variables ---------------------------------------------------------*/
  84. /* Private constants ---------------------------------------------------------*/
  85. /* Private macros ------------------------------------------------------------*/
  86. /** @defgroup PCD_Private_Macros PCD Private Macros
  87. * @{
  88. */
  89. #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
  90. #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
  91. /**
  92. * @}
  93. */
  94. /* Private functions ---------------------------------------------------------*/
  95. /** @defgroup PCD_Private_Functions PCD Private Functions
  96. * @{
  97. */
  98. #if defined (USB_OTG_FS)
  99. static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
  100. #endif /* USB_OTG_FS */
  101. #if defined (USB)
  102. static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd);
  103. #endif /* USB */
  104. /**
  105. * @}
  106. */
  107. /* Exported functions --------------------------------------------------------*/
  108. /** @defgroup PCD_Exported_Functions PCD Exported Functions
  109. * @{
  110. */
  111. /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
  112. * @brief Initialization and Configuration functions
  113. *
  114. @verbatim
  115. ===============================================================================
  116. ##### Initialization and de-initialization functions #####
  117. ===============================================================================
  118. [..] This section provides functions allowing to:
  119. @endverbatim
  120. * @{
  121. */
  122. /**
  123. * @brief Initializes the PCD according to the specified
  124. * parameters in the PCD_InitTypeDef and create the associated handle.
  125. * @param hpcd: PCD handle
  126. * @retval HAL status
  127. */
  128. HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
  129. {
  130. uint32_t index = 0;
  131. /* Check the PCD handle allocation */
  132. if(hpcd == NULL)
  133. {
  134. return HAL_ERROR;
  135. }
  136. /* Check the parameters */
  137. assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
  138. if(hpcd->State == HAL_PCD_STATE_RESET)
  139. {
  140. /* Allocate lock resource and initialize it */
  141. hpcd->Lock = HAL_UNLOCKED;
  142. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  143. HAL_PCD_MspInit(hpcd);
  144. }
  145. hpcd->State = HAL_PCD_STATE_BUSY;
  146. /* Disable the Interrupts */
  147. __HAL_PCD_DISABLE(hpcd);
  148. /*Init the Core (common init.) */
  149. USB_CoreInit(hpcd->Instance, hpcd->Init);
  150. /* Force Device Mode*/
  151. USB_SetCurrentMode(hpcd->Instance , USB_DEVICE_MODE);
  152. /* Init endpoints structures */
  153. for (index = 0; index < 15 ; index++)
  154. {
  155. /* Init ep structure */
  156. hpcd->IN_ep[index].is_in = 1;
  157. hpcd->IN_ep[index].num = index;
  158. hpcd->IN_ep[index].tx_fifo_num = index;
  159. /* Control until ep is actvated */
  160. hpcd->IN_ep[index].type = EP_TYPE_CTRL;
  161. hpcd->IN_ep[index].maxpacket = 0;
  162. hpcd->IN_ep[index].xfer_buff = 0;
  163. hpcd->IN_ep[index].xfer_len = 0;
  164. }
  165. for (index = 0; index < 15 ; index++)
  166. {
  167. hpcd->OUT_ep[index].is_in = 0;
  168. hpcd->OUT_ep[index].num = index;
  169. hpcd->IN_ep[index].tx_fifo_num = index;
  170. /* Control until ep is activated */
  171. hpcd->OUT_ep[index].type = EP_TYPE_CTRL;
  172. hpcd->OUT_ep[index].maxpacket = 0;
  173. hpcd->OUT_ep[index].xfer_buff = 0;
  174. hpcd->OUT_ep[index].xfer_len = 0;
  175. }
  176. /* Init Device */
  177. USB_DevInit(hpcd->Instance, hpcd->Init);
  178. hpcd->USB_Address = 0;
  179. hpcd->State= HAL_PCD_STATE_READY;
  180. USB_DevDisconnect (hpcd->Instance);
  181. return HAL_OK;
  182. }
  183. /**
  184. * @brief DeInitializes the PCD peripheral
  185. * @param hpcd: PCD handle
  186. * @retval HAL status
  187. */
  188. HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
  189. {
  190. /* Check the PCD handle allocation */
  191. if(hpcd == NULL)
  192. {
  193. return HAL_ERROR;
  194. }
  195. hpcd->State = HAL_PCD_STATE_BUSY;
  196. /* Stop Device */
  197. HAL_PCD_Stop(hpcd);
  198. /* DeInit the low level hardware */
  199. HAL_PCD_MspDeInit(hpcd);
  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. /**
  230. * @}
  231. */
  232. /** @defgroup PCD_Exported_Functions_Group2 IO operation functions
  233. * @brief Data transfers functions
  234. *
  235. @verbatim
  236. ===============================================================================
  237. ##### IO operation functions #####
  238. ===============================================================================
  239. [..]
  240. This subsection provides a set of functions allowing to manage the PCD data
  241. transfers.
  242. @endverbatim
  243. * @{
  244. */
  245. /**
  246. * @brief Start The USB Device.
  247. * @param hpcd: PCD handle
  248. * @retval HAL status
  249. */
  250. HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
  251. {
  252. __HAL_LOCK(hpcd);
  253. HAL_PCDEx_SetConnectionState (hpcd, 1);
  254. USB_DevConnect (hpcd->Instance);
  255. __HAL_PCD_ENABLE(hpcd);
  256. __HAL_UNLOCK(hpcd);
  257. return HAL_OK;
  258. }
  259. /**
  260. * @brief Stop The USB Device.
  261. * @param hpcd: PCD handle
  262. * @retval HAL status
  263. */
  264. HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
  265. {
  266. __HAL_LOCK(hpcd);
  267. __HAL_PCD_DISABLE(hpcd);
  268. USB_StopDevice(hpcd->Instance);
  269. USB_DevDisconnect (hpcd->Instance);
  270. __HAL_UNLOCK(hpcd);
  271. return HAL_OK;
  272. }
  273. #if defined (USB_OTG_FS)
  274. /**
  275. * @brief This function handles PCD interrupt request.
  276. * @param hpcd: PCD handle
  277. * @retval HAL status
  278. */
  279. void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
  280. {
  281. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  282. uint32_t index = 0, ep_intr = 0, epint = 0, epnum = 0;
  283. uint32_t fifoemptymsk = 0, temp = 0;
  284. USB_OTG_EPTypeDef *ep = NULL;
  285. /* ensure that we are in device mode */
  286. if (USB_GetMode(hpcd->Instance) == USB_OTG_MODE_DEVICE)
  287. {
  288. /* avoid spurious interrupt */
  289. if(__HAL_PCD_IS_INVALID_INTERRUPT(hpcd))
  290. {
  291. return;
  292. }
  293. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_MMIS))
  294. {
  295. /* incorrect mode, acknowledge the interrupt */
  296. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_MMIS);
  297. }
  298. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OEPINT))
  299. {
  300. epnum = 0;
  301. /* Read in the device interrupt bits */
  302. ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
  303. while ( ep_intr )
  304. {
  305. if (ep_intr & 0x1)
  306. {
  307. epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, epnum);
  308. if(( epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC)
  309. {
  310. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
  311. HAL_PCD_DataOutStageCallback(hpcd, epnum);
  312. }
  313. if(( epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP)
  314. {
  315. /* Inform the upper layer that a setup packet is available */
  316. HAL_PCD_SetupStageCallback(hpcd);
  317. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
  318. }
  319. if(( epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
  320. {
  321. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
  322. }
  323. }
  324. epnum++;
  325. ep_intr >>= 1;
  326. }
  327. }
  328. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT))
  329. {
  330. /* Read in the device interrupt bits */
  331. ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
  332. epnum = 0;
  333. while ( ep_intr )
  334. {
  335. if (ep_intr & 0x1) /* In ITR */
  336. {
  337. epint = USB_ReadDevInEPInterrupt(hpcd->Instance, epnum);
  338. if(( epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
  339. {
  340. fifoemptymsk = 0x1 << epnum;
  341. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  342. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
  343. HAL_PCD_DataInStageCallback(hpcd, epnum);
  344. }
  345. if(( epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
  346. {
  347. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
  348. }
  349. if(( epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
  350. {
  351. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
  352. }
  353. if(( epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
  354. {
  355. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
  356. }
  357. if(( epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
  358. {
  359. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
  360. }
  361. if(( epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE)
  362. {
  363. PCD_WriteEmptyTxFifo(hpcd , epnum);
  364. }
  365. }
  366. epnum++;
  367. ep_intr >>= 1;
  368. }
  369. }
  370. /* Handle Resume Interrupt */
  371. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT))
  372. {
  373. /* Clear the Remote Wake-up signalling */
  374. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  375. HAL_PCD_ResumeCallback(hpcd);
  376. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
  377. }
  378. /* Handle Suspend Interrupt */
  379. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP))
  380. {
  381. if((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
  382. {
  383. HAL_PCD_SuspendCallback(hpcd);
  384. }
  385. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
  386. }
  387. /* Handle Reset Interrupt */
  388. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST))
  389. {
  390. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  391. USB_FlushTxFifo(hpcd->Instance , 0 );
  392. for (index = 0; index < hpcd->Init.dev_endpoints ; index++)
  393. {
  394. USBx_INEP(index)->DIEPINT = 0xFF;
  395. USBx_OUTEP(index)->DOEPINT = 0xFF;
  396. }
  397. USBx_DEVICE->DAINT = 0xFFFFFFFF;
  398. USBx_DEVICE->DAINTMSK |= 0x10001;
  399. USBx_DEVICE->DOEPMSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
  400. USBx_DEVICE->DIEPMSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
  401. /* Set Default Address to 0 */
  402. USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
  403. /* setup EP0 to receive SETUP packets */
  404. USB_EP0_OutStart(hpcd->Instance, (uint8_t *)hpcd->Setup);
  405. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
  406. }
  407. /* Handle Enumeration done Interrupt */
  408. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE))
  409. {
  410. USB_ActivateSetup(hpcd->Instance);
  411. hpcd->Instance->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT;
  412. hpcd->Init.speed = USB_OTG_SPEED_FULL;
  413. hpcd->Init.ep0_mps = USB_OTG_FS_MAX_PACKET_SIZE ;
  414. hpcd->Instance->GUSBCFG |= (uint32_t)((USBD_FS_TRDT_VALUE << 10) & USB_OTG_GUSBCFG_TRDT);
  415. HAL_PCD_ResetCallback(hpcd);
  416. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE);
  417. }
  418. /* Handle RxQLevel Interrupt */
  419. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL))
  420. {
  421. USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  422. temp = USBx->GRXSTSP;
  423. ep = &hpcd->OUT_ep[temp & USB_OTG_GRXSTSP_EPNUM];
  424. if(((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT)
  425. {
  426. if((temp & USB_OTG_GRXSTSP_BCNT) != 0)
  427. {
  428. USB_ReadPacket(USBx, ep->xfer_buff, (temp & USB_OTG_GRXSTSP_BCNT) >> 4);
  429. ep->xfer_buff += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  430. ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  431. }
  432. }
  433. else if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT)
  434. {
  435. USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8);
  436. ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  437. }
  438. USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  439. }
  440. /* Handle SOF Interrupt */
  441. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SOF))
  442. {
  443. HAL_PCD_SOFCallback(hpcd);
  444. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SOF);
  445. }
  446. /* Handle Incomplete ISO IN Interrupt */
  447. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR))
  448. {
  449. HAL_PCD_ISOINIncompleteCallback(hpcd, epnum);
  450. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR);
  451. }
  452. /* Handle Incomplete ISO OUT Interrupt */
  453. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
  454. {
  455. HAL_PCD_ISOOUTIncompleteCallback(hpcd, epnum);
  456. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
  457. }
  458. /* Handle Connection event Interrupt */
  459. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT))
  460. {
  461. HAL_PCD_ConnectCallback(hpcd);
  462. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT);
  463. }
  464. /* Handle Disconnection event Interrupt */
  465. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OTGINT))
  466. {
  467. temp = hpcd->Instance->GOTGINT;
  468. if((temp & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET)
  469. {
  470. HAL_PCD_DisconnectCallback(hpcd);
  471. }
  472. hpcd->Instance->GOTGINT |= temp;
  473. }
  474. }
  475. }
  476. #endif /* USB_OTG_FS */
  477. #if defined (USB)
  478. /**
  479. * @brief This function handles PCD interrupt request.
  480. * @param hpcd: PCD handle
  481. * @retval HAL status
  482. */
  483. void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
  484. {
  485. uint32_t wInterrupt_Mask = 0;
  486. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_CTR))
  487. {
  488. /* servicing of the endpoint correct transfer interrupt */
  489. /* clear of the CTR flag into the sub */
  490. PCD_EP_ISR_Handler(hpcd);
  491. }
  492. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_RESET))
  493. {
  494. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_RESET);
  495. HAL_PCD_ResetCallback(hpcd);
  496. HAL_PCD_SetAddress(hpcd, 0);
  497. }
  498. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_PMAOVR))
  499. {
  500. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_PMAOVR);
  501. }
  502. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_ERR))
  503. {
  504. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ERR);
  505. }
  506. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_WKUP))
  507. {
  508. hpcd->Instance->CNTR &= ~(USB_CNTR_LP_MODE);
  509. /*set wInterrupt_Mask global variable*/
  510. wInterrupt_Mask = USB_CNTR_CTRM | USB_CNTR_WKUPM | USB_CNTR_SUSPM | USB_CNTR_ERRM \
  511. | USB_CNTR_ESOFM | USB_CNTR_RESETM;
  512. /*Set interrupt mask*/
  513. hpcd->Instance->CNTR = wInterrupt_Mask;
  514. HAL_PCD_ResumeCallback(hpcd);
  515. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_WKUP);
  516. }
  517. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_SUSP))
  518. {
  519. /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
  520. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SUSP);
  521. /* Force low-power mode in the macrocell */
  522. hpcd->Instance->CNTR |= USB_CNTR_FSUSP;
  523. hpcd->Instance->CNTR |= USB_CNTR_LP_MODE;
  524. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_WKUP) == 0)
  525. {
  526. HAL_PCD_SuspendCallback(hpcd);
  527. }
  528. }
  529. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_SOF))
  530. {
  531. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SOF);
  532. HAL_PCD_SOFCallback(hpcd);
  533. }
  534. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_ESOF))
  535. {
  536. /* clear ESOF flag in ISTR */
  537. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ESOF);
  538. }
  539. }
  540. #endif /* USB */
  541. /**
  542. * @brief Data out stage callbacks
  543. * @param hpcd: PCD handle
  544. * @param epnum: endpoint number
  545. * @retval None
  546. */
  547. __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  548. {
  549. /* Prevent unused argument(s) compilation warning */
  550. UNUSED(hpcd);
  551. UNUSED(epnum);
  552. /* NOTE : This function should not be modified, when the callback is needed,
  553. the HAL_PCD_DataOutStageCallback could be implemented in the user file
  554. */
  555. }
  556. /**
  557. * @brief Data IN stage callbacks
  558. * @param hpcd: PCD handle
  559. * @param epnum: endpoint number
  560. * @retval None
  561. */
  562. __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  563. {
  564. /* Prevent unused argument(s) compilation warning */
  565. UNUSED(hpcd);
  566. UNUSED(epnum);
  567. /* NOTE : This function should not be modified, when the callback is needed,
  568. the HAL_PCD_DataInStageCallback could be implemented in the user file
  569. */
  570. }
  571. /**
  572. * @brief Setup stage callback
  573. * @param hpcd: PCD handle
  574. * @retval None
  575. */
  576. __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  577. {
  578. /* Prevent unused argument(s) compilation warning */
  579. UNUSED(hpcd);
  580. /* NOTE : This function should not be modified, when the callback is needed,
  581. the HAL_PCD_SetupStageCallback could be implemented in the user file
  582. */
  583. }
  584. /**
  585. * @brief USB Start Of Frame callbacks
  586. * @param hpcd: PCD handle
  587. * @retval None
  588. */
  589. __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  590. {
  591. /* Prevent unused argument(s) compilation warning */
  592. UNUSED(hpcd);
  593. /* NOTE : This function should not be modified, when the callback is needed,
  594. the HAL_PCD_SOFCallback could be implemented in the user file
  595. */
  596. }
  597. /**
  598. * @brief USB Reset callbacks
  599. * @param hpcd: PCD handle
  600. * @retval None
  601. */
  602. __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  603. {
  604. /* Prevent unused argument(s) compilation warning */
  605. UNUSED(hpcd);
  606. /* NOTE : This function should not be modified, when the callback is needed,
  607. the HAL_PCD_ResetCallback could be implemented in the user file
  608. */
  609. }
  610. /**
  611. * @brief Suspend event callbacks
  612. * @param hpcd: PCD handle
  613. * @retval None
  614. */
  615. __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  616. {
  617. /* Prevent unused argument(s) compilation warning */
  618. UNUSED(hpcd);
  619. /* NOTE : This function should not be modified, when the callback is needed,
  620. the HAL_PCD_SuspendCallback could be implemented in the user file
  621. */
  622. }
  623. /**
  624. * @brief Resume event callbacks
  625. * @param hpcd: PCD handle
  626. * @retval None
  627. */
  628. __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  629. {
  630. /* Prevent unused argument(s) compilation warning */
  631. UNUSED(hpcd);
  632. /* NOTE : This function should not be modified, when the callback is needed,
  633. the HAL_PCD_ResumeCallback could be implemented in the user file
  634. */
  635. }
  636. /**
  637. * @brief Incomplete ISO OUT callbacks
  638. * @param hpcd: PCD handle
  639. * @param epnum: endpoint number
  640. * @retval None
  641. */
  642. __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  643. {
  644. /* Prevent unused argument(s) compilation warning */
  645. UNUSED(hpcd);
  646. UNUSED(epnum);
  647. /* NOTE : This function should not be modified, when the callback is needed,
  648. the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
  649. */
  650. }
  651. /**
  652. * @brief Incomplete ISO IN callbacks
  653. * @param hpcd: PCD handle
  654. * @param epnum: endpoint number
  655. * @retval None
  656. */
  657. __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  658. {
  659. /* Prevent unused argument(s) compilation warning */
  660. UNUSED(hpcd);
  661. UNUSED(epnum);
  662. /* NOTE : This function should not be modified, when the callback is needed,
  663. the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
  664. */
  665. }
  666. /**
  667. * @brief Connection event callbacks
  668. * @param hpcd: PCD handle
  669. * @retval None
  670. */
  671. __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  672. {
  673. /* Prevent unused argument(s) compilation warning */
  674. UNUSED(hpcd);
  675. /* NOTE : This function should not be modified, when the callback is needed,
  676. the HAL_PCD_ConnectCallback could be implemented in the user file
  677. */
  678. }
  679. /**
  680. * @brief Disconnection event callbacks
  681. * @param hpcd: PCD handle
  682. * @retval None
  683. */
  684. __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  685. {
  686. /* Prevent unused argument(s) compilation warning */
  687. UNUSED(hpcd);
  688. /* NOTE : This function should not be modified, when the callback is needed,
  689. the HAL_PCD_DisconnectCallback could be implemented in the user file
  690. */
  691. }
  692. /**
  693. * @}
  694. */
  695. /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
  696. * @brief management functions
  697. *
  698. @verbatim
  699. ===============================================================================
  700. ##### Peripheral Control functions #####
  701. ===============================================================================
  702. [..]
  703. This subsection provides a set of functions allowing to control the PCD data
  704. transfers.
  705. @endverbatim
  706. * @{
  707. */
  708. /**
  709. * @brief Connect the USB device
  710. * @param hpcd: PCD handle
  711. * @retval HAL status
  712. */
  713. HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
  714. {
  715. __HAL_LOCK(hpcd);
  716. HAL_PCDEx_SetConnectionState (hpcd, 1);
  717. USB_DevConnect(hpcd->Instance);
  718. __HAL_UNLOCK(hpcd);
  719. return HAL_OK;
  720. }
  721. /**
  722. * @brief Disconnect the USB device
  723. * @param hpcd: PCD handle
  724. * @retval HAL status
  725. */
  726. HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
  727. {
  728. __HAL_LOCK(hpcd);
  729. HAL_PCDEx_SetConnectionState (hpcd, 0);
  730. USB_DevDisconnect(hpcd->Instance);
  731. __HAL_UNLOCK(hpcd);
  732. return HAL_OK;
  733. }
  734. /**
  735. * @brief Set the USB Device address
  736. * @param hpcd: PCD handle
  737. * @param address: new device address
  738. * @retval HAL status
  739. */
  740. HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
  741. {
  742. __HAL_LOCK(hpcd);
  743. hpcd->USB_Address = address;
  744. USB_SetDevAddress(hpcd->Instance, address);
  745. __HAL_UNLOCK(hpcd);
  746. return HAL_OK;
  747. }
  748. /**
  749. * @brief Open and configure an endpoint
  750. * @param hpcd: PCD handle
  751. * @param ep_addr: endpoint address
  752. * @param ep_mps: endpoint max packet size
  753. * @param ep_type: endpoint type
  754. * @retval HAL status
  755. */
  756. HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type)
  757. {
  758. HAL_StatusTypeDef ret = HAL_OK;
  759. PCD_EPTypeDef *ep = NULL;
  760. if ((ep_addr & 0x80) == 0x80)
  761. {
  762. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  763. }
  764. else
  765. {
  766. ep = &hpcd->OUT_ep[ep_addr & 0x7F];
  767. }
  768. ep->num = ep_addr & 0x7F;
  769. ep->is_in = (0x80 & ep_addr) != 0;
  770. ep->maxpacket = ep_mps;
  771. ep->type = ep_type;
  772. __HAL_LOCK(hpcd);
  773. USB_ActivateEndpoint(hpcd->Instance , ep);
  774. __HAL_UNLOCK(hpcd);
  775. return ret;
  776. }
  777. /**
  778. * @brief Deactivate an endpoint
  779. * @param hpcd: PCD handle
  780. * @param ep_addr: endpoint address
  781. * @retval HAL status
  782. */
  783. HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  784. {
  785. PCD_EPTypeDef *ep = NULL;
  786. if ((ep_addr & 0x80) == 0x80)
  787. {
  788. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  789. }
  790. else
  791. {
  792. ep = &hpcd->OUT_ep[ep_addr & 0x7F];
  793. }
  794. ep->num = ep_addr & 0x7F;
  795. ep->is_in = (0x80 & ep_addr) != 0;
  796. __HAL_LOCK(hpcd);
  797. USB_DeactivateEndpoint(hpcd->Instance , ep);
  798. __HAL_UNLOCK(hpcd);
  799. return HAL_OK;
  800. }
  801. /**
  802. * @brief Receive an amount of data
  803. * @param hpcd: PCD handle
  804. * @param ep_addr: endpoint address
  805. * @param pBuf: pointer to the reception buffer
  806. * @param len: amount of data to be received
  807. * @retval HAL status
  808. */
  809. HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  810. {
  811. PCD_EPTypeDef *ep = NULL;
  812. ep = &hpcd->OUT_ep[ep_addr & 0x7F];
  813. /*setup and start the Xfer */
  814. ep->xfer_buff = pBuf;
  815. ep->xfer_len = len;
  816. ep->xfer_count = 0;
  817. ep->is_in = 0;
  818. ep->num = ep_addr & 0x7F;
  819. __HAL_LOCK(hpcd);
  820. if ((ep_addr & 0x7F) == 0 )
  821. {
  822. USB_EP0StartXfer(hpcd->Instance , ep);
  823. }
  824. else
  825. {
  826. USB_EPStartXfer(hpcd->Instance , ep);
  827. }
  828. __HAL_UNLOCK(hpcd);
  829. return HAL_OK;
  830. }
  831. /**
  832. * @brief Get Received Data Size
  833. * @param hpcd: PCD handle
  834. * @param ep_addr: endpoint address
  835. * @retval Data Size
  836. */
  837. uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  838. {
  839. return hpcd->OUT_ep[ep_addr & 0x7F].xfer_count;
  840. }
  841. /**
  842. * @brief Send an amount of data
  843. * @param hpcd: PCD handle
  844. * @param ep_addr: endpoint address
  845. * @param pBuf: pointer to the transmission buffer
  846. * @param len: amount of data to be sent
  847. * @retval HAL status
  848. */
  849. HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  850. {
  851. PCD_EPTypeDef *ep = NULL;
  852. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  853. /*setup and start the Xfer */
  854. ep->xfer_buff = pBuf;
  855. ep->xfer_len = len;
  856. ep->xfer_count = 0;
  857. ep->is_in = 1;
  858. ep->num = ep_addr & 0x7F;
  859. __HAL_LOCK(hpcd);
  860. if ((ep_addr & 0x7F) == 0 )
  861. {
  862. USB_EP0StartXfer(hpcd->Instance , ep);
  863. }
  864. else
  865. {
  866. USB_EPStartXfer(hpcd->Instance , ep);
  867. }
  868. __HAL_UNLOCK(hpcd);
  869. return HAL_OK;
  870. }
  871. /**
  872. * @brief Set a STALL condition over an endpoint
  873. * @param hpcd: PCD handle
  874. * @param ep_addr: endpoint address
  875. * @retval HAL status
  876. */
  877. HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  878. {
  879. PCD_EPTypeDef *ep = NULL;
  880. if ((0x80 & ep_addr) == 0x80)
  881. {
  882. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  883. }
  884. else
  885. {
  886. ep = &hpcd->OUT_ep[ep_addr];
  887. }
  888. ep->is_stall = 1;
  889. ep->num = ep_addr & 0x7F;
  890. ep->is_in = ((ep_addr & 0x80) == 0x80);
  891. __HAL_LOCK(hpcd);
  892. USB_EPSetStall(hpcd->Instance , ep);
  893. if((ep_addr & 0x7F) == 0)
  894. {
  895. USB_EP0_OutStart(hpcd->Instance, (uint8_t *)hpcd->Setup);
  896. }
  897. __HAL_UNLOCK(hpcd);
  898. return HAL_OK;
  899. }
  900. /**
  901. * @brief Clear a STALL condition over in an endpoint
  902. * @param hpcd: PCD handle
  903. * @param ep_addr: endpoint address
  904. * @retval HAL status
  905. */
  906. HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  907. {
  908. PCD_EPTypeDef *ep = NULL;
  909. if ((0x80 & ep_addr) == 0x80)
  910. {
  911. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  912. }
  913. else
  914. {
  915. ep = &hpcd->OUT_ep[ep_addr];
  916. }
  917. ep->is_stall = 0;
  918. ep->num = ep_addr & 0x7F;
  919. ep->is_in = ((ep_addr & 0x80) == 0x80);
  920. __HAL_LOCK(hpcd);
  921. USB_EPClearStall(hpcd->Instance , ep);
  922. __HAL_UNLOCK(hpcd);
  923. return HAL_OK;
  924. }
  925. /**
  926. * @brief Flush an endpoint
  927. * @param hpcd: PCD handle
  928. * @param ep_addr: endpoint address
  929. * @retval HAL status
  930. */
  931. HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  932. {
  933. __HAL_LOCK(hpcd);
  934. if ((ep_addr & 0x80) == 0x80)
  935. {
  936. USB_FlushTxFifo(hpcd->Instance, ep_addr & 0x7F);
  937. }
  938. else
  939. {
  940. USB_FlushRxFifo(hpcd->Instance);
  941. }
  942. __HAL_UNLOCK(hpcd);
  943. return HAL_OK;
  944. }
  945. /**
  946. * @brief HAL_PCD_ActivateRemoteWakeup : active remote wakeup signalling
  947. * @param hpcd: PCD handle
  948. * @retval HAL status
  949. */
  950. HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  951. {
  952. return(USB_ActivateRemoteWakeup(hpcd->Instance));
  953. }
  954. /**
  955. * @brief HAL_PCD_DeActivateRemoteWakeup : de-active remote wakeup signalling
  956. * @param hpcd: PCD handle
  957. * @retval HAL status
  958. */
  959. HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  960. {
  961. return(USB_DeActivateRemoteWakeup(hpcd->Instance));
  962. }
  963. /**
  964. * @}
  965. */
  966. /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
  967. * @brief Peripheral State functions
  968. *
  969. @verbatim
  970. ===============================================================================
  971. ##### Peripheral State functions #####
  972. ===============================================================================
  973. [..]
  974. This subsection permits to get in run-time the status of the peripheral
  975. and the data flow.
  976. @endverbatim
  977. * @{
  978. */
  979. /**
  980. * @brief Return the PCD state
  981. * @param hpcd: PCD handle
  982. * @retval HAL state
  983. */
  984. PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
  985. {
  986. return hpcd->State;
  987. }
  988. /**
  989. * @}
  990. */
  991. /**
  992. * @}
  993. */
  994. /** @addtogroup PCD_Private_Functions
  995. * @{
  996. */
  997. #if defined (USB_OTG_FS)
  998. /**
  999. * @brief DCD_WriteEmptyTxFifo
  1000. * check FIFO for the next packet to be loaded
  1001. * @param hpcd: PCD handle
  1002. * @param epnum : endpoint number
  1003. * This parameter can be a value from 0 to 15
  1004. * @retval HAL status
  1005. */
  1006. static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1007. {
  1008. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1009. USB_OTG_EPTypeDef *ep = NULL;
  1010. int32_t len = 0;
  1011. uint32_t len32b = 0;
  1012. uint32_t fifoemptymsk = 0;
  1013. ep = &hpcd->IN_ep[epnum];
  1014. len = ep->xfer_len - ep->xfer_count;
  1015. if (len > ep->maxpacket)
  1016. {
  1017. len = ep->maxpacket;
  1018. }
  1019. len32b = (len + 3) / 4;
  1020. while ((USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) > len32b &&
  1021. ep->xfer_count < ep->xfer_len &&
  1022. ep->xfer_len != 0)
  1023. {
  1024. /* Write the FIFO */
  1025. len = ep->xfer_len - ep->xfer_count;
  1026. if (len > ep->maxpacket)
  1027. {
  1028. len = ep->maxpacket;
  1029. }
  1030. len32b = (len + 3) / 4;
  1031. USB_WritePacket(USBx, ep->xfer_buff, epnum, len);
  1032. ep->xfer_buff += len;
  1033. ep->xfer_count += len;
  1034. }
  1035. if(len <= 0)
  1036. {
  1037. fifoemptymsk = 0x1 << epnum;
  1038. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  1039. }
  1040. return HAL_OK;
  1041. }
  1042. #endif /* USB_OTG_FS */
  1043. #if defined (USB)
  1044. /**
  1045. * @brief This function handles PCD Endpoint interrupt request.
  1046. * @param hpcd: PCD handle
  1047. * @retval HAL status
  1048. */
  1049. static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
  1050. {
  1051. PCD_EPTypeDef *ep = NULL;
  1052. uint16_t count = 0;
  1053. uint8_t epindex = 0;
  1054. __IO uint16_t wIstr = 0;
  1055. __IO uint16_t wEPVal = 0;
  1056. /* stay in loop while pending interrupts */
  1057. while (((wIstr = hpcd->Instance->ISTR) & USB_ISTR_CTR) != 0)
  1058. {
  1059. /* extract highest priority endpoint number */
  1060. epindex = (uint8_t)(wIstr & USB_ISTR_EP_ID);
  1061. if (epindex == 0)
  1062. {
  1063. /* Decode and service control endpoint interrupt */
  1064. /* DIR bit = origin of the interrupt */
  1065. if ((wIstr & USB_ISTR_DIR) == 0)
  1066. {
  1067. /* DIR = 0 */
  1068. /* DIR = 0 => IN int */
  1069. /* DIR = 0 implies that (EP_CTR_TX = 1) always */
  1070. PCD_CLEAR_TX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  1071. ep = &hpcd->IN_ep[0];
  1072. ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
  1073. ep->xfer_buff += ep->xfer_count;
  1074. /* TX COMPLETE */
  1075. HAL_PCD_DataInStageCallback(hpcd, 0);
  1076. if((hpcd->USB_Address > 0)&& ( ep->xfer_len == 0))
  1077. {
  1078. hpcd->Instance->DADDR = (hpcd->USB_Address | USB_DADDR_EF);
  1079. hpcd->USB_Address = 0;
  1080. }
  1081. }
  1082. else
  1083. {
  1084. /* DIR = 1 */
  1085. /* DIR = 1 & CTR_RX => SETUP or OUT int */
  1086. /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
  1087. ep = &hpcd->OUT_ep[0];
  1088. wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
  1089. if ((wEPVal & USB_EP_SETUP) != 0)
  1090. {
  1091. /* Get SETUP Packet*/
  1092. ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  1093. USB_ReadPMA(hpcd->Instance, (uint8_t*)hpcd->Setup ,ep->pmaadress , ep->xfer_count);
  1094. /* SETUP bit kept frozen while CTR_RX = 1*/
  1095. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  1096. /* Process SETUP Packet*/
  1097. HAL_PCD_SetupStageCallback(hpcd);
  1098. }
  1099. else if ((wEPVal & USB_EP_CTR_RX) != 0)
  1100. {
  1101. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  1102. /* Get Control Data OUT Packet*/
  1103. ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  1104. if (ep->xfer_count != 0)
  1105. {
  1106. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, ep->xfer_count);
  1107. ep->xfer_buff+=ep->xfer_count;
  1108. }
  1109. /* Process Control Data OUT Packet*/
  1110. HAL_PCD_DataOutStageCallback(hpcd, 0);
  1111. PCD_SET_EP_RX_CNT(hpcd->Instance, PCD_ENDP0, ep->maxpacket);
  1112. PCD_SET_EP_RX_STATUS(hpcd->Instance, PCD_ENDP0, USB_EP_RX_VALID);
  1113. }
  1114. }
  1115. }
  1116. else
  1117. {
  1118. /* Decode and service non control endpoints interrupt */
  1119. /* process related endpoint register */
  1120. wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, epindex);
  1121. if ((wEPVal & USB_EP_CTR_RX) != 0)
  1122. {
  1123. /* clear int flag */
  1124. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, epindex);
  1125. ep = &hpcd->OUT_ep[epindex];
  1126. /* OUT double Buffering*/
  1127. if (ep->doublebuffer == 0)
  1128. {
  1129. count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  1130. if (count != 0)
  1131. {
  1132. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, count);
  1133. }
  1134. }
  1135. else
  1136. {
  1137. if (PCD_GET_ENDPOINT(hpcd->Instance, ep->num) & USB_EP_DTOG_RX)
  1138. {
  1139. /*read from endpoint BUF0Addr buffer*/
  1140. count = PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  1141. if (count != 0)
  1142. {
  1143. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
  1144. }
  1145. }
  1146. else
  1147. {
  1148. /*read from endpoint BUF1Addr buffer*/
  1149. count = PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
  1150. if (count != 0)
  1151. {
  1152. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
  1153. }
  1154. }
  1155. PCD_FreeUserBuffer(hpcd->Instance, ep->num, PCD_EP_DBUF_OUT);
  1156. }
  1157. /*multi-packet on the NON control OUT endpoint*/
  1158. ep->xfer_count+=count;
  1159. ep->xfer_buff+=count;
  1160. if ((ep->xfer_len == 0) || (count < ep->maxpacket))
  1161. {
  1162. /* RX COMPLETE */
  1163. HAL_PCD_DataOutStageCallback(hpcd, ep->num);
  1164. }
  1165. else
  1166. {
  1167. HAL_PCD_EP_Receive(hpcd, ep->num, ep->xfer_buff, ep->xfer_len);
  1168. }
  1169. } /* if((wEPVal & EP_CTR_RX) */
  1170. if ((wEPVal & USB_EP_CTR_TX) != 0)
  1171. {
  1172. ep = &hpcd->IN_ep[epindex];
  1173. /* clear int flag */
  1174. PCD_CLEAR_TX_EP_CTR(hpcd->Instance, epindex);
  1175. /* IN double Buffering*/
  1176. if (ep->doublebuffer == 0)
  1177. {
  1178. ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
  1179. if (ep->xfer_count != 0)
  1180. {
  1181. USB_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, ep->xfer_count);
  1182. }
  1183. }
  1184. else
  1185. {
  1186. if (PCD_GET_ENDPOINT(hpcd->Instance, ep->num) & USB_EP_DTOG_TX)
  1187. {
  1188. /*read from endpoint BUF0Addr buffer*/
  1189. ep->xfer_count = PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  1190. if (ep->xfer_count != 0)
  1191. {
  1192. USB_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, ep->xfer_count);
  1193. }
  1194. }
  1195. else
  1196. {
  1197. /*read from endpoint BUF1Addr buffer*/
  1198. ep->xfer_count = PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
  1199. if (ep->xfer_count != 0)
  1200. {
  1201. USB_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, ep->xfer_count);
  1202. }
  1203. }
  1204. PCD_FreeUserBuffer(hpcd->Instance, ep->num, PCD_EP_DBUF_IN);
  1205. }
  1206. /*multi-packet on the NON control IN endpoint*/
  1207. ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
  1208. ep->xfer_buff+=ep->xfer_count;
  1209. /* Zero Length Packet? */
  1210. if (ep->xfer_len == 0)
  1211. {
  1212. /* TX COMPLETE */
  1213. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  1214. }
  1215. else
  1216. {
  1217. HAL_PCD_EP_Transmit(hpcd, ep->num, ep->xfer_buff, ep->xfer_len);
  1218. }
  1219. }
  1220. }
  1221. }
  1222. return HAL_OK;
  1223. }
  1224. #endif /* USB */
  1225. /**
  1226. * @}
  1227. */
  1228. /**
  1229. * @}
  1230. */
  1231. #endif /* STM32F102x6 || STM32F102xB || */
  1232. /* STM32F103x6 || STM32F103xB || */
  1233. /* STM32F103xE || STM32F103xG || */
  1234. /* STM32F105xC || STM32F107xC */
  1235. #endif /* HAL_PCD_MODULE_ENABLED */
  1236. /**
  1237. * @}
  1238. */
  1239. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/