stm32h7xx_hal_eth.c 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_eth.c
  4. * @author MCD Application Team
  5. * @brief ETH HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Ethernet (ETH) peripheral:
  8. * + Initialization and deinitialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State and Errors functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2017 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. The ETH HAL driver can be used as follows:
  30. (#)Declare a ETH_HandleTypeDef handle structure, for example:
  31. ETH_HandleTypeDef heth;
  32. (#)Fill parameters of Init structure in heth handle
  33. (#)Call HAL_ETH_Init() API to initialize the Ethernet peripheral (MAC, DMA, ...)
  34. (#)Initialize the ETH low level resources through the HAL_ETH_MspInit() API:
  35. (##) Enable the Ethernet interface clock using
  36. (+++) __HAL_RCC_ETH1MAC_CLK_ENABLE()
  37. (+++) __HAL_RCC_ETH1TX_CLK_ENABLE()
  38. (+++) __HAL_RCC_ETH1RX_CLK_ENABLE()
  39. (##) Initialize the related GPIO clocks
  40. (##) Configure Ethernet pinout
  41. (##) Configure Ethernet NVIC interrupt (in Interrupt mode)
  42. (#) Ethernet data reception is asynchronous, so call the following API
  43. to start the listening mode:
  44. (##) HAL_ETH_Start():
  45. This API starts the MAC and DMA transmission and reception process,
  46. without enabling end of transfer interrupts, in this mode user
  47. has to poll for data reception by calling HAL_ETH_ReadData()
  48. (##) HAL_ETH_Start_IT():
  49. This API starts the MAC and DMA transmission and reception process,
  50. end of transfer interrupts are enabled in this mode,
  51. HAL_ETH_RxCpltCallback() will be executed when an Ethernet packet is received
  52. (#) When data is received user can call the following API to get received data:
  53. (##) HAL_ETH_ReadData(): Read a received packet
  54. (#) For transmission path, two APIs are available:
  55. (##) HAL_ETH_Transmit(): Transmit an ETH frame in blocking mode
  56. (##) HAL_ETH_Transmit_IT(): Transmit an ETH frame in interrupt mode,
  57. HAL_ETH_TxCpltCallback() will be executed when end of transfer occur
  58. (#) Communication with an external PHY device:
  59. (##) HAL_ETH_ReadPHYRegister(): Read a register from an external PHY
  60. (##) HAL_ETH_WritePHYRegister(): Write data to an external RHY register
  61. (#) Configure the Ethernet MAC after ETH peripheral initialization
  62. (##) HAL_ETH_GetMACConfig(): Get MAC actual configuration into ETH_MACConfigTypeDef
  63. (##) HAL_ETH_SetMACConfig(): Set MAC configuration based on ETH_MACConfigTypeDef
  64. (#) Configure the Ethernet DMA after ETH peripheral initialization
  65. (##) HAL_ETH_GetDMAConfig(): Get DMA actual configuration into ETH_DMAConfigTypeDef
  66. (##) HAL_ETH_SetDMAConfig(): Set DMA configuration based on ETH_DMAConfigTypeDef
  67. (#) Configure the Ethernet PTP after ETH peripheral initialization
  68. (##) Define HAL_ETH_USE_PTP to use PTP APIs.
  69. (##) HAL_ETH_PTP_GetConfig(): Get PTP actual configuration into ETH_PTP_ConfigTypeDef
  70. (##) HAL_ETH_PTP_SetConfig(): Set PTP configuration based on ETH_PTP_ConfigTypeDef
  71. (##) HAL_ETH_PTP_GetTime(): Get Seconds and Nanoseconds for the Ethernet PTP registers
  72. (##) HAL_ETH_PTP_SetTime(): Set Seconds and Nanoseconds for the Ethernet PTP registers
  73. (##) HAL_ETH_PTP_AddTimeOffset(): Add Seconds and Nanoseconds offset for the Ethernet PTP registers
  74. (##) HAL_ETH_PTP_InsertTxTimestamp(): Insert Timestamp in transmission
  75. (##) HAL_ETH_PTP_GetTxTimestamp(): Get transmission timestamp
  76. (##) HAL_ETH_PTP_GetRxTimestamp(): Get reception timestamp
  77. -@- The ARP offload feature is not supported in this driver.
  78. -@- The PTP offload feature is not supported in this driver.
  79. *** Callback registration ***
  80. =============================================
  81. The compilation define USE_HAL_ETH_REGISTER_CALLBACKS when set to 1
  82. allows the user to configure dynamically the driver callbacks.
  83. Use Function HAL_ETH_RegisterCallback() to register an interrupt callback.
  84. Function HAL_ETH_RegisterCallback() allows to register following callbacks:
  85. (+) TxCpltCallback : Tx Complete Callback.
  86. (+) RxCpltCallback : Rx Complete Callback.
  87. (+) ErrorCallback : Error Callback.
  88. (+) PMTCallback : Power Management Callback
  89. (+) EEECallback : EEE Callback.
  90. (+) WakeUpCallback : Wake UP Callback
  91. (+) MspInitCallback : MspInit Callback.
  92. (+) MspDeInitCallback: MspDeInit Callback.
  93. This function takes as parameters the HAL peripheral handle, the Callback ID
  94. and a pointer to the user callback function.
  95. For specific callbacks RxAllocateCallback use dedicated register callbacks:
  96. respectively HAL_ETH_RegisterRxAllocateCallback().
  97. For specific callbacks RxLinkCallback use dedicated register callbacks:
  98. respectively HAL_ETH_RegisterRxLinkCallback().
  99. For specific callbacks TxFreeCallback use dedicated register callbacks:
  100. respectively HAL_ETH_RegisterTxFreeCallback().
  101. For specific callbacks TxPtpCallback use dedicated register callbacks:
  102. respectively HAL_ETH_RegisterTxPtpCallback().
  103. Use function HAL_ETH_UnRegisterCallback() to reset a callback to the default
  104. weak function.
  105. HAL_ETH_UnRegisterCallback takes as parameters the HAL peripheral handle,
  106. and the Callback ID.
  107. This function allows to reset following callbacks:
  108. (+) TxCpltCallback : Tx Complete Callback.
  109. (+) RxCpltCallback : Rx Complete Callback.
  110. (+) ErrorCallback : Error Callback.
  111. (+) PMTCallback : Power Management Callback
  112. (+) EEECallback : EEE Callback.
  113. (+) WakeUpCallback : Wake UP Callback
  114. (+) MspInitCallback : MspInit Callback.
  115. (+) MspDeInitCallback: MspDeInit Callback.
  116. For specific callbacks RxAllocateCallback use dedicated unregister callbacks:
  117. respectively HAL_ETH_UnRegisterRxAllocateCallback().
  118. For specific callbacks RxLinkCallback use dedicated unregister callbacks:
  119. respectively HAL_ETH_UnRegisterRxLinkCallback().
  120. For specific callbacks TxFreeCallback use dedicated unregister callbacks:
  121. respectively HAL_ETH_UnRegisterTxFreeCallback().
  122. For specific callbacks TxPtpCallback use dedicated unregister callbacks:
  123. respectively HAL_ETH_UnRegisterTxPtpCallback().
  124. By default, after the HAL_ETH_Init and when the state is HAL_ETH_STATE_RESET
  125. all callbacks are set to the corresponding weak functions:
  126. examples HAL_ETH_TxCpltCallback(), HAL_ETH_RxCpltCallback().
  127. Exception done for MspInit and MspDeInit functions that are
  128. reset to the legacy weak function in the HAL_ETH_Init/ HAL_ETH_DeInit only when
  129. these callbacks are null (not registered beforehand).
  130. if not, MspInit or MspDeInit are not null, the HAL_ETH_Init/ HAL_ETH_DeInit
  131. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  132. Callbacks can be registered/unregistered in HAL_ETH_STATE_READY state only.
  133. Exception done MspInit/MspDeInit that can be registered/unregistered
  134. in HAL_ETH_STATE_READY or HAL_ETH_STATE_RESET state,
  135. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  136. In that case first register the MspInit/MspDeInit user callbacks
  137. using HAL_ETH_RegisterCallback() before calling HAL_ETH_DeInit
  138. or HAL_ETH_Init function.
  139. When The compilation define USE_HAL_ETH_REGISTER_CALLBACKS is set to 0 or
  140. not defined, the callback registration feature is not available and all callbacks
  141. are set to the corresponding weak functions.
  142. @endverbatim
  143. ******************************************************************************
  144. */
  145. /* Includes ------------------------------------------------------------------*/
  146. #include "stm32h7xx_hal.h"
  147. /** @addtogroup STM32H7xx_HAL_Driver
  148. * @{
  149. */
  150. #ifdef HAL_ETH_MODULE_ENABLED
  151. #if defined(ETH)
  152. /** @defgroup ETH ETH
  153. * @brief ETH HAL module driver
  154. * @{
  155. */
  156. /* Private typedef -----------------------------------------------------------*/
  157. /* Private define ------------------------------------------------------------*/
  158. /** @addtogroup ETH_Private_Constants ETH Private Constants
  159. * @{
  160. */
  161. #define ETH_MACCR_MASK ((uint32_t)0xFFFB7F7CU)
  162. #define ETH_MACECR_MASK ((uint32_t)0x3F077FFFU)
  163. #define ETH_MACPFR_MASK ((uint32_t)0x800007FFU)
  164. #define ETH_MACWTR_MASK ((uint32_t)0x0000010FU)
  165. #define ETH_MACTFCR_MASK ((uint32_t)0xFFFF00F2U)
  166. #define ETH_MACRFCR_MASK ((uint32_t)0x00000003U)
  167. #define ETH_MTLTQOMR_MASK ((uint32_t)0x00000072U)
  168. #define ETH_MTLRQOMR_MASK ((uint32_t)0x0000007BU)
  169. #define ETH_DMAMR_MASK ((uint32_t)0x00007802U)
  170. #define ETH_DMASBMR_MASK ((uint32_t)0x0000D001U)
  171. #define ETH_DMACCR_MASK ((uint32_t)0x00013FFFU)
  172. #define ETH_DMACTCR_MASK ((uint32_t)0x003F1010U)
  173. #define ETH_DMACRCR_MASK ((uint32_t)0x803F0000U)
  174. #define ETH_MACPCSR_MASK (ETH_MACPCSR_PWRDWN | ETH_MACPCSR_RWKPKTEN | \
  175. ETH_MACPCSR_MGKPKTEN | ETH_MACPCSR_GLBLUCAST | \
  176. ETH_MACPCSR_RWKPFE)
  177. /* Timeout values */
  178. #define ETH_DMARXNDESCWBF_ERRORS_MASK ((uint32_t)(ETH_DMARXNDESCWBF_DE | ETH_DMARXNDESCWBF_RE | \
  179. ETH_DMARXNDESCWBF_OE | ETH_DMARXNDESCWBF_RWT |\
  180. ETH_DMARXNDESCWBF_GP | ETH_DMARXNDESCWBF_CE))
  181. #define ETH_MACTSCR_MASK ((uint32_t)0x0087FF2FU)
  182. #define ETH_MACSTSUR_VALUE ((uint32_t)0xFFFFFFFFU)
  183. #define ETH_MACSTNUR_VALUE ((uint32_t)0xBB9ACA00U)
  184. #define ETH_SEGMENT_SIZE_DEFAULT ((uint32_t)0x218U)
  185. /**
  186. * @}
  187. */
  188. /* Private macros ------------------------------------------------------------*/
  189. /** @defgroup ETH_Private_Macros ETH Private Macros
  190. * @{
  191. */
  192. /* Helper macros for TX descriptor handling */
  193. #define INCR_TX_DESC_INDEX(inx, offset) do {\
  194. (inx) += (offset);\
  195. if ((inx) >= (uint32_t)ETH_TX_DESC_CNT){\
  196. (inx) = ((inx) - (uint32_t)ETH_TX_DESC_CNT);}\
  197. } while (0)
  198. /* Helper macros for RX descriptor handling */
  199. #define INCR_RX_DESC_INDEX(inx, offset) do {\
  200. (inx) += (offset);\
  201. if ((inx) >= (uint32_t)ETH_RX_DESC_CNT){\
  202. (inx) = ((inx) - (uint32_t)ETH_RX_DESC_CNT);}\
  203. } while (0)
  204. /**
  205. * @}
  206. */
  207. /* Private function prototypes -----------------------------------------------*/
  208. /** @defgroup ETH_Private_Functions ETH Private Functions
  209. * @{
  210. */
  211. static void ETH_SetMACConfig(ETH_HandleTypeDef *heth, ETH_MACConfigTypeDef *macconf);
  212. static void ETH_SetDMAConfig(ETH_HandleTypeDef *heth, ETH_DMAConfigTypeDef *dmaconf);
  213. static void ETH_MACDMAConfig(ETH_HandleTypeDef *heth);
  214. static void ETH_DMATxDescListInit(ETH_HandleTypeDef *heth);
  215. static void ETH_DMARxDescListInit(ETH_HandleTypeDef *heth);
  216. static uint32_t ETH_Prepare_Tx_Descriptors(ETH_HandleTypeDef *heth, ETH_TxPacketConfig *pTxConfig, uint32_t ItMode);
  217. static void ETH_UpdateDescriptor(ETH_HandleTypeDef *heth);
  218. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  219. static void ETH_InitCallbacksToDefault(ETH_HandleTypeDef *heth);
  220. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  221. /**
  222. * @}
  223. */
  224. /* Exported functions ---------------------------------------------------------*/
  225. /** @defgroup ETH_Exported_Functions ETH Exported Functions
  226. * @{
  227. */
  228. /** @defgroup ETH_Exported_Functions_Group1 Initialization and deinitialization functions
  229. * @brief Initialization and Configuration functions
  230. *
  231. @verbatim
  232. ===============================================================================
  233. ##### Initialization and Configuration functions #####
  234. ===============================================================================
  235. [..] This subsection provides a set of functions allowing to initialize and
  236. deinitialize the ETH peripheral:
  237. (+) User must Implement HAL_ETH_MspInit() function in which he configures
  238. all related peripherals resources (CLOCK, GPIO and NVIC ).
  239. (+) Call the function HAL_ETH_Init() to configure the selected device with
  240. the selected configuration:
  241. (++) MAC address
  242. (++) Media interface (MII or RMII)
  243. (++) Rx DMA Descriptors Tab
  244. (++) Tx DMA Descriptors Tab
  245. (++) Length of Rx Buffers
  246. (+) Call the function HAL_ETH_DeInit() to restore the default configuration
  247. of the selected ETH peripheral.
  248. @endverbatim
  249. * @{
  250. */
  251. /**
  252. * @brief Initialize the Ethernet peripheral registers.
  253. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  254. * the configuration information for ETHERNET module
  255. * @retval HAL status
  256. */
  257. HAL_StatusTypeDef HAL_ETH_Init(ETH_HandleTypeDef *heth)
  258. {
  259. uint32_t tickstart;
  260. if (heth == NULL)
  261. {
  262. return HAL_ERROR;
  263. }
  264. if (heth->gState == HAL_ETH_STATE_RESET)
  265. {
  266. heth->gState = HAL_ETH_STATE_BUSY;
  267. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  268. ETH_InitCallbacksToDefault(heth);
  269. if (heth->MspInitCallback == NULL)
  270. {
  271. heth->MspInitCallback = HAL_ETH_MspInit;
  272. }
  273. /* Init the low level hardware */
  274. heth->MspInitCallback(heth);
  275. #else
  276. /* Init the low level hardware : GPIO, CLOCK, NVIC. */
  277. HAL_ETH_MspInit(heth);
  278. #endif /* (USE_HAL_ETH_REGISTER_CALLBACKS) */
  279. }
  280. __HAL_RCC_SYSCFG_CLK_ENABLE();
  281. if (heth->Init.MediaInterface == HAL_ETH_MII_MODE)
  282. {
  283. HAL_SYSCFG_ETHInterfaceSelect(SYSCFG_ETH_MII);
  284. }
  285. else
  286. {
  287. HAL_SYSCFG_ETHInterfaceSelect(SYSCFG_ETH_RMII);
  288. }
  289. /* Dummy read to sync with ETH */
  290. (void)SYSCFG->PMCR;
  291. /* Ethernet Software reset */
  292. /* Set the SWR bit: resets all MAC subsystem internal registers and logic */
  293. /* After reset all the registers holds their respective reset values */
  294. SET_BIT(heth->Instance->DMAMR, ETH_DMAMR_SWR);
  295. /* Get tick */
  296. tickstart = HAL_GetTick();
  297. /* Wait for software reset */
  298. while (READ_BIT(heth->Instance->DMAMR, ETH_DMAMR_SWR) > 0U)
  299. {
  300. if (((HAL_GetTick() - tickstart) > ETH_SWRESET_TIMEOUT))
  301. {
  302. /* Set Error Code */
  303. heth->ErrorCode = HAL_ETH_ERROR_TIMEOUT;
  304. /* Set State as Error */
  305. heth->gState = HAL_ETH_STATE_ERROR;
  306. /* Return Error */
  307. return HAL_ERROR;
  308. }
  309. }
  310. /*------------------ MDIO CSR Clock Range Configuration --------------------*/
  311. HAL_ETH_SetMDIOClockRange(heth);
  312. /*------------------ MAC LPI 1US Tic Counter Configuration --------------------*/
  313. WRITE_REG(heth->Instance->MAC1USTCR, (((uint32_t)HAL_RCC_GetHCLKFreq() / ETH_MAC_US_TICK) - 1U));
  314. /*------------------ MAC, MTL and DMA default Configuration ----------------*/
  315. ETH_MACDMAConfig(heth);
  316. /* SET DSL to 64 bit */
  317. MODIFY_REG(heth->Instance->DMACCR, ETH_DMACCR_DSL, ETH_DMACCR_DSL_64BIT);
  318. /* Set Receive Buffers Length (must be a multiple of 4) */
  319. if ((heth->Init.RxBuffLen % 0x4U) != 0x0U)
  320. {
  321. /* Set Error Code */
  322. heth->ErrorCode = HAL_ETH_ERROR_PARAM;
  323. /* Set State as Error */
  324. heth->gState = HAL_ETH_STATE_ERROR;
  325. /* Return Error */
  326. return HAL_ERROR;
  327. }
  328. else
  329. {
  330. MODIFY_REG(heth->Instance->DMACRCR, ETH_DMACRCR_RBSZ, ((heth->Init.RxBuffLen) << 1));
  331. }
  332. /*------------------ DMA Tx Descriptors Configuration ----------------------*/
  333. ETH_DMATxDescListInit(heth);
  334. /*------------------ DMA Rx Descriptors Configuration ----------------------*/
  335. ETH_DMARxDescListInit(heth);
  336. /*--------------------- ETHERNET MAC Address Configuration ------------------*/
  337. /* Set MAC addr bits 32 to 47 */
  338. heth->Instance->MACA0HR = (((uint32_t)(heth->Init.MACAddr[5]) << 8) | (uint32_t)heth->Init.MACAddr[4]);
  339. /* Set MAC addr bits 0 to 31 */
  340. heth->Instance->MACA0LR = (((uint32_t)(heth->Init.MACAddr[3]) << 24) | ((uint32_t)(heth->Init.MACAddr[2]) << 16) |
  341. ((uint32_t)(heth->Init.MACAddr[1]) << 8) | (uint32_t)heth->Init.MACAddr[0]);
  342. heth->ErrorCode = HAL_ETH_ERROR_NONE;
  343. heth->gState = HAL_ETH_STATE_READY;
  344. return HAL_OK;
  345. }
  346. /**
  347. * @brief DeInitializes the ETH peripheral.
  348. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  349. * the configuration information for ETHERNET module
  350. * @retval HAL status
  351. */
  352. HAL_StatusTypeDef HAL_ETH_DeInit(ETH_HandleTypeDef *heth)
  353. {
  354. /* Set the ETH peripheral state to BUSY */
  355. heth->gState = HAL_ETH_STATE_BUSY;
  356. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  357. if (heth->MspDeInitCallback == NULL)
  358. {
  359. heth->MspDeInitCallback = HAL_ETH_MspDeInit;
  360. }
  361. /* DeInit the low level hardware */
  362. heth->MspDeInitCallback(heth);
  363. #else
  364. /* De-Init the low level hardware : GPIO, CLOCK, NVIC. */
  365. HAL_ETH_MspDeInit(heth);
  366. #endif /* (USE_HAL_ETH_REGISTER_CALLBACKS) */
  367. /* Set ETH HAL state to Disabled */
  368. heth->gState = HAL_ETH_STATE_RESET;
  369. /* Return function status */
  370. return HAL_OK;
  371. }
  372. /**
  373. * @brief Initializes the ETH MSP.
  374. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  375. * the configuration information for ETHERNET module
  376. * @retval None
  377. */
  378. __weak void HAL_ETH_MspInit(ETH_HandleTypeDef *heth)
  379. {
  380. /* Prevent unused argument(s) compilation warning */
  381. UNUSED(heth);
  382. /* NOTE : This function Should not be modified, when the callback is needed,
  383. the HAL_ETH_MspInit could be implemented in the user file
  384. */
  385. }
  386. /**
  387. * @brief DeInitializes ETH MSP.
  388. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  389. * the configuration information for ETHERNET module
  390. * @retval None
  391. */
  392. __weak void HAL_ETH_MspDeInit(ETH_HandleTypeDef *heth)
  393. {
  394. /* Prevent unused argument(s) compilation warning */
  395. UNUSED(heth);
  396. /* NOTE : This function Should not be modified, when the callback is needed,
  397. the HAL_ETH_MspDeInit could be implemented in the user file
  398. */
  399. }
  400. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  401. /**
  402. * @brief Register a User ETH Callback
  403. * To be used instead of the weak predefined callback
  404. * @param heth eth handle
  405. * @param CallbackID ID of the callback to be registered
  406. * This parameter can be one of the following values:
  407. * @arg @ref HAL_ETH_TX_COMPLETE_CB_ID Tx Complete Callback ID
  408. * @arg @ref HAL_ETH_RX_COMPLETE_CB_ID Rx Complete Callback ID
  409. * @arg @ref HAL_ETH_ERROR_CB_ID Error Callback ID
  410. * @arg @ref HAL_ETH_PMT_CB_ID Power Management Callback ID
  411. * @arg @ref HAL_ETH_EEE_CB_ID EEE Callback ID
  412. * @arg @ref HAL_ETH_WAKEUP_CB_ID Wake UP Callback ID
  413. * @arg @ref HAL_ETH_MSPINIT_CB_ID MspInit callback ID
  414. * @arg @ref HAL_ETH_MSPDEINIT_CB_ID MspDeInit callback ID
  415. * @param pCallback pointer to the Callback function
  416. * @retval status
  417. */
  418. HAL_StatusTypeDef HAL_ETH_RegisterCallback(ETH_HandleTypeDef *heth, HAL_ETH_CallbackIDTypeDef CallbackID,
  419. pETH_CallbackTypeDef pCallback)
  420. {
  421. HAL_StatusTypeDef status = HAL_OK;
  422. if (pCallback == NULL)
  423. {
  424. /* Update the error code */
  425. heth->ErrorCode |= HAL_ETH_ERROR_INVALID_CALLBACK;
  426. return HAL_ERROR;
  427. }
  428. if (heth->gState == HAL_ETH_STATE_READY)
  429. {
  430. switch (CallbackID)
  431. {
  432. case HAL_ETH_TX_COMPLETE_CB_ID :
  433. heth->TxCpltCallback = pCallback;
  434. break;
  435. case HAL_ETH_RX_COMPLETE_CB_ID :
  436. heth->RxCpltCallback = pCallback;
  437. break;
  438. case HAL_ETH_ERROR_CB_ID :
  439. heth->ErrorCallback = pCallback;
  440. break;
  441. case HAL_ETH_PMT_CB_ID :
  442. heth->PMTCallback = pCallback;
  443. break;
  444. case HAL_ETH_EEE_CB_ID :
  445. heth->EEECallback = pCallback;
  446. break;
  447. case HAL_ETH_WAKEUP_CB_ID :
  448. heth->WakeUpCallback = pCallback;
  449. break;
  450. case HAL_ETH_MSPINIT_CB_ID :
  451. heth->MspInitCallback = pCallback;
  452. break;
  453. case HAL_ETH_MSPDEINIT_CB_ID :
  454. heth->MspDeInitCallback = pCallback;
  455. break;
  456. default :
  457. /* Update the error code */
  458. heth->ErrorCode |= HAL_ETH_ERROR_INVALID_CALLBACK;
  459. /* Return error status */
  460. status = HAL_ERROR;
  461. break;
  462. }
  463. }
  464. else if (heth->gState == HAL_ETH_STATE_RESET)
  465. {
  466. switch (CallbackID)
  467. {
  468. case HAL_ETH_MSPINIT_CB_ID :
  469. heth->MspInitCallback = pCallback;
  470. break;
  471. case HAL_ETH_MSPDEINIT_CB_ID :
  472. heth->MspDeInitCallback = pCallback;
  473. break;
  474. default :
  475. /* Update the error code */
  476. heth->ErrorCode |= HAL_ETH_ERROR_INVALID_CALLBACK;
  477. /* Return error status */
  478. status = HAL_ERROR;
  479. break;
  480. }
  481. }
  482. else
  483. {
  484. /* Update the error code */
  485. heth->ErrorCode |= HAL_ETH_ERROR_INVALID_CALLBACK;
  486. /* Return error status */
  487. status = HAL_ERROR;
  488. }
  489. return status;
  490. }
  491. /**
  492. * @brief Unregister an ETH Callback
  493. * ETH callabck is redirected to the weak predefined callback
  494. * @param heth eth handle
  495. * @param CallbackID ID of the callback to be unregistered
  496. * This parameter can be one of the following values:
  497. * @arg @ref HAL_ETH_TX_COMPLETE_CB_ID Tx Complete Callback ID
  498. * @arg @ref HAL_ETH_RX_COMPLETE_CB_ID Rx Complete Callback ID
  499. * @arg @ref HAL_ETH_ERROR_CB_ID Error Callback ID
  500. * @arg @ref HAL_ETH_PMT_CB_ID Power Management Callback ID
  501. * @arg @ref HAL_ETH_EEE_CB_ID EEE Callback ID
  502. * @arg @ref HAL_ETH_WAKEUP_CB_ID Wake UP Callback ID
  503. * @arg @ref HAL_ETH_MSPINIT_CB_ID MspInit callback ID
  504. * @arg @ref HAL_ETH_MSPDEINIT_CB_ID MspDeInit callback ID
  505. * @retval status
  506. */
  507. HAL_StatusTypeDef HAL_ETH_UnRegisterCallback(ETH_HandleTypeDef *heth, HAL_ETH_CallbackIDTypeDef CallbackID)
  508. {
  509. HAL_StatusTypeDef status = HAL_OK;
  510. if (heth->gState == HAL_ETH_STATE_READY)
  511. {
  512. switch (CallbackID)
  513. {
  514. case HAL_ETH_TX_COMPLETE_CB_ID :
  515. heth->TxCpltCallback = HAL_ETH_TxCpltCallback;
  516. break;
  517. case HAL_ETH_RX_COMPLETE_CB_ID :
  518. heth->RxCpltCallback = HAL_ETH_RxCpltCallback;
  519. break;
  520. case HAL_ETH_ERROR_CB_ID :
  521. heth->ErrorCallback = HAL_ETH_ErrorCallback;
  522. break;
  523. case HAL_ETH_PMT_CB_ID :
  524. heth->PMTCallback = HAL_ETH_PMTCallback;
  525. break;
  526. case HAL_ETH_EEE_CB_ID :
  527. heth->EEECallback = HAL_ETH_EEECallback;
  528. break;
  529. case HAL_ETH_WAKEUP_CB_ID :
  530. heth->WakeUpCallback = HAL_ETH_WakeUpCallback;
  531. break;
  532. case HAL_ETH_MSPINIT_CB_ID :
  533. heth->MspInitCallback = HAL_ETH_MspInit;
  534. break;
  535. case HAL_ETH_MSPDEINIT_CB_ID :
  536. heth->MspDeInitCallback = HAL_ETH_MspDeInit;
  537. break;
  538. default :
  539. /* Update the error code */
  540. heth->ErrorCode |= HAL_ETH_ERROR_INVALID_CALLBACK;
  541. /* Return error status */
  542. status = HAL_ERROR;
  543. break;
  544. }
  545. }
  546. else if (heth->gState == HAL_ETH_STATE_RESET)
  547. {
  548. switch (CallbackID)
  549. {
  550. case HAL_ETH_MSPINIT_CB_ID :
  551. heth->MspInitCallback = HAL_ETH_MspInit;
  552. break;
  553. case HAL_ETH_MSPDEINIT_CB_ID :
  554. heth->MspDeInitCallback = HAL_ETH_MspDeInit;
  555. break;
  556. default :
  557. /* Update the error code */
  558. heth->ErrorCode |= HAL_ETH_ERROR_INVALID_CALLBACK;
  559. /* Return error status */
  560. status = HAL_ERROR;
  561. break;
  562. }
  563. }
  564. else
  565. {
  566. /* Update the error code */
  567. heth->ErrorCode |= HAL_ETH_ERROR_INVALID_CALLBACK;
  568. /* Return error status */
  569. status = HAL_ERROR;
  570. }
  571. return status;
  572. }
  573. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  574. /**
  575. * @}
  576. */
  577. /** @defgroup ETH_Exported_Functions_Group2 IO operation functions
  578. * @brief ETH Transmit and Receive functions
  579. *
  580. @verbatim
  581. ==============================================================================
  582. ##### IO operation functions #####
  583. ==============================================================================
  584. [..]
  585. This subsection provides a set of functions allowing to manage the ETH
  586. data transfer.
  587. @endverbatim
  588. * @{
  589. */
  590. /**
  591. * @brief Enables Ethernet MAC and DMA reception and transmission
  592. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  593. * the configuration information for ETHERNET module
  594. * @retval HAL status
  595. */
  596. HAL_StatusTypeDef HAL_ETH_Start(ETH_HandleTypeDef *heth)
  597. {
  598. if (heth->gState == HAL_ETH_STATE_READY)
  599. {
  600. heth->gState = HAL_ETH_STATE_BUSY;
  601. /* Set nombre of descriptors to build */
  602. heth->RxDescList.RxBuildDescCnt = ETH_RX_DESC_CNT;
  603. /* Build all descriptors */
  604. ETH_UpdateDescriptor(heth);
  605. /* Enable the MAC transmission */
  606. SET_BIT(heth->Instance->MACCR, ETH_MACCR_TE);
  607. /* Enable the MAC reception */
  608. SET_BIT(heth->Instance->MACCR, ETH_MACCR_RE);
  609. /* Set the Flush Transmit FIFO bit */
  610. SET_BIT(heth->Instance->MTLTQOMR, ETH_MTLTQOMR_FTQ);
  611. /* Enable the DMA transmission */
  612. SET_BIT(heth->Instance->DMACTCR, ETH_DMACTCR_ST);
  613. /* Enable the DMA reception */
  614. SET_BIT(heth->Instance->DMACRCR, ETH_DMACRCR_SR);
  615. /* Clear Tx and Rx process stopped flags */
  616. heth->Instance->DMACSR |= (ETH_DMACSR_TPS | ETH_DMACSR_RPS);
  617. heth->gState = HAL_ETH_STATE_STARTED;
  618. return HAL_OK;
  619. }
  620. else
  621. {
  622. return HAL_ERROR;
  623. }
  624. }
  625. /**
  626. * @brief Enables Ethernet MAC and DMA reception/transmission in Interrupt mode
  627. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  628. * the configuration information for ETHERNET module
  629. * @retval HAL status
  630. */
  631. HAL_StatusTypeDef HAL_ETH_Start_IT(ETH_HandleTypeDef *heth)
  632. {
  633. if (heth->gState == HAL_ETH_STATE_READY)
  634. {
  635. heth->gState = HAL_ETH_STATE_BUSY;
  636. /* save IT mode to ETH Handle */
  637. heth->RxDescList.ItMode = 1U;
  638. /* Disable Rx MMC Interrupts */
  639. SET_BIT(heth->Instance->MMCRIMR, ETH_MMCRIMR_RXLPITRCIM | ETH_MMCRIMR_RXLPIUSCIM | \
  640. ETH_MMCRIMR_RXUCGPIM | ETH_MMCRIMR_RXALGNERPIM | ETH_MMCRIMR_RXCRCERPIM);
  641. /* Disable Tx MMC Interrupts */
  642. SET_BIT(heth->Instance->MMCTIMR, ETH_MMCTIMR_TXLPITRCIM | ETH_MMCTIMR_TXLPIUSCIM | \
  643. ETH_MMCTIMR_TXGPKTIM | ETH_MMCTIMR_TXMCOLGPIM | ETH_MMCTIMR_TXSCOLGPIM);
  644. /* Set nombre of descriptors to build */
  645. heth->RxDescList.RxBuildDescCnt = ETH_RX_DESC_CNT;
  646. /* Build all descriptors */
  647. ETH_UpdateDescriptor(heth);
  648. /* Enable the MAC transmission */
  649. SET_BIT(heth->Instance->MACCR, ETH_MACCR_TE);
  650. /* Enable the MAC reception */
  651. SET_BIT(heth->Instance->MACCR, ETH_MACCR_RE);
  652. /* Set the Flush Transmit FIFO bit */
  653. SET_BIT(heth->Instance->MTLTQOMR, ETH_MTLTQOMR_FTQ);
  654. /* Enable the DMA transmission */
  655. SET_BIT(heth->Instance->DMACTCR, ETH_DMACTCR_ST);
  656. /* Enable the DMA reception */
  657. SET_BIT(heth->Instance->DMACRCR, ETH_DMACRCR_SR);
  658. /* Clear Tx and Rx process stopped flags */
  659. heth->Instance->DMACSR |= (ETH_DMACSR_TPS | ETH_DMACSR_RPS);
  660. /* Enable ETH DMA interrupts:
  661. - Tx complete interrupt
  662. - Rx complete interrupt
  663. - Fatal bus interrupt
  664. */
  665. __HAL_ETH_DMA_ENABLE_IT(heth, (ETH_DMACIER_NIE | ETH_DMACIER_RIE | ETH_DMACIER_TIE |
  666. ETH_DMACIER_FBEE | ETH_DMACIER_AIE | ETH_DMACIER_RBUE));
  667. heth->gState = HAL_ETH_STATE_STARTED;
  668. return HAL_OK;
  669. }
  670. else
  671. {
  672. return HAL_ERROR;
  673. }
  674. }
  675. /**
  676. * @brief Stop Ethernet MAC and DMA reception/transmission
  677. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  678. * the configuration information for ETHERNET module
  679. * @retval HAL status
  680. */
  681. HAL_StatusTypeDef HAL_ETH_Stop(ETH_HandleTypeDef *heth)
  682. {
  683. if (heth->gState == HAL_ETH_STATE_STARTED)
  684. {
  685. /* Set the ETH peripheral state to BUSY */
  686. heth->gState = HAL_ETH_STATE_BUSY;
  687. /* Disable the DMA transmission */
  688. CLEAR_BIT(heth->Instance->DMACTCR, ETH_DMACTCR_ST);
  689. /* Disable the DMA reception */
  690. CLEAR_BIT(heth->Instance->DMACRCR, ETH_DMACRCR_SR);
  691. /* Disable the MAC reception */
  692. CLEAR_BIT(heth->Instance->MACCR, ETH_MACCR_RE);
  693. /* Set the Flush Transmit FIFO bit */
  694. SET_BIT(heth->Instance->MTLTQOMR, ETH_MTLTQOMR_FTQ);
  695. /* Disable the MAC transmission */
  696. CLEAR_BIT(heth->Instance->MACCR, ETH_MACCR_TE);
  697. heth->gState = HAL_ETH_STATE_READY;
  698. /* Return function status */
  699. return HAL_OK;
  700. }
  701. else
  702. {
  703. return HAL_ERROR;
  704. }
  705. }
  706. /**
  707. * @brief Stop Ethernet MAC and DMA reception/transmission in Interrupt mode
  708. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  709. * the configuration information for ETHERNET module
  710. * @retval HAL status
  711. */
  712. HAL_StatusTypeDef HAL_ETH_Stop_IT(ETH_HandleTypeDef *heth)
  713. {
  714. ETH_DMADescTypeDef *dmarxdesc;
  715. uint32_t descindex;
  716. if (heth->gState == HAL_ETH_STATE_STARTED)
  717. {
  718. /* Set the ETH peripheral state to BUSY */
  719. heth->gState = HAL_ETH_STATE_BUSY;
  720. /* Disable interrupts:
  721. - Tx complete interrupt
  722. - Rx complete interrupt
  723. - Fatal bus interrupt
  724. */
  725. __HAL_ETH_DMA_DISABLE_IT(heth, (ETH_DMACIER_NIE | ETH_DMACIER_RIE | ETH_DMACIER_TIE |
  726. ETH_DMACIER_FBEE | ETH_DMACIER_AIE | ETH_DMACIER_RBUE));
  727. /* Disable the DMA transmission */
  728. CLEAR_BIT(heth->Instance->DMACTCR, ETH_DMACTCR_ST);
  729. /* Disable the DMA reception */
  730. CLEAR_BIT(heth->Instance->DMACRCR, ETH_DMACRCR_SR);
  731. /* Disable the MAC reception */
  732. CLEAR_BIT(heth->Instance->MACCR, ETH_MACCR_RE);
  733. /* Set the Flush Transmit FIFO bit */
  734. SET_BIT(heth->Instance->MTLTQOMR, ETH_MTLTQOMR_FTQ);
  735. /* Disable the MAC transmission */
  736. CLEAR_BIT(heth->Instance->MACCR, ETH_MACCR_TE);
  737. /* Clear IOC bit to all Rx descriptors */
  738. for (descindex = 0; descindex < (uint32_t)ETH_RX_DESC_CNT; descindex++)
  739. {
  740. dmarxdesc = (ETH_DMADescTypeDef *)heth->RxDescList.RxDesc[descindex];
  741. CLEAR_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCRF_IOC);
  742. }
  743. heth->RxDescList.ItMode = 0U;
  744. heth->gState = HAL_ETH_STATE_READY;
  745. /* Return function status */
  746. return HAL_OK;
  747. }
  748. else
  749. {
  750. return HAL_ERROR;
  751. }
  752. }
  753. /**
  754. * @brief Sends an Ethernet Packet in polling mode.
  755. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  756. * the configuration information for ETHERNET module
  757. * @param pTxConfig: Hold the configuration of packet to be transmitted
  758. * @param Timeout: timeout value
  759. * @retval HAL status
  760. */
  761. HAL_StatusTypeDef HAL_ETH_Transmit(ETH_HandleTypeDef *heth, ETH_TxPacketConfig *pTxConfig, uint32_t Timeout)
  762. {
  763. uint32_t tickstart;
  764. ETH_DMADescTypeDef *dmatxdesc;
  765. if (pTxConfig == NULL)
  766. {
  767. heth->ErrorCode |= HAL_ETH_ERROR_PARAM;
  768. return HAL_ERROR;
  769. }
  770. if (heth->gState == HAL_ETH_STATE_STARTED)
  771. {
  772. /* Config DMA Tx descriptor by Tx Packet info */
  773. if (ETH_Prepare_Tx_Descriptors(heth, pTxConfig, 0) != HAL_ETH_ERROR_NONE)
  774. {
  775. /* Set the ETH error code */
  776. heth->ErrorCode |= HAL_ETH_ERROR_BUSY;
  777. return HAL_ERROR;
  778. }
  779. /* Ensure completion of descriptor preparation before transmission start */
  780. __DSB();
  781. dmatxdesc = (ETH_DMADescTypeDef *)(&heth->TxDescList)->TxDesc[heth->TxDescList.CurTxDesc];
  782. /* Incr current tx desc index */
  783. INCR_TX_DESC_INDEX(heth->TxDescList.CurTxDesc, 1U);
  784. /* Start transmission */
  785. /* issue a poll command to Tx DMA by writing address of next immediate free descriptor */
  786. WRITE_REG(heth->Instance->DMACTDTPR, (uint32_t)(heth->TxDescList.TxDesc[heth->TxDescList.CurTxDesc]));
  787. tickstart = HAL_GetTick();
  788. /* Wait for data to be transmitted or timeout occurred */
  789. while ((dmatxdesc->DESC3 & ETH_DMATXNDESCWBF_OWN) != (uint32_t)RESET)
  790. {
  791. if ((heth->Instance->DMACSR & ETH_DMACSR_FBE) != (uint32_t)RESET)
  792. {
  793. heth->ErrorCode |= HAL_ETH_ERROR_DMA;
  794. heth->DMAErrorCode = heth->Instance->DMACSR;
  795. /* Return function status */
  796. return HAL_ERROR;
  797. }
  798. /* Check for the Timeout */
  799. if (Timeout != HAL_MAX_DELAY)
  800. {
  801. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  802. {
  803. heth->ErrorCode |= HAL_ETH_ERROR_TIMEOUT;
  804. /* Clear TX descriptor so that we can proceed */
  805. dmatxdesc->DESC3 = (ETH_DMATXNDESCWBF_FD | ETH_DMATXNDESCWBF_LD);
  806. return HAL_ERROR;
  807. }
  808. }
  809. }
  810. /* Return function status */
  811. return HAL_OK;
  812. }
  813. else
  814. {
  815. return HAL_ERROR;
  816. }
  817. }
  818. /**
  819. * @brief Sends an Ethernet Packet in interrupt mode.
  820. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  821. * the configuration information for ETHERNET module
  822. * @param pTxConfig: Hold the configuration of packet to be transmitted
  823. * @retval HAL status
  824. */
  825. HAL_StatusTypeDef HAL_ETH_Transmit_IT(ETH_HandleTypeDef *heth, ETH_TxPacketConfig *pTxConfig)
  826. {
  827. if (pTxConfig == NULL)
  828. {
  829. heth->ErrorCode |= HAL_ETH_ERROR_PARAM;
  830. return HAL_ERROR;
  831. }
  832. if (heth->gState == HAL_ETH_STATE_STARTED)
  833. {
  834. /* Save the packet pointer to release. */
  835. heth->TxDescList.CurrentPacketAddress = (uint32_t *)pTxConfig->pData;
  836. /* Config DMA Tx descriptor by Tx Packet info */
  837. if (ETH_Prepare_Tx_Descriptors(heth, pTxConfig, 1) != HAL_ETH_ERROR_NONE)
  838. {
  839. heth->ErrorCode |= HAL_ETH_ERROR_BUSY;
  840. return HAL_ERROR;
  841. }
  842. /* Ensure completion of descriptor preparation before transmission start */
  843. __DSB();
  844. /* Incr current tx desc index */
  845. INCR_TX_DESC_INDEX(heth->TxDescList.CurTxDesc, 1U);
  846. /* Start transmission */
  847. /* issue a poll command to Tx DMA by writing address of next immediate free descriptor */
  848. WRITE_REG(heth->Instance->DMACTDTPR, (uint32_t)(heth->TxDescList.TxDesc[heth->TxDescList.CurTxDesc]));
  849. return HAL_OK;
  850. }
  851. else
  852. {
  853. return HAL_ERROR;
  854. }
  855. }
  856. /**
  857. * @brief Read a received packet.
  858. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  859. * the configuration information for ETHERNET module
  860. * @param pAppBuff: Pointer to an application buffer to receive the packet.
  861. * @retval HAL status
  862. */
  863. HAL_StatusTypeDef HAL_ETH_ReadData(ETH_HandleTypeDef *heth, void **pAppBuff)
  864. {
  865. uint32_t descidx;
  866. ETH_DMADescTypeDef *dmarxdesc;
  867. uint32_t desccnt = 0U;
  868. uint32_t desccntmax;
  869. uint32_t bufflength;
  870. uint8_t rxdataready = 0U;
  871. if (pAppBuff == NULL)
  872. {
  873. heth->ErrorCode |= HAL_ETH_ERROR_PARAM;
  874. return HAL_ERROR;
  875. }
  876. if (heth->gState != HAL_ETH_STATE_STARTED)
  877. {
  878. return HAL_ERROR;
  879. }
  880. descidx = heth->RxDescList.RxDescIdx;
  881. dmarxdesc = (ETH_DMADescTypeDef *)heth->RxDescList.RxDesc[descidx];
  882. desccntmax = ETH_RX_DESC_CNT - heth->RxDescList.RxBuildDescCnt;
  883. /* Check if descriptor is not owned by DMA */
  884. while ((READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_OWN) == (uint32_t)RESET) && (desccnt < desccntmax)
  885. && (rxdataready == 0U))
  886. {
  887. if (READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_CTXT) != (uint32_t)RESET)
  888. {
  889. /* Get timestamp high */
  890. heth->RxDescList.TimeStamp.TimeStampHigh = dmarxdesc->DESC1;
  891. /* Get timestamp low */
  892. heth->RxDescList.TimeStamp.TimeStampLow = dmarxdesc->DESC0;
  893. }
  894. if ((READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_FD) != (uint32_t)RESET) || (heth->RxDescList.pRxStart != NULL))
  895. {
  896. /* Check if first descriptor */
  897. if (READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_FD) != (uint32_t)RESET)
  898. {
  899. heth->RxDescList.RxDescCnt = 0;
  900. heth->RxDescList.RxDataLength = 0;
  901. }
  902. /* Check if last descriptor */
  903. bufflength = heth->Init.RxBuffLen;
  904. if (READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_LD) != (uint32_t)RESET)
  905. {
  906. bufflength = READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_PL) - heth->RxDescList.RxDataLength;
  907. /* Save Last descriptor index */
  908. heth->RxDescList.pRxLastRxDesc = dmarxdesc->DESC3;
  909. /* Packet ready */
  910. rxdataready = 1;
  911. }
  912. /* Link data */
  913. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  914. /*Call registered Link callback*/
  915. heth->rxLinkCallback(&heth->RxDescList.pRxStart, &heth->RxDescList.pRxEnd,
  916. (uint8_t *)dmarxdesc->BackupAddr0, bufflength);
  917. #else
  918. /* Link callback */
  919. HAL_ETH_RxLinkCallback(&heth->RxDescList.pRxStart, &heth->RxDescList.pRxEnd,
  920. (uint8_t *)dmarxdesc->BackupAddr0, (uint16_t) bufflength);
  921. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  922. heth->RxDescList.RxDescCnt++;
  923. heth->RxDescList.RxDataLength += bufflength;
  924. /* Clear buffer pointer */
  925. dmarxdesc->BackupAddr0 = 0;
  926. }
  927. /* Increment current rx descriptor index */
  928. INCR_RX_DESC_INDEX(descidx, 1U);
  929. /* Get current descriptor address */
  930. dmarxdesc = (ETH_DMADescTypeDef *)heth->RxDescList.RxDesc[descidx];
  931. desccnt++;
  932. }
  933. heth->RxDescList.RxBuildDescCnt += desccnt;
  934. if ((heth->RxDescList.RxBuildDescCnt) != 0U)
  935. {
  936. /* Update Descriptors */
  937. ETH_UpdateDescriptor(heth);
  938. }
  939. heth->RxDescList.RxDescIdx = descidx;
  940. if (rxdataready == 1U)
  941. {
  942. /* Return received packet */
  943. *pAppBuff = heth->RxDescList.pRxStart;
  944. /* Reset first element */
  945. heth->RxDescList.pRxStart = NULL;
  946. return HAL_OK;
  947. }
  948. /* Packet not ready */
  949. return HAL_ERROR;
  950. }
  951. /**
  952. * @brief This function gives back Rx Desc of the last received Packet
  953. * to the DMA, so ETH DMA will be able to use these descriptors
  954. * to receive next Packets.
  955. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  956. * the configuration information for ETHERNET module
  957. * @retval HAL status
  958. */
  959. static void ETH_UpdateDescriptor(ETH_HandleTypeDef *heth)
  960. {
  961. uint32_t descidx;
  962. uint32_t desccount;
  963. ETH_DMADescTypeDef *dmarxdesc;
  964. uint8_t *buff = NULL;
  965. uint8_t allocStatus = 1U;
  966. descidx = heth->RxDescList.RxBuildDescIdx;
  967. dmarxdesc = (ETH_DMADescTypeDef *)heth->RxDescList.RxDesc[descidx];
  968. desccount = heth->RxDescList.RxBuildDescCnt;
  969. while ((desccount > 0U) && (allocStatus != 0U))
  970. {
  971. /* Check if a buffer's attached the descriptor */
  972. if (READ_REG(dmarxdesc->BackupAddr0) == 0U)
  973. {
  974. /* Get a new buffer. */
  975. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  976. /*Call registered Allocate callback*/
  977. heth->rxAllocateCallback(&buff);
  978. #else
  979. /* Allocate callback */
  980. HAL_ETH_RxAllocateCallback(&buff);
  981. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  982. if (buff == NULL)
  983. {
  984. allocStatus = 0U;
  985. }
  986. else
  987. {
  988. WRITE_REG(dmarxdesc->BackupAddr0, (uint32_t)buff);
  989. WRITE_REG(dmarxdesc->DESC0, (uint32_t)buff);
  990. }
  991. }
  992. if (allocStatus != 0U)
  993. {
  994. /* Ensure rest of descriptor is written to RAM before the OWN bit */
  995. __DMB();
  996. if (heth->RxDescList.ItMode != 0U)
  997. {
  998. WRITE_REG(dmarxdesc->DESC3, ETH_DMARXNDESCRF_OWN | ETH_DMARXNDESCRF_BUF1V | ETH_DMARXNDESCRF_IOC);
  999. }
  1000. else
  1001. {
  1002. WRITE_REG(dmarxdesc->DESC3, ETH_DMARXNDESCRF_OWN | ETH_DMARXNDESCRF_BUF1V);
  1003. }
  1004. /* Increment current rx descriptor index */
  1005. INCR_RX_DESC_INDEX(descidx, 1U);
  1006. /* Get current descriptor address */
  1007. dmarxdesc = (ETH_DMADescTypeDef *)heth->RxDescList.RxDesc[descidx];
  1008. desccount--;
  1009. }
  1010. }
  1011. if (heth->RxDescList.RxBuildDescCnt != desccount)
  1012. {
  1013. /* Set the Tail pointer address */
  1014. WRITE_REG(heth->Instance->DMACRDTPR, 0);
  1015. heth->RxDescList.RxBuildDescIdx = descidx;
  1016. heth->RxDescList.RxBuildDescCnt = desccount;
  1017. }
  1018. }
  1019. /**
  1020. * @brief Register the Rx alloc callback.
  1021. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1022. * the configuration information for ETHERNET module
  1023. * @param rxAllocateCallback: pointer to function to alloc buffer
  1024. * @retval HAL status
  1025. */
  1026. HAL_StatusTypeDef HAL_ETH_RegisterRxAllocateCallback(ETH_HandleTypeDef *heth,
  1027. pETH_rxAllocateCallbackTypeDef rxAllocateCallback)
  1028. {
  1029. if (rxAllocateCallback == NULL)
  1030. {
  1031. /* No buffer to save */
  1032. return HAL_ERROR;
  1033. }
  1034. /* Set function to allocate buffer */
  1035. heth->rxAllocateCallback = rxAllocateCallback;
  1036. return HAL_OK;
  1037. }
  1038. /**
  1039. * @brief Unregister the Rx alloc callback.
  1040. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1041. * the configuration information for ETHERNET module
  1042. * @retval HAL status
  1043. */
  1044. HAL_StatusTypeDef HAL_ETH_UnRegisterRxAllocateCallback(ETH_HandleTypeDef *heth)
  1045. {
  1046. /* Set function to allocate buffer */
  1047. heth->rxAllocateCallback = HAL_ETH_RxAllocateCallback;
  1048. return HAL_OK;
  1049. }
  1050. /**
  1051. * @brief Rx Allocate callback.
  1052. * @param buff: pointer to allocated buffer
  1053. * @retval None
  1054. */
  1055. __weak void HAL_ETH_RxAllocateCallback(uint8_t **buff)
  1056. {
  1057. /* Prevent unused argument(s) compilation warning */
  1058. UNUSED(buff);
  1059. /* NOTE : This function Should not be modified, when the callback is needed,
  1060. the HAL_ETH_RxAllocateCallback could be implemented in the user file
  1061. */
  1062. }
  1063. /**
  1064. * @brief Rx Link callback.
  1065. * @param pStart: pointer to packet start
  1066. * @param pStart: pointer to packet end
  1067. * @param buff: pointer to received data
  1068. * @param Length: received data length
  1069. * @retval None
  1070. */
  1071. __weak void HAL_ETH_RxLinkCallback(void **pStart, void **pEnd, uint8_t *buff, uint16_t Length)
  1072. {
  1073. /* Prevent unused argument(s) compilation warning */
  1074. UNUSED(pStart);
  1075. UNUSED(pEnd);
  1076. UNUSED(buff);
  1077. UNUSED(Length);
  1078. /* NOTE : This function Should not be modified, when the callback is needed,
  1079. the HAL_ETH_RxLinkCallback could be implemented in the user file
  1080. */
  1081. }
  1082. /**
  1083. * @brief Set the Rx link data function.
  1084. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1085. * the configuration information for ETHERNET module
  1086. * @param rxLinkCallback: pointer to function to link data
  1087. * @retval HAL status
  1088. */
  1089. HAL_StatusTypeDef HAL_ETH_RegisterRxLinkCallback(ETH_HandleTypeDef *heth, pETH_rxLinkCallbackTypeDef rxLinkCallback)
  1090. {
  1091. if (rxLinkCallback == NULL)
  1092. {
  1093. /* No buffer to save */
  1094. return HAL_ERROR;
  1095. }
  1096. /* Set function to link data */
  1097. heth->rxLinkCallback = rxLinkCallback;
  1098. return HAL_OK;
  1099. }
  1100. /**
  1101. * @brief Unregister the Rx link callback.
  1102. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1103. * the configuration information for ETHERNET module
  1104. * @retval HAL status
  1105. */
  1106. HAL_StatusTypeDef HAL_ETH_UnRegisterRxLinkCallback(ETH_HandleTypeDef *heth)
  1107. {
  1108. /* Set function to allocate buffer */
  1109. heth->rxLinkCallback = HAL_ETH_RxLinkCallback;
  1110. return HAL_OK;
  1111. }
  1112. /**
  1113. * @brief Get the error state of the last received packet.
  1114. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1115. * the configuration information for ETHERNET module
  1116. * @param pErrorCode: pointer to uint32_t to hold the error code
  1117. * @retval HAL status
  1118. */
  1119. HAL_StatusTypeDef HAL_ETH_GetRxDataErrorCode(ETH_HandleTypeDef *heth, uint32_t *pErrorCode)
  1120. {
  1121. /* Get error bits. */
  1122. *pErrorCode = READ_BIT(heth->RxDescList.pRxLastRxDesc, ETH_DMARXNDESCWBF_ERRORS_MASK);
  1123. return HAL_OK;
  1124. }
  1125. /**
  1126. * @brief Set the Tx free function.
  1127. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1128. * the configuration information for ETHERNET module
  1129. * @param txFreeCallback: pointer to function to release the packet
  1130. * @retval HAL status
  1131. */
  1132. HAL_StatusTypeDef HAL_ETH_RegisterTxFreeCallback(ETH_HandleTypeDef *heth, pETH_txFreeCallbackTypeDef txFreeCallback)
  1133. {
  1134. if (txFreeCallback == NULL)
  1135. {
  1136. /* No buffer to save */
  1137. return HAL_ERROR;
  1138. }
  1139. /* Set function to free transmmitted packet */
  1140. heth->txFreeCallback = txFreeCallback;
  1141. return HAL_OK;
  1142. }
  1143. /**
  1144. * @brief Unregister the Tx free callback.
  1145. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1146. * the configuration information for ETHERNET module
  1147. * @retval HAL status
  1148. */
  1149. HAL_StatusTypeDef HAL_ETH_UnRegisterTxFreeCallback(ETH_HandleTypeDef *heth)
  1150. {
  1151. /* Set function to allocate buffer */
  1152. heth->txFreeCallback = HAL_ETH_TxFreeCallback;
  1153. return HAL_OK;
  1154. }
  1155. /**
  1156. * @brief Tx Free callback.
  1157. * @param buff: pointer to buffer to free
  1158. * @retval None
  1159. */
  1160. __weak void HAL_ETH_TxFreeCallback(uint32_t *buff)
  1161. {
  1162. /* Prevent unused argument(s) compilation warning */
  1163. UNUSED(buff);
  1164. /* NOTE : This function Should not be modified, when the callback is needed,
  1165. the HAL_ETH_TxFreeCallback could be implemented in the user file
  1166. */
  1167. }
  1168. /**
  1169. * @brief Release transmitted Tx packets.
  1170. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1171. * the configuration information for ETHERNET module
  1172. * @retval HAL status
  1173. */
  1174. HAL_StatusTypeDef HAL_ETH_ReleaseTxPacket(ETH_HandleTypeDef *heth)
  1175. {
  1176. ETH_TxDescListTypeDef *dmatxdesclist = &heth->TxDescList;
  1177. uint32_t numOfBuf = dmatxdesclist->BuffersInUse;
  1178. uint32_t idx = dmatxdesclist->releaseIndex;
  1179. uint8_t pktTxStatus = 1U;
  1180. uint8_t pktInUse;
  1181. #ifdef HAL_ETH_USE_PTP
  1182. ETH_TimeStampTypeDef *timestamp = &heth->TxTimestamp;
  1183. #endif /* HAL_ETH_USE_PTP */
  1184. /* Loop through buffers in use. */
  1185. while ((numOfBuf != 0U) && (pktTxStatus != 0U))
  1186. {
  1187. pktInUse = 1U;
  1188. numOfBuf--;
  1189. /* If no packet, just examine the next packet. */
  1190. if (dmatxdesclist->PacketAddress[idx] == NULL)
  1191. {
  1192. /* No packet in use, skip to next. */
  1193. idx = (idx + 1U) & (ETH_TX_DESC_CNT - 1U);
  1194. pktInUse = 0U;
  1195. }
  1196. if (pktInUse != 0U)
  1197. {
  1198. /* Determine if the packet has been transmitted. */
  1199. if ((heth->Init.TxDesc[idx].DESC3 & ETH_DMATXNDESCRF_OWN) == 0U)
  1200. {
  1201. #ifdef HAL_ETH_USE_PTP
  1202. /* Disable Ptp transmission */
  1203. CLEAR_BIT(heth->Init.TxDesc[idx].DESC3, ((uint32_t)0x40000000U));
  1204. /* Get timestamp low */
  1205. timestamp->TimeStampLow = heth->Init.TxDesc[idx].DESC0;
  1206. /* Get timestamp high */
  1207. timestamp->TimeStampHigh = heth->Init.TxDesc[idx].DESC1;
  1208. #endif /* HAL_ETH_USE_PTP */
  1209. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  1210. /*Call registered callbacks*/
  1211. #ifdef HAL_ETH_USE_PTP
  1212. /* Handle Ptp */
  1213. heth->txPtpCallback(dmatxdesclist->PacketAddress[idx], timestamp);
  1214. #endif /* HAL_ETH_USE_PTP */
  1215. /* Release the packet. */
  1216. heth->txFreeCallback(dmatxdesclist->PacketAddress[idx]);
  1217. #else
  1218. /* Call callbacks */
  1219. #ifdef HAL_ETH_USE_PTP
  1220. /* Handle Ptp */
  1221. HAL_ETH_TxPtpCallback(dmatxdesclist->PacketAddress[idx], timestamp);
  1222. #endif /* HAL_ETH_USE_PTP */
  1223. /* Release the packet. */
  1224. HAL_ETH_TxFreeCallback(dmatxdesclist->PacketAddress[idx]);
  1225. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  1226. /* Clear the entry in the in-use array. */
  1227. dmatxdesclist->PacketAddress[idx] = NULL;
  1228. /* Update the transmit relesae index and number of buffers in use. */
  1229. idx = (idx + 1U) & (ETH_TX_DESC_CNT - 1U);
  1230. dmatxdesclist->BuffersInUse = numOfBuf;
  1231. dmatxdesclist->releaseIndex = idx;
  1232. }
  1233. else
  1234. {
  1235. /* Get out of the loop! */
  1236. pktTxStatus = 0U;
  1237. }
  1238. }
  1239. }
  1240. return HAL_OK;
  1241. }
  1242. #ifdef HAL_ETH_USE_PTP
  1243. /**
  1244. * @brief Set the Ethernet PTP configuration.
  1245. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1246. * the configuration information for ETHERNET module
  1247. * @param ptpconfig: pointer to a ETH_PTP_ConfigTypeDef structure that contains
  1248. * the configuration information for PTP
  1249. * @retval HAL status
  1250. */
  1251. HAL_StatusTypeDef HAL_ETH_PTP_SetConfig(ETH_HandleTypeDef *heth, ETH_PTP_ConfigTypeDef *ptpconfig)
  1252. {
  1253. uint32_t tmpTSCR;
  1254. ETH_TimeTypeDef time;
  1255. if (ptpconfig == NULL)
  1256. {
  1257. return HAL_ERROR;
  1258. }
  1259. tmpTSCR = ptpconfig->Timestamp |
  1260. ((uint32_t)ptpconfig->TimestampUpdate << ETH_MACTSCR_TSUPDT_Pos) |
  1261. ((uint32_t)ptpconfig->TimestampAll << ETH_MACTSCR_TSENALL_Pos) |
  1262. ((uint32_t)ptpconfig->TimestampRolloverMode << ETH_MACTSCR_TSCTRLSSR_Pos) |
  1263. ((uint32_t)ptpconfig->TimestampV2 << ETH_MACTSCR_TSVER2ENA_Pos) |
  1264. ((uint32_t)ptpconfig->TimestampEthernet << ETH_MACTSCR_TSIPENA_Pos) |
  1265. ((uint32_t)ptpconfig->TimestampIPv6 << ETH_MACTSCR_TSIPV6ENA_Pos) |
  1266. ((uint32_t)ptpconfig->TimestampIPv4 << ETH_MACTSCR_TSIPV4ENA_Pos) |
  1267. ((uint32_t)ptpconfig->TimestampEvent << ETH_MACTSCR_TSEVNTENA_Pos) |
  1268. ((uint32_t)ptpconfig->TimestampMaster << ETH_MACTSCR_TSMSTRENA_Pos) |
  1269. ((uint32_t)ptpconfig->TimestampSnapshots << ETH_MACTSCR_SNAPTYPSEL_Pos) |
  1270. ((uint32_t)ptpconfig->TimestampFilter << ETH_MACTSCR_TSENMACADDR_Pos) |
  1271. ((uint32_t)ptpconfig->TimestampChecksumCorrection << ETH_MACTSCR_CSC_Pos) |
  1272. ((uint32_t)ptpconfig->TimestampStatusMode << ETH_MACTSCR_TXTSSTSM_Pos);
  1273. /* Write to MACTSCR */
  1274. MODIFY_REG(heth->Instance->MACTSCR, ETH_MACTSCR_MASK, tmpTSCR);
  1275. /* Enable Timestamp */
  1276. SET_BIT(heth->Instance->MACTSCR, ETH_MACTSCR_TSENA);
  1277. WRITE_REG(heth->Instance->MACSSIR, ptpconfig->TimestampSubsecondInc);
  1278. WRITE_REG(heth->Instance->MACTSAR, ptpconfig->TimestampAddend);
  1279. /* Enable Timestamp */
  1280. if (ptpconfig->TimestampAddendUpdate == ENABLE)
  1281. {
  1282. SET_BIT(heth->Instance->MACTSCR, ETH_MACTSCR_TSADDREG);
  1283. while ((heth->Instance->MACTSCR & ETH_MACTSCR_TSADDREG) != 0) {}
  1284. }
  1285. /* Enable Update mode */
  1286. if (ptpconfig->TimestampUpdateMode == ENABLE)
  1287. {
  1288. SET_BIT(heth->Instance->MACTSCR, ETH_MACTSCR_TSCFUPDT);
  1289. }
  1290. /* Initialize Time */
  1291. time.Seconds = 0;
  1292. time.NanoSeconds = 0;
  1293. HAL_ETH_PTP_SetTime(heth, &time);
  1294. /* Ptp Init */
  1295. SET_BIT(heth->Instance->MACTSCR, ETH_MACTSCR_TSINIT);
  1296. /* Set PTP Configuration done */
  1297. heth->IsPtpConfigured = HAL_ETH_PTP_CONFIGURATED;
  1298. /* Return function status */
  1299. return HAL_OK;
  1300. }
  1301. /**
  1302. * @brief Get the Ethernet PTP configuration.
  1303. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1304. * the configuration information for ETHERNET module
  1305. * @param ptpconfig: pointer to a ETH_PTP_ConfigTypeDef structure that contains
  1306. * the configuration information for PTP
  1307. * @retval HAL status
  1308. */
  1309. HAL_StatusTypeDef HAL_ETH_PTP_GetConfig(ETH_HandleTypeDef *heth, ETH_PTP_ConfigTypeDef *ptpconfig)
  1310. {
  1311. if (ptpconfig == NULL)
  1312. {
  1313. return HAL_ERROR;
  1314. }
  1315. ptpconfig->Timestamp = READ_BIT(heth->Instance->MACTSCR, ETH_MACTSCR_TSENA);
  1316. ptpconfig->TimestampUpdate = ((READ_BIT(heth->Instance->MACTSCR,
  1317. ETH_MACTSCR_TSCFUPDT) >> ETH_MACTSCR_TSUPDT_Pos) > 0U) ? ENABLE : DISABLE;
  1318. ptpconfig->TimestampAll = ((READ_BIT(heth->Instance->MACTSCR,
  1319. ETH_MACTSCR_TSENALL) >> ETH_MACTSCR_TSENALL_Pos) > 0U) ? ENABLE : DISABLE;
  1320. ptpconfig->TimestampRolloverMode = ((READ_BIT(heth->Instance->MACTSCR,
  1321. ETH_MACTSCR_TSCTRLSSR) >> ETH_MACTSCR_TSCTRLSSR_Pos) > 0U)
  1322. ? ENABLE : DISABLE;
  1323. ptpconfig->TimestampV2 = ((READ_BIT(heth->Instance->MACTSCR,
  1324. ETH_MACTSCR_TSVER2ENA) >> ETH_MACTSCR_TSVER2ENA_Pos) > 0U) ? ENABLE : DISABLE;
  1325. ptpconfig->TimestampEthernet = ((READ_BIT(heth->Instance->MACTSCR,
  1326. ETH_MACTSCR_TSIPENA) >> ETH_MACTSCR_TSIPENA_Pos) > 0U) ? ENABLE : DISABLE;
  1327. ptpconfig->TimestampIPv6 = ((READ_BIT(heth->Instance->MACTSCR,
  1328. ETH_MACTSCR_TSIPV6ENA) >> ETH_MACTSCR_TSIPV6ENA_Pos) > 0U) ? ENABLE : DISABLE;
  1329. ptpconfig->TimestampIPv4 = ((READ_BIT(heth->Instance->MACTSCR,
  1330. ETH_MACTSCR_TSIPV4ENA) >> ETH_MACTSCR_TSIPV4ENA_Pos) > 0U) ? ENABLE : DISABLE;
  1331. ptpconfig->TimestampEvent = ((READ_BIT(heth->Instance->MACTSCR,
  1332. ETH_MACTSCR_TSEVNTENA) >> ETH_MACTSCR_TSEVNTENA_Pos) > 0U) ? ENABLE : DISABLE;
  1333. ptpconfig->TimestampMaster = ((READ_BIT(heth->Instance->MACTSCR,
  1334. ETH_MACTSCR_TSMSTRENA) >> ETH_MACTSCR_TSMSTRENA_Pos) > 0U) ? ENABLE : DISABLE;
  1335. ptpconfig->TimestampSnapshots = ((READ_BIT(heth->Instance->MACTSCR,
  1336. ETH_MACTSCR_SNAPTYPSEL) >> ETH_MACTSCR_SNAPTYPSEL_Pos) > 0U)
  1337. ? ENABLE : DISABLE;
  1338. ptpconfig->TimestampFilter = ((READ_BIT(heth->Instance->MACTSCR,
  1339. ETH_MACTSCR_TSENMACADDR) >> ETH_MACTSCR_TSENMACADDR_Pos) > 0U)
  1340. ? ENABLE : DISABLE;
  1341. ptpconfig->TimestampChecksumCorrection = ((READ_BIT(heth->Instance->MACTSCR,
  1342. ETH_MACTSCR_CSC) >> ETH_MACTSCR_CSC_Pos) > 0U) ? ENABLE : DISABLE;
  1343. ptpconfig->TimestampStatusMode = ((READ_BIT(heth->Instance->MACTSCR,
  1344. ETH_MACTSCR_TXTSSTSM) >> ETH_MACTSCR_TXTSSTSM_Pos) > 0U)
  1345. ? ENABLE : DISABLE;
  1346. /* Return function status */
  1347. return HAL_OK;
  1348. }
  1349. /**
  1350. * @brief Set Seconds and Nanoseconds for the Ethernet PTP registers.
  1351. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1352. * the configuration information for ETHERNET module
  1353. * @param heth: pointer to a ETH_TimeTypeDef structure that contains
  1354. * time to set
  1355. * @retval HAL status
  1356. */
  1357. HAL_StatusTypeDef HAL_ETH_PTP_SetTime(ETH_HandleTypeDef *heth, ETH_TimeTypeDef *time)
  1358. {
  1359. if (heth->IsPtpConfigured == HAL_ETH_PTP_CONFIGURATED)
  1360. {
  1361. /* Set Seconds */
  1362. heth->Instance->MACSTSUR = time->Seconds;
  1363. /* Set NanoSeconds */
  1364. heth->Instance->MACSTNUR = time->NanoSeconds;
  1365. /* Return function status */
  1366. return HAL_OK;
  1367. }
  1368. else
  1369. {
  1370. /* Return function status */
  1371. return HAL_ERROR;
  1372. }
  1373. }
  1374. /**
  1375. * @brief Get Seconds and Nanoseconds for the Ethernet PTP registers.
  1376. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1377. * the configuration information for ETHERNET module
  1378. * @param heth: pointer to a ETH_TimeTypeDef structure that contains
  1379. * time to get
  1380. * @retval HAL status
  1381. */
  1382. HAL_StatusTypeDef HAL_ETH_PTP_GetTime(ETH_HandleTypeDef *heth, ETH_TimeTypeDef *time)
  1383. {
  1384. if (heth->IsPtpConfigured == HAL_ETH_PTP_CONFIGURATED)
  1385. {
  1386. /* Get Seconds */
  1387. time->Seconds = heth->Instance->MACSTSUR;
  1388. /* Get NanoSeconds */
  1389. time->NanoSeconds = heth->Instance->MACSTNUR;
  1390. /* Return function status */
  1391. return HAL_OK;
  1392. }
  1393. else
  1394. {
  1395. /* Return function status */
  1396. return HAL_ERROR;
  1397. }
  1398. }
  1399. /**
  1400. * @brief Update time for the Ethernet PTP registers.
  1401. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1402. * the configuration information for ETHERNET module
  1403. * @param timeupdate: pointer to a ETH_TIMEUPDATETypeDef structure that contains
  1404. * the time update information
  1405. * @retval HAL status
  1406. */
  1407. HAL_StatusTypeDef HAL_ETH_PTP_AddTimeOffset(ETH_HandleTypeDef *heth, ETH_PtpUpdateTypeDef ptpoffsettype,
  1408. ETH_TimeTypeDef *timeoffset)
  1409. {
  1410. if (heth->IsPtpConfigured == HAL_ETH_PTP_CONFIGURATED)
  1411. {
  1412. if (ptpoffsettype == HAL_ETH_PTP_NEGATIVE_UPDATE)
  1413. {
  1414. /* Set Seconds update */
  1415. heth->Instance->MACSTSUR = ETH_MACSTSUR_VALUE - timeoffset->Seconds + 1U;
  1416. if (READ_BIT(heth->Instance->MACTSCR, ETH_MACTSCR_TSCTRLSSR) == ETH_MACTSCR_TSCTRLSSR)
  1417. {
  1418. /* Set nanoSeconds update */
  1419. heth->Instance->MACSTNUR = ETH_MACSTNUR_VALUE - timeoffset->NanoSeconds;
  1420. }
  1421. else
  1422. {
  1423. /* Set nanoSeconds update */
  1424. heth->Instance->MACSTNUR = ETH_MACSTSUR_VALUE - timeoffset->NanoSeconds + 1U;
  1425. }
  1426. }
  1427. else
  1428. {
  1429. /* Set Seconds update */
  1430. heth->Instance->MACSTSUR = timeoffset->Seconds;
  1431. /* Set nanoSeconds update */
  1432. heth->Instance->MACSTNUR = timeoffset->NanoSeconds;
  1433. }
  1434. /* Return function status */
  1435. return HAL_OK;
  1436. }
  1437. else
  1438. {
  1439. /* Return function status */
  1440. return HAL_ERROR;
  1441. }
  1442. }
  1443. /**
  1444. * @brief Insert Timestamp in transmission.
  1445. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1446. * the configuration information for ETHERNET module
  1447. * @param txtimestampconf: Enable or Disable timestamp in transmission
  1448. * @retval HAL status
  1449. */
  1450. HAL_StatusTypeDef HAL_ETH_PTP_InsertTxTimestamp(ETH_HandleTypeDef *heth)
  1451. {
  1452. ETH_TxDescListTypeDef *dmatxdesclist = &heth->TxDescList;
  1453. uint32_t descidx = dmatxdesclist->CurTxDesc;
  1454. ETH_DMADescTypeDef *dmatxdesc = (ETH_DMADescTypeDef *)dmatxdesclist->TxDesc[descidx];
  1455. if (heth->IsPtpConfigured == HAL_ETH_PTP_CONFIGURATED)
  1456. {
  1457. /* Enable Time Stamp transmission */
  1458. SET_BIT(dmatxdesc->DESC2, ETH_DMATXNDESCRF_TTSE);
  1459. /* Return function status */
  1460. return HAL_OK;
  1461. }
  1462. else
  1463. {
  1464. /* Return function status */
  1465. return HAL_ERROR;
  1466. }
  1467. }
  1468. /**
  1469. * @brief Get transmission timestamp.
  1470. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1471. * the configuration information for ETHERNET module
  1472. * @param timestamp: pointer to ETH_TIMESTAMPTypeDef structure that contains
  1473. * transmission timestamp
  1474. * @retval HAL status
  1475. */
  1476. HAL_StatusTypeDef HAL_ETH_PTP_GetTxTimestamp(ETH_HandleTypeDef *heth, ETH_TimeStampTypeDef *timestamp)
  1477. {
  1478. ETH_TxDescListTypeDef *dmatxdesclist = &heth->TxDescList;
  1479. uint32_t idx = dmatxdesclist->releaseIndex;
  1480. ETH_DMADescTypeDef *dmatxdesc = (ETH_DMADescTypeDef *)dmatxdesclist->TxDesc[idx];
  1481. if (heth->IsPtpConfigured == HAL_ETH_PTP_CONFIGURATED)
  1482. {
  1483. /* Get timestamp low */
  1484. timestamp->TimeStampLow = dmatxdesc->DESC0;
  1485. /* Get timestamp high */
  1486. timestamp->TimeStampHigh = dmatxdesc->DESC1;
  1487. /* Return function status */
  1488. return HAL_OK;
  1489. }
  1490. else
  1491. {
  1492. /* Return function status */
  1493. return HAL_ERROR;
  1494. }
  1495. }
  1496. /**
  1497. * @brief Get receive timestamp.
  1498. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1499. * the configuration information for ETHERNET module
  1500. * @param timestamp: pointer to ETH_TIMESTAMPTypeDef structure that contains
  1501. * receive timestamp
  1502. * @retval HAL status
  1503. */
  1504. HAL_StatusTypeDef HAL_ETH_PTP_GetRxTimestamp(ETH_HandleTypeDef *heth, ETH_TimeStampTypeDef *timestamp)
  1505. {
  1506. if (heth->IsPtpConfigured == HAL_ETH_PTP_CONFIGURATED)
  1507. {
  1508. /* Get timestamp low */
  1509. timestamp->TimeStampLow = heth->RxDescList.TimeStamp.TimeStampLow;
  1510. /* Get timestamp high */
  1511. timestamp->TimeStampHigh = heth->RxDescList.TimeStamp.TimeStampHigh;
  1512. /* Return function status */
  1513. return HAL_OK;
  1514. }
  1515. else
  1516. {
  1517. /* Return function status */
  1518. return HAL_ERROR;
  1519. }
  1520. }
  1521. /**
  1522. * @brief Register the Tx Ptp callback.
  1523. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1524. * the configuration information for ETHERNET module
  1525. * @param txPtpCallback: Function to handle Ptp transmission
  1526. * @retval HAL status
  1527. */
  1528. HAL_StatusTypeDef HAL_ETH_RegisterTxPtpCallback(ETH_HandleTypeDef *heth, pETH_txPtpCallbackTypeDef txPtpCallback)
  1529. {
  1530. if (txPtpCallback == NULL)
  1531. {
  1532. /* No buffer to save */
  1533. return HAL_ERROR;
  1534. }
  1535. /* Set Function to handle Tx Ptp */
  1536. heth->txPtpCallback = txPtpCallback;
  1537. return HAL_OK;
  1538. }
  1539. /**
  1540. * @brief Unregister the Tx Ptp callback.
  1541. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1542. * the configuration information for ETHERNET module
  1543. * @retval HAL status
  1544. */
  1545. HAL_StatusTypeDef HAL_ETH_UnRegisterTxPtpCallback(ETH_HandleTypeDef *heth)
  1546. {
  1547. /* Set function to allocate buffer */
  1548. heth->txPtpCallback = HAL_ETH_TxPtpCallback;
  1549. return HAL_OK;
  1550. }
  1551. /**
  1552. * @brief Tx Ptp callback.
  1553. * @param buff: pointer to application buffer
  1554. * @retval None
  1555. */
  1556. __weak void HAL_ETH_TxPtpCallback(uint32_t *buff, ETH_TimeStampTypeDef *timestamp)
  1557. {
  1558. /* Prevent unused argument(s) compilation warning */
  1559. UNUSED(buff);
  1560. /* NOTE : This function Should not be modified, when the callback is needed,
  1561. the HAL_ETH_TxPtpCallback could be implemented in the user file
  1562. */
  1563. }
  1564. #endif /* HAL_ETH_USE_PTP */
  1565. /**
  1566. * @brief This function handles ETH interrupt request.
  1567. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1568. * the configuration information for ETHERNET module
  1569. * @retval HAL status
  1570. */
  1571. void HAL_ETH_IRQHandler(ETH_HandleTypeDef *heth)
  1572. {
  1573. uint32_t macirqenable;
  1574. /* Packet received */
  1575. if (__HAL_ETH_DMA_GET_IT(heth, ETH_DMACSR_RI))
  1576. {
  1577. if (__HAL_ETH_DMA_GET_IT_SOURCE(heth, ETH_DMACIER_RIE))
  1578. {
  1579. /* Clear the Eth DMA Rx IT pending bits */
  1580. __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMACSR_RI | ETH_DMACSR_NIS);
  1581. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  1582. /*Call registered Receive complete callback*/
  1583. heth->RxCpltCallback(heth);
  1584. #else
  1585. /* Receive complete callback */
  1586. HAL_ETH_RxCpltCallback(heth);
  1587. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  1588. }
  1589. }
  1590. /* Packet transmitted */
  1591. if (__HAL_ETH_DMA_GET_IT(heth, ETH_DMACSR_TI))
  1592. {
  1593. if (__HAL_ETH_DMA_GET_IT_SOURCE(heth, ETH_DMACIER_TIE))
  1594. {
  1595. /* Clear the Eth DMA Tx IT pending bits */
  1596. __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMACSR_TI | ETH_DMACSR_NIS);
  1597. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  1598. /*Call registered Transmit complete callback*/
  1599. heth->TxCpltCallback(heth);
  1600. #else
  1601. /* Transfer complete callback */
  1602. HAL_ETH_TxCpltCallback(heth);
  1603. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  1604. }
  1605. }
  1606. /* ETH DMA Error */
  1607. if (__HAL_ETH_DMA_GET_IT(heth, ETH_DMACSR_AIS))
  1608. {
  1609. if (__HAL_ETH_DMA_GET_IT_SOURCE(heth, ETH_DMACIER_AIE))
  1610. {
  1611. heth->ErrorCode |= HAL_ETH_ERROR_DMA;
  1612. /* if fatal bus error occurred */
  1613. if (__HAL_ETH_DMA_GET_IT(heth, ETH_DMACSR_FBE))
  1614. {
  1615. /* Get DMA error code */
  1616. heth->DMAErrorCode = READ_BIT(heth->Instance->DMACSR, (ETH_DMACSR_FBE | ETH_DMACSR_TPS | ETH_DMACSR_RPS));
  1617. /* Disable all interrupts */
  1618. __HAL_ETH_DMA_DISABLE_IT(heth, ETH_DMACIER_NIE | ETH_DMACIER_AIE);
  1619. /* Set HAL state to ERROR */
  1620. heth->gState = HAL_ETH_STATE_ERROR;
  1621. }
  1622. else
  1623. {
  1624. /* Get DMA error status */
  1625. heth->DMAErrorCode = READ_BIT(heth->Instance->DMACSR, (ETH_DMACSR_CDE | ETH_DMACSR_ETI | ETH_DMACSR_RWT |
  1626. ETH_DMACSR_RBU | ETH_DMACSR_AIS));
  1627. /* Clear the interrupt summary flag */
  1628. __HAL_ETH_DMA_CLEAR_IT(heth, (ETH_DMACSR_CDE | ETH_DMACSR_ETI | ETH_DMACSR_RWT |
  1629. ETH_DMACSR_RBU | ETH_DMACSR_AIS));
  1630. }
  1631. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  1632. /* Call registered Error callback*/
  1633. heth->ErrorCallback(heth);
  1634. #else
  1635. /* Ethernet DMA Error callback */
  1636. HAL_ETH_ErrorCallback(heth);
  1637. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  1638. }
  1639. }
  1640. /* ETH MAC Error IT */
  1641. macirqenable = heth->Instance->MACIER;
  1642. if (((macirqenable & ETH_MACIER_RXSTSIE) == ETH_MACIER_RXSTSIE) || \
  1643. ((macirqenable & ETH_MACIER_TXSTSIE) == ETH_MACIER_TXSTSIE))
  1644. {
  1645. heth->ErrorCode |= HAL_ETH_ERROR_MAC;
  1646. /* Get MAC Rx Tx status and clear Status register pending bit */
  1647. heth->MACErrorCode = READ_REG(heth->Instance->MACRXTXSR);
  1648. heth->gState = HAL_ETH_STATE_ERROR;
  1649. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  1650. /* Call registered Error callback*/
  1651. heth->ErrorCallback(heth);
  1652. #else
  1653. /* Ethernet Error callback */
  1654. HAL_ETH_ErrorCallback(heth);
  1655. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  1656. heth->MACErrorCode = (uint32_t)(0x0U);
  1657. }
  1658. /* ETH PMT IT */
  1659. if (__HAL_ETH_MAC_GET_IT(heth, ETH_MAC_PMT_IT))
  1660. {
  1661. /* Get MAC Wake-up source and clear the status register pending bit */
  1662. heth->MACWakeUpEvent = READ_BIT(heth->Instance->MACPCSR, (ETH_MACPCSR_RWKPRCVD | ETH_MACPCSR_MGKPRCVD));
  1663. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  1664. /* Call registered PMT callback*/
  1665. heth->PMTCallback(heth);
  1666. #else
  1667. /* Ethernet PMT callback */
  1668. HAL_ETH_PMTCallback(heth);
  1669. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  1670. heth->MACWakeUpEvent = (uint32_t)(0x0U);
  1671. }
  1672. /* ETH EEE IT */
  1673. if (__HAL_ETH_MAC_GET_IT(heth, ETH_MAC_LPI_IT))
  1674. {
  1675. /* Get MAC LPI interrupt source and clear the status register pending bit */
  1676. heth->MACLPIEvent = READ_BIT(heth->Instance->MACPCSR, 0x0000000FU);
  1677. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  1678. /* Call registered EEE callback*/
  1679. heth->EEECallback(heth);
  1680. #else
  1681. /* Ethernet EEE callback */
  1682. HAL_ETH_EEECallback(heth);
  1683. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  1684. heth->MACLPIEvent = (uint32_t)(0x0U);
  1685. }
  1686. #if defined(DUAL_CORE)
  1687. if (HAL_GetCurrentCPUID() == CM7_CPUID)
  1688. {
  1689. /* check ETH WAKEUP exti flag */
  1690. if (__HAL_ETH_WAKEUP_EXTI_GET_FLAG(ETH_WAKEUP_EXTI_LINE) != (uint32_t)RESET)
  1691. {
  1692. /* Clear ETH WAKEUP Exti pending bit */
  1693. __HAL_ETH_WAKEUP_EXTI_CLEAR_FLAG(ETH_WAKEUP_EXTI_LINE);
  1694. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  1695. /* Call registered WakeUp callback*/
  1696. heth->WakeUpCallback(heth);
  1697. #else
  1698. /* ETH WAKEUP callback */
  1699. HAL_ETH_WakeUpCallback(heth);
  1700. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  1701. }
  1702. }
  1703. else
  1704. {
  1705. /* check ETH WAKEUP exti flag */
  1706. if (__HAL_ETH_WAKEUP_EXTID2_GET_FLAG(ETH_WAKEUP_EXTI_LINE) != (uint32_t)RESET)
  1707. {
  1708. /* Clear ETH WAKEUP Exti pending bit */
  1709. __HAL_ETH_WAKEUP_EXTID2_CLEAR_FLAG(ETH_WAKEUP_EXTI_LINE);
  1710. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  1711. /* Call registered WakeUp callback*/
  1712. heth->WakeUpCallback(heth);
  1713. #else
  1714. /* ETH WAKEUP callback */
  1715. HAL_ETH_WakeUpCallback(heth);
  1716. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  1717. }
  1718. }
  1719. #else /* USE_HAL_ETH_REGISTER_CALLBACKS */
  1720. /* check ETH WAKEUP exti flag */
  1721. if (__HAL_ETH_WAKEUP_EXTI_GET_FLAG(ETH_WAKEUP_EXTI_LINE) != (uint32_t)RESET)
  1722. {
  1723. /* Clear ETH WAKEUP Exti pending bit */
  1724. __HAL_ETH_WAKEUP_EXTI_CLEAR_FLAG(ETH_WAKEUP_EXTI_LINE);
  1725. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  1726. /* Call registered WakeUp callback*/
  1727. heth->WakeUpCallback(heth);
  1728. #else
  1729. /* ETH WAKEUP callback */
  1730. HAL_ETH_WakeUpCallback(heth);
  1731. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  1732. }
  1733. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  1734. }
  1735. /**
  1736. * @brief Tx Transfer completed callbacks.
  1737. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1738. * the configuration information for ETHERNET module
  1739. * @retval None
  1740. */
  1741. __weak void HAL_ETH_TxCpltCallback(ETH_HandleTypeDef *heth)
  1742. {
  1743. /* Prevent unused argument(s) compilation warning */
  1744. UNUSED(heth);
  1745. /* NOTE : This function Should not be modified, when the callback is needed,
  1746. the HAL_ETH_TxCpltCallback could be implemented in the user file
  1747. */
  1748. }
  1749. /**
  1750. * @brief Rx Transfer completed callbacks.
  1751. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1752. * the configuration information for ETHERNET module
  1753. * @retval None
  1754. */
  1755. __weak void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)
  1756. {
  1757. /* Prevent unused argument(s) compilation warning */
  1758. UNUSED(heth);
  1759. /* NOTE : This function Should not be modified, when the callback is needed,
  1760. the HAL_ETH_RxCpltCallback could be implemented in the user file
  1761. */
  1762. }
  1763. /**
  1764. * @brief Ethernet transfer error callbacks
  1765. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1766. * the configuration information for ETHERNET module
  1767. * @retval None
  1768. */
  1769. __weak void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *heth)
  1770. {
  1771. /* Prevent unused argument(s) compilation warning */
  1772. UNUSED(heth);
  1773. /* NOTE : This function Should not be modified, when the callback is needed,
  1774. the HAL_ETH_ErrorCallback could be implemented in the user file
  1775. */
  1776. }
  1777. /**
  1778. * @brief Ethernet Power Management module IT callback
  1779. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1780. * the configuration information for ETHERNET module
  1781. * @retval None
  1782. */
  1783. __weak void HAL_ETH_PMTCallback(ETH_HandleTypeDef *heth)
  1784. {
  1785. /* Prevent unused argument(s) compilation warning */
  1786. UNUSED(heth);
  1787. /* NOTE : This function Should not be modified, when the callback is needed,
  1788. the HAL_ETH_PMTCallback could be implemented in the user file
  1789. */
  1790. }
  1791. /**
  1792. * @brief Energy Efficient Etherent IT callback
  1793. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1794. * the configuration information for ETHERNET module
  1795. * @retval None
  1796. */
  1797. __weak void HAL_ETH_EEECallback(ETH_HandleTypeDef *heth)
  1798. {
  1799. /* Prevent unused argument(s) compilation warning */
  1800. UNUSED(heth);
  1801. /* NOTE : This function Should not be modified, when the callback is needed,
  1802. the HAL_ETH_EEECallback could be implemented in the user file
  1803. */
  1804. }
  1805. /**
  1806. * @brief ETH WAKEUP interrupt callback
  1807. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1808. * the configuration information for ETHERNET module
  1809. * @retval None
  1810. */
  1811. __weak void HAL_ETH_WakeUpCallback(ETH_HandleTypeDef *heth)
  1812. {
  1813. /* Prevent unused argument(s) compilation warning */
  1814. UNUSED(heth);
  1815. /* NOTE : This function Should not be modified, when the callback is needed,
  1816. the HAL_ETH_WakeUpCallback could be implemented in the user file
  1817. */
  1818. }
  1819. /**
  1820. * @brief Read a PHY register
  1821. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1822. * the configuration information for ETHERNET module
  1823. * @param PHYAddr: PHY port address, must be a value from 0 to 31
  1824. * @param PHYReg: PHY register address, must be a value from 0 to 31
  1825. * @param pRegValue: parameter to hold read value
  1826. * @retval HAL status
  1827. */
  1828. HAL_StatusTypeDef HAL_ETH_ReadPHYRegister(ETH_HandleTypeDef *heth, uint32_t PHYAddr, uint32_t PHYReg,
  1829. uint32_t *pRegValue)
  1830. {
  1831. uint32_t tickstart;
  1832. uint32_t tmpreg;
  1833. /* Check for the Busy flag */
  1834. if (READ_BIT(heth->Instance->MACMDIOAR, ETH_MACMDIOAR_MB) != (uint32_t)RESET)
  1835. {
  1836. return HAL_ERROR;
  1837. }
  1838. /* Get the MACMDIOAR value */
  1839. WRITE_REG(tmpreg, heth->Instance->MACMDIOAR);
  1840. /* Prepare the MDIO Address Register value
  1841. - Set the PHY device address
  1842. - Set the PHY register address
  1843. - Set the read mode
  1844. - Set the MII Busy bit */
  1845. MODIFY_REG(tmpreg, ETH_MACMDIOAR_PA, (PHYAddr << 21));
  1846. MODIFY_REG(tmpreg, ETH_MACMDIOAR_RDA, (PHYReg << 16));
  1847. MODIFY_REG(tmpreg, ETH_MACMDIOAR_MOC, ETH_MACMDIOAR_MOC_RD);
  1848. SET_BIT(tmpreg, ETH_MACMDIOAR_MB);
  1849. /* Write the result value into the MDII Address register */
  1850. WRITE_REG(heth->Instance->MACMDIOAR, tmpreg);
  1851. tickstart = HAL_GetTick();
  1852. /* Wait for the Busy flag */
  1853. while (READ_BIT(heth->Instance->MACMDIOAR, ETH_MACMDIOAR_MB) > 0U)
  1854. {
  1855. if (((HAL_GetTick() - tickstart) > ETH_MDIO_BUS_TIMEOUT))
  1856. {
  1857. return HAL_ERROR;
  1858. }
  1859. }
  1860. /* Get MACMIIDR value */
  1861. WRITE_REG(*pRegValue, (uint16_t)heth->Instance->MACMDIODR);
  1862. return HAL_OK;
  1863. }
  1864. /**
  1865. * @brief Writes to a PHY register.
  1866. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1867. * the configuration information for ETHERNET module
  1868. * @param PHYAddr: PHY port address, must be a value from 0 to 31
  1869. * @param PHYReg: PHY register address, must be a value from 0 to 31
  1870. * @param RegValue: the value to write
  1871. * @retval HAL status
  1872. */
  1873. HAL_StatusTypeDef HAL_ETH_WritePHYRegister(ETH_HandleTypeDef *heth, uint32_t PHYAddr, uint32_t PHYReg,
  1874. uint32_t RegValue)
  1875. {
  1876. uint32_t tickstart;
  1877. uint32_t tmpreg;
  1878. /* Check for the Busy flag */
  1879. if (READ_BIT(heth->Instance->MACMDIOAR, ETH_MACMDIOAR_MB) != (uint32_t)RESET)
  1880. {
  1881. return HAL_ERROR;
  1882. }
  1883. /* Get the MACMDIOAR value */
  1884. WRITE_REG(tmpreg, heth->Instance->MACMDIOAR);
  1885. /* Prepare the MDIO Address Register value
  1886. - Set the PHY device address
  1887. - Set the PHY register address
  1888. - Set the write mode
  1889. - Set the MII Busy bit */
  1890. MODIFY_REG(tmpreg, ETH_MACMDIOAR_PA, (PHYAddr << 21));
  1891. MODIFY_REG(tmpreg, ETH_MACMDIOAR_RDA, (PHYReg << 16));
  1892. MODIFY_REG(tmpreg, ETH_MACMDIOAR_MOC, ETH_MACMDIOAR_MOC_WR);
  1893. SET_BIT(tmpreg, ETH_MACMDIOAR_MB);
  1894. /* Give the value to the MII data register */
  1895. WRITE_REG(ETH->MACMDIODR, (uint16_t)RegValue);
  1896. /* Write the result value into the MII Address register */
  1897. WRITE_REG(ETH->MACMDIOAR, tmpreg);
  1898. tickstart = HAL_GetTick();
  1899. /* Wait for the Busy flag */
  1900. while (READ_BIT(heth->Instance->MACMDIOAR, ETH_MACMDIOAR_MB) > 0U)
  1901. {
  1902. if (((HAL_GetTick() - tickstart) > ETH_MDIO_BUS_TIMEOUT))
  1903. {
  1904. return HAL_ERROR;
  1905. }
  1906. }
  1907. return HAL_OK;
  1908. }
  1909. /**
  1910. * @}
  1911. */
  1912. /** @defgroup ETH_Exported_Functions_Group3 Peripheral Control functions
  1913. * @brief ETH control functions
  1914. *
  1915. @verbatim
  1916. ==============================================================================
  1917. ##### Peripheral Control functions #####
  1918. ==============================================================================
  1919. [..]
  1920. This subsection provides a set of functions allowing to control the ETH
  1921. peripheral.
  1922. @endverbatim
  1923. * @{
  1924. */
  1925. /**
  1926. * @brief Get the configuration of the MAC and MTL subsystems.
  1927. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1928. * the configuration information for ETHERNET module
  1929. * @param macconf: pointer to a ETH_MACConfigTypeDef structure that will hold
  1930. * the configuration of the MAC.
  1931. * @retval HAL Status
  1932. */
  1933. HAL_StatusTypeDef HAL_ETH_GetMACConfig(ETH_HandleTypeDef *heth, ETH_MACConfigTypeDef *macconf)
  1934. {
  1935. if (macconf == NULL)
  1936. {
  1937. return HAL_ERROR;
  1938. }
  1939. /* Get MAC parameters */
  1940. macconf->PreambleLength = READ_BIT(heth->Instance->MACCR, ETH_MACCR_PRELEN);
  1941. macconf->DeferralCheck = ((READ_BIT(heth->Instance->MACCR, ETH_MACCR_DC) >> 4) > 0U) ? ENABLE : DISABLE;
  1942. macconf->BackOffLimit = READ_BIT(heth->Instance->MACCR, ETH_MACCR_BL);
  1943. macconf->RetryTransmission = ((READ_BIT(heth->Instance->MACCR, ETH_MACCR_DR) >> 8) == 0U) ? ENABLE : DISABLE;
  1944. macconf->CarrierSenseDuringTransmit = ((READ_BIT(heth->Instance->MACCR, ETH_MACCR_DCRS) >> 9) > 0U)
  1945. ? ENABLE : DISABLE;
  1946. macconf->ReceiveOwn = ((READ_BIT(heth->Instance->MACCR, ETH_MACCR_DO) >> 10) == 0U) ? ENABLE : DISABLE;
  1947. macconf->CarrierSenseBeforeTransmit = ((READ_BIT(heth->Instance->MACCR,
  1948. ETH_MACCR_ECRSFD) >> 11) > 0U) ? ENABLE : DISABLE;
  1949. macconf->LoopbackMode = ((READ_BIT(heth->Instance->MACCR, ETH_MACCR_LM) >> 12) > 0U) ? ENABLE : DISABLE;
  1950. macconf->DuplexMode = READ_BIT(heth->Instance->MACCR, ETH_MACCR_DM);
  1951. macconf->Speed = READ_BIT(heth->Instance->MACCR, ETH_MACCR_FES);
  1952. macconf->JumboPacket = ((READ_BIT(heth->Instance->MACCR, ETH_MACCR_JE) >> 16) > 0U) ? ENABLE : DISABLE;
  1953. macconf->Jabber = ((READ_BIT(heth->Instance->MACCR, ETH_MACCR_JD) >> 17) == 0U) ? ENABLE : DISABLE;
  1954. macconf->Watchdog = ((READ_BIT(heth->Instance->MACCR, ETH_MACCR_WD) >> 19) == 0U) ? ENABLE : DISABLE;
  1955. macconf->AutomaticPadCRCStrip = ((READ_BIT(heth->Instance->MACCR, ETH_MACCR_ACS) >> 20) > 0U) ? ENABLE : DISABLE;
  1956. macconf->CRCStripTypePacket = ((READ_BIT(heth->Instance->MACCR, ETH_MACCR_CST) >> 21) > 0U) ? ENABLE : DISABLE;
  1957. macconf->Support2KPacket = ((READ_BIT(heth->Instance->MACCR, ETH_MACCR_S2KP) >> 22) > 0U) ? ENABLE : DISABLE;
  1958. macconf->GiantPacketSizeLimitControl = ((READ_BIT(heth->Instance->MACCR,
  1959. ETH_MACCR_GPSLCE) >> 23) > 0U) ? ENABLE : DISABLE;
  1960. macconf->InterPacketGapVal = READ_BIT(heth->Instance->MACCR, ETH_MACCR_IPG);
  1961. macconf->ChecksumOffload = ((READ_BIT(heth->Instance->MACCR, ETH_MACCR_IPC) >> 27) > 0U) ? ENABLE : DISABLE;
  1962. macconf->SourceAddrControl = READ_BIT(heth->Instance->MACCR, ETH_MACCR_SARC);
  1963. macconf->GiantPacketSizeLimit = READ_BIT(heth->Instance->MACECR, ETH_MACECR_GPSL);
  1964. macconf->CRCCheckingRxPackets = ((READ_BIT(heth->Instance->MACECR, ETH_MACECR_DCRCC) >> 16) == 0U) ? ENABLE : DISABLE;
  1965. macconf->SlowProtocolDetect = ((READ_BIT(heth->Instance->MACECR, ETH_MACECR_SPEN) >> 17) > 0U) ? ENABLE : DISABLE;
  1966. macconf->UnicastSlowProtocolPacketDetect = ((READ_BIT(heth->Instance->MACECR,
  1967. ETH_MACECR_USP) >> 18) > 0U) ? ENABLE : DISABLE;
  1968. macconf->ExtendedInterPacketGap = ((READ_BIT(heth->Instance->MACECR, ETH_MACECR_EIPGEN) >> 24) > 0U)
  1969. ? ENABLE : DISABLE;
  1970. macconf->ExtendedInterPacketGapVal = READ_BIT(heth->Instance->MACECR, ETH_MACECR_EIPG) >> 25;
  1971. macconf->ProgrammableWatchdog = ((READ_BIT(heth->Instance->MACWTR, ETH_MACWTR_PWE) >> 8) > 0U) ? ENABLE : DISABLE;
  1972. macconf->WatchdogTimeout = READ_BIT(heth->Instance->MACWTR, ETH_MACWTR_WTO);
  1973. macconf->TransmitFlowControl = ((READ_BIT(heth->Instance->MACTFCR, ETH_MACTFCR_TFE) >> 1) > 0U) ? ENABLE : DISABLE;
  1974. macconf->ZeroQuantaPause = ((READ_BIT(heth->Instance->MACTFCR, ETH_MACTFCR_DZPQ) >> 7) == 0U) ? ENABLE : DISABLE;
  1975. macconf->PauseLowThreshold = READ_BIT(heth->Instance->MACTFCR, ETH_MACTFCR_PLT);
  1976. macconf->PauseTime = (READ_BIT(heth->Instance->MACTFCR, ETH_MACTFCR_PT) >> 16);
  1977. macconf->ReceiveFlowControl = (READ_BIT(heth->Instance->MACRFCR, ETH_MACRFCR_RFE) > 0U) ? ENABLE : DISABLE;
  1978. macconf->UnicastPausePacketDetect = ((READ_BIT(heth->Instance->MACRFCR, ETH_MACRFCR_UP) >> 1) > 0U)
  1979. ? ENABLE : DISABLE;
  1980. macconf->TransmitQueueMode = READ_BIT(heth->Instance->MTLTQOMR, (ETH_MTLTQOMR_TTC | ETH_MTLTQOMR_TSF));
  1981. macconf->ReceiveQueueMode = READ_BIT(heth->Instance->MTLRQOMR, (ETH_MTLRQOMR_RTC | ETH_MTLRQOMR_RSF));
  1982. macconf->ForwardRxUndersizedGoodPacket = ((READ_BIT(heth->Instance->MTLRQOMR,
  1983. ETH_MTLRQOMR_FUP) >> 3) > 0U) ? ENABLE : DISABLE;
  1984. macconf->ForwardRxErrorPacket = ((READ_BIT(heth->Instance->MTLRQOMR, ETH_MTLRQOMR_FEP) >> 4) > 0U) ? ENABLE : DISABLE;
  1985. macconf->DropTCPIPChecksumErrorPacket = ((READ_BIT(heth->Instance->MTLRQOMR,
  1986. ETH_MTLRQOMR_DISTCPEF) >> 6) == 0U) ? ENABLE : DISABLE;
  1987. return HAL_OK;
  1988. }
  1989. /**
  1990. * @brief Get the configuration of the DMA.
  1991. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1992. * the configuration information for ETHERNET module
  1993. * @param dmaconf: pointer to a ETH_DMAConfigTypeDef structure that will hold
  1994. * the configuration of the ETH DMA.
  1995. * @retval HAL Status
  1996. */
  1997. HAL_StatusTypeDef HAL_ETH_GetDMAConfig(ETH_HandleTypeDef *heth, ETH_DMAConfigTypeDef *dmaconf)
  1998. {
  1999. if (dmaconf == NULL)
  2000. {
  2001. return HAL_ERROR;
  2002. }
  2003. dmaconf->AddressAlignedBeats = ((READ_BIT(heth->Instance->DMASBMR, ETH_DMASBMR_AAL) >> 12) > 0U) ? ENABLE : DISABLE;
  2004. dmaconf->BurstMode = READ_BIT(heth->Instance->DMASBMR, ETH_DMASBMR_FB | ETH_DMASBMR_MB);
  2005. dmaconf->RebuildINCRxBurst = ((READ_BIT(heth->Instance->DMASBMR, ETH_DMASBMR_RB) >> 15) > 0U) ? ENABLE : DISABLE;
  2006. dmaconf->DMAArbitration = READ_BIT(heth->Instance->DMAMR, (ETH_DMAMR_TXPR | ETH_DMAMR_PR | ETH_DMAMR_DA));
  2007. dmaconf->PBLx8Mode = ((READ_BIT(heth->Instance->DMACCR, ETH_DMACCR_8PBL) >> 16) > 0U) ? ENABLE : DISABLE;
  2008. dmaconf->MaximumSegmentSize = READ_BIT(heth->Instance->DMACCR, ETH_DMACCR_MSS);
  2009. dmaconf->FlushRxPacket = ((READ_BIT(heth->Instance->DMACRCR, ETH_DMACRCR_RPF) >> 31) > 0U) ? ENABLE : DISABLE;
  2010. dmaconf->RxDMABurstLength = READ_BIT(heth->Instance->DMACRCR, ETH_DMACRCR_RPBL);
  2011. dmaconf->SecondPacketOperate = ((READ_BIT(heth->Instance->DMACTCR, ETH_DMACTCR_OSP) >> 4) > 0U) ? ENABLE : DISABLE;
  2012. dmaconf->TCPSegmentation = ((READ_BIT(heth->Instance->DMACTCR, ETH_DMACTCR_TSE) >> 12) > 0U) ? ENABLE : DISABLE;
  2013. dmaconf->TxDMABurstLength = READ_BIT(heth->Instance->DMACTCR, ETH_DMACTCR_TPBL);
  2014. return HAL_OK;
  2015. }
  2016. /**
  2017. * @brief Set the MAC configuration.
  2018. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2019. * the configuration information for ETHERNET module
  2020. * @param macconf: pointer to a ETH_MACConfigTypeDef structure that contains
  2021. * the configuration of the MAC.
  2022. * @retval HAL status
  2023. */
  2024. HAL_StatusTypeDef HAL_ETH_SetMACConfig(ETH_HandleTypeDef *heth, ETH_MACConfigTypeDef *macconf)
  2025. {
  2026. if (macconf == NULL)
  2027. {
  2028. return HAL_ERROR;
  2029. }
  2030. if (heth->gState == HAL_ETH_STATE_READY)
  2031. {
  2032. ETH_SetMACConfig(heth, macconf);
  2033. return HAL_OK;
  2034. }
  2035. else
  2036. {
  2037. return HAL_ERROR;
  2038. }
  2039. }
  2040. /**
  2041. * @brief Set the ETH DMA configuration.
  2042. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2043. * the configuration information for ETHERNET module
  2044. * @param dmaconf: pointer to a ETH_DMAConfigTypeDef structure that will hold
  2045. * the configuration of the ETH DMA.
  2046. * @retval HAL status
  2047. */
  2048. HAL_StatusTypeDef HAL_ETH_SetDMAConfig(ETH_HandleTypeDef *heth, ETH_DMAConfigTypeDef *dmaconf)
  2049. {
  2050. if (dmaconf == NULL)
  2051. {
  2052. return HAL_ERROR;
  2053. }
  2054. if (heth->gState == HAL_ETH_STATE_READY)
  2055. {
  2056. ETH_SetDMAConfig(heth, dmaconf);
  2057. return HAL_OK;
  2058. }
  2059. else
  2060. {
  2061. return HAL_ERROR;
  2062. }
  2063. }
  2064. /**
  2065. * @brief Configures the Clock range of ETH MDIO interface.
  2066. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2067. * the configuration information for ETHERNET module
  2068. * @retval None
  2069. */
  2070. void HAL_ETH_SetMDIOClockRange(ETH_HandleTypeDef *heth)
  2071. {
  2072. uint32_t hclk;
  2073. uint32_t tmpreg;
  2074. /* Get the ETHERNET MACMDIOAR value */
  2075. tmpreg = (heth->Instance)->MACMDIOAR;
  2076. /* Clear CSR Clock Range bits */
  2077. tmpreg &= ~ETH_MACMDIOAR_CR;
  2078. /* Get hclk frequency value */
  2079. hclk = HAL_RCC_GetHCLKFreq();
  2080. /* Set CR bits depending on hclk value */
  2081. if ((hclk >= 20000000U) && (hclk < 35000000U))
  2082. {
  2083. /* CSR Clock Range between 20-35 MHz */
  2084. tmpreg |= (uint32_t)ETH_MACMDIOAR_CR_DIV16;
  2085. }
  2086. else if ((hclk >= 35000000U) && (hclk < 60000000U))
  2087. {
  2088. /* CSR Clock Range between 35-60 MHz */
  2089. tmpreg |= (uint32_t)ETH_MACMDIOAR_CR_DIV26;
  2090. }
  2091. else if ((hclk >= 60000000U) && (hclk < 100000000U))
  2092. {
  2093. /* CSR Clock Range between 60-100 MHz */
  2094. tmpreg |= (uint32_t)ETH_MACMDIOAR_CR_DIV42;
  2095. }
  2096. else if ((hclk >= 100000000U) && (hclk < 150000000U))
  2097. {
  2098. /* CSR Clock Range between 100-150 MHz */
  2099. tmpreg |= (uint32_t)ETH_MACMDIOAR_CR_DIV62;
  2100. }
  2101. else /* (hclk >= 150000000)&&(hclk <= 200000000) */
  2102. {
  2103. /* CSR Clock Range between 150-200 MHz */
  2104. tmpreg |= (uint32_t)ETH_MACMDIOAR_CR_DIV102;
  2105. }
  2106. /* Configure the CSR Clock Range */
  2107. (heth->Instance)->MACMDIOAR = (uint32_t)tmpreg;
  2108. }
  2109. /**
  2110. * @brief Set the ETH MAC (L2) Filters configuration.
  2111. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2112. * the configuration information for ETHERNET module
  2113. * @param pFilterConfig: pointer to a ETH_MACFilterConfigTypeDef structure that contains
  2114. * the configuration of the ETH MAC filters.
  2115. * @retval HAL status
  2116. */
  2117. HAL_StatusTypeDef HAL_ETH_SetMACFilterConfig(ETH_HandleTypeDef *heth, ETH_MACFilterConfigTypeDef *pFilterConfig)
  2118. {
  2119. uint32_t filterconfig;
  2120. if (pFilterConfig == NULL)
  2121. {
  2122. return HAL_ERROR;
  2123. }
  2124. filterconfig = ((uint32_t)pFilterConfig->PromiscuousMode |
  2125. ((uint32_t)pFilterConfig->HashUnicast << 1) |
  2126. ((uint32_t)pFilterConfig->HashMulticast << 2) |
  2127. ((uint32_t)pFilterConfig->DestAddrInverseFiltering << 3) |
  2128. ((uint32_t)pFilterConfig->PassAllMulticast << 4) |
  2129. ((uint32_t)((pFilterConfig->BroadcastFilter == DISABLE) ? 1U : 0U) << 5) |
  2130. ((uint32_t)pFilterConfig->SrcAddrInverseFiltering << 8) |
  2131. ((uint32_t)pFilterConfig->SrcAddrFiltering << 9) |
  2132. ((uint32_t)pFilterConfig->HachOrPerfectFilter << 10) |
  2133. ((uint32_t)pFilterConfig->ReceiveAllMode << 31) |
  2134. pFilterConfig->ControlPacketsFilter);
  2135. MODIFY_REG(heth->Instance->MACPFR, ETH_MACPFR_MASK, filterconfig);
  2136. return HAL_OK;
  2137. }
  2138. /**
  2139. * @brief Get the ETH MAC (L2) Filters configuration.
  2140. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2141. * the configuration information for ETHERNET module
  2142. * @param pFilterConfig: pointer to a ETH_MACFilterConfigTypeDef structure that will hold
  2143. * the configuration of the ETH MAC filters.
  2144. * @retval HAL status
  2145. */
  2146. HAL_StatusTypeDef HAL_ETH_GetMACFilterConfig(ETH_HandleTypeDef *heth, ETH_MACFilterConfigTypeDef *pFilterConfig)
  2147. {
  2148. if (pFilterConfig == NULL)
  2149. {
  2150. return HAL_ERROR;
  2151. }
  2152. pFilterConfig->PromiscuousMode = ((READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_PR)) > 0U) ? ENABLE : DISABLE;
  2153. pFilterConfig->HashUnicast = ((READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_HUC) >> 1) > 0U) ? ENABLE : DISABLE;
  2154. pFilterConfig->HashMulticast = ((READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_HMC) >> 2) > 0U) ? ENABLE : DISABLE;
  2155. pFilterConfig->DestAddrInverseFiltering = ((READ_BIT(heth->Instance->MACPFR,
  2156. ETH_MACPFR_DAIF) >> 3) > 0U) ? ENABLE : DISABLE;
  2157. pFilterConfig->PassAllMulticast = ((READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_PM) >> 4) > 0U) ? ENABLE : DISABLE;
  2158. pFilterConfig->BroadcastFilter = ((READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_DBF) >> 5) == 0U) ? ENABLE : DISABLE;
  2159. pFilterConfig->ControlPacketsFilter = READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_PCF);
  2160. pFilterConfig->SrcAddrInverseFiltering = ((READ_BIT(heth->Instance->MACPFR,
  2161. ETH_MACPFR_SAIF) >> 8) > 0U) ? ENABLE : DISABLE;
  2162. pFilterConfig->SrcAddrFiltering = ((READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_SAF) >> 9) > 0U) ? ENABLE : DISABLE;
  2163. pFilterConfig->HachOrPerfectFilter = ((READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_HPF) >> 10) > 0U)
  2164. ? ENABLE : DISABLE;
  2165. pFilterConfig->ReceiveAllMode = ((READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_RA) >> 31) > 0U) ? ENABLE : DISABLE;
  2166. return HAL_OK;
  2167. }
  2168. /**
  2169. * @brief Set the source MAC Address to be matched.
  2170. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2171. * the configuration information for ETHERNET module
  2172. * @param AddrNbr: The MAC address to configure
  2173. * This parameter must be a value of the following:
  2174. * ETH_MAC_ADDRESS1
  2175. * ETH_MAC_ADDRESS2
  2176. * ETH_MAC_ADDRESS3
  2177. * @param pMACAddr: Pointer to MAC address buffer data (6 bytes)
  2178. * @retval HAL status
  2179. */
  2180. HAL_StatusTypeDef HAL_ETH_SetSourceMACAddrMatch(ETH_HandleTypeDef *heth, uint32_t AddrNbr, uint8_t *pMACAddr)
  2181. {
  2182. uint32_t macaddrlr;
  2183. uint32_t macaddrhr;
  2184. if (pMACAddr == NULL)
  2185. {
  2186. return HAL_ERROR;
  2187. }
  2188. /* Get mac addr high reg offset */
  2189. macaddrhr = ((uint32_t) &(heth->Instance->MACA0HR) + AddrNbr);
  2190. /* Get mac addr low reg offset */
  2191. macaddrlr = ((uint32_t) &(heth->Instance->MACA0LR) + AddrNbr);
  2192. /* Set MAC addr bits 32 to 47 */
  2193. (*(__IO uint32_t *)macaddrhr) = (((uint32_t)(pMACAddr[5]) << 8) | (uint32_t)pMACAddr[4]);
  2194. /* Set MAC addr bits 0 to 31 */
  2195. (*(__IO uint32_t *)macaddrlr) = (((uint32_t)(pMACAddr[3]) << 24) | ((uint32_t)(pMACAddr[2]) << 16) |
  2196. ((uint32_t)(pMACAddr[1]) << 8) | (uint32_t)pMACAddr[0]);
  2197. /* Enable address and set source address bit */
  2198. (*(__IO uint32_t *)macaddrhr) |= (ETH_MACAHR_SA | ETH_MACAHR_AE);
  2199. return HAL_OK;
  2200. }
  2201. /**
  2202. * @brief Set the ETH Hash Table Value.
  2203. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2204. * the configuration information for ETHERNET module
  2205. * @param pHashTable: pointer to a table of two 32 bit values, that contains
  2206. * the 64 bits of the hash table.
  2207. * @retval HAL status
  2208. */
  2209. HAL_StatusTypeDef HAL_ETH_SetHashTable(ETH_HandleTypeDef *heth, uint32_t *pHashTable)
  2210. {
  2211. if (pHashTable == NULL)
  2212. {
  2213. return HAL_ERROR;
  2214. }
  2215. heth->Instance->MACHT0R = pHashTable[0];
  2216. heth->Instance->MACHT1R = pHashTable[1];
  2217. return HAL_OK;
  2218. }
  2219. /**
  2220. * @brief Set the VLAN Identifier for Rx packets
  2221. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2222. * the configuration information for ETHERNET module
  2223. * @param ComparisonBits: 12 or 16 bit comparison mode
  2224. must be a value of @ref ETH_VLAN_Tag_Comparison
  2225. * @param VLANIdentifier: VLAN Identifier value
  2226. * @retval None
  2227. */
  2228. void HAL_ETH_SetRxVLANIdentifier(ETH_HandleTypeDef *heth, uint32_t ComparisonBits, uint32_t VLANIdentifier)
  2229. {
  2230. if (ComparisonBits == ETH_VLANTAGCOMPARISON_16BIT)
  2231. {
  2232. MODIFY_REG(heth->Instance->MACVTR, ETH_MACVTR_VL, VLANIdentifier);
  2233. CLEAR_BIT(heth->Instance->MACVTR, ETH_MACVTR_ETV);
  2234. }
  2235. else
  2236. {
  2237. MODIFY_REG(heth->Instance->MACVTR, ETH_MACVTR_VL_VID, VLANIdentifier);
  2238. SET_BIT(heth->Instance->MACVTR, ETH_MACVTR_ETV);
  2239. }
  2240. }
  2241. /**
  2242. * @brief Enters the Power down mode.
  2243. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2244. * the configuration information for ETHERNET module
  2245. * @param pPowerDownConfig: a pointer to ETH_PowerDownConfigTypeDef structure
  2246. * that contains the Power Down configuration
  2247. * @retval None.
  2248. */
  2249. void HAL_ETH_EnterPowerDownMode(ETH_HandleTypeDef *heth, ETH_PowerDownConfigTypeDef *pPowerDownConfig)
  2250. {
  2251. uint32_t powerdownconfig;
  2252. powerdownconfig = (((uint32_t)pPowerDownConfig->MagicPacket << 1) |
  2253. ((uint32_t)pPowerDownConfig->WakeUpPacket << 2) |
  2254. ((uint32_t)pPowerDownConfig->GlobalUnicast << 9) |
  2255. ((uint32_t)pPowerDownConfig->WakeUpForward << 10) |
  2256. ETH_MACPCSR_PWRDWN);
  2257. /* Enable PMT interrupt */
  2258. __HAL_ETH_MAC_ENABLE_IT(heth, ETH_MACIER_PMTIE);
  2259. MODIFY_REG(heth->Instance->MACPCSR, ETH_MACPCSR_MASK, powerdownconfig);
  2260. }
  2261. /**
  2262. * @brief Exits from the Power down mode.
  2263. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2264. * the configuration information for ETHERNET module
  2265. * @retval None.
  2266. */
  2267. void HAL_ETH_ExitPowerDownMode(ETH_HandleTypeDef *heth)
  2268. {
  2269. /* clear wake up sources */
  2270. CLEAR_BIT(heth->Instance->MACPCSR, ETH_MACPCSR_RWKPKTEN | ETH_MACPCSR_MGKPKTEN | ETH_MACPCSR_GLBLUCAST |
  2271. ETH_MACPCSR_RWKPFE);
  2272. if (READ_BIT(heth->Instance->MACPCSR, ETH_MACPCSR_PWRDWN) != (uint32_t)RESET)
  2273. {
  2274. /* Exit power down mode */
  2275. CLEAR_BIT(heth->Instance->MACPCSR, ETH_MACPCSR_PWRDWN);
  2276. }
  2277. /* Disable PMT interrupt */
  2278. __HAL_ETH_MAC_DISABLE_IT(heth, ETH_MACIER_PMTIE);
  2279. }
  2280. /**
  2281. * @brief Set the WakeUp filter.
  2282. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2283. * the configuration information for ETHERNET module
  2284. * @param pFilter: pointer to filter registers values
  2285. * @param Count: number of filter registers, must be from 1 to 8.
  2286. * @retval None.
  2287. */
  2288. HAL_StatusTypeDef HAL_ETH_SetWakeUpFilter(ETH_HandleTypeDef *heth, uint32_t *pFilter, uint32_t Count)
  2289. {
  2290. uint32_t regindex;
  2291. if (pFilter == NULL)
  2292. {
  2293. return HAL_ERROR;
  2294. }
  2295. /* Reset Filter Pointer */
  2296. SET_BIT(heth->Instance->MACPCSR, ETH_MACPCSR_RWKFILTRST);
  2297. /* Wake up packet filter config */
  2298. for (regindex = 0; regindex < Count; regindex++)
  2299. {
  2300. /* Write filter regs */
  2301. WRITE_REG(heth->Instance->MACRWKPFR, pFilter[regindex]);
  2302. }
  2303. return HAL_OK;
  2304. }
  2305. /**
  2306. * @}
  2307. */
  2308. /** @defgroup ETH_Exported_Functions_Group4 Peripheral State and Errors functions
  2309. * @brief ETH State and Errors functions
  2310. *
  2311. @verbatim
  2312. ==============================================================================
  2313. ##### Peripheral State and Errors functions #####
  2314. ==============================================================================
  2315. [..]
  2316. This subsection provides a set of functions allowing to return the State of
  2317. ETH communication process, return Peripheral Errors occurred during communication
  2318. process
  2319. @endverbatim
  2320. * @{
  2321. */
  2322. /**
  2323. * @brief Returns the ETH state.
  2324. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2325. * the configuration information for ETHERNET module
  2326. * @retval HAL state
  2327. */
  2328. HAL_ETH_StateTypeDef HAL_ETH_GetState(ETH_HandleTypeDef *heth)
  2329. {
  2330. return heth->gState;
  2331. }
  2332. /**
  2333. * @brief Returns the ETH error code
  2334. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2335. * the configuration information for ETHERNET module
  2336. * @retval ETH Error Code
  2337. */
  2338. uint32_t HAL_ETH_GetError(ETH_HandleTypeDef *heth)
  2339. {
  2340. return heth->ErrorCode;
  2341. }
  2342. /**
  2343. * @brief Returns the ETH DMA error code
  2344. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2345. * the configuration information for ETHERNET module
  2346. * @retval ETH DMA Error Code
  2347. */
  2348. uint32_t HAL_ETH_GetDMAError(ETH_HandleTypeDef *heth)
  2349. {
  2350. return heth->DMAErrorCode;
  2351. }
  2352. /**
  2353. * @brief Returns the ETH MAC error code
  2354. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2355. * the configuration information for ETHERNET module
  2356. * @retval ETH MAC Error Code
  2357. */
  2358. uint32_t HAL_ETH_GetMACError(ETH_HandleTypeDef *heth)
  2359. {
  2360. return heth->MACErrorCode;
  2361. }
  2362. /**
  2363. * @brief Returns the ETH MAC WakeUp event source
  2364. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2365. * the configuration information for ETHERNET module
  2366. * @retval ETH MAC WakeUp event source
  2367. */
  2368. uint32_t HAL_ETH_GetMACWakeUpSource(ETH_HandleTypeDef *heth)
  2369. {
  2370. return heth->MACWakeUpEvent;
  2371. }
  2372. /**
  2373. * @}
  2374. */
  2375. /**
  2376. * @}
  2377. */
  2378. /** @addtogroup ETH_Private_Functions ETH Private Functions
  2379. * @{
  2380. */
  2381. static void ETH_SetMACConfig(ETH_HandleTypeDef *heth, ETH_MACConfigTypeDef *macconf)
  2382. {
  2383. uint32_t macregval;
  2384. /*------------------------ MACCR Configuration --------------------*/
  2385. macregval = (macconf->InterPacketGapVal |
  2386. macconf->SourceAddrControl |
  2387. ((uint32_t)macconf->ChecksumOffload << 27) |
  2388. ((uint32_t)macconf->GiantPacketSizeLimitControl << 23) |
  2389. ((uint32_t)macconf->Support2KPacket << 22) |
  2390. ((uint32_t)macconf->CRCStripTypePacket << 21) |
  2391. ((uint32_t)macconf->AutomaticPadCRCStrip << 20) |
  2392. ((uint32_t)((macconf->Watchdog == DISABLE) ? 1U : 0U) << 19) |
  2393. ((uint32_t)((macconf->Jabber == DISABLE) ? 1U : 0U) << 17) |
  2394. ((uint32_t)macconf->JumboPacket << 16) |
  2395. macconf->Speed |
  2396. macconf->DuplexMode |
  2397. ((uint32_t)macconf->LoopbackMode << 12) |
  2398. ((uint32_t)macconf->CarrierSenseBeforeTransmit << 11) |
  2399. ((uint32_t)((macconf->ReceiveOwn == DISABLE) ? 1U : 0U) << 10) |
  2400. ((uint32_t)macconf->CarrierSenseDuringTransmit << 9) |
  2401. ((uint32_t)((macconf->RetryTransmission == DISABLE) ? 1U : 0U) << 8) |
  2402. macconf->BackOffLimit |
  2403. ((uint32_t)macconf->DeferralCheck << 4) |
  2404. macconf->PreambleLength);
  2405. /* Write to MACCR */
  2406. MODIFY_REG(heth->Instance->MACCR, ETH_MACCR_MASK, macregval);
  2407. /*------------------------ MACECR Configuration --------------------*/
  2408. macregval = ((macconf->ExtendedInterPacketGapVal << 25) |
  2409. ((uint32_t)macconf->ExtendedInterPacketGap << 24) |
  2410. ((uint32_t)macconf->UnicastSlowProtocolPacketDetect << 18) |
  2411. ((uint32_t)macconf->SlowProtocolDetect << 17) |
  2412. ((uint32_t)((macconf->CRCCheckingRxPackets == DISABLE) ? 1U : 0U) << 16) |
  2413. macconf->GiantPacketSizeLimit);
  2414. /* Write to MACECR */
  2415. MODIFY_REG(heth->Instance->MACECR, ETH_MACECR_MASK, macregval);
  2416. /*------------------------ MACWTR Configuration --------------------*/
  2417. macregval = (((uint32_t)macconf->ProgrammableWatchdog << 8) |
  2418. macconf->WatchdogTimeout);
  2419. /* Write to MACWTR */
  2420. MODIFY_REG(heth->Instance->MACWTR, ETH_MACWTR_MASK, macregval);
  2421. /*------------------------ MACTFCR Configuration --------------------*/
  2422. macregval = (((uint32_t)macconf->TransmitFlowControl << 1) |
  2423. macconf->PauseLowThreshold |
  2424. ((uint32_t)((macconf->ZeroQuantaPause == DISABLE) ? 1U : 0U) << 7) |
  2425. (macconf->PauseTime << 16));
  2426. /* Write to MACTFCR */
  2427. MODIFY_REG(heth->Instance->MACTFCR, ETH_MACTFCR_MASK, macregval);
  2428. /*------------------------ MACRFCR Configuration --------------------*/
  2429. macregval = ((uint32_t)macconf->ReceiveFlowControl |
  2430. ((uint32_t)macconf->UnicastPausePacketDetect << 1));
  2431. /* Write to MACRFCR */
  2432. MODIFY_REG(heth->Instance->MACRFCR, ETH_MACRFCR_MASK, macregval);
  2433. /*------------------------ MTLTQOMR Configuration --------------------*/
  2434. /* Write to MTLTQOMR */
  2435. MODIFY_REG(heth->Instance->MTLTQOMR, ETH_MTLTQOMR_MASK, macconf->TransmitQueueMode);
  2436. /*------------------------ MTLRQOMR Configuration --------------------*/
  2437. macregval = (macconf->ReceiveQueueMode |
  2438. ((uint32_t)((macconf->DropTCPIPChecksumErrorPacket == DISABLE) ? 1U : 0U) << 6) |
  2439. ((uint32_t)macconf->ForwardRxErrorPacket << 4) |
  2440. ((uint32_t)macconf->ForwardRxUndersizedGoodPacket << 3));
  2441. /* Write to MTLRQOMR */
  2442. MODIFY_REG(heth->Instance->MTLRQOMR, ETH_MTLRQOMR_MASK, macregval);
  2443. }
  2444. static void ETH_SetDMAConfig(ETH_HandleTypeDef *heth, ETH_DMAConfigTypeDef *dmaconf)
  2445. {
  2446. uint32_t dmaregval;
  2447. /*------------------------ DMAMR Configuration --------------------*/
  2448. MODIFY_REG(heth->Instance->DMAMR, ETH_DMAMR_MASK, dmaconf->DMAArbitration);
  2449. /*------------------------ DMASBMR Configuration --------------------*/
  2450. dmaregval = (((uint32_t)dmaconf->AddressAlignedBeats << 12) |
  2451. dmaconf->BurstMode |
  2452. ((uint32_t)dmaconf->RebuildINCRxBurst << 15));
  2453. MODIFY_REG(heth->Instance->DMASBMR, ETH_DMASBMR_MASK, dmaregval);
  2454. /*------------------------ DMACCR Configuration --------------------*/
  2455. dmaregval = (((uint32_t)dmaconf->PBLx8Mode << 16) |
  2456. dmaconf->MaximumSegmentSize);
  2457. MODIFY_REG(heth->Instance->DMACCR, ETH_DMACCR_MASK, dmaregval);
  2458. /*------------------------ DMACTCR Configuration --------------------*/
  2459. dmaregval = (dmaconf->TxDMABurstLength |
  2460. ((uint32_t)dmaconf->SecondPacketOperate << 4) |
  2461. ((uint32_t)dmaconf->TCPSegmentation << 12));
  2462. MODIFY_REG(heth->Instance->DMACTCR, ETH_DMACTCR_MASK, dmaregval);
  2463. /*------------------------ DMACRCR Configuration --------------------*/
  2464. dmaregval = (((uint32_t)dmaconf->FlushRxPacket << 31) |
  2465. dmaconf->RxDMABurstLength);
  2466. /* Write to DMACRCR */
  2467. MODIFY_REG(heth->Instance->DMACRCR, ETH_DMACRCR_MASK, dmaregval);
  2468. }
  2469. /**
  2470. * @brief Configures Ethernet MAC and DMA with default parameters.
  2471. * called by HAL_ETH_Init() API.
  2472. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2473. * the configuration information for ETHERNET module
  2474. * @retval HAL status
  2475. */
  2476. static void ETH_MACDMAConfig(ETH_HandleTypeDef *heth)
  2477. {
  2478. ETH_MACConfigTypeDef macDefaultConf;
  2479. ETH_DMAConfigTypeDef dmaDefaultConf;
  2480. /*--------------- ETHERNET MAC registers default Configuration --------------*/
  2481. macDefaultConf.AutomaticPadCRCStrip = ENABLE;
  2482. macDefaultConf.BackOffLimit = ETH_BACKOFFLIMIT_10;
  2483. macDefaultConf.CarrierSenseBeforeTransmit = DISABLE;
  2484. macDefaultConf.CarrierSenseDuringTransmit = DISABLE;
  2485. macDefaultConf.ChecksumOffload = ENABLE;
  2486. macDefaultConf.CRCCheckingRxPackets = ENABLE;
  2487. macDefaultConf.CRCStripTypePacket = ENABLE;
  2488. macDefaultConf.DeferralCheck = DISABLE;
  2489. macDefaultConf.DropTCPIPChecksumErrorPacket = ENABLE;
  2490. macDefaultConf.DuplexMode = ETH_FULLDUPLEX_MODE;
  2491. macDefaultConf.ExtendedInterPacketGap = DISABLE;
  2492. macDefaultConf.ExtendedInterPacketGapVal = 0x0;
  2493. macDefaultConf.ForwardRxErrorPacket = DISABLE;
  2494. macDefaultConf.ForwardRxUndersizedGoodPacket = DISABLE;
  2495. macDefaultConf.GiantPacketSizeLimit = 0x618;
  2496. macDefaultConf.GiantPacketSizeLimitControl = DISABLE;
  2497. macDefaultConf.InterPacketGapVal = ETH_INTERPACKETGAP_96BIT;
  2498. macDefaultConf.Jabber = ENABLE;
  2499. macDefaultConf.JumboPacket = DISABLE;
  2500. macDefaultConf.LoopbackMode = DISABLE;
  2501. macDefaultConf.PauseLowThreshold = ETH_PAUSELOWTHRESHOLD_MINUS_4;
  2502. macDefaultConf.PauseTime = 0x0;
  2503. macDefaultConf.PreambleLength = ETH_PREAMBLELENGTH_7;
  2504. macDefaultConf.ProgrammableWatchdog = DISABLE;
  2505. macDefaultConf.ReceiveFlowControl = DISABLE;
  2506. macDefaultConf.ReceiveOwn = ENABLE;
  2507. macDefaultConf.ReceiveQueueMode = ETH_RECEIVESTOREFORWARD;
  2508. macDefaultConf.RetryTransmission = ENABLE;
  2509. macDefaultConf.SlowProtocolDetect = DISABLE;
  2510. macDefaultConf.SourceAddrControl = ETH_SOURCEADDRESS_REPLACE_ADDR0;
  2511. macDefaultConf.Speed = ETH_SPEED_100M;
  2512. macDefaultConf.Support2KPacket = DISABLE;
  2513. macDefaultConf.TransmitQueueMode = ETH_TRANSMITSTOREFORWARD;
  2514. macDefaultConf.TransmitFlowControl = DISABLE;
  2515. macDefaultConf.UnicastPausePacketDetect = DISABLE;
  2516. macDefaultConf.UnicastSlowProtocolPacketDetect = DISABLE;
  2517. macDefaultConf.Watchdog = ENABLE;
  2518. macDefaultConf.WatchdogTimeout = ETH_MACWTR_WTO_2KB;
  2519. macDefaultConf.ZeroQuantaPause = ENABLE;
  2520. /* MAC default configuration */
  2521. ETH_SetMACConfig(heth, &macDefaultConf);
  2522. /*--------------- ETHERNET DMA registers default Configuration --------------*/
  2523. dmaDefaultConf.AddressAlignedBeats = ENABLE;
  2524. dmaDefaultConf.BurstMode = ETH_BURSTLENGTH_FIXED;
  2525. dmaDefaultConf.DMAArbitration = ETH_DMAARBITRATION_RX1_TX1;
  2526. dmaDefaultConf.FlushRxPacket = DISABLE;
  2527. dmaDefaultConf.PBLx8Mode = DISABLE;
  2528. dmaDefaultConf.RebuildINCRxBurst = DISABLE;
  2529. dmaDefaultConf.RxDMABurstLength = ETH_RXDMABURSTLENGTH_32BEAT;
  2530. dmaDefaultConf.SecondPacketOperate = DISABLE;
  2531. dmaDefaultConf.TxDMABurstLength = ETH_TXDMABURSTLENGTH_32BEAT;
  2532. dmaDefaultConf.TCPSegmentation = DISABLE;
  2533. dmaDefaultConf.MaximumSegmentSize = ETH_SEGMENT_SIZE_DEFAULT;
  2534. /* DMA default configuration */
  2535. ETH_SetDMAConfig(heth, &dmaDefaultConf);
  2536. }
  2537. /**
  2538. * @brief Initializes the DMA Tx descriptors.
  2539. * called by HAL_ETH_Init() API.
  2540. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2541. * the configuration information for ETHERNET module
  2542. * @retval None
  2543. */
  2544. static void ETH_DMATxDescListInit(ETH_HandleTypeDef *heth)
  2545. {
  2546. ETH_DMADescTypeDef *dmatxdesc;
  2547. uint32_t i;
  2548. /* Fill each DMATxDesc descriptor with the right values */
  2549. for (i = 0; i < (uint32_t)ETH_TX_DESC_CNT; i++)
  2550. {
  2551. dmatxdesc = heth->Init.TxDesc + i;
  2552. WRITE_REG(dmatxdesc->DESC0, 0x0);
  2553. WRITE_REG(dmatxdesc->DESC1, 0x0);
  2554. WRITE_REG(dmatxdesc->DESC2, 0x0);
  2555. WRITE_REG(dmatxdesc->DESC3, 0x0);
  2556. WRITE_REG(heth->TxDescList.TxDesc[i], (uint32_t)dmatxdesc);
  2557. }
  2558. heth->TxDescList.CurTxDesc = 0;
  2559. /* Set Transmit Descriptor Ring Length */
  2560. WRITE_REG(heth->Instance->DMACTDRLR, (ETH_TX_DESC_CNT - 1U));
  2561. /* Set Transmit Descriptor List Address */
  2562. WRITE_REG(heth->Instance->DMACTDLAR, (uint32_t) heth->Init.TxDesc);
  2563. /* Set Transmit Descriptor Tail pointer */
  2564. WRITE_REG(heth->Instance->DMACTDTPR, (uint32_t) heth->Init.TxDesc);
  2565. }
  2566. /**
  2567. * @brief Initializes the DMA Rx descriptors in chain mode.
  2568. * called by HAL_ETH_Init() API.
  2569. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2570. * the configuration information for ETHERNET module
  2571. * @retval None
  2572. */
  2573. static void ETH_DMARxDescListInit(ETH_HandleTypeDef *heth)
  2574. {
  2575. ETH_DMADescTypeDef *dmarxdesc;
  2576. uint32_t i;
  2577. for (i = 0; i < (uint32_t)ETH_RX_DESC_CNT; i++)
  2578. {
  2579. dmarxdesc = heth->Init.RxDesc + i;
  2580. WRITE_REG(dmarxdesc->DESC0, 0x0);
  2581. WRITE_REG(dmarxdesc->DESC1, 0x0);
  2582. WRITE_REG(dmarxdesc->DESC2, 0x0);
  2583. WRITE_REG(dmarxdesc->DESC3, 0x0);
  2584. WRITE_REG(dmarxdesc->BackupAddr0, 0x0);
  2585. WRITE_REG(dmarxdesc->BackupAddr1, 0x0);
  2586. /* Set Rx descritors addresses */
  2587. WRITE_REG(heth->RxDescList.RxDesc[i], (uint32_t)dmarxdesc);
  2588. }
  2589. WRITE_REG(heth->RxDescList.RxDescIdx, 0);
  2590. WRITE_REG(heth->RxDescList.RxDescCnt, 0);
  2591. WRITE_REG(heth->RxDescList.RxBuildDescIdx, 0);
  2592. WRITE_REG(heth->RxDescList.RxBuildDescCnt, 0);
  2593. WRITE_REG(heth->RxDescList.ItMode, 0);
  2594. /* Set Receive Descriptor Ring Length */
  2595. WRITE_REG(heth->Instance->DMACRDRLR, ((uint32_t)(ETH_RX_DESC_CNT - 1U)));
  2596. /* Set Receive Descriptor List Address */
  2597. WRITE_REG(heth->Instance->DMACRDLAR, (uint32_t) heth->Init.RxDesc);
  2598. /* Set Receive Descriptor Tail pointer Address */
  2599. WRITE_REG(heth->Instance->DMACRDTPR, ((uint32_t)(heth->Init.RxDesc + (uint32_t)(ETH_RX_DESC_CNT - 1U))));
  2600. }
  2601. /**
  2602. * @brief Prepare Tx DMA descriptor before transmission.
  2603. * called by HAL_ETH_Transmit_IT and HAL_ETH_Transmit_IT() API.
  2604. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  2605. * the configuration information for ETHERNET module
  2606. * @param pTxConfig: Tx packet configuration
  2607. * @param ItMode: Enable or disable Tx EOT interrept
  2608. * @retval Status
  2609. */
  2610. static uint32_t ETH_Prepare_Tx_Descriptors(ETH_HandleTypeDef *heth, ETH_TxPacketConfig *pTxConfig, uint32_t ItMode)
  2611. {
  2612. ETH_TxDescListTypeDef *dmatxdesclist = &heth->TxDescList;
  2613. uint32_t descidx = dmatxdesclist->CurTxDesc;
  2614. uint32_t firstdescidx = dmatxdesclist->CurTxDesc;
  2615. uint32_t idx;
  2616. uint32_t descnbr = 0;
  2617. ETH_DMADescTypeDef *dmatxdesc = (ETH_DMADescTypeDef *)dmatxdesclist->TxDesc[descidx];
  2618. ETH_BufferTypeDef *txbuffer = pTxConfig->TxBuffer;
  2619. uint32_t bd_count = 0;
  2620. /* Current Tx Descriptor Owned by DMA: cannot be used by the application */
  2621. if ((READ_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCWBF_OWN) == ETH_DMATXNDESCWBF_OWN)
  2622. || (dmatxdesclist->PacketAddress[descidx] != NULL))
  2623. {
  2624. return HAL_ETH_ERROR_BUSY;
  2625. }
  2626. /***************************************************************************/
  2627. /***************** Context descriptor configuration (Optional) **********/
  2628. /***************************************************************************/
  2629. /* If VLAN tag is enabled for this packet */
  2630. if (READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_VLANTAG) != (uint32_t)RESET)
  2631. {
  2632. /* Set vlan tag value */
  2633. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXCDESC_VT, pTxConfig->VlanTag);
  2634. /* Set vlan tag valid bit */
  2635. SET_BIT(dmatxdesc->DESC3, ETH_DMATXCDESC_VLTV);
  2636. /* Set the descriptor as the vlan input source */
  2637. SET_BIT(heth->Instance->MACVIR, ETH_MACVIR_VLTI);
  2638. /* if inner VLAN is enabled */
  2639. if (READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_INNERVLANTAG) != (uint32_t)RESET)
  2640. {
  2641. /* Set inner vlan tag value */
  2642. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXCDESC_IVT, (pTxConfig->InnerVlanTag << 16));
  2643. /* Set inner vlan tag valid bit */
  2644. SET_BIT(dmatxdesc->DESC3, ETH_DMATXCDESC_IVLTV);
  2645. /* Set Vlan Tag control */
  2646. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXCDESC_IVTIR, pTxConfig->InnerVlanCtrl);
  2647. /* Set the descriptor as the inner vlan input source */
  2648. SET_BIT(heth->Instance->MACIVIR, ETH_MACIVIR_VLTI);
  2649. /* Enable double VLAN processing */
  2650. SET_BIT(heth->Instance->MACVTR, ETH_MACVTR_EDVLP);
  2651. }
  2652. }
  2653. /* if tcp segmentation is enabled for this packet */
  2654. if (READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_TSO) != (uint32_t)RESET)
  2655. {
  2656. /* Set MSS value */
  2657. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXCDESC_MSS, pTxConfig->MaxSegmentSize);
  2658. /* Set MSS valid bit */
  2659. SET_BIT(dmatxdesc->DESC3, ETH_DMATXCDESC_TCMSSV);
  2660. }
  2661. if ((READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_VLANTAG) != (uint32_t)RESET)
  2662. || (READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_TSO) != (uint32_t)RESET))
  2663. {
  2664. /* Set as context descriptor */
  2665. SET_BIT(dmatxdesc->DESC3, ETH_DMATXCDESC_CTXT);
  2666. /* Ensure rest of descriptor is written to RAM before the OWN bit */
  2667. __DMB();
  2668. /* Set own bit */
  2669. SET_BIT(dmatxdesc->DESC3, ETH_DMATXCDESC_OWN);
  2670. /* Increment current tx descriptor index */
  2671. INCR_TX_DESC_INDEX(descidx, 1U);
  2672. /* Get current descriptor address */
  2673. dmatxdesc = (ETH_DMADescTypeDef *)dmatxdesclist->TxDesc[descidx];
  2674. descnbr += 1U;
  2675. /* Current Tx Descriptor Owned by DMA: cannot be used by the application */
  2676. if (READ_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCWBF_OWN) == ETH_DMATXNDESCWBF_OWN)
  2677. {
  2678. dmatxdesc = (ETH_DMADescTypeDef *)dmatxdesclist->TxDesc[firstdescidx];
  2679. /* Ensure rest of descriptor is written to RAM before the OWN bit */
  2680. __DMB();
  2681. /* Clear own bit */
  2682. CLEAR_BIT(dmatxdesc->DESC3, ETH_DMATXCDESC_OWN);
  2683. return HAL_ETH_ERROR_BUSY;
  2684. }
  2685. }
  2686. /***************************************************************************/
  2687. /***************** Normal descriptors configuration *****************/
  2688. /***************************************************************************/
  2689. descnbr += 1U;
  2690. /* Set header or buffer 1 address */
  2691. WRITE_REG(dmatxdesc->DESC0, (uint32_t)txbuffer->buffer);
  2692. /* Set header or buffer 1 Length */
  2693. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXNDESCRF_B1L, txbuffer->len);
  2694. if (txbuffer->next != NULL)
  2695. {
  2696. txbuffer = txbuffer->next;
  2697. /* Set buffer 2 address */
  2698. WRITE_REG(dmatxdesc->DESC1, (uint32_t)txbuffer->buffer);
  2699. /* Set buffer 2 Length */
  2700. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXNDESCRF_B2L, (txbuffer->len << 16));
  2701. }
  2702. else
  2703. {
  2704. WRITE_REG(dmatxdesc->DESC1, 0x0);
  2705. /* Set buffer 2 Length */
  2706. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXNDESCRF_B2L, 0x0U);
  2707. }
  2708. if (READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_TSO) != (uint32_t)RESET)
  2709. {
  2710. /* Set TCP Header length */
  2711. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_THL, (pTxConfig->TCPHeaderLen << 19));
  2712. /* Set TCP payload length */
  2713. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_TPL, pTxConfig->PayloadLen);
  2714. /* Set TCP Segmentation Enabled bit */
  2715. SET_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_TSE);
  2716. }
  2717. else
  2718. {
  2719. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_FL, pTxConfig->Length);
  2720. if (READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_CSUM) != (uint32_t)RESET)
  2721. {
  2722. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_CIC, pTxConfig->ChecksumCtrl);
  2723. }
  2724. if (READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_CRCPAD) != (uint32_t)RESET)
  2725. {
  2726. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_CPC, pTxConfig->CRCPadCtrl);
  2727. }
  2728. }
  2729. if (READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_VLANTAG) != (uint32_t)RESET)
  2730. {
  2731. /* Set Vlan Tag control */
  2732. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXNDESCRF_VTIR, pTxConfig->VlanCtrl);
  2733. }
  2734. /* Mark it as First Descriptor */
  2735. SET_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_FD);
  2736. /* Mark it as NORMAL descriptor */
  2737. CLEAR_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_CTXT);
  2738. /* Ensure rest of descriptor is written to RAM before the OWN bit */
  2739. __DMB();
  2740. /* set OWN bit of FIRST descriptor */
  2741. SET_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_OWN);
  2742. /* If source address insertion/replacement is enabled for this packet */
  2743. if (READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_SAIC) != (uint32_t)RESET)
  2744. {
  2745. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_SAIC, pTxConfig->SrcAddrCtrl);
  2746. }
  2747. /* only if the packet is split into more than one descriptors > 1 */
  2748. while (txbuffer->next != NULL)
  2749. {
  2750. /* Clear the LD bit of previous descriptor */
  2751. CLEAR_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_LD);
  2752. /* Increment current tx descriptor index */
  2753. INCR_TX_DESC_INDEX(descidx, 1U);
  2754. /* Get current descriptor address */
  2755. dmatxdesc = (ETH_DMADescTypeDef *)dmatxdesclist->TxDesc[descidx];
  2756. /* Clear the FD bit of new Descriptor */
  2757. CLEAR_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_FD);
  2758. /* Current Tx Descriptor Owned by DMA: cannot be used by the application */
  2759. if ((READ_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_OWN) == ETH_DMATXNDESCRF_OWN)
  2760. || (dmatxdesclist->PacketAddress[descidx] != NULL))
  2761. {
  2762. descidx = firstdescidx;
  2763. dmatxdesc = (ETH_DMADescTypeDef *)dmatxdesclist->TxDesc[descidx];
  2764. /* clear previous desc own bit */
  2765. for (idx = 0; idx < descnbr; idx ++)
  2766. {
  2767. /* Ensure rest of descriptor is written to RAM before the OWN bit */
  2768. __DMB();
  2769. CLEAR_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_OWN);
  2770. /* Increment current tx descriptor index */
  2771. INCR_TX_DESC_INDEX(descidx, 1U);
  2772. /* Get current descriptor address */
  2773. dmatxdesc = (ETH_DMADescTypeDef *)dmatxdesclist->TxDesc[descidx];
  2774. }
  2775. return HAL_ETH_ERROR_BUSY;
  2776. }
  2777. descnbr += 1U;
  2778. /* Get the next Tx buffer in the list */
  2779. txbuffer = txbuffer->next;
  2780. /* Set header or buffer 1 address */
  2781. WRITE_REG(dmatxdesc->DESC0, (uint32_t)txbuffer->buffer);
  2782. /* Set header or buffer 1 Length */
  2783. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXNDESCRF_B1L, txbuffer->len);
  2784. if (txbuffer->next != NULL)
  2785. {
  2786. /* Get the next Tx buffer in the list */
  2787. txbuffer = txbuffer->next;
  2788. /* Set buffer 2 address */
  2789. WRITE_REG(dmatxdesc->DESC1, (uint32_t)txbuffer->buffer);
  2790. /* Set buffer 2 Length */
  2791. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXNDESCRF_B2L, (txbuffer->len << 16));
  2792. }
  2793. else
  2794. {
  2795. WRITE_REG(dmatxdesc->DESC1, 0x0);
  2796. /* Set buffer 2 Length */
  2797. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXNDESCRF_B2L, 0x0U);
  2798. }
  2799. if (READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_TSO) != (uint32_t)RESET)
  2800. {
  2801. /* Set TCP payload length */
  2802. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_TPL, pTxConfig->PayloadLen);
  2803. /* Set TCP Segmentation Enabled bit */
  2804. SET_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_TSE);
  2805. }
  2806. else
  2807. {
  2808. /* Set the packet length */
  2809. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_FL, pTxConfig->Length);
  2810. if (READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_CSUM) != (uint32_t)RESET)
  2811. {
  2812. /* Checksum Insertion Control */
  2813. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_CIC, pTxConfig->ChecksumCtrl);
  2814. }
  2815. }
  2816. bd_count += 1U;
  2817. /* Ensure rest of descriptor is written to RAM before the OWN bit */
  2818. __DMB();
  2819. /* Set Own bit */
  2820. SET_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_OWN);
  2821. /* Mark it as NORMAL descriptor */
  2822. CLEAR_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_CTXT);
  2823. }
  2824. if (ItMode != ((uint32_t)RESET))
  2825. {
  2826. /* Set Interrupt on completion bit */
  2827. SET_BIT(dmatxdesc->DESC2, ETH_DMATXNDESCRF_IOC);
  2828. }
  2829. else
  2830. {
  2831. /* Clear Interrupt on completion bit */
  2832. CLEAR_BIT(dmatxdesc->DESC2, ETH_DMATXNDESCRF_IOC);
  2833. }
  2834. /* Mark it as LAST descriptor */
  2835. SET_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_LD);
  2836. /* Save the current packet address to expose it to the application */
  2837. dmatxdesclist->PacketAddress[descidx] = dmatxdesclist->CurrentPacketAddress;
  2838. dmatxdesclist->CurTxDesc = descidx;
  2839. /* disable the interrupt */
  2840. __disable_irq();
  2841. dmatxdesclist->BuffersInUse += bd_count + 1U;
  2842. /* Enable interrupts back */
  2843. __enable_irq();
  2844. /* Return function status */
  2845. return HAL_ETH_ERROR_NONE;
  2846. }
  2847. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  2848. static void ETH_InitCallbacksToDefault(ETH_HandleTypeDef *heth)
  2849. {
  2850. /* Init the ETH Callback settings */
  2851. heth->TxCpltCallback = HAL_ETH_TxCpltCallback; /* Legacy weak TxCpltCallback */
  2852. heth->RxCpltCallback = HAL_ETH_RxCpltCallback; /* Legacy weak RxCpltCallback */
  2853. heth->ErrorCallback = HAL_ETH_ErrorCallback; /* Legacy weak ErrorCallback */
  2854. heth->PMTCallback = HAL_ETH_PMTCallback; /* Legacy weak PMTCallback */
  2855. heth->EEECallback = HAL_ETH_EEECallback; /* Legacy weak EEECallback */
  2856. heth->WakeUpCallback = HAL_ETH_WakeUpCallback; /* Legacy weak WakeUpCallback */
  2857. }
  2858. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  2859. /**
  2860. * @}
  2861. */
  2862. /**
  2863. * @}
  2864. */
  2865. #endif /* ETH */
  2866. #endif /* HAL_ETH_MODULE_ENABLED */
  2867. /**
  2868. * @}
  2869. */