stm32g0xx_hal_hcd.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_hal_hcd.c
  4. * @author MCD Application Team
  5. * @brief HCD HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the USB Peripheral Controller:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2018 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. (#)Declare a HCD_HandleTypeDef handle structure, for example:
  30. HCD_HandleTypeDef hhcd;
  31. (#)Fill parameters of Init structure in HCD handle
  32. (#)Call HAL_HCD_Init() API to initialize the HCD peripheral (Core, Host core, ...)
  33. (#)Initialize the HCD low level resources through the HAL_HCD_MspInit() API:
  34. (##) Enable the HCD/USB Low Level interface clock using the following macros
  35. (+++) __HAL_RCC_USB_CLK_ENABLE();
  36. (##) Initialize the related GPIO clocks
  37. (##) Configure HCD pin-out
  38. (##) Configure HCD NVIC interrupt
  39. (#)Associate the Upper USB Host stack to the HAL HCD Driver:
  40. (##) hhcd.pData = phost;
  41. (#)Enable HCD transmission and reception:
  42. (##) HAL_HCD_Start();
  43. @endverbatim
  44. ******************************************************************************
  45. */
  46. /* Includes ------------------------------------------------------------------*/
  47. #include "stm32g0xx_hal.h"
  48. /** @addtogroup STM32G0xx_HAL_Driver
  49. * @{
  50. */
  51. #ifdef HAL_HCD_MODULE_ENABLED
  52. #if defined (USB_DRD_FS)
  53. /** @defgroup HCD HCD
  54. * @brief HCD HAL module driver
  55. * @{
  56. */
  57. /* Private typedef -----------------------------------------------------------*/
  58. /* Private define ------------------------------------------------------------*/
  59. /* Private macro -------------------------------------------------------------*/
  60. /* Private variables ---------------------------------------------------------*/
  61. /* Private function ----------------------------------------------------------*/
  62. /** @defgroup HCD_Private_Functions HCD Private Functions
  63. * @{
  64. */
  65. static void HCD_HC_IN_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum);
  66. static void HCD_HC_OUT_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum);
  67. static void HCD_Port_IRQHandler(HCD_HandleTypeDef *hhcd);
  68. static void HAL_HCD_ClearPhyChannel(HCD_HandleTypeDef *hhcd);
  69. static uint8_t HAL_HCD_GetLogical_Channel(HCD_HandleTypeDef const *hhcd, uint8_t phy_chnum, uint8_t dir);
  70. static uint8_t HAL_HCD_Check_usedChannel(HCD_HandleTypeDef const *hhcd, uint8_t ch_num);
  71. static uint8_t HAL_HCD_Get_FreePhyChannel(HCD_HandleTypeDef *hhcd, uint8_t ch_num, uint8_t epnum, uint8_t ep_type);
  72. #if (USE_USB_DOUBLE_BUFFER == 1U)
  73. static void HCD_HC_IN_BulkDb(HCD_HandleTypeDef *hhcd, uint8_t ch_num, uint8_t phy_chnum, uint32_t regvalue);
  74. static void HCD_HC_OUT_BulkDb(HCD_HandleTypeDef *hhcd, uint8_t ch_num, uint8_t phy_chnum, uint32_t regvalue);
  75. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  76. static uint16_t HAL_HCD_GetFreePMA(HCD_HandleTypeDef *hhcd, uint16_t mps);
  77. static HAL_StatusTypeDef HAL_HCD_PMAFree(HCD_HandleTypeDef *hhcd, uint32_t pma_base, uint16_t mps);
  78. static void inline HCD_HC_IN_ISO(HCD_HandleTypeDef *hhcd, uint8_t ch_num, uint8_t phy_chnum, uint32_t regvalue);
  79. /**
  80. * @}
  81. */
  82. /* Exported functions --------------------------------------------------------*/
  83. /** @defgroup HCD_Exported_Functions HCD Exported Functions
  84. * @{
  85. */
  86. /** @defgroup HCD_Exported_Functions_Group1 Initialization and de-initialization functions
  87. * @brief Initialization and Configuration functions
  88. *
  89. @verbatim
  90. ===============================================================================
  91. ##### Initialization and de-initialization functions #####
  92. ===============================================================================
  93. [..] This section provides functions allowing to:
  94. @endverbatim
  95. * @{
  96. */
  97. /**
  98. * @brief Initialize the host driver.
  99. * @param hhcd HCD handle
  100. * @retval HAL status
  101. */
  102. HAL_StatusTypeDef HAL_HCD_Init(HCD_HandleTypeDef *hhcd)
  103. {
  104. /* Check the HCD handle allocation */
  105. if (hhcd == NULL)
  106. {
  107. return HAL_ERROR;
  108. }
  109. /* Check the parameters */
  110. assert_param(IS_HCD_ALL_INSTANCE(hhcd->Instance));
  111. if (hhcd->State == HAL_HCD_STATE_RESET)
  112. {
  113. /* Allocate lock resource and initialize it */
  114. hhcd->Lock = HAL_UNLOCKED;
  115. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  116. hhcd->SOFCallback = HAL_HCD_SOF_Callback;
  117. hhcd->ConnectCallback = HAL_HCD_Connect_Callback;
  118. hhcd->DisconnectCallback = HAL_HCD_Disconnect_Callback;
  119. hhcd->PortEnabledCallback = HAL_HCD_PortEnabled_Callback;
  120. hhcd->PortDisabledCallback = HAL_HCD_PortDisabled_Callback;
  121. hhcd->HC_NotifyURBChangeCallback = HAL_HCD_HC_NotifyURBChange_Callback;
  122. if (hhcd->MspInitCallback == NULL)
  123. {
  124. hhcd->MspInitCallback = HAL_HCD_MspInit;
  125. }
  126. /* Init the low level hardware */
  127. hhcd->MspInitCallback(hhcd);
  128. #else
  129. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  130. HAL_HCD_MspInit(hhcd);
  131. #endif /* (USE_HAL_HCD_REGISTER_CALLBACKS) */
  132. }
  133. hhcd->State = HAL_HCD_STATE_BUSY;
  134. /* Disable the Interrupts */
  135. (void)__HAL_HCD_DISABLE(hhcd);
  136. /* Dma not supported, force to zero */
  137. hhcd->Init.dma_enable = 0U;
  138. /* Init the Core (common init.) */
  139. (void)USB_CoreInit(hhcd->Instance, hhcd->Init);
  140. /* Force Host Mode */
  141. (void)USB_SetCurrentMode(hhcd->Instance, USB_HOST_MODE);
  142. /* Init Host */
  143. (void)USB_HostInit(hhcd->Instance, hhcd->Init);
  144. /* Deactivate the power down */
  145. hhcd->Instance->CNTR &= ~USB_CNTR_PDWN;
  146. hhcd->State = HAL_HCD_STATE_READY;
  147. /* Host Port State */
  148. hhcd->HostState = HCD_HCD_STATE_DISCONNECTED;
  149. /* Init PMA Address */
  150. (void)HAL_HCD_PMAReset(hhcd);
  151. hhcd->State = HAL_HCD_STATE_READY;
  152. return HAL_OK;
  153. }
  154. /**
  155. * @brief Initialize a host channel.
  156. * @param hhcd HCD handle
  157. * @param ch_num Channel number.
  158. * This parameter can be a value from 1 to 15
  159. * @param epnum Endpoint number.
  160. * This parameter can be a value from 1 to 15
  161. * @param dev_address Current device address
  162. * This parameter can be a value from 0 to 255
  163. * @param speed Current device speed.
  164. * This parameter can be one of these values:
  165. * HCD_DEVICE_SPEED_HIGH High speed mode,
  166. * HCD_DEVICE_SPEED_FULL Full speed mode,
  167. * HCD_DEVICE_SPEED_LOW Low speed mode
  168. * @param ep_type Endpoint Type.
  169. * This parameter can be one of these values:
  170. * USBH_EP_CONTROL Control type,
  171. * USBH_EP_ISO Isochronous type,
  172. * USBH_EP_BULK Bulk type,
  173. * USBH_EP_INTERRUPT Interrupt type
  174. * @param mps Max Packet Size.
  175. * This parameter can be a value from 0 to32K
  176. * @retval HAL status
  177. */
  178. HAL_StatusTypeDef HAL_HCD_HC_Init(HCD_HandleTypeDef *hhcd, uint8_t ch_num,
  179. uint8_t epnum, uint8_t dev_address,
  180. uint8_t speed, uint8_t ep_type, uint16_t mps)
  181. {
  182. HAL_StatusTypeDef status;
  183. uint8_t used_channel;
  184. uint8_t ep0_virtual_channel;
  185. __HAL_LOCK(hhcd);
  186. /* Check if the logical channel are already allocated */
  187. used_channel = HAL_HCD_Check_usedChannel(hhcd, ch_num);
  188. /* Check if the channel is not already opened */
  189. if (used_channel == 0U)
  190. {
  191. /* Allocate New Physical channel */
  192. hhcd->hc[ch_num & 0xFU].phy_ch_num = HAL_HCD_Get_FreePhyChannel(hhcd, ch_num, epnum, ep_type);
  193. /* No free Channel available, return error */
  194. if (hhcd->hc[ch_num & 0xFU].phy_ch_num == HCD_FREE_CH_NOT_FOUND)
  195. {
  196. return HAL_ERROR;
  197. }
  198. }
  199. /* Channel already opened */
  200. else
  201. {
  202. /* Get Physical Channel number */
  203. hhcd->hc[ch_num & 0xFU].phy_ch_num = (used_channel & 0xF0U) >> 4U;
  204. }
  205. if ((epnum & 0x80U) != 0U)
  206. {
  207. hhcd->hc[ch_num & 0xFU].ch_dir = CH_IN_DIR;
  208. }
  209. else
  210. {
  211. hhcd->hc[ch_num & 0xFU].ch_dir = CH_OUT_DIR;
  212. }
  213. hhcd->hc[ch_num & 0xFU].dev_addr = dev_address;
  214. hhcd->hc[ch_num & 0xFU].max_packet = mps;
  215. hhcd->hc[ch_num & 0xFU].ep_type = ep_type;
  216. hhcd->hc[ch_num & 0xFU].ep_num = epnum & 0x7FU;
  217. hhcd->hc[ch_num & 0xFU].speed = speed;
  218. /* Check if the channel is not already opened */
  219. if (used_channel == 0U)
  220. {
  221. if (((ep_type == EP_TYPE_ISOC) && (hhcd->Init.iso_singlebuffer_enable == 0U)) ||
  222. ((ep_type == EP_TYPE_BULK) && (hhcd->Init.bulk_doublebuffer_enable == 1U)))
  223. {
  224. /* PMA Dynamic Allocation */
  225. status = HAL_HCD_PMAlloc(hhcd, ch_num, HCD_DBL_BUF, mps);
  226. if (status == HAL_ERROR)
  227. {
  228. return HAL_ERROR;
  229. }
  230. /* Clear Channel DTOG_TX */
  231. HCD_CLEAR_TX_DTOG(hhcd->Instance, hhcd->hc[ch_num & 0xFU].phy_ch_num);
  232. /* Clear Channel DTOG RX */
  233. HCD_CLEAR_RX_DTOG(hhcd->Instance, hhcd->hc[ch_num & 0xFU].phy_ch_num);
  234. }
  235. else
  236. {
  237. if (hhcd->hc[ch_num & 0xFU].ep_num != 0U)
  238. {
  239. status = HAL_HCD_PMAlloc(hhcd, ch_num, HCD_SNG_BUF, mps);
  240. if (status == HAL_ERROR)
  241. {
  242. return HAL_ERROR;
  243. }
  244. }
  245. else
  246. {
  247. if (ch_num == 0U)
  248. {
  249. ep0_virtual_channel = (uint8_t)(hhcd->ep0_PmaAllocState & 0xFU);
  250. if ((ep0_virtual_channel != 0U) && (((hhcd->ep0_PmaAllocState & 0xF0U) >> 4) == CH_IN_DIR))
  251. {
  252. if (hhcd->hc[ch_num & 0xFU].ch_dir == CH_OUT_DIR)
  253. {
  254. status = HAL_HCD_PMAlloc(hhcd, ch_num, HCD_SNG_BUF, 64U);
  255. if (status == HAL_ERROR)
  256. {
  257. return HAL_ERROR;
  258. }
  259. }
  260. else
  261. {
  262. return HAL_ERROR;
  263. }
  264. }
  265. else
  266. {
  267. /* PMA Dynamic Allocation for EP0 OUT direction */
  268. hhcd->hc[ch_num & 0xFU].ch_dir = CH_OUT_DIR;
  269. status = HAL_HCD_PMAlloc(hhcd, ch_num, HCD_SNG_BUF, 64U);
  270. if (status == HAL_ERROR)
  271. {
  272. return HAL_ERROR;
  273. }
  274. /* PMA Dynamic Allocation for EP0 IN direction */
  275. hhcd->hc[ch_num & 0xFU].ch_dir = CH_IN_DIR;
  276. status = HAL_HCD_PMAlloc(hhcd, ch_num, HCD_SNG_BUF, 64U);
  277. if (status == HAL_ERROR)
  278. {
  279. return HAL_ERROR;
  280. }
  281. }
  282. }
  283. else
  284. {
  285. if (((hhcd->ep0_PmaAllocState & 0xF00U) >> 8) == 1U)
  286. {
  287. ep0_virtual_channel = (uint8_t)(hhcd->ep0_PmaAllocState & 0xFU);
  288. if (((hhcd->ep0_PmaAllocState & 0xF0U) >> 4) == CH_IN_DIR)
  289. {
  290. hhcd->hc[ch_num & 0xFU].pmaaddr1 = hhcd->hc[ep0_virtual_channel & 0xFU].pmaaddr1;
  291. }
  292. else
  293. {
  294. hhcd->hc[ch_num & 0xFU].pmaaddr0 = hhcd->hc[ep0_virtual_channel & 0xFU].pmaaddr0;
  295. }
  296. }
  297. else
  298. {
  299. status = HAL_HCD_PMAlloc(hhcd, ch_num, HCD_SNG_BUF, 64U);
  300. if (status == HAL_ERROR)
  301. {
  302. return HAL_ERROR;
  303. }
  304. }
  305. }
  306. }
  307. }
  308. }
  309. if ((epnum & 0x80U) != 0U)
  310. {
  311. hhcd->hc[ch_num & 0xFU].ch_dir = CH_IN_DIR;
  312. if (hhcd->hc[ch_num & 0xFU].ep_num == 0U)
  313. {
  314. hhcd->hc[ch_num & 0xFU].pmaadress = hhcd->hc[ch_num & 0xFU].pmaaddr1;
  315. }
  316. }
  317. else
  318. {
  319. hhcd->hc[ch_num & 0xFU].ch_dir = CH_OUT_DIR;
  320. if (hhcd->hc[ch_num & 0xFU].ep_num == 0U)
  321. {
  322. hhcd->hc[ch_num & 0xFU].pmaadress = hhcd->hc[ch_num & 0xFU].pmaaddr0;
  323. }
  324. }
  325. /* Init the USB Channel CHEPRx */
  326. status = USB_HC_Init(hhcd->Instance, hhcd->hc[ch_num & 0xFU].phy_ch_num,
  327. epnum, dev_address, speed, ep_type, mps);
  328. /* check single buffer for isochronous channel */
  329. if (ep_type == EP_TYPE_ISOC)
  330. {
  331. if (hhcd->Init.iso_singlebuffer_enable == 1U)
  332. {
  333. (void)USB_HC_DoubleBuffer(hhcd->Instance, hhcd->hc[ch_num & 0xFU].phy_ch_num,
  334. USB_DRD_ISOC_DBUFF_DISABLE);
  335. }
  336. }
  337. /* Bulk double buffer check */
  338. if (ep_type == EP_TYPE_BULK)
  339. {
  340. if (hhcd->Init.bulk_doublebuffer_enable == 1U)
  341. {
  342. (void)USB_HC_DoubleBuffer(hhcd->Instance, hhcd->hc[ch_num & 0xFU].phy_ch_num,
  343. USB_DRD_BULK_DBUFF_ENBALE);
  344. }
  345. }
  346. __HAL_UNLOCK(hhcd);
  347. return status;
  348. }
  349. /**
  350. * @brief HAL_HCD_HC_Close Pipe
  351. * @param hhcd HCD handle
  352. * @param ch_num Channel number.
  353. * This parameter can be a value from 1 to 15
  354. * @retval HAL status
  355. */
  356. HAL_StatusTypeDef HAL_HCD_HC_Close(HCD_HandleTypeDef *hhcd, uint8_t ch_num)
  357. {
  358. /* Stop the channel */
  359. (void) HAL_HCD_HC_Halt(hhcd, ch_num);
  360. HAL_Delay(3U);
  361. if (hhcd->hc[ch_num & 0xFU].ch_dir == CH_IN_DIR)
  362. {
  363. /* Free Allocated Channel */
  364. hhcd->phy_chin_state[hhcd->hc[ch_num & 0xFU].phy_ch_num] = 0U;
  365. }
  366. else
  367. {
  368. /* Free Allocated Channel */
  369. hhcd->phy_chout_state[hhcd->hc[ch_num & 0xFU].phy_ch_num] = 0U;
  370. }
  371. /* Reset PMA Channel_Allocation */
  372. (void)HAL_HCD_PMADeAlloc(hhcd, ch_num);
  373. return HAL_OK;
  374. }
  375. /**
  376. * @brief Halt a host channel.
  377. * @param hhcd HCD handle
  378. * @param ch_num Channel number.
  379. * This parameter can be a value from 1 to 15
  380. * @retval HAL status
  381. */
  382. HAL_StatusTypeDef HAL_HCD_HC_Halt(HCD_HandleTypeDef *hhcd, uint8_t ch_num)
  383. {
  384. HAL_StatusTypeDef status = HAL_OK;
  385. __HAL_LOCK(hhcd);
  386. if (hhcd->hc[ch_num & 0xFU].ch_dir == CH_IN_DIR)
  387. {
  388. (void)USB_HC_IN_Halt(hhcd->Instance, (uint8_t) hhcd->hc[ch_num & 0xFU].phy_ch_num);
  389. }
  390. else
  391. {
  392. (void)USB_HC_OUT_Halt(hhcd->Instance, (uint8_t) hhcd->hc[ch_num & 0xFU].phy_ch_num);
  393. }
  394. __HAL_UNLOCK(hhcd);
  395. return status;
  396. }
  397. /**
  398. * @brief DeInitialize the host driver.
  399. * @param hhcd HCD handle
  400. * @retval HAL status
  401. */
  402. HAL_StatusTypeDef HAL_HCD_DeInit(HCD_HandleTypeDef *hhcd)
  403. {
  404. uint8_t idx;
  405. /* Check the HCD handle allocation */
  406. if (hhcd == NULL)
  407. {
  408. return HAL_ERROR;
  409. }
  410. /* Host Port State */
  411. hhcd->HostState = HCD_HCD_STATE_DISCONNECTED;
  412. /* Reset PMA Address */
  413. (void)HAL_HCD_PMAReset(hhcd);
  414. for (idx = 0U; idx < hhcd->Init.Host_channels; idx++)
  415. {
  416. hhcd->phy_chin_state[idx] = 0U;
  417. hhcd->phy_chout_state[idx] = 0U;
  418. }
  419. /* reset Ep0 Pma allocation state */
  420. hhcd->ep0_PmaAllocState = 0U;
  421. hhcd->State = HAL_HCD_STATE_BUSY;
  422. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  423. if (hhcd->MspDeInitCallback == NULL)
  424. {
  425. hhcd->MspDeInitCallback = HAL_HCD_MspDeInit; /* Legacy weak MspDeInit */
  426. }
  427. /* DeInit the low level hardware */
  428. hhcd->MspDeInitCallback(hhcd);
  429. #else
  430. /* DeInit the low level hardware: CLOCK, NVIC. */
  431. HAL_HCD_MspDeInit(hhcd);
  432. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  433. hhcd->State = HAL_HCD_STATE_RESET;
  434. return HAL_OK;
  435. }
  436. /**
  437. * @brief Initialize the HCD MSP.
  438. * @param hhcd HCD handle
  439. * @retval None
  440. */
  441. __weak void HAL_HCD_MspInit(HCD_HandleTypeDef *hhcd)
  442. {
  443. /* Prevent unused argument(s) compilation warning */
  444. UNUSED(hhcd);
  445. /* NOTE : This function Should not be modified, when the callback is needed,
  446. the HAL_HCD_MspInit could be implemented in the user file
  447. */
  448. }
  449. /**
  450. * @brief DeInitialize the HCD MSP.
  451. * @param hhcd HCD handle
  452. * @retval None
  453. */
  454. __weak void HAL_HCD_MspDeInit(HCD_HandleTypeDef *hhcd)
  455. {
  456. /* Prevent unused argument(s) compilation warning */
  457. UNUSED(hhcd);
  458. /* NOTE : This function Should not be modified, when the callback is needed,
  459. the HAL_HCD_MspDeInit could be implemented in the user file
  460. */
  461. }
  462. __weak void HAL_HCD_SuspendCallback(HCD_HandleTypeDef *hhcd)
  463. {
  464. /* Prevent unused argument(s) compilation warning */
  465. UNUSED(hhcd);
  466. /* NOTE : This function Should not be modified, when the callback is needed,
  467. the HAL_HCD_SuspendCallback could be implemented in the user file
  468. */
  469. }
  470. __weak void HAL_HCD_ResumeCallback(HCD_HandleTypeDef *hhcd)
  471. {
  472. /* Prevent unused argument(s) compilation warning */
  473. UNUSED(hhcd);
  474. /* NOTE : This function Should not be modified, when the callback is needed,
  475. the HAL_HCD_ResumeCallback could be implemented in the user file
  476. */
  477. }
  478. /**
  479. * @}
  480. */
  481. /** @defgroup HCD_Exported_Functions_Group2 Input and Output operation functions
  482. * @brief HCD IO operation functions
  483. *
  484. @verbatim
  485. ===============================================================================
  486. ##### IO operation functions #####
  487. ===============================================================================
  488. [..] This subsection provides a set of functions allowing to manage the USB Host Data
  489. Transfer
  490. @endverbatim
  491. * @{
  492. */
  493. /**
  494. * @brief Submit a new URB for processing.
  495. * @param hhcd HCD handle
  496. * @param ch_num Channel number.
  497. * This parameter can be a value from 1 to 15
  498. * @param direction Channel number.
  499. * This parameter can be one of these values:
  500. * 0 : Output / 1 : Input
  501. * @param ep_type Endpoint Type.
  502. * This parameter can be one of these values:
  503. * USBH_EP_CONTROL : Control type/
  504. * USBH_EP_ISO : Isochronous type/
  505. * USBH_EP_BULK : Bulk type/
  506. * USBH_EP_INTERRUPT : Interrupt type/
  507. * @param token Endpoint Type.
  508. * This parameter can be one of these values:
  509. * 0: HC_PID_SETUP / 1: HC_PID_DATA1
  510. * @param pbuff pointer to URB data
  511. * @param length Length of URB data
  512. * @param do_ping activate do ping protocol (for high speed only).
  513. * This parameter can be one of these values:
  514. * 0 : do ping inactive / 1 : do ping active
  515. * @retval HAL status
  516. */
  517. HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd, uint8_t ch_num,
  518. uint8_t direction, uint8_t ep_type,
  519. uint8_t token, uint8_t *pbuff,
  520. uint16_t length, uint8_t do_ping)
  521. {
  522. UNUSED(do_ping);
  523. if (token == 0U)
  524. {
  525. hhcd->hc[ch_num & 0xFU].data_pid = HC_PID_SETUP;
  526. }
  527. else
  528. {
  529. hhcd->hc[ch_num & 0xFU].data_pid = HC_PID_DATA1;
  530. }
  531. /* Manage Data Toggle */
  532. switch (ep_type)
  533. {
  534. case EP_TYPE_CTRL:
  535. if ((token == 1U) && (direction == 0U)) /* send data */
  536. {
  537. if (length == 0U)
  538. {
  539. /* For Status OUT stage, Length==0, Status Out PID = 1 */
  540. hhcd->hc[ch_num & 0xFU].toggle_out = 1U;
  541. }
  542. /* Set the Data Toggle bit as per the Flag */
  543. if (hhcd->hc[ch_num & 0xFU].toggle_out == 0U)
  544. {
  545. /* Put the PID 0 */
  546. hhcd->hc[ch_num & 0xFU].data_pid = HC_PID_DATA0;
  547. }
  548. else
  549. {
  550. /* Put the PID 1 */
  551. hhcd->hc[ch_num & 0xFU].data_pid = HC_PID_DATA1;
  552. }
  553. }
  554. break;
  555. case EP_TYPE_BULK:
  556. if (direction == 0U)
  557. {
  558. /* Set the Data Toggle bit as per the Flag */
  559. if (hhcd->hc[ch_num & 0xFU].toggle_out == 0U)
  560. {
  561. /* Put the PID 0 */
  562. hhcd->hc[ch_num & 0xFU].data_pid = HC_PID_DATA0;
  563. }
  564. else
  565. {
  566. /* Put the PID 1 */
  567. hhcd->hc[ch_num & 0xFU].data_pid = HC_PID_DATA1;
  568. }
  569. }
  570. else
  571. {
  572. if (hhcd->hc[ch_num & 0xFU].toggle_in == 0U)
  573. {
  574. hhcd->hc[ch_num & 0xFU].data_pid = HC_PID_DATA0;
  575. }
  576. else
  577. {
  578. hhcd->hc[ch_num & 0xFU].data_pid = HC_PID_DATA1;
  579. }
  580. }
  581. break;
  582. case EP_TYPE_INTR:
  583. if (direction == 0U)
  584. {
  585. /* Set the Data Toggle bit as per the Flag */
  586. if (hhcd->hc[ch_num & 0xFU].toggle_out == 0U)
  587. {
  588. /* Put the PID 0 */
  589. hhcd->hc[ch_num & 0xFU].data_pid = HC_PID_DATA0;
  590. }
  591. else
  592. {
  593. /* Put the PID 1 */
  594. hhcd->hc[ch_num & 0xFU].data_pid = HC_PID_DATA1;
  595. }
  596. }
  597. else
  598. {
  599. if (hhcd->hc[ch_num & 0xFU].toggle_in == 0U)
  600. {
  601. hhcd->hc[ch_num & 0xFU].data_pid = HC_PID_DATA0;
  602. }
  603. else
  604. {
  605. hhcd->hc[ch_num & 0xFU].data_pid = HC_PID_DATA1;
  606. }
  607. }
  608. break;
  609. case EP_TYPE_ISOC:
  610. hhcd->hc[ch_num & 0xFU].data_pid = HC_PID_DATA0;
  611. break;
  612. default:
  613. break;
  614. }
  615. hhcd->hc[ch_num & 0xFU].xfer_buff = pbuff;
  616. hhcd->hc[ch_num & 0xFU].xfer_len = length;
  617. hhcd->hc[ch_num & 0xFU].xfer_len_db = length;
  618. hhcd->hc[ch_num & 0xFU].urb_state = URB_IDLE;
  619. hhcd->hc[ch_num & 0xFU].xfer_count = 0U;
  620. hhcd->hc[ch_num & 0xFU].state = HC_IDLE;
  621. return USB_HC_StartXfer(hhcd->Instance, &hhcd->hc[ch_num & 0xFU]);
  622. }
  623. /**
  624. * @brief Handle HCD interrupt request.
  625. * @param hhcd HCD handle
  626. * @retval None
  627. */
  628. void HAL_HCD_IRQHandler(HCD_HandleTypeDef *hhcd)
  629. {
  630. uint8_t phy_chnum;
  631. uint8_t chnum;
  632. uint32_t epch_reg;
  633. uint32_t wIstr = USB_ReadInterrupts(hhcd->Instance);
  634. /* check if this is an USB pending IT */
  635. if ((SYSCFG->IT_LINE_SR[8] & (0x1U << 2)) == 0U)
  636. {
  637. return;
  638. }
  639. /* Port Change Detected (Connection/Disconnection) */
  640. if ((wIstr & USB_ISTR_DCON) == USB_ISTR_DCON)
  641. {
  642. /* Clear Flag */
  643. __HAL_HCD_CLEAR_FLAG(hhcd, USB_ISTR_DCON);
  644. /* Call Port IRQHandler */
  645. HCD_Port_IRQHandler(hhcd);
  646. return;
  647. }
  648. /* Correct Transaction Detected -------*/
  649. if ((wIstr & USB_ISTR_CTR) == USB_ISTR_CTR)
  650. {
  651. /* Handle Host channel Interrupt */
  652. for (phy_chnum = 0U; phy_chnum < hhcd->Init.Host_channels; phy_chnum++)
  653. {
  654. if ((HCD_GET_CHANNEL(hhcd->Instance, phy_chnum) & USB_CH_VTRX) != 0U)
  655. {
  656. /* Get Logical channel to check if the channel is already opened */
  657. chnum = HAL_HCD_GetLogical_Channel(hhcd, phy_chnum, 1U);
  658. if (chnum != HCD_LOGICAL_CH_NOT_OPENED)
  659. {
  660. /* Call Channel_IN_IRQ() */
  661. HCD_HC_IN_IRQHandler(hhcd, chnum);
  662. }
  663. else
  664. {
  665. /*Channel was not closed correctly still have interrupt */
  666. epch_reg = HCD_GET_CHANNEL(hhcd->Instance, phy_chnum);
  667. epch_reg = (epch_reg & (USB_CHEP_REG_MASK & (~USB_CH_ERRRX) & (~USB_CH_VTRX))) |
  668. (USB_CH_VTTX | USB_CH_ERRTX);
  669. HCD_SET_CHANNEL(hhcd->Instance, phy_chnum, epch_reg);
  670. }
  671. }
  672. if ((HCD_GET_CHANNEL(hhcd->Instance, phy_chnum) & USB_CH_VTTX) != 0U)
  673. {
  674. /* Get Logical channel to check if the channel is already opened */
  675. chnum = HAL_HCD_GetLogical_Channel(hhcd, phy_chnum, 0U);
  676. if (chnum != HCD_LOGICAL_CH_NOT_OPENED)
  677. {
  678. /*Call Channel_OUT_IRQ()*/
  679. HCD_HC_OUT_IRQHandler(hhcd, chnum);
  680. }
  681. else
  682. {
  683. /* Clear Error & unwanted VTTX or Channel was not closed correctly */
  684. epch_reg = HCD_GET_CHANNEL(hhcd->Instance, phy_chnum);
  685. epch_reg = (epch_reg & (USB_CHEP_REG_MASK & (~USB_CH_ERRTX) & (~USB_CH_VTTX))) |
  686. (USB_CH_VTRX | USB_CH_ERRRX);
  687. HCD_SET_CHANNEL(hhcd->Instance, phy_chnum, epch_reg);
  688. }
  689. }
  690. }
  691. return;
  692. }
  693. /* Wakeup Flag Detected */
  694. if ((wIstr & USB_ISTR_WKUP) == USB_ISTR_WKUP)
  695. {
  696. if (hhcd->HostState == HCD_HCD_STATE_SUSPEND)
  697. {
  698. /* Set The L2Resume bit */
  699. hhcd->Instance->CNTR |= USB_CNTR_L2RES;
  700. /* Clear the wake-up flag */
  701. __HAL_HCD_CLEAR_FLAG(hhcd, USB_ISTR_WKUP);
  702. /* Update the USB Software state machine */
  703. HAL_HCD_ResumeCallback(hhcd);
  704. hhcd->HostState = HCD_HCD_STATE_RESUME;
  705. }
  706. else
  707. {
  708. /* Clear the wake-up flag */
  709. __HAL_HCD_CLEAR_FLAG(hhcd, USB_ISTR_WKUP);
  710. }
  711. return;
  712. }
  713. /* Global Error Flag Detected */
  714. if ((wIstr & USB_ISTR_ERR) == USB_ISTR_ERR)
  715. {
  716. __HAL_HCD_CLEAR_FLAG(hhcd, USB_ISTR_ERR);
  717. return;
  718. }
  719. /* PMA Overrun detected */
  720. if ((wIstr & USB_ISTR_PMAOVR) == USB_ISTR_PMAOVR)
  721. {
  722. __HAL_HCD_CLEAR_FLAG(hhcd, USB_ISTR_PMAOVR);
  723. return;
  724. }
  725. /* Suspend Detected */
  726. if ((wIstr & USB_ISTR_SUSP) == USB_ISTR_SUSP)
  727. {
  728. /* Set HAL State to Suspend */
  729. hhcd->HostState = HCD_HCD_STATE_SUSPEND;
  730. /* Force low-power mode in the macrocell */
  731. hhcd->Instance->CNTR |= USB_CNTR_SUSPEN;
  732. /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
  733. __HAL_HCD_CLEAR_FLAG(hhcd, USB_ISTR_SUSP);
  734. /* Call suspend Callback */
  735. HAL_HCD_SuspendCallback(hhcd);
  736. return;
  737. }
  738. /* Start Of Frame Detected */
  739. if ((wIstr & USB_ISTR_SOF) == USB_ISTR_SOF)
  740. {
  741. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  742. hhcd->SOFCallback(hhcd);
  743. #else
  744. HAL_HCD_SOF_Callback(hhcd);
  745. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  746. __HAL_HCD_CLEAR_FLAG(hhcd, USB_ISTR_SOF);
  747. /* when first SOF is detected after USB_RESET is asserted */
  748. if (hhcd->HostState == HCD_HCD_STATE_RESETED)
  749. {
  750. /* HAL State */
  751. hhcd->HostState = HCD_HCD_STATE_RUN;
  752. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  753. hhcd->PortEnabledCallback(hhcd);
  754. #else
  755. HAL_HCD_PortEnabled_Callback(hhcd);
  756. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  757. }
  758. return;
  759. }
  760. }
  761. /**
  762. * @brief SOF callback.
  763. * @param hhcd HCD handle
  764. * @retval None
  765. */
  766. __weak void HAL_HCD_SOF_Callback(HCD_HandleTypeDef *hhcd)
  767. {
  768. /* Prevent unused argument(s) compilation warning */
  769. UNUSED(hhcd);
  770. /* NOTE : This function Should not be modified, when the callback is needed,
  771. the HAL_HCD_SOF_Callback could be implemented in the user file
  772. */
  773. }
  774. /**
  775. * @brief Connection Event callback.
  776. * @param hhcd HCD handle
  777. * @retval None
  778. */
  779. __weak void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd)
  780. {
  781. /* Prevent unused argument(s) compilation warning */
  782. UNUSED(hhcd);
  783. /* NOTE : This function Should not be modified, when the callback is needed,
  784. the HAL_HCD_Connect_Callback could be implemented in the user file
  785. */
  786. }
  787. /**
  788. * @brief Disconnection Event callback.
  789. * @param hhcd HCD handle
  790. * @retval None
  791. */
  792. __weak void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd)
  793. {
  794. /* Prevent unused argument(s) compilation warning */
  795. UNUSED(hhcd);
  796. /* NOTE : This function Should not be modified, when the callback is needed,
  797. the HAL_HCD_Disconnect_Callback could be implemented in the user file
  798. */
  799. }
  800. /**
  801. * @brief Port Enabled Event callback.
  802. * @param hhcd HCD handle
  803. * @retval None
  804. */
  805. __weak void HAL_HCD_PortEnabled_Callback(HCD_HandleTypeDef *hhcd)
  806. {
  807. /* Prevent unused argument(s) compilation warning */
  808. UNUSED(hhcd);
  809. /* NOTE : This function should not be modified, when the callback is needed,
  810. the HAL_HCD_Disconnect_Callback could be implemented in the user file
  811. */
  812. }
  813. /**
  814. * @brief Port Disabled Event callback.
  815. * @param hhcd HCD handle
  816. * @retval None
  817. */
  818. __weak void HAL_HCD_PortDisabled_Callback(HCD_HandleTypeDef *hhcd)
  819. {
  820. /* Prevent unused argument(s) compilation warning */
  821. UNUSED(hhcd);
  822. /* NOTE : This function should not be modified, when the callback is needed,
  823. the HAL_HCD_Disconnect_Callback could be implemented in the user file
  824. */
  825. }
  826. /**
  827. * @brief Notify URB state change callback.
  828. * @param hhcd HCD handle
  829. * @param chnum Channel number.
  830. * This parameter can be a value from 1 to 15
  831. * @param urb_state
  832. * This parameter can be one of these values:
  833. * URB_IDLE/
  834. * URB_DONE/
  835. * URB_NOTREADY/
  836. * URB_NYET/
  837. * URB_ERROR/
  838. * URB_STALL/
  839. * @retval None
  840. */
  841. __weak void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd,
  842. uint8_t chnum, HCD_URBStateTypeDef urb_state)
  843. {
  844. /* Prevent unused argument(s) compilation warning */
  845. UNUSED(hhcd);
  846. UNUSED(chnum);
  847. UNUSED(urb_state);
  848. /* NOTE : This function Should not be modified, when the callback is needed,
  849. the HAL_HCD_HC_NotifyURBChange_Callback could be implemented in the user file
  850. */
  851. }
  852. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  853. /**
  854. * @brief Register a User USB HCD Callback
  855. * To be used instead of the weak predefined callback
  856. * @param hhcd USB HCD handle
  857. * @param CallbackID ID of the callback to be registered
  858. * This parameter can be one of the following values:
  859. * @arg @ref HAL_HCD_SOF_CB_ID USB HCD SOF callback ID
  860. * @arg @ref HAL_HCD_CONNECT_CB_ID USB HCD Connect callback ID
  861. * @arg @ref HAL_HCD_DISCONNECT_CB_ID HCD Disconnect callback ID
  862. * @arg @ref HAL_HCD_PORT_ENABLED_CB_ID USB HCD Port Enable callback ID
  863. * @arg @ref HAL_HCD_PORT_DISABLED_CB_ID USB HCD Port Disable callback ID
  864. * @arg @ref HAL_HCD_MSPINIT_CB_ID MspDeInit callback ID
  865. * @arg @ref HAL_HCD_MSPDEINIT_CB_ID MspDeInit callback ID
  866. * @param pCallback pointer to the Callback function
  867. * @retval HAL status
  868. */
  869. HAL_StatusTypeDef HAL_HCD_RegisterCallback(HCD_HandleTypeDef *hhcd,
  870. HAL_HCD_CallbackIDTypeDef CallbackID,
  871. pHCD_CallbackTypeDef pCallback)
  872. {
  873. HAL_StatusTypeDef status = HAL_OK;
  874. if (pCallback == NULL)
  875. {
  876. /* Update the error code */
  877. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  878. return HAL_ERROR;
  879. }
  880. /* Process locked */
  881. __HAL_LOCK(hhcd);
  882. if (hhcd->State == HAL_HCD_STATE_READY)
  883. {
  884. switch (CallbackID)
  885. {
  886. case HAL_HCD_SOF_CB_ID :
  887. hhcd->SOFCallback = pCallback;
  888. break;
  889. case HAL_HCD_CONNECT_CB_ID :
  890. hhcd->ConnectCallback = pCallback;
  891. break;
  892. case HAL_HCD_DISCONNECT_CB_ID :
  893. hhcd->DisconnectCallback = pCallback;
  894. break;
  895. case HAL_HCD_PORT_ENABLED_CB_ID :
  896. hhcd->PortEnabledCallback = pCallback;
  897. break;
  898. case HAL_HCD_PORT_DISABLED_CB_ID :
  899. hhcd->PortDisabledCallback = pCallback;
  900. break;
  901. case HAL_HCD_MSPINIT_CB_ID :
  902. hhcd->MspInitCallback = pCallback;
  903. break;
  904. case HAL_HCD_MSPDEINIT_CB_ID :
  905. hhcd->MspDeInitCallback = pCallback;
  906. break;
  907. default :
  908. /* Update the error code */
  909. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  910. /* Return error status */
  911. status = HAL_ERROR;
  912. break;
  913. }
  914. }
  915. else if (hhcd->State == HAL_HCD_STATE_RESET)
  916. {
  917. switch (CallbackID)
  918. {
  919. case HAL_HCD_MSPINIT_CB_ID :
  920. hhcd->MspInitCallback = pCallback;
  921. break;
  922. case HAL_HCD_MSPDEINIT_CB_ID :
  923. hhcd->MspDeInitCallback = pCallback;
  924. break;
  925. default :
  926. /* Update the error code */
  927. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  928. /* Return error status */
  929. status = HAL_ERROR;
  930. break;
  931. }
  932. }
  933. else
  934. {
  935. /* Update the error code */
  936. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  937. /* Return error status */
  938. status = HAL_ERROR;
  939. }
  940. /* Release Lock */
  941. __HAL_UNLOCK(hhcd);
  942. return status;
  943. }
  944. /**
  945. * @brief Unregister an USB HCD Callback
  946. * USB HCD callback is redirected to the weak predefined callback
  947. * @param hhcd USB HCD handle
  948. * @param CallbackID ID of the callback to be unregistered
  949. * This parameter can be one of the following values:
  950. * @arg @ref HAL_HCD_SOF_CB_ID USB HCD SOF callback ID
  951. * @arg @ref HAL_HCD_CONNECT_CB_ID USB HCD Connect callback ID
  952. * @arg @ref HAL_HCD_DISCONNECT_CB_ID DRD HCD Disconnect callback ID
  953. * @arg @ref HAL_HCD_PORT_ENABLED_CB_ID USB HCD Port Enabled callback ID
  954. * @arg @ref HAL_HCD_PORT_DISABLED_CB_ID USB HCD Port Disabled callback ID
  955. * @arg @ref HAL_HCD_MSPINIT_CB_ID MspDeInit callback ID
  956. * @arg @ref HAL_HCD_MSPDEINIT_CB_ID MspDeInit callback ID
  957. * @retval HAL status
  958. */
  959. HAL_StatusTypeDef HAL_HCD_UnRegisterCallback(HCD_HandleTypeDef *hhcd,
  960. HAL_HCD_CallbackIDTypeDef CallbackID)
  961. {
  962. HAL_StatusTypeDef status = HAL_OK;
  963. /* Process locked */
  964. __HAL_LOCK(hhcd);
  965. /* Setup Legacy weak Callbacks */
  966. if (hhcd->State == HAL_HCD_STATE_READY)
  967. {
  968. switch (CallbackID)
  969. {
  970. case HAL_HCD_SOF_CB_ID :
  971. hhcd->SOFCallback = HAL_HCD_SOF_Callback;
  972. break;
  973. case HAL_HCD_CONNECT_CB_ID :
  974. hhcd->ConnectCallback = HAL_HCD_Connect_Callback;
  975. break;
  976. case HAL_HCD_DISCONNECT_CB_ID :
  977. hhcd->DisconnectCallback = HAL_HCD_Disconnect_Callback;
  978. break;
  979. case HAL_HCD_PORT_ENABLED_CB_ID :
  980. hhcd->PortEnabledCallback = HAL_HCD_PortEnabled_Callback;
  981. break;
  982. case HAL_HCD_PORT_DISABLED_CB_ID :
  983. hhcd->PortDisabledCallback = HAL_HCD_PortDisabled_Callback;
  984. break;
  985. case HAL_HCD_MSPINIT_CB_ID :
  986. hhcd->MspInitCallback = HAL_HCD_MspInit;
  987. break;
  988. case HAL_HCD_MSPDEINIT_CB_ID :
  989. hhcd->MspDeInitCallback = HAL_HCD_MspDeInit;
  990. break;
  991. default :
  992. /* Update the error code */
  993. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  994. /* Return error status */
  995. status = HAL_ERROR;
  996. break;
  997. }
  998. }
  999. else if (hhcd->State == HAL_HCD_STATE_RESET)
  1000. {
  1001. switch (CallbackID)
  1002. {
  1003. case HAL_HCD_MSPINIT_CB_ID :
  1004. hhcd->MspInitCallback = HAL_HCD_MspInit;
  1005. break;
  1006. case HAL_HCD_MSPDEINIT_CB_ID :
  1007. hhcd->MspDeInitCallback = HAL_HCD_MspDeInit;
  1008. break;
  1009. default :
  1010. /* Update the error code */
  1011. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  1012. /* Return error status */
  1013. status = HAL_ERROR;
  1014. break;
  1015. }
  1016. }
  1017. else
  1018. {
  1019. /* Update the error code */
  1020. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  1021. /* Return error status */
  1022. status = HAL_ERROR;
  1023. }
  1024. /* Release Lock */
  1025. __HAL_UNLOCK(hhcd);
  1026. return status;
  1027. }
  1028. /**
  1029. * @brief Register USB HCD Host Channel Notify URB Change Callback
  1030. * To be used instead of the weak HAL_HCD_HC_NotifyURBChange_Callback() predefined callback
  1031. * @param hhcd HCD handle
  1032. * @param pCallback pointer to the USB HCD Host Channel Notify URB Change Callback function
  1033. * @retval HAL status
  1034. */
  1035. HAL_StatusTypeDef HAL_HCD_RegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef *hhcd,
  1036. pHCD_HC_NotifyURBChangeCallbackTypeDef pCallback)
  1037. {
  1038. HAL_StatusTypeDef status = HAL_OK;
  1039. if (pCallback == NULL)
  1040. {
  1041. /* Update the error code */
  1042. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  1043. return HAL_ERROR;
  1044. }
  1045. /* Process locked */
  1046. __HAL_LOCK(hhcd);
  1047. if (hhcd->State == HAL_HCD_STATE_READY)
  1048. {
  1049. hhcd->HC_NotifyURBChangeCallback = pCallback;
  1050. }
  1051. else
  1052. {
  1053. /* Update the error code */
  1054. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  1055. /* Return error status */
  1056. status = HAL_ERROR;
  1057. }
  1058. /* Release Lock */
  1059. __HAL_UNLOCK(hhcd);
  1060. return status;
  1061. }
  1062. /**
  1063. * @brief Unregister the USB HCD Host Channel Notify URB Change Callback
  1064. * USB HCD Host Channel Notify URB Change Callback is redirected
  1065. * to the weak HAL_HCD_HC_NotifyURBChange_Callback() predefined callback
  1066. * @param hhcd HCD handle
  1067. * @retval HAL status
  1068. */
  1069. HAL_StatusTypeDef HAL_HCD_UnRegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef *hhcd)
  1070. {
  1071. HAL_StatusTypeDef status = HAL_OK;
  1072. /* Process locked */
  1073. __HAL_LOCK(hhcd);
  1074. if (hhcd->State == HAL_HCD_STATE_READY)
  1075. {
  1076. hhcd->HC_NotifyURBChangeCallback = HAL_HCD_HC_NotifyURBChange_Callback; /* Legacy weak DataOutStageCallback */
  1077. }
  1078. else
  1079. {
  1080. /* Update the error code */
  1081. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  1082. /* Return error status */
  1083. status = HAL_ERROR;
  1084. }
  1085. /* Release Lock */
  1086. __HAL_UNLOCK(hhcd);
  1087. return status;
  1088. }
  1089. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1090. /**
  1091. * @}
  1092. */
  1093. /** @defgroup HCD_Exported_Functions_Group3 Peripheral Control functions
  1094. * @brief Management functions
  1095. *
  1096. @verbatim
  1097. ===============================================================================
  1098. ##### Peripheral Control functions #####
  1099. ===============================================================================
  1100. [..]
  1101. This subsection provides a set of functions allowing to control the HCD data
  1102. transfers.
  1103. @endverbatim
  1104. * @{
  1105. */
  1106. /**
  1107. * @brief Start the host driver.
  1108. * @param hhcd HCD handle
  1109. * @retval HAL status
  1110. */
  1111. HAL_StatusTypeDef HAL_HCD_Start(HCD_HandleTypeDef *hhcd)
  1112. {
  1113. __HAL_LOCK(hhcd);
  1114. /*Set the PullDown on the PHY */
  1115. hhcd->Instance->BCDR |= USB_BCDR_DPPD;
  1116. /* Clear Reset */
  1117. hhcd->Instance->CNTR &= ~USB_CNTR_USBRST;
  1118. /*Remove PowerDown */
  1119. hhcd->Instance->CNTR &= ~USB_CNTR_PDWN;
  1120. __HAL_UNLOCK(hhcd);
  1121. return HAL_OK;
  1122. }
  1123. /**
  1124. * @brief Stop the host driver.
  1125. * @param hhcd HCD handle
  1126. * @retval HAL status
  1127. */
  1128. HAL_StatusTypeDef HAL_HCD_Stop(HCD_HandleTypeDef *hhcd)
  1129. {
  1130. __HAL_LOCK(hhcd);
  1131. /*Stop the Host IP: setting powerdown */
  1132. (void)USB_StopHost(hhcd->Instance);
  1133. /* clear all allocated virtual channel */
  1134. HAL_HCD_ClearPhyChannel(hhcd);
  1135. /* Reset the PMA current pointer */
  1136. (void)HAL_HCD_PMAReset(hhcd);
  1137. /* reset Ep0 Pma allocation state */
  1138. hhcd->ep0_PmaAllocState = 0U;
  1139. __HAL_UNLOCK(hhcd);
  1140. return HAL_OK;
  1141. }
  1142. /**
  1143. * @brief Put the Device in suspend mode
  1144. * @param hhcd HCD handle
  1145. * @retval HAL status
  1146. */
  1147. HAL_StatusTypeDef HAL_HCD_Suspend(HCD_HandleTypeDef *hhcd)
  1148. {
  1149. __IO uint32_t count = 0U;
  1150. /* Set Suspend Mode */
  1151. hhcd->Instance->CNTR |= USB_CNTR_SUSPEN;
  1152. /* wait for Suspend Ready */
  1153. while ((hhcd->Instance->CNTR & USB_CNTR_SUSPRDY) == 0U)
  1154. {
  1155. if (++count > HAL_USB_TIMEOUT)
  1156. {
  1157. return HAL_TIMEOUT;
  1158. }
  1159. }
  1160. return HAL_OK;
  1161. }
  1162. /**
  1163. * @brief Resume host port
  1164. * @param hhcd HCD handle
  1165. * @retval HAL status
  1166. */
  1167. HAL_StatusTypeDef HAL_HCD_Resume(HCD_HandleTypeDef *hhcd)
  1168. {
  1169. /* Set Resume bit */
  1170. hhcd->Instance->CNTR |= USB_CNTR_L2RES;
  1171. return HAL_OK;
  1172. }
  1173. /**
  1174. * @brief Reset the host port.
  1175. * @param hhcd HCD handle
  1176. * @retval HAL status
  1177. */
  1178. HAL_StatusTypeDef HAL_HCD_ResetPort(HCD_HandleTypeDef *hhcd)
  1179. {
  1180. __HAL_LOCK(hhcd);
  1181. /* Reset the USB Port by inserting an SE0 on the bus */
  1182. (void)USB_ResetPort(hhcd->Instance);
  1183. if (hhcd->HostState == HCD_HCD_STATE_CONNECTED)
  1184. {
  1185. hhcd->HostState = HCD_HCD_STATE_RESETED;
  1186. }
  1187. __HAL_UNLOCK(hhcd);
  1188. return HAL_OK;
  1189. }
  1190. /**
  1191. * @brief Resme the host port.
  1192. * @param hhcd HCD handle
  1193. * @retval HAL status
  1194. */
  1195. HAL_StatusTypeDef HAL_HCD_ResumePort(HCD_HandleTypeDef *hhcd)
  1196. {
  1197. /* Set Resume bit */
  1198. hhcd->Instance->CNTR |= USB_CNTR_L2RES;
  1199. HAL_Delay(30U);
  1200. /* Clear Resume bit */
  1201. hhcd->Instance->CNTR &= ~USB_CNTR_L2RES;
  1202. return HAL_OK;
  1203. }
  1204. /**
  1205. * @}
  1206. */
  1207. /** @defgroup HCD_Exported_Functions_Group4 Peripheral State functions
  1208. * @brief Peripheral State functions
  1209. *
  1210. @verbatim
  1211. ===============================================================================
  1212. ##### Peripheral State functions #####
  1213. ===============================================================================
  1214. [..]
  1215. This subsection permits to get in run-time the status of the peripheral
  1216. and the data flow.
  1217. @endverbatim
  1218. * @{
  1219. */
  1220. /**
  1221. * @brief Return the HCD handle state.
  1222. * @param hhcd HCD handle
  1223. * @retval HAL state
  1224. */
  1225. HCD_StateTypeDef HAL_HCD_GetState(HCD_HandleTypeDef const *hhcd)
  1226. {
  1227. return hhcd->State;
  1228. }
  1229. /**
  1230. * @brief Return URB state for a channel.
  1231. * @param hhcd HCD handle
  1232. * @param chnum Channel number.
  1233. * This parameter can be a value from 1 to 15
  1234. * @retval URB state.
  1235. * This parameter can be one of these values:
  1236. * URB_IDLE/
  1237. * URB_DONE/
  1238. * URB_NOTREADY/
  1239. * URB_NYET/
  1240. * URB_ERROR/
  1241. * URB_STALL
  1242. */
  1243. HCD_URBStateTypeDef HAL_HCD_HC_GetURBState(HCD_HandleTypeDef const *hhcd, uint8_t chnum)
  1244. {
  1245. return hhcd->hc[chnum].urb_state;
  1246. }
  1247. /**
  1248. * @brief Return the last host transfer size.
  1249. * @param hhcd HCD handle
  1250. * @param chnum Channel number.
  1251. * This parameter can be a value from 1 to 15
  1252. * @retval last transfer size in byte
  1253. */
  1254. uint32_t HAL_HCD_HC_GetXferCount(HCD_HandleTypeDef const *hhcd, uint8_t chnum)
  1255. {
  1256. return hhcd->hc[chnum].xfer_count;
  1257. }
  1258. /**
  1259. * @brief Return the Host Channel state.
  1260. * @param hhcd HCD handle
  1261. * @param chnum Channel number.
  1262. * This parameter can be a value from 1 to 15
  1263. * @retval Host channel state
  1264. * This parameter can be one of these values:
  1265. * HC_IDLE/
  1266. * HC_XFRC/
  1267. * HC_HALTED/
  1268. * HC_NYET/
  1269. * HC_NAK/
  1270. * HC_STALL/
  1271. * HC_XACTERR/
  1272. * HC_BBLERR/
  1273. * HC_DATATGLERR
  1274. */
  1275. HCD_HCStateTypeDef HAL_HCD_HC_GetState(HCD_HandleTypeDef const *hhcd, uint8_t chnum)
  1276. {
  1277. return hhcd->hc[chnum].state;
  1278. }
  1279. /**
  1280. * @brief Return the current Host frame number.
  1281. * @param hhcd HCD handle
  1282. * @retval Current Host frame number
  1283. */
  1284. uint32_t HAL_HCD_GetCurrentFrame(HCD_HandleTypeDef *hhcd)
  1285. {
  1286. return (USB_GetCurrentFrame(hhcd->Instance));
  1287. }
  1288. /**
  1289. * @brief Return the Host enumeration speed.
  1290. * @param hhcd HCD handle
  1291. * @retval speed : Device speed after Host enumeration
  1292. * This parameter can be one of these values:
  1293. * @arg HCD_DEVICE_SPEED_FULL: Full speed mode
  1294. * @arg HCD_DEVICE_SPEED_LOW: Low speed mode
  1295. */
  1296. uint32_t HAL_HCD_GetCurrentSpeed(HCD_HandleTypeDef *hhcd)
  1297. {
  1298. return (USB_GetHostSpeed(hhcd->Instance));
  1299. }
  1300. /**
  1301. * @brief Set host channel Hub Information.
  1302. * @param hhcd HCD handle
  1303. * @param ch_num Channel number.
  1304. * This parameter can be a value from 1 to 8
  1305. * @param addr Hub address
  1306. * @param PortNbr Hub port number
  1307. * @retval HAL status
  1308. */
  1309. HAL_StatusTypeDef HAL_HCD_HC_SetHubInfo(HCD_HandleTypeDef *hhcd, uint8_t ch_num,
  1310. uint8_t addr, uint8_t PortNbr)
  1311. {
  1312. hhcd->hc[ch_num].hub_addr = addr;
  1313. hhcd->hc[ch_num].hub_port_nbr = PortNbr;
  1314. return HAL_OK;
  1315. }
  1316. /**
  1317. * @brief Clear host channel hub information.
  1318. * @param hhcd HCD handle
  1319. * @param ch_num Channel number.
  1320. * This parameter can be a value from 1 to 8
  1321. * @retval HAL status
  1322. */
  1323. HAL_StatusTypeDef HAL_HCD_HC_ClearHubInfo(HCD_HandleTypeDef *hhcd, uint8_t ch_num)
  1324. {
  1325. hhcd->hc[ch_num].hub_addr = 0U;
  1326. hhcd->hc[ch_num].hub_port_nbr = 0U;
  1327. return HAL_OK;
  1328. }
  1329. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1330. /**
  1331. * @brief Handle Host Channel OUT Double Buffer Bulk requests.
  1332. * @param hhcd HCD handle
  1333. * @param ch_num Channel number This parameter can be a value from 1 to 15
  1334. * @param phy_chnum Physical Channel number [0..7]
  1335. * @param regvalue contain Snapshot of the EPCHn register when ISR is detected
  1336. * @retval none
  1337. */
  1338. static void HCD_HC_OUT_BulkDb(HCD_HandleTypeDef *hhcd, uint8_t ch_num,
  1339. uint8_t phy_chnum, uint32_t regvalue)
  1340. {
  1341. uint16_t data_xfr;
  1342. uint16_t len;
  1343. /* Send Buffer0 */
  1344. if ((regvalue & USB_CH_DTOG_TX) != 0U)
  1345. {
  1346. data_xfr = (uint16_t)(((USB_DRD_PMA_BUFF + phy_chnum)->TXBD & 0x03FF0000U) >> 16U);
  1347. if (hhcd->hc[ch_num & 0xFU].xfer_len >= data_xfr)
  1348. {
  1349. hhcd->hc[ch_num & 0xFU].xfer_len -= data_xfr;
  1350. }
  1351. else
  1352. {
  1353. hhcd->hc[ch_num & 0xFU].xfer_len = 0U;
  1354. }
  1355. /* Transfer no yet finished only one packet of mps is transferred and ACKed from device */
  1356. if (hhcd->hc[ch_num & 0xFU].xfer_len != 0U)
  1357. {
  1358. /* manage multiple Xfer */
  1359. hhcd->hc[ch_num & 0xFU].xfer_count += data_xfr;
  1360. /* check if we need to free user buffer */
  1361. if ((regvalue & USB_CH_DTOG_RX) != 0U)
  1362. {
  1363. /* Toggle SwBuff */
  1364. HCD_CLEAR_TX_DTOG(hhcd->Instance, phy_chnum);
  1365. HCD_CLEAR_RX_DTOG(hhcd->Instance, phy_chnum);
  1366. HCD_TX_DTOG(hhcd->Instance, phy_chnum);
  1367. }
  1368. /* hhcd->hc[ch_num&0xFU].xfer_len_db==0 ==> when all data are written in the PMA to yet transferred */
  1369. if (hhcd->hc[ch_num & 0xFU].xfer_len_db > 0U) /* Still data to fill in the buffer */
  1370. {
  1371. hhcd->hc[ch_num & 0xFU].xfer_buff += data_xfr;
  1372. /* calculate len of new buffer to fill */
  1373. if (hhcd->hc[ch_num & 0xFU].xfer_len_db > hhcd->hc[ch_num & 0xFU].max_packet)
  1374. {
  1375. len = (uint16_t)hhcd->hc[ch_num & 0xFU].max_packet;
  1376. hhcd->hc[ch_num & 0xFU].xfer_len_db -= len;
  1377. }
  1378. else
  1379. {
  1380. len = (uint16_t)hhcd->hc[ch_num & 0xFU].xfer_len_db;
  1381. hhcd->hc[ch_num & 0xFU].xfer_len_db = 0U; /* end of fill buffer */
  1382. }
  1383. /* Write remaining data to Buffer0 */
  1384. HCD_SET_CH_DBUF0_CNT(hhcd->Instance, phy_chnum, 1U, (uint16_t)len);
  1385. USB_WritePMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff,
  1386. hhcd->hc[ch_num & 0xFU].pmaaddr0, (uint16_t)len);
  1387. }
  1388. /* start a new transfer */
  1389. HCD_SET_CH_TX_STATUS(hhcd->Instance, phy_chnum, USB_CH_TX_VALID);
  1390. }
  1391. else
  1392. {
  1393. /* Transfer complete state */
  1394. hhcd->hc[ch_num & 0xFU].xfer_count += data_xfr;
  1395. hhcd->hc[ch_num & 0xFU].state = HC_XFRC;
  1396. hhcd->hc[ch_num & 0xFU].urb_state = URB_DONE;
  1397. hhcd->hc[ch_num & 0xFU].toggle_out ^= 1U;
  1398. /* Close the Channel */
  1399. HCD_SET_CH_TX_STATUS(hhcd->Instance, phy_chnum, USB_CH_TX_DIS);
  1400. }
  1401. }
  1402. else
  1403. {
  1404. /* Send Buffer1 */
  1405. data_xfr = (uint16_t)(((USB_DRD_PMA_BUFF + phy_chnum)->RXBD & 0x03FF0000U) >> 16U);
  1406. if (hhcd->hc[ch_num & 0xFU].xfer_len >= data_xfr) /* updated */
  1407. {
  1408. hhcd->hc[ch_num & 0xFU].xfer_len -= data_xfr;
  1409. }
  1410. /* Transfer no yet finished only one packet of mps is transferred and ACKed from device */
  1411. if (hhcd->hc[ch_num & 0xFU].xfer_len != 0U)
  1412. {
  1413. /* manage multiple Xfer */
  1414. hhcd->hc[ch_num & 0xFU].xfer_count += data_xfr;
  1415. /* check if we need to free user buffer */
  1416. if ((regvalue & USB_CH_DTOG_RX) == 0U)
  1417. {
  1418. /* Toggle SwBuff */
  1419. HCD_CLEAR_TX_DTOG(hhcd->Instance, phy_chnum);
  1420. HCD_CLEAR_RX_DTOG(hhcd->Instance, phy_chnum);
  1421. HCD_RX_DTOG(hhcd->Instance, phy_chnum);
  1422. }
  1423. /* hhcd->hc[ch_num&0xFU].xfer_len_db==0 ==> when all data are written in the PMA to yet transferred */
  1424. if (hhcd->hc[ch_num & 0xFU].xfer_len_db > 0U) /* Still data to fill in the buffer */
  1425. {
  1426. hhcd->hc[ch_num & 0xFU].xfer_buff += data_xfr;
  1427. /* calculate len of new buffer to fill */
  1428. if (hhcd->hc[ch_num & 0xFU].xfer_len_db > hhcd->hc[ch_num & 0xFU].max_packet)
  1429. {
  1430. len = hhcd->hc[ch_num & 0xFU].max_packet;
  1431. hhcd->hc[ch_num & 0xFU].xfer_len_db -= len;
  1432. }
  1433. else
  1434. {
  1435. len = (uint16_t)hhcd->hc[ch_num & 0xFU].xfer_len_db;
  1436. hhcd->hc[ch_num & 0xFU].xfer_len_db = 0U; /* end of fill buffer */
  1437. }
  1438. /* Write remaining data to Buffer0 */
  1439. HCD_SET_CH_DBUF1_CNT(hhcd->Instance, phy_chnum, 1U, (uint16_t)len);
  1440. USB_WritePMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff,
  1441. hhcd->hc[ch_num & 0xFU].pmaaddr1, (uint16_t)len);
  1442. }
  1443. /* start a new transfer */
  1444. HCD_SET_CH_TX_STATUS(hhcd->Instance, phy_chnum, USB_CH_TX_VALID);
  1445. }
  1446. else
  1447. {
  1448. /* Transfer complete state */
  1449. hhcd->hc[ch_num & 0xFU].xfer_count += data_xfr;
  1450. hhcd->hc[ch_num & 0xFU].state = HC_XFRC;
  1451. hhcd->hc[ch_num & 0xFU].urb_state = URB_DONE;
  1452. hhcd->hc[ch_num & 0xFU].toggle_out ^= 1U;
  1453. /* Close the channel */
  1454. HCD_SET_CH_TX_STATUS(hhcd->Instance, phy_chnum, USB_CH_TX_DIS);
  1455. }
  1456. }
  1457. }
  1458. /**
  1459. * @brief Handle Host Channel IN Double Buffer Bulk requests.
  1460. * @param hhcd HCD handle
  1461. * @param ch_num Channel number: This parameter can be a value from 1 to 15
  1462. * @param phy_chnum Physical Channel number [0..7]
  1463. * @param regvalue contain Snapshot of the EPCHn register when ISR is detected
  1464. * @retval none
  1465. */
  1466. static void HCD_HC_IN_BulkDb(HCD_HandleTypeDef *hhcd,
  1467. uint8_t ch_num, uint8_t phy_chnum, uint32_t regvalue)
  1468. {
  1469. uint16_t received_bytes;
  1470. /* Read from Buffer 0 */
  1471. if ((regvalue & USB_CH_DTOG_RX) != 0U)
  1472. {
  1473. received_bytes = (uint16_t)HCD_GET_CH_DBUF0_CNT(hhcd->Instance, phy_chnum);
  1474. if (hhcd->hc[ch_num & 0xFU].xfer_len <= received_bytes)
  1475. {
  1476. hhcd->hc[ch_num & 0xFU].xfer_len = 0U;
  1477. }
  1478. else
  1479. {
  1480. hhcd->hc[ch_num & 0xFU].xfer_len -= received_bytes;
  1481. }
  1482. /* Check if we Need to free the other buffer for the IP */
  1483. if ((hhcd->hc[ch_num & 0xFU].xfer_len != 0U) && ((regvalue & USB_CH_DTOG_TX) != 0U))
  1484. {
  1485. /* Toggle SwBuff to Allow the IP to submit a new IN */
  1486. HCD_FREE_USER_BUFFER(hhcd->Instance, phy_chnum, 0U);
  1487. }
  1488. /* Read the byte from PMA to user Buffer(System Memory) */
  1489. USB_ReadPMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff,
  1490. hhcd->hc[ch_num & 0xFU].pmaaddr0, (uint16_t)received_bytes);
  1491. }
  1492. else
  1493. {
  1494. /* Read from Buffer 1 */
  1495. received_bytes = (uint16_t) HCD_GET_CH_DBUF1_CNT(hhcd->Instance, phy_chnum);
  1496. if (hhcd->hc[ch_num & 0xFU].xfer_len <= received_bytes)
  1497. {
  1498. hhcd->hc[ch_num & 0xFU].xfer_len = 0U;
  1499. }
  1500. else
  1501. {
  1502. hhcd->hc[ch_num & 0xFU].xfer_len -= received_bytes;
  1503. }
  1504. /* Check if we Need to free the other buffer for the IP */
  1505. if ((hhcd->hc[ch_num & 0xFU].xfer_len != 0U) && ((regvalue & USB_CH_DTOG_TX) == 0U))
  1506. {
  1507. /* Toggle SwBuff */
  1508. HCD_FREE_USER_BUFFER(hhcd->Instance, phy_chnum, 0U);
  1509. }
  1510. /* Read the byte from PMA to user Buffer(System Memory) */
  1511. USB_ReadPMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff,
  1512. hhcd->hc[ch_num & 0xFU].pmaaddr1, (uint16_t)received_bytes);
  1513. }
  1514. /* update the global number of all received bytes */
  1515. hhcd->hc[ch_num & 0xFU].xfer_count += received_bytes;
  1516. /* Transfer complete state */
  1517. hhcd->hc[ch_num & 0xFU].state = HC_ACK;
  1518. hhcd->hc[ch_num & 0xFU].ErrCnt = 0U;
  1519. if ((hhcd->hc[ch_num & 0xFU].xfer_len == 0U) ||
  1520. ((received_bytes < hhcd->hc[ch_num & 0xFU].max_packet)))
  1521. {
  1522. hhcd->hc[ch_num & 0xFU].urb_state = URB_DONE;
  1523. hhcd->hc[ch_num & 0xFU].state = HC_XFRC;
  1524. /* disable channel */
  1525. HCD_SET_CH_RX_STATUS(hhcd->Instance, phy_chnum, USB_CH_RX_DIS);
  1526. }
  1527. else
  1528. {
  1529. hhcd->hc[ch_num & 0xFU].xfer_buff += received_bytes;
  1530. /* Reactivate the Channel Submit an other URB since the Transfer is not yet completed */
  1531. HCD_SET_CH_RX_STATUS(hhcd->Instance, phy_chnum, USB_CH_RX_STRX);
  1532. }
  1533. }
  1534. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1535. /**
  1536. * @brief Handle Host Channel IN Isochronous Transaction
  1537. * @param hhcd HCD handle
  1538. * @param ch_num Channel number: This parameter can be a value from 1 to 15
  1539. * @param phy_chnum Physical Channel number [0..7]
  1540. * @param regvalue contain Snapshot of the EPCHn register when ISR is detected
  1541. * @retval none
  1542. */
  1543. static void inline HCD_HC_IN_ISO(HCD_HandleTypeDef *hhcd, uint8_t ch_num,
  1544. uint8_t phy_chnum, uint32_t regvalue)
  1545. {
  1546. /* Check if Double buffer isochronous */
  1547. if ((regvalue & USB_CH_KIND) != 0U)
  1548. {
  1549. /* Get Data IN Packet */
  1550. hhcd->hc[ch_num & 0xFU].xfer_count = HCD_GET_CH_RX_CNT(hhcd->Instance, phy_chnum);
  1551. if (hhcd->hc[ch_num & 0xFU].xfer_count != 0U)
  1552. {
  1553. USB_ReadPMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff,
  1554. hhcd->hc[ch_num & 0xFU].pmaadress,
  1555. (uint16_t)hhcd->hc[ch_num & 0xFU].xfer_count);
  1556. hhcd->hc[ch_num & 0xFU].urb_state = URB_DONE;
  1557. }
  1558. }
  1559. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1560. else /* double buffer isochronous */
  1561. {
  1562. /* Read from Buffer0 */
  1563. if ((regvalue & USB_CH_DTOG_RX) != 0U)
  1564. {
  1565. /* Get number of Received byte in buffer0 */
  1566. hhcd->hc[ch_num & 0xFU].xfer_count = HCD_GET_CH_DBUF0_CNT(hhcd->Instance, phy_chnum);
  1567. if (hhcd->hc[ch_num & 0xFU].xfer_count != 0U)
  1568. {
  1569. /* Read from Buffer0 */
  1570. USB_ReadPMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff,
  1571. hhcd->hc[ch_num & 0xFU].pmaaddr0,
  1572. (uint16_t)hhcd->hc[ch_num & 0xFU].xfer_count);
  1573. hhcd->hc[ch_num & 0xFU].urb_state = URB_DONE;
  1574. }
  1575. }
  1576. else
  1577. {
  1578. /* Get number of Received byte in buffer1 */
  1579. hhcd->hc[ch_num & 0xFU].xfer_count = HCD_GET_CH_DBUF1_CNT(hhcd->Instance, phy_chnum);
  1580. if (hhcd->hc[ch_num & 0xFU].xfer_count != 0U)
  1581. {
  1582. /* Read from Buffer1 */
  1583. USB_ReadPMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff,
  1584. hhcd->hc[ch_num & 0xFU].pmaaddr1,
  1585. (uint16_t)hhcd->hc[ch_num & 0xFU].xfer_count);
  1586. hhcd->hc[ch_num & 0xFU].urb_state = URB_DONE;
  1587. }
  1588. }
  1589. }
  1590. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1591. /* Transfer complete state */
  1592. hhcd->hc[ch_num & 0xFU].state = HC_XFRC;
  1593. /* Clear VTRX */
  1594. HCD_CLEAR_RX_CH_CTR(hhcd->Instance, phy_chnum);
  1595. }
  1596. /**
  1597. * @brief Handle Host Channel IN interrupt requests.
  1598. * @param hhcd HCD handle
  1599. * @param ch_num Channel number
  1600. * This parameter can be a value from 1 to 15
  1601. * @retval none
  1602. */
  1603. static void HCD_HC_IN_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t ch_num)
  1604. {
  1605. uint16_t received_bytes;
  1606. uint8_t phy_chnum = (uint8_t)__HAL_HCD_GET_CHNUM(hhcd);
  1607. /*Take a Flag snapshot from the CHEP register, due to STRX bits are used for both control and status */
  1608. uint32_t ch_reg = HCD_GET_CHANNEL(hhcd->Instance, phy_chnum);
  1609. /* Manage Correct Transaction */
  1610. if ((ch_reg & USB_CH_ERRRX) == 0U)
  1611. {
  1612. /* Isochronous Channel */
  1613. if ((ch_reg & USB_CH_UTYPE) == USB_EP_ISOCHRONOUS)
  1614. {
  1615. HCD_HC_IN_ISO(hhcd, ch_num, phy_chnum, ch_reg);
  1616. }
  1617. else
  1618. {
  1619. /* manage ACK response single buffer */
  1620. if (((ch_reg) & USB_CH_RX_STRX) == USB_CH_RX_ACK_SBUF)
  1621. {
  1622. /* Get Control Data OUT Packet */
  1623. received_bytes = (uint16_t)HCD_GET_CH_RX_CNT(hhcd->Instance, phy_chnum);
  1624. /* Read the byte from PMA to user Buffer(System Memory) */
  1625. USB_ReadPMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff,
  1626. hhcd->hc[ch_num & 0xFU].pmaadress, (uint16_t)received_bytes);
  1627. /* update the global number of all received bytes */
  1628. hhcd->hc[ch_num & 0xFU].xfer_count += received_bytes;
  1629. /* Transfer complete state */
  1630. hhcd->hc[ch_num & 0xFU].state = HC_ACK;
  1631. hhcd->hc[ch_num & 0xFU].ErrCnt = 0U;
  1632. if (hhcd->hc[ch_num & 0xFU].xfer_len <= received_bytes)
  1633. {
  1634. hhcd->hc[ch_num & 0xFU].xfer_len = 0U;
  1635. }
  1636. else
  1637. {
  1638. hhcd->hc[ch_num & 0xFU].xfer_len -= received_bytes;
  1639. }
  1640. if ((hhcd->hc[ch_num & 0xFU].xfer_len == 0U) ||
  1641. ((received_bytes < hhcd->hc[ch_num & 0xFU].max_packet)))
  1642. {
  1643. hhcd->hc[ch_num & 0xFU].urb_state = URB_DONE;
  1644. hhcd->hc[ch_num & 0xFU].state = HC_XFRC;
  1645. }
  1646. else
  1647. {
  1648. hhcd->hc[ch_num & 0xFU].xfer_buff += received_bytes;
  1649. /* Reactivate the Channel to Submit another URB since the Transfer is not yet completed */
  1650. HCD_SET_CH_RX_STATUS(hhcd->Instance, phy_chnum, USB_CH_RX_STRX);
  1651. }
  1652. if ((hhcd->hc[ch_num & 0xFU].ep_type == EP_TYPE_BULK) ||
  1653. (hhcd->hc[ch_num & 0xFU].ep_type == EP_TYPE_INTR))
  1654. {
  1655. hhcd->hc[ch_num & 0xFU].toggle_out ^= 1U;
  1656. }
  1657. }
  1658. /* manage NACK Response */
  1659. else if (((ch_reg & USB_CH_RX_STRX) == USB_CH_RX_NAK)
  1660. && (hhcd->hc[ch_num & 0xFU].urb_state != URB_DONE))
  1661. {
  1662. hhcd->hc[ch_num & 0xFU].urb_state = URB_NOTREADY;
  1663. hhcd->hc[ch_num & 0xFU].ErrCnt = 0U;
  1664. hhcd->hc[ch_num & 0xFU].state = HC_NAK;
  1665. if (hhcd->hc[ch_num & 0xFU].ep_type == EP_TYPE_INTR)
  1666. {
  1667. /* Close the channel */
  1668. HCD_SET_CH_RX_STATUS(hhcd->Instance, phy_chnum, USB_CH_RX_DIS);
  1669. }
  1670. }
  1671. /* manage STALL Response */
  1672. else if ((ch_reg & USB_CH_RX_STRX) == USB_CH_RX_STALL)
  1673. {
  1674. (void)HAL_HCD_HC_Halt(hhcd, ch_num);
  1675. hhcd->hc[ch_num & 0xFU].state = HC_STALL;
  1676. hhcd->hc[ch_num & 0xFU].urb_state = URB_STALL;
  1677. /* Close the channel */
  1678. HCD_SET_CH_RX_STATUS(hhcd->Instance, phy_chnum, USB_CH_RX_DIS);
  1679. }
  1680. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1681. /* Double Buffer Management in case of Bulk Transaction */
  1682. else if (((ch_reg & USB_CH_RX_STRX) == USB_CH_RX_ACK_DBUF)
  1683. && ((ch_reg & USB_CH_KIND) != 0U))
  1684. {
  1685. /* Bulk IN Double Buffer ISR */
  1686. HCD_HC_IN_BulkDb(hhcd, ch_num, phy_chnum, ch_reg);
  1687. }
  1688. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1689. else
  1690. {
  1691. /*....*/
  1692. /* not defined state: STRX=11 in single buffer no iso is not defined */
  1693. }
  1694. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1695. hhcd->HC_NotifyURBChangeCallback(hhcd, ch_num, hhcd->hc[ch_num & 0xFU].urb_state);
  1696. #else
  1697. HAL_HCD_HC_NotifyURBChange_Callback(hhcd, ch_num, hhcd->hc[ch_num & 0xFU].urb_state);
  1698. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1699. /*Clear VTRX */
  1700. HCD_CLEAR_RX_CH_CTR(hhcd->Instance, phy_chnum);
  1701. }
  1702. }
  1703. else /* Error detected during last transaction */
  1704. {
  1705. /* Set URB Error State */
  1706. hhcd->hc[ch_num & 0xFU].urb_state = URB_NOTREADY;
  1707. hhcd->hc[ch_num & 0xFU].ErrCnt++;
  1708. hhcd->hc[ch_num & 0xFU].state = HC_XACTERR;
  1709. /* Clear VTTRX & ERR_RX */
  1710. HCD_CLEAR_RX_CH_ERR(hhcd->Instance, phy_chnum);
  1711. /* Check Error number */
  1712. if (hhcd->hc[ch_num & 0xFU].ErrCnt > 3U)
  1713. {
  1714. hhcd->hc[ch_num & 0xFU].urb_state = URB_ERROR;
  1715. HCD_SET_CH_RX_STATUS(hhcd->Instance, phy_chnum, USB_CH_RX_DIS);
  1716. /* Clear pending err_tx */
  1717. HCD_CLEAR_RX_CH_ERR(hhcd->Instance, phy_chnum);
  1718. }
  1719. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1720. hhcd->HC_NotifyURBChangeCallback(hhcd, ch_num, hhcd->hc[ch_num & 0xFU].urb_state);
  1721. #else
  1722. HAL_HCD_HC_NotifyURBChange_Callback(hhcd, ch_num, hhcd->hc[ch_num & 0xFU].urb_state);
  1723. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1724. }
  1725. }
  1726. /**
  1727. * @brief Handle Host Channel OUT interrupt requests.
  1728. * @param hhcd HCD handle
  1729. * @param chnum Channel number
  1730. * This parameter can be a value from 1 to 15
  1731. * @retval none
  1732. */
  1733. static void HCD_HC_OUT_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum)
  1734. {
  1735. uint16_t data_xfr;
  1736. __IO uint32_t WregCh;
  1737. /* Get Physical Channel number */
  1738. uint32_t phy_chnum = (uint8_t)__HAL_HCD_GET_CHNUM(hhcd);
  1739. /* Take a Flag snapshot from the CHEP register, due to STRX bits are used for both control &status */
  1740. uint32_t ch_reg = *(__IO uint32_t *)(&(hhcd->Instance->CHEP0R) + phy_chnum);
  1741. /*------ Manage Correct Transaction ------*/
  1742. if ((ch_reg & USB_CH_ERRTX) == 0U)
  1743. {
  1744. /* Handle Isochronous channel */
  1745. if ((ch_reg & USB_CH_UTYPE) == USB_EP_ISOCHRONOUS)
  1746. {
  1747. /* correct transaction */
  1748. if ((hhcd->Instance->ISTR & USB_ISTR_ERR) == 0U)
  1749. {
  1750. /* Double buffer isochronous out */
  1751. if ((ch_reg & USB_CH_KIND) != 0U)
  1752. {
  1753. HCD_SET_CH_TX_CNT(hhcd->Instance, phy_chnum, 0U);
  1754. }
  1755. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1756. else /* double buffer isochronous out */
  1757. {
  1758. /* Odd Transaction */
  1759. if ((ch_reg & USB_CH_DTOG_TX) != 0U)
  1760. {
  1761. HCD_SET_CH_TX_CNT(hhcd->Instance, phy_chnum, 0U);
  1762. }
  1763. /* Even Transaction */
  1764. else
  1765. {
  1766. HCD_SET_CH_RX_CNT(hhcd->Instance, phy_chnum, 0U);
  1767. }
  1768. USB_DRD_SET_CHEP_TX_STATUS(hhcd->Instance, phy_chnum, USB_CH_TX_DIS);
  1769. }
  1770. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1771. /* Transfer complete state */
  1772. hhcd->hc[chnum & 0xFU].state = HC_XFRC;
  1773. hhcd->hc[chnum & 0xFU].urb_state = URB_DONE;
  1774. }
  1775. /*Clear Correct Transfer */
  1776. HCD_CLEAR_TX_CH_CTR(hhcd->Instance, phy_chnum);
  1777. /*TX COMPLETE*/
  1778. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1779. hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state);
  1780. #else
  1781. HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state);
  1782. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1783. }
  1784. else /* Manage all Non Isochronous Transaction */
  1785. {
  1786. /* Check ACK response */
  1787. if ((ch_reg & USB_CH_TX_STTX) == USB_CH_TX_ACK_SBUF)
  1788. {
  1789. data_xfr = (uint16_t)(((USB_DRD_PMA_BUFF + phy_chnum)->TXBD & 0x03FF0000U) >> 16U);
  1790. if (hhcd->hc[chnum & 0xFU].xfer_len >= data_xfr)
  1791. {
  1792. hhcd->hc[chnum & 0xFU].xfer_len -= data_xfr;
  1793. }
  1794. else
  1795. {
  1796. hhcd->hc[chnum & 0xFU].xfer_len = 0U;
  1797. }
  1798. /* Transfer no yet finished only one packet of mps is transferred and ACKed from device */
  1799. if (hhcd->hc[chnum & 0xFU].xfer_len != 0U)
  1800. {
  1801. /* manage multiple Xfer */
  1802. hhcd->hc[chnum & 0xFU].xfer_buff += data_xfr;
  1803. hhcd->hc[chnum & 0xFU].xfer_count += data_xfr;
  1804. /* start a new transfer */
  1805. (void) USB_HC_StartXfer(hhcd->Instance, &hhcd->hc[chnum & 0xFU]);
  1806. }
  1807. else
  1808. {
  1809. /* Transfer complete */
  1810. hhcd->hc[chnum & 0xFU].xfer_count += data_xfr;
  1811. hhcd->hc[chnum & 0xFU].state = HC_XFRC;
  1812. hhcd->hc[chnum & 0xFU].urb_state = URB_DONE;
  1813. if ((hhcd->hc[chnum & 0xFU].ep_type == EP_TYPE_BULK) ||
  1814. (hhcd->hc[chnum & 0xFU].ep_type == EP_TYPE_INTR))
  1815. {
  1816. hhcd->hc[chnum & 0xFU].toggle_out ^= 1U;
  1817. }
  1818. }
  1819. }
  1820. /* Check NACK Response */
  1821. else if (((ch_reg & USB_CHEP_NAK) == USB_CHEP_NAK) ||
  1822. ((ch_reg & USB_CH_TX_STTX) == USB_CH_TX_NAK))
  1823. {
  1824. /* Update Channel status */
  1825. hhcd->hc[chnum & 0xFU].state = HC_NAK;
  1826. hhcd->hc[chnum & 0xFU].urb_state = URB_NOTREADY;
  1827. hhcd->hc[chnum & 0xFU].ErrCnt = 0U;
  1828. /* Get Channel register value */
  1829. WregCh = *(__IO uint32_t *)(&(hhcd->Instance->CHEP0R) + phy_chnum);
  1830. /*clear NAK status*/
  1831. WregCh &= ~USB_CHEP_NAK & USB_CHEP_REG_MASK;
  1832. /* Update channel register Value */
  1833. HCD_SET_CHANNEL(hhcd->Instance, phy_chnum, WregCh);
  1834. if (hhcd->hc[chnum & 0xFU].doublebuffer == 0U)
  1835. {
  1836. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1837. hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state);
  1838. #else
  1839. HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state);
  1840. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1841. }
  1842. }
  1843. /* Check STALL Response */
  1844. else if ((ch_reg & USB_CH_TX_STTX) == USB_CH_TX_STALL)
  1845. {
  1846. (void) HAL_HCD_HC_Halt(hhcd, (uint8_t)chnum);
  1847. hhcd->hc[chnum & 0xFU].state = HC_STALL;
  1848. hhcd->hc[chnum & 0xFU].urb_state = URB_STALL;
  1849. }
  1850. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1851. /* Check double buffer ACK in case of bulk transaction */
  1852. else if ((ch_reg & USB_CH_TX_STTX) == USB_CH_TX_ACK_DBUF)
  1853. {
  1854. /* Double buffer management Bulk Out */
  1855. (void) HCD_HC_OUT_BulkDb(hhcd, chnum, (uint8_t)phy_chnum, ch_reg);
  1856. }
  1857. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1858. else
  1859. {
  1860. /*...*/
  1861. }
  1862. if ((ch_reg & USB_CH_TX_STTX) != USB_CH_TX_NAK)
  1863. {
  1864. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1865. hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state);
  1866. #else
  1867. HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state);
  1868. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1869. }
  1870. HCD_CLEAR_TX_CH_CTR(hhcd->Instance, phy_chnum);
  1871. } /* end no isochronous */
  1872. }
  1873. /*------ Manage Transaction Error------*/
  1874. else
  1875. {
  1876. hhcd->hc[chnum & 0xFU].ErrCnt++;
  1877. if (hhcd->hc[chnum & 0xFU].ErrCnt > 3U)
  1878. {
  1879. HCD_SET_CH_TX_STATUS(hhcd->Instance, phy_chnum, USB_CH_TX_DIS);
  1880. hhcd->hc[chnum & 0xFU].urb_state = URB_ERROR;
  1881. }
  1882. else
  1883. {
  1884. hhcd->hc[chnum & 0xFU].urb_state = URB_NOTREADY;
  1885. }
  1886. hhcd->hc[chnum & 0xFU].state = HC_XACTERR;
  1887. /*Clear ERR_TX*/
  1888. HCD_CLEAR_TX_CH_ERR(hhcd->Instance, phy_chnum);
  1889. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1890. hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state);
  1891. #else
  1892. HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state);
  1893. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1894. }
  1895. }
  1896. /**
  1897. * @brief Handle Host Port interrupt requests.
  1898. * @param hhcd HCD handle
  1899. * @retval None
  1900. */
  1901. static void HCD_Port_IRQHandler(HCD_HandleTypeDef *hhcd)
  1902. {
  1903. uint32_t FnrReg = hhcd->Instance->FNR;
  1904. uint32_t IstrReg = hhcd->Instance->ISTR;
  1905. /* SE0 detected USB Disconnected state */
  1906. if ((FnrReg & (USB_FNR_RXDP | USB_FNR_RXDM)) == 0U)
  1907. {
  1908. /* Host Port State */
  1909. hhcd->HostState = HCD_HCD_STATE_DISCONNECTED;
  1910. /* clear all allocated virtual channel */
  1911. HAL_HCD_ClearPhyChannel(hhcd);
  1912. /* Reset the PMA current pointer */
  1913. (void)HAL_HCD_PMAReset(hhcd);
  1914. /* reset Ep0 Pma allocation state */
  1915. hhcd->ep0_PmaAllocState = 0U;
  1916. /* Disconnection Callback */
  1917. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1918. hhcd->DisconnectCallback(hhcd);
  1919. #else
  1920. HAL_HCD_Disconnect_Callback(hhcd);
  1921. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1922. return;
  1923. }
  1924. if ((hhcd->HostState == HCD_HCD_STATE_DISCONNECTED) != 0U)
  1925. {
  1926. /* J-state or K-state detected & LastState=Disconnected */
  1927. if (((FnrReg & USB_FNR_RXDP) != 0U) || ((IstrReg & USB_ISTR_LS_DCONN) != 0U))
  1928. {
  1929. hhcd->HostState = HCD_HCD_STATE_CONNECTED;
  1930. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1931. hhcd->ConnectCallback(hhcd);
  1932. #else
  1933. HAL_HCD_Connect_Callback(hhcd);
  1934. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1935. }
  1936. }
  1937. else
  1938. {
  1939. /* J-state or K-state detected & lastState=Connected: a Missed disconnection is detected */
  1940. if (((FnrReg & USB_FNR_RXDP) != 0U) || ((IstrReg & USB_ISTR_LS_DCONN) != 0U))
  1941. {
  1942. /* Host Port State */
  1943. hhcd->HostState = HCD_HCD_STATE_DISCONNECTED;
  1944. /* clear all allocated virtual channel */
  1945. HAL_HCD_ClearPhyChannel(hhcd);
  1946. /* Reset the PMA current pointer */
  1947. (void)HAL_HCD_PMAReset(hhcd);
  1948. /* reset Ep0 PMA allocation state */
  1949. hhcd->ep0_PmaAllocState = 0U;
  1950. /* Disconnection Callback */
  1951. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1952. hhcd->DisconnectCallback(hhcd);
  1953. #else
  1954. HAL_HCD_Disconnect_Callback(hhcd);
  1955. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1956. }
  1957. }
  1958. }
  1959. /**
  1960. * @brief Check if the ch_num are already reserved to a physical channel
  1961. * @param hhcd HCD handle
  1962. * @param ch_num Channel number
  1963. * This parameter can be a value from 1 to 15
  1964. * @retval HAL status
  1965. */
  1966. static uint8_t HAL_HCD_Check_usedChannel(HCD_HandleTypeDef const *hhcd, uint8_t ch_num)
  1967. {
  1968. uint8_t idx;
  1969. /* Check if the logical channel are already opened */
  1970. for (idx = 0U; idx < hhcd->Init.Host_channels; idx++)
  1971. {
  1972. if ((((hhcd->phy_chin_state[idx] & 0xF0U) >> 4U) == ((uint16_t)ch_num + 1U)) &&
  1973. (hhcd->phy_chin_state[idx] != 0U))
  1974. {
  1975. return (1U | (idx << 4U));
  1976. }
  1977. if ((((hhcd->phy_chout_state[idx] & 0xF0U) >> 4U) == ((uint16_t)ch_num + 1U)) &&
  1978. (hhcd->phy_chout_state[idx] != 0U))
  1979. {
  1980. return (1U | (idx << 4U));
  1981. }
  1982. }
  1983. return 0U;
  1984. }
  1985. /**
  1986. * @brief Get a Logical Channel number from physical Channel
  1987. * @param hhcd HCD handle
  1988. * @param phy_chnum
  1989. * This parameter can be a value from 1 to 15
  1990. * @param dir Channel direction
  1991. * -0 OUT_Channel
  1992. * -1 IN_Channel
  1993. * @retval HAL status
  1994. */
  1995. static uint8_t HAL_HCD_GetLogical_Channel(HCD_HandleTypeDef const *hhcd,
  1996. uint8_t phy_chnum, uint8_t dir)
  1997. {
  1998. /* Out Channel Direction */
  1999. if (dir == 0U)
  2000. {
  2001. if (((hhcd->phy_chout_state[phy_chnum & 0x7U] & 0x00F0U) >> 4U) != 0U)
  2002. {
  2003. return ((uint8_t)((hhcd->phy_chout_state[phy_chnum & 0x7U] & 0x00F0U) >> 4U) - 1U);
  2004. }
  2005. else
  2006. {
  2007. /* Channel not registered Error */
  2008. return HCD_LOGICAL_CH_NOT_OPENED;
  2009. }
  2010. }
  2011. /* IN Channel Direction */
  2012. else
  2013. {
  2014. if (((hhcd->phy_chin_state[phy_chnum & 0x7U] & 0x00F0U) >> 4U) != 0U)
  2015. {
  2016. return ((uint8_t)((hhcd->phy_chin_state[phy_chnum & 0x7U] & 0x00F0U) >> 4U) - 1U);
  2017. }
  2018. else
  2019. {
  2020. /* Channel not registered Error */
  2021. return HCD_LOGICAL_CH_NOT_OPENED;
  2022. }
  2023. }
  2024. }
  2025. /**
  2026. * @brief Get a free physical Channel number according to the direction
  2027. * @param hhcd HCD handle
  2028. * @param ch_num Channel number
  2029. * This parameter can be a value from 1 to 15
  2030. * @param epnum Endpoint number
  2031. * This parameter can be a value from 1 to 15
  2032. * @param ep_type Endpoint Type
  2033. * This parameter can be one of these values:
  2034. * EP_TYPE_CTRL Control type,
  2035. * EP_TYPE_ISOC Isochronous type,
  2036. * EP_TYPE_BULK Bulk type,
  2037. * EP_TYPE_INTR Interrupt type
  2038. * @retval if physical channel is available return Phy_channel number
  2039. else return HCD_FREE_CH_NOT_FOUND
  2040. */
  2041. static uint8_t HAL_HCD_Get_FreePhyChannel(HCD_HandleTypeDef *hhcd, uint8_t ch_num,
  2042. uint8_t epnum, uint8_t ep_type)
  2043. {
  2044. uint8_t idx;
  2045. if ((epnum & 0x7FU) == 0U)
  2046. {
  2047. idx = 0U;
  2048. if (ch_num == 0U)
  2049. {
  2050. if (hhcd->phy_chin_state[idx] == 0U)
  2051. {
  2052. /* chin_state to store the ep_type to be used for the same channel in OUT direction
  2053. * adding + 1 to ep_type avoid starting with a 0 value. ep_type take by default (0/1/2/3) */
  2054. hhcd->phy_chin_state[idx] = (((uint16_t)ch_num + 1U) << 4U) |
  2055. ((uint16_t)ep_type + 1U) |
  2056. (((uint16_t)epnum & 0x0FU) << 8U);
  2057. }
  2058. if (hhcd->phy_chout_state[idx] == 0U)
  2059. {
  2060. /* chout_state will store the ep_type to be used for the same channel in IN direction
  2061. * adding + 1 to ep_type avoid starting with a 0 value. ep_type take by default (0/1/2/3) */
  2062. hhcd->phy_chout_state[idx] = (((uint16_t)ch_num + 1U) << 4U) |
  2063. ((uint16_t)ep_type + 1U) |
  2064. (((uint16_t)epnum & 0x0FU) << 8U);
  2065. }
  2066. }
  2067. else
  2068. {
  2069. if ((epnum & 0x80U) != 0U)
  2070. {
  2071. if (((hhcd->phy_chin_state[idx] & 0xF0U) >> 4U) != ((uint16_t)ch_num + 1U))
  2072. {
  2073. /* chin_state to store the ep_type to be used for the same channel in OUT direction
  2074. * adding + 1 to ep_type avoid starting with a 0 value. ep_type take by default (0/1/2/3) */
  2075. hhcd->phy_chin_state[idx] = (((uint16_t)ch_num + 1U) << 4U) |
  2076. ((uint16_t)ep_type + 1U) |
  2077. (((uint16_t)epnum & 0x0FU) << 8U);
  2078. }
  2079. }
  2080. else
  2081. {
  2082. if (((hhcd->phy_chout_state[idx] & 0xF0U) >> 4U) != ((uint16_t)ch_num + 1U))
  2083. {
  2084. /* chout_state will store the ep_type to be used for the same channel in IN direction
  2085. * adding + 1 to ep_type avoid starting with a 0 value. ep_type take by default (0/1/2/3) */
  2086. hhcd->phy_chout_state[idx] = (((uint16_t)ch_num + 1U) << 4U) |
  2087. ((uint16_t)ep_type + 1U) |
  2088. (((uint16_t)epnum & 0x0FU) << 8U);
  2089. }
  2090. }
  2091. }
  2092. return idx;
  2093. }
  2094. if ((epnum & 0x80U) != 0U)
  2095. {
  2096. /* Find a new available physical in channel */
  2097. for (idx = 1U; idx < hhcd->Init.Host_channels; idx++)
  2098. {
  2099. /* Check if the same epnum is allocated then allocate the same physical channel OUT for IN Logical Channel */
  2100. if ((hhcd->phy_chin_state[idx] == 0U) &&
  2101. ((((hhcd->phy_chout_state[idx] & 0x000FU) == ((uint16_t)ep_type + 1U)) &&
  2102. (((hhcd->phy_chout_state[idx] & 0x0F00U) == ((uint16_t)epnum & 0x0FU)))) ||
  2103. (hhcd->phy_chout_state[idx] == 0U)))
  2104. {
  2105. /* chin_state to store the ep_type to be used for the same channel in OUT direction
  2106. * adding + 1 to ep_type avoid starting with a 0 value. ep_type take by default (0/1/2/3) */
  2107. hhcd->phy_chin_state[idx] = (((uint16_t)ch_num + 1U) << 4U) |
  2108. ((uint16_t)ep_type + 1U) |
  2109. (((uint16_t)epnum & 0x0FU) << 8U);
  2110. return idx;
  2111. }
  2112. }
  2113. }
  2114. else
  2115. {
  2116. /* Find a new available physical out channel */
  2117. for (idx = 1U; idx < hhcd->Init.Host_channels; idx++)
  2118. {
  2119. /* Check if the same epnum is allocated then allocate the same physical channel IN for OUT Logical Channel */
  2120. if ((hhcd->phy_chout_state[idx] == 0U) &&
  2121. ((((hhcd->phy_chin_state[idx] & 0x0FU) == ((uint16_t)ep_type + 1U)) &&
  2122. ((hhcd->phy_chin_state[idx] & 0x0F00U) == ((uint16_t)epnum & 0x0FU))) ||
  2123. (hhcd->phy_chin_state[idx] == 0U)))
  2124. {
  2125. /* chout_state will store the ep_type to be used for the same channel in IN direction
  2126. * adding + 1 to ep_type avoid starting with a 0 value. ep_type take by default (0/1/2/3) */
  2127. hhcd->phy_chout_state[idx] = (((uint16_t)ch_num + 1U) << 4U) |
  2128. ((uint16_t)ep_type + 1U) |
  2129. (((uint16_t)epnum & 0x0FU) << 8U);
  2130. return idx;
  2131. }
  2132. }
  2133. }
  2134. /* in case of Error */
  2135. return HCD_FREE_CH_NOT_FOUND;
  2136. }
  2137. /**
  2138. * @brief Free All Channel allocation
  2139. * @param hhcd HCD handle
  2140. * @retval HAL status
  2141. */
  2142. static void HAL_HCD_ClearPhyChannel(HCD_HandleTypeDef *hhcd)
  2143. {
  2144. uint8_t idx;
  2145. for (idx = 0U; idx < hhcd->Init.Host_channels; idx++)
  2146. {
  2147. /*Reset channel allocation value */
  2148. hhcd->phy_chout_state[idx] = 0U;
  2149. hhcd->phy_chin_state[idx] = 0U;
  2150. }
  2151. }
  2152. /*---------------------- PMA Allocation Section --------------------- */
  2153. /*
  2154. __col31________________col0__ Column-- >
  2155. lin0 | entry31.|....... | entry0 | Line
  2156. |---------|---------|--------| |
  2157. line1| entry63.|....... | entry32| |
  2158. |---------|---------|--------| \|/
  2159. | entry127|....... | entry64|
  2160. |---------|---------|--------|
  2161. | entry256|...... |entry128|
  2162. ----------------------------
  2163. an allocation space of 64byte need 8 Free contiguous Entry in the Matrix
  2164. - a Free Entry is a bit with 0 Value/ a busy entry is a bit with 1 value. */
  2165. /**
  2166. * @brief Fetch in the PMA_LockupTable free space of number of mps byte
  2167. * @param hhcd Host instance
  2168. * @param mps Channel Max Packet Size
  2169. * @retval PMA_Address of the first free block containing mps byte
  2170. 0xFFFF in case of no space available
  2171. */
  2172. static uint16_t HAL_HCD_GetFreePMA(HCD_HandleTypeDef *hhcd, uint16_t mps)
  2173. {
  2174. uint32_t Entry;
  2175. uint32_t FreeBlocks = 0U;
  2176. uint8_t FirstFreeBlock_col = 0U;
  2177. uint8_t FirstFreeBlock_line = 0U;
  2178. uint8_t ColIndex;
  2179. uint16_t NbrReqBlocks;
  2180. uint16_t mps_t = mps;
  2181. /* since PMA buffer descriptor RXBD allocate address according to BLSIZE, BLSIZE=1==> mps>64
  2182. allocation in PMA is done in 32Bytes each entry */
  2183. if ((mps_t > 64U) && ((mps_t % 32U) != 0U))
  2184. {
  2185. /* Align the mps to 32byte block to match the allocation in PMA,
  2186. check Definition of allocation buffer memory in usb user spec */
  2187. mps_t = (uint16_t)(((mps_t / 32U) + 1U) * 32U);
  2188. }
  2189. /* calculate the number of block(8byte) to allocate */
  2190. NbrReqBlocks = mps_t / 8U;
  2191. /* check if we need remaining Block */
  2192. if ((mps_t % 8U) != 0U)
  2193. {
  2194. NbrReqBlocks++;
  2195. }
  2196. /* Look For NbrReqBlocks * Empty Block */
  2197. for (uint8_t i = 0U; ((i < PMA_BLOCKS) && (FreeBlocks != NbrReqBlocks)); i++)
  2198. {
  2199. Entry = hhcd->PMALookupTable[i];
  2200. /* when parse is in progress, check the first col to look for a contiguous block */
  2201. if ((FreeBlocks != 0U) && ((Entry & (uint32_t)1U) != 0U))
  2202. {
  2203. FreeBlocks = 0U;
  2204. }
  2205. uint8_t j = 0U;
  2206. while ((j <= 31U) && (FreeBlocks != NbrReqBlocks))
  2207. {
  2208. /* check if block j is free */
  2209. if ((Entry & ((uint32_t)1U << j)) == 0U)
  2210. {
  2211. if (FreeBlocks == 0U)
  2212. {
  2213. FirstFreeBlock_col = j;
  2214. FirstFreeBlock_line = i;
  2215. FreeBlocks++;
  2216. }
  2217. j++;
  2218. /* Parse Column PMALockTable */
  2219. while ((j <= 31U) && ((Entry & ((uint32_t)1U << j)) == 0U) && (FreeBlocks < NbrReqBlocks))
  2220. {
  2221. FreeBlocks++;
  2222. j++;
  2223. }
  2224. /* Free contiguous Blocks not found */
  2225. if (((FreeBlocks < NbrReqBlocks) && (j < 31U)) ||
  2226. ((j == 31U) && ((Entry & ((uint32_t)1U << j)) != 0U)))
  2227. {
  2228. FreeBlocks = 0U;
  2229. }
  2230. }
  2231. j++;
  2232. } /* end for j */
  2233. } /* end for i */
  2234. /* Free block found */
  2235. if (FreeBlocks >= NbrReqBlocks)
  2236. {
  2237. ColIndex = FirstFreeBlock_col;
  2238. for (uint8_t i = FirstFreeBlock_line; ((i < PMA_BLOCKS) && (FreeBlocks > 0U)); i++)
  2239. {
  2240. for (uint8_t j = ColIndex; j <= 31U; j++)
  2241. {
  2242. hhcd->PMALookupTable[i] |= ((uint32_t)1U << j);
  2243. if (--FreeBlocks == 0U)
  2244. {
  2245. break;
  2246. }
  2247. }
  2248. ColIndex = 0U;
  2249. }
  2250. return (uint16_t)((FirstFreeBlock_line * (uint16_t)256U) + (FirstFreeBlock_col * (uint16_t)8U));
  2251. }
  2252. else
  2253. {
  2254. return 0xFFFFU;
  2255. }
  2256. }
  2257. /**
  2258. * @brief Allocate PMA buffer for Channel
  2259. * This API will fetch a free space
  2260. * @param hhcd Host instance
  2261. * @param ch_num Channel number
  2262. * @param ch_kind endpoint Kind
  2263. * USB_SNG_BUF Single Buffer used
  2264. * USB_DBL_BUF Double Buffer used
  2265. * @param mps Channel Max Packet Size
  2266. * @retval HAL status
  2267. */
  2268. HAL_StatusTypeDef HAL_HCD_PMAlloc(HCD_HandleTypeDef *hhcd, uint8_t ch_num,
  2269. uint16_t ch_kind, uint16_t mps)
  2270. {
  2271. uint16_t pma_addr0;
  2272. #if (USE_USB_DOUBLE_BUFFER == 1U)
  2273. uint16_t pma_addr1; /* used for double buffer mode if enabled */
  2274. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  2275. /* Host Channel */
  2276. HCD_HCTypeDef *hc = &(hhcd->hc[ch_num]);
  2277. /* Get a FreePMA Address */
  2278. pma_addr0 = HAL_HCD_GetFreePMA(hhcd, mps);
  2279. /* if there is no free space to allocate */
  2280. if (pma_addr0 == 0xFFFFU)
  2281. {
  2282. return HAL_ERROR;
  2283. }
  2284. else
  2285. {
  2286. /* Here we check if the endpoint is single or double Buffer */
  2287. if (ch_kind == HCD_SNG_BUF)
  2288. {
  2289. /* Single Buffer */
  2290. hc->doublebuffer = 0U;
  2291. if (hc->ep_num == 0U)
  2292. {
  2293. hhcd->ep0_PmaAllocState = ch_num;
  2294. hhcd->ep0_PmaAllocState |= (1U << 8);
  2295. }
  2296. /* Configure the PMA */
  2297. if (hc->ch_dir == CH_IN_DIR)
  2298. {
  2299. hc->pmaaddr1 = pma_addr0;
  2300. (USB_DRD_PMA_BUFF + hc->phy_ch_num)->RXBD = hc->pmaaddr1;
  2301. if (hc->ep_num == 0U)
  2302. {
  2303. hhcd->ep0_PmaAllocState |= (CH_IN_DIR << 4);
  2304. }
  2305. }
  2306. else
  2307. {
  2308. hc->pmaaddr0 = pma_addr0;
  2309. (USB_DRD_PMA_BUFF + hc->phy_ch_num)->TXBD = hc->pmaaddr0;
  2310. }
  2311. /* Set the PmaAddress */
  2312. hc->pmaadress = pma_addr0;
  2313. }
  2314. #if (USE_USB_DOUBLE_BUFFER == 1U)
  2315. else /* USB_DBL_BUF */
  2316. {
  2317. /* Double Buffer Endpoint */
  2318. hc->doublebuffer = 1U;
  2319. /* Get a FreePMA Address for buffer 2 */
  2320. pma_addr1 = HAL_HCD_GetFreePMA(hhcd, mps);
  2321. if (pma_addr1 == 0xFFFFU)
  2322. {
  2323. /* Free the first buffer */
  2324. (void)HAL_HCD_PMAFree(hhcd, pma_addr0, mps);
  2325. return HAL_ERROR;
  2326. }
  2327. else
  2328. {
  2329. /* Configure the PMA */
  2330. hc->pmaaddr0 = (uint16_t)(pma_addr0);
  2331. hc->pmaaddr1 = (uint16_t)(pma_addr1);
  2332. /* Set Buffer0 pma address */
  2333. (USB_DRD_PMA_BUFF + hc->phy_ch_num)->TXBD = pma_addr0;
  2334. /* Set Buffer1 pma address */
  2335. (USB_DRD_PMA_BUFF + hc->phy_ch_num)->RXBD = pma_addr1;
  2336. /* Used for Bulk DB MPS < 64bytes */
  2337. if (hc->ch_dir == CH_IN_DIR)
  2338. {
  2339. hc->pmaadress = hc->pmaaddr1;
  2340. }
  2341. else
  2342. {
  2343. hc->pmaadress = hc->pmaaddr0;
  2344. }
  2345. }
  2346. }
  2347. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  2348. }
  2349. return HAL_OK;
  2350. }
  2351. /**
  2352. * @brief PMA De-Allocation for Channel Free the reserved block in the PMA-LookupTable
  2353. * @param hhcd Host instance
  2354. * @param ch_num Channel number
  2355. * @retval HAL status
  2356. */
  2357. HAL_StatusTypeDef HAL_HCD_PMADeAlloc(HCD_HandleTypeDef *hhcd, uint8_t ch_num)
  2358. {
  2359. HAL_StatusTypeDef status;
  2360. #if (USE_USB_DOUBLE_BUFFER == 1U)
  2361. uint8_t Err = 0U;
  2362. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  2363. /* Host Channel */
  2364. HCD_HCTypeDef *hc = &(hhcd->hc[ch_num]);
  2365. /* Single Buffer */
  2366. if (hc->doublebuffer == 0U)
  2367. {
  2368. status = HAL_HCD_PMAFree(hhcd, hc->pmaadress, hc->max_packet);
  2369. }
  2370. else /* Double buffer */
  2371. {
  2372. #if (USE_USB_DOUBLE_BUFFER == 1U)
  2373. status = HAL_HCD_PMAFree(hhcd, hc->pmaaddr0, hc->max_packet);
  2374. if (status != HAL_OK)
  2375. {
  2376. Err++;
  2377. }
  2378. status = HAL_HCD_PMAFree(hhcd, hc->pmaaddr1, hc->max_packet);
  2379. if (status != HAL_OK)
  2380. {
  2381. Err++;
  2382. }
  2383. if (Err != 0U)
  2384. {
  2385. return HAL_ERROR;
  2386. }
  2387. #else
  2388. status = HAL_ERROR;
  2389. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  2390. }
  2391. return status;
  2392. }
  2393. /**
  2394. * @brief PMA Reset
  2395. * @param hhcd Host instance
  2396. * @retval HAL status
  2397. */
  2398. HAL_StatusTypeDef HAL_HCD_PMAReset(HCD_HandleTypeDef *hhcd)
  2399. {
  2400. /* Reset All PMA Entry */
  2401. for (uint8_t i = 0U; i < PMA_BLOCKS; i++)
  2402. {
  2403. hhcd->PMALookupTable[i] = 0U;
  2404. }
  2405. /* Allocate a Space for buffer descriptor table depending on the Host channel number */
  2406. for (uint8_t i = 0U; i < hhcd->Init.Host_channels; i++)
  2407. {
  2408. hhcd->PMALookupTable[0] |= ((uint32_t)1U << i);
  2409. }
  2410. return HAL_OK;
  2411. }
  2412. /**
  2413. * @brief PMA Free
  2414. * @param hhcd Host instance
  2415. * @param pma_base PMA base offset stored in hhcd->hc.pmaaddr
  2416. * @param mps Max Packet Size
  2417. * @retval HAL status
  2418. */
  2419. static HAL_StatusTypeDef HAL_HCD_PMAFree(HCD_HandleTypeDef *hhcd, uint32_t pma_base, uint16_t mps)
  2420. {
  2421. uint32_t block_nbr;
  2422. uint8_t ColIndex;
  2423. uint8_t LineIndex;
  2424. uint16_t mps_t = mps;
  2425. /* since PMA buffer descriptor RXBD allocate address according to BLSIZE, BLSIZE=1==> mps>64
  2426. allocation in PMA is done in 32Bytes each entry */
  2427. if ((mps_t > 64U) && ((mps_t % 32U) != 0U))
  2428. {
  2429. /* Align the mps to 32byte block to match the allocation in PMA,
  2430. check Definition of allocation buffer memory in usb user spec */
  2431. mps_t = (uint16_t)(((mps_t / 32U) + 1U) * 32U);
  2432. }
  2433. /* Calculate the number of needed block to Free */
  2434. if ((mps_t / 8U) != 0U)
  2435. {
  2436. block_nbr = ((uint32_t)mps_t / 8U);
  2437. if ((mps_t % 8U) != 0U)
  2438. {
  2439. block_nbr++;
  2440. }
  2441. }
  2442. else
  2443. {
  2444. block_nbr = 1U;
  2445. }
  2446. /* Decode Col/Line of PMA_Base position in the PMA_LookupTable */
  2447. if (pma_base > 256U)
  2448. {
  2449. LineIndex = (uint8_t)(pma_base / 256U);
  2450. ColIndex = (uint8_t)((pma_base - ((uint32_t)LineIndex * 256U)) / 8U);
  2451. }
  2452. else
  2453. {
  2454. LineIndex = 0U;
  2455. ColIndex = (uint8_t)(pma_base / 8U);
  2456. }
  2457. /* Reset the corresponding bit in the lookupTable */
  2458. for (uint8_t i = LineIndex; ((i < PMA_BLOCKS) && (block_nbr > 0U)); i++)
  2459. {
  2460. for (uint8_t j = ColIndex; j <= 31U; j++)
  2461. {
  2462. /* Check if the block is not already reserved or it was already closed */
  2463. if ((hhcd->PMALookupTable[i] & ((uint32_t)1U << j)) == 0U)
  2464. {
  2465. return HAL_ERROR;
  2466. }
  2467. /* Free the reserved block by resetting the corresponding bit */
  2468. hhcd->PMALookupTable[i] &= ~(1U << j);
  2469. if (--block_nbr == 0U)
  2470. {
  2471. break;
  2472. }
  2473. }
  2474. ColIndex = 0U;
  2475. }
  2476. return HAL_OK;
  2477. }
  2478. /**
  2479. * @}
  2480. */
  2481. /**
  2482. * @}
  2483. */
  2484. #endif /* defined (USB_DRD_FS) */
  2485. #endif /* HAL_HCD_MODULE_ENABLED */
  2486. /**
  2487. * @}
  2488. */
  2489. /**
  2490. * @}
  2491. */