12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897 |
- /**
- ******************************************************************************
- * @file stm32f1xx_ll_usb.c
- * @author MCD Application Team
- * @brief USB Low Layer HAL module driver.
- *
- * This file provides firmware functions to manage the following
- * functionalities of the USB Peripheral Controller:
- * + Initialization/de-initialization functions
- * + I/O 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 #####
- ==============================================================================
- [..]
- (#) Fill parameters of Init structure in USB_CfgTypeDef structure.
- (#) Call USB_CoreInit() API to initialize the USB Core peripheral.
- (#) The upper HAL HCD/PCD driver will call the right routines for its internal processes.
- @endverbatim
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f1xx_hal.h"
- /** @addtogroup STM32F1xx_LL_USB_DRIVER
- * @{
- */
- #if defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED)
- #if defined (USB) || defined (USB_OTG_FS)
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- #if defined (USB_OTG_FS)
- static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx);
- /* Exported functions --------------------------------------------------------*/
- /** @defgroup USB_LL_Exported_Functions USB Low Layer Exported Functions
- * @{
- */
- /** @defgroup USB_LL_Exported_Functions_Group1 Initialization/de-initialization functions
- * @brief Initialization and Configuration functions
- *
- @verbatim
- ===============================================================================
- ##### Initialization/de-initialization functions #####
- ===============================================================================
- @endverbatim
- * @{
- */
- /**
- * @brief Initializes the USB Core
- * @param USBx USB Instance
- * @param cfg pointer to a USB_OTG_CfgTypeDef structure that contains
- * the configuration information for the specified USBx peripheral.
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_CoreInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
- {
- HAL_StatusTypeDef ret;
- /* Select FS Embedded PHY */
- USBx->GUSBCFG |= USB_OTG_GUSBCFG_PHYSEL;
- /* Reset after a PHY select */
- ret = USB_CoreReset(USBx);
- /* Activate the USB Transceiver */
- USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
- return ret;
- }
- /**
- * @brief Set the USB turnaround time
- * @param USBx USB Instance
- * @param hclk: AHB clock frequency
- * @retval USB turnaround time In PHY Clocks number
- */
- HAL_StatusTypeDef USB_SetTurnaroundTime(USB_OTG_GlobalTypeDef *USBx,
- uint32_t hclk, uint8_t speed)
- {
- uint32_t UsbTrd;
- /* The USBTRD is configured according to the tables below, depending on AHB frequency
- used by application. In the low AHB frequency range it is used to stretch enough the USB response
- time to IN tokens, the USB turnaround time, so to compensate for the longer AHB read access
- latency to the Data FIFO */
- if (speed == USBD_FS_SPEED)
- {
- if ((hclk >= 14200000U) && (hclk < 15000000U))
- {
- /* hclk Clock Range between 14.2-15 MHz */
- UsbTrd = 0xFU;
- }
- else if ((hclk >= 15000000U) && (hclk < 16000000U))
- {
- /* hclk Clock Range between 15-16 MHz */
- UsbTrd = 0xEU;
- }
- else if ((hclk >= 16000000U) && (hclk < 17200000U))
- {
- /* hclk Clock Range between 16-17.2 MHz */
- UsbTrd = 0xDU;
- }
- else if ((hclk >= 17200000U) && (hclk < 18500000U))
- {
- /* hclk Clock Range between 17.2-18.5 MHz */
- UsbTrd = 0xCU;
- }
- else if ((hclk >= 18500000U) && (hclk < 20000000U))
- {
- /* hclk Clock Range between 18.5-20 MHz */
- UsbTrd = 0xBU;
- }
- else if ((hclk >= 20000000U) && (hclk < 21800000U))
- {
- /* hclk Clock Range between 20-21.8 MHz */
- UsbTrd = 0xAU;
- }
- else if ((hclk >= 21800000U) && (hclk < 24000000U))
- {
- /* hclk Clock Range between 21.8-24 MHz */
- UsbTrd = 0x9U;
- }
- else if ((hclk >= 24000000U) && (hclk < 27700000U))
- {
- /* hclk Clock Range between 24-27.7 MHz */
- UsbTrd = 0x8U;
- }
- else if ((hclk >= 27700000U) && (hclk < 32000000U))
- {
- /* hclk Clock Range between 27.7-32 MHz */
- UsbTrd = 0x7U;
- }
- else /* if(hclk >= 32000000) */
- {
- /* hclk Clock Range between 32-200 MHz */
- UsbTrd = 0x6U;
- }
- }
- else
- {
- UsbTrd = USBD_DEFAULT_TRDT_VALUE;
- }
- USBx->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT;
- USBx->GUSBCFG |= (uint32_t)((UsbTrd << 10) & USB_OTG_GUSBCFG_TRDT);
- return HAL_OK;
- }
- /**
- * @brief USB_EnableGlobalInt
- * Enables the controller's Global Int in the AHB Config reg
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_EnableGlobalInt(USB_OTG_GlobalTypeDef *USBx)
- {
- USBx->GAHBCFG |= USB_OTG_GAHBCFG_GINT;
- return HAL_OK;
- }
- /**
- * @brief USB_DisableGlobalInt
- * Disable the controller's Global Int in the AHB Config reg
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_DisableGlobalInt(USB_OTG_GlobalTypeDef *USBx)
- {
- USBx->GAHBCFG &= ~USB_OTG_GAHBCFG_GINT;
- return HAL_OK;
- }
- /**
- * @brief USB_SetCurrentMode Set functional mode
- * @param USBx Selected device
- * @param mode current core mode
- * This parameter can be one of these values:
- * @arg USB_DEVICE_MODE Peripheral mode
- * @arg USB_HOST_MODE Host mode
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_SetCurrentMode(USB_OTG_GlobalTypeDef *USBx, USB_ModeTypeDef mode)
- {
- uint32_t ms = 0U;
- USBx->GUSBCFG &= ~(USB_OTG_GUSBCFG_FHMOD | USB_OTG_GUSBCFG_FDMOD);
- if (mode == USB_HOST_MODE)
- {
- USBx->GUSBCFG |= USB_OTG_GUSBCFG_FHMOD;
- do
- {
- HAL_Delay(1U);
- ms++;
- } while ((USB_GetMode(USBx) != (uint32_t)USB_HOST_MODE) && (ms < 50U));
- }
- else if (mode == USB_DEVICE_MODE)
- {
- USBx->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD;
- do
- {
- HAL_Delay(1U);
- ms++;
- } while ((USB_GetMode(USBx) != (uint32_t)USB_DEVICE_MODE) && (ms < 50U));
- }
- else
- {
- return HAL_ERROR;
- }
- if (ms == 50U)
- {
- return HAL_ERROR;
- }
- return HAL_OK;
- }
- /**
- * @brief USB_DevInit Initializes the USB_OTG controller registers
- * for device mode
- * @param USBx Selected device
- * @param cfg pointer to a USB_OTG_CfgTypeDef structure that contains
- * the configuration information for the specified USBx peripheral.
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_DevInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
- {
- HAL_StatusTypeDef ret = HAL_OK;
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t i;
- for (i = 0U; i < 15U; i++)
- {
- USBx->DIEPTXF[i] = 0U;
- }
- /* Enable HW VBUS sensing */
- USBx->GCCFG |= USB_OTG_GCCFG_VBUSBSEN;
- /* Restart the Phy Clock */
- USBx_PCGCCTL = 0U;
- /* Set Core speed to Full speed mode */
- (void)USB_SetDevSpeed(USBx, USB_OTG_SPEED_FULL);
- /* Flush the FIFOs */
- if (USB_FlushTxFifo(USBx, 0x10U) != HAL_OK) /* all Tx FIFOs */
- {
- ret = HAL_ERROR;
- }
- if (USB_FlushRxFifo(USBx) != HAL_OK)
- {
- ret = HAL_ERROR;
- }
- /* Clear all pending Device Interrupts */
- USBx_DEVICE->DIEPMSK = 0U;
- USBx_DEVICE->DOEPMSK = 0U;
- USBx_DEVICE->DAINTMSK = 0U;
- for (i = 0U; i < cfg.dev_endpoints; i++)
- {
- if ((USBx_INEP(i)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
- {
- if (i == 0U)
- {
- USBx_INEP(i)->DIEPCTL = USB_OTG_DIEPCTL_SNAK;
- }
- else
- {
- USBx_INEP(i)->DIEPCTL = USB_OTG_DIEPCTL_EPDIS | USB_OTG_DIEPCTL_SNAK;
- }
- }
- else
- {
- USBx_INEP(i)->DIEPCTL = 0U;
- }
- USBx_INEP(i)->DIEPTSIZ = 0U;
- USBx_INEP(i)->DIEPINT = 0xFB7FU;
- }
- for (i = 0U; i < cfg.dev_endpoints; i++)
- {
- if ((USBx_OUTEP(i)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
- {
- if (i == 0U)
- {
- USBx_OUTEP(i)->DOEPCTL = USB_OTG_DOEPCTL_SNAK;
- }
- else
- {
- USBx_OUTEP(i)->DOEPCTL = USB_OTG_DOEPCTL_EPDIS | USB_OTG_DOEPCTL_SNAK;
- }
- }
- else
- {
- USBx_OUTEP(i)->DOEPCTL = 0U;
- }
- USBx_OUTEP(i)->DOEPTSIZ = 0U;
- USBx_OUTEP(i)->DOEPINT = 0xFB7FU;
- }
- USBx_DEVICE->DIEPMSK &= ~(USB_OTG_DIEPMSK_TXFURM);
- /* Disable all interrupts. */
- USBx->GINTMSK = 0U;
- /* Clear any pending interrupts */
- USBx->GINTSTS = 0xBFFFFFFFU;
- /* Enable the common interrupts */
- USBx->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
- /* Enable interrupts matching to the Device mode ONLY */
- USBx->GINTMSK |= USB_OTG_GINTMSK_USBSUSPM | USB_OTG_GINTMSK_USBRST |
- USB_OTG_GINTMSK_ENUMDNEM | USB_OTG_GINTMSK_IEPINT |
- USB_OTG_GINTMSK_OEPINT | USB_OTG_GINTMSK_IISOIXFRM |
- USB_OTG_GINTMSK_PXFRM_IISOOXFRM | USB_OTG_GINTMSK_WUIM;
- if (cfg.Sof_enable != 0U)
- {
- USBx->GINTMSK |= USB_OTG_GINTMSK_SOFM;
- }
- if (cfg.vbus_sensing_enable == 1U)
- {
- USBx->GINTMSK |= (USB_OTG_GINTMSK_SRQIM | USB_OTG_GINTMSK_OTGINT);
- }
- return ret;
- }
- /**
- * @brief USB_FlushTxFifo Flush a Tx FIFO
- * @param USBx Selected device
- * @param num FIFO number
- * This parameter can be a value from 1 to 15
- 15 means Flush all Tx FIFOs
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_FlushTxFifo(USB_OTG_GlobalTypeDef *USBx, uint32_t num)
- {
- __IO uint32_t count = 0U;
- /* Wait for AHB master IDLE state. */
- do
- {
- count++;
- if (count > 200000U)
- {
- return HAL_TIMEOUT;
- }
- } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
- /* Flush TX Fifo */
- count = 0U;
- USBx->GRSTCTL = (USB_OTG_GRSTCTL_TXFFLSH | (num << 6));
- do
- {
- count++;
- if (count > 200000U)
- {
- return HAL_TIMEOUT;
- }
- } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_TXFFLSH) == USB_OTG_GRSTCTL_TXFFLSH);
- return HAL_OK;
- }
- /**
- * @brief USB_FlushRxFifo Flush Rx FIFO
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_FlushRxFifo(USB_OTG_GlobalTypeDef *USBx)
- {
- __IO uint32_t count = 0U;
- /* Wait for AHB master IDLE state. */
- do
- {
- count++;
- if (count > 200000U)
- {
- return HAL_TIMEOUT;
- }
- } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
- /* Flush RX Fifo */
- count = 0U;
- USBx->GRSTCTL = USB_OTG_GRSTCTL_RXFFLSH;
- do
- {
- count++;
- if (count > 200000U)
- {
- return HAL_TIMEOUT;
- }
- } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_RXFFLSH) == USB_OTG_GRSTCTL_RXFFLSH);
- return HAL_OK;
- }
- /**
- * @brief USB_SetDevSpeed Initializes the DevSpd field of DCFG register
- * depending the PHY type and the enumeration speed of the device.
- * @param USBx Selected device
- * @param speed device speed
- * This parameter can be one of these values:
- * @arg USB_OTG_SPEED_FULL: Full speed mode
- * @retval Hal status
- */
- HAL_StatusTypeDef USB_SetDevSpeed(USB_OTG_GlobalTypeDef *USBx, uint8_t speed)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- USBx_DEVICE->DCFG |= speed;
- return HAL_OK;
- }
- /**
- * @brief USB_GetDevSpeed Return the Dev Speed
- * @param USBx Selected device
- * @retval speed device speed
- * This parameter can be one of these values:
- * @arg USBD_FS_SPEED: Full speed mode
- */
- uint8_t USB_GetDevSpeed(USB_OTG_GlobalTypeDef *USBx)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint8_t speed;
- uint32_t DevEnumSpeed = USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD;
- if ((DevEnumSpeed == DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ) ||
- (DevEnumSpeed == DSTS_ENUMSPD_FS_PHY_48MHZ))
- {
- speed = USBD_FS_SPEED;
- }
- else
- {
- speed = 0xFU;
- }
- return speed;
- }
- /**
- * @brief Activate and configure an endpoint
- * @param USBx Selected device
- * @param ep pointer to endpoint structure
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_ActivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t epnum = (uint32_t)ep->num;
- if (ep->is_in == 1U)
- {
- USBx_DEVICE->DAINTMSK |= USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK));
- if ((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_USBAEP) == 0U)
- {
- USBx_INEP(epnum)->DIEPCTL |= (ep->maxpacket & USB_OTG_DIEPCTL_MPSIZ) |
- ((uint32_t)ep->type << 18) | (epnum << 22) |
- USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
- USB_OTG_DIEPCTL_USBAEP;
- }
- }
- else
- {
- USBx_DEVICE->DAINTMSK |= USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16);
- if (((USBx_OUTEP(epnum)->DOEPCTL) & USB_OTG_DOEPCTL_USBAEP) == 0U)
- {
- USBx_OUTEP(epnum)->DOEPCTL |= (ep->maxpacket & USB_OTG_DOEPCTL_MPSIZ) |
- ((uint32_t)ep->type << 18) |
- USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
- USB_OTG_DOEPCTL_USBAEP;
- }
- }
- return HAL_OK;
- }
- /**
- * @brief Activate and configure a dedicated endpoint
- * @param USBx Selected device
- * @param ep pointer to endpoint structure
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_ActivateDedicatedEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t epnum = (uint32_t)ep->num;
- /* Read DEPCTLn register */
- if (ep->is_in == 1U)
- {
- if (((USBx_INEP(epnum)->DIEPCTL) & USB_OTG_DIEPCTL_USBAEP) == 0U)
- {
- USBx_INEP(epnum)->DIEPCTL |= (ep->maxpacket & USB_OTG_DIEPCTL_MPSIZ) |
- ((uint32_t)ep->type << 18) | (epnum << 22) |
- USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
- USB_OTG_DIEPCTL_USBAEP;
- }
- USBx_DEVICE->DEACHMSK |= USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK));
- }
- else
- {
- if (((USBx_OUTEP(epnum)->DOEPCTL) & USB_OTG_DOEPCTL_USBAEP) == 0U)
- {
- USBx_OUTEP(epnum)->DOEPCTL |= (ep->maxpacket & USB_OTG_DOEPCTL_MPSIZ) |
- ((uint32_t)ep->type << 18) | (epnum << 22) |
- USB_OTG_DOEPCTL_USBAEP;
- }
- USBx_DEVICE->DEACHMSK |= USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16);
- }
- return HAL_OK;
- }
- /**
- * @brief De-activate and de-initialize an endpoint
- * @param USBx Selected device
- * @param ep pointer to endpoint structure
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_DeactivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t epnum = (uint32_t)ep->num;
- /* Read DEPCTLn register */
- if (ep->is_in == 1U)
- {
- if ((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
- {
- USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SNAK;
- USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_EPDIS;
- }
- USBx_DEVICE->DEACHMSK &= ~(USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK)));
- USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK)));
- USBx_INEP(epnum)->DIEPCTL &= ~(USB_OTG_DIEPCTL_USBAEP |
- USB_OTG_DIEPCTL_MPSIZ |
- USB_OTG_DIEPCTL_TXFNUM |
- USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
- USB_OTG_DIEPCTL_EPTYP);
- }
- else
- {
- if ((USBx_OUTEP(epnum)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
- {
- USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
- USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_EPDIS;
- }
- USBx_DEVICE->DEACHMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16));
- USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16));
- USBx_OUTEP(epnum)->DOEPCTL &= ~(USB_OTG_DOEPCTL_USBAEP |
- USB_OTG_DOEPCTL_MPSIZ |
- USB_OTG_DOEPCTL_SD0PID_SEVNFRM |
- USB_OTG_DOEPCTL_EPTYP);
- }
- return HAL_OK;
- }
- /**
- * @brief De-activate and de-initialize a dedicated endpoint
- * @param USBx Selected device
- * @param ep pointer to endpoint structure
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_DeactivateDedicatedEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t epnum = (uint32_t)ep->num;
- /* Read DEPCTLn register */
- if (ep->is_in == 1U)
- {
- if ((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
- {
- USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SNAK;
- USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_EPDIS;
- }
- USBx_INEP(epnum)->DIEPCTL &= ~ USB_OTG_DIEPCTL_USBAEP;
- USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK)));
- }
- else
- {
- if ((USBx_OUTEP(epnum)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
- {
- USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
- USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_EPDIS;
- }
- USBx_OUTEP(epnum)->DOEPCTL &= ~USB_OTG_DOEPCTL_USBAEP;
- USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16));
- }
- return HAL_OK;
- }
- /**
- * @brief USB_EPStartXfer : setup and starts a transfer over an EP
- * @param USBx Selected device
- * @param ep pointer to endpoint structure
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_EPStartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t epnum = (uint32_t)ep->num;
- uint16_t pktcnt;
- /* IN endpoint */
- if (ep->is_in == 1U)
- {
- /* Zero Length Packet? */
- if (ep->xfer_len == 0U)
- {
- USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
- USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1U << 19));
- USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
- }
- else
- {
- /* Program the transfer size and packet count
- * as follows: xfersize = N * maxpacket +
- * short_packet pktcnt = N + (short_packet
- * exist ? 1 : 0)
- */
- USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
- USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
- if (epnum == 0U)
- {
- if (ep->xfer_len > ep->maxpacket)
- {
- ep->xfer_len = ep->maxpacket;
- }
- USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1U << 19));
- }
- else
- {
- USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT &
- (((ep->xfer_len + ep->maxpacket - 1U) / ep->maxpacket) << 19));
- }
- USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_XFRSIZ & ep->xfer_len);
- if (ep->type == EP_TYPE_ISOC)
- {
- USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_MULCNT);
- USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_MULCNT & (1U << 29));
- }
- }
- /* EP enable, IN data in FIFO */
- USBx_INEP(epnum)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
- if (ep->type != EP_TYPE_ISOC)
- {
- /* Enable the Tx FIFO Empty Interrupt for this EP */
- if (ep->xfer_len > 0U)
- {
- USBx_DEVICE->DIEPEMPMSK |= 1UL << (ep->num & EP_ADDR_MSK);
- }
- }
- else
- {
- if ((USBx_DEVICE->DSTS & (1U << 8)) == 0U)
- {
- USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SODDFRM;
- }
- else
- {
- USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM;
- }
- (void)USB_WritePacket(USBx, ep->xfer_buff, ep->num, (uint16_t)ep->xfer_len);
- }
- }
- else /* OUT endpoint */
- {
- /* Program the transfer size and packet count as follows:
- * pktcnt = N
- * xfersize = N * maxpacket
- */
- USBx_OUTEP(epnum)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_XFRSIZ);
- USBx_OUTEP(epnum)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_PKTCNT);
- if (epnum == 0U)
- {
- if (ep->xfer_len > 0U)
- {
- ep->xfer_len = ep->maxpacket;
- }
- /* Store transfer size, for EP0 this is equal to endpoint max packet size */
- ep->xfer_size = ep->maxpacket;
- USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & ep->xfer_size);
- USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19));
- }
- else
- {
- if (ep->xfer_len == 0U)
- {
- USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & ep->maxpacket);
- USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19));
- }
- else
- {
- pktcnt = (uint16_t)((ep->xfer_len + ep->maxpacket - 1U) / ep->maxpacket);
- ep->xfer_size = ep->maxpacket * pktcnt;
- USBx_OUTEP(epnum)->DOEPTSIZ |= USB_OTG_DOEPTSIZ_PKTCNT & ((uint32_t)pktcnt << 19);
- USBx_OUTEP(epnum)->DOEPTSIZ |= USB_OTG_DOEPTSIZ_XFRSIZ & ep->xfer_size;
- }
- }
- if (ep->type == EP_TYPE_ISOC)
- {
- if ((USBx_DEVICE->DSTS & (1U << 8)) == 0U)
- {
- USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SODDFRM;
- }
- else
- {
- USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM;
- }
- }
- /* EP enable */
- USBx_OUTEP(epnum)->DOEPCTL |= (USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
- }
- return HAL_OK;
- }
- /**
- * @brief USB_EPStoptXfer Stop transfer on an EP
- * @param USBx usb device instance
- * @param ep pointer to endpoint structure
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_EPStopXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
- {
- __IO uint32_t count = 0U;
- HAL_StatusTypeDef ret = HAL_OK;
- uint32_t USBx_BASE = (uint32_t)USBx;
- /* IN endpoint */
- if (ep->is_in == 1U)
- {
- /* EP enable, IN data in FIFO */
- if (((USBx_INEP(ep->num)->DIEPCTL) & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
- {
- USBx_INEP(ep->num)->DIEPCTL |= (USB_OTG_DIEPCTL_SNAK);
- USBx_INEP(ep->num)->DIEPCTL |= (USB_OTG_DIEPCTL_EPDIS);
- do
- {
- count++;
- if (count > 10000U)
- {
- ret = HAL_ERROR;
- break;
- }
- } while (((USBx_INEP(ep->num)->DIEPCTL) & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA);
- }
- }
- else /* OUT endpoint */
- {
- if (((USBx_OUTEP(ep->num)->DOEPCTL) & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
- {
- USBx_OUTEP(ep->num)->DOEPCTL |= (USB_OTG_DOEPCTL_SNAK);
- USBx_OUTEP(ep->num)->DOEPCTL |= (USB_OTG_DOEPCTL_EPDIS);
- do
- {
- count++;
- if (count > 10000U)
- {
- ret = HAL_ERROR;
- break;
- }
- } while (((USBx_OUTEP(ep->num)->DOEPCTL) & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA);
- }
- }
- return ret;
- }
- /**
- * @brief USB_WritePacket : Writes a packet into the Tx FIFO associated
- * with the EP/channel
- * @param USBx Selected device
- * @param src pointer to source buffer
- * @param ch_ep_num endpoint or host channel number
- * @param len Number of bytes to write
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_WritePacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *src,
- uint8_t ch_ep_num, uint16_t len)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint8_t *pSrc = src;
- uint32_t count32b;
- uint32_t i;
- count32b = ((uint32_t)len + 3U) / 4U;
- for (i = 0U; i < count32b; i++)
- {
- USBx_DFIFO((uint32_t)ch_ep_num) = __UNALIGNED_UINT32_READ(pSrc);
- pSrc++;
- pSrc++;
- pSrc++;
- pSrc++;
- }
- return HAL_OK;
- }
- /**
- * @brief USB_ReadPacket : read a packet from the RX FIFO
- * @param USBx Selected device
- * @param dest source pointer
- * @param len Number of bytes to read
- * @retval pointer to destination buffer
- */
- void *USB_ReadPacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *dest, uint16_t len)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint8_t *pDest = dest;
- uint32_t pData;
- uint32_t i;
- uint32_t count32b = (uint32_t)len >> 2U;
- uint16_t remaining_bytes = len % 4U;
- for (i = 0U; i < count32b; i++)
- {
- __UNALIGNED_UINT32_WRITE(pDest, USBx_DFIFO(0U));
- pDest++;
- pDest++;
- pDest++;
- pDest++;
- }
- /* When Number of data is not word aligned, read the remaining byte */
- if (remaining_bytes != 0U)
- {
- i = 0U;
- __UNALIGNED_UINT32_WRITE(&pData, USBx_DFIFO(0U));
- do
- {
- *(uint8_t *)pDest = (uint8_t)(pData >> (8U * (uint8_t)(i)));
- i++;
- pDest++;
- remaining_bytes--;
- } while (remaining_bytes != 0U);
- }
- return ((void *)pDest);
- }
- /**
- * @brief USB_EPSetStall : set a stall condition over an EP
- * @param USBx Selected device
- * @param ep pointer to endpoint structure
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_EPSetStall(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t epnum = (uint32_t)ep->num;
- if (ep->is_in == 1U)
- {
- if (((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == 0U) && (epnum != 0U))
- {
- USBx_INEP(epnum)->DIEPCTL &= ~(USB_OTG_DIEPCTL_EPDIS);
- }
- USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_STALL;
- }
- else
- {
- if (((USBx_OUTEP(epnum)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == 0U) && (epnum != 0U))
- {
- USBx_OUTEP(epnum)->DOEPCTL &= ~(USB_OTG_DOEPCTL_EPDIS);
- }
- USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_STALL;
- }
- return HAL_OK;
- }
- /**
- * @brief USB_EPClearStall : Clear a stall condition over an EP
- * @param USBx Selected device
- * @param ep pointer to endpoint structure
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_EPClearStall(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t epnum = (uint32_t)ep->num;
- if (ep->is_in == 1U)
- {
- USBx_INEP(epnum)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
- if ((ep->type == EP_TYPE_INTR) || (ep->type == EP_TYPE_BULK))
- {
- USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM; /* DATA0 */
- }
- }
- else
- {
- USBx_OUTEP(epnum)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
- if ((ep->type == EP_TYPE_INTR) || (ep->type == EP_TYPE_BULK))
- {
- USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM; /* DATA0 */
- }
- }
- return HAL_OK;
- }
- /**
- * @brief USB_StopDevice : Stop the usb device mode
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_StopDevice(USB_OTG_GlobalTypeDef *USBx)
- {
- HAL_StatusTypeDef ret;
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t i;
- /* Clear Pending interrupt */
- for (i = 0U; i < 15U; i++)
- {
- USBx_INEP(i)->DIEPINT = 0xFB7FU;
- USBx_OUTEP(i)->DOEPINT = 0xFB7FU;
- }
- /* Clear interrupt masks */
- USBx_DEVICE->DIEPMSK = 0U;
- USBx_DEVICE->DOEPMSK = 0U;
- USBx_DEVICE->DAINTMSK = 0U;
- /* Flush the FIFO */
- ret = USB_FlushRxFifo(USBx);
- if (ret != HAL_OK)
- {
- return ret;
- }
- ret = USB_FlushTxFifo(USBx, 0x10U);
- if (ret != HAL_OK)
- {
- return ret;
- }
- return ret;
- }
- /**
- * @brief USB_SetDevAddress : Stop the usb device mode
- * @param USBx Selected device
- * @param address new device address to be assigned
- * This parameter can be a value from 0 to 255
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_SetDevAddress(USB_OTG_GlobalTypeDef *USBx, uint8_t address)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- USBx_DEVICE->DCFG &= ~(USB_OTG_DCFG_DAD);
- USBx_DEVICE->DCFG |= ((uint32_t)address << 4) & USB_OTG_DCFG_DAD;
- return HAL_OK;
- }
- /**
- * @brief USB_DevConnect : Connect the USB device by enabling Rpu
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_DevConnect(USB_OTG_GlobalTypeDef *USBx)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- /* In case phy is stopped, ensure to ungate and restore the phy CLK */
- USBx_PCGCCTL &= ~(USB_OTG_PCGCCTL_STOPCLK | USB_OTG_PCGCCTL_GATECLK);
- USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_SDIS;
- return HAL_OK;
- }
- /**
- * @brief USB_DevDisconnect : Disconnect the USB device by disabling Rpu
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_DevDisconnect(USB_OTG_GlobalTypeDef *USBx)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- /* In case phy is stopped, ensure to ungate and restore the phy CLK */
- USBx_PCGCCTL &= ~(USB_OTG_PCGCCTL_STOPCLK | USB_OTG_PCGCCTL_GATECLK);
- USBx_DEVICE->DCTL |= USB_OTG_DCTL_SDIS;
- return HAL_OK;
- }
- /**
- * @brief USB_ReadInterrupts: return the global USB interrupt status
- * @param USBx Selected device
- * @retval USB Global Interrupt status
- */
- uint32_t USB_ReadInterrupts(USB_OTG_GlobalTypeDef const *USBx)
- {
- uint32_t tmpreg;
- tmpreg = USBx->GINTSTS;
- tmpreg &= USBx->GINTMSK;
- return tmpreg;
- }
- /**
- * @brief USB_ReadChInterrupts: return USB channel interrupt status
- * @param USBx Selected device
- * @param chnum Channel number
- * @retval USB Channel Interrupt status
- */
- uint32_t USB_ReadChInterrupts(USB_OTG_GlobalTypeDef *USBx, uint8_t chnum)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t tmpreg;
- tmpreg = USBx_HC(chnum)->HCINT;
- tmpreg &= USBx_HC(chnum)->HCINTMSK;
- return tmpreg;
- }
- /**
- * @brief USB_ReadDevAllOutEpInterrupt: return the USB device OUT endpoints interrupt status
- * @param USBx Selected device
- * @retval USB Device OUT EP interrupt status
- */
- uint32_t USB_ReadDevAllOutEpInterrupt(USB_OTG_GlobalTypeDef *USBx)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t tmpreg;
- tmpreg = USBx_DEVICE->DAINT;
- tmpreg &= USBx_DEVICE->DAINTMSK;
- return ((tmpreg & 0xffff0000U) >> 16);
- }
- /**
- * @brief USB_ReadDevAllInEpInterrupt: return the USB device IN endpoints interrupt status
- * @param USBx Selected device
- * @retval USB Device IN EP interrupt status
- */
- uint32_t USB_ReadDevAllInEpInterrupt(USB_OTG_GlobalTypeDef *USBx)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t tmpreg;
- tmpreg = USBx_DEVICE->DAINT;
- tmpreg &= USBx_DEVICE->DAINTMSK;
- return ((tmpreg & 0xFFFFU));
- }
- /**
- * @brief Returns Device OUT EP Interrupt register
- * @param USBx Selected device
- * @param epnum endpoint number
- * This parameter can be a value from 0 to 15
- * @retval Device OUT EP Interrupt register
- */
- uint32_t USB_ReadDevOutEPInterrupt(USB_OTG_GlobalTypeDef *USBx, uint8_t epnum)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t tmpreg;
- tmpreg = USBx_OUTEP((uint32_t)epnum)->DOEPINT;
- tmpreg &= USBx_DEVICE->DOEPMSK;
- return tmpreg;
- }
- /**
- * @brief Returns Device IN EP Interrupt register
- * @param USBx Selected device
- * @param epnum endpoint number
- * This parameter can be a value from 0 to 15
- * @retval Device IN EP Interrupt register
- */
- uint32_t USB_ReadDevInEPInterrupt(USB_OTG_GlobalTypeDef *USBx, uint8_t epnum)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t tmpreg;
- uint32_t msk;
- uint32_t emp;
- msk = USBx_DEVICE->DIEPMSK;
- emp = USBx_DEVICE->DIEPEMPMSK;
- msk |= ((emp >> (epnum & EP_ADDR_MSK)) & 0x1U) << 7;
- tmpreg = USBx_INEP((uint32_t)epnum)->DIEPINT & msk;
- return tmpreg;
- }
- /**
- * @brief USB_ClearInterrupts: clear a USB interrupt
- * @param USBx Selected device
- * @param interrupt flag
- * @retval None
- */
- void USB_ClearInterrupts(USB_OTG_GlobalTypeDef *USBx, uint32_t interrupt)
- {
- USBx->GINTSTS &= interrupt;
- }
- /**
- * @brief Returns USB core mode
- * @param USBx Selected device
- * @retval return core mode : Host or Device
- * This parameter can be one of these values:
- * 0 : Host
- * 1 : Device
- */
- uint32_t USB_GetMode(USB_OTG_GlobalTypeDef *USBx)
- {
- return ((USBx->GINTSTS) & 0x1U);
- }
- /**
- * @brief Activate EP0 for Setup transactions
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_ActivateSetup(USB_OTG_GlobalTypeDef *USBx)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- /* Set the MPS of the IN EP0 to 64 bytes */
- USBx_INEP(0U)->DIEPCTL &= ~USB_OTG_DIEPCTL_MPSIZ;
- USBx_DEVICE->DCTL |= USB_OTG_DCTL_CGINAK;
- return HAL_OK;
- }
- /**
- * @brief Prepare the EP0 to start the first control setup
- * @param USBx Selected device
- * @param psetup pointer to setup packet
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_EP0_OutStart(USB_OTG_GlobalTypeDef *USBx, uint8_t *psetup)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t gSNPSiD = *(__IO uint32_t *)(&USBx->CID + 0x1U);
- UNUSED(psetup);
- if (gSNPSiD > USB_OTG_CORE_ID_300A)
- {
- if ((USBx_OUTEP(0U)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
- {
- return HAL_OK;
- }
- }
- USBx_OUTEP(0U)->DOEPTSIZ = 0U;
- USBx_OUTEP(0U)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19));
- USBx_OUTEP(0U)->DOEPTSIZ |= (3U * 8U);
- USBx_OUTEP(0U)->DOEPTSIZ |= USB_OTG_DOEPTSIZ_STUPCNT;
- return HAL_OK;
- }
- /**
- * @brief Reset the USB Core (needed after USB clock settings change)
- * @param USBx Selected device
- * @retval HAL status
- */
- static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx)
- {
- __IO uint32_t count = 0U;
- /* Wait for AHB master IDLE state. */
- do
- {
- count++;
- if (count > 200000U)
- {
- return HAL_TIMEOUT;
- }
- } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
- /* Core Soft Reset */
- count = 0U;
- USBx->GRSTCTL |= USB_OTG_GRSTCTL_CSRST;
- do
- {
- count++;
- if (count > 200000U)
- {
- return HAL_TIMEOUT;
- }
- } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_CSRST) == USB_OTG_GRSTCTL_CSRST);
- return HAL_OK;
- }
- /**
- * @brief USB_HostInit : Initializes the USB OTG controller registers
- * for Host mode
- * @param USBx Selected device
- * @param cfg pointer to a USB_OTG_CfgTypeDef structure that contains
- * the configuration information for the specified USBx peripheral.
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_HostInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
- {
- HAL_StatusTypeDef ret = HAL_OK;
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t i;
- /* Restart the Phy Clock */
- USBx_PCGCCTL = 0U;
- /* Disable VBUS sensing */
- USBx->GCCFG &= ~(USB_OTG_GCCFG_VBUSASEN);
- USBx->GCCFG &= ~(USB_OTG_GCCFG_VBUSBSEN);
- /* Set default Max speed support */
- USBx_HOST->HCFG &= ~(USB_OTG_HCFG_FSLSS);
- /* Make sure the FIFOs are flushed. */
- if (USB_FlushTxFifo(USBx, 0x10U) != HAL_OK) /* all Tx FIFOs */
- {
- ret = HAL_ERROR;
- }
- if (USB_FlushRxFifo(USBx) != HAL_OK)
- {
- ret = HAL_ERROR;
- }
- /* Clear all pending HC Interrupts */
- for (i = 0U; i < cfg.Host_channels; i++)
- {
- USBx_HC(i)->HCINT = CLEAR_INTERRUPT_MASK;
- USBx_HC(i)->HCINTMSK = 0U;
- }
- /* Disable all interrupts. */
- USBx->GINTMSK = 0U;
- /* Clear any pending interrupts */
- USBx->GINTSTS = CLEAR_INTERRUPT_MASK;
- /* set Rx FIFO size */
- USBx->GRXFSIZ = 0x80U;
- USBx->DIEPTXF0_HNPTXFSIZ = (uint32_t)(((0x60U << 16) & USB_OTG_NPTXFD) | 0x80U);
- USBx->HPTXFSIZ = (uint32_t)(((0x40U << 16)& USB_OTG_HPTXFSIZ_PTXFD) | 0xE0U);
- /* Enable the common interrupts */
- USBx->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
- /* Enable interrupts matching to the Host mode ONLY */
- USBx->GINTMSK |= (USB_OTG_GINTMSK_PRTIM | USB_OTG_GINTMSK_HCIM | \
- USB_OTG_GINTMSK_SOFM | USB_OTG_GINTSTS_DISCINT | \
- USB_OTG_GINTMSK_PXFRM_IISOOXFRM | USB_OTG_GINTMSK_WUIM);
- return ret;
- }
- /**
- * @brief USB_InitFSLSPClkSel : Initializes the FSLSPClkSel field of the
- * HCFG register on the PHY type and set the right frame interval
- * @param USBx Selected device
- * @param freq clock frequency
- * This parameter can be one of these values:
- * HCFG_48_MHZ : Full Speed 48 MHz Clock
- * HCFG_6_MHZ : Low Speed 6 MHz Clock
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_InitFSLSPClkSel(USB_OTG_GlobalTypeDef *USBx, uint8_t freq)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- USBx_HOST->HCFG &= ~(USB_OTG_HCFG_FSLSPCS);
- USBx_HOST->HCFG |= (uint32_t)freq & USB_OTG_HCFG_FSLSPCS;
- if (freq == HCFG_48_MHZ)
- {
- USBx_HOST->HFIR = HFIR_48_MHZ;
- }
- else if (freq == HCFG_6_MHZ)
- {
- USBx_HOST->HFIR = HFIR_6_MHZ;
- }
- else
- {
- return HAL_ERROR;
- }
- return HAL_OK;
- }
- /**
- * @brief USB_OTG_ResetPort : Reset Host Port
- * @param USBx Selected device
- * @retval HAL status
- * @note (1)The application must wait at least 10 ms
- * before clearing the reset bit.
- */
- HAL_StatusTypeDef USB_ResetPort(USB_OTG_GlobalTypeDef *USBx)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- __IO uint32_t hprt0 = 0U;
- hprt0 = USBx_HPRT0;
- hprt0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |
- USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
- USBx_HPRT0 = (USB_OTG_HPRT_PRST | hprt0);
- HAL_Delay(100U); /* See Note #1 */
- USBx_HPRT0 = ((~USB_OTG_HPRT_PRST) & hprt0);
- HAL_Delay(10U);
- return HAL_OK;
- }
- /**
- * @brief USB_DriveVbus : activate or de-activate vbus
- * @param state VBUS state
- * This parameter can be one of these values:
- * 0 : Deactivate VBUS
- * 1 : Activate VBUS
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_DriveVbus(USB_OTG_GlobalTypeDef *USBx, uint8_t state)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- __IO uint32_t hprt0 = 0U;
- hprt0 = USBx_HPRT0;
- hprt0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |
- USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
- if (((hprt0 & USB_OTG_HPRT_PPWR) == 0U) && (state == 1U))
- {
- USBx_HPRT0 = (USB_OTG_HPRT_PPWR | hprt0);
- }
- if (((hprt0 & USB_OTG_HPRT_PPWR) == USB_OTG_HPRT_PPWR) && (state == 0U))
- {
- USBx_HPRT0 = ((~USB_OTG_HPRT_PPWR) & hprt0);
- }
- return HAL_OK;
- }
- /**
- * @brief Return Host Core speed
- * @param USBx Selected device
- * @retval speed : Host speed
- * This parameter can be one of these values:
- * @arg HCD_SPEED_FULL: Full speed mode
- * @arg HCD_SPEED_LOW: Low speed mode
- */
- uint32_t USB_GetHostSpeed(USB_OTG_GlobalTypeDef const *USBx)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- __IO uint32_t hprt0 = 0U;
- hprt0 = USBx_HPRT0;
- return ((hprt0 & USB_OTG_HPRT_PSPD) >> 17);
- }
- /**
- * @brief Return Host Current Frame number
- * @param USBx Selected device
- * @retval current frame number
- */
- uint32_t USB_GetCurrentFrame(USB_OTG_GlobalTypeDef const *USBx)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- return (USBx_HOST->HFNUM & USB_OTG_HFNUM_FRNUM);
- }
- /**
- * @brief Initialize a host channel
- * @param USBx Selected device
- * @param ch_num Channel number
- * This parameter can be a value from 1 to 15
- * @param epnum Endpoint number
- * This parameter can be a value from 1 to 15
- * @param dev_address Current device address
- * This parameter can be a value from 0 to 255
- * @param speed Current device speed
- * This parameter can be one of these values:
- * @arg USB_OTG_SPEED_FULL: Full speed mode
- * @arg USB_OTG_SPEED_LOW: Low speed mode
- * @param ep_type Endpoint Type
- * This parameter can be one of these values:
- * @arg EP_TYPE_CTRL: Control type
- * @arg EP_TYPE_ISOC: Isochronous type
- * @arg EP_TYPE_BULK: Bulk type
- * @arg EP_TYPE_INTR: Interrupt type
- * @param mps Max Packet Size
- * This parameter can be a value from 0 to 32K
- * @retval HAL state
- */
- HAL_StatusTypeDef USB_HC_Init(USB_OTG_GlobalTypeDef *USBx, uint8_t ch_num,
- uint8_t epnum, uint8_t dev_address, uint8_t speed,
- uint8_t ep_type, uint16_t mps)
- {
- HAL_StatusTypeDef ret = HAL_OK;
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t HCcharEpDir;
- uint32_t HCcharLowSpeed;
- uint32_t HostCoreSpeed;
- /* Clear old interrupt conditions for this host channel. */
- USBx_HC((uint32_t)ch_num)->HCINT = CLEAR_INTERRUPT_MASK;
- /* Enable channel interrupts required for this transfer. */
- switch (ep_type)
- {
- case EP_TYPE_CTRL:
- case EP_TYPE_BULK:
- USBx_HC((uint32_t)ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM |
- USB_OTG_HCINTMSK_STALLM |
- USB_OTG_HCINTMSK_TXERRM |
- USB_OTG_HCINTMSK_DTERRM |
- USB_OTG_HCINTMSK_AHBERR |
- USB_OTG_HCINTMSK_NAKM;
- if ((epnum & 0x80U) == 0x80U)
- {
- USBx_HC((uint32_t)ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
- }
- break;
- case EP_TYPE_INTR:
- USBx_HC((uint32_t)ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM |
- USB_OTG_HCINTMSK_STALLM |
- USB_OTG_HCINTMSK_TXERRM |
- USB_OTG_HCINTMSK_DTERRM |
- USB_OTG_HCINTMSK_NAKM |
- USB_OTG_HCINTMSK_AHBERR |
- USB_OTG_HCINTMSK_FRMORM;
- if ((epnum & 0x80U) == 0x80U)
- {
- USBx_HC((uint32_t)ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
- }
- break;
- case EP_TYPE_ISOC:
- USBx_HC((uint32_t)ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM |
- USB_OTG_HCINTMSK_ACKM |
- USB_OTG_HCINTMSK_AHBERR |
- USB_OTG_HCINTMSK_FRMORM;
- if ((epnum & 0x80U) == 0x80U)
- {
- USBx_HC((uint32_t)ch_num)->HCINTMSK |= (USB_OTG_HCINTMSK_TXERRM | USB_OTG_HCINTMSK_BBERRM);
- }
- break;
- default:
- ret = HAL_ERROR;
- break;
- }
- /* Enable host channel Halt interrupt */
- USBx_HC((uint32_t)ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_CHHM;
- /* Enable the top level host channel interrupt. */
- USBx_HOST->HAINTMSK |= 1UL << (ch_num & 0xFU);
- /* Make sure host channel interrupts are enabled. */
- USBx->GINTMSK |= USB_OTG_GINTMSK_HCIM;
- /* Program the HCCHAR register */
- if ((epnum & 0x80U) == 0x80U)
- {
- HCcharEpDir = (0x1U << 15) & USB_OTG_HCCHAR_EPDIR;
- }
- else
- {
- HCcharEpDir = 0U;
- }
- HostCoreSpeed = USB_GetHostSpeed(USBx);
- /* LS device plugged to HUB */
- if ((speed == HPRT0_PRTSPD_LOW_SPEED) && (HostCoreSpeed != HPRT0_PRTSPD_LOW_SPEED))
- {
- HCcharLowSpeed = (0x1U << 17) & USB_OTG_HCCHAR_LSDEV;
- }
- else
- {
- HCcharLowSpeed = 0U;
- }
- USBx_HC((uint32_t)ch_num)->HCCHAR = (((uint32_t)dev_address << 22) & USB_OTG_HCCHAR_DAD) |
- ((((uint32_t)epnum & 0x7FU) << 11) & USB_OTG_HCCHAR_EPNUM) |
- (((uint32_t)ep_type << 18) & USB_OTG_HCCHAR_EPTYP) |
- ((uint32_t)mps & USB_OTG_HCCHAR_MPSIZ) |
- USB_OTG_HCCHAR_MC_0 | HCcharEpDir | HCcharLowSpeed;
- if ((ep_type == EP_TYPE_INTR) || (ep_type == EP_TYPE_ISOC))
- {
- USBx_HC((uint32_t)ch_num)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM;
- }
- return ret;
- }
- /**
- * @brief Start a transfer over a host channel
- * @param USBx Selected device
- * @param hc pointer to host channel structure
- * @retval HAL state
- */
- HAL_StatusTypeDef USB_HC_StartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_HCTypeDef *hc)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t ch_num = (uint32_t)hc->ch_num;
- __IO uint32_t tmpreg;
- uint8_t is_oddframe;
- uint16_t len_words;
- uint16_t num_packets;
- uint16_t max_hc_pkt_count = HC_MAX_PKT_CNT;
- /* Compute the expected number of packets associated to the transfer */
- if (hc->xfer_len > 0U)
- {
- num_packets = (uint16_t)((hc->xfer_len + hc->max_packet - 1U) / hc->max_packet);
- if (num_packets > max_hc_pkt_count)
- {
- num_packets = max_hc_pkt_count;
- hc->XferSize = (uint32_t)num_packets * hc->max_packet;
- }
- }
- else
- {
- num_packets = 1U;
- }
- /*
- * For IN channel HCTSIZ.XferSize is expected to be an integer multiple of
- * max_packet size.
- */
- if (hc->ep_is_in != 0U)
- {
- hc->XferSize = (uint32_t)num_packets * hc->max_packet;
- }
- else
- {
- hc->XferSize = hc->xfer_len;
- }
- /* Initialize the HCTSIZn register */
- USBx_HC(ch_num)->HCTSIZ = (hc->XferSize & USB_OTG_HCTSIZ_XFRSIZ) |
- (((uint32_t)num_packets << 19) & USB_OTG_HCTSIZ_PKTCNT) |
- (((uint32_t)hc->data_pid << 29) & USB_OTG_HCTSIZ_DPID);
- is_oddframe = (((uint32_t)USBx_HOST->HFNUM & 0x01U) != 0U) ? 0U : 1U;
- USBx_HC(ch_num)->HCCHAR &= ~USB_OTG_HCCHAR_ODDFRM;
- USBx_HC(ch_num)->HCCHAR |= (uint32_t)is_oddframe << 29;
- /* Set host channel enable */
- tmpreg = USBx_HC(ch_num)->HCCHAR;
- tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
- /* make sure to set the correct ep direction */
- if (hc->ep_is_in != 0U)
- {
- tmpreg |= USB_OTG_HCCHAR_EPDIR;
- }
- else
- {
- tmpreg &= ~USB_OTG_HCCHAR_EPDIR;
- }
- tmpreg |= USB_OTG_HCCHAR_CHENA;
- USBx_HC(ch_num)->HCCHAR = tmpreg;
- if ((hc->ep_is_in == 0U) && (hc->xfer_len > 0U))
- {
- switch (hc->ep_type)
- {
- /* Non periodic transfer */
- case EP_TYPE_CTRL:
- case EP_TYPE_BULK:
- len_words = (uint16_t)((hc->xfer_len + 3U) / 4U);
- /* check if there is enough space in FIFO space */
- if (len_words > (USBx->HNPTXSTS & 0xFFFFU))
- {
- /* need to process data in nptxfempty interrupt */
- USBx->GINTMSK |= USB_OTG_GINTMSK_NPTXFEM;
- }
- break;
- /* Periodic transfer */
- case EP_TYPE_INTR:
- case EP_TYPE_ISOC:
- len_words = (uint16_t)((hc->xfer_len + 3U) / 4U);
- /* check if there is enough space in FIFO space */
- if (len_words > (USBx_HOST->HPTXSTS & 0xFFFFU)) /* split the transfer */
- {
- /* need to process data in ptxfempty interrupt */
- USBx->GINTMSK |= USB_OTG_GINTMSK_PTXFEM;
- }
- break;
- default:
- break;
- }
- /* Write packet into the Tx FIFO. */
- (void)USB_WritePacket(USBx, hc->xfer_buff, hc->ch_num, (uint16_t)hc->xfer_len);
- }
- return HAL_OK;
- }
- /**
- * @brief Read all host channel interrupts status
- * @param USBx Selected device
- * @retval HAL state
- */
- uint32_t USB_HC_ReadInterrupt(USB_OTG_GlobalTypeDef *USBx)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- return ((USBx_HOST->HAINT) & 0xFFFFU);
- }
- /**
- * @brief Halt a host channel
- * @param USBx Selected device
- * @param hc_num Host Channel number
- * This parameter can be a value from 1 to 15
- * @retval HAL state
- */
- HAL_StatusTypeDef USB_HC_Halt(USB_OTG_GlobalTypeDef *USBx, uint8_t hc_num)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t hcnum = (uint32_t)hc_num;
- __IO uint32_t count = 0U;
- uint32_t HcEpType = (USBx_HC(hcnum)->HCCHAR & USB_OTG_HCCHAR_EPTYP) >> 18;
- uint32_t ChannelEna = (USBx_HC(hcnum)->HCCHAR & USB_OTG_HCCHAR_CHENA) >> 31;
- uint32_t SplitEna = (USBx_HC(hcnum)->HCSPLT & USB_OTG_HCSPLT_SPLITEN) >> 31;
- /* In buffer DMA, Channel disable must not be programmed for non-split periodic channels.
- At the end of the next uframe/frame (in the worst case), the core generates a channel halted
- and disables the channel automatically. */
- if ((((USBx->GAHBCFG & USB_OTG_GAHBCFG_DMAEN) == USB_OTG_GAHBCFG_DMAEN) && (SplitEna == 0U)) &&
- ((ChannelEna == 0U) || (((HcEpType == HCCHAR_ISOC) || (HcEpType == HCCHAR_INTR)))))
- {
- return HAL_OK;
- }
- /* Check for space in the request queue to issue the halt. */
- if ((HcEpType == HCCHAR_CTRL) || (HcEpType == HCCHAR_BULK))
- {
- USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHDIS;
- if ((USBx->GAHBCFG & USB_OTG_GAHBCFG_DMAEN) == 0U)
- {
- if ((USBx->HNPTXSTS & (0xFFU << 16)) == 0U)
- {
- USBx_HC(hcnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHENA;
- USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
- do
- {
- count++;
- if (count > 1000U)
- {
- break;
- }
- } while ((USBx_HC(hcnum)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
- }
- else
- {
- USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
- }
- }
- else
- {
- USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
- }
- }
- else
- {
- USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHDIS;
- if ((USBx_HOST->HPTXSTS & (0xFFU << 16)) == 0U)
- {
- USBx_HC(hcnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHENA;
- USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
- do
- {
- count++;
- if (count > 1000U)
- {
- break;
- }
- } while ((USBx_HC(hcnum)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
- }
- else
- {
- USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
- }
- }
- return HAL_OK;
- }
- /**
- * @brief Initiate Do Ping protocol
- * @param USBx Selected device
- * @param hc_num Host Channel number
- * This parameter can be a value from 1 to 15
- * @retval HAL state
- */
- HAL_StatusTypeDef USB_DoPing(USB_OTG_GlobalTypeDef *USBx, uint8_t ch_num)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- uint32_t chnum = (uint32_t)ch_num;
- uint32_t num_packets = 1U;
- uint32_t tmpreg;
- USBx_HC(chnum)->HCTSIZ = ((num_packets << 19) & USB_OTG_HCTSIZ_PKTCNT) |
- USB_OTG_HCTSIZ_DOPING;
- /* Set host channel enable */
- tmpreg = USBx_HC(chnum)->HCCHAR;
- tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
- tmpreg |= USB_OTG_HCCHAR_CHENA;
- USBx_HC(chnum)->HCCHAR = tmpreg;
- return HAL_OK;
- }
- /**
- * @brief Stop Host Core
- * @param USBx Selected device
- * @retval HAL state
- */
- HAL_StatusTypeDef USB_StopHost(USB_OTG_GlobalTypeDef *USBx)
- {
- HAL_StatusTypeDef ret = HAL_OK;
- uint32_t USBx_BASE = (uint32_t)USBx;
- __IO uint32_t count = 0U;
- uint32_t value;
- uint32_t i;
- (void)USB_DisableGlobalInt(USBx);
- /* Flush USB FIFO */
- if (USB_FlushTxFifo(USBx, 0x10U) != HAL_OK) /* all Tx FIFOs */
- {
- ret = HAL_ERROR;
- }
- if (USB_FlushRxFifo(USBx) != HAL_OK)
- {
- ret = HAL_ERROR;
- }
- /* Flush out any leftover queued requests. */
- for (i = 0U; i <= 15U; i++)
- {
- value = USBx_HC(i)->HCCHAR;
- value |= USB_OTG_HCCHAR_CHDIS;
- value &= ~USB_OTG_HCCHAR_CHENA;
- value &= ~USB_OTG_HCCHAR_EPDIR;
- USBx_HC(i)->HCCHAR = value;
- }
- /* Halt all channels to put them into a known state. */
- for (i = 0U; i <= 15U; i++)
- {
- value = USBx_HC(i)->HCCHAR;
- value |= USB_OTG_HCCHAR_CHDIS;
- value |= USB_OTG_HCCHAR_CHENA;
- value &= ~USB_OTG_HCCHAR_EPDIR;
- USBx_HC(i)->HCCHAR = value;
- do
- {
- count++;
- if (count > 1000U)
- {
- break;
- }
- } while ((USBx_HC(i)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
- }
- /* Clear any pending Host interrupts */
- USBx_HOST->HAINT = CLEAR_INTERRUPT_MASK;
- USBx->GINTSTS = CLEAR_INTERRUPT_MASK;
- (void)USB_EnableGlobalInt(USBx);
- return ret;
- }
- /**
- * @brief USB_ActivateRemoteWakeup active remote wakeup signalling
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_ActivateRemoteWakeup(USB_OTG_GlobalTypeDef *USBx)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- if ((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
- {
- /* active Remote wakeup signalling */
- USBx_DEVICE->DCTL |= USB_OTG_DCTL_RWUSIG;
- }
- return HAL_OK;
- }
- /**
- * @brief USB_DeActivateRemoteWakeup de-active remote wakeup signalling
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_DeActivateRemoteWakeup(USB_OTG_GlobalTypeDef *USBx)
- {
- uint32_t USBx_BASE = (uint32_t)USBx;
- /* active Remote wakeup signalling */
- USBx_DEVICE->DCTL &= ~(USB_OTG_DCTL_RWUSIG);
- return HAL_OK;
- }
- #endif /* defined (USB_OTG_FS) */
- #if defined (USB)
- /**
- * @brief Initializes the USB Core
- * @param USBx USB Instance
- * @param cfg pointer to a USB_CfgTypeDef structure that contains
- * the configuration information for the specified USBx peripheral.
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_CoreInit(USB_TypeDef *USBx, USB_CfgTypeDef cfg)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(USBx);
- UNUSED(cfg);
- /* NOTE : - This function is not required by USB Device FS peripheral, it is used
- only by USB OTG FS peripheral.
- - This function is added to ensure compatibility across platforms.
- */
- return HAL_OK;
- }
- /**
- * @brief USB_EnableGlobalInt
- * Enables the controller's Global Int in the AHB Config reg
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_EnableGlobalInt(USB_TypeDef *USBx)
- {
- uint32_t winterruptmask;
- /* Clear pending interrupts */
- USBx->ISTR = 0U;
- /* Set winterruptmask variable */
- winterruptmask = USB_CNTR_CTRM | USB_CNTR_WKUPM |
- USB_CNTR_SUSPM | USB_CNTR_ERRM |
- USB_CNTR_SOFM | USB_CNTR_ESOFM |
- USB_CNTR_RESETM;
- /* Set interrupt mask */
- USBx->CNTR = (uint16_t)winterruptmask;
- return HAL_OK;
- }
- /**
- * @brief USB_DisableGlobalInt
- * Disable the controller's Global Int in the AHB Config reg
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_DisableGlobalInt(USB_TypeDef *USBx)
- {
- uint32_t winterruptmask;
- /* Set winterruptmask variable */
- winterruptmask = USB_CNTR_CTRM | USB_CNTR_WKUPM |
- USB_CNTR_SUSPM | USB_CNTR_ERRM |
- USB_CNTR_SOFM | USB_CNTR_ESOFM |
- USB_CNTR_RESETM;
- /* Clear interrupt mask */
- USBx->CNTR &= (uint16_t)(~winterruptmask);
- return HAL_OK;
- }
- /**
- * @brief USB_SetCurrentMode Set functional mode
- * @param USBx Selected device
- * @param mode current core mode
- * This parameter can be one of the these values:
- * @arg USB_DEVICE_MODE Peripheral mode
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_SetCurrentMode(USB_TypeDef *USBx, USB_ModeTypeDef mode)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(USBx);
- UNUSED(mode);
- /* NOTE : - This function is not required by USB Device FS peripheral, it is used
- only by USB OTG FS peripheral.
- - This function is added to ensure compatibility across platforms.
- */
- return HAL_OK;
- }
- /**
- * @brief USB_DevInit Initializes the USB controller registers
- * for device mode
- * @param USBx Selected device
- * @param cfg pointer to a USB_CfgTypeDef structure that contains
- * the configuration information for the specified USBx peripheral.
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_DevInit(USB_TypeDef *USBx, USB_CfgTypeDef cfg)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(cfg);
- /* Init Device */
- /* CNTR_FRES = 1 */
- USBx->CNTR = (uint16_t)USB_CNTR_FRES;
- /* CNTR_FRES = 0 */
- USBx->CNTR = 0U;
- /* Clear pending interrupts */
- USBx->ISTR = 0U;
- /*Set Btable Address*/
- USBx->BTABLE = BTABLE_ADDRESS;
- return HAL_OK;
- }
- /**
- * @brief USB_FlushTxFifo : Flush a Tx FIFO
- * @param USBx : Selected device
- * @param num : FIFO number
- * This parameter can be a value from 1 to 15
- 15 means Flush all Tx FIFOs
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_FlushTxFifo(USB_TypeDef const *USBx, uint32_t num)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(USBx);
- UNUSED(num);
- /* NOTE : - This function is not required by USB Device FS peripheral, it is used
- only by USB OTG FS peripheral.
- - This function is added to ensure compatibility across platforms.
- */
- return HAL_OK;
- }
- /**
- * @brief USB_FlushRxFifo : Flush Rx FIFO
- * @param USBx : Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_FlushRxFifo(USB_TypeDef const *USBx)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(USBx);
- /* NOTE : - This function is not required by USB Device FS peripheral, it is used
- only by USB OTG FS peripheral.
- - This function is added to ensure compatibility across platforms.
- */
- return HAL_OK;
- }
- #if defined (HAL_PCD_MODULE_ENABLED)
- /**
- * @brief Activate and configure an endpoint
- * @param USBx Selected device
- * @param ep pointer to endpoint structure
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_ActivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep)
- {
- HAL_StatusTypeDef ret = HAL_OK;
- uint16_t wEpRegVal;
- wEpRegVal = PCD_GET_ENDPOINT(USBx, ep->num) & USB_EP_T_MASK;
- /* initialize Endpoint */
- switch (ep->type)
- {
- case EP_TYPE_CTRL:
- wEpRegVal |= USB_EP_CONTROL;
- break;
- case EP_TYPE_BULK:
- wEpRegVal |= USB_EP_BULK;
- break;
- case EP_TYPE_INTR:
- wEpRegVal |= USB_EP_INTERRUPT;
- break;
- case EP_TYPE_ISOC:
- wEpRegVal |= USB_EP_ISOCHRONOUS;
- break;
- default:
- ret = HAL_ERROR;
- break;
- }
- PCD_SET_ENDPOINT(USBx, ep->num, (wEpRegVal | USB_EP_CTR_RX | USB_EP_CTR_TX));
- PCD_SET_EP_ADDRESS(USBx, ep->num, ep->num);
- if (ep->doublebuffer == 0U)
- {
- if (ep->is_in != 0U)
- {
- /*Set the endpoint Transmit buffer address */
- PCD_SET_EP_TX_ADDRESS(USBx, ep->num, ep->pmaadress);
- PCD_CLEAR_TX_DTOG(USBx, ep->num);
- if (ep->type != EP_TYPE_ISOC)
- {
- /* Configure NAK status for the Endpoint */
- PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
- }
- else
- {
- /* Configure TX Endpoint to disabled state */
- PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
- }
- }
- else
- {
- /* Set the endpoint Receive buffer address */
- PCD_SET_EP_RX_ADDRESS(USBx, ep->num, ep->pmaadress);
- /* Set the endpoint Receive buffer counter */
- PCD_SET_EP_RX_CNT(USBx, ep->num, ep->maxpacket);
- PCD_CLEAR_RX_DTOG(USBx, ep->num);
- if (ep->num == 0U)
- {
- /* Configure VALID status for EP0 */
- PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
- }
- else
- {
- /* Configure NAK status for OUT Endpoint */
- PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_NAK);
- }
- }
- }
- #if (USE_USB_DOUBLE_BUFFER == 1U)
- /* Double Buffer */
- else
- {
- if (ep->type == EP_TYPE_BULK)
- {
- /* Set bulk endpoint as double buffered */
- PCD_SET_BULK_EP_DBUF(USBx, ep->num);
- }
- else
- {
- /* Set the ISOC endpoint in double buffer mode */
- PCD_CLEAR_EP_KIND(USBx, ep->num);
- }
- /* Set buffer address for double buffered mode */
- PCD_SET_EP_DBUF_ADDR(USBx, ep->num, ep->pmaaddr0, ep->pmaaddr1);
- if (ep->is_in == 0U)
- {
- /* Clear the data toggle bits for the endpoint IN/OUT */
- PCD_CLEAR_RX_DTOG(USBx, ep->num);
- PCD_CLEAR_TX_DTOG(USBx, ep->num);
- PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
- PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
- }
- else
- {
- /* Clear the data toggle bits for the endpoint IN/OUT */
- PCD_CLEAR_RX_DTOG(USBx, ep->num);
- PCD_CLEAR_TX_DTOG(USBx, ep->num);
- if (ep->type != EP_TYPE_ISOC)
- {
- /* Configure NAK status for the Endpoint */
- PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
- }
- else
- {
- /* Configure TX Endpoint to disabled state */
- PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
- }
- PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
- }
- }
- #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
- return ret;
- }
- /**
- * @brief De-activate and de-initialize an endpoint
- * @param USBx Selected device
- * @param ep pointer to endpoint structure
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_DeactivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep)
- {
- if (ep->doublebuffer == 0U)
- {
- if (ep->is_in != 0U)
- {
- PCD_CLEAR_TX_DTOG(USBx, ep->num);
- /* Configure DISABLE status for the Endpoint */
- PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
- }
- else
- {
- PCD_CLEAR_RX_DTOG(USBx, ep->num);
- /* Configure DISABLE status for the Endpoint */
- PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
- }
- }
- #if (USE_USB_DOUBLE_BUFFER == 1U)
- /* Double Buffer */
- else
- {
- if (ep->is_in == 0U)
- {
- /* Clear the data toggle bits for the endpoint IN/OUT*/
- PCD_CLEAR_RX_DTOG(USBx, ep->num);
- PCD_CLEAR_TX_DTOG(USBx, ep->num);
- /* Reset value of the data toggle bits for the endpoint out*/
- PCD_TX_DTOG(USBx, ep->num);
- PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
- PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
- }
- else
- {
- /* Clear the data toggle bits for the endpoint IN/OUT*/
- PCD_CLEAR_RX_DTOG(USBx, ep->num);
- PCD_CLEAR_TX_DTOG(USBx, ep->num);
- PCD_RX_DTOG(USBx, ep->num);
- /* Configure DISABLE status for the Endpoint*/
- PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
- PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
- }
- }
- #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
- return HAL_OK;
- }
- /**
- * @brief USB_EPStartXfer setup and starts a transfer over an EP
- * @param USBx Selected device
- * @param ep pointer to endpoint structure
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_EPStartXfer(USB_TypeDef *USBx, USB_EPTypeDef *ep)
- {
- uint32_t len;
- #if (USE_USB_DOUBLE_BUFFER == 1U)
- uint16_t pmabuffer;
- uint16_t wEPVal;
- #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
- /* IN endpoint */
- if (ep->is_in == 1U)
- {
- /*Multi packet transfer*/
- if (ep->xfer_len > ep->maxpacket)
- {
- len = ep->maxpacket;
- }
- else
- {
- len = ep->xfer_len;
- }
- /* configure and validate Tx endpoint */
- if (ep->doublebuffer == 0U)
- {
- USB_WritePMA(USBx, ep->xfer_buff, ep->pmaadress, (uint16_t)len);
- PCD_SET_EP_TX_CNT(USBx, ep->num, len);
- }
- #if (USE_USB_DOUBLE_BUFFER == 1U)
- else
- {
- /* double buffer bulk management */
- if (ep->type == EP_TYPE_BULK)
- {
- if (ep->xfer_len_db > ep->maxpacket)
- {
- /* enable double buffer */
- PCD_SET_BULK_EP_DBUF(USBx, ep->num);
- /* each Time to write in PMA xfer_len_db will */
- ep->xfer_len_db -= len;
- /* Fill the two first buffer in the Buffer0 & Buffer1 */
- if ((PCD_GET_ENDPOINT(USBx, ep->num) & USB_EP_DTOG_TX) != 0U)
- {
- /* Set the Double buffer counter for pmabuffer1 */
- PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len);
- pmabuffer = ep->pmaaddr1;
- /* Write the user buffer to USB PMA */
- USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
- ep->xfer_buff += len;
- if (ep->xfer_len_db > ep->maxpacket)
- {
- ep->xfer_len_db -= len;
- }
- else
- {
- len = ep->xfer_len_db;
- ep->xfer_len_db = 0U;
- }
- /* Set the Double buffer counter for pmabuffer0 */
- PCD_SET_EP_DBUF0_CNT(USBx, ep->num, ep->is_in, len);
- pmabuffer = ep->pmaaddr0;
- /* Write the user buffer to USB PMA */
- USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
- }
- else
- {
- /* Set the Double buffer counter for pmabuffer0 */
- PCD_SET_EP_DBUF0_CNT(USBx, ep->num, ep->is_in, len);
- pmabuffer = ep->pmaaddr0;
- /* Write the user buffer to USB PMA */
- USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
- ep->xfer_buff += len;
- if (ep->xfer_len_db > ep->maxpacket)
- {
- ep->xfer_len_db -= len;
- }
- else
- {
- len = ep->xfer_len_db;
- ep->xfer_len_db = 0U;
- }
- /* Set the Double buffer counter for pmabuffer1 */
- PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len);
- pmabuffer = ep->pmaaddr1;
- /* Write the user buffer to USB PMA */
- USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
- }
- }
- /* auto Switch to single buffer mode when transfer <Mps no need to manage in double buffer */
- else
- {
- len = ep->xfer_len_db;
- /* disable double buffer mode for Bulk endpoint */
- PCD_CLEAR_BULK_EP_DBUF(USBx, ep->num);
- /* Set Tx count with nbre of byte to be transmitted */
- PCD_SET_EP_TX_CNT(USBx, ep->num, len);
- pmabuffer = ep->pmaaddr0;
- /* Write the user buffer to USB PMA */
- USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
- }
- }
- else /* manage isochronous double buffer IN mode */
- {
- /* each Time to write in PMA xfer_len_db will */
- ep->xfer_len_db -= len;
- /* Fill the data buffer */
- if ((PCD_GET_ENDPOINT(USBx, ep->num) & USB_EP_DTOG_TX) != 0U)
- {
- /* Set the Double buffer counter for pmabuffer1 */
- PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len);
- pmabuffer = ep->pmaaddr1;
- /* Write the user buffer to USB PMA */
- USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
- }
- else
- {
- /* Set the Double buffer counter for pmabuffer0 */
- PCD_SET_EP_DBUF0_CNT(USBx, ep->num, ep->is_in, len);
- pmabuffer = ep->pmaaddr0;
- /* Write the user buffer to USB PMA */
- USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
- }
- }
- }
- #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
- PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_VALID);
- }
- else /* OUT endpoint */
- {
- if (ep->doublebuffer == 0U)
- {
- /* Multi packet transfer */
- if (ep->xfer_len > ep->maxpacket)
- {
- len = ep->maxpacket;
- ep->xfer_len -= len;
- }
- else
- {
- len = ep->xfer_len;
- ep->xfer_len = 0U;
- }
- /* configure and validate Rx endpoint */
- PCD_SET_EP_RX_CNT(USBx, ep->num, len);
- }
- #if (USE_USB_DOUBLE_BUFFER == 1U)
- else
- {
- /* First Transfer Coming From HAL_PCD_EP_Receive & From ISR */
- /* Set the Double buffer counter */
- if (ep->type == EP_TYPE_BULK)
- {
- PCD_SET_EP_DBUF_CNT(USBx, ep->num, ep->is_in, ep->maxpacket);
- /* Coming from ISR */
- if (ep->xfer_count != 0U)
- {
- /* update last value to check if there is blocking state */
- wEPVal = PCD_GET_ENDPOINT(USBx, ep->num);
- /*Blocking State */
- if ((((wEPVal & USB_EP_DTOG_RX) != 0U) && ((wEPVal & USB_EP_DTOG_TX) != 0U)) ||
- (((wEPVal & USB_EP_DTOG_RX) == 0U) && ((wEPVal & USB_EP_DTOG_TX) == 0U)))
- {
- PCD_FREE_USER_BUFFER(USBx, ep->num, 0U);
- }
- }
- }
- /* iso out double */
- else if (ep->type == EP_TYPE_ISOC)
- {
- /* Multi packet transfer */
- if (ep->xfer_len > ep->maxpacket)
- {
- len = ep->maxpacket;
- ep->xfer_len -= len;
- }
- else
- {
- len = ep->xfer_len;
- ep->xfer_len = 0U;
- }
- PCD_SET_EP_DBUF_CNT(USBx, ep->num, ep->is_in, len);
- }
- else
- {
- return HAL_ERROR;
- }
- }
- #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
- PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
- }
- return HAL_OK;
- }
- /**
- * @brief USB_EPSetStall set a stall condition over an EP
- * @param USBx Selected device
- * @param ep pointer to endpoint structure
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_EPSetStall(USB_TypeDef *USBx, USB_EPTypeDef *ep)
- {
- if (ep->is_in != 0U)
- {
- PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_STALL);
- }
- else
- {
- PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_STALL);
- }
- return HAL_OK;
- }
- /**
- * @brief USB_EPClearStall Clear a stall condition over an EP
- * @param USBx Selected device
- * @param ep pointer to endpoint structure
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_EPClearStall(USB_TypeDef *USBx, USB_EPTypeDef *ep)
- {
- if (ep->doublebuffer == 0U)
- {
- if (ep->is_in != 0U)
- {
- PCD_CLEAR_TX_DTOG(USBx, ep->num);
- if (ep->type != EP_TYPE_ISOC)
- {
- /* Configure NAK status for the Endpoint */
- PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
- }
- }
- else
- {
- PCD_CLEAR_RX_DTOG(USBx, ep->num);
- /* Configure VALID status for the Endpoint */
- PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
- }
- }
- return HAL_OK;
- }
- /**
- * @brief USB_EPStoptXfer Stop transfer on an EP
- * @param USBx usb device instance
- * @param ep pointer to endpoint structure
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_EPStopXfer(USB_TypeDef *USBx, USB_EPTypeDef *ep)
- {
- /* IN endpoint */
- if (ep->is_in == 1U)
- {
- if (ep->doublebuffer == 0U)
- {
- if (ep->type != EP_TYPE_ISOC)
- {
- /* Configure NAK status for the Endpoint */
- PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
- }
- else
- {
- /* Configure TX Endpoint to disabled state */
- PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
- }
- }
- }
- else /* OUT endpoint */
- {
- if (ep->doublebuffer == 0U)
- {
- if (ep->type != EP_TYPE_ISOC)
- {
- /* Configure NAK status for the Endpoint */
- PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_NAK);
- }
- else
- {
- /* Configure RX Endpoint to disabled state */
- PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
- }
- }
- }
- return HAL_OK;
- }
- #endif /* defined (HAL_PCD_MODULE_ENABLED) */
- /**
- * @brief USB_StopDevice Stop the usb device mode
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_StopDevice(USB_TypeDef *USBx)
- {
- /* disable all interrupts and force USB reset */
- USBx->CNTR = (uint16_t)USB_CNTR_FRES;
- /* clear interrupt status register */
- USBx->ISTR = 0U;
- /* switch-off device */
- USBx->CNTR = (uint16_t)(USB_CNTR_FRES | USB_CNTR_PDWN);
- return HAL_OK;
- }
- /**
- * @brief USB_SetDevAddress Stop the usb device mode
- * @param USBx Selected device
- * @param address new device address to be assigned
- * This parameter can be a value from 0 to 255
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_SetDevAddress(USB_TypeDef *USBx, uint8_t address)
- {
- if (address == 0U)
- {
- /* set device address and enable function */
- USBx->DADDR = (uint16_t)USB_DADDR_EF;
- }
- return HAL_OK;
- }
- /**
- * @brief USB_DevConnect Connect the USB device by enabling the pull-up/pull-down
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_DevConnect(USB_TypeDef *USBx)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(USBx);
- /* NOTE : - This function is not required by USB Device FS peripheral, it is used
- only by USB OTG FS peripheral.
- - This function is added to ensure compatibility across platforms.
- */
- return HAL_OK;
- }
- /**
- * @brief USB_DevDisconnect Disconnect the USB device by disabling the pull-up/pull-down
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_DevDisconnect(USB_TypeDef *USBx)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(USBx);
- /* NOTE : - This function is not required by USB Device FS peripheral, it is used
- only by USB OTG FS peripheral.
- - This function is added to ensure compatibility across platforms.
- */
- return HAL_OK;
- }
- /**
- * @brief USB_ReadInterrupts return the global USB interrupt status
- * @param USBx Selected device
- * @retval USB Global Interrupt status
- */
- uint32_t USB_ReadInterrupts(USB_TypeDef const *USBx)
- {
- uint32_t tmpreg;
- tmpreg = USBx->ISTR;
- return tmpreg;
- }
- /**
- * @brief USB_ReadDevAllOutEpInterrupt return the USB device OUT endpoints interrupt status
- * @param USBx Selected device
- * @retval HAL status
- */
- uint32_t USB_ReadDevAllOutEpInterrupt(USB_TypeDef *USBx)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(USBx);
- /* NOTE : - This function is not required by USB Device FS peripheral, it is used
- only by USB OTG FS peripheral.
- - This function is added to ensure compatibility across platforms.
- */
- return (0);
- }
- /**
- * @brief USB_ReadDevAllInEpInterrupt return the USB device IN endpoints interrupt status
- * @param USBx Selected device
- * @retval HAL status
- */
- uint32_t USB_ReadDevAllInEpInterrupt(USB_TypeDef *USBx)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(USBx);
- /* NOTE : - This function is not required by USB Device FS peripheral, it is used
- only by USB OTG FS peripheral.
- - This function is added to ensure compatibility across platforms.
- */
- return (0);
- }
- /**
- * @brief Returns Device OUT EP Interrupt register
- * @param USBx Selected device
- * @param epnum endpoint number
- * This parameter can be a value from 0 to 15
- * @retval Device OUT EP Interrupt register
- */
- uint32_t USB_ReadDevOutEPInterrupt(USB_TypeDef *USBx, uint8_t epnum)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(USBx);
- UNUSED(epnum);
- /* NOTE : - This function is not required by USB Device FS peripheral, it is used
- only by USB OTG FS peripheral.
- - This function is added to ensure compatibility across platforms.
- */
- return (0);
- }
- /**
- * @brief Returns Device IN EP Interrupt register
- * @param USBx Selected device
- * @param epnum endpoint number
- * This parameter can be a value from 0 to 15
- * @retval Device IN EP Interrupt register
- */
- uint32_t USB_ReadDevInEPInterrupt(USB_TypeDef *USBx, uint8_t epnum)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(USBx);
- UNUSED(epnum);
- /* NOTE : - This function is not required by USB Device FS peripheral, it is used
- only by USB OTG FS peripheral.
- - This function is added to ensure compatibility across platforms.
- */
- return (0);
- }
- /**
- * @brief USB_ClearInterrupts: clear a USB interrupt
- * @param USBx Selected device
- * @param interrupt flag
- * @retval None
- */
- void USB_ClearInterrupts(USB_TypeDef *USBx, uint32_t interrupt)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(USBx);
- UNUSED(interrupt);
- /* NOTE : - This function is not required by USB Device FS peripheral, it is used
- only by USB OTG FS peripheral.
- - This function is added to ensure compatibility across platforms.
- */
- }
- /**
- * @brief Prepare the EP0 to start the first control setup
- * @param USBx Selected device
- * @param psetup pointer to setup packet
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_EP0_OutStart(USB_TypeDef *USBx, uint8_t *psetup)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(USBx);
- UNUSED(psetup);
- /* NOTE : - This function is not required by USB Device FS peripheral, it is used
- only by USB OTG FS peripheral.
- - This function is added to ensure compatibility across platforms.
- */
- return HAL_OK;
- }
- /**
- * @brief USB_ActivateRemoteWakeup : active remote wakeup signalling
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_ActivateRemoteWakeup(USB_TypeDef *USBx)
- {
- USBx->CNTR |= (uint16_t)USB_CNTR_RESUME;
- return HAL_OK;
- }
- /**
- * @brief USB_DeActivateRemoteWakeup de-active remote wakeup signalling
- * @param USBx Selected device
- * @retval HAL status
- */
- HAL_StatusTypeDef USB_DeActivateRemoteWakeup(USB_TypeDef *USBx)
- {
- USBx->CNTR &= (uint16_t)(~USB_CNTR_RESUME);
- return HAL_OK;
- }
- /**
- * @brief Copy a buffer from user memory area to packet memory area (PMA)
- * @param USBx USB peripheral instance register address.
- * @param pbUsrBuf pointer to user memory area.
- * @param wPMABufAddr address into PMA.
- * @param wNBytes no. of bytes to be copied.
- * @retval None
- */
- void USB_WritePMA(USB_TypeDef const *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
- {
- uint32_t n = ((uint32_t)wNBytes + 1U) >> 1;
- uint32_t BaseAddr = (uint32_t)USBx;
- uint32_t count;
- uint16_t WrVal;
- __IO uint16_t *pdwVal;
- uint8_t *pBuf = pbUsrBuf;
- pdwVal = (__IO uint16_t *)(BaseAddr + 0x400U + ((uint32_t)wPMABufAddr * PMA_ACCESS));
- for (count = n; count != 0U; count--)
- {
- WrVal = pBuf[0];
- WrVal |= (uint16_t)pBuf[1] << 8;
- *pdwVal = (WrVal & 0xFFFFU);
- pdwVal++;
- #if PMA_ACCESS > 1U
- pdwVal++;
- #endif /* PMA_ACCESS */
- pBuf++;
- pBuf++;
- }
- }
- /**
- * @brief Copy data from packet memory area (PMA) to user memory buffer
- * @param USBx USB peripheral instance register address.
- * @param pbUsrBuf pointer to user memory area.
- * @param wPMABufAddr address into PMA.
- * @param wNBytes no. of bytes to be copied.
- * @retval None
- */
- void USB_ReadPMA(USB_TypeDef const *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
- {
- uint32_t n = (uint32_t)wNBytes >> 1;
- uint32_t BaseAddr = (uint32_t)USBx;
- uint32_t count;
- uint32_t RdVal;
- __IO uint16_t *pdwVal;
- uint8_t *pBuf = pbUsrBuf;
- pdwVal = (__IO uint16_t *)(BaseAddr + 0x400U + ((uint32_t)wPMABufAddr * PMA_ACCESS));
- for (count = n; count != 0U; count--)
- {
- RdVal = *(__IO uint16_t *)pdwVal;
- pdwVal++;
- *pBuf = (uint8_t)((RdVal >> 0) & 0xFFU);
- pBuf++;
- *pBuf = (uint8_t)((RdVal >> 8) & 0xFFU);
- pBuf++;
- #if PMA_ACCESS > 1U
- pdwVal++;
- #endif /* PMA_ACCESS */
- }
- if ((wNBytes % 2U) != 0U)
- {
- RdVal = *pdwVal;
- *pBuf = (uint8_t)((RdVal >> 0) & 0xFFU);
- }
- }
- #endif /* defined (USB) */
- /**
- * @}
- */
- /**
- * @}
- */
- #endif /* defined (USB) || defined (USB_OTG_FS) */
- #endif /* defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED) */
- /**
- * @}
- */
|