stm32h7xx_hal_eth.c 118 KB

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