123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274 |
- /**
- ******************************************************************************
- * @file stm32f7xx_hal_pcd.c
- * @author MCD Application Team
- * @brief PCD HAL module driver.
- * This file provides firmware functions to manage the following
- * functionalities of the USB Peripheral Controller:
- * + Initialization and de-initialization functions
- * + IO operation functions
- * + Peripheral Control functions
- * + Peripheral State functions
- *
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2017 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- @verbatim
- ==============================================================================
- ##### How to use this driver #####
- ==============================================================================
- [..]
- The PCD HAL driver can be used as follows:
- (#) Declare a PCD_HandleTypeDef handle structure, for example:
- PCD_HandleTypeDef hpcd;
- (#) Fill parameters of Init structure in HCD handle
- (#) Call HAL_PCD_Init() API to initialize the PCD peripheral (Core, Device core, ...)
- (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
- (##) Enable the PCD/USB Low Level interface clock using
- (+++) __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
- (+++) __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); (For High Speed Mode)
- (##) Initialize the related GPIO clocks
- (##) Configure PCD pin-out
- (##) Configure PCD NVIC interrupt
- (#)Associate the Upper USB device stack to the HAL PCD Driver:
- (##) hpcd.pData = pdev;
- (#)Enable PCD transmission and reception:
- (##) HAL_PCD_Start();
- @endverbatim
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f7xx_hal.h"
- /** @addtogroup STM32F7xx_HAL_Driver
- * @{
- */
- /** @defgroup PCD PCD
- * @brief PCD HAL module driver
- * @{
- */
- #ifdef HAL_PCD_MODULE_ENABLED
- #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
- /* Private types -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private constants ---------------------------------------------------------*/
- /* Private macros ------------------------------------------------------------*/
- /** @defgroup PCD_Private_Macros PCD Private Macros
- * @{
- */
- #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
- #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
- /**
- * @}
- */
- /* Private functions prototypes ----------------------------------------------*/
- /** @defgroup PCD_Private_Functions PCD Private Functions
- * @{
- */
- #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
- static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
- static HAL_StatusTypeDef PCD_EP_OutXfrComplete_int(PCD_HandleTypeDef *hpcd, uint32_t epnum);
- static HAL_StatusTypeDef PCD_EP_OutSetupPacket_int(PCD_HandleTypeDef *hpcd, uint32_t epnum);
- #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
- /**
- * @}
- */
- /* Exported functions --------------------------------------------------------*/
- /** @defgroup PCD_Exported_Functions PCD Exported Functions
- * @{
- */
- /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
- * @brief Initialization and Configuration functions
- *
- @verbatim
- ===============================================================================
- ##### Initialization and de-initialization functions #####
- ===============================================================================
- [..] This section provides functions allowing to:
- @endverbatim
- * @{
- */
- /**
- * @brief Initializes the PCD according to the specified
- * parameters in the PCD_InitTypeDef and initialize the associated handle.
- * @param hpcd PCD handle
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
- {
- USB_OTG_GlobalTypeDef *USBx;
- uint8_t i;
- /* Check the PCD handle allocation */
- if (hpcd == NULL)
- {
- return HAL_ERROR;
- }
- /* Check the parameters */
- assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
- USBx = hpcd->Instance;
- if (hpcd->State == HAL_PCD_STATE_RESET)
- {
- /* Allocate lock resource and initialize it */
- hpcd->Lock = HAL_UNLOCKED;
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->SOFCallback = HAL_PCD_SOFCallback;
- hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
- hpcd->ResetCallback = HAL_PCD_ResetCallback;
- hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
- hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
- hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
- hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
- hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback;
- hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback;
- hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback;
- hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback;
- if (hpcd->MspInitCallback == NULL)
- {
- hpcd->MspInitCallback = HAL_PCD_MspInit;
- }
- /* Init the low level hardware */
- hpcd->MspInitCallback(hpcd);
- #else
- /* Init the low level hardware : GPIO, CLOCK, NVIC... */
- HAL_PCD_MspInit(hpcd);
- #endif /* (USE_HAL_PCD_REGISTER_CALLBACKS) */
- }
- hpcd->State = HAL_PCD_STATE_BUSY;
- /* Disable DMA mode for FS instance */
- if ((USBx->CID & (0x1U << 8)) == 0U)
- {
- hpcd->Init.dma_enable = 0U;
- }
- /* Disable the Interrupts */
- __HAL_PCD_DISABLE(hpcd);
- /*Init the Core (common init.) */
- if (USB_CoreInit(hpcd->Instance, hpcd->Init) != HAL_OK)
- {
- hpcd->State = HAL_PCD_STATE_ERROR;
- return HAL_ERROR;
- }
- /* Force Device Mode*/
- (void)USB_SetCurrentMode(hpcd->Instance, USB_DEVICE_MODE);
- /* Init endpoints structures */
- for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
- {
- /* Init ep structure */
- hpcd->IN_ep[i].is_in = 1U;
- hpcd->IN_ep[i].num = i;
- hpcd->IN_ep[i].tx_fifo_num = i;
- /* Control until ep is activated */
- hpcd->IN_ep[i].type = EP_TYPE_CTRL;
- hpcd->IN_ep[i].maxpacket = 0U;
- hpcd->IN_ep[i].xfer_buff = 0U;
- hpcd->IN_ep[i].xfer_len = 0U;
- }
- for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
- {
- hpcd->OUT_ep[i].is_in = 0U;
- hpcd->OUT_ep[i].num = i;
- /* Control until ep is activated */
- hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
- hpcd->OUT_ep[i].maxpacket = 0U;
- hpcd->OUT_ep[i].xfer_buff = 0U;
- hpcd->OUT_ep[i].xfer_len = 0U;
- }
- /* Init Device */
- if (USB_DevInit(hpcd->Instance, hpcd->Init) != HAL_OK)
- {
- hpcd->State = HAL_PCD_STATE_ERROR;
- return HAL_ERROR;
- }
- hpcd->USB_Address = 0U;
- hpcd->State = HAL_PCD_STATE_READY;
- /* Activate LPM */
- if (hpcd->Init.lpm_enable == 1U)
- {
- (void)HAL_PCDEx_ActivateLPM(hpcd);
- }
- (void)USB_DevDisconnect(hpcd->Instance);
- return HAL_OK;
- }
- /**
- * @brief DeInitializes the PCD peripheral.
- * @param hpcd PCD handle
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
- {
- /* Check the PCD handle allocation */
- if (hpcd == NULL)
- {
- return HAL_ERROR;
- }
- hpcd->State = HAL_PCD_STATE_BUSY;
- /* Stop Device */
- if (USB_StopDevice(hpcd->Instance) != HAL_OK)
- {
- return HAL_ERROR;
- }
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- if (hpcd->MspDeInitCallback == NULL)
- {
- hpcd->MspDeInitCallback = HAL_PCD_MspDeInit; /* Legacy weak MspDeInit */
- }
- /* DeInit the low level hardware */
- hpcd->MspDeInitCallback(hpcd);
- #else
- /* DeInit the low level hardware: CLOCK, NVIC.*/
- HAL_PCD_MspDeInit(hpcd);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- hpcd->State = HAL_PCD_STATE_RESET;
- return HAL_OK;
- }
- /**
- * @brief Initializes the PCD MSP.
- * @param hpcd PCD handle
- * @retval None
- */
- __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hpcd);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_PCD_MspInit could be implemented in the user file
- */
- }
- /**
- * @brief DeInitializes PCD MSP.
- * @param hpcd PCD handle
- * @retval None
- */
- __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hpcd);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_PCD_MspDeInit could be implemented in the user file
- */
- }
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- /**
- * @brief Register a User USB PCD Callback
- * To be used instead of the weak predefined callback
- * @param hpcd USB PCD handle
- * @param CallbackID ID of the callback to be registered
- * This parameter can be one of the following values:
- * @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
- * @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
- * @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
- * @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
- * @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
- * @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
- * @arg @ref HAL_PCD_DISCONNECT_CB_ID OTG PCD Disconnect callback ID
- * @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
- * @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
- * @param pCallback pointer to the Callback function
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_RegisterCallback(PCD_HandleTypeDef *hpcd,
- HAL_PCD_CallbackIDTypeDef CallbackID,
- pPCD_CallbackTypeDef pCallback)
- {
- HAL_StatusTypeDef status = HAL_OK;
- if (pCallback == NULL)
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- return HAL_ERROR;
- }
- /* Process locked */
- __HAL_LOCK(hpcd);
- if (hpcd->State == HAL_PCD_STATE_READY)
- {
- switch (CallbackID)
- {
- case HAL_PCD_SOF_CB_ID :
- hpcd->SOFCallback = pCallback;
- break;
- case HAL_PCD_SETUPSTAGE_CB_ID :
- hpcd->SetupStageCallback = pCallback;
- break;
- case HAL_PCD_RESET_CB_ID :
- hpcd->ResetCallback = pCallback;
- break;
- case HAL_PCD_SUSPEND_CB_ID :
- hpcd->SuspendCallback = pCallback;
- break;
- case HAL_PCD_RESUME_CB_ID :
- hpcd->ResumeCallback = pCallback;
- break;
- case HAL_PCD_CONNECT_CB_ID :
- hpcd->ConnectCallback = pCallback;
- break;
- case HAL_PCD_DISCONNECT_CB_ID :
- hpcd->DisconnectCallback = pCallback;
- break;
- case HAL_PCD_MSPINIT_CB_ID :
- hpcd->MspInitCallback = pCallback;
- break;
- case HAL_PCD_MSPDEINIT_CB_ID :
- hpcd->MspDeInitCallback = pCallback;
- break;
- default :
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- break;
- }
- }
- else if (hpcd->State == HAL_PCD_STATE_RESET)
- {
- switch (CallbackID)
- {
- case HAL_PCD_MSPINIT_CB_ID :
- hpcd->MspInitCallback = pCallback;
- break;
- case HAL_PCD_MSPDEINIT_CB_ID :
- hpcd->MspDeInitCallback = pCallback;
- break;
- default :
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- break;
- }
- }
- else
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- }
- /* Release Lock */
- __HAL_UNLOCK(hpcd);
- return status;
- }
- /**
- * @brief Unregister an USB PCD Callback
- * USB PCD callback is redirected to the weak predefined callback
- * @param hpcd USB PCD handle
- * @param CallbackID ID of the callback to be unregistered
- * This parameter can be one of the following values:
- * @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
- * @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
- * @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
- * @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
- * @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
- * @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
- * @arg @ref HAL_PCD_DISCONNECT_CB_ID OTG PCD Disconnect callback ID
- * @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
- * @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_UnRegisterCallback(PCD_HandleTypeDef *hpcd, HAL_PCD_CallbackIDTypeDef CallbackID)
- {
- HAL_StatusTypeDef status = HAL_OK;
- /* Process locked */
- __HAL_LOCK(hpcd);
- /* Setup Legacy weak Callbacks */
- if (hpcd->State == HAL_PCD_STATE_READY)
- {
- switch (CallbackID)
- {
- case HAL_PCD_SOF_CB_ID :
- hpcd->SOFCallback = HAL_PCD_SOFCallback;
- break;
- case HAL_PCD_SETUPSTAGE_CB_ID :
- hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
- break;
- case HAL_PCD_RESET_CB_ID :
- hpcd->ResetCallback = HAL_PCD_ResetCallback;
- break;
- case HAL_PCD_SUSPEND_CB_ID :
- hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
- break;
- case HAL_PCD_RESUME_CB_ID :
- hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
- break;
- case HAL_PCD_CONNECT_CB_ID :
- hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
- break;
- case HAL_PCD_DISCONNECT_CB_ID :
- hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
- break;
- case HAL_PCD_MSPINIT_CB_ID :
- hpcd->MspInitCallback = HAL_PCD_MspInit;
- break;
- case HAL_PCD_MSPDEINIT_CB_ID :
- hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
- break;
- default :
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- break;
- }
- }
- else if (hpcd->State == HAL_PCD_STATE_RESET)
- {
- switch (CallbackID)
- {
- case HAL_PCD_MSPINIT_CB_ID :
- hpcd->MspInitCallback = HAL_PCD_MspInit;
- break;
- case HAL_PCD_MSPDEINIT_CB_ID :
- hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
- break;
- default :
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- break;
- }
- }
- else
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- }
- /* Release Lock */
- __HAL_UNLOCK(hpcd);
- return status;
- }
- /**
- * @brief Register USB PCD Data OUT Stage Callback
- * To be used instead of the weak HAL_PCD_DataOutStageCallback() predefined callback
- * @param hpcd PCD handle
- * @param pCallback pointer to the USB PCD Data OUT Stage Callback function
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_RegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd,
- pPCD_DataOutStageCallbackTypeDef pCallback)
- {
- HAL_StatusTypeDef status = HAL_OK;
- if (pCallback == NULL)
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- return HAL_ERROR;
- }
- /* Process locked */
- __HAL_LOCK(hpcd);
- if (hpcd->State == HAL_PCD_STATE_READY)
- {
- hpcd->DataOutStageCallback = pCallback;
- }
- else
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- }
- /* Release Lock */
- __HAL_UNLOCK(hpcd);
- return status;
- }
- /**
- * @brief Unregister the USB PCD Data OUT Stage Callback
- * USB PCD Data OUT Stage Callback is redirected to the weak HAL_PCD_DataOutStageCallback() predefined callback
- * @param hpcd PCD handle
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_UnRegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd)
- {
- HAL_StatusTypeDef status = HAL_OK;
- /* Process locked */
- __HAL_LOCK(hpcd);
- if (hpcd->State == HAL_PCD_STATE_READY)
- {
- hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback; /* Legacy weak DataOutStageCallback */
- }
- else
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- }
- /* Release Lock */
- __HAL_UNLOCK(hpcd);
- return status;
- }
- /**
- * @brief Register USB PCD Data IN Stage Callback
- * To be used instead of the weak HAL_PCD_DataInStageCallback() predefined callback
- * @param hpcd PCD handle
- * @param pCallback pointer to the USB PCD Data IN Stage Callback function
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_RegisterDataInStageCallback(PCD_HandleTypeDef *hpcd,
- pPCD_DataInStageCallbackTypeDef pCallback)
- {
- HAL_StatusTypeDef status = HAL_OK;
- if (pCallback == NULL)
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- return HAL_ERROR;
- }
- /* Process locked */
- __HAL_LOCK(hpcd);
- if (hpcd->State == HAL_PCD_STATE_READY)
- {
- hpcd->DataInStageCallback = pCallback;
- }
- else
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- }
- /* Release Lock */
- __HAL_UNLOCK(hpcd);
- return status;
- }
- /**
- * @brief Unregister the USB PCD Data IN Stage Callback
- * USB PCD Data OUT Stage Callback is redirected to the weak HAL_PCD_DataInStageCallback() predefined callback
- * @param hpcd PCD handle
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_UnRegisterDataInStageCallback(PCD_HandleTypeDef *hpcd)
- {
- HAL_StatusTypeDef status = HAL_OK;
- /* Process locked */
- __HAL_LOCK(hpcd);
- if (hpcd->State == HAL_PCD_STATE_READY)
- {
- hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback; /* Legacy weak DataInStageCallback */
- }
- else
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- }
- /* Release Lock */
- __HAL_UNLOCK(hpcd);
- return status;
- }
- /**
- * @brief Register USB PCD Iso OUT incomplete Callback
- * To be used instead of the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
- * @param hpcd PCD handle
- * @param pCallback pointer to the USB PCD Iso OUT incomplete Callback function
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_RegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd,
- pPCD_IsoOutIncpltCallbackTypeDef pCallback)
- {
- HAL_StatusTypeDef status = HAL_OK;
- if (pCallback == NULL)
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- return HAL_ERROR;
- }
- /* Process locked */
- __HAL_LOCK(hpcd);
- if (hpcd->State == HAL_PCD_STATE_READY)
- {
- hpcd->ISOOUTIncompleteCallback = pCallback;
- }
- else
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- }
- /* Release Lock */
- __HAL_UNLOCK(hpcd);
- return status;
- }
- /**
- * @brief Unregister the USB PCD Iso OUT incomplete Callback
- * USB PCD Iso OUT incomplete Callback is redirected
- * to the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
- * @param hpcd PCD handle
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_UnRegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd)
- {
- HAL_StatusTypeDef status = HAL_OK;
- /* Process locked */
- __HAL_LOCK(hpcd);
- if (hpcd->State == HAL_PCD_STATE_READY)
- {
- hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback; /* Legacy weak ISOOUTIncompleteCallback */
- }
- else
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- }
- /* Release Lock */
- __HAL_UNLOCK(hpcd);
- return status;
- }
- /**
- * @brief Register USB PCD Iso IN incomplete Callback
- * To be used instead of the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
- * @param hpcd PCD handle
- * @param pCallback pointer to the USB PCD Iso IN incomplete Callback function
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_RegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd,
- pPCD_IsoInIncpltCallbackTypeDef pCallback)
- {
- HAL_StatusTypeDef status = HAL_OK;
- if (pCallback == NULL)
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- return HAL_ERROR;
- }
- /* Process locked */
- __HAL_LOCK(hpcd);
- if (hpcd->State == HAL_PCD_STATE_READY)
- {
- hpcd->ISOINIncompleteCallback = pCallback;
- }
- else
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- }
- /* Release Lock */
- __HAL_UNLOCK(hpcd);
- return status;
- }
- /**
- * @brief Unregister the USB PCD Iso IN incomplete Callback
- * USB PCD Iso IN incomplete Callback is redirected
- * to the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
- * @param hpcd PCD handle
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_UnRegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd)
- {
- HAL_StatusTypeDef status = HAL_OK;
- /* Process locked */
- __HAL_LOCK(hpcd);
- if (hpcd->State == HAL_PCD_STATE_READY)
- {
- hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback; /* Legacy weak ISOINIncompleteCallback */
- }
- else
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- }
- /* Release Lock */
- __HAL_UNLOCK(hpcd);
- return status;
- }
- /**
- * @brief Register USB PCD LPM Callback
- * To be used instead of the weak HAL_PCDEx_LPM_Callback() predefined callback
- * @param hpcd PCD handle
- * @param pCallback pointer to the USB PCD LPM Callback function
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_RegisterLpmCallback(PCD_HandleTypeDef *hpcd, pPCD_LpmCallbackTypeDef pCallback)
- {
- HAL_StatusTypeDef status = HAL_OK;
- if (pCallback == NULL)
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- return HAL_ERROR;
- }
- /* Process locked */
- __HAL_LOCK(hpcd);
- if (hpcd->State == HAL_PCD_STATE_READY)
- {
- hpcd->LPMCallback = pCallback;
- }
- else
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- }
- /* Release Lock */
- __HAL_UNLOCK(hpcd);
- return status;
- }
- /**
- * @brief Unregister the USB PCD LPM Callback
- * USB LPM Callback is redirected to the weak HAL_PCDEx_LPM_Callback() predefined callback
- * @param hpcd PCD handle
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_UnRegisterLpmCallback(PCD_HandleTypeDef *hpcd)
- {
- HAL_StatusTypeDef status = HAL_OK;
- /* Process locked */
- __HAL_LOCK(hpcd);
- if (hpcd->State == HAL_PCD_STATE_READY)
- {
- hpcd->LPMCallback = HAL_PCDEx_LPM_Callback; /* Legacy weak HAL_PCDEx_LPM_Callback */
- }
- else
- {
- /* Update the error code */
- hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- }
- /* Release Lock */
- __HAL_UNLOCK(hpcd);
- return status;
- }
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- /**
- * @}
- */
- /** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
- * @brief Data transfers functions
- *
- @verbatim
- ===============================================================================
- ##### IO operation functions #####
- ===============================================================================
- [..]
- This subsection provides a set of functions allowing to manage the PCD data
- transfers.
- @endverbatim
- * @{
- */
- /**
- * @brief Start the USB device
- * @param hpcd PCD handle
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
- {
- __HAL_LOCK(hpcd);
- __HAL_PCD_ENABLE(hpcd);
- (void)USB_DevConnect(hpcd->Instance);
- __HAL_UNLOCK(hpcd);
- return HAL_OK;
- }
- /**
- * @brief Stop the USB device.
- * @param hpcd PCD handle
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
- {
- __HAL_LOCK(hpcd);
- __HAL_PCD_DISABLE(hpcd);
- (void)USB_DevDisconnect(hpcd->Instance);
- (void)USB_FlushTxFifo(hpcd->Instance, 0x10U);
- __HAL_UNLOCK(hpcd);
- return HAL_OK;
- }
- #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
- /**
- * @brief Handles PCD interrupt request.
- * @param hpcd PCD handle
- * @retval HAL status
- */
- void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
- {
- USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
- uint32_t USBx_BASE = (uint32_t)USBx;
- USB_OTG_EPTypeDef *ep;
- uint32_t i;
- uint32_t ep_intr;
- uint32_t epint;
- uint32_t epnum;
- uint32_t fifoemptymsk;
- uint32_t RegVal;
- /* ensure that we are in device mode */
- if (USB_GetMode(hpcd->Instance) == USB_OTG_MODE_DEVICE)
- {
- /* avoid spurious interrupt */
- if (__HAL_PCD_IS_INVALID_INTERRUPT(hpcd))
- {
- return;
- }
- /* store current frame number */
- hpcd->FrameNumber = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF_Msk) >> USB_OTG_DSTS_FNSOF_Pos;
- if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_MMIS))
- {
- /* incorrect mode, acknowledge the interrupt */
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_MMIS);
- }
- /* Handle RxQLevel Interrupt */
- if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL))
- {
- USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
- RegVal = USBx->GRXSTSP;
- ep = &hpcd->OUT_ep[RegVal & USB_OTG_GRXSTSP_EPNUM];
- if (((RegVal & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT)
- {
- if ((RegVal & USB_OTG_GRXSTSP_BCNT) != 0U)
- {
- (void)USB_ReadPacket(USBx, ep->xfer_buff,
- (uint16_t)((RegVal & USB_OTG_GRXSTSP_BCNT) >> 4));
- ep->xfer_buff += (RegVal & USB_OTG_GRXSTSP_BCNT) >> 4;
- ep->xfer_count += (RegVal & USB_OTG_GRXSTSP_BCNT) >> 4;
- }
- }
- else if (((RegVal & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT)
- {
- (void)USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8U);
- ep->xfer_count += (RegVal & USB_OTG_GRXSTSP_BCNT) >> 4;
- }
- else
- {
- /* ... */
- }
- USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
- }
- if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OEPINT))
- {
- epnum = 0U;
- /* Read in the device interrupt bits */
- ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
- while (ep_intr != 0U)
- {
- if ((ep_intr & 0x1U) != 0U)
- {
- epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, (uint8_t)epnum);
- if ((epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC)
- {
- CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
- (void)PCD_EP_OutXfrComplete_int(hpcd, epnum);
- }
- if ((epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP)
- {
- CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
- /* Class B setup phase done for previous decoded setup */
- (void)PCD_EP_OutSetupPacket_int(hpcd, epnum);
- }
- if ((epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
- {
- CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
- }
- /* Clear OUT Endpoint disable interrupt */
- if ((epint & USB_OTG_DOEPINT_EPDISD) == USB_OTG_DOEPINT_EPDISD)
- {
- if ((USBx->GINTSTS & USB_OTG_GINTSTS_BOUTNAKEFF) == USB_OTG_GINTSTS_BOUTNAKEFF)
- {
- USBx_DEVICE->DCTL |= USB_OTG_DCTL_CGONAK;
- }
- ep = &hpcd->OUT_ep[epnum];
- if (ep->is_iso_incomplete == 1U)
- {
- ep->is_iso_incomplete = 0U;
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->ISOOUTIncompleteCallback(hpcd, (uint8_t)epnum);
- #else
- HAL_PCD_ISOOUTIncompleteCallback(hpcd, (uint8_t)epnum);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_EPDISD);
- }
- /* Clear Status Phase Received interrupt */
- if ((epint & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
- {
- CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
- }
- /* Clear OUT NAK interrupt */
- if ((epint & USB_OTG_DOEPINT_NAK) == USB_OTG_DOEPINT_NAK)
- {
- CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_NAK);
- }
- }
- epnum++;
- ep_intr >>= 1U;
- }
- }
- if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT))
- {
- /* Read in the device interrupt bits */
- ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
- epnum = 0U;
- while (ep_intr != 0U)
- {
- if ((ep_intr & 0x1U) != 0U) /* In ITR */
- {
- epint = USB_ReadDevInEPInterrupt(hpcd->Instance, (uint8_t)epnum);
- if ((epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
- {
- fifoemptymsk = (uint32_t)(0x1UL << (epnum & EP_ADDR_MSK));
- USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
- CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
- if (hpcd->Init.dma_enable == 1U)
- {
- hpcd->IN_ep[epnum].xfer_buff += hpcd->IN_ep[epnum].maxpacket;
- /* this is ZLP, so prepare EP0 for next setup */
- if ((epnum == 0U) && (hpcd->IN_ep[epnum].xfer_len == 0U))
- {
- /* prepare to rx more setup packets */
- (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
- }
- }
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->DataInStageCallback(hpcd, (uint8_t)epnum);
- #else
- HAL_PCD_DataInStageCallback(hpcd, (uint8_t)epnum);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- if ((epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
- {
- CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
- }
- if ((epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
- {
- CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
- }
- if ((epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
- {
- CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
- }
- if ((epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
- {
- (void)USB_FlushTxFifo(USBx, epnum);
- ep = &hpcd->IN_ep[epnum];
- if (ep->is_iso_incomplete == 1U)
- {
- ep->is_iso_incomplete = 0U;
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->ISOINIncompleteCallback(hpcd, (uint8_t)epnum);
- #else
- HAL_PCD_ISOINIncompleteCallback(hpcd, (uint8_t)epnum);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
- }
- if ((epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE)
- {
- (void)PCD_WriteEmptyTxFifo(hpcd, epnum);
- }
- }
- epnum++;
- ep_intr >>= 1U;
- }
- }
- /* Handle Resume Interrupt */
- if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT))
- {
- /* Clear the Remote Wake-up Signaling */
- USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
- if (hpcd->LPM_State == LPM_L1)
- {
- hpcd->LPM_State = LPM_L0;
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->LPMCallback(hpcd, PCD_LPM_L0_ACTIVE);
- #else
- HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- else
- {
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->ResumeCallback(hpcd);
- #else
- HAL_PCD_ResumeCallback(hpcd);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
- }
- /* Handle Suspend Interrupt */
- if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP))
- {
- if ((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
- {
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->SuspendCallback(hpcd);
- #else
- HAL_PCD_SuspendCallback(hpcd);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
- }
- /* Handle LPM Interrupt */
- if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT))
- {
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT);
- if (hpcd->LPM_State == LPM_L0)
- {
- hpcd->LPM_State = LPM_L1;
- hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >> 2U;
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->LPMCallback(hpcd, PCD_LPM_L1_ACTIVE);
- #else
- HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- else
- {
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->SuspendCallback(hpcd);
- #else
- HAL_PCD_SuspendCallback(hpcd);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- }
- /* Handle Reset Interrupt */
- if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST))
- {
- USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
- (void)USB_FlushTxFifo(hpcd->Instance, 0x10U);
- for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
- {
- USBx_INEP(i)->DIEPINT = 0xFB7FU;
- USBx_INEP(i)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
- USBx_OUTEP(i)->DOEPINT = 0xFB7FU;
- USBx_OUTEP(i)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
- USBx_OUTEP(i)->DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
- }
- USBx_DEVICE->DAINTMSK |= 0x10001U;
- if (hpcd->Init.use_dedicated_ep1 != 0U)
- {
- USBx_DEVICE->DOUTEP1MSK |= USB_OTG_DOEPMSK_STUPM |
- USB_OTG_DOEPMSK_XFRCM |
- USB_OTG_DOEPMSK_EPDM;
- USBx_DEVICE->DINEP1MSK |= USB_OTG_DIEPMSK_TOM |
- USB_OTG_DIEPMSK_XFRCM |
- USB_OTG_DIEPMSK_EPDM;
- }
- else
- {
- USBx_DEVICE->DOEPMSK |= USB_OTG_DOEPMSK_STUPM |
- USB_OTG_DOEPMSK_XFRCM |
- USB_OTG_DOEPMSK_EPDM |
- USB_OTG_DOEPMSK_OTEPSPRM |
- USB_OTG_DOEPMSK_NAKM;
- USBx_DEVICE->DIEPMSK |= USB_OTG_DIEPMSK_TOM |
- USB_OTG_DIEPMSK_XFRCM |
- USB_OTG_DIEPMSK_EPDM;
- }
- /* Set Default Address to 0 */
- USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
- /* setup EP0 to receive SETUP packets */
- (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t)hpcd->Init.dma_enable,
- (uint8_t *)hpcd->Setup);
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
- }
- /* Handle Enumeration done Interrupt */
- if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE))
- {
- (void)USB_ActivateSetup(hpcd->Instance);
- hpcd->Init.speed = USB_GetDevSpeed(hpcd->Instance);
- /* Set USB Turnaround time */
- (void)USB_SetTurnaroundTime(hpcd->Instance,
- HAL_RCC_GetHCLKFreq(),
- (uint8_t)hpcd->Init.speed);
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->ResetCallback(hpcd);
- #else
- HAL_PCD_ResetCallback(hpcd);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE);
- }
- /* Handle SOF Interrupt */
- if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SOF))
- {
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->SOFCallback(hpcd);
- #else
- HAL_PCD_SOFCallback(hpcd);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SOF);
- }
- /* Handle Global OUT NAK effective Interrupt */
- if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_BOUTNAKEFF))
- {
- USBx->GINTMSK &= ~USB_OTG_GINTMSK_GONAKEFFM;
- for (epnum = 1U; epnum < hpcd->Init.dev_endpoints; epnum++)
- {
- if (hpcd->OUT_ep[epnum].is_iso_incomplete == 1U)
- {
- /* Abort current transaction and disable the EP */
- (void)HAL_PCD_EP_Abort(hpcd, (uint8_t)epnum);
- }
- }
- }
- /* Handle Incomplete ISO IN Interrupt */
- if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR))
- {
- for (epnum = 1U; epnum < hpcd->Init.dev_endpoints; epnum++)
- {
- RegVal = USBx_INEP(epnum)->DIEPCTL;
- if ((hpcd->IN_ep[epnum].type == EP_TYPE_ISOC) &&
- ((RegVal & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA))
- {
- hpcd->IN_ep[epnum].is_iso_incomplete = 1U;
- /* Abort current transaction and disable the EP */
- (void)HAL_PCD_EP_Abort(hpcd, (uint8_t)(epnum | 0x80U));
- }
- }
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR);
- }
- /* Handle Incomplete ISO OUT Interrupt */
- if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
- {
- for (epnum = 1U; epnum < hpcd->Init.dev_endpoints; epnum++)
- {
- RegVal = USBx_OUTEP(epnum)->DOEPCTL;
- if ((hpcd->OUT_ep[epnum].type == EP_TYPE_ISOC) &&
- ((RegVal & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA) &&
- ((RegVal & (0x1U << 16)) == (hpcd->FrameNumber & 0x1U)))
- {
- hpcd->OUT_ep[epnum].is_iso_incomplete = 1U;
- USBx->GINTMSK |= USB_OTG_GINTMSK_GONAKEFFM;
- if ((USBx->GINTSTS & USB_OTG_GINTSTS_BOUTNAKEFF) == 0U)
- {
- USBx_DEVICE->DCTL |= USB_OTG_DCTL_SGONAK;
- break;
- }
- }
- }
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
- }
- /* Handle Connection event Interrupt */
- if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT))
- {
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->ConnectCallback(hpcd);
- #else
- HAL_PCD_ConnectCallback(hpcd);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT);
- }
- /* Handle Disconnection event Interrupt */
- if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OTGINT))
- {
- RegVal = hpcd->Instance->GOTGINT;
- if ((RegVal & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET)
- {
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->DisconnectCallback(hpcd);
- #else
- HAL_PCD_DisconnectCallback(hpcd);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- hpcd->Instance->GOTGINT |= RegVal;
- }
- }
- }
- /**
- * @brief Handles PCD Wakeup interrupt request.
- * @param hpcd PCD handle
- * @retval HAL status
- */
- void HAL_PCD_WKUP_IRQHandler(PCD_HandleTypeDef *hpcd)
- {
- USB_OTG_GlobalTypeDef *USBx;
- USBx = hpcd->Instance;
- if ((USBx->CID & (0x1U << 8)) == 0U)
- {
- /* Clear EXTI pending Bit */
- __HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG();
- }
- else
- {
- /* Clear EXTI pending Bit */
- __HAL_USB_OTG_HS_WAKEUP_EXTI_CLEAR_FLAG();
- }
- }
- #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
- /**
- * @brief Data OUT stage callback.
- * @param hpcd PCD handle
- * @param epnum endpoint number
- * @retval None
- */
- __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hpcd);
- UNUSED(epnum);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_PCD_DataOutStageCallback could be implemented in the user file
- */
- }
- /**
- * @brief Data IN stage callback
- * @param hpcd PCD handle
- * @param epnum endpoint number
- * @retval None
- */
- __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hpcd);
- UNUSED(epnum);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_PCD_DataInStageCallback could be implemented in the user file
- */
- }
- /**
- * @brief Setup stage callback
- * @param hpcd PCD handle
- * @retval None
- */
- __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hpcd);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_PCD_SetupStageCallback could be implemented in the user file
- */
- }
- /**
- * @brief USB Start Of Frame callback.
- * @param hpcd PCD handle
- * @retval None
- */
- __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hpcd);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_PCD_SOFCallback could be implemented in the user file
- */
- }
- /**
- * @brief USB Reset callback.
- * @param hpcd PCD handle
- * @retval None
- */
- __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hpcd);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_PCD_ResetCallback could be implemented in the user file
- */
- }
- /**
- * @brief Suspend event callback.
- * @param hpcd PCD handle
- * @retval None
- */
- __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hpcd);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_PCD_SuspendCallback could be implemented in the user file
- */
- }
- /**
- * @brief Resume event callback.
- * @param hpcd PCD handle
- * @retval None
- */
- __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hpcd);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_PCD_ResumeCallback could be implemented in the user file
- */
- }
- /**
- * @brief Incomplete ISO OUT callback.
- * @param hpcd PCD handle
- * @param epnum endpoint number
- * @retval None
- */
- __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hpcd);
- UNUSED(epnum);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
- */
- }
- /**
- * @brief Incomplete ISO IN callback.
- * @param hpcd PCD handle
- * @param epnum endpoint number
- * @retval None
- */
- __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hpcd);
- UNUSED(epnum);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
- */
- }
- /**
- * @brief Connection event callback.
- * @param hpcd PCD handle
- * @retval None
- */
- __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hpcd);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_PCD_ConnectCallback could be implemented in the user file
- */
- }
- /**
- * @brief Disconnection event callback.
- * @param hpcd PCD handle
- * @retval None
- */
- __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hpcd);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_PCD_DisconnectCallback could be implemented in the user file
- */
- }
- /**
- * @}
- */
- /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
- * @brief management functions
- *
- @verbatim
- ===============================================================================
- ##### Peripheral Control functions #####
- ===============================================================================
- [..]
- This subsection provides a set of functions allowing to control the PCD data
- transfers.
- @endverbatim
- * @{
- */
- /**
- * @brief Connect the USB device
- * @param hpcd PCD handle
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
- {
- __HAL_LOCK(hpcd);
- (void)USB_DevConnect(hpcd->Instance);
- __HAL_UNLOCK(hpcd);
- return HAL_OK;
- }
- /**
- * @brief Disconnect the USB device.
- * @param hpcd PCD handle
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
- {
- __HAL_LOCK(hpcd);
- (void)USB_DevDisconnect(hpcd->Instance);
- __HAL_UNLOCK(hpcd);
- return HAL_OK;
- }
- /**
- * @brief Set the USB Device address.
- * @param hpcd PCD handle
- * @param address new device address
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
- {
- __HAL_LOCK(hpcd);
- hpcd->USB_Address = address;
- (void)USB_SetDevAddress(hpcd->Instance, address);
- __HAL_UNLOCK(hpcd);
- return HAL_OK;
- }
- /**
- * @brief Open and configure an endpoint.
- * @param hpcd PCD handle
- * @param ep_addr endpoint address
- * @param ep_mps endpoint max packet size
- * @param ep_type endpoint type
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
- uint16_t ep_mps, uint8_t ep_type)
- {
- HAL_StatusTypeDef ret = HAL_OK;
- PCD_EPTypeDef *ep;
- if ((ep_addr & 0x80U) == 0x80U)
- {
- ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
- ep->is_in = 1U;
- }
- else
- {
- ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
- ep->is_in = 0U;
- }
- ep->num = ep_addr & EP_ADDR_MSK;
- ep->maxpacket = ep_mps;
- ep->type = ep_type;
- if (ep->is_in != 0U)
- {
- /* Assign a Tx FIFO */
- ep->tx_fifo_num = ep->num;
- }
- /* Set initial data PID. */
- if (ep_type == EP_TYPE_BULK)
- {
- ep->data_pid_start = 0U;
- }
- __HAL_LOCK(hpcd);
- (void)USB_ActivateEndpoint(hpcd->Instance, ep);
- __HAL_UNLOCK(hpcd);
- return ret;
- }
- /**
- * @brief Deactivate an endpoint.
- * @param hpcd PCD handle
- * @param ep_addr endpoint address
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
- {
- PCD_EPTypeDef *ep;
- if ((ep_addr & 0x80U) == 0x80U)
- {
- ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
- ep->is_in = 1U;
- }
- else
- {
- ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
- ep->is_in = 0U;
- }
- ep->num = ep_addr & EP_ADDR_MSK;
- __HAL_LOCK(hpcd);
- (void)USB_DeactivateEndpoint(hpcd->Instance, ep);
- __HAL_UNLOCK(hpcd);
- return HAL_OK;
- }
- /**
- * @brief Receive an amount of data.
- * @param hpcd PCD handle
- * @param ep_addr endpoint address
- * @param pBuf pointer to the reception buffer
- * @param len amount of data to be received
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
- {
- PCD_EPTypeDef *ep;
- ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
- /*setup and start the Xfer */
- ep->xfer_buff = pBuf;
- ep->xfer_len = len;
- ep->xfer_count = 0U;
- ep->is_in = 0U;
- ep->num = ep_addr & EP_ADDR_MSK;
- if (hpcd->Init.dma_enable == 1U)
- {
- ep->dma_addr = (uint32_t)pBuf;
- }
- if ((ep_addr & EP_ADDR_MSK) == 0U)
- {
- (void)USB_EP0StartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
- }
- else
- {
- (void)USB_EPStartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
- }
- return HAL_OK;
- }
- /**
- * @brief Get Received Data Size
- * @param hpcd PCD handle
- * @param ep_addr endpoint address
- * @retval Data Size
- */
- uint32_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
- {
- return hpcd->OUT_ep[ep_addr & EP_ADDR_MSK].xfer_count;
- }
- /**
- * @brief Send an amount of data
- * @param hpcd PCD handle
- * @param ep_addr endpoint address
- * @param pBuf pointer to the transmission buffer
- * @param len amount of data to be sent
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
- {
- PCD_EPTypeDef *ep;
- ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
- /*setup and start the Xfer */
- ep->xfer_buff = pBuf;
- ep->xfer_len = len;
- ep->xfer_count = 0U;
- ep->is_in = 1U;
- ep->num = ep_addr & EP_ADDR_MSK;
- if (hpcd->Init.dma_enable == 1U)
- {
- ep->dma_addr = (uint32_t)pBuf;
- }
- if ((ep_addr & EP_ADDR_MSK) == 0U)
- {
- (void)USB_EP0StartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
- }
- else
- {
- (void)USB_EPStartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
- }
- return HAL_OK;
- }
- /**
- * @brief Set a STALL condition over an endpoint
- * @param hpcd PCD handle
- * @param ep_addr endpoint address
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
- {
- PCD_EPTypeDef *ep;
- if (((uint32_t)ep_addr & EP_ADDR_MSK) > hpcd->Init.dev_endpoints)
- {
- return HAL_ERROR;
- }
- if ((0x80U & ep_addr) == 0x80U)
- {
- ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
- ep->is_in = 1U;
- }
- else
- {
- ep = &hpcd->OUT_ep[ep_addr];
- ep->is_in = 0U;
- }
- ep->is_stall = 1U;
- ep->num = ep_addr & EP_ADDR_MSK;
- __HAL_LOCK(hpcd);
- (void)USB_EPSetStall(hpcd->Instance, ep);
- if ((ep_addr & EP_ADDR_MSK) == 0U)
- {
- (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t)hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
- }
- __HAL_UNLOCK(hpcd);
- return HAL_OK;
- }
- /**
- * @brief Clear a STALL condition over in an endpoint
- * @param hpcd PCD handle
- * @param ep_addr endpoint address
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
- {
- PCD_EPTypeDef *ep;
- if (((uint32_t)ep_addr & 0x0FU) > hpcd->Init.dev_endpoints)
- {
- return HAL_ERROR;
- }
- if ((0x80U & ep_addr) == 0x80U)
- {
- ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
- ep->is_in = 1U;
- }
- else
- {
- ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
- ep->is_in = 0U;
- }
- ep->is_stall = 0U;
- ep->num = ep_addr & EP_ADDR_MSK;
- __HAL_LOCK(hpcd);
- (void)USB_EPClearStall(hpcd->Instance, ep);
- __HAL_UNLOCK(hpcd);
- return HAL_OK;
- }
- /**
- * @brief Abort an USB EP transaction.
- * @param hpcd PCD handle
- * @param ep_addr endpoint address
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_EP_Abort(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
- {
- HAL_StatusTypeDef ret;
- PCD_EPTypeDef *ep;
- if ((0x80U & ep_addr) == 0x80U)
- {
- ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
- }
- else
- {
- ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
- }
- /* Stop Xfer */
- ret = USB_EPStopXfer(hpcd->Instance, ep);
- return ret;
- }
- /**
- * @brief Flush an endpoint
- * @param hpcd PCD handle
- * @param ep_addr endpoint address
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
- {
- __HAL_LOCK(hpcd);
- if ((ep_addr & 0x80U) == 0x80U)
- {
- (void)USB_FlushTxFifo(hpcd->Instance, (uint32_t)ep_addr & EP_ADDR_MSK);
- }
- else
- {
- (void)USB_FlushRxFifo(hpcd->Instance);
- }
- __HAL_UNLOCK(hpcd);
- return HAL_OK;
- }
- /**
- * @brief Activate remote wakeup signalling
- * @param hpcd PCD handle
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
- {
- return (USB_ActivateRemoteWakeup(hpcd->Instance));
- }
- /**
- * @brief De-activate remote wakeup signalling.
- * @param hpcd PCD handle
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
- {
- return (USB_DeActivateRemoteWakeup(hpcd->Instance));
- }
- /**
- * @}
- */
- /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
- * @brief Peripheral State functions
- *
- @verbatim
- ===============================================================================
- ##### Peripheral State functions #####
- ===============================================================================
- [..]
- This subsection permits to get in run-time the status of the peripheral
- and the data flow.
- @endverbatim
- * @{
- */
- /**
- * @brief Return the PCD handle state.
- * @param hpcd PCD handle
- * @retval HAL state
- */
- PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
- {
- return hpcd->State;
- }
- /**
- * @brief Set the USB Device high speed test mode.
- * @param hpcd PCD handle
- * @param testmode USB Device high speed test mode
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_PCD_SetTestMode(PCD_HandleTypeDef *hpcd, uint8_t testmode)
- {
- USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
- uint32_t USBx_BASE = (uint32_t)USBx;
- switch (testmode)
- {
- case TEST_J:
- case TEST_K:
- case TEST_SE0_NAK:
- case TEST_PACKET:
- case TEST_FORCE_EN:
- USBx_DEVICE->DCTL |= (uint32_t)testmode << 4;
- break;
- default:
- break;
- }
- return HAL_OK;
- }
- /**
- * @}
- */
- /**
- * @}
- */
- /* Private functions ---------------------------------------------------------*/
- /** @addtogroup PCD_Private_Functions
- * @{
- */
- #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
- /**
- * @brief Check FIFO for the next packet to be loaded.
- * @param hpcd PCD handle
- * @param epnum endpoint number
- * @retval HAL status
- */
- static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
- {
- USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
- uint32_t USBx_BASE = (uint32_t)USBx;
- USB_OTG_EPTypeDef *ep;
- uint32_t len;
- uint32_t len32b;
- uint32_t fifoemptymsk;
- ep = &hpcd->IN_ep[epnum];
- if (ep->xfer_count > ep->xfer_len)
- {
- return HAL_ERROR;
- }
- len = ep->xfer_len - ep->xfer_count;
- if (len > ep->maxpacket)
- {
- len = ep->maxpacket;
- }
- len32b = (len + 3U) / 4U;
- while (((USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) >= len32b) &&
- (ep->xfer_count < ep->xfer_len) && (ep->xfer_len != 0U))
- {
- /* Write the FIFO */
- len = ep->xfer_len - ep->xfer_count;
- if (len > ep->maxpacket)
- {
- len = ep->maxpacket;
- }
- len32b = (len + 3U) / 4U;
- (void)USB_WritePacket(USBx, ep->xfer_buff, (uint8_t)epnum, (uint16_t)len,
- (uint8_t)hpcd->Init.dma_enable);
- ep->xfer_buff += len;
- ep->xfer_count += len;
- }
- if (ep->xfer_len <= ep->xfer_count)
- {
- fifoemptymsk = (uint32_t)(0x1UL << (epnum & EP_ADDR_MSK));
- USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
- }
- return HAL_OK;
- }
- /**
- * @brief process EP OUT transfer complete interrupt.
- * @param hpcd PCD handle
- * @param epnum endpoint number
- * @retval HAL status
- */
- static HAL_StatusTypeDef PCD_EP_OutXfrComplete_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
- {
- USB_OTG_EPTypeDef *ep;
- USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t gSNPSiD = *(__IO uint32_t *)(&USBx->CID + 0x1U);
- uint32_t DoepintReg = USBx_OUTEP(epnum)->DOEPINT;
- if (hpcd->Init.dma_enable == 1U)
- {
- if ((DoepintReg & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP) /* Class C */
- {
- /* StupPktRcvd = 1 this is a setup packet */
- if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
- ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX))
- {
- CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
- }
- }
- else if ((DoepintReg & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR) /* Class E */
- {
- CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
- }
- else if ((DoepintReg & (USB_OTG_DOEPINT_STUP | USB_OTG_DOEPINT_OTEPSPR)) == 0U)
- {
- /* StupPktRcvd = 1 this is a setup packet */
- if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
- ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX))
- {
- CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
- }
- else
- {
- ep = &hpcd->OUT_ep[epnum];
- /* out data packet received over EP */
- ep->xfer_count = ep->xfer_size - (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
- if (epnum == 0U)
- {
- if (ep->xfer_len == 0U)
- {
- /* this is ZLP, so prepare EP0 for next setup */
- (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
- }
- else
- {
- ep->xfer_buff += ep->xfer_count;
- }
- }
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
- #else
- HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- }
- else
- {
- /* ... */
- }
- }
- else
- {
- if (gSNPSiD == USB_OTG_CORE_ID_310A)
- {
- /* StupPktRcvd = 1 this is a setup packet */
- if ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX)
- {
- CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
- }
- else
- {
- if ((DoepintReg & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
- {
- CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
- }
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
- #else
- HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- }
- else
- {
- if ((epnum == 0U) && (hpcd->OUT_ep[epnum].xfer_len == 0U))
- {
- /* this is ZLP, so prepare EP0 for next setup */
- (void)USB_EP0_OutStart(hpcd->Instance, 0U, (uint8_t *)hpcd->Setup);
- }
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
- #else
- HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- }
- return HAL_OK;
- }
- /**
- * @brief process EP OUT setup packet received interrupt.
- * @param hpcd PCD handle
- * @param epnum endpoint number
- * @retval HAL status
- */
- static HAL_StatusTypeDef PCD_EP_OutSetupPacket_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
- {
- USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t gSNPSiD = *(__IO uint32_t *)(&USBx->CID + 0x1U);
- uint32_t DoepintReg = USBx_OUTEP(epnum)->DOEPINT;
- if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
- ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX))
- {
- CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
- }
- /* Inform the upper layer that a setup packet is available */
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->SetupStageCallback(hpcd);
- #else
- HAL_PCD_SetupStageCallback(hpcd);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- if ((gSNPSiD > USB_OTG_CORE_ID_300A) && (hpcd->Init.dma_enable == 1U))
- {
- (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
- }
- return HAL_OK;
- }
- #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
- /**
- * @}
- */
- #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
- #endif /* HAL_PCD_MODULE_ENABLED */
- /**
- * @}
- */
- /**
- * @}
- */
|