1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728 |
- /**
- ******************************************************************************
- * @file stm32f1xx_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) 2016 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_CLK_ENABLE(); For USB Device FS peripheral
- (+++) __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
- (##) 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 "stm32f1xx_hal.h"
- /** @addtogroup STM32F1xx_HAL_Driver
- * @{
- */
- /** @defgroup PCD PCD
- * @brief PCD HAL module driver
- * @{
- */
- #ifdef HAL_PCD_MODULE_ENABLED
- #if defined (USB) || defined (USB_OTG_FS)
- /* 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)
- 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) */
- #if defined (USB)
- static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd);
- #if (USE_USB_DOUBLE_BUFFER == 1U)
- static HAL_StatusTypeDef HAL_PCD_EP_DB_Transmit(PCD_HandleTypeDef *hpcd, PCD_EPTypeDef *ep, uint16_t wEPVal);
- static uint16_t HAL_PCD_EP_DB_Receive(PCD_HandleTypeDef *hpcd, PCD_EPTypeDef *ep, uint16_t wEPVal);
- #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
- #endif /* defined (USB) */
- /**
- * @}
- */
- /* 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)
- {
- #if defined (USB_OTG_FS)
- USB_OTG_GlobalTypeDef *USBx;
- #endif /* defined (USB_OTG_FS) */
- 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));
- #if defined (USB_OTG_FS)
- USBx = hpcd->Instance;
- #endif /* defined (USB_OTG_FS) */
- 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;
- #if defined (USB_OTG_FS)
- /* Disable DMA mode for FS instance */
- if ((USBx->CID & (0x1U << 8)) == 0U)
- {
- hpcd->Init.dma_enable = 0U;
- }
- #endif /* defined (USB_OTG_FS) */
- /* 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;
- #if defined (USB_OTG_FS)
- hpcd->IN_ep[i].tx_fifo_num = i;
- #endif /* defined (USB_OTG_FS) */
- /* 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;
- (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 USB 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 USB 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;
- }
- #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);
- #if defined (USB)
- HAL_PCDEx_SetConnectionState(hpcd, 1U);
- #endif /* defined (USB) */
- (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);
- #if defined (USB)
- HAL_PCDEx_SetConnectionState(hpcd, 0U);
- #endif /* defined (USB) */
- (void)USB_DevDisconnect(hpcd->Instance);
- #if defined (USB_OTG_FS)
- (void)USB_FlushTxFifo(hpcd->Instance, 0x10U);
- #endif /* defined (USB_OTG_FS) */
- __HAL_UNLOCK(hpcd);
- return HAL_OK;
- }
- #if defined (USB_OTG_FS)
- /**
- * @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 (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 (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 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->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)
- {
- /* Clear EXTI pending Bit */
- __HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG();
- }
- #endif /* defined (USB_OTG_FS) */
- #if defined (USB)
- /**
- * @brief This function handles PCD interrupt request.
- * @param hpcd PCD handle
- * @retval HAL status
- */
- void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
- {
- uint32_t wIstr = USB_ReadInterrupts(hpcd->Instance);
- uint16_t store_ep[8];
- uint8_t i;
- if ((wIstr & USB_ISTR_CTR) == USB_ISTR_CTR)
- {
- /* servicing of the endpoint correct transfer interrupt */
- /* clear of the CTR flag into the sub */
- (void)PCD_EP_ISR_Handler(hpcd);
- return;
- }
- if ((wIstr & USB_ISTR_RESET) == USB_ISTR_RESET)
- {
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_RESET);
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->ResetCallback(hpcd);
- #else
- HAL_PCD_ResetCallback(hpcd);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- (void)HAL_PCD_SetAddress(hpcd, 0U);
- return;
- }
- if ((wIstr & USB_ISTR_PMAOVR) == USB_ISTR_PMAOVR)
- {
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_PMAOVR);
- return;
- }
- if ((wIstr & USB_ISTR_ERR) == USB_ISTR_ERR)
- {
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ERR);
- return;
- }
- if ((wIstr & USB_ISTR_WKUP) == USB_ISTR_WKUP)
- {
- hpcd->Instance->CNTR &= (uint16_t) ~(USB_CNTR_LP_MODE);
- hpcd->Instance->CNTR &= (uint16_t) ~(USB_CNTR_FSUSP);
- #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_ISTR_WKUP);
- return;
- }
- if ((wIstr & USB_ISTR_SUSP) == USB_ISTR_SUSP)
- {
- /* WA: To Clear Wakeup flag if raised with suspend signal */
- /* Store Endpoint registers */
- for (i = 0U; i < 8U; i++)
- {
- store_ep[i] = PCD_GET_ENDPOINT(hpcd->Instance, i);
- }
- /* FORCE RESET */
- hpcd->Instance->CNTR |= (uint16_t)(USB_CNTR_FRES);
- /* CLEAR RESET */
- hpcd->Instance->CNTR &= (uint16_t)(~USB_CNTR_FRES);
- /* wait for reset flag in ISTR */
- while ((hpcd->Instance->ISTR & USB_ISTR_RESET) == 0U)
- {
- }
- /* Clear Reset Flag */
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_RESET);
- /* Restore Registre */
- for (i = 0U; i < 8U; i++)
- {
- PCD_SET_ENDPOINT(hpcd->Instance, i, store_ep[i]);
- }
- /* Force low-power mode in the macrocell */
- hpcd->Instance->CNTR |= (uint16_t)USB_CNTR_FSUSP;
- /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SUSP);
- hpcd->Instance->CNTR |= (uint16_t)USB_CNTR_LP_MODE;
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->SuspendCallback(hpcd);
- #else
- HAL_PCD_SuspendCallback(hpcd);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- return;
- }
- if ((wIstr & USB_ISTR_SOF) == USB_ISTR_SOF)
- {
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SOF);
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->SOFCallback(hpcd);
- #else
- HAL_PCD_SOFCallback(hpcd);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- return;
- }
- if ((wIstr & USB_ISTR_ESOF) == USB_ISTR_ESOF)
- {
- /* clear ESOF flag in ISTR */
- __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ESOF);
- return;
- }
- }
- /**
- * @brief Handles PCD Wakeup interrupt request.
- * @param hpcd PCD handle
- * @retval HAL status
- */
- void HAL_PCD_WKUP_IRQHandler(PCD_HandleTypeDef *hpcd)
- {
- /* Clear EXTI pending Bit */
- __HAL_USB_WAKEUP_EXTI_CLEAR_FLAG();
- }
- #endif /* defined (USB) */
- /**
- * @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);
- #if defined (USB)
- HAL_PCDEx_SetConnectionState(hpcd, 1U);
- #endif /* defined (USB) */
- (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);
- #if defined (USB)
- HAL_PCDEx_SetConnectionState(hpcd, 0U);
- #endif /* defined (USB) */
- (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 defined (USB_OTG_FS)
- if (ep->is_in != 0U)
- {
- /* Assign a Tx FIFO */
- ep->tx_fifo_num = ep->num;
- }
- #endif /* defined (USB_OTG_FS) */
- /* 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;
- (void)USB_EPStartXfer(hpcd->Instance, ep);
- 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 const *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;
- #if defined (USB)
- ep->xfer_fill_db = 1U;
- ep->xfer_len_db = len;
- #endif /* defined (USB) */
- ep->xfer_count = 0U;
- ep->is_in = 1U;
- ep->num = ep_addr & EP_ADDR_MSK;
- (void)USB_EPStartXfer(hpcd->Instance, ep);
- 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->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 const *hpcd)
- {
- return hpcd->State;
- }
- /**
- * @}
- */
- /**
- * @}
- */
- /* Private functions ---------------------------------------------------------*/
- /** @addtogroup PCD_Private_Functions
- * @{
- */
- #if defined (USB_OTG_FS)
- /**
- * @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);
- 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_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_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 (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 */
- return HAL_OK;
- }
- #endif /* defined (USB_OTG_FS) */
- #if defined (USB)
- /**
- * @brief This function handles PCD Endpoint interrupt request.
- * @param hpcd PCD handle
- * @retval HAL status
- */
- static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
- {
- PCD_EPTypeDef *ep;
- uint16_t count;
- uint16_t wIstr;
- uint16_t wEPVal;
- uint16_t TxPctSize;
- uint8_t epindex;
- #if (USE_USB_DOUBLE_BUFFER != 1U)
- count = 0U;
- #endif /* USE_USB_DOUBLE_BUFFER */
- /* stay in loop while pending interrupts */
- while ((hpcd->Instance->ISTR & USB_ISTR_CTR) != 0U)
- {
- wIstr = hpcd->Instance->ISTR;
- /* extract highest priority endpoint number */
- epindex = (uint8_t)(wIstr & USB_ISTR_EP_ID);
- if (epindex == 0U)
- {
- /* Decode and service control endpoint interrupt */
- /* DIR bit = origin of the interrupt */
- if ((wIstr & USB_ISTR_DIR) == 0U)
- {
- /* DIR = 0 */
- /* DIR = 0 => IN int */
- /* DIR = 0 implies that (EP_CTR_TX = 1) always */
- PCD_CLEAR_TX_EP_CTR(hpcd->Instance, PCD_ENDP0);
- ep = &hpcd->IN_ep[0];
- ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
- ep->xfer_buff += ep->xfer_count;
- /* TX COMPLETE */
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->DataInStageCallback(hpcd, 0U);
- #else
- HAL_PCD_DataInStageCallback(hpcd, 0U);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- if ((hpcd->USB_Address > 0U) && (ep->xfer_len == 0U))
- {
- hpcd->Instance->DADDR = ((uint16_t)hpcd->USB_Address | USB_DADDR_EF);
- hpcd->USB_Address = 0U;
- }
- }
- else
- {
- /* DIR = 1 */
- /* DIR = 1 & CTR_RX => SETUP or OUT int */
- /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
- ep = &hpcd->OUT_ep[0];
- wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
- if ((wEPVal & USB_EP_SETUP) != 0U)
- {
- /* Get SETUP Packet */
- ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
- USB_ReadPMA(hpcd->Instance, (uint8_t *)hpcd->Setup,
- ep->pmaadress, (uint16_t)ep->xfer_count);
- /* SETUP bit kept frozen while CTR_RX = 1 */
- PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
- /* Process SETUP Packet*/
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->SetupStageCallback(hpcd);
- #else
- HAL_PCD_SetupStageCallback(hpcd);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- else if ((wEPVal & USB_EP_CTR_RX) != 0U)
- {
- PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
- /* Get Control Data OUT Packet */
- ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
- if ((ep->xfer_count != 0U) && (ep->xfer_buff != 0U))
- {
- USB_ReadPMA(hpcd->Instance, ep->xfer_buff,
- ep->pmaadress, (uint16_t)ep->xfer_count);
- ep->xfer_buff += ep->xfer_count;
- /* Process Control Data OUT Packet */
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->DataOutStageCallback(hpcd, 0U);
- #else
- HAL_PCD_DataOutStageCallback(hpcd, 0U);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- wEPVal = (uint16_t)PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
- if (((wEPVal & USB_EP_SETUP) == 0U) && ((wEPVal & USB_EP_RX_STRX) != USB_EP_RX_VALID))
- {
- PCD_SET_EP_RX_CNT(hpcd->Instance, PCD_ENDP0, ep->maxpacket);
- PCD_SET_EP_RX_STATUS(hpcd->Instance, PCD_ENDP0, USB_EP_RX_VALID);
- }
- }
- }
- }
- else
- {
- /* Decode and service non control endpoints interrupt */
- /* process related endpoint register */
- wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, epindex);
- if ((wEPVal & USB_EP_CTR_RX) != 0U)
- {
- /* clear int flag */
- PCD_CLEAR_RX_EP_CTR(hpcd->Instance, epindex);
- ep = &hpcd->OUT_ep[epindex];
- /* OUT Single Buffering */
- if (ep->doublebuffer == 0U)
- {
- count = (uint16_t)PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
- if (count != 0U)
- {
- USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, count);
- }
- }
- #if (USE_USB_DOUBLE_BUFFER == 1U)
- else
- {
- /* manage double buffer bulk out */
- if (ep->type == EP_TYPE_BULK)
- {
- count = HAL_PCD_EP_DB_Receive(hpcd, ep, wEPVal);
- }
- else /* manage double buffer iso out */
- {
- /* free EP OUT Buffer */
- PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
- if ((PCD_GET_ENDPOINT(hpcd->Instance, ep->num) & USB_EP_DTOG_RX) != 0U)
- {
- /* read from endpoint BUF0Addr buffer */
- count = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
- if (count != 0U)
- {
- USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
- }
- }
- else
- {
- /* read from endpoint BUF1Addr buffer */
- count = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
- if (count != 0U)
- {
- USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
- }
- }
- }
- }
- #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
- /* multi-packet on the NON control OUT endpoint */
- ep->xfer_count += count;
- ep->xfer_buff += count;
- if ((ep->xfer_len == 0U) || (count < ep->maxpacket))
- {
- /* RX COMPLETE */
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->DataOutStageCallback(hpcd, ep->num);
- #else
- HAL_PCD_DataOutStageCallback(hpcd, ep->num);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- else
- {
- (void)USB_EPStartXfer(hpcd->Instance, ep);
- }
- }
- if ((wEPVal & USB_EP_CTR_TX) != 0U)
- {
- ep = &hpcd->IN_ep[epindex];
- /* clear int flag */
- PCD_CLEAR_TX_EP_CTR(hpcd->Instance, epindex);
- if (ep->type == EP_TYPE_ISOC)
- {
- ep->xfer_len = 0U;
- #if (USE_USB_DOUBLE_BUFFER == 1U)
- if (ep->doublebuffer != 0U)
- {
- if ((wEPVal & USB_EP_DTOG_TX) != 0U)
- {
- PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
- }
- else
- {
- PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
- }
- }
- #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
- /* TX COMPLETE */
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->DataInStageCallback(hpcd, ep->num);
- #else
- HAL_PCD_DataInStageCallback(hpcd, ep->num);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- else
- {
- /* Manage Single Buffer Transaction */
- if ((wEPVal & USB_EP_KIND) == 0U)
- {
- /* multi-packet on the NON control IN endpoint */
- TxPctSize = (uint16_t)PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
- if (ep->xfer_len > TxPctSize)
- {
- ep->xfer_len -= TxPctSize;
- }
- else
- {
- ep->xfer_len = 0U;
- }
- /* Zero Length Packet? */
- if (ep->xfer_len == 0U)
- {
- /* TX COMPLETE */
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->DataInStageCallback(hpcd, ep->num);
- #else
- HAL_PCD_DataInStageCallback(hpcd, ep->num);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- }
- else
- {
- /* Transfer is not yet Done */
- ep->xfer_buff += TxPctSize;
- ep->xfer_count += TxPctSize;
- (void)USB_EPStartXfer(hpcd->Instance, ep);
- }
- }
- #if (USE_USB_DOUBLE_BUFFER == 1U)
- /* Double Buffer bulk IN (bulk transfer Len > Ep_Mps) */
- else
- {
- (void)HAL_PCD_EP_DB_Transmit(hpcd, ep, wEPVal);
- }
- #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
- }
- }
- }
- }
- return HAL_OK;
- }
- #if (USE_USB_DOUBLE_BUFFER == 1U)
- /**
- * @brief Manage double buffer bulk out transaction from ISR
- * @param hpcd PCD handle
- * @param ep current endpoint handle
- * @param wEPVal Last snapshot of EPRx register value taken in ISR
- * @retval HAL status
- */
- static uint16_t HAL_PCD_EP_DB_Receive(PCD_HandleTypeDef *hpcd,
- PCD_EPTypeDef *ep, uint16_t wEPVal)
- {
- uint16_t count;
- /* Manage Buffer0 OUT */
- if ((wEPVal & USB_EP_DTOG_RX) != 0U)
- {
- /* Get count of received Data on buffer0 */
- count = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
- if (ep->xfer_len >= count)
- {
- ep->xfer_len -= count;
- }
- else
- {
- ep->xfer_len = 0U;
- }
- if (ep->xfer_len == 0U)
- {
- /* set NAK to OUT endpoint since double buffer is enabled */
- PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_NAK);
- }
- /* Check if Buffer1 is in blocked state which requires to toggle */
- if ((wEPVal & USB_EP_DTOG_TX) != 0U)
- {
- PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
- }
- if (count != 0U)
- {
- USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
- }
- }
- /* Manage Buffer 1 DTOG_RX=0 */
- else
- {
- /* Get count of received data */
- count = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
- if (ep->xfer_len >= count)
- {
- ep->xfer_len -= count;
- }
- else
- {
- ep->xfer_len = 0U;
- }
- if (ep->xfer_len == 0U)
- {
- /* set NAK on the current endpoint */
- PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_NAK);
- }
- /*Need to FreeUser Buffer*/
- if ((wEPVal & USB_EP_DTOG_TX) == 0U)
- {
- PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
- }
- if (count != 0U)
- {
- USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
- }
- }
- return count;
- }
- /**
- * @brief Manage double buffer bulk IN transaction from ISR
- * @param hpcd PCD handle
- * @param ep current endpoint handle
- * @param wEPVal Last snapshot of EPRx register value taken in ISR
- * @retval HAL status
- */
- static HAL_StatusTypeDef HAL_PCD_EP_DB_Transmit(PCD_HandleTypeDef *hpcd,
- PCD_EPTypeDef *ep, uint16_t wEPVal)
- {
- uint32_t len;
- uint16_t TxPctSize;
- /* Data Buffer0 ACK received */
- if ((wEPVal & USB_EP_DTOG_TX) != 0U)
- {
- /* multi-packet on the NON control IN endpoint */
- TxPctSize = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
- if (ep->xfer_len > TxPctSize)
- {
- ep->xfer_len -= TxPctSize;
- }
- else
- {
- ep->xfer_len = 0U;
- }
- /* Transfer is completed */
- if (ep->xfer_len == 0U)
- {
- PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
- PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
- /* TX COMPLETE */
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->DataInStageCallback(hpcd, ep->num);
- #else
- HAL_PCD_DataInStageCallback(hpcd, ep->num);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- if ((wEPVal & USB_EP_DTOG_RX) != 0U)
- {
- PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
- }
- }
- else /* Transfer is not yet Done */
- {
- /* need to Free USB Buff */
- if ((wEPVal & USB_EP_DTOG_RX) != 0U)
- {
- PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
- }
- /* Still there is data to Fill in the next Buffer */
- if (ep->xfer_fill_db == 1U)
- {
- ep->xfer_buff += TxPctSize;
- ep->xfer_count += TxPctSize;
- /* Calculate the len of the new buffer to fill */
- if (ep->xfer_len_db >= ep->maxpacket)
- {
- len = ep->maxpacket;
- ep->xfer_len_db -= len;
- }
- else if (ep->xfer_len_db == 0U)
- {
- len = TxPctSize;
- ep->xfer_fill_db = 0U;
- }
- else
- {
- ep->xfer_fill_db = 0U;
- len = ep->xfer_len_db;
- ep->xfer_len_db = 0U;
- }
- /* Write remaining Data to Buffer */
- /* Set the Double buffer counter for pma buffer1 */
- PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, len);
- /* Copy user buffer to USB PMA */
- USB_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, (uint16_t)len);
- }
- }
- }
- else /* Data Buffer1 ACK received */
- {
- /* multi-packet on the NON control IN endpoint */
- TxPctSize = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
- if (ep->xfer_len >= TxPctSize)
- {
- ep->xfer_len -= TxPctSize;
- }
- else
- {
- ep->xfer_len = 0U;
- }
- /* Transfer is completed */
- if (ep->xfer_len == 0U)
- {
- PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
- PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
- /* TX COMPLETE */
- #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
- hpcd->DataInStageCallback(hpcd, ep->num);
- #else
- HAL_PCD_DataInStageCallback(hpcd, ep->num);
- #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
- /* need to Free USB Buff */
- if ((wEPVal & USB_EP_DTOG_RX) == 0U)
- {
- PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
- }
- }
- else /* Transfer is not yet Done */
- {
- /* need to Free USB Buff */
- if ((wEPVal & USB_EP_DTOG_RX) == 0U)
- {
- PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
- }
- /* Still there is data to Fill in the next Buffer */
- if (ep->xfer_fill_db == 1U)
- {
- ep->xfer_buff += TxPctSize;
- ep->xfer_count += TxPctSize;
- /* Calculate the len of the new buffer to fill */
- if (ep->xfer_len_db >= ep->maxpacket)
- {
- len = ep->maxpacket;
- ep->xfer_len_db -= len;
- }
- else if (ep->xfer_len_db == 0U)
- {
- len = TxPctSize;
- ep->xfer_fill_db = 0U;
- }
- else
- {
- len = ep->xfer_len_db;
- ep->xfer_len_db = 0U;
- ep->xfer_fill_db = 0;
- }
- /* Set the Double buffer counter for pmabuffer1 */
- PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, len);
- /* Copy the user buffer to USB PMA */
- USB_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, (uint16_t)len);
- }
- }
- }
- /*enable endpoint IN*/
- PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_VALID);
- return HAL_OK;
- }
- #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
- #endif /* defined (USB) */
- /**
- * @}
- */
- #endif /* defined (USB) || defined (USB_OTG_FS) */
- #endif /* HAL_PCD_MODULE_ENABLED */
- /**
- * @}
- */
- /**
- * @}
- */
|