gd32f10x_eth.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587
  1. /**
  2. ******************************************************************************
  3. * @brief ETH header file of the firmware library.
  4. ******************************************************************************
  5. */
  6. #ifdef GD32F10X_CL
  7. /* Includes ------------------------------------------------------------------*/
  8. #include "gd32f10x_eth.h"
  9. #include "gd32f10x_rcc.h"
  10. /** @addtogroup GD32F10x_Firmware
  11. * @{
  12. */
  13. /** @defgroup ETH
  14. * @brief ETH driver modules
  15. * @{
  16. */
  17. /** @defgroup ETH_Private_Defines
  18. * @{
  19. */
  20. /* Global transmit and receive descriptors pointers */
  21. ETH_DMADESCTypeDef *DMACurrentTxDesc;
  22. ETH_DMADESCTypeDef *DMACurrentRxDesc;
  23. ETH_DMADESCTypeDef *DMACurrentPTPTxDesc;
  24. ETH_DMADESCTypeDef *DMACurrentPTPRxDesc;
  25. /* ETHERNET MAC address offsets */
  26. #define ETH_MAC_ADDR_HBASE (ETH_MAC_BASE + 0x40) /* ETHERNET MAC address high offset */
  27. #define ETH_MAC_ADDR_LBASE (ETH_MAC_BASE + 0x44) /* ETHERNET MAC address low offset */
  28. /* ETHERNET MAC_PHYAR register Mask */
  29. #define MAC_PHYAR_CLR_MASK ((uint32_t)0xFFFFFFE3)
  30. /* ETHERNET MAC_PHYAR register PHY address shift */
  31. #define MAC_PHYAR_PHYADDRSHIFT 11
  32. /* ETHERNET MAC_PHYAR register PHY register shift */
  33. #define MAC_PHYAR_PHYREGSHIFT 6
  34. /* ETHERNET MAC_CFR register Mask */
  35. #define MAC_CFR_CLEAR_MASK ((uint32_t)0xFF20810F)
  36. /* ETHERNET MAC_FCTLR register Mask */
  37. #define MAC_FCTLR_CLEAR_MASK ((uint32_t)0x0000FF41)
  38. /* ETHERNET DMA_CTLR register Mask */
  39. #define DMA_CTLR_CLEAR_MASK ((uint32_t)0xF8DE3F23)
  40. /* ETHERNET Remote Wake-up frame register length */
  41. #define ETH_WAKEUP_REGISTER_LENGTH 8
  42. /* ETHERNET Missed frames counter Shift */
  43. #define ETH_DMA_RX_OVERFLOW_MISSEDFRAMES_COUNTERSHIFT 17
  44. /* ETHERNET DMA Tx descriptors Collision Count Shift */
  45. #define ETH_DMATXDESC_COLLISION_COUNTSHIFT 3
  46. /* ETHERNET DMA Tx descriptors size */
  47. #define ETH_DMATXDESC_SIZE 0x10
  48. /* ETHERNET DMA Rx descriptors size */
  49. #define ETH_DMARXDESC_SIZE 0x10
  50. /* ETHERNET DMA Tx descriptors Buffer2 Size Shift */
  51. #define ETH_DMATXDESC_BUFFER2_SIZESHIFT 16
  52. /* ETHERNET DMA Rx descriptors Frame Length Shift */
  53. #define ETH_DMARXDESC_FRAME_LENGTHSHIFT 16
  54. /* ETHERNET DMA Rx descriptors Buffer2 Size Shift */
  55. #define ETH_DMARXDESC_BUFFER2_SIZESHIFT 16
  56. /* ETHERNET errors */
  57. #define ERROR ((uint32_t)0)
  58. #define SUCCESS ((uint32_t)1)
  59. /**
  60. * @}
  61. */
  62. /** @defgroup ETH_Private_FunctionPrototypes
  63. * @{
  64. */
  65. #ifndef USE_Delay
  66. static void ETH_Delay(__IO uint32_t nCount);
  67. #endif /* USE_Delay*/
  68. /**
  69. * @}
  70. */
  71. /** @defgroup ETH_Private_Functions
  72. * @{
  73. */
  74. /**
  75. * @brief Reset the ETH registers.
  76. * @param None
  77. * @retval None
  78. */
  79. void ETH_DeInit(void)
  80. {
  81. RCC_AHBPeriphReset_Enable(RCC_AHBPERIPH_ETH_MAC, ENABLE);
  82. RCC_AHBPeriphReset_Enable(RCC_AHBPERIPH_ETH_MAC, DISABLE);
  83. }
  84. /**
  85. * @brief Initialize MDIO parameters.
  86. * @param None
  87. * @retval None
  88. */
  89. void ETH_MDIOInit(void)
  90. {
  91. uint32_t temp = 0;
  92. RCC_ClocksPara rcc_clocks;
  93. uint32_t hclk;
  94. /* ETHERNET MAC_PHYAR Configuration */
  95. /* Get the ETH_MAC_PHYAR value */
  96. temp = ETH_MAC->PHYAR;
  97. /* Clear Clock Range CLR[2:0] bits */
  98. temp &= MAC_PHYAR_CLR_MASK;
  99. /* Get hclk frequency value */
  100. RCC_GetClocksFreq(&rcc_clocks);
  101. hclk = rcc_clocks.AHB_Frequency;
  102. /* Set CLR bits depending on hclk value */
  103. if ((hclk >= 20000000) && (hclk < 35000000)) {
  104. /* Clock Range between 20-35 MHz */
  105. temp |= (uint32_t)ETH_MAC_PHYAR_CLR_DIV16;
  106. } else if ((hclk >= 35000000) && (hclk < 60000000)) {
  107. /* Clock Range between 35-60 MHz */
  108. temp |= (uint32_t)ETH_MAC_PHYAR_CLR_DIV26;
  109. } else if ((hclk >= 60000000) && (hclk < 90000000)) {
  110. /* Clock Range between 60-90 MHz */
  111. temp |= (uint32_t)ETH_MAC_PHYAR_CLR_DIV42;
  112. }
  113. /* ((hclk >= 90000000)&&(hclk <= 108000000)) */
  114. else {
  115. /* Clock Range between 90-108 MHz */
  116. temp |= (uint32_t)ETH_MAC_PHYAR_CLR_DIV64;
  117. }
  118. /* Write to ETHERNET MAC PHYAR */
  119. ETH_MAC->PHYAR = (uint32_t)temp;
  120. }
  121. /**
  122. * @brief Initial the ETH parameters.
  123. * @param ETH_InitParaStruct: The ETH_InitPara structure pointer.
  124. * @param PHYAddress: external PHY address
  125. * @retval The Initialize result(ERROR or SUCCESS)
  126. */
  127. uint32_t ETH_Init(ETH_InitPara *ETH_InitParaStruct, uint16_t PHYAddress)
  128. {
  129. uint32_t RegValue = 0, temp = 0;
  130. __IO uint32_t i = 0;
  131. RCC_ClocksPara rcc_clocks;
  132. uint32_t hclk;
  133. __IO uint32_t timeout = 0;
  134. /* MAC Config */
  135. /* ETH_MAC_PHYAR Configuration */
  136. /* Get the ETH_MAC_PHYAR value */
  137. temp = ETH_MAC->PHYAR;
  138. /* Clear Clock Range CLR[2:0] bits */
  139. temp &= MAC_PHYAR_CLR_MASK;
  140. /* Get hclk frequency value */
  141. RCC_GetClocksFreq(&rcc_clocks);
  142. hclk = rcc_clocks.AHB_Frequency;
  143. /* Set CLR bits depending on hclk value */
  144. if ((hclk >= 20000000) && (hclk < 35000000)) {
  145. /* Clock Range between 20-35 MHz */
  146. temp |= (uint32_t)ETH_MAC_PHYAR_CLR_DIV16;
  147. } else if ((hclk >= 35000000) && (hclk < 60000000)) {
  148. /* Clock Range between 35-60 MHz */
  149. temp |= (uint32_t)ETH_MAC_PHYAR_CLR_DIV26;
  150. } else if ((hclk >= 60000000) && (hclk < 90000000)) {
  151. /* Clock Range between 60-90 MHz */
  152. temp |= (uint32_t)ETH_MAC_PHYAR_CLR_DIV42;
  153. }
  154. /* ((hclk >= 90000000)&&(hclk <= 108000000)) */
  155. else {
  156. /* Clock Range between 90-108 MHz */
  157. temp |= (uint32_t)ETH_MAC_PHYAR_CLR_DIV64;
  158. }
  159. ETH_MAC->PHYAR = (uint32_t)temp;
  160. /* PHY initialization and configuration */
  161. /* Set the PHY into reset mode */
  162. if (!(ETH_SetPHYRegisterValue(PHYAddress, PHY_BCR, PHY_RESET))) {
  163. /* Return ERROR due to write timeout */
  164. return ERROR;
  165. }
  166. /* PHY reset need some time */
  167. _eth_delay_(PHY_RESETDELAY);
  168. if (ETH_InitParaStruct->ETH_MAC_AutoNegotiation != ETH_AUTONEGOTIATION_DISABLE) {
  169. /* Wait for PHY_LINKED_STATUS bit be set */
  170. do {
  171. timeout++;
  172. } while (!(ETH_GetPHYRegisterValue(PHYAddress, PHY_BSR) & PHY_LINKED_STATUS) && (timeout < PHY_READ_TO));
  173. /* Return ERROR due to timeout */
  174. if (timeout == PHY_READ_TO) {
  175. return ERROR;
  176. }
  177. /* Reset Timeout counter */
  178. timeout = 0;
  179. /* Enable Auto-Negotiation */
  180. if (!(ETH_SetPHYRegisterValue(PHYAddress, PHY_BCR, PHY_AUTONEGOTIATION))) {
  181. /* Return ERROR due to write timeout */
  182. return ERROR;
  183. }
  184. /* Wait for the PHY_AUTONEGO_COMPLETE bit be set */
  185. do {
  186. timeout++;
  187. } while (!(ETH_GetPHYRegisterValue(PHYAddress, PHY_BSR) & PHY_AUTONEGO_COMPLETE) && (timeout < (uint32_t)PHY_READ_TO));
  188. /* Return ERROR due to timeout */
  189. if (timeout == PHY_READ_TO) {
  190. return ERROR;
  191. }
  192. /* Reset Timeout counter */
  193. timeout = 0;
  194. /* Read the result of the autonegotiation */
  195. RegValue = ETH_GetPHYRegisterValue(PHYAddress, PHY_SR);
  196. /* Configure the Duplex Mode of MAC following the autonegotiation result */
  197. if ((RegValue & PHY_DUPLEX_STATUS) != (uint32_t)RESET) {
  198. ETH_InitParaStruct->ETH_MAC_Mode = ETH_MODE_FULLDUPLEX;
  199. } else {
  200. ETH_InitParaStruct->ETH_MAC_Mode = ETH_MODE_HALFDUPLEX;
  201. }
  202. /* Configure the Communication speed of MAC following the autonegotiation result */
  203. if (RegValue & PHY_Speed_Status) {
  204. ETH_InitParaStruct->ETH_MAC_Speed = ETH_SPEEDMODE_10M;
  205. } else {
  206. ETH_InitParaStruct->ETH_MAC_Speed = ETH_SPEEDMODE_100M;
  207. }
  208. } else {
  209. if (!ETH_SetPHYRegisterValue(PHYAddress, PHY_BCR, ((uint16_t)(ETH_InitParaStruct->ETH_MAC_Mode >> 3) |
  210. (uint16_t)(ETH_InitParaStruct->ETH_MAC_Speed >> 1)))) {
  211. /* Return ERROR due to write timeout */
  212. return ERROR;
  213. }
  214. /* PHY configuration need some time */
  215. _eth_delay_(PHY_CONFIGDELAY);
  216. }
  217. /* ETH_MAC_CFR Configuration */
  218. /* Get the ETH_MAC_CFR value */
  219. temp = ETH_MAC->CFR;
  220. temp &= MAC_CFR_CLEAR_MASK;
  221. /* Set the WDD bit according to ETH_MAC_Watchdog value */
  222. /* Set the JBD: bit according to ETH_MAC_Jabber value */
  223. /* Set the IG bit according to ETH_MAC_InterFrameGap value */
  224. /* Set the CSD bit according to ETH_MAC_CarrierSense value */
  225. /* Set the SPD bit according to ETH_MAC_Speed value */
  226. /* Set the ROD bit according to ETH_MAC_ReceiveOwn value */
  227. /* Set the LBM bit according to ETH_MAC_LoopbackMode value */
  228. /* Set the DPM bit according to ETH_MAC_Mode value */
  229. /* Set the IP4CO bit according to ETH_MAC_ChecksumOffload value */
  230. /* Set the RTD bit according to ETH_MAC_RetryTransmission value */
  231. /* Set the APCD bit according to ETH_MAC_AutomaticPadCRCDrop value */
  232. /* Set the BOL bit according to ETH_MAC_BackOffLimit value */
  233. /* Set the DFC bit according to ETH_MAC_DeferralCheck value */
  234. temp |= (uint32_t)(ETH_InitParaStruct->ETH_MAC_Watchdog |
  235. ETH_InitParaStruct->ETH_MAC_Jabber |
  236. ETH_InitParaStruct->ETH_MAC_InterFrameGap |
  237. ETH_InitParaStruct->ETH_MAC_CarrierSense |
  238. ETH_InitParaStruct->ETH_MAC_Speed |
  239. ETH_InitParaStruct->ETH_MAC_ReceiveOwn |
  240. ETH_InitParaStruct->ETH_MAC_LoopbackMode |
  241. ETH_InitParaStruct->ETH_MAC_Mode |
  242. ETH_InitParaStruct->ETH_MAC_ChecksumOffload |
  243. ETH_InitParaStruct->ETH_MAC_RetryTransmission |
  244. ETH_InitParaStruct->ETH_MAC_AutomaticPadCRCDrop |
  245. ETH_InitParaStruct->ETH_MAC_BackOffLimit |
  246. ETH_InitParaStruct->ETH_MAC_DeferralCheck);
  247. /* Write to ETH_MAC_CFR */
  248. ETH_MAC->CFR = (uint32_t)temp;
  249. /* ETH_MAC_FRMFR Configuration */
  250. /* Set the FD bit according to ETH_MAC_FilterDisable value */
  251. /* Set the SAFLT and SAIFLT bits according to ETH_MAC_SourceAddrFilter value */
  252. /* Set the PCFRM bit according to ETH_MAC_PassControlFrames value */
  253. /* Set the DBF bit according to ETH_MAC_BroadcastFramesReception value */
  254. /* Set the DAIFLT bit according to ETH_MAC_DestinationAddrFilter value */
  255. /* Set the PM bit according to ETH_MAC_PromiscuousMode value */
  256. /* Set the PM, HMF and HPFLT bits according to ETH_MAC_MulticastFramesFilter value */
  257. /* Set the HUF and HPFLT bits according to ETH_MAC_UnicastFramesFilter value */
  258. /* Write to ETH_MAC_FRMFR */
  259. ETH_MAC->FRMFR = (uint32_t)(ETH_InitParaStruct->ETH_MAC_FilterDisable |
  260. ETH_InitParaStruct->ETH_MAC_SourceAddrFilter |
  261. ETH_InitParaStruct->ETH_MAC_PassControlFrames |
  262. ETH_InitParaStruct->ETH_MAC_BroadcastFramesReception |
  263. ETH_InitParaStruct->ETH_MAC_DestinationAddrFilter |
  264. ETH_InitParaStruct->ETH_MAC_PromiscuousMode |
  265. ETH_InitParaStruct->ETH_MAC_MulticastFramesFilter |
  266. ETH_InitParaStruct->ETH_MAC_UnicastFramesFilter);
  267. /* ETH_MAC_HLHR and MAC_HLLR Configuration */
  268. /* Write to ETHERNET MAC_HLHR */
  269. ETH_MAC->HLHR = (uint32_t)ETH_InitParaStruct->ETH_MAC_HashListHigh;
  270. /* Write to ETHERNET MAC_HLLR */
  271. ETH_MAC->HLLR = (uint32_t)ETH_InitParaStruct->ETH_MAC_HashListLow;
  272. /* ETH_MAC_FCTLR Configuration */
  273. /* Get the ETH_MAC_FCTLR value */
  274. temp = ETH_MAC->FCTLR;
  275. temp &= MAC_FCTLR_CLEAR_MASK;
  276. /* Set the PTM bit according to ETH_MAC_PauseTime value */
  277. /* Set the ZQPD bit according to ETH_MAC_ZeroQuantaPause value */
  278. /* Set the PLTS bit according to ETH_MAC_PauseLowThreshold value */
  279. /* Set the UPFDT bit according to ETH_MAC_UnicastPauseFrameDetect value */
  280. /* Set the RFCEN bit according to ETH_MAC_ReceiveFlowControl value */
  281. /* Set the TFCEN bit according to ETH_MAC_TransmitFlowControl value */
  282. temp |= (uint32_t)((ETH_InitParaStruct->ETH_MAC_PauseTime << 16) |
  283. ETH_InitParaStruct->ETH_MAC_ZeroQuantaPause |
  284. ETH_InitParaStruct->ETH_MAC_PauseLowThreshold |
  285. ETH_InitParaStruct->ETH_MAC_UnicastPauseFrameDetect |
  286. ETH_InitParaStruct->ETH_MAC_ReceiveFlowControl |
  287. ETH_InitParaStruct->ETH_MAC_TransmitFlowControl);
  288. /* Write to ETH_MAC_FCTLR */
  289. ETH_MAC->FCTLR = (uint32_t)temp;
  290. /* ETH_MAC_FCTHR Configuration */
  291. temp = ETH_MAC->FCTHR;
  292. temp |= (uint32_t)(ETH_InitParaStruct->ETH_MAC_FlowControlDeactiveThreshold |
  293. ETH_InitParaStruct->ETH_MAC_FlowControlActiveThreshold);
  294. ETH_MAC->FCTHR = (uint32_t)temp;
  295. /* ETH_MAC_VLTR Configuration */
  296. /* Set the VLTC bit according to ETH_MAC_VLANTagComparison value */
  297. /* Set the VLTI bit according to ETH_MAC_VLANTagIdentifier value */
  298. ETH_MAC->VLTR = (uint32_t)(ETH_InitParaStruct->ETH_MAC_VLANTagComparison |
  299. ETH_InitParaStruct->ETH_MAC_VLANTagIdentifier);
  300. /* DMA Config */
  301. /* ETH_DMA_CTLR Configuration */
  302. /* Get the ETHERNET DMA_CTLR value */
  303. temp = ETH_DMA->CTLR;
  304. temp &= DMA_CTLR_CLEAR_MASK;
  305. /* Set the DTCERFD bit according to ETH_DMA_DropTCPIPChecksumErrorFrame value */
  306. /* Set the RSFD bit according to ETH_DMA_ReceiveStoreForward value */
  307. /* Set the DAFRF bit according to ETH_DMA_FlushReceivedFrame value */
  308. /* Set the TSFD bit according to ETH_DMA_TransmitStoreForward value */
  309. /* Set the TTCH bit according to ETH_DMA_TransmitThresholdControl value */
  310. /* Set the FERF bit according to ETH_DMA_ForwardErrorFrames value */
  311. /* Set the FUF bit according to ETH_DMA_ForwardUndersizedGoodFrames value */
  312. /* Set the RTHC bit according to ETH_DMA_ReceiveThresholdControl value */
  313. /* Set the OSF bit according to ETH_DMA_SecondFrameOperate value */
  314. temp |= (uint32_t)(ETH_InitParaStruct->ETH_DMA_DropTCPIPChecksumErrorFrame |
  315. ETH_InitParaStruct->ETH_DMA_ReceiveStoreForward |
  316. ETH_InitParaStruct->ETH_DMA_FlushReceivedFrame |
  317. ETH_InitParaStruct->ETH_DMA_TransmitStoreForward |
  318. ETH_InitParaStruct->ETH_DMA_TransmitThresholdControl |
  319. ETH_InitParaStruct->ETH_DMA_ForwardErrorFrames |
  320. ETH_InitParaStruct->ETH_DMA_ForwardUndersizedGoodFrames |
  321. ETH_InitParaStruct->ETH_DMA_ReceiveThresholdControl |
  322. ETH_InitParaStruct->ETH_DMA_SecondFrameOperate);
  323. /* Write to ETH_DMA_CTLR */
  324. ETH_DMA->CTLR = (uint32_t)temp;
  325. /* ETH_DMA_BCR Configuration */
  326. /* Set the AA bit according to ETH_DMA_AddressAligned value */
  327. /* Set the FB bit according to ETH_DMA_FixedBurst value */
  328. /* Set the RXDP and 4*PBL bits according to ETH_DMA_RxDMABurstLength value */
  329. /* Set the FPBL and 4*PBL bits according to ETH_DMA_TxDMABurstLength value */
  330. /* Set the DPSL bit according to ETH_DesciptorSkipLength value */
  331. /* Set the RTPR and DAB bits according to ETH_DMA_Arbitration value */
  332. ETH_DMA->BCR = (uint32_t)(ETH_InitParaStruct->ETH_DMA_AddressAligned |
  333. ETH_InitParaStruct->ETH_DMA_FixedBurst |
  334. ETH_InitParaStruct->ETH_DMA_RxDMABurstLength | /* If 4xPBL is selected for Tx or Rx it is applied for the other */
  335. ETH_InitParaStruct->ETH_DMA_TxDMABurstLength |
  336. (ETH_InitParaStruct->ETH_DMA_DescriptorSkipLength << 2) |
  337. ETH_InitParaStruct->ETH_DMA_Arbitration |
  338. ETH_DMA_BCR_UIP); /* Enable use of separate PBL for Rx and Tx */
  339. /* Return Ethernet configuration success */
  340. return SUCCESS;
  341. }
  342. /**
  343. * @brief Initial the sturct ETH_InitPara.
  344. * @param ETH_InitParaStruct: pointer to a ETH_InitPara structure.
  345. * @retval None
  346. */
  347. void ETH_ParaInit(ETH_InitPara *ETH_InitParaStruct)
  348. {
  349. /* Reset ETH init structure parameters values */
  350. /* MAC */
  351. ETH_InitParaStruct->ETH_MAC_AutoNegotiation = ETH_AUTONEGOTIATION_DISABLE;
  352. ETH_InitParaStruct->ETH_MAC_Watchdog = ETH_WATCHDOG_ENABLE;
  353. ETH_InitParaStruct->ETH_MAC_Jabber = ETH_JABBER_ENABLE;
  354. ETH_InitParaStruct->ETH_MAC_InterFrameGap = ETH_INTERFRAMEGAP_96BIT;
  355. ETH_InitParaStruct->ETH_MAC_CarrierSense = ETH_CARRIERSENSE_ENABLE;
  356. ETH_InitParaStruct->ETH_MAC_Speed = ETH_SPEEDMODE_10M;
  357. ETH_InitParaStruct->ETH_MAC_ReceiveOwn = ETH_RECEIVEOWN_ENABLE;
  358. ETH_InitParaStruct->ETH_MAC_LoopbackMode = ETH_LOOPBACKMODE_DISABLE;
  359. ETH_InitParaStruct->ETH_MAC_Mode = ETH_MODE_HALFDUPLEX;
  360. ETH_InitParaStruct->ETH_MAC_ChecksumOffload = ETH_CHECKSUMOFFLOAD_DISABLE;
  361. ETH_InitParaStruct->ETH_MAC_RetryTransmission = ETH_RETRYTRANSMISSION_ENABLE;
  362. ETH_InitParaStruct->ETH_MAC_AutomaticPadCRCDrop = ETH_AUTOMATICPADCRCDROP_DISABLE;
  363. ETH_InitParaStruct->ETH_MAC_BackOffLimit = ETH_BACKOFFLIMIT_10;
  364. ETH_InitParaStruct->ETH_MAC_DeferralCheck = ETH_DEFERRALCHECK_DISABLE;
  365. ETH_InitParaStruct->ETH_MAC_FilterDisable = ETH_FILTERDISABLE_DISABLE;
  366. ETH_InitParaStruct->ETH_MAC_SourceAddrFilter = ETH_SOURCEADDRFILTER_DISABLE;
  367. ETH_InitParaStruct->ETH_MAC_PassControlFrames = ETH_PASSCONTROLFRAMES_BLOCKALL;
  368. ETH_InitParaStruct->ETH_MAC_BroadcastFramesReception = ETH_BROADCASTFRAMESRECEPTION_DISABLE;
  369. ETH_InitParaStruct->ETH_MAC_DestinationAddrFilter = ETH_DESTINATIONADDRFILTER_NORMAL;
  370. ETH_InitParaStruct->ETH_MAC_PromiscuousMode = ETH_PROMISCUOUSMODE_DISABLE;
  371. ETH_InitParaStruct->ETH_MAC_MulticastFramesFilter = ETH_MULTICASTFRAMESFILTER_PERFECT;
  372. ETH_InitParaStruct->ETH_MAC_UnicastFramesFilter = ETH_UNICASTFRAMESFILTER_PERFECT;
  373. ETH_InitParaStruct->ETH_MAC_HashListHigh = 0x0;
  374. ETH_InitParaStruct->ETH_MAC_HashListLow = 0x0;
  375. ETH_InitParaStruct->ETH_MAC_PauseTime = 0x0;
  376. ETH_InitParaStruct->ETH_MAC_ZeroQuantaPause = ETH_ZEROQUANTAPAUSE_DISABLE;
  377. ETH_InitParaStruct->ETH_MAC_PauseLowThreshold = ETH_PAUSELOWTHRESHOLD_MINUS4;
  378. ETH_InitParaStruct->ETH_MAC_FlowControlDeactiveThreshold = ETH_RFD_512BYTES;
  379. ETH_InitParaStruct->ETH_MAC_FlowControlActiveThreshold = ETH_RFA_1536BYTES;
  380. ETH_InitParaStruct->ETH_MAC_UnicastPauseFrameDetect = ETH_UNICASTPAUSEFRAMEDETECT_DISABLE;
  381. ETH_InitParaStruct->ETH_MAC_ReceiveFlowControl = ETH_RECEIVEFLOWCONTROL_DISABLE;
  382. ETH_InitParaStruct->ETH_MAC_TransmitFlowControl = ETH_TRANSMITFLOWCONTROL_DISABLE;
  383. ETH_InitParaStruct->ETH_MAC_VLANTagComparison = ETH_VLANTAGCOMPARISON_16BIT;
  384. ETH_InitParaStruct->ETH_MAC_VLANTagIdentifier = 0x0;
  385. /* DMA */
  386. ETH_InitParaStruct->ETH_DMA_DropTCPIPChecksumErrorFrame = ETH_DROPTCPIPCHECKSUMERRORFRAME_DISABLE;
  387. ETH_InitParaStruct->ETH_DMA_ReceiveStoreForward = ETH_RECEIVESTOREFORWARD_ENABLE;
  388. ETH_InitParaStruct->ETH_DMA_FlushReceivedFrame = ETH_FLUSHRECEIVEDFRAME_DISABLE;
  389. ETH_InitParaStruct->ETH_DMA_TransmitStoreForward = ETH_TRANSMITSTOREFORWARD_ENABLE;
  390. ETH_InitParaStruct->ETH_DMA_TransmitThresholdControl = ETH_TRANSMITTHRESHOLDCONTROL_64BYTES;
  391. ETH_InitParaStruct->ETH_DMA_ForwardErrorFrames = ETH_FORWARDERRORFRAMES_DISABLE;
  392. ETH_InitParaStruct->ETH_DMA_ForwardUndersizedGoodFrames = ETH_FORWARDUNDERSIZEDGOODFRAMES_DISABLE;
  393. ETH_InitParaStruct->ETH_DMA_ReceiveThresholdControl = ETH_RECEIVETHRESHOLDCONTROL_64BYTES;
  394. ETH_InitParaStruct->ETH_DMA_SecondFrameOperate = ETH_SECONDFRAMEOPERATE_DISABLE;
  395. ETH_InitParaStruct->ETH_DMA_AddressAligned = ETH_ADDRESSALIGNED_ENABLE;
  396. ETH_InitParaStruct->ETH_DMA_FixedBurst = ETH_FIXEDBURST_DISABLE;
  397. ETH_InitParaStruct->ETH_DMA_RxDMABurstLength = ETH_RXDMABURSTLENGTH_1BEAT;
  398. ETH_InitParaStruct->ETH_DMA_TxDMABurstLength = ETH_TXDMABURSTLENGTH_1BEAT;
  399. ETH_InitParaStruct->ETH_DMA_DescriptorSkipLength = 0x0;
  400. ETH_InitParaStruct->ETH_DMA_Arbitration = ETH_DMAARBITRATION_ROUNDROBIN_RXTX_1_1;
  401. }
  402. /**
  403. * @brief Enable or disable the ETH's receive and transmit.
  404. * @param NewValue: ENABLE or DISABLE.
  405. * @retval None
  406. */
  407. void ETH_Enable(TypeState NewValue)
  408. {
  409. /* Enable or Disable the ETH module */
  410. if (NewValue != DISABLE) {
  411. /* Enable MAC transmit */
  412. ETH_MACTransmission_Enable(ENABLE);
  413. /* Flush ETHERNET DMA Transmit FIFO */
  414. ETH_CleanTransmitFIFO();
  415. /* Enable MAC receive */
  416. ETH_MACReception_Enable(ENABLE);
  417. /* Enable DMA transmission */
  418. ETH_DMATransmission_Enable(ENABLE);
  419. /* Enable DMA reception */
  420. ETH_DMAReception_Enable(ENABLE);
  421. } else {
  422. /* Disable MAC transmit */
  423. ETH_MACTransmission_Enable(DISABLE);
  424. /* Flush ETHERNET DMA Transmit FIFO */
  425. ETH_CleanTransmitFIFO();
  426. /* Disable MAC receive */
  427. ETH_MACReception_Enable(DISABLE);
  428. /* Disable DMA transmission */
  429. ETH_DMATransmission_Enable(DISABLE);
  430. /* Disable DMA reception */
  431. ETH_DMAReception_Enable(DISABLE);
  432. }
  433. }
  434. /**
  435. * @brief Send data of application buffer as a transmit packet.
  436. * @param pbuf: Pointer to the application buffer.
  437. * @param size: the application buffer size.
  438. * @retval The transmission result(ERROR or SUCCESS)
  439. */
  440. uint32_t ETH_HandleTxPkt(uint8_t *pbuf, uint16_t size)
  441. {
  442. uint32_t offset = 0;
  443. /* Check the busy bit of Tx descriptor status */
  444. if ((DMACurrentTxDesc->Status & ETH_DMATXDESC_BUSY) != (uint32_t)RESET) {
  445. /* Return ERROR: the descriptor is busy due to own by the DMA */
  446. return ERROR;
  447. }
  448. for (offset = 0; offset < size; offset++) {
  449. (*(__IO uint8_t *)((DMACurrentTxDesc->Buffer1Addr) + offset)) = (*(pbuf + offset));
  450. }
  451. /* Setting the Frame Length */
  452. DMACurrentTxDesc->ControlBufferSize = (size & ETH_DMATXDESC_TB1S);
  453. /* Setting the segment of frame (ETH_DMATXDESC_LSG and ETH_DMATXDESC_FSG are SET that frame is transmitted in one descriptor) */
  454. DMACurrentTxDesc->Status |= ETH_DMATXDESC_LSG | ETH_DMATXDESC_FSG;
  455. /* Enable the DMA transmission */
  456. DMACurrentTxDesc->Status |= ETH_DMATXDESC_BUSY;
  457. /* Check Tx Buffer unavailable flag status */
  458. if ((ETH_DMA->STR & ETH_DMA_STR_TBU) != (uint32_t)RESET) {
  459. /* Clear TBU ETHERNET DMA flag */
  460. ETH_DMA->STR = ETH_DMA_STR_TBU;
  461. /* Resume DMA transmission by writing to the TPER register*/
  462. ETH_DMA->TPER = 0;
  463. }
  464. /* Update the ETHERNET DMA current Tx descriptor pointer to the next Tx decriptor in DMA Tx decriptor talbe*/
  465. /* Chained Mode */
  466. if ((DMACurrentTxDesc->Status & ETH_DMATXDESC_TCHM) != (uint32_t)RESET) {
  467. DMACurrentTxDesc = (ETH_DMADESCTypeDef *)(DMACurrentTxDesc->Buffer2NextDescAddr);
  468. }
  469. /* Ring Mode */
  470. else {
  471. if ((DMACurrentTxDesc->Status & ETH_DMATXDESC_TERM) != (uint32_t)RESET) {
  472. DMACurrentTxDesc = (ETH_DMADESCTypeDef *)(ETH_DMA->TDTAR);
  473. } else {
  474. DMACurrentTxDesc = (ETH_DMADESCTypeDef *)((uint32_t)DMACurrentTxDesc + ETH_DMATXDESC_SIZE + ((ETH_DMA->BCR & ETH_DMA_BCR_DPSL) >> 2));
  475. }
  476. }
  477. /* Return SUCCESS */
  478. return SUCCESS;
  479. }
  480. /**
  481. * @brief Receive a packet data to application buffer.
  482. * @param pbuf: Pointer on the application buffer.
  483. * @retval The Receive size(If framelength is equal to ERROR, the receiving unsuccessful)
  484. */
  485. uint32_t ETH_HandleRxPkt(uint8_t *pbuf)
  486. {
  487. uint32_t offset = 0, size = 0;
  488. /* Check the busy bit of Rx descriptor status */
  489. if ((DMACurrentRxDesc->Status & ETH_DMARXDESC_BUSY) != (uint32_t)RESET) {
  490. /* Return ERROR: the descriptor is busy due to own by the DMA */
  491. return ERROR;
  492. }
  493. if (((DMACurrentRxDesc->Status & ETH_DMARXDESC_ERRS) == (uint32_t)RESET) &&
  494. ((DMACurrentRxDesc->Status & ETH_DMARXDESC_LDES) != (uint32_t)RESET) &&
  495. ((DMACurrentRxDesc->Status & ETH_DMARXDESC_FDES) != (uint32_t)RESET)) {
  496. /* Get the Frame Length exclusive CRC */
  497. size = ((DMACurrentRxDesc->Status & ETH_DMARXDESC_FRML) >> ETH_DMARXDESC_FRAME_LENGTHSHIFT) - 4;
  498. for (offset = 0; offset < size; offset++) {
  499. (*(pbuf + offset)) = (*(__IO uint8_t *)((DMACurrentRxDesc->Buffer1Addr) + offset));
  500. }
  501. } else {
  502. /* Return ERROR */
  503. size = ERROR;
  504. }
  505. /* Enable reception */
  506. DMACurrentRxDesc->Status = ETH_DMARXDESC_BUSY;
  507. /* Check Rx Buffer unavailable flag status */
  508. if ((ETH_DMA->STR & ETH_DMA_STR_RBU) != (uint32_t)RESET) {
  509. /* Clear RBU ETHERNET DMA flag */
  510. ETH_DMA->STR = ETH_DMA_STR_RBU;
  511. /* Resume DMA reception by writing to the RPER register*/
  512. ETH_DMA->RPER = 0;
  513. }
  514. /* Update the ETHERNET DMA current Rx descriptor pointer to the next Rx decriptor in DMA Rx decriptor talbe*/
  515. /* Chained Mode */
  516. if ((DMACurrentRxDesc->ControlBufferSize & ETH_DMARXDESC_RCHM) != (uint32_t)RESET) {
  517. DMACurrentRxDesc = (ETH_DMADESCTypeDef *)(DMACurrentRxDesc->Buffer2NextDescAddr);
  518. }
  519. /* Ring Mode */
  520. else {
  521. if ((DMACurrentRxDesc->ControlBufferSize & ETH_DMARXDESC_RERR) != (uint32_t)RESET) {
  522. DMACurrentRxDesc = (ETH_DMADESCTypeDef *)(ETH_DMA->RDTAR);
  523. } else {
  524. DMACurrentRxDesc = (ETH_DMADESCTypeDef *)((uint32_t)DMACurrentRxDesc + ETH_DMARXDESC_SIZE + ((ETH_DMA->BCR & ETH_DMA_BCR_DPSL) >> 2));
  525. }
  526. }
  527. /* Return Frame size or ERROR */
  528. return (size);
  529. }
  530. /**
  531. * @brief To obtain the received data length
  532. * @param None
  533. * @retval Received frame length.
  534. */
  535. uint32_t ETH_GetRxPktSize(void)
  536. {
  537. uint32_t size = 0;
  538. if ((DMACurrentRxDesc->Status & ETH_DMARXDESC_BUSY) != (uint32_t)RESET) {
  539. return 0;
  540. }
  541. if (((DMACurrentRxDesc->Status & ETH_DMARXDESC_ERRS) != (uint32_t)RESET) ||
  542. ((DMACurrentRxDesc->Status & ETH_DMARXDESC_LDES) == (uint32_t)RESET) ||
  543. ((DMACurrentRxDesc->Status & ETH_DMARXDESC_FDES) == (uint32_t)RESET)) {
  544. ETH_DropRxPkt();
  545. return 0;
  546. }
  547. if (((DMACurrentRxDesc->Status & ETH_DMARXDESC_BUSY) == (uint32_t)RESET) &&
  548. ((DMACurrentRxDesc->Status & ETH_DMARXDESC_ERRS) == (uint32_t)RESET) &&
  549. ((DMACurrentRxDesc->Status & ETH_DMARXDESC_LDES) != (uint32_t)RESET) &&
  550. ((DMACurrentRxDesc->Status & ETH_DMARXDESC_FDES) != (uint32_t)RESET)) {
  551. /* Get the size of the received data including CRC */
  552. size = ETH_GetDMARxDescFrameLength(DMACurrentRxDesc);
  553. }
  554. /* Return Packet size */
  555. return size;
  556. }
  557. /**
  558. * @brief Discard a Received packet
  559. * @param None
  560. * @retval None
  561. */
  562. void ETH_DropRxPkt(void)
  563. {
  564. /* Enable reception */
  565. DMACurrentRxDesc->Status = ETH_DMARXDESC_BUSY;
  566. /* Chained Mode */
  567. if ((DMACurrentRxDesc->ControlBufferSize & ETH_DMARXDESC_RCHM) != (uint32_t)RESET) {
  568. DMACurrentRxDesc = (ETH_DMADESCTypeDef *)(DMACurrentRxDesc->Buffer2NextDescAddr);
  569. }
  570. /* Ring Mode */
  571. else {
  572. if ((DMACurrentRxDesc->ControlBufferSize & ETH_DMARXDESC_RERM) != (uint32_t)RESET) {
  573. DMACurrentRxDesc = (ETH_DMADESCTypeDef *)(ETH_DMA->RDTAR);
  574. } else {
  575. DMACurrentRxDesc = (ETH_DMADESCTypeDef *)((uint32_t)DMACurrentRxDesc + ETH_DMARXDESC_SIZE + ((ETH_DMA->BCR & ETH_DMA_BCR_DPSL) >> 2));
  576. }
  577. }
  578. }
  579. /* PHY */
  580. /**
  581. * @brief Get PHY of ETHERNET parameters.
  582. * @param PHYAddr: PHY device address, devices number max is 32.
  583. * @param PHYReg: PHY register address, register address max is 32.
  584. * Select one of the follwing values :
  585. * @arg PHY_BCR: Tranceiver Basic Control Register
  586. * @arg PHY_BSR: Tranceiver Basic Status Register
  587. * @arg PHY_SR : Tranceiver Status Register
  588. * @arg More PHY register could be read depending on the used PHY
  589. * @retval value read from the selected PHY register (if timeout return ERROR)
  590. */
  591. uint16_t ETH_GetPHYRegisterValue(uint16_t PHYAddr, uint16_t PHYReg)
  592. {
  593. uint32_t temp = 0;
  594. __IO uint32_t timeout = 0;
  595. /* Get the ETHERNET MAC_PHYAR value */
  596. temp = ETH_MAC->PHYAR;
  597. temp &= ~MAC_PHYAR_CLR_MASK;
  598. /* Configuration the PHY address register value */
  599. /* Set the PHY device address */
  600. temp |= (((uint32_t)PHYAddr << MAC_PHYAR_PHYADDRSHIFT) & ETH_MAC_PHYAR_PA);
  601. /* Set the PHY register address */
  602. temp |= (((uint32_t)PHYReg << MAC_PHYAR_PHYREGSHIFT) & ETH_MAC_PHYAR_PR);
  603. /* Set the read mode */
  604. temp &= ~ETH_MAC_PHYAR_PW;
  605. /* Set the PHY Busy bit */
  606. temp |= ETH_MAC_PHYAR_PB;
  607. ETH_MAC->PHYAR = temp;
  608. /* Check the PHY Busy flag status */
  609. do {
  610. timeout++;
  611. temp = ETH_MAC->PHYAR;
  612. } while ((temp & ETH_MAC_PHYAR_PB) && (timeout < (uint32_t)PHY_READ_TO));
  613. /* Return ERROR due to timeout */
  614. if (timeout == PHY_READ_TO) {
  615. return (uint16_t)ERROR;
  616. }
  617. /* Return PHY register selected value */
  618. return (uint16_t)(ETH_MAC->PHYDR);
  619. }
  620. /**
  621. * @brief Set PHY of ETHERNET parameters.
  622. * @param PHYAddr: PHY device address, devices number max is 32.
  623. * @param PHYReg: PHY register address, register address max is 32.
  624. * Select one of the follwing values :
  625. * @arg PHY_BCR : Tranceiver Control Register
  626. * @arg More PHY register could be written depending on the used PHY
  627. * @param PHYValue: write to register value
  628. * @retval The Write to the selected PHY register result(ERROR or SUCCESS)
  629. */
  630. uint32_t ETH_SetPHYRegisterValue(uint16_t PHYAddr, uint16_t PHYReg, uint16_t PHYValue)
  631. {
  632. uint32_t temp = 0;
  633. __IO uint32_t timeout = 0;
  634. /* Get the ETHERNET MAC_PHYAR value */
  635. temp = ETH_MAC->PHYAR;
  636. temp &= ~MAC_PHYAR_CLR_MASK;
  637. /* Configuration the PHY register address value */
  638. /* Set the PHY device address */
  639. temp |= (((uint32_t)PHYAddr << MAC_PHYAR_PHYADDRSHIFT) & ETH_MAC_PHYAR_PA);
  640. /* Set the PHY register address */
  641. temp |= (((uint32_t)PHYReg << MAC_PHYAR_PHYREGSHIFT) & ETH_MAC_PHYAR_PR);
  642. /* Set the write mode */
  643. temp |= ETH_MAC_PHYAR_PW;
  644. /* Set the PHY Busy bit */
  645. temp |= ETH_MAC_PHYAR_PB;
  646. /* Set the PHY selected register value */
  647. ETH_MAC->PHYDR = PHYValue;
  648. ETH_MAC->PHYAR = temp;
  649. /* Check the PHY Busy flag status */
  650. do {
  651. timeout++;
  652. temp = ETH_MAC->PHYAR;
  653. } while ((temp & ETH_MAC_PHYAR_PB) && (timeout < (uint32_t)PHY_WRITE_TO));
  654. /* Return ERROR due to timeout */
  655. if (timeout == PHY_WRITE_TO) {
  656. return ERROR;
  657. }
  658. return SUCCESS;
  659. }
  660. /**
  661. * @brief Enable or disable the PHY loopBack function by write PHY register.
  662. * @param PHYAddr: PHY device address, devices number max is 32.
  663. * @param NewValue: new value of the PHY loopBack mode.
  664. * This parameter can be: ENABLE or DISABLE.
  665. * @retval The Set PHYLoopBack mode result(ERROR or SUCCESS)
  666. */
  667. uint32_t ETH_PHYLoopBack_Enable(uint16_t PHYAddr, TypeState NewValue)
  668. {
  669. uint16_t temp = 0;
  670. /* Get the PHY BCR register value */
  671. temp = ETH_GetPHYRegisterValue(PHYAddr, PHY_BCR);
  672. if (NewValue != DISABLE) {
  673. /* Enable the PHY loopback mode */
  674. temp |= PHY_LOOPBACK;
  675. } else {
  676. /* Disable the PHY loopback mode (change to normal mode) */
  677. temp &= (uint16_t)(~(uint16_t)PHY_LOOPBACK);
  678. }
  679. /* Set the PHY BCR register with the new value */
  680. if (ETH_SetPHYRegisterValue(PHYAddr, PHY_BCR, temp) != (uint32_t)RESET) {
  681. return SUCCESS;
  682. } else {
  683. return ERROR;
  684. }
  685. }
  686. /* MAC */
  687. /**
  688. * @brief Enable or disable the MAC transmit function.
  689. * @param NewValue: new value of the MAC transmit function.
  690. * This parameter can be: ENABLE or DISABLE.
  691. * @retval None
  692. */
  693. void ETH_MACTransmission_Enable(TypeState NewValue)
  694. {
  695. if (NewValue != DISABLE) {
  696. /* Enable the MAC transmission */
  697. ETH_MAC->CFR |= ETH_MAC_CFR_TEN;
  698. } else {
  699. /* Disable the MAC transmission */
  700. ETH_MAC->CFR &= ~ETH_MAC_CFR_TEN;
  701. }
  702. }
  703. /**
  704. * @brief Enable or disable the MAC receive function.
  705. * @param NewValue: new value of the MAC reception.
  706. * This parameter can be: ENABLE or DISABLE.
  707. * @retval None
  708. */
  709. void ETH_MACReception_Enable(TypeState NewValue)
  710. {
  711. if (NewValue != DISABLE) {
  712. /* Enable the MAC reception */
  713. ETH_MAC->CFR |= ETH_MAC_CFR_REN;
  714. } else {
  715. /* Disable the MAC reception */
  716. ETH_MAC->CFR &= ~ETH_MAC_CFR_REN;
  717. }
  718. }
  719. /**
  720. * @brief Get the bit flag of the ETHERNET flow control busy status.
  721. * @param None
  722. * @retval current flow control busy bit status
  723. */
  724. TypeState ETH_GetFlowControlBusyBitState(void)
  725. {
  726. /* Check the flow control busy bit status */
  727. if ((ETH_MAC->FCTLR & ETH_MAC_FCTLR_FLCBBKPA) != (uint32_t)RESET) {
  728. return SET;
  729. } else {
  730. return RESET;
  731. }
  732. }
  733. /**
  734. * @brief Initiate a Pause Frame in Full-duplex mode only.
  735. * @param None
  736. * @retval None
  737. */
  738. void ETH_PauseFrameInit(void)
  739. {
  740. /* Initiate pause control frame in full duplex mode*/
  741. ETH_MAC->FCTLR |= ETH_MAC_FCTLR_FLCBBKPA;
  742. }
  743. /**
  744. * @brief Enable or disable the BackPressure requests in Half-duplex only.
  745. * @param NewValue: new value of the BackPressure operation requests.
  746. * This parameter can be: ENABLE or DISABLE.
  747. * @retval None
  748. */
  749. void ETH_BackPressureActivation_Enable(TypeState NewValue)
  750. {
  751. if (NewValue != DISABLE) {
  752. /* Enable the BackPressure requests */
  753. ETH_MAC->FCTLR |= ETH_MAC_FCTLR_FLCBBKPA;
  754. } else {
  755. /* Disable the BackPressure requests */
  756. ETH_MAC->FCTLR &= ~ETH_MAC_FCTLR_FLCBBKPA;
  757. }
  758. }
  759. /**
  760. * @brief Get the status flag of ETH_MAC_ISR register.
  761. * @param ETH_MAC_FLAG: the status flag of ETH_MAC_ISR register.
  762. * Select one of the follwing values :
  763. * @arg ETH_MAC_FLAG_TST : Time stamp trigger flag
  764. * @arg ETH_MAC_FLAG_MSCT : MSC transmit flag
  765. * @arg ETH_MAC_FLAG_MSCR : MSC receive flag
  766. * @arg ETH_MAC_FLAG_MSC : MSC flag
  767. * @arg ETH_MAC_FLAG_WUM : WUM flag
  768. * @retval The current MAC bit selected status(SET or RESET).
  769. */
  770. TypeState ETH_GetMACBitState(uint32_t ETH_MAC_FLAG)
  771. {
  772. /* Check the ETH_MAC_FLAG status */
  773. if ((ETH_MAC->ISR & ETH_MAC_FLAG) != (uint32_t)RESET) {
  774. return SET;
  775. } else {
  776. return RESET;
  777. }
  778. }
  779. /**
  780. * @brief Get the interrupt flag of ETH_MAC_ISR register.
  781. * @param ETH_MAC_INT: the interrupt flag of ETH_MAC_ISR register.
  782. * Select one of the follwing values :
  783. * @arg ETH_MAC_INT_TST : Time stamp trigger interrupt
  784. * @arg ETH_MAC_INT_MSCT : MSC transmit interrupt
  785. * @arg ETH_MAC_INT_MSCR : MSC receive interrupt
  786. * @arg ETH_MAC_INT_MSC : MSC interrupt
  787. * @arg ETH_MAC_INT_WUM : WUM interrupt
  788. * @retval The current MAC interrupt bit selected status(SET or RESET).
  789. */
  790. TypeState ETH_GetMACIntBitState(uint32_t ETH_MAC_INT)
  791. {
  792. /* Check the ETH_MAC_INT status */
  793. if ((ETH_MAC->ISR & ETH_MAC_INT) != (uint32_t)RESET) {
  794. return SET;
  795. } else {
  796. return RESET;
  797. }
  798. }
  799. /**
  800. * @brief Enable or disable the ETH_MAC_INT control bit.
  801. * @param ETH_MAC_INT: the interrupt bit flag.
  802. * Select one of the follwing values :
  803. * @arg ETH_MAC_INT_TST : Time stamp trigger interrupt
  804. * @arg ETH_MAC_INT_WUM : WUM interrupt
  805. * @param NewValue: new value of the ETH_MAC_INT value.
  806. * This parameter can be: ENABLE or DISABLE.
  807. * @retval None
  808. */
  809. void ETH_MACINTConfig(uint32_t ETH_MAC_INT, TypeState NewValue)
  810. {
  811. if (NewValue != DISABLE) {
  812. /* Enable the MAC interrupt */
  813. ETH_MAC->IMR &= (~(uint32_t)ETH_MAC_INT);
  814. } else {
  815. /* Disable the MAC interrupt */
  816. ETH_MAC->IMR |= ETH_MAC_INT;
  817. }
  818. }
  819. /**
  820. * @brief Set the MAC address to MAC address register selected.
  821. * @param addr: The MAC address register to selected.
  822. * Select one of the follwing values :
  823. * @arg ETH_MAC_ADDRESS0 : MAC Address0
  824. * @arg ETH_MAC_ADDRESS1 : MAC Address1
  825. * @arg ETH_MAC_ADDRESS2 : MAC Address2
  826. * @arg ETH_MAC_ADDRESS3 : MAC Address3
  827. * @param buf: Pointer to application MAC address buffer(6 bytes).
  828. * @retval None
  829. */
  830. void ETH_SetMACAddress(uint32_t addr, uint8_t *buf)
  831. {
  832. uint32_t temp;
  833. temp = ((uint32_t)buf[5] << 8) | (uint32_t)buf[4];
  834. /* Set the selectecd MAC address high register */
  835. (*(__IO uint32_t *)(ETH_MAC_ADDR_HBASE + addr)) = temp;
  836. temp = ((uint32_t)buf[3] << 24) | ((uint32_t)buf[2] << 16) | ((uint32_t)buf[1] << 8) | buf[0];
  837. /* Set the selectecd MAC address low register */
  838. (*(__IO uint32_t *)(ETH_MAC_ADDR_LBASE + addr)) = temp;
  839. }
  840. /**
  841. * @brief Get the MAC address from MAC address register selected.
  842. * @param addr: The MAC addres to selected.
  843. * Select one of the follwing values :
  844. * @arg ETH_MAC_ADDRESS0 : MAC Address0
  845. * @arg ETH_MAC_ADDRESS1 : MAC Address1
  846. * @arg ETH_MAC_ADDRESS2 : MAC Address2
  847. * @arg ETH_MAC_ADDRESS3 : MAC Address3
  848. * @param buf: Pointer to application MAC address buffer(6 bytes).
  849. * @retval None
  850. */
  851. void ETH_GetMACAddress(uint32_t addr, uint8_t *buf)
  852. {
  853. uint32_t temp;
  854. /* Get the selectecd MAC address high register */
  855. temp = (*(__IO uint32_t *)(ETH_MAC_ADDR_HBASE + addr));
  856. buf[5] = ((temp >> 8) & (uint8_t)0xFF);
  857. buf[4] = (temp & (uint8_t)0xFF);
  858. /* Get the selectecd MAC address low register */
  859. temp = (*(__IO uint32_t *)(ETH_MAC_ADDR_LBASE + addr));
  860. buf[3] = ((temp >> 24) & (uint8_t)0xFF);
  861. buf[2] = ((temp >> 16) & (uint8_t)0xFF);
  862. buf[1] = ((temp >> 8) & (uint8_t)0xFF);
  863. buf[0] = (temp & (uint8_t)0xFF);
  864. }
  865. /**
  866. * @brief Enable or disable the Address perfect filtering.
  867. * @param addr: the MAC address register selected for prfect filtering.
  868. * Select one of the follwing values :
  869. * @arg ETH_MAC_ADDRESS1 : MAC Address1
  870. * @arg ETH_MAC_ADDRESS2 : MAC Address2
  871. * @arg ETH_MAC_ADDRESS3 : MAC Address3
  872. * @param NewValue: new value of the MAC address register selected for perfect filtering.
  873. * This parameter can be: ENABLE or DISABLE.
  874. * @retval None
  875. */
  876. void ETH_MACAddressPerfectFilter_Enable(uint32_t addr, TypeState NewValue)
  877. {
  878. if (NewValue != DISABLE) {
  879. /* Enable the perfect filtering to the MAC address register selected */
  880. (*(__IO uint32_t *)(ETH_MAC_ADDR_HBASE + addr)) |= ETH_MAC_A1HR_AFE;
  881. } else {
  882. /* Disable the perfect filtering to the MAC address register selected */
  883. (*(__IO uint32_t *)(ETH_MAC_ADDR_HBASE + addr)) &= (~(uint32_t)ETH_MAC_A1HR_AFE);
  884. }
  885. }
  886. /**
  887. * @brief Set the selected MAC address filter type mode.
  888. * @param addr: the MAC address to be used for filtering.
  889. * Select one of the follwing values :
  890. * @arg ETH_MAC_ADDRESS1 : MAC Address1
  891. * @arg ETH_MAC_ADDRESS2 : MAC Address2
  892. * @arg ETH_MAC_ADDRESS3 : MAC Address3
  893. * @param Filterfield: the mode of receiving field for comparaison
  894. * Select one of the follwing values :
  895. * @arg ETH_MAC_ADDRESSFILTER_SA : Compare with the SA fields of the received frame.
  896. * @arg ETH_MAC_ADDRESSFILTER_DA : Compare with the DA fields of the received frame.
  897. * @retval None
  898. */
  899. void ETH_MACAddressFilterConfig(uint32_t addr, uint32_t Filterfield)
  900. {
  901. if (Filterfield != ETH_MAC_ADDRESSFILTER_DA) {
  902. /* Compare with the SA fields of the received frame. */
  903. (*(__IO uint32_t *)(ETH_MAC_ADDR_HBASE + addr)) |= ETH_MAC_A1HR_SAF;
  904. } else {
  905. /* compare with the DA fields of the received frame. */
  906. (*(__IO uint32_t *)(ETH_MAC_ADDR_HBASE + addr)) &= (~(uint32_t)ETH_MAC_A1HR_SAF);
  907. }
  908. }
  909. /**
  910. * @brief Set the selected MAC address filter maskbyte.
  911. * @param addr: the MAC address to be used for filtering
  912. * Select one of the follwing values :
  913. * @arg ETH_MAC_ADDRESS1 : MAC Address1
  914. * @arg ETH_MAC_ADDRESS2 : MAC Address2
  915. * @arg ETH_MAC_ADDRESS3 : MAC Address3
  916. * @param addrmask: the address bytes be selected for address filtering comparaison
  917. * Select one of the follwing values :
  918. * @arg ETH_MAC_ADDRESSMASK_BYTE6 : Mask MAC Address high register bits [15:8].
  919. * @arg ETH_MAC_ADDRESSMASK_BYTE5 : Mask MAC Address high register bits [7:0].
  920. * @arg ETH_MAC_ADDRESSMASK_BYTE4 : Mask MAC Address low register bits [31:24].
  921. * @arg ETH_MAC_ADDRESSMASK_BYTE3 : Mask MAC Address low register bits [23:16].
  922. * @arg ETH_MAC_ADDRESSMASK_BYTE2 : Mask MAC Address low register bits [15:8].
  923. * @arg ETH_MAC_ADDRESSMASK_BYTE1 : Mask MAC Address low register bits [7:0].
  924. * @retval None
  925. */
  926. void ETH_MACAddressFilterMaskBytesConfig(uint32_t addr, uint32_t addrmask)
  927. {
  928. /* Clear the MB bit of selected MAC address */
  929. (*(__IO uint32_t *)(ETH_MAC_ADDR_HBASE + addr)) &= (~(uint32_t)ETH_MAC_A1HR_MB);
  930. /* Set the mask bytes of selected Filetr */
  931. (*(__IO uint32_t *)(ETH_MAC_ADDR_HBASE + addr)) |= addrmask;
  932. }
  933. /* DMA Tx/Rx Desciptors */
  934. /**
  935. * @brief Initialize the DMA Tx descriptors's parameters in chain mode.
  936. * @param DMATxDescTab: Pointer to the first Tx descriptor table
  937. * @param pTxBuff: Pointer to the first TxBuffer table
  938. * @param TxBuffCnt: the used Tx desc num in the table
  939. * @retval None
  940. */
  941. void ETH_DMATxDescChainModeInit(ETH_DMADESCTypeDef *DMATxDescTab, uint8_t *pTxBuff, uint32_t TxBuffCnt)
  942. {
  943. uint32_t num = 0;
  944. ETH_DMADESCTypeDef *DMATxDesc;
  945. DMACurrentTxDesc = DMATxDescTab;
  946. /* Configuration each DMATxDesc descriptor */
  947. for (num = 0; num < TxBuffCnt; num++) {
  948. /* Get the pointer to the next descriptor of the Tx Desc table */
  949. DMATxDesc = DMATxDescTab + num;
  950. /* Set TCH bit with desc status */
  951. DMATxDesc->Status = ETH_DMATXDESC_TCHM;
  952. /* Set Buffer1 address pointer to application buffer */
  953. DMATxDesc->Buffer1Addr = (uint32_t)(&pTxBuff[num * ETH_MAX_FRAME_SIZE]);
  954. if (num < (TxBuffCnt - 1)) {
  955. /* Buffer2NextDescAddr equal to next descriptor address in the Tx Desc table */
  956. DMATxDesc->Buffer2NextDescAddr = (uint32_t)(DMATxDescTab + num + 1);
  957. } else {
  958. /* When it is the last descriptor, Buffer2NextDescAddr equal to first descriptor address in the Tx Desc table */
  959. DMATxDesc->Buffer2NextDescAddr = (uint32_t) DMATxDescTab;
  960. }
  961. }
  962. ETH_DMA->TDTAR = (uint32_t) DMATxDescTab;
  963. }
  964. /**
  965. * @brief Initialize the DMA Tx descriptors's parameters in ring mode.
  966. * @param DMATxDescTab: Pointer to the first Tx descriptor table
  967. * @param pTxBuff1: Pointer to the first TxBuffer1 table
  968. * @param pTxBuff2: Pointer to the first TxBuffer2 table
  969. * @param TxBuffCnt: the used Tx desc num in the table
  970. * @retval None
  971. */
  972. void ETH_DMATxDescRingModeInit(ETH_DMADESCTypeDef *DMATxDescTab, uint8_t *pTxBuff1, uint8_t *pTxBuff2, uint32_t TxBuffCnt)
  973. {
  974. uint32_t num = 0;
  975. ETH_DMADESCTypeDef *DMATxDesc;
  976. DMACurrentTxDesc = DMATxDescTab;
  977. /* Configuration each DMATxDesc descriptor */
  978. for (num = 0; num < TxBuffCnt; num++) {
  979. /* Get the pointer to the next descriptor of the Tx Desc table */
  980. DMATxDesc = DMATxDescTab + num;
  981. /* Set Buffer1 address pointer to application buffer1 */
  982. DMATxDesc->Buffer1Addr = (uint32_t)(&pTxBuff1[num * ETH_MAX_FRAME_SIZE]);
  983. /* Set Buffer2 address pointer to application buffer2 */
  984. DMATxDesc->Buffer2NextDescAddr = (uint32_t)(&pTxBuff2[num * ETH_MAX_FRAME_SIZE]);
  985. if (num == (TxBuffCnt - 1)) {
  986. /* Set ETH_DMATXDESC_TERM bit as transmitting End */
  987. DMATxDesc->Status = ETH_DMATXDESC_TERM;
  988. }
  989. }
  990. ETH_DMA->TDTAR = (uint32_t) DMATxDescTab;
  991. }
  992. /**
  993. * @brief Get the bit flag of ETHERNET DMA Tx Desc.
  994. * @param DMATxDesc: pointer to a DMA Tx descriptor
  995. * @param ETH_DMATxDescFlag: the flag of Tx descriptor status.
  996. * Select one of the follwing values :
  997. * @arg ETH_DMATXDESC_BUSY : BUSY bit: descriptor is owned by DMA engine
  998. * @arg ETH_DMATXDESC_INTC : Interrupt on completetion
  999. * @arg ETH_DMATXDESC_LSG : Last Segment
  1000. * @arg ETH_DMATXDESC_FSG : First Segment
  1001. * @arg ETH_DMATXDESC_DCRC : Disable CRC
  1002. * @arg ETH_DMATXDESC_DPAD : Disable Pad
  1003. * @arg ETH_DMATXDESC_TTSEN : Transmit Time Stamp Enable
  1004. * @arg ETH_DMATXDESC_TERM : Transmit End of Ring
  1005. * @arg ETH_DMATXDESC_TCHM : Second Address Chained
  1006. * @arg ETH_DMATXDESC_TTMSS : Tx Time Stamp Status
  1007. * @arg ETH_DMATXDESC_IPHE : IP Header Error
  1008. * @arg ETH_DMATXDESC_ES : Error summary
  1009. * @arg ETH_DMATXDESC_JT : Jabber Timeout
  1010. * @arg ETH_DMATXDESC_FRMF : Frame Flushed: DMA/MTL flushed the frame due to SW flush
  1011. * @arg ETH_DMATXDESC_IPPE : IP Payload Error
  1012. * @arg ETH_DMATXDESC_LCA : Loss of Carrier: carrier lost during tramsmission
  1013. * @arg ETH_DMATXDESC_NCA : No Carrier: no carrier signal from the tranceiver
  1014. * @arg ETH_DMATXDESC_LCO : Late Collision: transmission aborted due to collision
  1015. * @arg ETH_DMATXDESC_ECO : Excessive Collision: transmission aborted after 16 collisions
  1016. * @arg ETH_DMATXDESC_VFRM : VLAN Frame
  1017. * @arg ETH_DMATXDESC_COCNT : Collision Count
  1018. * @arg ETH_DMATXDESC_EXD : Excessive Deferral
  1019. * @arg ETH_DMATXDESC_UFE : Underflow Error: late data arrival from the memory
  1020. * @arg ETH_DMATXDESC_DB : Deferred Bit
  1021. * @retval The current ETH_DMATxDescFlag selected bit status(SET or RESET).
  1022. */
  1023. TypeState ETH_GetDMATxDescBitState(ETH_DMADESCTypeDef *DMATxDesc, uint32_t ETH_DMATxDescFlag)
  1024. {
  1025. if ((DMATxDesc->Status & ETH_DMATxDescFlag) != (uint32_t)RESET) {
  1026. /* ETH_DMATxDescFlag is set */
  1027. return SET;
  1028. } else {
  1029. /* ETH_DMATxDescFlag is reset */
  1030. return RESET;
  1031. }
  1032. }
  1033. /**
  1034. * @brief Get the DMA Tx Desc collision count.
  1035. * @param DMATxDesc: pointer to a DMA Tx descriptor
  1036. * @retval The value of Transmit descriptor collision counter.
  1037. */
  1038. uint32_t ETH_GetDMATxDescCollisionCount(ETH_DMADESCTypeDef *DMATxDesc)
  1039. {
  1040. return ((DMATxDesc->Status & ETH_DMATXDESC_COCNT) >> ETH_DMATXDESC_COLLISION_COUNTSHIFT);
  1041. }
  1042. /**
  1043. * @brief Set the DMA Tx Desc Busy bit for DMA or CPU.
  1044. * @param DMATxDesc: Pointer on a Transmit descriptor
  1045. * @retval None
  1046. */
  1047. void ETH_SetDMATxDescBusyBit(ETH_DMADESCTypeDef *DMATxDesc)
  1048. {
  1049. DMATxDesc->Status |= ETH_DMATXDESC_BUSY;
  1050. }
  1051. /**
  1052. * @brief Enable or disable the ETH_DMATXDESC_INTC control bit.
  1053. * @param DMATxDesc: Pointer to a Tx descriptor
  1054. * @param NewValue: new value of the ETH_DMATXDESC_INTC control bit.
  1055. * This parameter can be: ENABLE or DISABLE.
  1056. * @retval None
  1057. */
  1058. void ETH_DMATxDescTransmitINTConfig(ETH_DMADESCTypeDef *DMATxDesc, TypeState NewValue)
  1059. {
  1060. if (NewValue != DISABLE) {
  1061. /* Enable the DMA Tx Desc after the frame has been transmitted Set Transmit interrupt */
  1062. DMATxDesc->Status |= ETH_DMATXDESC_INTC;
  1063. } else {
  1064. /* Disable the DMA Tx Desc after the frame has been transmitted Set Transmit interrupt */
  1065. DMATxDesc->Status &= (~(uint32_t)ETH_DMATXDESC_INTC);
  1066. }
  1067. }
  1068. /**
  1069. * @brief Enable or disable the DMATxDesc_FrameSegment control bit.
  1070. * @param DMATxDesc: Pointer to a Tx descriptor
  1071. * @param DMATxDesc_Segment: the actual Tx buffer contain first or last segment.
  1072. * Select one of the follwing values :
  1073. * @arg ETH_DMATXDESC_LASTSEGMENT : current Tx desc selected contain last segment
  1074. * @arg ETH_DMATXDESC_FIRSTSEGMENT : current Tx desc selected contain first segment
  1075. * @retval None
  1076. */
  1077. void ETH_SetDMATxDescFrameSegment(ETH_DMADESCTypeDef *DMATxDesc, uint32_t DMATxDesc_Segment)
  1078. {
  1079. DMATxDesc->Status |= DMATxDesc_Segment;
  1080. }
  1081. /**
  1082. * @brief Set the DMA Tx Desc Checksum Insertion mode.
  1083. * @param DMATxDesc: pointer to a DMA Tx descriptor
  1084. * @param DMATxDesc_Checksum: the type of DMA Tx descriptor checksum insertion.
  1085. * Select one of the follwing values :
  1086. * @arg ETH_DMATXDESC_CHECKSUMDISABLE : Checksum bypass
  1087. * @arg ETH_DMATXDESC_CHECKSUMIPV4HEADER : IPv4 header checksum
  1088. * @arg ETH_DMATXDESC_CHECKSUMTCPUDPICMPSEGMENT : TCP/UDP/ICMP checksum. Pseudo header checksum is assumed to be present
  1089. * @arg ETH_DMATXDESC_CHECKSUMTCPUDPICMPFULL : TCP/UDP/ICMP checksum fully in hardware including pseudo header
  1090. * @retval None
  1091. */
  1092. void ETH_SetDMATxDescChecksumInsertion(ETH_DMADESCTypeDef *DMATxDesc, uint32_t DMATxDesc_Checksum)
  1093. {
  1094. DMATxDesc->Status |= DMATxDesc_Checksum;
  1095. }
  1096. /**
  1097. * @brief Enable or disable the DMA Tx descriptor CRC function.
  1098. * @param DMATxDesc: pointer to a DMA Tx descriptor
  1099. * @param NewValue: new value of the DMA Tx descriptor CRC function.
  1100. * This parameter can be: ENABLE or DISABLE.
  1101. * @retval None
  1102. */
  1103. void ETH_DMATxDescCRC_Enable(ETH_DMADESCTypeDef *DMATxDesc, TypeState NewValue)
  1104. {
  1105. if (NewValue != DISABLE) {
  1106. /* Enable the DMATxDesc CRC */
  1107. DMATxDesc->Status &= (~(uint32_t)ETH_DMATXDESC_DCRC);
  1108. } else {
  1109. /* Disable the DMATxDesc CRC */
  1110. DMATxDesc->Status |= ETH_DMATXDESC_DCRC;
  1111. }
  1112. }
  1113. /**
  1114. * @brief Enable or disable the DMA Tx descriptor end of ring.
  1115. * @param DMATxDesc: pointer to a DMA Tx descriptor
  1116. * @param NewValue: new value of the DMA Tx descriptor end of ring.
  1117. * This parameter can be: ENABLE or DISABLE.
  1118. * @retval None
  1119. */
  1120. void ETH_DMATxDescEndOfRing_Enable(ETH_DMADESCTypeDef *DMATxDesc, TypeState NewValue)
  1121. {
  1122. if (NewValue != DISABLE) {
  1123. /* Enable the DMATxDesc end of ring */
  1124. DMATxDesc->Status |= ETH_DMATXDESC_TERM;
  1125. } else {
  1126. /* Disable the DMATxDesc end of ring */
  1127. DMATxDesc->Status &= (~(uint32_t)ETH_DMATXDESC_TERM);
  1128. }
  1129. }
  1130. /**
  1131. * @brief Enable or disable the DMA Tx descriptor second address chained.
  1132. * @param DMATxDesc: pointer to a DMA Tx descriptor
  1133. * @param NewValue: new value of the DMA Tx descriptor second address chained.
  1134. * This parameter can be: ENABLE or DISABLE.
  1135. * @retval None
  1136. */
  1137. void ETH_DMATxDescSecondAddressChained_Enable(ETH_DMADESCTypeDef *DMATxDesc, TypeState NewValue)
  1138. {
  1139. if (NewValue != DISABLE) {
  1140. /* Enable the DMATxDesc second address chained */
  1141. DMATxDesc->Status |= ETH_DMATXDESC_TCHM;
  1142. } else {
  1143. /* Disable the DMATxDesc second address chained */
  1144. DMATxDesc->Status &= (~(uint32_t)ETH_DMATXDESC_TCHM);
  1145. }
  1146. }
  1147. /**
  1148. * @brief Enable or disable auto padding when transmit frame shorter than 64 bytes.
  1149. * @param DMATxDesc: pointer to a DMA Tx descriptor
  1150. * @param NewValue: new value of the auto padding status.
  1151. * This parameter can be: ENABLE or DISABLE.
  1152. * @retval None
  1153. */
  1154. void ETH_DMATxDescShortFramePadding_Enable(ETH_DMADESCTypeDef *DMATxDesc, TypeState NewValue)
  1155. {
  1156. if (NewValue != DISABLE) {
  1157. /* Enable the DMATxDesc short frame padding */
  1158. DMATxDesc->Status &= (~(uint32_t)ETH_DMATXDESC_DPAD);
  1159. } else {
  1160. /* Disable the DMATxDesc short frame padding */
  1161. DMATxDesc->Status |= ETH_DMATXDESC_DPAD;
  1162. }
  1163. }
  1164. /**
  1165. * @brief Enable or disable the DMA Tx descriptor time stamp function.
  1166. * @param DMATxDesc: pointer to a DMA Tx descriptor
  1167. * @param NewValue: new value of the DMA Tx descriptor time stamp.
  1168. * This parameter can be: ENABLE or DISABLE.
  1169. * @retval None
  1170. */
  1171. void ETH_DMATxDescTimeStamp_Enable(ETH_DMADESCTypeDef *DMATxDesc, TypeState NewValue)
  1172. {
  1173. if (NewValue != DISABLE) {
  1174. /* Enable the DMATxDesc time stamp */
  1175. DMATxDesc->Status |= ETH_DMATXDESC_TTSEN;
  1176. } else {
  1177. /* Disable the DMATxDesc time stamp */
  1178. DMATxDesc->Status &= (~(uint32_t)ETH_DMATXDESC_TTSEN);
  1179. }
  1180. }
  1181. /**
  1182. * @brief Set the frame length by configures the DMA Tx Desc buffer1 and buffer2 sizes.
  1183. * @param DMATxDesc: Pointer to a Tx descriptor
  1184. * @param Buffer1Size: the Tx buffer1 size.
  1185. * @param Buffer2Size: the Tx buffer2 size, set to 0 indicates it is not being used.
  1186. * @retval None
  1187. */
  1188. void ETH_SetDMATxDescBufferSize(ETH_DMADESCTypeDef *DMATxDesc, uint32_t Buffer1Size, uint32_t Buffer2Size)
  1189. {
  1190. DMATxDesc->ControlBufferSize |= (Buffer1Size | (Buffer2Size << ETH_DMATXDESC_BUFFER2_SIZESHIFT));
  1191. }
  1192. /**
  1193. * @brief Initialize the DMA Rx descriptors's parameters in chain mode.
  1194. * @param DMARxDescTab: Pointer to the first Rx descriptor table
  1195. * @param pRxBuff: Pointer to the first RxBuffer table
  1196. * @param RxBuffCnt: the used Rx desc num in the table
  1197. * @retval None
  1198. */
  1199. void ETH_DMARxDescChainModeInit(ETH_DMADESCTypeDef *DMARxDescTab, uint8_t *pRxBuff, uint32_t RxBuffCnt)
  1200. {
  1201. uint32_t num = 0;
  1202. ETH_DMADESCTypeDef *DMARxDesc;
  1203. DMACurrentRxDesc = DMARxDescTab;
  1204. /* Configuration each DMARxDesc descriptor */
  1205. for (num = 0; num < RxBuffCnt; num++) {
  1206. /* Get the pointer to the next descriptor of the Rx Desc table */
  1207. DMARxDesc = DMARxDescTab + num;
  1208. DMARxDesc->Status = ETH_DMARXDESC_BUSY;
  1209. /* Set TCH bit and buffer1 size */
  1210. DMARxDesc->ControlBufferSize = ETH_DMARXDESC_RCHM | (uint32_t)ETH_MAX_FRAME_SIZE;
  1211. /* Set Buffer1 address pointer to application buffer */
  1212. DMARxDesc->Buffer1Addr = (uint32_t)(&pRxBuff[num * ETH_MAX_FRAME_SIZE]);
  1213. if (num < (RxBuffCnt - 1)) {
  1214. /* Buffer2NextDescAddr equal to next descriptor address in the Rx Desc table */
  1215. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab + num + 1);
  1216. } else {
  1217. /* When it is the last descriptor, Buffer2NextDescAddr equal to first descriptor address in the Rx Desc table */
  1218. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab);
  1219. }
  1220. }
  1221. ETH_DMA->RDTAR = (uint32_t) DMARxDescTab;
  1222. }
  1223. /**
  1224. * @brief Initialize the DMA Rx descriptors's parameters in ring mode.
  1225. * @param DMARxDescTab: Pointer to the first Rx descriptor table
  1226. * @param pRxBuff1: Pointer to the first RxBuffer1 table
  1227. * @param pRxBuff2: Pointer to the first RxBuffer2 table
  1228. * @param RxBuffCnt: the used Rx descriptor num in the table
  1229. * @retval None
  1230. */
  1231. void ETH_DMARxDescRingModeInit(ETH_DMADESCTypeDef *DMARxDescTab, uint8_t *pRxBuff1, uint8_t *pRxBuff2, uint32_t RxBuffCnt)
  1232. {
  1233. uint32_t num = 0;
  1234. ETH_DMADESCTypeDef *DMARxDesc;
  1235. DMACurrentRxDesc = DMARxDescTab;
  1236. /* Configuration each DMARxDesc descriptor */
  1237. for (num = 0; num < RxBuffCnt; num++) {
  1238. /* Get the pointer to the next descriptor of the Rx Desc table */
  1239. DMARxDesc = DMARxDescTab + num;
  1240. DMARxDesc->Status = ETH_DMARXDESC_BUSY;
  1241. DMARxDesc->ControlBufferSize = ETH_MAX_FRAME_SIZE;
  1242. /* Set Buffer1 address pointer to application buffer1 */
  1243. DMARxDesc->Buffer1Addr = (uint32_t)(&pRxBuff1[num * ETH_MAX_FRAME_SIZE]);
  1244. /* Set Buffer2 address pointer to application buffer2 */
  1245. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(&pRxBuff2[num * ETH_MAX_FRAME_SIZE]);
  1246. if (num == (RxBuffCnt - 1)) {
  1247. /* Set ETH_DMARXDESC_RERR bit as Receive End */
  1248. DMARxDesc->ControlBufferSize |= ETH_DMARXDESC_RERR;
  1249. }
  1250. }
  1251. ETH_DMA->RDTAR = (uint32_t) DMARxDescTab;
  1252. }
  1253. /**
  1254. * @brief Get the bit flag of ETHERNET Rx descriptor.
  1255. * @param DMARxDesc: pointer to a DMA Rx descriptor
  1256. * @param ETH_DMARxDescFlag: the flag of Rx descriptor status.
  1257. * Select one of the follwing values :
  1258. * @arg ETH_DMARXDESC_BUSY : Descriptor is owned by DMA engine
  1259. * @arg ETH_DMARXDESC_DAFF : DA Filter Fail for the rx frame
  1260. * @arg ETH_DMARXDESC_ERRS : Error summary
  1261. * @arg ETH_DMARXDESC_DERR : Desciptor error: no more descriptors for receive frame
  1262. * @arg ETH_DMARXDESC_SAFF : SA Filter Fail for the received frame
  1263. * @arg ETH_DMARXDESC_LERR : Frame size not matching with length field
  1264. * @arg ETH_DMARXDESC_OERR : Overflow Error: Frame was damaged due to buffer overflow
  1265. * @arg ETH_DMARXDESC_VTAG : VLAN Tag: received frame is a VLAN frame
  1266. * @arg ETH_DMARXDESC_FDES : First descriptor of the frame
  1267. * @arg ETH_DMARXDESC_LDES : Last descriptor of the frame
  1268. * @arg ETH_DMARXDESC_IPHCERR : IPC Checksum Error/Giant Frame: Rx Ipv4 header checksum error
  1269. * @arg ETH_DMARXDESC_LCO : Late collision occurred during reception
  1270. * @arg ETH_DMARXDESC_FRMT : Frame type - Ethernet, otherwise 802.3
  1271. * @arg ETH_DMARXDESC_RWDT : Receive Watchdog Timeout: watchdog timer expired during reception
  1272. * @arg ETH_DMARXDESC_RERR : Receive error: error reported by MII interface
  1273. * @arg ETH_DMARXDESC_DERR : Dribble bit error: frame contains non int multiple of 8 bits
  1274. * @arg ETH_DMARXDESC_CERR : CRC error
  1275. * @arg ETH_DMARXDESC_PCERR : Rx MAC Address/Payload Checksum Error: Rx MAC address matched/ Rx Payload Checksum Error
  1276. * @retval The current ETH_DMARxDescFlag selected bit status(SET or RESET).
  1277. */
  1278. TypeState ETH_GetDMARxDescBitState(ETH_DMADESCTypeDef *DMARxDesc, uint32_t ETH_DMARxDescFlag)
  1279. {
  1280. if ((DMARxDesc->Status & ETH_DMARxDescFlag) != (uint32_t)RESET) {
  1281. /* ETH_DMARxDescFlag is set */
  1282. return SET;
  1283. } else {
  1284. /* ETH_DMARxDescFlag is reset */
  1285. return RESET;
  1286. }
  1287. }
  1288. /**
  1289. * @brief Set the DMA Rx Desc busy bit for DMA or CPU
  1290. * @param DMARxDesc: Pointer to a Rx descriptor
  1291. * @retval None
  1292. */
  1293. void ETH_SetDMARxDescBusyBit(ETH_DMADESCTypeDef *DMARxDesc)
  1294. {
  1295. DMARxDesc->Status |= ETH_DMARXDESC_BUSY;
  1296. }
  1297. /**
  1298. * @brief Get the DMA Rx Desc frame length.
  1299. * @param DMARxDesc: pointer to a DMA Rx descriptor
  1300. * @retval Received frame length of the Rx descriptor selected.
  1301. */
  1302. uint32_t ETH_GetDMARxDescFrameLength(ETH_DMADESCTypeDef *DMARxDesc)
  1303. {
  1304. return ((DMARxDesc->Status & ETH_DMARXDESC_FRML) >> ETH_DMARXDESC_FRAME_LENGTHSHIFT);
  1305. }
  1306. /**
  1307. * @brief Enable or disable the DMA Rx descriptor receive interrupt.
  1308. * @param DMARxDesc: Pointer to a Rx descriptor
  1309. * @param NewValue: new value of the DMA Rx descriptor interrupt.
  1310. * This parameter can be: ENABLE or DISABLE.
  1311. * @retval None
  1312. */
  1313. void ETH_DMARxDescReceiveINTConfig(ETH_DMADESCTypeDef *DMARxDesc, TypeState NewValue)
  1314. {
  1315. if (NewValue != DISABLE) {
  1316. /* Enable the DMARxDesc receive interrupt */
  1317. DMARxDesc->ControlBufferSize &= (~(uint32_t)ETH_DMARXDESC_DINTC);
  1318. } else {
  1319. /* Disable the DMARxDesc receive interrupt */
  1320. DMARxDesc->ControlBufferSize |= ETH_DMARXDESC_DINTC;
  1321. }
  1322. }
  1323. /**
  1324. * @brief Enable or disable the DMA Rx descriptor end of ring.
  1325. * @param DMARxDesc: pointer to a DMA Rx descriptor
  1326. * @param NewValue: new value of the DMA Rx descriptor end of ring.
  1327. * This parameter can be: ENABLE or DISABLE.
  1328. * @retval None
  1329. */
  1330. void ETH_DMARxDescEndOfRing_Enable(ETH_DMADESCTypeDef *DMARxDesc, TypeState NewValue)
  1331. {
  1332. if (NewValue != DISABLE) {
  1333. /* Enable the DMARxDesc end of ring */
  1334. DMARxDesc->ControlBufferSize |= ETH_DMARXDESC_RERR;
  1335. } else {
  1336. /* Disable the DMARxDesc end of ring */
  1337. DMARxDesc->ControlBufferSize &= (~(uint32_t)ETH_DMARXDESC_RERR);
  1338. }
  1339. }
  1340. /**
  1341. * @brief Enable or disable the DMA Rx descriptor second address chained.
  1342. * @param DMARxDesc: pointer to a DMA Rx descriptor
  1343. * @param NewValue: new value of the DMA Rx descriptor second address chained.
  1344. * This parameter can be: ENABLE or DISABLE.
  1345. * @retval None
  1346. */
  1347. void ETH_DMARxDescSecondAddressChained_Enable(ETH_DMADESCTypeDef *DMARxDesc, TypeState NewValue)
  1348. {
  1349. if (NewValue != DISABLE) {
  1350. /* Enable the DMARxDesc second address chained */
  1351. DMARxDesc->ControlBufferSize |= ETH_DMARXDESC_RCHM;
  1352. } else {
  1353. /* Disable the DMARxDesc second address chained */
  1354. DMARxDesc->ControlBufferSize &= (~(uint32_t)ETH_DMARXDESC_RCHM);
  1355. }
  1356. }
  1357. /**
  1358. * @brief Get the ETHERNET DMA Rx Desc buffer size.
  1359. * @param DMARxDesc: pointer to a DMA Rx descriptor
  1360. * @param DMARxDesc_BufferSelect: the DMA Rx descriptor buffer.
  1361. * Select one of the follwing values :
  1362. * @arg ETH_DMARXDESC_BUFFER1 : DMA Rx Desc Buffer1
  1363. * @arg ETH_DMARXDESC_BUFFER2 : DMA Rx Desc Buffer2
  1364. * @retval The Receive descriptor selected buffer size(buffer1 or buffer2).
  1365. */
  1366. uint32_t ETH_GetDMARxDescBufferSize(ETH_DMADESCTypeDef *DMARxDesc, uint32_t DMARxDesc_BufferSelect)
  1367. {
  1368. if (DMARxDesc_BufferSelect != ETH_DMARXDESC_BUFFER1) {
  1369. return ((DMARxDesc->ControlBufferSize & ETH_DMARXDESC_RB2S) >> ETH_DMARXDESC_BUFFER2_SIZESHIFT);
  1370. } else {
  1371. return (DMARxDesc->ControlBufferSize & ETH_DMARXDESC_RB1S);
  1372. }
  1373. }
  1374. /* DMA */
  1375. /**
  1376. * @brief Resets all MAC subsystem.
  1377. * @param None
  1378. * @retval None
  1379. */
  1380. void ETH_SoftReset(void)
  1381. {
  1382. ETH_DMA->BCR |= ETH_DMA_BCR_SWR;
  1383. }
  1384. /**
  1385. * @brief Get the bit flag of ETHERNET software reset.
  1386. * @param None
  1387. * @retval The current DMA Bus Mode register SWR bit status(SET or RESET).
  1388. */
  1389. TypeState ETH_GetSoftResetStatus(void)
  1390. {
  1391. if ((ETH_DMA->BCR & ETH_DMA_BCR_SWR) != (uint32_t)RESET) {
  1392. /* The ETH_DMA_BCR_SWR bit is set */
  1393. return SET;
  1394. } else {
  1395. /* The ETH_DMA_BCR_SWR bit is reset */
  1396. return RESET;
  1397. }
  1398. }
  1399. /**
  1400. * @brief Get the bit flag of specified ETHERNET DMA.
  1401. * @param ETH_DMA_FLAG: the flag of ETHERNET DMA_STR register.
  1402. * Select one of the follwing values :
  1403. * @arg ETH_DMA_FLAG_TST : Time-stamp trigger flag
  1404. * @arg ETH_DMA_FLAG_WUM : WUM flag
  1405. * @arg ETH_DMA_FLAG_MSC : MSC flag
  1406. * @arg ETH_DMA_FLAG_DATATRANSFERERROR : Error bits 0-data buffer, 1-desc. access
  1407. * @arg ETH_DMA_FLAG_READWRITEERROR : Error bits 0-write trnsf, 1-read transfr
  1408. * @arg ETH_DMA_FLAG_ACCESSERROR : Error bits 0-Rx DMA, 1-Tx DMA
  1409. * @arg ETH_DMA_FLAG_NIS : Normal interrupt summary flag
  1410. * @arg ETH_DMA_FLAG_AIS : Abnormal interrupt summary flag
  1411. * @arg ETH_DMA_FLAG_ER : Early receive flag
  1412. * @arg ETH_DMA_FLAG_FBE : Fatal bus error flag
  1413. * @arg ETH_DMA_FLAG_ET : Early transmit flag
  1414. * @arg ETH_DMA_FLAG_RWT : Receive watchdog timeout flag
  1415. * @arg ETH_DMA_FLAG_RPS : Receive process stopped flag
  1416. * @arg ETH_DMA_FLAG_RBU : Receive buffer unavailable flag
  1417. * @arg ETH_DMA_FLAG_R : Receive flag
  1418. * @arg ETH_DMA_FLAG_TU : Underflow flag
  1419. * @arg ETH_DMA_FLAG_RO : Overflow flag
  1420. * @arg ETH_DMA_FLAG_TJT : Transmit jabber timeout flag
  1421. * @arg ETH_DMA_FLAG_TBU : Transmit buffer unavailable flag
  1422. * @arg ETH_DMA_FLAG_TPS : Transmit process stopped flag
  1423. * @arg ETH_DMA_FLAG_T : Transmit flag
  1424. * @retval The current ETH_DMA_FLAG selected bit status(SET or RESET).
  1425. */
  1426. TypeState ETH_GetDMABitState(uint32_t ETH_DMA_FLAG)
  1427. {
  1428. if ((ETH_DMA->STR & ETH_DMA_FLAG) != (uint32_t)RESET) {
  1429. /* ETH_DMA_FLAG is set */
  1430. return SET;
  1431. } else {
  1432. /* ETH_DMA_FLAG is reset */
  1433. return RESET;
  1434. }
  1435. }
  1436. /**
  1437. * @brief Clear the ETHERNET¡¯s DMA bit flag.
  1438. * @param ETH_DMA_FLAG: the flag of ETH_DMA_STR register to clear.
  1439. * Select one of the follwing values :
  1440. * @arg ETH_DMA_FLAG_NIS : Normal interrupt summary flag
  1441. * @arg ETH_DMA_FLAG_AIS : Abnormal interrupt summary flag
  1442. * @arg ETH_DMA_FLAG_ER : Early receive flag
  1443. * @arg ETH_DMA_FLAG_FBE : Fatal bus error flag
  1444. * @arg ETH_DMA_FLAG_ETI : Early transmit flag
  1445. * @arg ETH_DMA_FLAG_RWT : Receive watchdog timeout flag
  1446. * @arg ETH_DMA_FLAG_RPS : Receive process stopped flag
  1447. * @arg ETH_DMA_FLAG_RBU : Receive buffer unavailable flag
  1448. * @arg ETH_DMA_FLAG_R : Receive flag
  1449. * @arg ETH_DMA_FLAG_TU : Transmit Underflow flag
  1450. * @arg ETH_DMA_FLAG_RO : Receive Overflow flag
  1451. * @arg ETH_DMA_FLAG_TJT : Transmit jabber timeout flag
  1452. * @arg ETH_DMA_FLAG_TBU : Transmit buffer unavailable flag
  1453. * @arg ETH_DMA_FLAG_TPS : Transmit process stopped flag
  1454. * @arg ETH_DMA_FLAG_T : Transmit flag
  1455. * @retval None
  1456. */
  1457. void ETH_DMAClearBitState(uint32_t ETH_DMA_FLAG)
  1458. {
  1459. ETH_DMA->STR = (uint32_t) ETH_DMA_FLAG;
  1460. }
  1461. /**
  1462. * @brief Get the bit flag of DMA interrupt.
  1463. * @param ETH_DMA_INT: the interrupt source flag of ETHERNET DMA_STR register.
  1464. * Select one of the follwing values :
  1465. * @arg ETH_DMA_INT_TST : Time-stamp trigger interrupt
  1466. * @arg ETH_DMA_INT_WUM : WUM interrupt
  1467. * @arg ETH_DMA_INT_MSC : MSC interrupt
  1468. * @arg ETH_DMA_INT_NIS : Normal interrupt summary
  1469. * @arg ETH_DMA_INT_AIS : Abnormal interrupt summary
  1470. * @arg ETH_DMA_INT_ER : Early receive interrupt
  1471. * @arg ETH_DMA_INT_FBE : Fatal bus error interrupt
  1472. * @arg ETH_DMA_INT_ET : Early transmit interrupt
  1473. * @arg ETH_DMA_INT_RWT : Receive watchdog timeout interrupt
  1474. * @arg ETH_DMA_INT_RPS : Receive process stopped interrupt
  1475. * @arg ETH_DMA_INT_RBU : Receive buffer unavailable interrupt
  1476. * @arg ETH_DMA_INT_R : Receive interrupt
  1477. * @arg ETH_DMA_INT_TU : Underflow interrupt
  1478. * @arg ETH_DMA_INT_RO : Overflow interrupt
  1479. * @arg ETH_DMA_INT_TJT : Transmit jabber timeout interrupt
  1480. * @arg ETH_DMA_INT_TBU : Transmit buffer unavailable interrupt
  1481. * @arg ETH_DMA_INT_TPS : Transmit process stopped interrupt
  1482. * @arg ETH_DMA_INT_T : Transmit interrupt
  1483. * @retval The current ETH_DMA_INT selected bit status(SET or RESET).
  1484. */
  1485. TypeState ETH_GetDMAIntBitState(uint32_t ETH_DMA_INT)
  1486. {
  1487. if ((ETH_DMA->STR & ETH_DMA_INT) != (uint32_t)RESET) {
  1488. /* ETH_DMA_INT is set */
  1489. return SET;
  1490. } else {
  1491. /* ETH_DMA_INT is reset */
  1492. return RESET;
  1493. }
  1494. }
  1495. /**
  1496. * @brief Clear the ETHERNET¡¯s DMA IT bit flag.
  1497. * @param ETH_DMA_INT: the interrupt source flag of ETH_DMA_STR register to clear.
  1498. * Select one of the follwing values :
  1499. * @arg ETH_DMA_INT_NIS : Normal interrupt summary
  1500. * @arg ETH_DMA_INT_AIS : Abnormal interrupt summary
  1501. * @arg ETH_DMA_INT_ER : Early receive interrupt
  1502. * @arg ETH_DMA_INT_FBE : Fatal bus error interrupt
  1503. * @arg ETH_DMA_INT_ETI : Early transmit interrupt
  1504. * @arg ETH_DMA_INT_RWT : Receive watchdog timeout interrupt
  1505. * @arg ETH_DMA_INT_RPS : Receive process stopped interrupt
  1506. * @arg ETH_DMA_INT_RBU : Receive buffer unavailable interrupt
  1507. * @arg ETH_DMA_INT_R : Receive interrupt
  1508. * @arg ETH_DMA_INT_TU : Transmit Underflow interrupt
  1509. * @arg ETH_DMA_INT_RO : Receive Overflow interrupt
  1510. * @arg ETH_DMA_INT_TJT : Transmit jabber timeout interrupt
  1511. * @arg ETH_DMA_INT_TBU : Transmit buffer unavailable interrupt
  1512. * @arg ETH_DMA_INT_TPS : Transmit process stopped interrupt
  1513. * @arg ETH_DMA_INT_T : Transmit interrupt
  1514. * @retval None
  1515. */
  1516. void ETH_DMAClearIntBitState(uint32_t ETH_DMA_INT)
  1517. {
  1518. ETH_DMA->STR = (uint32_t) ETH_DMA_INT;
  1519. }
  1520. /**
  1521. * @brief Get the DMA Transmit Process State.
  1522. * @param None
  1523. * @retval The current DMA Transmit Process State:
  1524. * Select one of the follwing values :
  1525. * - ETH_DMA_TransmitProcess_Stopped : Stopped - Reset or Stop Tx Command issued
  1526. * - ETH_DMA_TransmitProcess_Fetching : Running - fetching the Tx descriptor
  1527. * - ETH_DMA_TransmitProcess_Waiting : Running - waiting for status
  1528. * - ETH_DMA_TransmitProcess_Reading : unning - reading the data from host memory
  1529. * - ETH_DMA_TransmitProcess_Suspended : Suspended - Tx Desciptor unavailabe
  1530. * - ETH_DMA_TransmitProcess_Closing : Running - closing Rx descriptor
  1531. */
  1532. uint32_t ETH_GetTransmitProcessState(void)
  1533. {
  1534. return ((uint32_t)(ETH_DMA->STR & ETH_DMA_STR_TS));
  1535. }
  1536. /**
  1537. * @brief Get the DMA Receive Process State.
  1538. * @param None
  1539. * @retval The current DMA Receive Process State:
  1540. * Select one of the follwing values :
  1541. * - ETH_DMA_ReceiveProcess_Stopped : Stopped - Reset or Stop Rx Command issued
  1542. * - ETH_DMA_ReceiveProcess_Fetching : Running - fetching the Rx descriptor
  1543. * - ETH_DMA_ReceiveProcess_Waiting : Running - waiting for packet
  1544. * - ETH_DMA_ReceiveProcess_Suspended : Suspended - Rx Desciptor unavailable
  1545. * - ETH_DMA_ReceiveProcess_Closing : Running - closing descriptor
  1546. * - ETH_DMA_ReceiveProcess_Queuing : Running - queuing the recieve frame into host memory
  1547. */
  1548. uint32_t ETH_GetReceiveProcessState(void)
  1549. {
  1550. return ((uint32_t)(ETH_DMA->STR & ETH_DMA_STR_RS));
  1551. }
  1552. /**
  1553. * @brief Flush the ETHERNET transmit FIFO.
  1554. * @param None
  1555. * @retval None
  1556. */
  1557. void ETH_CleanTransmitFIFO(void)
  1558. {
  1559. /* Set the FTF bit for Flushing Transmit FIFO */
  1560. ETH_DMA->CTLR |= ETH_DMA_CTLR_FTF;
  1561. }
  1562. /**
  1563. * @brief Get the bit flag of ETHERNET transmit FIFO.
  1564. * @param None
  1565. * @retval The current ETHERNET flush transmit FIFO bit status(SET or RESET).
  1566. */
  1567. TypeState ETH_GetFlushTransmitFIFOStatus(void)
  1568. {
  1569. if ((ETH_DMA->CTLR & ETH_DMA_CTLR_FTF) != (uint32_t)RESET) {
  1570. /* Flush transmit FIFO bit is set */
  1571. return SET;
  1572. } else {
  1573. /* Flush transmit FIFO bit is reset */
  1574. return RESET;
  1575. }
  1576. }
  1577. /**
  1578. * @brief Enable or disable the DMA transmission function.
  1579. * @param NewValue: new value of the DMA transmission status.
  1580. * This parameter can be: ENABLE or DISABLE.
  1581. * @retval None
  1582. */
  1583. void ETH_DMATransmission_Enable(TypeState NewValue)
  1584. {
  1585. if (NewValue != DISABLE) {
  1586. /* Set the STE bit for Enable the DMA transmission */
  1587. ETH_DMA->CTLR |= ETH_DMA_CTLR_STE;
  1588. } else {
  1589. /* Reset the STE bit for Disable the DMA transmission */
  1590. ETH_DMA->CTLR &= ~ETH_DMA_CTLR_STE;
  1591. }
  1592. }
  1593. /**
  1594. * @brief Enable or disable the DMA reception function.
  1595. * @param NewValue: new value of the DMA reception status.
  1596. * This parameter can be: ENABLE or DISABLE.
  1597. * @retval None
  1598. */
  1599. void ETH_DMAReception_Enable(TypeState NewValue)
  1600. {
  1601. if (NewValue != DISABLE) {
  1602. /* Enable DMA reception */
  1603. ETH_DMA->CTLR |= ETH_DMA_CTLR_SRE;
  1604. } else {
  1605. /* Disable DMA reception */
  1606. ETH_DMA->CTLR &= ~ETH_DMA_CTLR_SRE;
  1607. }
  1608. }
  1609. /**
  1610. * @brief Enable or disable the ETH_DMA_INT control bit.
  1611. * @param ETH_DMA_INT: the interrupt source flag of ETH_DMA_IER register.
  1612. * Select one of the follwing values :
  1613. * @arg ETH_DMA_INT_NIS : Normal interrupt summary
  1614. * @arg ETH_DMA_INT_AIS : Abnormal interrupt summary
  1615. * @arg ETH_DMA_INT_ER : Early receive interrupt
  1616. * @arg ETH_DMA_INT_FBE : Fatal bus error interrupt
  1617. * @arg ETH_DMA_INT_ET : Early transmit interrupt
  1618. * @arg ETH_DMA_INT_RWT : Receive watchdog timeout interrupt
  1619. * @arg ETH_DMA_INT_RPS : Receive process stopped interrupt
  1620. * @arg ETH_DMA_INT_RBU : Receive buffer unavailable interrupt
  1621. * @arg ETH_DMA_INT_R : Receive interrupt
  1622. * @arg ETH_DMA_INT_TU : Underflow interrupt
  1623. * @arg ETH_DMA_INT_RO : Overflow interrupt
  1624. * @arg ETH_DMA_INT_TJT : Transmit jabber timeout interrupt
  1625. * @arg ETH_DMA_INT_TBU : Transmit buffer unavailable interrupt
  1626. * @arg ETH_DMA_INT_TPS : Transmit process stopped interrupt
  1627. * @arg ETH_DMA_INT_T : Transmit interrupt
  1628. * @param NewValue: new value of the DMA interrupts.
  1629. * This parameter can be: ENABLE or DISABLE.
  1630. * @retval None
  1631. */
  1632. void ETH_DMAINTConfig(uint32_t ETH_DMA_INT, TypeState NewValue)
  1633. {
  1634. if (NewValue != DISABLE) {
  1635. /* Enable the ETH_DMA_INT control bit */
  1636. ETH_DMA->IER |= ETH_DMA_INT;
  1637. } else {
  1638. /* Disable the ETH_DMA_INT control bit */
  1639. ETH_DMA->IER &= (~(uint32_t)ETH_DMA_INT);
  1640. }
  1641. }
  1642. /**
  1643. * @brief Get the bit flag of ETHERNET DMA overflow.
  1644. * @param ETH_DMA_Overflow: the DMA overflow flag of ETHERNET DMA_MFBOCNT register.
  1645. * Select one of the follwing values :
  1646. * @arg ETH_DMA_OVERFLOW_RXFIFOCOUNTER : Overflow for FIFO Overflow Counter
  1647. * @arg ETH_DMA_OVERFLOW_MISSEDFRAMECOUNTER : Overflow for Missed Frame Counter
  1648. * @retval The current ETHERNET DMA overflow Flag selected bit status(SET or RESET).
  1649. */
  1650. TypeState ETH_GetDMAOverflowStatus(uint32_t ETH_DMA_Overflow)
  1651. {
  1652. if ((ETH_DMA->MFBOCNT & ETH_DMA_Overflow) != (uint32_t)RESET) {
  1653. /* DMA overflow Flag is set */
  1654. return SET;
  1655. } else {
  1656. /* DMA overflow Flag is reset */
  1657. return RESET;
  1658. }
  1659. }
  1660. /**
  1661. * @brief Get the DMA Rx value of overflow Missed Frame Counter.
  1662. * @param None
  1663. * @retval The ETH_DMA_MFBOCNT register MFA bit value.
  1664. */
  1665. uint32_t ETH_GetRxOverflowMissedFrameCounter(void)
  1666. {
  1667. return ((uint32_t)((ETH_DMA->MFBOCNT & ETH_DMA_MFBOCNT_MSFA) >> ETH_DMA_RX_OVERFLOW_MISSEDFRAMES_COUNTERSHIFT));
  1668. }
  1669. /**
  1670. * @brief Get the DMA value of Buffer unavailable Missed Frame Counter.
  1671. * @param None
  1672. * @retval The ETH_DMA_MFBOCNT register MFC bit value.
  1673. */
  1674. uint32_t ETH_GetBufferUnavailableMissedFrameCounter(void)
  1675. {
  1676. return ((uint32_t)(ETH_DMA->MFBOCNT) & ETH_DMA_MFBOCNT_MSFC);
  1677. }
  1678. /**
  1679. * @brief Get the DMA value of the current Tx desc start address.
  1680. * @param None
  1681. * @retval The ETH_DMA_CTDAR register value.
  1682. */
  1683. uint32_t ETH_GetCurrentTxDescStartAddress(void)
  1684. {
  1685. return ((uint32_t)(ETH_DMA->CTDAR));
  1686. }
  1687. /**
  1688. * @brief Get the DMA value of the current Rx desc start address.
  1689. * @param None
  1690. * @retval The ETH_DMA_CRDAR register value.
  1691. */
  1692. uint32_t ETH_GetCurrentRxDescStartAddress(void)
  1693. {
  1694. return ((uint32_t)(ETH_DMA->CRDAR));
  1695. }
  1696. /**
  1697. * @brief Get the DMA value of the current Tx buffer address.
  1698. * @param None
  1699. * @retval The ETH_DMA_CTBAR register value.
  1700. */
  1701. uint32_t ETH_GetCurrentTxBufferAddress(void)
  1702. {
  1703. return ((uint32_t)(ETH_DMA->CTBAR));
  1704. }
  1705. /**
  1706. * @brief Get the DMA value of the current Rx buffer address.
  1707. * @param None
  1708. * @retval The ETH_DMA_CRBAR register value.
  1709. */
  1710. uint32_t ETH_GetCurrentRxBufferAddress(void)
  1711. {
  1712. return ((uint32_t)(ETH_DMA->CRBAR));
  1713. }
  1714. /**
  1715. * @brief Poll the DMA Transmission enable by writing any value to the ETH_DMA_TPER register.
  1716. * This will make the DMA to resume transmission.
  1717. * @param None
  1718. * @retval None.
  1719. */
  1720. void ETH_ResumeDMATransmission(void)
  1721. {
  1722. ETH_DMA->TPER = 0;
  1723. }
  1724. /**
  1725. * @brief Poll the DMA Transmission enable by writing any value to the ETH_DMA_RPER register.
  1726. * This will make the DMA to resume reception.
  1727. * @param None
  1728. * @retval None.
  1729. */
  1730. void ETH_ResumeDMAReception(void)
  1731. {
  1732. ETH_DMA->RPER = 0;
  1733. }
  1734. /* WUM */
  1735. /**
  1736. * @brief Reset Wakeup frame filter register pointer.
  1737. * @param None
  1738. * @retval None
  1739. */
  1740. void ETH_WakeUpFrameFilterRegisterReset(void)
  1741. {
  1742. /* Set WUFFRPR bit for Reseting the ETH_MAC_RWFFR register pointer to 0 */
  1743. ETH_MAC->WUMR |= ETH_MAC_WUMR_WUFFRPR;
  1744. }
  1745. /**
  1746. * @brief Set the remote wakeup frame registers.
  1747. * @param pBuffer: Pointer to remote WakeUp Frame Filter Register buffer data (8 words).
  1748. * @retval None
  1749. */
  1750. void ETH_SetWakeUpFrameFilterRegister(uint32_t *pBuffer)
  1751. {
  1752. uint32_t num = 0;
  1753. /* Configuration ETH_MAC_RWFFR register */
  1754. for (num = 0; num < ETH_WAKEUP_REGISTER_LENGTH; num++) {
  1755. ETH_MAC->RWFFR = pBuffer[num];
  1756. }
  1757. }
  1758. /**
  1759. * @brief Enable or disable ETH_MAC_WUMR_GU control bit.
  1760. * @param NewValue: new value of the MAC Global Unicast Wake-Up.
  1761. * This parameter can be: ENABLE or DISABLE.
  1762. * @retval None
  1763. */
  1764. void ETH_GlobalUnicastWakeUp_Enable(TypeState NewValue)
  1765. {
  1766. if (NewValue != DISABLE) {
  1767. /* Enable the ETH_MAC_WUMR_GU control bit */
  1768. ETH_MAC->WUMR |= ETH_MAC_WUMR_GU;
  1769. } else {
  1770. /* Disable the ETH_MAC_WUMR_GU control bit */
  1771. ETH_MAC->WUMR &= ~ETH_MAC_WUMR_GU;
  1772. }
  1773. }
  1774. /**
  1775. * @brief Get the bit flag of the WUM.
  1776. * @param ETH_WUM_FLAG: the flag of ETH_MAC_WUMR register.
  1777. * Select one of the follwing values :
  1778. * @arg ETH_WUM_FLAG_WUFFRPR : Wake-Up Frame Filter Register Poniter Reset
  1779. * @arg ETH_WUM_FLAG_WUFR : Wake-Up Frame Received
  1780. * @arg ETH_WUM_FLAG_MPKR : Magic Packet Received
  1781. * @retval The current ETHERNET WUM Flag selected bit status(SET or RESET).
  1782. */
  1783. TypeState ETH_GetWUMBitState(uint32_t ETH_WUM_FLAG)
  1784. {
  1785. if ((ETH_MAC->WUMR & ETH_WUM_FLAG) != (uint32_t)RESET) {
  1786. /* WUM Flag is set */
  1787. return SET;
  1788. } else {
  1789. /* WUM Flag is reset */
  1790. return RESET;
  1791. }
  1792. }
  1793. /**
  1794. * @brief Enable or disable the ETH_MAC_WUMR_WFEN control bit.
  1795. * @param NewValue: new value of the ETH_MAC_WUMR_WFEN bit.
  1796. * This parameter can be: ENABLE or DISABLE.
  1797. * @retval None
  1798. */
  1799. void ETH_WakeUpFrameDetection_Enable(TypeState NewValue)
  1800. {
  1801. if (NewValue != DISABLE) {
  1802. /* Active the MAC Wake-Up Frame Detection */
  1803. ETH_MAC->WUMR |= ETH_MAC_WUMR_WFEN;
  1804. } else {
  1805. /* Deactive the MAC Wake-Up Frame Detection */
  1806. ETH_MAC->WUMR &= ~ETH_MAC_WUMR_WFEN;
  1807. }
  1808. }
  1809. /**
  1810. * @brief Enable or disable the ETH_MAC_WUMR_MPEN control bit.
  1811. * @param NewValue: new value of the ETH_MAC_WUMR_MPEN bit.
  1812. * This parameter can be: ENABLE or DISABLE.
  1813. * @retval None
  1814. */
  1815. void ETH_MagicPacketDetection_Enable(TypeState NewValue)
  1816. {
  1817. if (NewValue != DISABLE) {
  1818. /* Enable the ETH_MAC_WUMR_MPEN control bit */
  1819. ETH_MAC->WUMR |= ETH_MAC_WUMR_MPEN;
  1820. } else {
  1821. /* Disable the ETH_MAC_WUMR_MPEN control bit */
  1822. ETH_MAC->WUMR &= ~ETH_MAC_WUMR_MPEN;
  1823. }
  1824. }
  1825. /**
  1826. * @brief Enable or disable the ETH_MAC_WUMR_PWD control bit.
  1827. * @param NewValue: new value of the ETH_MAC_WUMR_PWD bit.
  1828. * This parameter can be: ENABLE or DISABLE.
  1829. * @retval None
  1830. */
  1831. void ETH_PowerDown_Enable(TypeState NewValue)
  1832. {
  1833. if (NewValue != DISABLE) {
  1834. /* Enable the ETH_MAC_WUMR_PWD control bit */
  1835. ETH_MAC->WUMR |= ETH_MAC_WUMR_PWD;
  1836. } else {
  1837. /* Disable the ETH_MAC_WUMR_PWD control bit */
  1838. ETH_MAC->WUMR &= ~ETH_MAC_WUMR_PWD;
  1839. }
  1840. }
  1841. /* MSC */
  1842. /**
  1843. * @brief Enable or disable the ETH_MSC_CTLR_MCFZ control bit.
  1844. * @param NewValue: new value of the MSC Counter Freeze.
  1845. * This parameter can be: ENABLE or DISABLE.
  1846. * @retval None
  1847. */
  1848. void ETH_MSCCounterFreeze_Enable(TypeState NewValue)
  1849. {
  1850. if (NewValue != DISABLE) {
  1851. /* Enable the ETH_MSC_CTLR_MCFZ control bit */
  1852. ETH_MSC->CTLR |= ETH_MSC_CTLR_MCFZ;
  1853. } else {
  1854. /* Disable the ETH_MSC_CTLR_MCFZ control bit */
  1855. ETH_MSC->CTLR &= ~ETH_MSC_CTLR_MCFZ;
  1856. }
  1857. }
  1858. /**
  1859. * @brief Enable or disable the ETH_MSC_CTLR_RTOR control bit.
  1860. * @param NewValue: new value of the MSC Reset On Read.
  1861. * This parameter can be: ENABLE or DISABLE.
  1862. * @retval None
  1863. */
  1864. void ETH_MSCResetOnRead_Enable(TypeState NewValue)
  1865. {
  1866. if (NewValue != DISABLE) {
  1867. /* Enable the ETH_MSC_CTLR_RTOR control bit */
  1868. ETH_MSC->CTLR |= ETH_MSC_CTLR_RTOR;
  1869. } else {
  1870. /* Disable the ETH_MSC_CTLR_RTOR control bit */
  1871. ETH_MSC->CTLR &= ~ETH_MSC_CTLR_RTOR;
  1872. }
  1873. }
  1874. /**
  1875. * @brief Enable or disable the ETH_MSC_CTLR_CTSR control bit.
  1876. * @param NewValue: new value of the ETH_MSC_CTLR_CTSR bit.
  1877. * This parameter can be: ENABLE or DISABLE.
  1878. * @retval None
  1879. */
  1880. void ETH_MSCCounterRollover_Enable(TypeState NewValue)
  1881. {
  1882. if (NewValue != DISABLE) {
  1883. /* Enable the ETH_MSC_CTLR_CTSR control bit */
  1884. ETH_MSC->CTLR &= ~ETH_MSC_CTLR_CTSR;
  1885. } else {
  1886. /* Disable the ETH_MSC_CTLR_CTSR control bit */
  1887. ETH_MSC->CTLR |= ETH_MSC_CTLR_CTSR;
  1888. }
  1889. }
  1890. /**
  1891. * @brief Resets the MSC Counters.
  1892. * @param None
  1893. * @retval None
  1894. */
  1895. void ETH_MSCCountersReset(void)
  1896. {
  1897. ETH_MSC->CTLR |= ETH_MSC_CTLR_CTR;
  1898. }
  1899. /**
  1900. * @brief Enable or disable the ETH_MSC_INT control bit.
  1901. * @param ETH_MSC_INT: the MSC interrupt sources flag of MSC_RIMR and MSC_TIMR.
  1902. * This parameter can be any combination of Tx interrupt or
  1903. * any combination of Rx interrupt (but not both)of the following values:
  1904. * @arg ETH_MSC_INT_TGF : When Tx good frame counter reaches half the maximum value
  1905. * @arg ETH_MSC_INT_TGFMSC: When Tx good multi col counter reaches half the maximum value
  1906. * @arg ETH_MSC_INT_TGFSC : When Tx good single col counter reaches half the maximum value
  1907. * @arg ETH_MSC_INT_RGUF : When Rx good unicast frames counter reaches half the maximum value
  1908. * @arg ETH_MSC_INT_RFAE : When Rx alignment error counter reaches half the maximum value
  1909. * @arg ETH_MSC_INT_RFCE : When Rx crc error counter reaches half the maximum value
  1910. * @param NewValue: new value of the MSC interrupt sources flag of MSC_RIMR and MSC_TIMR.
  1911. * This parameter can be: ENABLE or DISABLE.
  1912. * @retval None
  1913. */
  1914. void ETH_MSCINTConfig(uint32_t ETH_MSC_INT, TypeState NewValue)
  1915. {
  1916. if ((ETH_MSC_INT & (uint32_t)0x10000000) != (uint32_t)RESET) {
  1917. /* Get MSC interrupts souce bit position */
  1918. ETH_MSC_INT &= 0xEFFFFFFF;
  1919. /* MSC Rx interrupts souce bit selected */
  1920. if (NewValue != DISABLE) {
  1921. /* Enable the Rx ETH_MSC_INT control bit */
  1922. ETH_MSC->RIMR &= (~(uint32_t)ETH_MSC_INT);
  1923. } else {
  1924. /* Disable the Rx ETH_MSC_INT control bit */
  1925. ETH_MSC->RIMR |= ETH_MSC_INT;
  1926. }
  1927. } else {
  1928. /* MSC Tx interrupts souce bit selected */
  1929. if (NewValue != DISABLE) {
  1930. /* Enable the Tx ETH_MSC_INT control bit */
  1931. ETH_MSC->TIMR &= (~(uint32_t)ETH_MSC_INT);
  1932. } else {
  1933. /* Disable the Tx ETH_MSC_INT control bit */
  1934. ETH_MSC->TIMR |= ETH_MSC_INT;
  1935. }
  1936. }
  1937. }
  1938. /**
  1939. * @brief Get the bit flag of MSC INT.
  1940. * @param ETH_MSC_INT: the MSC interrupt flag of ETH_MSC_RISR and ETH_MSC_TISR.
  1941. * Select one of the follwing values :
  1942. * @arg ETH_MSC_INT_TGF: When Tx good frame counter reaches half the maximum value
  1943. * @arg ETH_MSC_INT_TGFMSC: When Tx good multi col counter reaches half the maximum value
  1944. * @arg ETH_MSC_INT_TGFSC: When Tx good single col counter reaches half the maximum value
  1945. * @arg ETH_MSC_INT_RGUF: When Rx good unicast frames counter reaches half the maximum value
  1946. * @arg ETH_MSC_INT_RFAE : When Rx alignment error counter reaches half the maximum value
  1947. * @arg ETH_MSC_INT_RFCE : When Rx crc error counter reaches half the maximum value
  1948. * @retval The current ETHERNET MSC IT selected bit status(SET or RESET).
  1949. */
  1950. TypeState ETH_GetMSCINTStatus(uint32_t ETH_MSC_INT)
  1951. {
  1952. if ((ETH_MSC_INT & (uint32_t)0x10000000) != (uint32_t)RESET) {
  1953. /* Check if the MSC interrupt flag of ETH_MSC_RISR selected is enabled and occured */
  1954. if ((((ETH_MSC->RISR & ETH_MSC_INT) != (uint32_t)RESET)) && ((ETH_MSC->RIMR & ETH_MSC_INT) != (uint32_t)RESET)) {
  1955. /* The MSC interrupt flag of ETH_MSC_RISR is set */
  1956. return SET;
  1957. } else {
  1958. /* The MSC interrupt flag of ETH_MSC_RISR is reset */
  1959. return RESET;
  1960. }
  1961. } else {
  1962. /* Check if the MSC interrupt flag of ETH_MSC_TISR selected is enabled and occured */
  1963. if ((((ETH_MSC->TISR & ETH_MSC_INT) != (uint32_t)RESET)) && ((ETH_MSC->RIMR & ETH_MSC_INT) != (uint32_t)RESET)) {
  1964. /* The MSC interrupt flag of ETH_MSC_TISR is set */
  1965. return SET;
  1966. } else {
  1967. /* The MSC interrupt flag of ETH_MSC_TISR is reset */
  1968. return RESET;
  1969. }
  1970. }
  1971. }
  1972. /**
  1973. * @brief Read value of the MSC module register.
  1974. * @param ETH_MSC_Register: the MSC module register selected.
  1975. * Select one of the follwing values :
  1976. * @arg ETH_MSC_CTLR : MSC CR register
  1977. * @arg ETH_MSC_RISR : MSC RIR register
  1978. * @arg ETH_MSC_TISR : MSC TIR register
  1979. * @arg ETH_MSC_RIMR : MSC RIMR register
  1980. * @arg ETH_MSC_TIMR : MSC TIMR register
  1981. * @arg ETH_MSC_SCCNT : MSC TGFSCCR register
  1982. * @arg ETH_MSC_MSCCNT : MSC TGFMSCCR register
  1983. * @arg ETH_MSC_TGFCNT : MSC TGFCR register
  1984. * @arg ETH_MSC_RFCECNT : MSC RFCECR register
  1985. * @arg ETH_MSC_RFAECNT : MSC RFAECR register
  1986. * @arg ETH_MSC_RGUFCNT : MSC RGUFCRregister
  1987. * @retval MSC module Register selected value.
  1988. */
  1989. uint32_t ETH_GetMSCRegister(uint32_t ETH_MSC_Register)
  1990. {
  1991. /* Return the value of MSC module Register selected */
  1992. return (*(__IO uint32_t *)(ETH_MAC_BASE + ETH_MSC_Register));
  1993. }
  1994. /* PTP */
  1995. /**
  1996. * @brief Updated the Time Stamp Addend register value to the PTP block(used only when the system time is configured
  1997. * for Fine update mode).
  1998. * @param None
  1999. * @retval None
  2000. */
  2001. void ETH_PTPTimeStampAddendUpdate(void)
  2002. {
  2003. ETH_PTP->TSCTLR |= ETH_PTP_TSCTLR_TMSARU;
  2004. }
  2005. /**
  2006. * @brief Set ETH_PTP_TSCTLR_TMSITEN bit.
  2007. * @param NewValue: new value of the MAC reception.
  2008. * This parameter can be: ENABLE or DISABLE.
  2009. * @retval None
  2010. */
  2011. void ETH_PTPTimeStampIntTrigger_Enable(TypeState NewValue)
  2012. {
  2013. if (NewValue != DISABLE) {
  2014. /* Enable ETH_PTP_TSCTLR_TMSITEN bit */
  2015. ETH_PTP->TSCTLR |= ETH_PTP_TSCTLR_TMSITEN;
  2016. } else {
  2017. /* Disable ETH_PTP_TSCTLR_TMSITEN bit */
  2018. ETH_PTP->TSCTLR &= ~ETH_PTP_TSCTLR_TMSITEN;
  2019. }
  2020. }
  2021. /**
  2022. * @brief Updated the Time Stamp Update register value to the PTP system time.
  2023. * @param NewValue: new value of the MAC reception.
  2024. * This parameter can be: ENABLE or DISABLE.
  2025. * @retval None
  2026. */
  2027. void ETH_PTPTimeStampUpdate_Enable(TypeState NewValue)
  2028. {
  2029. if (NewValue != DISABLE) {
  2030. /* Enable PTP time stamp update */
  2031. ETH_PTP->TSCTLR |= ETH_PTP_TSCTLR_TMSSTU;
  2032. } else {
  2033. /* Disable PTP time stamp update */
  2034. ETH_PTP->TSCTLR &= ~ETH_PTP_TSCTLR_TMSSTU;
  2035. }
  2036. }
  2037. /**
  2038. * @brief Initialize the PTP Time Stamp mode
  2039. * @param None
  2040. * @retval None
  2041. */
  2042. void ETH_PTPTimeStampInit(void)
  2043. {
  2044. ETH_PTP->TSCTLR |= ETH_PTP_TSCTLR_TMSSTI;
  2045. }
  2046. /**
  2047. * @brief Set the PTP Time Stamp Update mode
  2048. * @param UpdateMode: the PTP Update mode selected
  2049. * Select one of the follwing values :
  2050. * @arg ETH_PTP_FINEMODE : Fine Update method
  2051. * @arg ETH_PTP_COARSEMODE : Coarse Update method
  2052. * @retval None
  2053. */
  2054. void ETH_PTPUpdateModeConfig(uint32_t UpdateMode)
  2055. {
  2056. if (UpdateMode != ETH_PTP_COARSEMODE) {
  2057. /* Enable the PTP Fine Update mode */
  2058. ETH_PTP->TSCTLR |= ETH_PTP_TSCTLR_TMSFCU;
  2059. } else {
  2060. /* Disable the PTP Coarse Update mode */
  2061. ETH_PTP->TSCTLR &= (~(uint32_t)ETH_PTP_TSCTLR_TMSFCU);
  2062. }
  2063. }
  2064. /**
  2065. * @brief Enable or disable ETH_PTP_TSCTLR_TMSEN control bit.
  2066. * @param NewValue: new value of the ETH_PTP_TSCTLR_TMSEN control bit
  2067. * This parameter can be: ENABLE or DISABLE.
  2068. * @retval None
  2069. */
  2070. void ETH_PTPTimeStamp_Enable(TypeState NewValue)
  2071. {
  2072. if (NewValue != DISABLE) {
  2073. /* Enable the ETH_PTP_TSCTLR_TMSEN control bit */
  2074. ETH_PTP->TSCTLR |= ETH_PTP_TSCTLR_TMSEN;
  2075. } else {
  2076. /* Disable the ETH_PTP_TSCTLR_TMSEN control bit */
  2077. ETH_PTP->TSCTLR &= (~(uint32_t)ETH_PTP_TSCTLR_TMSEN);
  2078. }
  2079. }
  2080. /**
  2081. * @brief Get the bit flag of ETHERNET PTP.
  2082. * @param ETH_PTP_FLAG: the flag of PTP_TSCTLR register.
  2083. * Select one of the follwing values :
  2084. * @arg ETH_PTP_FLAG_TMSARU : Addend Register Update
  2085. * @arg ETH_PTP_FLAG_TMSITEN : Time Stamp Interrupt Trigger Enable
  2086. * @arg ETH_PTP_FLAG_TMSSTU : Time Stamp Update
  2087. * @arg ETH_PTP_FLAG_TMSSTI : Time Stamp Initialize
  2088. * @retval The current ETHERNET PTP Flag selected bit status(SET or RESET).
  2089. */
  2090. TypeState ETH_GetPTPBitState(uint32_t ETH_PTP_FLAG)
  2091. {
  2092. if ((ETH_PTP->TSCTLR & ETH_PTP_FLAG) != (uint32_t)RESET) {
  2093. /* ETH_PTP_FLAG is set */
  2094. return SET;
  2095. } else {
  2096. /* ETH_PTP_FLAG is reset */
  2097. return RESET;
  2098. }
  2099. }
  2100. /**
  2101. * @brief Set Each HCLK cycle the system time Sub-Second Increased value.
  2102. * @param SubSecond: the PTP_SSINCR Register value.
  2103. * @retval None
  2104. */
  2105. void ETH_SetPTPSubSecondIncrement(uint32_t SubSecond)
  2106. {
  2107. ETH_PTP->SSINCR = SubSecond;
  2108. }
  2109. /**
  2110. * @brief Set the system time update sign and values.
  2111. * @param Sign: the PTP Time update value sign(positive or negative).
  2112. * Select one of the follwing values :
  2113. * @arg ETH_PTP_POSITIVETIME : positive time value.
  2114. * @arg ETH_PTP_NEGATIVETIME : negative time value.
  2115. * @param Second: the PTP Time update value in second part.
  2116. * @param SubSecond: the PTP Time update value in sub-second part.
  2117. * This parameter is a 31 bit value, bit32 correspond to the sign.
  2118. * @retval None
  2119. */
  2120. void ETH_SetPTPUpdateTimeValue(uint32_t Sign, uint32_t SecondValue, uint32_t SubSecondValue)
  2121. {
  2122. /* Set the PTP Time Update High Register with second value*/
  2123. ETH_PTP->TMSHUR = SecondValue;
  2124. /* Set the PTP Time Update Low Register with subsecond value and sign */
  2125. ETH_PTP->TMSLUR = Sign | SubSecondValue;
  2126. }
  2127. /**
  2128. * @brief Set the system time Addend value(used only when the system time is configured
  2129. * for Fine update mode).
  2130. * @param add: the PTP PTP_TSACNT Register value.
  2131. * @retval None
  2132. */
  2133. void ETH_SetPTPTimeStampAddend(uint32_t add)
  2134. {
  2135. ETH_PTP->TSACNT = add;
  2136. }
  2137. /**
  2138. * @brief Set the Target system Time values.
  2139. * @param HighReg_Value: the PTP Expected Time High Register value.
  2140. * @param LowReg_Value: the PTP Expected Time Low Register value.
  2141. * @retval None
  2142. */
  2143. void ETH_SetPTPTargetTime(uint32_t HighReg_Value, uint32_t LowReg_Value)
  2144. {
  2145. /* Set the PTP Expected Time High Register */
  2146. ETH_PTP->ETHR = HighReg_Value;
  2147. /* Set the PTP Expected Time Low Register */
  2148. ETH_PTP->ETLR = LowReg_Value;
  2149. }
  2150. /**
  2151. * @brief Read the value of PTP module register .
  2152. * @param ETH_PTPRegister: the PTP module register selected.
  2153. * Select one of the follwing values :
  2154. * @arg ETH_PTP_TSCTLR : Sub-Second Increment Register
  2155. * @arg ETH_PTP_SSINCR : Sub-Second Increment Register
  2156. * @arg ETH_PTP_TMSHR : Time Stamp High Register
  2157. * @arg ETH_PTP_TMSLR : Time Stamp Low Register
  2158. * @arg ETH_PTP_TMSHUR : Time Stamp High Update Register
  2159. * @arg ETH_PTP_TMSLUR : Time Stamp Low Update Register
  2160. * @arg ETH_PTP_TSACNT : Time Stamp Addend Register
  2161. * @arg ETH_PTP_ETHR : Target Time High Register
  2162. * @arg ETH_PTP_ETLR : Target Time Low Register
  2163. * @retval ETHERNET PTP Register selected value.
  2164. */
  2165. uint32_t ETH_GetPTPRegister(uint32_t ETH_PTPRegister)
  2166. {
  2167. return (*(__IO uint32_t *)(ETH_MAC_BASE + ETH_PTPRegister));
  2168. }
  2169. /**
  2170. * @brief Initialize the DMA Tx descriptors's parameters in chain mode with PTP.
  2171. * @param DMATxDescTab: Pointer to the first Tx descriptor table
  2172. * @param DMAPTPTxDescTab: Pointer to the first PTP Tx descriptor table
  2173. * @param pTxBuff: Pointer to the first TxBuffer table
  2174. * @param TxBuffCnt: the used Tx descriptor sum in the table
  2175. * @retval None
  2176. */
  2177. void ETH_DMAPTPTxDescChainModeInit(ETH_DMADESCTypeDef *DMATxDescTab, ETH_DMADESCTypeDef *DMAPTPTxDescTab,
  2178. uint8_t *pTxBuff, uint32_t TxBuffCnt)
  2179. {
  2180. uint32_t num = 0;
  2181. ETH_DMADESCTypeDef *DMATxDesc;
  2182. DMACurrentTxDesc = DMATxDescTab;
  2183. DMACurrentPTPTxDesc = DMAPTPTxDescTab;
  2184. /* Configuration each DMATxDesc descriptor */
  2185. for (num = 0; num < TxBuffCnt; num++) {
  2186. /* Get the pointer to the next descriptor of the Tx Desc table */
  2187. DMATxDesc = DMATxDescTab + num;
  2188. /* Set TCH bit and enable PTP with desc status */
  2189. DMATxDesc->Status = ETH_DMATXDESC_TCHM | ETH_DMATXDESC_TTSEN;
  2190. /* Set Buffer1 address pointer to application buffer */
  2191. DMATxDesc->Buffer1Addr = (uint32_t)(&pTxBuff[num * ETH_MAX_FRAME_SIZE]);
  2192. if (num < (TxBuffCnt - 1)) {
  2193. /* Buffer2NextDescAddr equal to next descriptor address in the Tx Desc table */
  2194. DMATxDesc->Buffer2NextDescAddr = (uint32_t)(DMATxDescTab + num + 1);
  2195. } else {
  2196. /* When it is the last descriptor, Buffer2NextDescAddr equal to first descriptor address in the Tx Desc table */
  2197. DMATxDesc->Buffer2NextDescAddr = (uint32_t) DMATxDescTab;
  2198. }
  2199. /* Set DMAPTPTxDescTab equal to DMATxDescTab */
  2200. (&DMAPTPTxDescTab[num])->Buffer1Addr = DMATxDesc->Buffer1Addr;
  2201. (&DMAPTPTxDescTab[num])->Buffer2NextDescAddr = DMATxDesc->Buffer2NextDescAddr;
  2202. }
  2203. /* When it is the last PTPdescriptor, DMAPTPTxDesc status equal to the first descriptor address in the PTPTx Desc list address */
  2204. (&DMAPTPTxDescTab[num - 1])->Status = (uint32_t) DMAPTPTxDescTab;
  2205. ETH_DMA->TDTAR = (uint32_t) DMATxDescTab;
  2206. }
  2207. /**
  2208. * @brief Initialize the DMA Rx descriptors's parameters in chain mode with PTP.
  2209. * @param DMARxDescTab: Pointer to the first Rx descriptor table
  2210. * @param DMAPTPRxDescTab: Pointer to the first PTP Rx descriptor table
  2211. * @param pRxBuff: Pointer to the first RxBuffer table
  2212. * @param RxBuffCnt: the used Rx descriptor sum in the table
  2213. * @retval None
  2214. */
  2215. void ETH_DMAPTPRxDescChainModeInit(ETH_DMADESCTypeDef *DMARxDescTab, ETH_DMADESCTypeDef *DMAPTPRxDescTab,
  2216. uint8_t *pRxBuff, uint32_t RxBuffCnt)
  2217. {
  2218. uint32_t num = 0;
  2219. ETH_DMADESCTypeDef *DMARxDesc;
  2220. DMACurrentRxDesc = DMARxDescTab;
  2221. DMACurrentPTPRxDesc = DMAPTPRxDescTab;
  2222. /* Configuration each DMARxDesc descriptor */
  2223. for (num = 0; num < RxBuffCnt; num++) {
  2224. /* Get the pointer to the next descriptor of the Rx Desc table */
  2225. DMARxDesc = DMARxDescTab + num;
  2226. DMARxDesc->Status = ETH_DMARXDESC_BUSY;
  2227. /* Set TCH bit and buffer1 size */
  2228. DMARxDesc->ControlBufferSize = ETH_DMARXDESC_RCHM | (uint32_t)ETH_MAX_FRAME_SIZE;
  2229. /* Set Buffer1 address pointer to application buffer */
  2230. DMARxDesc->Buffer1Addr = (uint32_t)(&pRxBuff[num * ETH_MAX_FRAME_SIZE]);
  2231. if (num < (RxBuffCnt - 1)) {
  2232. /* Buffer2NextDescAddr equal to next descriptor address in the Rx Desc table */
  2233. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab + num + 1);
  2234. } else {
  2235. /* When it is the last descriptor, Buffer2NextDescAddr equal to first descriptor address in the Rx Desc table */
  2236. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab);
  2237. }
  2238. /* Set DMAPTPRxDescTab equal to DMARxDescTab */
  2239. (&DMAPTPRxDescTab[num])->Buffer1Addr = DMARxDesc->Buffer1Addr;
  2240. (&DMAPTPRxDescTab[num])->Buffer2NextDescAddr = DMARxDesc->Buffer2NextDescAddr;
  2241. }
  2242. /* When it is the last PTPdescriptor, DMAPTPRxDesc status equal to the first descriptor address in the PTPRx Desc table address */
  2243. (&DMAPTPRxDescTab[num - 1])->Status = (uint32_t) DMAPTPRxDescTab;
  2244. /* Update Receive Desciptor Table Address Register */
  2245. ETH_DMA->RDTAR = (uint32_t) DMARxDescTab;
  2246. }
  2247. /**
  2248. * @brief Send data with Time Stamp values of application buffer as a transmit packet.
  2249. * @param pbuf: pointer to the applicationbuffer.
  2250. * @param size: the application buffer size.
  2251. * @param PTPTxTab: Pointer to the first PTP Tx table to store Time stamp values.
  2252. * @retval The transmission with PTP result(ERROR or SUCCESS)
  2253. */
  2254. uint32_t ETH_HandlePTPTxPkt(uint8_t *pbuf, uint16_t size, uint32_t *PTPTxTab)
  2255. {
  2256. uint32_t offset = 0, timeout = 0;
  2257. if ((DMACurrentTxDesc->Status & ETH_DMATXDESC_BUSY) != (uint32_t)RESET) {
  2258. /* Return ERROR: the descriptor is busy due to own by the DMA */
  2259. return ERROR;
  2260. }
  2261. for (offset = 0; offset < size; offset++) {
  2262. (*(__IO uint8_t *)((DMACurrentPTPTxDesc->Buffer1Addr) + offset)) = (*(pbuf + offset));
  2263. }
  2264. /* Setting the Frame Length */
  2265. DMACurrentTxDesc->ControlBufferSize = (size & (uint32_t)0x1FFF);
  2266. /* Setting the segment of frame (ETH_DMATXDESC_LSG and ETH_DMATXDESC_FSG are SET that frame is transmitted in one descriptor) */
  2267. DMACurrentTxDesc->Status |= ETH_DMATXDESC_LSG | ETH_DMATXDESC_FSG;
  2268. /* Enable the DMA transmission */
  2269. DMACurrentTxDesc->Status |= ETH_DMATXDESC_BUSY;
  2270. /* Check Tx Buffer unavailable flag status */
  2271. if ((ETH_DMA->STR & ETH_DMA_STR_TBU) != (uint32_t)RESET) {
  2272. /* Clear TBU ETHERNET DMA flag */
  2273. ETH_DMA->STR = ETH_DMA_STR_TBU;
  2274. /* Resume DMA transmission by writing to the TPER register*/
  2275. ETH_DMA->TPER = 0;
  2276. }
  2277. /* Wait for ETH_DMATXDESC_TTMSS flag to be set unless timeout */
  2278. do {
  2279. timeout++;
  2280. } while (!(DMACurrentTxDesc->Status & ETH_DMATXDESC_TTMSS) && (timeout < 0xFFFF));
  2281. /* Return ERROR due to timeout */
  2282. if (timeout == PHY_READ_TO) {
  2283. return ERROR;
  2284. }
  2285. /* Clear the DMACurrentTxDesc status register TTSS flag */
  2286. DMACurrentTxDesc->Status &= ~ETH_DMATXDESC_TTMSS;
  2287. *PTPTxTab++ = DMACurrentTxDesc->Buffer1Addr;
  2288. *PTPTxTab = DMACurrentTxDesc->Buffer2NextDescAddr;
  2289. /* Update the ETHERNET DMA current Tx descriptor pointer to the next Tx decriptor in DMA Tx decriptor talbe*/
  2290. /* Chained Mode */
  2291. if ((DMACurrentTxDesc->Status & ETH_DMATXDESC_TCHM) != (uint32_t)RESET) {
  2292. DMACurrentTxDesc = (ETH_DMADESCTypeDef *)(DMACurrentPTPTxDesc->Buffer2NextDescAddr);
  2293. if (DMACurrentPTPTxDesc->Status != 0) {
  2294. DMACurrentPTPTxDesc = (ETH_DMADESCTypeDef *)(DMACurrentPTPTxDesc->Status);
  2295. } else {
  2296. DMACurrentPTPTxDesc++;
  2297. }
  2298. }
  2299. /* Ring Mode */
  2300. else {
  2301. if ((DMACurrentTxDesc->Status & ETH_DMATXDESC_TERM) != (uint32_t)RESET) {
  2302. DMACurrentTxDesc = (ETH_DMADESCTypeDef *)(ETH_DMA->TDTAR);
  2303. DMACurrentPTPTxDesc = (ETH_DMADESCTypeDef *)(ETH_DMA->TDTAR);
  2304. } else {
  2305. DMACurrentTxDesc = (ETH_DMADESCTypeDef *)((uint32_t)DMACurrentTxDesc + ETH_DMATXDESC_SIZE + ((ETH_DMA->BCR & ETH_DMA_BCR_DPSL) >> 2));
  2306. DMACurrentPTPTxDesc = (ETH_DMADESCTypeDef *)((uint32_t)DMACurrentPTPTxDesc + ETH_DMATXDESC_SIZE + ((ETH_DMA->BCR & ETH_DMA_BCR_DPSL) >> 2));
  2307. }
  2308. }
  2309. return SUCCESS;
  2310. }
  2311. /**
  2312. * @brief Receive a packet data with Time Stamp values to application buffer.
  2313. * @param pbuf: Pointer on the application buffer.
  2314. * @param PTPRxTab: Pointer to the first PTP Rx table to store Time stamp values.
  2315. * @retval The Receive frame length with PTP(If size is equal to ERROR, the receiving unsuccessful)
  2316. */
  2317. uint32_t ETH_HandlePTPRxPkt(uint8_t *pbuf, uint32_t *PTPRxTab)
  2318. {
  2319. uint32_t offset = 0, size = 0;
  2320. if ((DMACurrentRxDesc->Status & ETH_DMARXDESC_BUSY) != (uint32_t)RESET) {
  2321. /* Return ERROR: the descriptor is busy due to own by the DMA */
  2322. return ERROR;
  2323. }
  2324. if (((DMACurrentRxDesc->Status & ETH_DMARXDESC_ERRS) == (uint32_t)RESET) &&
  2325. ((DMACurrentRxDesc->Status & ETH_DMARXDESC_LDES) != (uint32_t)RESET) &&
  2326. ((DMACurrentRxDesc->Status & ETH_DMARXDESC_FDES) != (uint32_t)RESET)) {
  2327. /* Get the Frame Length exclusive CRC */
  2328. size = ((DMACurrentRxDesc->Status & ETH_DMARXDESC_FRML) >> ETH_DMARXDESC_FRAME_LENGTHSHIFT) - 4;
  2329. for (offset = 0; offset < size; offset++) {
  2330. (*(pbuf + offset)) = (*(__IO uint8_t *)((DMACurrentPTPRxDesc->Buffer1Addr) + offset));
  2331. }
  2332. } else {
  2333. /* Return ERROR */
  2334. size = ERROR;
  2335. }
  2336. /* Check Rx Buffer unavailable flag status */
  2337. if ((ETH_DMA->STR & ETH_DMA_STR_RBU) != (uint32_t)RESET) {
  2338. /* Clear RBU ETHERNET DMA flag */
  2339. ETH_DMA->STR = ETH_DMA_STR_RBU;
  2340. /* Resume DMA reception by writing to the RPER register*/
  2341. ETH_DMA->RPER = 0;
  2342. }
  2343. *PTPRxTab++ = DMACurrentRxDesc->Buffer1Addr;
  2344. *PTPRxTab = DMACurrentRxDesc->Buffer2NextDescAddr;
  2345. /* Enable reception */
  2346. DMACurrentRxDesc->Status |= ETH_DMARXDESC_BUSY;
  2347. /* Update the ETHERNET DMA current Rx descriptor pointer to the next Rx decriptor in DMA Rx decriptor talbe*/
  2348. /* Chained Mode */
  2349. if ((DMACurrentRxDesc->ControlBufferSize & ETH_DMARXDESC_RCHM) != (uint32_t)RESET) {
  2350. DMACurrentRxDesc = (ETH_DMADESCTypeDef *)(DMACurrentPTPRxDesc->Buffer2NextDescAddr);
  2351. if (DMACurrentPTPRxDesc->Status != 0) {
  2352. DMACurrentPTPRxDesc = (ETH_DMADESCTypeDef *)(DMACurrentPTPRxDesc->Status);
  2353. } else {
  2354. DMACurrentPTPRxDesc++;
  2355. }
  2356. }
  2357. /* Ring Mode */
  2358. else {
  2359. if ((DMACurrentRxDesc->ControlBufferSize & ETH_DMARXDESC_RERR) != (uint32_t)RESET) {
  2360. DMACurrentRxDesc = (ETH_DMADESCTypeDef *)(ETH_DMA->RDTAR);
  2361. } else {
  2362. DMACurrentRxDesc = (ETH_DMADESCTypeDef *)((uint32_t)DMACurrentRxDesc + ETH_DMARXDESC_SIZE + ((ETH_DMA->BCR & ETH_DMA_BCR_DPSL) >> 2));
  2363. }
  2364. }
  2365. /* Return Frame size or ERROR */
  2366. return (size);
  2367. }
  2368. #ifndef USE_Delay
  2369. /**
  2370. * @brief Insert a delay time.
  2371. * @param nCount: specifies the delay time length.
  2372. * @retval None
  2373. */
  2374. static void ETH_Delay(__IO uint32_t nCount)
  2375. {
  2376. __IO uint32_t time = 0;
  2377. for (time = nCount; time != 0; time--) {
  2378. }
  2379. }
  2380. #endif /* USE_Delay */
  2381. #endif /* GD32F10X_CL */
  2382. /**
  2383. * @}
  2384. */
  2385. /**
  2386. * @}
  2387. */
  2388. /**
  2389. * @}
  2390. */