n32g45x_eth.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_eth.c
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x_eth.h"
  35. #include "n32g45x_gpio.h"
  36. /** @addtogroup N32G45X_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup ETH
  40. * @brief ETH driver modules
  41. * @{
  42. */
  43. /**
  44. * @brief Initialize GPIO pins for MII/RMII interface.
  45. *
  46. * @param ETH_Interface specifies the interface, can be the following values:
  47. * @arg ETH_INTERFACE_RMII Reduced media-independent interface
  48. * @arg ETH_INTERFACE_MII Media-independent interface
  49. * @param remap remap mode, can be 0~3
  50. */
  51. void ETH_ConfigGpio(uint8_t ETH_Interface, uint8_t remap)
  52. {
  53. GPIO_InitType GPIO_InitStructure;
  54. uint32_t ETH_PA_O;
  55. uint32_t ETH_PA_I;
  56. uint32_t ETH_PB_O;
  57. uint32_t ETH_PB_I;
  58. uint32_t ETH_PC_O;
  59. uint32_t ETH_PC_I;
  60. uint32_t ETH_PD_O;
  61. uint32_t ETH_PD_I;
  62. if (ETH_Interface == ETH_INTERFACE_MII)
  63. {
  64. switch (remap)
  65. {
  66. case 0:
  67. ETH_PA_O = GPIO_PIN_2;
  68. ETH_PA_I = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3 | GPIO_PIN_7;
  69. ETH_PB_O = GPIO_PIN_8 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
  70. ETH_PB_I = GPIO_PIN_10 | GPIO_PIN_0 | GPIO_PIN_1;
  71. ETH_PC_O = GPIO_PIN_1 | GPIO_PIN_2;
  72. ETH_PC_I = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5;
  73. ETH_PD_O = 0;
  74. ETH_PD_I = 0;
  75. break;
  76. case 1:
  77. ETH_PA_O = GPIO_PIN_2;
  78. ETH_PA_I = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3;
  79. ETH_PB_O = GPIO_PIN_8 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
  80. ETH_PB_I = GPIO_PIN_10;
  81. ETH_PC_O = GPIO_PIN_1 | GPIO_PIN_2;
  82. ETH_PC_I = GPIO_PIN_3;
  83. ETH_PD_O = 0;
  84. ETH_PD_I = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
  85. GPIO_ConfigPinRemap(GPIO_RMP1_ETH, ENABLE);
  86. break;
  87. case 2:
  88. ETH_PA_O = GPIO_PIN_2;
  89. ETH_PA_I = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3 | GPIO_PIN_7;
  90. ETH_PB_O = GPIO_PIN_7 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
  91. ETH_PB_I = GPIO_PIN_10 | GPIO_PIN_0 | GPIO_PIN_1;
  92. ETH_PC_O = GPIO_PIN_1 | GPIO_PIN_2;
  93. ETH_PC_I = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5;
  94. ETH_PD_O = 0;
  95. ETH_PD_I = 0;
  96. GPIO_ConfigPinRemap(GPIO_RMP2_ETH, ENABLE);
  97. break;
  98. case 3:
  99. ETH_PA_O = GPIO_PIN_2;
  100. ETH_PA_I = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3;
  101. ETH_PB_O = GPIO_PIN_7 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
  102. ETH_PB_I = GPIO_PIN_10 | GPIO_PIN_0 | GPIO_PIN_1;
  103. ETH_PC_O = GPIO_PIN_1 | GPIO_PIN_2;
  104. ETH_PC_I = GPIO_PIN_3;
  105. ETH_PD_O = 0;
  106. ETH_PD_I = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10;
  107. GPIO_ConfigPinRemap(GPIO_RMP3_ETH, ENABLE);
  108. break;
  109. default:
  110. while (1)
  111. ;
  112. }
  113. }
  114. else /* RMII */
  115. {
  116. switch (remap)
  117. {
  118. case 0:
  119. case 2:
  120. ETH_PA_O = GPIO_PIN_2;
  121. ETH_PA_I = GPIO_PIN_1 | GPIO_PIN_7;
  122. ETH_PB_O = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
  123. ETH_PB_I = 0;
  124. ETH_PC_O = GPIO_PIN_1;
  125. ETH_PC_I = GPIO_PIN_4 | GPIO_PIN_5;
  126. ETH_PD_O = 0;
  127. ETH_PD_I = 0;
  128. break;
  129. case 1:
  130. case 3:
  131. ETH_PA_O = GPIO_PIN_2;
  132. ETH_PA_I = GPIO_PIN_1;
  133. ETH_PB_O = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
  134. ETH_PB_I = 0;
  135. ETH_PC_O = GPIO_PIN_1;
  136. ETH_PC_I = 0;
  137. ETH_PD_O = 0;
  138. ETH_PD_I = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10;
  139. GPIO_ConfigPinRemap(GPIO_RMP1_ETH, ENABLE);
  140. break;
  141. default:
  142. while (1)
  143. ;
  144. }
  145. }
  146. if (ETH_PA_O)
  147. {
  148. /* Configure Ethernet PA and PA8 (MCO) as alternate function push-pull */
  149. GPIO_InitStructure.Pin = ETH_PA_O;
  150. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  151. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  152. GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
  153. }
  154. if (ETH_PB_O)
  155. {
  156. /* Configure Ethernet PB and PB5 (PPS) as alternate function push-pull */
  157. GPIO_InitStructure.Pin = ETH_PB_O;
  158. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  159. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  160. GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
  161. }
  162. if (ETH_PC_O)
  163. {
  164. /* Configure Ethernet PC as alternate function push-pull */
  165. GPIO_InitStructure.Pin = ETH_PC_O;
  166. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  167. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  168. GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);
  169. }
  170. if (ETH_PD_O)
  171. {
  172. /* Configure Ethernet PD as alternate function push-pull */
  173. GPIO_InitStructure.Pin = ETH_PD_O;
  174. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  175. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  176. GPIO_InitPeripheral(GPIOD, &GPIO_InitStructure);
  177. }
  178. if (ETH_PA_I)
  179. {
  180. /* Configure Ethernet PA as input */
  181. GPIO_InitStructure.Pin = ETH_PA_I;
  182. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  183. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  184. GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
  185. }
  186. if (ETH_PB_I)
  187. {
  188. /* Configure Ethernet PB as input */
  189. GPIO_InitStructure.Pin = ETH_PB_I;
  190. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  191. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  192. GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
  193. }
  194. if (ETH_PC_I)
  195. {
  196. /* Configure Ethernet PC as input */
  197. GPIO_InitStructure.Pin = ETH_PC_I;
  198. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  199. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  200. GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);
  201. }
  202. if (ETH_PD_I)
  203. {
  204. /* Configure Ethernet PD as input */
  205. GPIO_InitStructure.Pin = ETH_PD_I;
  206. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  207. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  208. GPIO_InitPeripheral(GPIOD, &GPIO_InitStructure);
  209. }
  210. }
  211. /** @addtogroup ETH_Private_TypesDefinitions
  212. * @{
  213. */
  214. /**
  215. * @}
  216. */
  217. /** @addtogroup ETH_Private_Defines
  218. * @{
  219. */
  220. /* Global pointers on Tx and Rx descriptor used to track transmit and receive descriptors */
  221. __IO ETH_DMADescType* DMATxDescToSet;
  222. __IO ETH_DMADescType* DMARxDescToGet;
  223. __IO ETH_DMADescType* DMAPTPTxDescToSet;
  224. __IO ETH_DMADescType* DMAPTPRxDescToGet;
  225. /* ETHERNET MAC address offsets */
  226. #define ETH_MAC_ADDR_HBASE (ETH_MAC_BASE + 0x40) /* ETHERNET MAC address high offset */
  227. #define ETH_MAC_ADDR_LBASE (ETH_MAC_BASE + 0x44) /* ETHERNET MAC address low offset */
  228. /* ETHERNET MACMIIADDR register Mask */
  229. #define MACMIIAR_CR_MASK ((uint32_t)0xFFFFFFE3)
  230. /* ETHERNET MACCFG register Mask */
  231. #define MACCR_CLR_MASK ((uint32_t)0xFF20810F)
  232. /* ETHERNET MACFLWCTRL register Mask */
  233. #define MACFCR_CLR_MASK ((uint32_t)0x0000FF41)
  234. /* ETHERNET DMAOPMOD register Mask */
  235. #define DMAOMR_CLR_MASK ((uint32_t)0xF8DE3F23)
  236. /* ETHERNET Remote Wake-up frame register length */
  237. #define ETH_WAKEUP_REG_LEN 8
  238. /* ETHERNET Missed frames counter Shift */
  239. #define ETH_DMA_RX_OVERFLOW_MISSEDFRAMES_COUNTER_SHIFT 17
  240. /* ETHERNET DMA Tx descriptors Collision Count Shift */
  241. #define ETH_DMA_TX_DESC_COLLISION_COUNTER_SHIFT 3
  242. /* ETHERNET DMA Tx descriptors Buffer2 Size Shift */
  243. #define ETH_DMA_TX_DESC_BUF2_SIZE_SHIFT 11
  244. /* ETHERNET DMA Rx descriptors Frame Length Shift */
  245. #define ETH_DMA_RX_DESC_FRAME_LEN_SHIFT 16
  246. /* ETHERNET DMA Rx descriptors Buffer2 Size Shift */
  247. #define ETH_DMA_RX_DESC_BUF2_SIZE_SHIFT 11
  248. /**
  249. * @}
  250. */
  251. /** @addtogroup ETH_Private_Macros
  252. * @{
  253. */
  254. /**
  255. * @}
  256. */
  257. /** @addtogroup ETH_Private_Variables
  258. * @{
  259. */
  260. /**
  261. * @}
  262. */
  263. /** @addtogroup ETH_Private_FunctionPrototypes
  264. * @{
  265. */
  266. /**
  267. * @}
  268. */
  269. /** @addtogroup ETH_Private_Functions
  270. * @{
  271. */
  272. /**
  273. * @brief Deinitializes the ETHERNET peripheral registers to their default reset values.
  274. */
  275. void ETH_DeInit(void)
  276. {
  277. RCC_EnableAHBPeriphReset(RCC_AHB_PERIPH_ETHMAC, ENABLE);
  278. RCC_EnableAHBPeriphReset(RCC_AHB_PERIPH_ETHMAC, DISABLE);
  279. }
  280. /**
  281. * @brief Initializes the ETHERNET peripheral according to the specified
  282. * parameters in the ETH_InitStruct .
  283. * @param ETH_InitStruct pointer to a ETH_InitType structure that contains
  284. * the configuration information for the specified ETHERNET peripheral.
  285. * @param callable a function pointer of @ref ETH_InitPHY
  286. * @return ETH_ERROR: Ethernet initialization failed
  287. * ETH_SUCCESS: Ethernet successfully initialized
  288. */
  289. uint32_t ETH_Init(ETH_InitType* ETH_InitStruct, ETH_InitPHY callable)
  290. {
  291. uint32_t tmpregister = 0;
  292. RCC_ClocksType rcc_clocks;
  293. uint32_t hclk = 60000000;
  294. /* Check the parameters */
  295. /* MAC --------------------------*/
  296. assert_param(IS_ETH_AUTONEG(ETH_InitStruct->AutoNegotiation));
  297. assert_param(IS_ETH_WATCHDOG(ETH_InitStruct->Watchdog));
  298. assert_param(IS_ETH_JABBER(ETH_InitStruct->Jabber));
  299. assert_param(IS_ETH_INTER_FRAME_GAP(ETH_InitStruct->InterFrameGap));
  300. assert_param(IS_ETH_CARRIER_SENSE(ETH_InitStruct->CarrierSense));
  301. assert_param(IS_ETH_SPEED_MODE(ETH_InitStruct->SpeedMode));
  302. assert_param(IS_ETH_RX_OWN(ETH_InitStruct->RxOwn));
  303. assert_param(IS_ETH_LOOPBACK_MODE(ETH_InitStruct->LoopbackMode));
  304. assert_param(IS_ETH_DUPLEX_MODE(ETH_InitStruct->DuplexMode));
  305. assert_param(IS_ETH_CHECKSUM_OFFLOAD(ETH_InitStruct->ChecksumOffload));
  306. assert_param(IS_ETH_RETRY_TRANSMISSION(ETH_InitStruct->RetryTransmission));
  307. assert_param(IS_ETH_AUTO_PAD_CRC_STRIP(ETH_InitStruct->AutomaticPadCRCStrip));
  308. assert_param(IS_ETH_BACKOFF_LIMIT(ETH_InitStruct->BackoffLimit));
  309. assert_param(IS_ETH_DEFERRAL_CHECK(ETH_InitStruct->DeferralCheck));
  310. assert_param(IS_ETH_RX_ALL(ETH_InitStruct->RxAll));
  311. assert_param(IS_ETH_SRC_ADDR_FILTER(ETH_InitStruct->SrcAddrFilter));
  312. assert_param(IS_ETH_PASS_CTRL_FRAMES(ETH_InitStruct->PassCtrlFrames));
  313. assert_param(IS_ETH_BROADCAST_FRAMES_RECEPTION(ETH_InitStruct->BroadcastFramesReception));
  314. assert_param(IS_ETH_DEST_ADDR_FILTER(ETH_InitStruct->DestAddrFilter));
  315. assert_param(IS_ETH_PROMISCUOUS_MODE(ETH_InitStruct->PromiscuousMode));
  316. assert_param(IS_ETH_MULTICAST_FRAMES_FILTER(ETH_InitStruct->MulticastFramesFilter));
  317. assert_param(IS_ETH_UNICAST_FRAMES_FILTER(ETH_InitStruct->UnicastFramesFilter));
  318. assert_param(IS_ETH_PAUSE_TIME(ETH_InitStruct->PauseTime));
  319. assert_param(IS_ETH_ZERO_QUANTA_PAUSE(ETH_InitStruct->ZeroQuantaPause));
  320. assert_param(IS_ETH_PAUSE_LOW_THRESHOLD(ETH_InitStruct->PauseLowThreshold));
  321. assert_param(IS_ETH_UNICAST_PAUSE_FRAME_DETECT(ETH_InitStruct->UnicastPauseFrameDetect));
  322. assert_param(IS_ETH_RX_FLOW_CTRL(ETH_InitStruct->RxFlowCtrl));
  323. assert_param(IS_ETH_TX_FLOW_CTRL(ETH_InitStruct->TxFlowCtrl));
  324. assert_param(IS_ETH_VLAN_TAG_COMPARISON(ETH_InitStruct->VLANTagComparison));
  325. assert_param(IS_ETH_VLAN_TAG_IDENTIFIER(ETH_InitStruct->VLANTagIdentifier));
  326. /* DMA --------------------------*/
  327. assert_param(IS_ETH_DROP_TCPIP_CHECKSUM_FRAME(ETH_InitStruct->DropTCPIPChecksumErrorFrame));
  328. assert_param(IS_ETH_RX_STORE_FORWARD(ETH_InitStruct->RxStoreForward));
  329. assert_param(IS_ETH_FLUSH_RX_FRAME(ETH_InitStruct->FlushRxFrame));
  330. assert_param(IS_ETH_TX_STORE_FORWARD(ETH_InitStruct->TxStoreForward));
  331. assert_param(IS_ETH_TX_THRESHOLD_CTRL(ETH_InitStruct->TxThresholdCtrl));
  332. assert_param(IS_ETH_FORWARD_ERROR_FRAMES(ETH_InitStruct->ForwardErrorFrames));
  333. assert_param(IS_ETH_FORWARD_UNDERSIZED_GOOD_FRAMES(ETH_InitStruct->ForwardUndersizedGoodFrames));
  334. assert_param(IS_ETH_RX_THRESHOLD_CTRL(ETH_InitStruct->RxThresholdCtrl));
  335. assert_param(IS_ETH_SECOND_FRAME_OPERATE(ETH_InitStruct->SecondFrameOperate));
  336. assert_param(IS_ETH_ADDR_ALIGNED_BEATS(ETH_InitStruct->AddrAlignedBeats));
  337. assert_param(IS_ETH_FIXED_BURST(ETH_InitStruct->FixedBurst));
  338. assert_param(IS_ETH_RX_DMA_BURST_LEN(ETH_InitStruct->RxDMABurstLen));
  339. assert_param(IS_ETH_TX_DMA_BURST_LEN(ETH_InitStruct->TxDMABurstLen));
  340. assert_param(IS_ETH_DMA_DESC_SKIP_LEN(ETH_InitStruct->DescSkipLen));
  341. assert_param(IS_ETH_DMA_ARBITRATION_ROUND_ROBIN_RXTX(ETH_InitStruct->DMAArbitration));
  342. /*-------------------------------- MAC Config ------------------------------*/
  343. /*---------------------- ETHERNET MACMIIADDR Configuration -------------------*/
  344. /* Get the ETHERNET MACMIIADDR value */
  345. tmpregister = ETH->MACMIIADDR;
  346. /* Clear CTRLSTS Clock Range CTRL[2:0] bits */
  347. tmpregister &= MACMIIAR_CR_MASK;
  348. /* Get hclk frequency value */
  349. RCC_GetClocksFreqValue(&rcc_clocks);
  350. hclk = rcc_clocks.HclkFreq;
  351. /* Set CTRL bits depending on hclk value */
  352. if (/*(hclk >= 20000000) && */ (hclk < 35000000))
  353. {
  354. /* CTRLSTS Clock Range between 20-35 MHz */
  355. tmpregister |= (uint32_t)ETH_MACMIIADDR_CR_DIV16;
  356. }
  357. else if ((hclk >= 35000000) && (hclk < 60000000))
  358. {
  359. /* CTRLSTS Clock Range between 35-60 MHz */
  360. tmpregister |= (uint32_t)ETH_MACMIIADDR_CR_DIV26;
  361. }
  362. else if ((hclk >= 60000000) && (hclk <= 72000000))
  363. {
  364. /* CTRLSTS Clock Range between 60-72 MHz */
  365. tmpregister |= (uint32_t)ETH_MACMIIADDR_CR_DIV42;
  366. }
  367. /* Write to ETHERNET MAC MIIAR: Configure the ETHERNET CTRLSTS Clock Range */
  368. ETH->MACMIIADDR = (uint32_t)tmpregister;
  369. /*-------------------- PHY initialization and configuration ----------------*/
  370. if (ETH_ERROR == callable(ETH_InitStruct))
  371. {
  372. return ETH_ERROR;
  373. }
  374. /*------------------------ ETHERNET MACCFG Configuration --------------------*/
  375. /* Get the ETHERNET MACCFG value */
  376. tmpregister = ETH->MACCFG;
  377. /* Clear WD, PCE, PS, TE and RE bits */
  378. tmpregister &= MACCR_CLR_MASK;
  379. /* Set the WD bit according to Watchdog value */
  380. /* Set the JD: bit according to Jabber value */
  381. /* Set the IFG bit according to InterFrameGap value */
  382. /* Set the DCRS bit according to CarrierSense value */
  383. /* Set the FES bit according to SpeedMode value */
  384. /* Set the DO bit according to RxOwn value */
  385. /* Set the LM bit according to LoopbackMode value */
  386. /* Set the DM bit according to DuplexMode value */
  387. /* Set the IPC bit according to ChecksumOffload value */
  388. /* Set the DAT bit according to RetryTransmission value */
  389. /* Set the ACS bit according to AutomaticPadCRCStrip value */
  390. /* Set the BL bit according to BackoffLimit value */
  391. /* Set the DC bit according to DeferralCheck value */
  392. tmpregister |= (uint32_t)(
  393. ETH_InitStruct->Watchdog | ETH_InitStruct->Jabber | ETH_InitStruct->InterFrameGap | ETH_InitStruct->CarrierSense
  394. | ETH_InitStruct->SpeedMode | ETH_InitStruct->RxOwn | ETH_InitStruct->LoopbackMode | ETH_InitStruct->DuplexMode
  395. | ETH_InitStruct->ChecksumOffload | ETH_InitStruct->RetryTransmission | ETH_InitStruct->AutomaticPadCRCStrip
  396. | ETH_InitStruct->BackoffLimit | ETH_InitStruct->DeferralCheck);
  397. /* Write to ETHERNET MACCFG */
  398. ETH->MACCFG = (uint32_t)tmpregister;
  399. /*----------------------- ETHERNET MACFFLT Configuration --------------------*/
  400. /* Set the RA bit according to RxAll value */
  401. /* Set the SAF and SAIF bits according to SrcAddrFilter value */
  402. /* Set the PCF bit according to PassCtrlFrames value */
  403. /* Set the DBF bit according to BroadcastFramesReception value */
  404. /* Set the DAIF bit according to DestAddrFilter value */
  405. /* Set the PEND bit according to PromiscuousMode value */
  406. /* Set the PM, HMC and HPF bits according to MulticastFramesFilter value */
  407. /* Set the HUC and HPF bits according to UnicastFramesFilter value */
  408. /* Write to ETHERNET MACFFLT */
  409. ETH->MACFFLT = (uint32_t)(ETH_InitStruct->RxAll | ETH_InitStruct->SrcAddrFilter | ETH_InitStruct->PassCtrlFrames
  410. | ETH_InitStruct->BroadcastFramesReception | ETH_InitStruct->DestAddrFilter
  411. | ETH_InitStruct->PromiscuousMode | ETH_InitStruct->MulticastFramesFilter
  412. | ETH_InitStruct->UnicastFramesFilter);
  413. /*--------------- ETHERNET MACHASHHI and MACHASHLO Configuration ---------------*/
  414. /* Write to ETHERNET MACHASHHI */
  415. ETH->MACHASHHI = (uint32_t)ETH_InitStruct->HashTableHigh;
  416. /* Write to ETHERNET MACHASHLO */
  417. ETH->MACHASHLO = (uint32_t)ETH_InitStruct->HashTableLow;
  418. /*----------------------- ETHERNET MACFLWCTRL Configuration --------------------*/
  419. /* Get the ETHERNET MACFLWCTRL value */
  420. tmpregister = ETH->MACFLWCTRL;
  421. /* Clear xx bits */
  422. tmpregister &= MACFCR_CLR_MASK;
  423. /* Set the PT bit according to PauseTime value */
  424. /* Set the DZPQ bit according to ZeroQuantaPause value */
  425. /* Set the PLT bit according to PauseLowThreshold value */
  426. /* Set the UP bit according to UnicastPauseFrameDetect value */
  427. /* Set the RFE bit according to RxFlowCtrl value */
  428. /* Set the TFE bit according to TxFlowCtrl value */
  429. tmpregister |= (uint32_t)((ETH_InitStruct->PauseTime << 16) | ETH_InitStruct->ZeroQuantaPause
  430. | ETH_InitStruct->PauseLowThreshold | ETH_InitStruct->UnicastPauseFrameDetect
  431. | ETH_InitStruct->RxFlowCtrl | ETH_InitStruct->TxFlowCtrl);
  432. /* Write to ETHERNET MACFLWCTRL */
  433. ETH->MACFLWCTRL = (uint32_t)tmpregister;
  434. /*----------------------- ETHERNET MACVLANTAG Configuration -----------------*/
  435. /* Set the ETV bit according to VLANTagComparison value */
  436. /* Set the VL bit according to VLANTagIdentifier value */
  437. ETH->MACVLANTAG = (uint32_t)(ETH_InitStruct->VLANTagComparison | ETH_InitStruct->VLANTagIdentifier);
  438. /*-------------------------------- DMA Config ------------------------------*/
  439. /*----------------------- ETHERNET DMAOPMOD Configuration --------------------*/
  440. /* Get the ETHERNET DMAOPMOD value */
  441. tmpregister = ETH->DMAOPMOD;
  442. /* Clear xx bits */
  443. tmpregister &= DMAOMR_CLR_MASK;
  444. /* Set the DT bit according to DropTCPIPChecksumErrorFrame value */
  445. /* Set the RSYF bit according to RxStoreForward value */
  446. /* Set the DFF bit according to FlushRxFrame value */
  447. /* Set the TSF bit according to TxStoreForward value */
  448. /* Set the TTC bit according to TxThresholdCtrl value */
  449. /* Set the FEF bit according to ForwardErrorFrames value */
  450. /* Set the FUF bit according to ForwardUndersizedGoodFrames value */
  451. /* Set the RTC bit according to RxThresholdCtrl value */
  452. /* Set the OSF bit according to SecondFrameOperate value */
  453. tmpregister |=
  454. (uint32_t)(ETH_InitStruct->DropTCPIPChecksumErrorFrame | ETH_InitStruct->RxStoreForward
  455. | ETH_InitStruct->FlushRxFrame | ETH_InitStruct->TxStoreForward | ETH_InitStruct->TxThresholdCtrl
  456. | ETH_InitStruct->ForwardErrorFrames | ETH_InitStruct->ForwardUndersizedGoodFrames
  457. | ETH_InitStruct->RxThresholdCtrl | ETH_InitStruct->SecondFrameOperate);
  458. /* Write to ETHERNET DMAOPMOD */
  459. ETH->DMAOPMOD = (uint32_t)tmpregister;
  460. /*----------------------- ETHERNET DMABUSMOD Configuration --------------------*/
  461. /* Set the AAL bit according to AddrAlignedBeats value */
  462. /* Set the FB bit according to FixedBurst value */
  463. /* Set the RPBL and 4*PBL bits according to RxDMABurstLen value */
  464. /* Set the PBL and 4*PBL bits according to TxDMABurstLen value */
  465. /* Set the DSL bit according to ETH_DesciptorSkipLength value */
  466. /* Set the PEND and DA bits according to DMAArbitration value */
  467. ETH->DMABUSMOD =
  468. (uint32_t)(ETH_InitStruct->AddrAlignedBeats | ETH_InitStruct->FixedBurst | ETH_InitStruct->RxDMABurstLen
  469. | /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */
  470. ETH_InitStruct->TxDMABurstLen | (ETH_InitStruct->DescSkipLen << 2) | ETH_InitStruct->DMAArbitration
  471. | ETH_DMABUSMOD_USP); /* Enable use of separate PBL for Rx and Tx */
  472. /* Disable all MMC interrupt */
  473. ETH->MMCRXINTMSK = 0xffffffffUL;
  474. ETH->MMCTXINTMSK = 0xffffffffUL;
  475. ETH->MMCRXCOINTMSK = 0xffffffffUL;
  476. /* Return Ethernet configuration success */
  477. return ETH_SUCCESS;
  478. }
  479. /**
  480. * @brief Fills each ETH_InitStruct member with its default value.
  481. * @param ETH_InitStruct pointer to a ETH_InitType structure which will be initialized.
  482. */
  483. void ETH_InitStruct(ETH_InitType* ETH_InitStruct)
  484. {
  485. /* ETH_InitStruct members default value */
  486. /*------------------------ MAC -----------------------------------*/
  487. ETH_InitStruct->AutoNegotiation = ETH_AUTONEG_DISABLE;
  488. ETH_InitStruct->Watchdog = ETH_WATCHDOG_ENABLE;
  489. ETH_InitStruct->Jabber = ETH_JABBER_ENABLE;
  490. ETH_InitStruct->InterFrameGap = ETH_INTER_FRAME_GAP_96BIT;
  491. ETH_InitStruct->CarrierSense = ETH_CARRIER_SENSE_ENABLE;
  492. ETH_InitStruct->SpeedMode = ETH_SPEED_MODE_10M;
  493. ETH_InitStruct->RxOwn = ETH_RX_OWN_ENABLE;
  494. ETH_InitStruct->LoopbackMode = ETH_LOOPBACK_MODE_DISABLE;
  495. ETH_InitStruct->DuplexMode = ETH_DUPLEX_MODE_HALF;
  496. ETH_InitStruct->ChecksumOffload = ETH_CHECKSUM_OFFLOAD_DISABLE;
  497. ETH_InitStruct->RetryTransmission = ETH_RETRY_TRANSMISSION_ENABLE;
  498. ETH_InitStruct->AutomaticPadCRCStrip = ETH_AUTO_PAD_CRC_STRIP_DISABLE;
  499. ETH_InitStruct->BackoffLimit = ETH_BACKOFF_LIMIT_10;
  500. ETH_InitStruct->DeferralCheck = ETH_DEFERRAL_CHECK_DISABLE;
  501. ETH_InitStruct->RxAll = ETH_RX_ALL_DISABLE;
  502. ETH_InitStruct->SrcAddrFilter = ETH_SRC_ADDR_FILTER_DISABLE;
  503. ETH_InitStruct->PassCtrlFrames = ETH_PASS_CTRL_FRAMES_BLOCK_ALL;
  504. ETH_InitStruct->BroadcastFramesReception = ETH_BROADCAST_FRAMES_RECEPTION_DISABLE;
  505. ETH_InitStruct->DestAddrFilter = ETH_DEST_ADDR_FILTER_NORMAL;
  506. ETH_InitStruct->PromiscuousMode = ETH_PROMISCUOUS_MODE_DISABLE;
  507. ETH_InitStruct->MulticastFramesFilter = ETH_MULTICAST_FRAMES_FILTER_PERFECT;
  508. ETH_InitStruct->UnicastFramesFilter = ETH_UNICAST_FRAMES_FILTER_PERFECT;
  509. ETH_InitStruct->HashTableHigh = 0x0;
  510. ETH_InitStruct->HashTableLow = 0x0;
  511. ETH_InitStruct->PauseTime = 0x0;
  512. ETH_InitStruct->ZeroQuantaPause = ETH_ZERO_QUANTA_PAUSE_DISABLE;
  513. ETH_InitStruct->PauseLowThreshold = ETH_PAUSE_LOW_THRESHOLD_MINUS4;
  514. ETH_InitStruct->UnicastPauseFrameDetect = ETH_UNICAST_PAUSE_FRAME_DETECT_DISABLE;
  515. ETH_InitStruct->RxFlowCtrl = ETH_RX_FLOW_CTRL_DISABLE;
  516. ETH_InitStruct->TxFlowCtrl = ETH_TX_FLOW_CTRL_DISABLE;
  517. ETH_InitStruct->VLANTagComparison = ETH_VLAN_TAG_COMPARISON_16BIT;
  518. ETH_InitStruct->VLANTagIdentifier = 0x0;
  519. /*------------------------ DMA -----------------------------------*/
  520. ETH_InitStruct->DropTCPIPChecksumErrorFrame = ETH_DROP_TCPIP_CHECKSUM_ERROR_FRAME_DISABLE;
  521. ETH_InitStruct->RxStoreForward = ETH_RX_STORE_FORWARD_ENABLE;
  522. ETH_InitStruct->FlushRxFrame = ETH_FLUSH_RX_FRAME_DISABLE;
  523. ETH_InitStruct->TxStoreForward = ETH_TX_STORE_FORWARD_ENABLE;
  524. ETH_InitStruct->TxThresholdCtrl = ETH_TX_THRESHOLD_CTRL_64BYTES;
  525. ETH_InitStruct->ForwardErrorFrames = ETH_FORWARD_ERROR_FRAMES_DISABLE;
  526. ETH_InitStruct->ForwardUndersizedGoodFrames = ETH_FORWARD_UNDERSIZED_GOOD_FRAMES_ENABLE;
  527. ETH_InitStruct->RxThresholdCtrl = ETH_RX_THRESHOLD_CTRL_64BYTES;
  528. ETH_InitStruct->SecondFrameOperate = ETH_SECOND_FRAME_OPERATE_DISABLE;
  529. ETH_InitStruct->AddrAlignedBeats = ETH_ADDR_ALIGNED_BEATS_ENABLE;
  530. ETH_InitStruct->FixedBurst = ETH_FIXED_BURST_DISABLE;
  531. ETH_InitStruct->RxDMABurstLen = ETH_RX_DMA_BURST_LEN_1BEAT;
  532. ETH_InitStruct->TxDMABurstLen = ETH_TX_DMA_BURST_LEN_1BEAT;
  533. ETH_InitStruct->DescSkipLen = 0x0;
  534. ETH_InitStruct->DMAArbitration = ETH_DMA_ARBITRATION_ROUND_ROBIN_RXTX_1_1;
  535. }
  536. /**
  537. * @brief Enables ENET MAC and DMA reception/transmission
  538. */
  539. void ETH_EnableTxRx(void)
  540. {
  541. /* Enable transmit state machine of the MAC for transmission on the MII */
  542. ETH_EnableMacTx(ENABLE);
  543. /* Flush Transmit DATFIFO */
  544. ETH_FlushTxFifo();
  545. /* Enable receive state machine of the MAC for reception from the MII */
  546. ETH_EnableMacRx(ENABLE);
  547. /* Start DMA transmission */
  548. ETH_EnableDmaTx(ENABLE);
  549. /* Start DMA reception */
  550. ETH_EnableDmaRx(ENABLE);
  551. }
  552. /**
  553. * @brief Transmits a packet, from application buffer, pointed by ppkt.
  554. * @param ppkt pointer to the application's packet buffer to transmit.
  555. * @param FrameLength Tx Packet size.
  556. * @return ETH_ERROR: in case of Tx desc owned by DMA
  557. * ETH_SUCCESS: for correct transmission
  558. */
  559. uint32_t ETH_TxPacket(uint8_t* ppkt, uint16_t FrameLength)
  560. {
  561. uint32_t send_len = 0;
  562. while (send_len < FrameLength)
  563. {
  564. uint32_t offset = 0;
  565. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  566. if ((DMATxDescToSet->Status & ETH_DMA_TX_DESC_OWN) != (uint32_t)RESET)
  567. {
  568. /* Return ERROR: OWN bit set */
  569. return ETH_ERROR;
  570. }
  571. uint16_t block_len = FrameLength - send_len;
  572. if (block_len > ETH_DMA_TX_DESC_TBS1)
  573. {
  574. block_len = ETH_DMA_TX_DESC_TBS1;
  575. }
  576. /* Copy the frame to be sent into memory pointed by the current ETHERNET DMA Tx descriptor */
  577. for (offset = 0; offset < block_len; offset++)
  578. {
  579. (*(__IO uint8_t*)((DMATxDescToSet->Buf1Addr) + offset)) = (*(ppkt + offset + send_len));
  580. }
  581. /* Setting the Frame Length: bits[10:0] */
  582. DMATxDescToSet->CtrlOrBufSize &= (~ETH_DMA_TX_DESC_TBS1);
  583. DMATxDescToSet->CtrlOrBufSize |= block_len;
  584. /* Setting the last segment and first segment bits (in this case a frame is transmitted in one descriptor) */
  585. if (send_len == 0)
  586. {
  587. DMATxDescToSet->CtrlOrBufSize |= ETH_DMA_TX_DESC_FS;
  588. }
  589. send_len += block_len;
  590. if (send_len == FrameLength)
  591. {
  592. DMATxDescToSet->CtrlOrBufSize |= ETH_DMA_TX_DESC_LS;
  593. }
  594. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  595. DMATxDescToSet->Status |= ETH_DMA_TX_DESC_OWN;
  596. /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
  597. if ((ETH->DMASTS & ETH_DMASTS_TU) != (uint32_t)RESET)
  598. {
  599. /* Clear TBUS ETHERNET DMA flag */
  600. ETH->DMASTS = ETH_DMASTS_TU;
  601. /* Resume DMA transmission*/
  602. ETH->DMATXPD = 0;
  603. }
  604. /* Update the ETHERNET DMA global Tx descriptor with next Tx decriptor */
  605. /* Chained Mode */
  606. if ((DMATxDescToSet->CtrlOrBufSize & ETH_DMA_TX_DESC_TCH) != (uint32_t)RESET)
  607. {
  608. /* Selects the next DMA Tx descriptor list for next buffer to send */
  609. DMATxDescToSet = (ETH_DMADescType*)(DMATxDescToSet->Buf2OrNextDescAddr);
  610. }
  611. else /* Ring Mode */
  612. {
  613. if ((DMATxDescToSet->CtrlOrBufSize & ETH_DMA_TX_DESC_TER) != (uint32_t)RESET)
  614. {
  615. /* Selects the first DMA Tx descriptor for next buffer to send: last Tx descriptor was used */
  616. DMATxDescToSet = (ETH_DMADescType*)(ETH->DMATXDLADDR);
  617. }
  618. else
  619. {
  620. /* Selects the next DMA Tx descriptor list for next buffer to send */
  621. DMATxDescToSet =
  622. (ETH_DMADescType*)((uint32_t)DMATxDescToSet + 0x10 + ((ETH->DMABUSMOD & ETH_DMABUSMOD_DSL)));
  623. }
  624. }
  625. }
  626. /* Return SUCCESS */
  627. return ETH_SUCCESS;
  628. }
  629. /**
  630. * @brief Receives a packet and copies it to memory pointed by ppkt.
  631. * @param ppkt pointer to the application packet receive buffer.
  632. * @param checkErr whether check error
  633. * @return ETH_ERROR: if there is error in reception
  634. * framelength: received packet size if packet reception is correct
  635. */
  636. uint32_t ETH_RxPacket(uint8_t* ppkt, uint8_t checkErr)
  637. {
  638. uint32_t offset = 0, framelength = 0;
  639. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  640. if ((DMARxDescToGet->Status & ETH_DMA_RX_DESC_OWN) != (uint32_t)RESET)
  641. {
  642. /* Return error: OWN bit set */
  643. return ETH_ERROR;
  644. }
  645. if (((checkErr && ((DMARxDescToGet->Status & ETH_DMA_RX_DESC_ES) == (uint32_t)RESET)) || !checkErr)
  646. && ((DMARxDescToGet->Status & ETH_DMA_RX_DESC_LS) != (uint32_t)RESET)
  647. && ((DMARxDescToGet->Status & ETH_DMA_RX_DESC_FS) != (uint32_t)RESET))
  648. {
  649. /* Get the Frame Length of the received packet */
  650. framelength = ((DMARxDescToGet->Status & ETH_DMA_RX_DESC_FL) >> ETH_DMA_RX_DESC_FRAME_LEN_SHIFT);
  651. /* Copy the received frame into buffer from memory pointed by the current ETHERNET DMA Rx descriptor */
  652. for (offset = 0; offset < framelength; offset++)
  653. {
  654. (*(ppkt + offset)) = (*(__IO uint8_t*)((DMARxDescToGet->Buf1Addr) + offset));
  655. }
  656. }
  657. else
  658. {
  659. /* Return ERROR */
  660. framelength = ETH_ERROR;
  661. }
  662. /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */
  663. DMARxDescToGet->Status = ETH_DMA_RX_DESC_OWN;
  664. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  665. if ((ETH->DMASTS & ETH_DMASTS_RU) != (uint32_t)RESET)
  666. {
  667. /* Clear RBUS ETHERNET DMA flag */
  668. ETH->DMASTS = ETH_DMASTS_RU;
  669. /* Resume DMA reception */
  670. ETH->DMARXPD = 0;
  671. }
  672. /* Update the ETHERNET DMA global Rx descriptor with next Rx decriptor */
  673. /* Chained Mode */
  674. if ((DMARxDescToGet->CtrlOrBufSize & ETH_DMA_RX_DESC_RCH) != (uint32_t)RESET)
  675. {
  676. /* Selects the next DMA Rx descriptor list for next buffer to read */
  677. DMARxDescToGet = (ETH_DMADescType*)(DMARxDescToGet->Buf2OrNextDescAddr);
  678. }
  679. else /* Ring Mode */
  680. {
  681. if ((DMARxDescToGet->CtrlOrBufSize & ETH_DMA_RX_DESC_RER) != (uint32_t)RESET)
  682. {
  683. /* Selects the first DMA Rx descriptor for next buffer to read: last Rx descriptor was used */
  684. DMARxDescToGet = (ETH_DMADescType*)(ETH->DMARXDLADDR);
  685. }
  686. else
  687. {
  688. /* Selects the next DMA Rx descriptor list for next buffer to read */
  689. DMARxDescToGet =
  690. (ETH_DMADescType*)((uint32_t)DMARxDescToGet + 0x10 + ((ETH->DMABUSMOD & ETH_DMABUSMOD_DSL)));
  691. }
  692. }
  693. /* Return Frame Length/ERROR */
  694. return (framelength);
  695. }
  696. /**
  697. * @brief Get the size of received the received packet.
  698. * @return framelength: received packet size
  699. */
  700. uint32_t ETH_GetRxPacketSize(void)
  701. {
  702. uint32_t frameLength = 0;
  703. if (((DMARxDescToGet->Status & ETH_DMA_RX_DESC_OWN) == (uint32_t)RESET)
  704. && ((DMARxDescToGet->Status & ETH_DMA_RX_DESC_ES) == (uint32_t)RESET)
  705. && ((DMARxDescToGet->Status & ETH_DMA_RX_DESC_LS) != (uint32_t)RESET)
  706. && ((DMARxDescToGet->Status & ETH_DMA_RX_DESC_FS) != (uint32_t)RESET))
  707. {
  708. /* Get the size of the packet: including 4 bytes of the CRC */
  709. frameLength = ETH_GetDmaRxDescFrameLen(DMARxDescToGet);
  710. }
  711. /* Return Frame Length */
  712. return frameLength;
  713. }
  714. /**
  715. * @brief Drop a Received packet (too small packet, etc...)
  716. */
  717. void ETH_DropRxPacket(void)
  718. {
  719. /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */
  720. DMARxDescToGet->Status = ETH_DMA_RX_DESC_OWN;
  721. /* Chained Mode */
  722. if ((DMARxDescToGet->CtrlOrBufSize & ETH_DMA_RX_DESC_RCH) != (uint32_t)RESET)
  723. {
  724. /* Selects the next DMA Rx descriptor list for next buffer read */
  725. DMARxDescToGet = (ETH_DMADescType*)(DMARxDescToGet->Buf2OrNextDescAddr);
  726. }
  727. else /* Ring Mode */
  728. {
  729. if ((DMARxDescToGet->CtrlOrBufSize & ETH_DMA_RX_DESC_RER) != (uint32_t)RESET)
  730. {
  731. /* Selects the next DMA Rx descriptor list for next buffer read: this will
  732. be the first Rx descriptor in this case */
  733. DMARxDescToGet = (ETH_DMADescType*)(ETH->DMARXDLADDR);
  734. }
  735. else
  736. {
  737. /* Selects the next DMA Rx descriptor list for next buffer read */
  738. DMARxDescToGet =
  739. (ETH_DMADescType*)((uint32_t)DMARxDescToGet + 0x10 + ((ETH->DMABUSMOD & ETH_DMABUSMOD_DSL)));
  740. }
  741. }
  742. }
  743. /*--------------------------------- PHY ------------------------------------*/
  744. /**
  745. * @brief Read a PHY register
  746. * @param PHYAddress PHY device address, is the index of one of supported 32 PHY devices.
  747. * This parameter can be one of the following values: 0,..,31
  748. * @param PHYReg PHY register address, is the index of one of the 32 PHY register.
  749. * This parameter can be one of the following values:
  750. * @arg PHY_BCR Tranceiver Basic Control Register
  751. * @arg PHY_BSR Tranceiver Basic Status Register
  752. * @arg PHY_SR Tranceiver Status Register
  753. * @arg More PHY register could be read depending on the used PHY
  754. * @return ETH_ERROR: in case of timeout
  755. * MAC MIIDR register value: Data read from the selected PHY register (correct read )
  756. */
  757. uint16_t ETH_ReadPhyRegister(uint16_t PHYAddress, uint16_t PHYReg)
  758. {
  759. uint32_t tmpregister = 0;
  760. __IO uint32_t timeout = 0;
  761. /* Check the parameters */
  762. assert_param(IS_ETH_PHY_ADDRESS(PHYAddress));
  763. assert_param(IS_ETH_PHY_REG(PHYReg));
  764. /* Get the ETHERNET MACMIIADDR value */
  765. tmpregister = ETH->MACMIIADDR;
  766. /* Keep only the CTRLSTS Clock Range CTRL[2:0] bits value */
  767. tmpregister &= ~MACMIIAR_CR_MASK;
  768. /* Prepare the MII address register value */
  769. tmpregister |= (((uint32_t)PHYAddress << 11) & ETH_MACMIIADDR_PA); /* Set the PHY device address */
  770. tmpregister |= (((uint32_t)PHYReg << 6) & ETH_MACMIIADDR_MR); /* Set the PHY register address */
  771. tmpregister &= ~ETH_MACMIIADDR_MW; /* Set the read mode */
  772. tmpregister |= ETH_MACMIIADDR_MB; /* Set the MII Busy bit */
  773. /* Write the result value into the MII Address register */
  774. ETH->MACMIIADDR = tmpregister;
  775. /* Check for the Busy flag */
  776. do
  777. {
  778. timeout++;
  779. tmpregister = ETH->MACMIIADDR;
  780. } while ((tmpregister & ETH_MACMIIADDR_MB) && (timeout < (uint32_t)PHY_READ_TO));
  781. /* Return ERROR in case of timeout */
  782. if (timeout == PHY_READ_TO)
  783. {
  784. return (uint16_t)ETH_ERROR;
  785. }
  786. /* Return data register value */
  787. uint16_t ret = (uint16_t)(ETH->MACMIIDAT);
  788. return ret;
  789. }
  790. /**
  791. * @brief Write to a PHY register
  792. * @param PHYAddress PHY device address, is the index of one of supported 32 PHY devices.
  793. * This parameter can be one of the following values: 0,..,31
  794. * @param PHYReg PHY register address, is the index of one of the 32 PHY register.
  795. * This parameter can be one of the following values:
  796. * @arg PHY_BCR Tranceiver Control Register
  797. * @arg More PHY register could be written depending on the used PHY
  798. * @param PHYValue the value to write
  799. * @return ETH_ERROR: in case of timeout
  800. * ETH_SUCCESS: for correct write
  801. */
  802. uint32_t ETH_WritePhyRegister(uint16_t PHYAddress, uint16_t PHYReg, uint16_t PHYValue)
  803. {
  804. uint32_t tmpregister = 0;
  805. __IO uint32_t timeout = 0;
  806. /* Check the parameters */
  807. assert_param(IS_ETH_PHY_ADDRESS(PHYAddress));
  808. assert_param(IS_ETH_PHY_REG(PHYReg));
  809. /* Get the ETHERNET MACMIIADDR value */
  810. tmpregister = ETH->MACMIIADDR;
  811. /* Keep only the CTRLSTS Clock Range CTRL[2:0] bits value */
  812. tmpregister &= ~MACMIIAR_CR_MASK;
  813. /* Prepare the MII register address value */
  814. tmpregister |= (((uint32_t)PHYAddress << 11) & ETH_MACMIIADDR_PA); /* Set the PHY device address */
  815. tmpregister |= (((uint32_t)PHYReg << 6) & ETH_MACMIIADDR_MR); /* Set the PHY register address */
  816. tmpregister |= ETH_MACMIIADDR_MW; /* Set the write mode */
  817. tmpregister |= ETH_MACMIIADDR_MB; /* Set the MII Busy bit */
  818. /* Give the value to the MII data register */
  819. ETH->MACMIIDAT = PHYValue;
  820. /* Write the result value into the MII Address register */
  821. ETH->MACMIIADDR = tmpregister;
  822. /* Check for the Busy flag */
  823. do
  824. {
  825. timeout++;
  826. tmpregister = ETH->MACMIIADDR;
  827. } while ((tmpregister & ETH_MACMIIADDR_MB) && (timeout < (uint32_t)PHY_WRITE_TO));
  828. /* Return ERROR in case of timeout */
  829. if (timeout == PHY_WRITE_TO)
  830. {
  831. return ETH_ERROR;
  832. }
  833. /* Return SUCCESS */
  834. return ETH_SUCCESS;
  835. }
  836. /**
  837. * @brief Enables or disables the PHY loopBack mode.
  838. * @note: Don't be confused with ETH_MACLoopBackCmd function which enables internal
  839. * loopback at MII level
  840. * @param PHYAddress PHY device address, is the index of one of supported 32 PHY devices.
  841. * This parameter can be one of the following values:
  842. * @param Cmd new state of the PHY loopBack mode.
  843. * This parameter can be: ENABLE or DISABLE.
  844. * @return ETH_ERROR: in case of bad PHY configuration
  845. * ETH_SUCCESS: for correct PHY configuration
  846. */
  847. uint32_t ETH_EnablePhyLoopBack(uint16_t PHYAddress, FunctionalState Cmd)
  848. {
  849. uint16_t tmpregister = 0;
  850. /* Check the parameters */
  851. assert_param(IS_ETH_PHY_ADDRESS(PHYAddress));
  852. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  853. /* Get the PHY configuration to update it */
  854. tmpregister = ETH_ReadPhyRegister(PHYAddress, PHY_BCR);
  855. if (Cmd != DISABLE)
  856. {
  857. /* Enable the PHY loopback mode */
  858. tmpregister |= PHY_LOOPBACK;
  859. }
  860. else
  861. {
  862. /* Disable the PHY loopback mode: normal mode */
  863. tmpregister &= (uint16_t)(~(uint16_t)PHY_LOOPBACK);
  864. }
  865. /* Update the PHY control register with the new configuration */
  866. if (ETH_WritePhyRegister(PHYAddress, PHY_BCR, tmpregister) != (uint32_t)RESET)
  867. {
  868. return ETH_SUCCESS;
  869. }
  870. else
  871. {
  872. /* Return SUCCESS */
  873. return ETH_ERROR;
  874. }
  875. }
  876. /*--------------------------------- MAC ------------------------------------*/
  877. /**
  878. * @brief Enables or disables the MAC transmission.
  879. * @param Cmd new state of the MAC transmission.
  880. * This parameter can be: ENABLE or DISABLE.
  881. */
  882. void ETH_EnableMacTx(FunctionalState Cmd)
  883. {
  884. /* Check the parameters */
  885. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  886. if (Cmd != DISABLE)
  887. {
  888. /* Enable the MAC transmission */
  889. ETH->MACCFG |= ETH_MACCFG_TE;
  890. }
  891. else
  892. {
  893. /* Disable the MAC transmission */
  894. ETH->MACCFG &= ~ETH_MACCFG_TE;
  895. }
  896. }
  897. /**
  898. * @brief Enables or disables the MAC reception.
  899. * @param Cmd new state of the MAC reception.
  900. * This parameter can be: ENABLE or DISABLE.
  901. */
  902. void ETH_EnableMacRx(FunctionalState Cmd)
  903. {
  904. /* Check the parameters */
  905. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  906. if (Cmd != DISABLE)
  907. {
  908. /* Enable the MAC reception */
  909. ETH->MACCFG |= ETH_MACCFG_RE;
  910. }
  911. else
  912. {
  913. /* Disable the MAC reception */
  914. ETH->MACCFG &= ~ETH_MACCFG_RE;
  915. }
  916. }
  917. /**
  918. * @brief Checks whether the ETHERNET flow control busy bit is set or not.
  919. * @return The new state of flow control busy status bit (SET or RESET).
  920. */
  921. FlagStatus ETH_GetFlowCtrlBusyStatus(void)
  922. {
  923. FlagStatus bitstatus = RESET;
  924. /* The Flow Control register should not be written to until this bit is cleared */
  925. if ((ETH->MACFLWCTRL & ETH_MACFLWCTRL_FCB_BPA) != (uint32_t)RESET)
  926. {
  927. bitstatus = SET;
  928. }
  929. else
  930. {
  931. bitstatus = RESET;
  932. }
  933. return bitstatus;
  934. }
  935. /**
  936. * @brief Initiate a Pause Control Frame (Full-duplex only).
  937. */
  938. void ETH_GeneratePauseCtrlFrame(void)
  939. {
  940. /* When Set In full duplex MAC initiates pause control frame */
  941. ETH->MACFLWCTRL |= ETH_MACFLWCTRL_FCB_BPA;
  942. }
  943. /**
  944. * @brief Enables or disables the MAC BackPressure operation activation (Half-duplex only).
  945. * @param Cmd new state of the MAC BackPressure operation activation.
  946. * This parameter can be: ENABLE or DISABLE.
  947. */
  948. void ETH_EnableBackPressureActivation(FunctionalState Cmd)
  949. {
  950. /* Check the parameters */
  951. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  952. if (Cmd != DISABLE)
  953. {
  954. /* Activate the MAC BackPressure operation */
  955. /* In Half duplex: during backpressure, when the MAC receives a new frame,
  956. the transmitter starts sending a JAM pattern resulting in a collision */
  957. ETH->MACFLWCTRL |= ETH_MACFLWCTRL_FCB_BPA;
  958. }
  959. else
  960. {
  961. /* Desactivate the MAC BackPressure operation */
  962. ETH->MACFLWCTRL &= ~ETH_MACFLWCTRL_FCB_BPA;
  963. }
  964. }
  965. /**
  966. * @brief Checks whether the specified ETHERNET MAC flag is set or not.
  967. * @param ETH_MAC_FLAG specifies the flag to check.
  968. * This parameter can be one of the following values:
  969. * @arg ETH_MAC_FLAG_TST Time stamp trigger flag
  970. * @arg ETH_MAC_FLAG_MMCTX MMC transmit flag
  971. * @arg ETH_MAC_FLAG_MMCRX MMC receive flag
  972. * @arg ETH_MAC_FLAG_MMC MMC flag
  973. * @arg ETH_MAC_FLAG_PMT PMT flag
  974. * @return The new state of ETHERNET MAC flag (SET or RESET).
  975. */
  976. FlagStatus ETH_GetMacFlagStatus(uint32_t ETH_MAC_FLAG)
  977. {
  978. FlagStatus bitstatus = RESET;
  979. /* Check the parameters */
  980. assert_param(IS_ETH_MAC_GET_FLAG(ETH_MAC_FLAG));
  981. if ((ETH->MACINTSTS & ETH_MAC_FLAG) != (uint32_t)RESET)
  982. {
  983. bitstatus = SET;
  984. }
  985. else
  986. {
  987. bitstatus = RESET;
  988. }
  989. return bitstatus;
  990. }
  991. /**
  992. * @brief Checks whether the specified ETHERNET MAC interrupt has occurred or not.
  993. * @param ETH_MAC_IT specifies the interrupt source to check.
  994. * This parameter can be one of the following values:
  995. * @arg ETH_MAC_INT_TST Time stamp trigger interrupt
  996. * @arg ETH_MAC_INT_MMCTX MMC transmit interrupt
  997. * @arg ETH_MAC_INT_MMCRX MMC receive interrupt
  998. * @arg ETH_MAC_INT_MMC MMC interrupt
  999. * @arg ETH_MAC_INT_PMT PMT interrupt
  1000. * @return The new state of ETHERNET MAC interrupt (SET or RESET).
  1001. */
  1002. INTStatus ETH_GetMacIntStatus(uint32_t ETH_MAC_IT)
  1003. {
  1004. INTStatus bitstatus = RESET;
  1005. /* Check the parameters */
  1006. assert_param(IS_ETH_MAC_GET_INT(ETH_MAC_IT));
  1007. if ((ETH->MACINTSTS & ETH_MAC_IT) != (uint32_t)RESET)
  1008. {
  1009. bitstatus = SET;
  1010. }
  1011. else
  1012. {
  1013. bitstatus = RESET;
  1014. }
  1015. return bitstatus;
  1016. }
  1017. /**
  1018. * @brief Enables or disables the specified ETHERNET MAC interrupts.
  1019. * @param ETH_MAC_IT specifies the ETHERNET MAC interrupt sources to be
  1020. * enabled or disabled.
  1021. * This parameter can be any combination of the following values:
  1022. * @arg ETH_MAC_INT_TST Time stamp trigger interrupt
  1023. * @arg ETH_MAC_INT_PMT PMT interrupt
  1024. * @param Cmd new state of the specified ETHERNET MAC interrupts.
  1025. * This parameter can be: ENABLE or DISABLE.
  1026. */
  1027. void ETH_EnableMacInt(uint32_t ETH_MAC_IT, FunctionalState Cmd)
  1028. {
  1029. /* Check the parameters */
  1030. assert_param(IS_ETH_MAC_INT(ETH_MAC_IT));
  1031. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1032. if (Cmd != DISABLE)
  1033. {
  1034. /* Enable the selected ETHERNET MAC interrupts */
  1035. ETH->MACINTMSK &= (~(uint32_t)ETH_MAC_IT);
  1036. }
  1037. else
  1038. {
  1039. /* Disable the selected ETHERNET MAC interrupts */
  1040. ETH->MACINTMSK |= ETH_MAC_IT;
  1041. }
  1042. }
  1043. /**
  1044. * @brief Configures the selected MAC address.
  1045. * @param MacAddr The MAC addres to configure.
  1046. * This parameter can be one of the following values:
  1047. * @arg ETH_MAC_ADDR0 MAC Address0
  1048. * @arg ETH_MAC_ADDR1 MAC Address1
  1049. * @arg ETH_MAC_ADDR2 MAC Address2
  1050. * @arg ETH_MAC_ADDR3 MAC Address3
  1051. * @param Addr Pointer on MAC address buffer data (6 bytes).
  1052. */
  1053. void ETH_SetMacAddr(uint32_t MacAddr, uint8_t* Addr)
  1054. {
  1055. uint32_t tmpregister;
  1056. /* Check the parameters */
  1057. assert_param(IS_ETH_MAC_ADDR0123(MacAddr));
  1058. /* Calculate the selectecd MAC address high register */
  1059. tmpregister = ((uint32_t)Addr[5] << 8) | (uint32_t)Addr[4];
  1060. /* Load the selectecd MAC address high register */
  1061. (*(__IO uint32_t*)(ETH_MAC_ADDR_HBASE + MacAddr)) = tmpregister;
  1062. /* Calculate the selectecd MAC address low register */
  1063. tmpregister = ((uint32_t)Addr[3] << 24) | ((uint32_t)Addr[2] << 16) | ((uint32_t)Addr[1] << 8) | Addr[0];
  1064. /* Load the selectecd MAC address low register */
  1065. (*(__IO uint32_t*)(ETH_MAC_ADDR_LBASE + MacAddr)) = tmpregister;
  1066. }
  1067. /**
  1068. * @brief Get the selected MAC address.
  1069. * @param MacAddr The MAC addres to return.
  1070. * This parameter can be one of the following values:
  1071. * @arg ETH_MAC_ADDR0 MAC Address0
  1072. * @arg ETH_MAC_ADDR1 MAC Address1
  1073. * @arg ETH_MAC_ADDR2 MAC Address2
  1074. * @arg ETH_MAC_ADDR3 MAC Address3
  1075. * @param Addr Pointer on MAC address buffer data (6 bytes).
  1076. */
  1077. void ETH_GetMacAddr(uint32_t MacAddr, uint8_t* Addr)
  1078. {
  1079. uint32_t tmpregister;
  1080. /* Check the parameters */
  1081. assert_param(IS_ETH_MAC_ADDR0123(MacAddr));
  1082. /* Get the selectecd MAC address high register */
  1083. tmpregister = (*(__IO uint32_t*)(ETH_MAC_ADDR_HBASE + MacAddr));
  1084. /* Calculate the selectecd MAC address buffer */
  1085. Addr[5] = ((tmpregister >> 8) & (uint8_t)0xFF);
  1086. Addr[4] = (tmpregister & (uint8_t)0xFF);
  1087. /* Load the selectecd MAC address low register */
  1088. tmpregister = (*(__IO uint32_t*)(ETH_MAC_ADDR_LBASE + MacAddr));
  1089. /* Calculate the selectecd MAC address buffer */
  1090. Addr[3] = ((tmpregister >> 24) & (uint8_t)0xFF);
  1091. Addr[2] = ((tmpregister >> 16) & (uint8_t)0xFF);
  1092. Addr[1] = ((tmpregister >> 8) & (uint8_t)0xFF);
  1093. Addr[0] = (tmpregister & (uint8_t)0xFF);
  1094. }
  1095. /**
  1096. * @brief Enables or disables the Address filter module uses the specified
  1097. * ETHERNET MAC address for perfect filtering
  1098. * @param MacAddr specifies the ETHERNET MAC address to be used for prfect filtering.
  1099. * This parameter can be one of the following values:
  1100. * @arg ETH_MAC_ADDR1 MAC Address1
  1101. * @arg ETH_MAC_ADDR2 MAC Address2
  1102. * @arg ETH_MAC_ADDR3 MAC Address3
  1103. * @param Cmd new state of the specified ETHERNET MAC address use.
  1104. * This parameter can be: ENABLE or DISABLE.
  1105. */
  1106. void ETH_EnableMacAddrPerfectFilter(uint32_t MacAddr, FunctionalState Cmd)
  1107. {
  1108. /* Check the parameters */
  1109. assert_param(IS_ETH_MAC_ADDR123(MacAddr));
  1110. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1111. if (Cmd != DISABLE)
  1112. {
  1113. /* Enable the selected ETHERNET MAC address for perfect filtering */
  1114. (*(__IO uint32_t*)(ETH_MAC_ADDR_HBASE + MacAddr)) |= ETH_MACADDR1HI_AE;
  1115. }
  1116. else
  1117. {
  1118. /* Disable the selected ETHERNET MAC address for perfect filtering */
  1119. (*(__IO uint32_t*)(ETH_MAC_ADDR_HBASE + MacAddr)) &= (~(uint32_t)ETH_MACADDR1HI_AE);
  1120. }
  1121. }
  1122. /**
  1123. * @brief Set the filter type for the specified ETHERNET MAC address
  1124. * @param MacAddr specifies the ETHERNET MAC address
  1125. * This parameter can be one of the following values:
  1126. * @arg ETH_MAC_ADDR1 MAC Address1
  1127. * @arg ETH_MAC_ADDR2 MAC Address2
  1128. * @arg ETH_MAC_ADDR3 MAC Address3
  1129. * @param Filter specifies the used frame received field for comparaison
  1130. * This parameter can be one of the following values:
  1131. * @arg ETH_MAC_ADDR_FILTER_SA MAC Address is used to compare with the
  1132. * SA fields of the received frame.
  1133. * @arg ETH_MAC_ADDR_FILTER_DA MAC Address is used to compare with the
  1134. * DA fields of the received frame.
  1135. */
  1136. void ETH_ConfigMacAddrFilter(uint32_t MacAddr, uint32_t Filter)
  1137. {
  1138. /* Check the parameters */
  1139. assert_param(IS_ETH_MAC_ADDR123(MacAddr));
  1140. assert_param(IS_ETH_MAC_ADDR_FILTER(Filter));
  1141. if (Filter != ETH_MAC_ADDR_FILTER_DA)
  1142. {
  1143. /* The selected ETHERNET MAC address is used to compare with the SA fields of the
  1144. received frame. */
  1145. (*(__IO uint32_t*)(ETH_MAC_ADDR_HBASE + MacAddr)) |= ETH_MACADDR1HI_SA;
  1146. }
  1147. else
  1148. {
  1149. /* The selected ETHERNET MAC address is used to compare with the DA fields of the
  1150. received frame. */
  1151. (*(__IO uint32_t*)(ETH_MAC_ADDR_HBASE + MacAddr)) &= (~(uint32_t)ETH_MACADDR1HI_SA);
  1152. }
  1153. }
  1154. /**
  1155. * @brief Set the filter type for the specified ETHERNET MAC address
  1156. * @param MacAddr specifies the ETHERNET MAC address
  1157. * This parameter can be one of the following values:
  1158. * @arg ETH_MAC_ADDR1 MAC Address1
  1159. * @arg ETH_MAC_ADDR2 MAC Address2
  1160. * @arg ETH_MAC_ADDR3 MAC Address3
  1161. * @param MaskByte specifies the used address bytes for comparaison
  1162. * This parameter can be any combination of the following values:
  1163. * @arg ETH_MAC_ADDR_MASK_BYTE6 Mask MAC Address high reg bits [15:8].
  1164. * @arg ETH_MAC_ADDR_MASK_BYTE5 Mask MAC Address high reg bits [7:0].
  1165. * @arg ETH_MAC_ADDR_MASK_BYTE4 Mask MAC Address low reg bits [31:24].
  1166. * @arg ETH_MAC_ADDR_MASK_BYTE3 Mask MAC Address low reg bits [23:16].
  1167. * @arg ETH_MAC_ADDR_MASK_BYTE2 Mask MAC Address low reg bits [15:8].
  1168. * @arg ETH_MAC_ADDR_MASK_BYTE1 Mask MAC Address low reg bits [7:0].
  1169. */
  1170. void ETH_ConfigMacAddrMaskBytesFilter(uint32_t MacAddr, uint32_t MaskByte)
  1171. {
  1172. /* Check the parameters */
  1173. assert_param(IS_ETH_MAC_ADDR123(MacAddr));
  1174. assert_param(IS_ETH_MAC_ADDR_MASK(MaskByte));
  1175. /* Clear MBC bits in the selected MAC address high register */
  1176. (*(__IO uint32_t*)(ETH_MAC_ADDR_HBASE + MacAddr)) &= (~(uint32_t)ETH_MACADDR1HI_MBC);
  1177. /* Set the selected Filetr mask bytes */
  1178. (*(__IO uint32_t*)(ETH_MAC_ADDR_HBASE + MacAddr)) |= MaskByte;
  1179. }
  1180. /*------------------------ DMA Tx/Rx Desciptors -----------------------------*/
  1181. /**
  1182. * @brief Initializes the DMA Tx descriptors in chain mode.
  1183. * @param DMATxDescTab Pointer on the first Tx desc list
  1184. * @param TxBuff Pointer on the first TxBuffer list
  1185. * @param BuffSize Buffer size of each descriptor
  1186. * @param TxBuffCount Number of the used Tx desc in the list
  1187. */
  1188. void ETH_ConfigDmaTxDescInChainMode(ETH_DMADescType* DMATxDescTab,
  1189. uint8_t* TxBuff,
  1190. uint32_t BuffSize,
  1191. uint32_t TxBuffCount)
  1192. {
  1193. uint32_t i = 0;
  1194. ETH_DMADescType* DMATxDesc;
  1195. /* Set the DMATxDescToSet pointer with the first one of the DMATxDescTab list */
  1196. DMATxDescToSet = DMATxDescTab;
  1197. /* Fill each DMATxDesc descriptor with the right values */
  1198. for (i = 0; i < TxBuffCount; i++)
  1199. {
  1200. /* Get the pointer on the ith member of the Tx Desc list */
  1201. DMATxDesc = DMATxDescTab + i;
  1202. /* Set Second Address Chained bit */
  1203. DMATxDesc->Status = 0;
  1204. DMATxDesc->CtrlOrBufSize = ETH_DMA_TX_DESC_TCH;
  1205. /* Set Buffer1 address pointer */
  1206. DMATxDesc->Buf1Addr = (uint32_t)(&TxBuff[i * BuffSize]);
  1207. /* Initialize the next descriptor with the Next Desciptor Polling Enable */
  1208. if (i < (TxBuffCount - 1))
  1209. {
  1210. /* Set next descriptor address register with next descriptor base address */
  1211. DMATxDesc->Buf2OrNextDescAddr = (uint32_t)(DMATxDescTab + i + 1);
  1212. }
  1213. else
  1214. {
  1215. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  1216. DMATxDesc->Buf2OrNextDescAddr = (uint32_t)DMATxDescTab;
  1217. }
  1218. }
  1219. /* Set Transmit Desciptor List Address Register */
  1220. ETH->DMATXDLADDR = (uint32_t)DMATxDescTab;
  1221. }
  1222. /**
  1223. * @brief Initializes the DMA Tx descriptors in ring mode.
  1224. * @param DMATxDescTab Pointer on the first Tx desc list
  1225. * @param TxBuff1 Pointer on the first TxBuffer1 list
  1226. * @param TxBuff2 Pointer on the first TxBuffer2 list
  1227. * @param BuffSize Buffer size of each descriptor
  1228. * @param TxBuffCount Number of the used Tx desc in the list
  1229. * Note: see decriptor skip length defined in ETH_DMA_InitStruct
  1230. * for the number of Words to skip between two unchained descriptors.
  1231. */
  1232. void ETH_ConfigDmaTxDescInRingMode(ETH_DMADescType* DMATxDescTab,
  1233. uint8_t* TxBuff1,
  1234. uint8_t* TxBuff2,
  1235. uint32_t BuffSize,
  1236. uint32_t TxBuffCount)
  1237. {
  1238. uint32_t i = 0;
  1239. ETH_DMADescType* DMATxDesc;
  1240. uint32_t dsl = (ETH->DMABUSMOD & ETH_DMABUSMOD_DSL) >> 2;
  1241. /* Set the DMATxDescToSet pointer with the first one of the DMATxDescTab list */
  1242. DMATxDescToSet = DMATxDescTab;
  1243. /* Fill each DMATxDesc descriptor with the right values */
  1244. for (i = 0; i < TxBuffCount; i++)
  1245. {
  1246. /* Get the pointer on the ith member of the Tx Desc list */
  1247. // DMATxDesc = DMATxDescTab + i;
  1248. DMATxDesc = (ETH_DMADescType*)((uint32_t)DMATxDescTab + i * (16 + 4 * dsl));
  1249. /* Set Buffer1 address pointer */
  1250. DMATxDesc->Buf1Addr = (uint32_t)(&TxBuff1[i * BuffSize]);
  1251. /* Set Buffer2 address pointer */
  1252. DMATxDesc->Buf2OrNextDescAddr = (uint32_t)(&TxBuff2[i * BuffSize]);
  1253. /* Set Transmit End of Ring bit for last descriptor: The DMA returns to the base
  1254. address of the list, creating a Desciptor Ring */
  1255. if (i == (TxBuffCount - 1))
  1256. {
  1257. /* Set Transmit End of Ring bit */
  1258. DMATxDesc->CtrlOrBufSize = ETH_DMA_TX_DESC_TER;
  1259. }
  1260. }
  1261. /* Set Transmit Desciptor List Address Register */
  1262. ETH->DMATXDLADDR = (uint32_t)DMATxDescTab;
  1263. }
  1264. /**
  1265. * @brief Checks whether the specified ETHERNET DMA Tx Desc flag is set or not.
  1266. * @param DMATxDesc pointer on a DMA Tx descriptor
  1267. * @param ETH_DMATxDescFlag specifies the flag to check.
  1268. * This parameter can be one of the following values:
  1269. * @arg ETH_DMA_TX_DESC_OWN OWN bit: descriptor is owned by DMA engine
  1270. * @arg ETH_DMA_TX_DESC_IC Interrupt on completetion
  1271. * @arg ETH_DMA_TX_DESC_LS Last Segment
  1272. * @arg ETH_DMA_TX_DESC_FS First Segment
  1273. * @arg ETH_DMA_TX_DESC_DC Disable CRC
  1274. * @arg ETH_DMA_TX_DESC_DP Disable Pad
  1275. * @arg ETH_DMA_TX_DESC_TTSE Transmit Time Stamp Enable
  1276. * @arg ETH_DMA_TX_DESC_TER Transmit End of Ring
  1277. * @arg ETH_DMA_TX_DESC_TCH Second Address Chained
  1278. * @arg ETH_DMA_TX_DESC_TTSS Tx Time Stamp Status
  1279. * @arg ETH_DMA_TX_DESC_IHE IP Header Error
  1280. * @arg ETH_DMA_TX_DESC_ES Error summary
  1281. * @arg ETH_DMA_TX_DESC_JT Jabber Timeout
  1282. * @arg ETH_DMA_TX_DESC_FF Frame Flushed: DMA/MTL flushed the frame due to SW flush
  1283. * @arg ETH_DMA_TX_DESC_PCE Payload Checksum Error
  1284. * @arg ETH_DMA_TX_DESC_LOC Loss of Carrier: carrier lost during tramsmission
  1285. * @arg ETH_DMA_TX_DESC_NC No Carrier: no carrier signal from the tranceiver
  1286. * @arg ETH_DMA_TX_DESC_LC Late Collision: transmission aborted due to collision
  1287. * @arg ETH_DMA_TX_DESC_EC Excessive Collision: transmission aborted after 16 collisions
  1288. * @arg ETH_DMA_TX_DESC_VF VLAN Frame
  1289. * @arg ETH_DMA_TX_DESC_CC Collision Count
  1290. * @arg ETH_DMA_TX_DESC_ED Excessive Deferral
  1291. * @arg ETH_DMA_TX_DESC_UF Underflow Error: late data arrival from the memory
  1292. * @arg ETH_DMA_TX_DESC_DB Deferred Bit
  1293. * @return The new state of ETH_DMATxDescFlag (SET or RESET).
  1294. */
  1295. FlagStatus ETH_GetDmaTxDescFlagStatus(ETH_DMADescType* DMATxDesc, uint32_t ETH_DMATxDescFlag)
  1296. {
  1297. FlagStatus bitstatus = RESET;
  1298. /* Check the parameters */
  1299. assert_param(IS_ETH_DMATXDESC_GET_FLAG(ETH_DMATxDescFlag));
  1300. if ((DMATxDesc->Status & ETH_DMATxDescFlag) != (uint32_t)RESET)
  1301. {
  1302. bitstatus = SET;
  1303. }
  1304. else
  1305. {
  1306. bitstatus = RESET;
  1307. }
  1308. return bitstatus;
  1309. }
  1310. /**
  1311. * @brief Returns the specified ETHERNET DMA Tx Desc collision count.
  1312. * @param DMATxDesc pointer on a DMA Tx descriptor
  1313. * @return The Transmit descriptor collision counter value.
  1314. */
  1315. uint32_t ETH_GetDmaTxDescCollisionCount(ETH_DMADescType* DMATxDesc)
  1316. {
  1317. /* Return the Receive descriptor frame length */
  1318. return ((DMATxDesc->Status & ETH_DMA_TX_DESC_CC) >> ETH_DMA_TX_DESC_COLLISION_COUNTER_SHIFT);
  1319. }
  1320. /**
  1321. * @brief Set the specified DMA Tx Desc Own bit.
  1322. * @param DMATxDesc Pointer on a Tx desc
  1323. */
  1324. void ETH_SetDmaTxDescOwn(ETH_DMADescType* DMATxDesc)
  1325. {
  1326. /* Set the DMA Tx Desc Own bit */
  1327. DMATxDesc->Status |= ETH_DMA_TX_DESC_OWN;
  1328. }
  1329. /**
  1330. * @brief Enables or disables the specified DMA Tx Desc Transmit interrupt.
  1331. * @param DMATxDesc Pointer on a Tx desc
  1332. * @param Cmd new state of the DMA Tx Desc transmit interrupt.
  1333. * This parameter can be: ENABLE or DISABLE.
  1334. */
  1335. void ETH_EnableDmaTxDescTransmitInt(ETH_DMADescType* DMATxDesc, FunctionalState Cmd)
  1336. {
  1337. /* Check the parameters */
  1338. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1339. if (Cmd != DISABLE)
  1340. {
  1341. /* Enable the DMA Tx Desc Transmit interrupt */
  1342. DMATxDesc->CtrlOrBufSize |= ETH_DMA_TX_DESC_IC;
  1343. }
  1344. else
  1345. {
  1346. /* Disable the DMA Tx Desc Transmit interrupt */
  1347. DMATxDesc->CtrlOrBufSize &= (~(uint32_t)ETH_DMA_TX_DESC_IC);
  1348. }
  1349. }
  1350. /**
  1351. * @brief Set the specified DMA Tx Desc frame segment.
  1352. * @param DMATxDesc Pointer on a Tx desc
  1353. * @param DMATxDesc_FrameSegment specifies is the actual Tx desc contain last or first segment.
  1354. * This parameter can be one of the following values:
  1355. * @arg ETH_DMA_TX_DESC_LAST_SEGMENT actual Tx desc contain last segment
  1356. * @arg ETH_DMA_TX_DESC_FIRST_SEGMENT actual Tx desc contain first segment
  1357. */
  1358. void ETH_ConfigDmaTxDescFrameSegment(ETH_DMADescType* DMATxDesc, uint32_t DMATxDesc_FrameSegment)
  1359. {
  1360. /* Check the parameters */
  1361. assert_param(IS_ETH_DMA_TX_DESC_SEGMENT(DMATxDesc_FrameSegment));
  1362. /* Selects the DMA Tx Desc Frame segment */
  1363. DMATxDesc->CtrlOrBufSize |= DMATxDesc_FrameSegment;
  1364. }
  1365. /**
  1366. * @brief Selects the specified ETHERNET DMA Tx Desc Checksum Insertion.
  1367. * @param DMATxDesc pointer on a DMA Tx descriptor
  1368. * @param DMATxDesc_Checksum specifies is the DMA Tx desc checksum insertion.
  1369. * This parameter can be one of the following values:
  1370. * @arg ETH_DMA_TX_DESC_CHECKSUM_BYPASS Checksum bypass
  1371. * @arg ETH_DMA_TX_DESC_CHECKSUM_IPV4_HEADER IPv4 header checksum
  1372. * @arg ETH_DMA_TX_DESC_CHECKSUM_TCPUDPICMP_SEGMENT TCP/UDP/ICMP checksum. Pseudo header checksum is assumed to be
  1373. * present
  1374. * @arg ETH_DMA_TX_DESC_CHECKSUM_TCPUDPICMP_FULL TCP/UDP/ICMP checksum fully in hardware including pseudo header
  1375. */
  1376. void ETH_ConfigDmaTxDescChecksumInsertion(ETH_DMADescType* DMATxDesc, uint32_t DMATxDesc_Checksum)
  1377. {
  1378. /* Check the parameters */
  1379. assert_param(IS_ETH_DMA_TX_DESC_CHECKSUM(DMATxDesc_Checksum));
  1380. /* Set the selected DMA Tx desc checksum insertion control */
  1381. DMATxDesc->CtrlOrBufSize |= DMATxDesc_Checksum;
  1382. }
  1383. /**
  1384. * @brief Enables or disables the DMA Tx Desc CRC.
  1385. * @param DMATxDesc pointer on a DMA Tx descriptor
  1386. * @param Cmd new state of the specified DMA Tx Desc CRC.
  1387. * This parameter can be: ENABLE or DISABLE.
  1388. */
  1389. void ETH_EnableDmaTxDescCrc(ETH_DMADescType* DMATxDesc, FunctionalState Cmd)
  1390. {
  1391. /* Check the parameters */
  1392. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1393. if (Cmd != DISABLE)
  1394. {
  1395. /* Enable the selected DMA Tx Desc CRC */
  1396. DMATxDesc->CtrlOrBufSize &= (~(uint32_t)ETH_DMA_TX_DESC_DC);
  1397. }
  1398. else
  1399. {
  1400. /* Disable the selected DMA Tx Desc CRC */
  1401. DMATxDesc->CtrlOrBufSize |= ETH_DMA_TX_DESC_DC;
  1402. }
  1403. }
  1404. /**
  1405. * @brief Enables or disables the DMA Tx Desc end of ring.
  1406. * @param DMATxDesc pointer on a DMA Tx descriptor
  1407. * @param Cmd new state of the specified DMA Tx Desc end of ring.
  1408. * This parameter can be: ENABLE or DISABLE.
  1409. */
  1410. void ETH_EnableDmaTxDescEndOfRing(ETH_DMADescType* DMATxDesc, FunctionalState Cmd)
  1411. {
  1412. /* Check the parameters */
  1413. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1414. if (Cmd != DISABLE)
  1415. {
  1416. /* Enable the selected DMA Tx Desc end of ring */
  1417. DMATxDesc->CtrlOrBufSize |= ETH_DMA_TX_DESC_TER;
  1418. }
  1419. else
  1420. {
  1421. /* Disable the selected DMA Tx Desc end of ring */
  1422. DMATxDesc->CtrlOrBufSize &= (~(uint32_t)ETH_DMA_TX_DESC_TER);
  1423. }
  1424. }
  1425. /**
  1426. * @brief Enables or disables the DMA Tx Desc second address chained.
  1427. * @param DMATxDesc pointer on a DMA Tx descriptor
  1428. * @param Cmd new state of the specified DMA Tx Desc second address chained.
  1429. * This parameter can be: ENABLE or DISABLE.
  1430. */
  1431. void ETH_EnableDmaTxDescSecondAddrChained(ETH_DMADescType* DMATxDesc, FunctionalState Cmd)
  1432. {
  1433. /* Check the parameters */
  1434. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1435. if (Cmd != DISABLE)
  1436. {
  1437. /* Enable the selected DMA Tx Desc second address chained */
  1438. DMATxDesc->CtrlOrBufSize |= ETH_DMA_TX_DESC_TCH;
  1439. }
  1440. else
  1441. {
  1442. /* Disable the selected DMA Tx Desc second address chained */
  1443. DMATxDesc->CtrlOrBufSize &= (~(uint32_t)ETH_DMA_TX_DESC_TCH);
  1444. }
  1445. }
  1446. /**
  1447. * @brief Enables or disables the DMA Tx Desc padding for frame shorter than 64 bytes.
  1448. * @param DMATxDesc pointer on a DMA Tx descriptor
  1449. * @param Cmd new state of the specified DMA Tx Desc padding for frame shorter than 64 bytes.
  1450. * This parameter can be: ENABLE or DISABLE.
  1451. */
  1452. void ETH_EnableDmaTxDescShortFramePadding(ETH_DMADescType* DMATxDesc, FunctionalState Cmd)
  1453. {
  1454. /* Check the parameters */
  1455. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1456. if (Cmd != DISABLE)
  1457. {
  1458. /* Enable the selected DMA Tx Desc padding for frame shorter than 64 bytes */
  1459. DMATxDesc->CtrlOrBufSize &= (~(uint32_t)ETH_DMA_TX_DESC_DP);
  1460. }
  1461. else
  1462. {
  1463. /* Disable the selected DMA Tx Desc padding for frame shorter than 64 bytes*/
  1464. DMATxDesc->CtrlOrBufSize |= ETH_DMA_TX_DESC_DP;
  1465. }
  1466. }
  1467. /**
  1468. * @brief Enables or disables the DMA Tx Desc time stamp.
  1469. * @param DMATxDesc pointer on a DMA Tx descriptor
  1470. * @param Cmd new state of the specified DMA Tx Desc time stamp.
  1471. * This parameter can be: ENABLE or DISABLE.
  1472. */
  1473. void ETH_EnableDmaTxDescTimeStamp(ETH_DMADescType* DMATxDesc, FunctionalState Cmd)
  1474. {
  1475. /* Check the parameters */
  1476. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1477. if (Cmd != DISABLE)
  1478. {
  1479. /* Enable the selected DMA Tx Desc time stamp */
  1480. DMATxDesc->CtrlOrBufSize |= ETH_DMA_TX_DESC_TTSE;
  1481. }
  1482. else
  1483. {
  1484. /* Disable the selected DMA Tx Desc time stamp */
  1485. DMATxDesc->CtrlOrBufSize &= (~(uint32_t)ETH_DMA_TX_DESC_TTSE);
  1486. }
  1487. }
  1488. /**
  1489. * @brief Configures the specified DMA Tx Desc buffer1 and buffer2 sizes.
  1490. * @param DMATxDesc Pointer on a Tx desc
  1491. * @param BufferSize1 specifies the Tx desc buffer1 size.
  1492. * @param BufferSize2 specifies the Tx desc buffer2 size (put "0" if not used).
  1493. */
  1494. void ETH_ConfigDmaTxDescBufSize(ETH_DMADescType* DMATxDesc, uint32_t BufferSize1, uint32_t BufferSize2)
  1495. {
  1496. /* Check the parameters */
  1497. assert_param(IS_ETH_DMA_TX_DESC_BUFFER_SIZE(BufferSize1));
  1498. assert_param(IS_ETH_DMA_TX_DESC_BUFFER_SIZE(BufferSize2));
  1499. /* Set the DMA Tx Desc buffer1 and buffer2 sizes values */
  1500. DMATxDesc->CtrlOrBufSize |= (BufferSize1 | (BufferSize2 << ETH_DMA_TX_DESC_BUF2_SIZE_SHIFT));
  1501. }
  1502. /**
  1503. * @brief Initializes the DMA Rx descriptors in chain mode.
  1504. * @param DMARxDescTab Pointer on the first Rx desc list
  1505. * @param RxBuff Pointer on the first RxBuffer list
  1506. * @param BuffSize the buffer size of each RxBuffer
  1507. * @param RxBuffCount Number of the used Rx desc in the list
  1508. */
  1509. void ETH_ConfigDmaRxDescInChainMode(ETH_DMADescType* DMARxDescTab,
  1510. uint8_t* RxBuff,
  1511. uint32_t BuffSize,
  1512. uint32_t RxBuffCount)
  1513. {
  1514. uint32_t i = 0;
  1515. ETH_DMADescType* DMARxDesc;
  1516. /* Set the DMARxDescToGet pointer with the first one of the DMARxDescTab list */
  1517. DMARxDescToGet = DMARxDescTab;
  1518. /* Fill each DMARxDesc descriptor with the right values */
  1519. for (i = 0; i < RxBuffCount; i++)
  1520. {
  1521. /* Get the pointer on the ith member of the Rx Desc list */
  1522. DMARxDesc = DMARxDescTab + i;
  1523. /* Set Own bit of the Rx descriptor Status */
  1524. DMARxDesc->Status = ETH_DMA_RX_DESC_OWN;
  1525. /* Set Buffer1 size and Second Address Chained bit */
  1526. DMARxDesc->CtrlOrBufSize = ETH_DMA_RX_DESC_RCH | (uint32_t)(BuffSize & 0x1FFFUL);
  1527. /* Set Buffer1 address pointer */
  1528. DMARxDesc->Buf1Addr = (uint32_t)(&RxBuff[i * BuffSize]);
  1529. /* Initialize the next descriptor with the Next Desciptor Polling Enable */
  1530. if (i < (RxBuffCount - 1))
  1531. {
  1532. /* Set next descriptor address register with next descriptor base address */
  1533. DMARxDesc->Buf2OrNextDescAddr = (uint32_t)(DMARxDescTab + i + 1);
  1534. }
  1535. else
  1536. {
  1537. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  1538. DMARxDesc->Buf2OrNextDescAddr = (uint32_t)(DMARxDescTab);
  1539. }
  1540. }
  1541. /* Set Receive Desciptor List Address Register */
  1542. ETH->DMARXDLADDR = (uint32_t)DMARxDescTab;
  1543. }
  1544. /**
  1545. * @brief Initializes the DMA Rx descriptors in ring mode.
  1546. * @param DMARxDescTab Pointer on the first Rx desc list
  1547. * @param RxBuff1 Pointer on the first RxBuffer1 list
  1548. * @param RxBuff2 Pointer on the first RxBuffer2 list
  1549. * @param BuffSize the buffer size of each RxBuffer
  1550. * @param RxBuffCount Number of the used Rx desc in the list
  1551. * Note: see decriptor skip length defined in ETH_DMA_InitStruct
  1552. * for the number of Words to skip between two unchained descriptors.
  1553. */
  1554. void ETH_ConfigDmaRxDescInRingMode(ETH_DMADescType* DMARxDescTab,
  1555. uint8_t* RxBuff1,
  1556. uint8_t* RxBuff2,
  1557. uint32_t BuffSize,
  1558. uint32_t RxBuffCount)
  1559. {
  1560. uint32_t i = 0;
  1561. ETH_DMADescType* DMARxDesc;
  1562. uint32_t dsl = (ETH->DMABUSMOD & ETH_DMABUSMOD_DSL) >> 2;
  1563. /* Set the DMARxDescToGet pointer with the first one of the DMARxDescTab list */
  1564. DMARxDescToGet = DMARxDescTab;
  1565. /* Fill each DMARxDesc descriptor with the right values */
  1566. for (i = 0; i < RxBuffCount; i++)
  1567. {
  1568. /* Get the pointer on the ith member of the Rx Desc list */
  1569. // DMARxDesc = DMARxDescTab + i;
  1570. DMARxDesc = (ETH_DMADescType*)((uint32_t)DMARxDescTab + i * (16 + 4 * dsl));
  1571. /* Set Own bit of the Rx descriptor Status */
  1572. DMARxDesc->Status = ETH_DMA_RX_DESC_OWN;
  1573. /* Set Buffer1 size */
  1574. DMARxDesc->CtrlOrBufSize = BuffSize;
  1575. /* Set Buffer1 address pointer */
  1576. DMARxDesc->Buf1Addr = (uint32_t)(&RxBuff1[i * BuffSize]);
  1577. /* Set Buffer2 address pointer */
  1578. DMARxDesc->Buf2OrNextDescAddr = (uint32_t)(&RxBuff2[i * BuffSize]);
  1579. /* Set Receive End of Ring bit for last descriptor: The DMA returns to the base
  1580. address of the list, creating a Desciptor Ring */
  1581. if (i == (RxBuffCount - 1))
  1582. {
  1583. /* Set Receive End of Ring bit */
  1584. DMARxDesc->CtrlOrBufSize |= ETH_DMA_RX_DESC_RER;
  1585. }
  1586. }
  1587. /* Set Receive Desciptor List Address Register */
  1588. ETH->DMARXDLADDR = (uint32_t)DMARxDescTab;
  1589. }
  1590. /**
  1591. * @brief Checks whether the specified ETHERNET Rx Desc flag is set or not.
  1592. * @param DMARxDesc pointer on a DMA Rx descriptor
  1593. * @param ETH_DMARxDescFlag specifies the flag to check.
  1594. * This parameter can be one of the following values:
  1595. * @arg ETH_DMA_RX_DESC_OWN OWN bit: descriptor is owned by DMA engine
  1596. * @arg ETH_DMA_RX_DESC_AFM DA Filter Fail for the rx frame
  1597. * @arg ETH_DMA_RX_DESC_ES Error summary
  1598. * @arg ETH_DMA_RX_DESC_DE Desciptor error: no more descriptors for receive frame
  1599. * @arg ETH_DMA_RX_DESC_SAF SA Filter Fail for the received frame
  1600. * @arg ETH_DMA_RX_DESC_LE Frame size not matching with length field
  1601. * @arg ETH_DMA_RX_DESC_OE Overflow Error: Frame was damaged due to buffer overflow
  1602. * @arg ETH_DMA_RX_DESC_VLAN VLAN Tag: received frame is a VLAN frame
  1603. * @arg ETH_DMA_RX_DESC_FS First descriptor of the frame
  1604. * @arg ETH_DMA_RX_DESC_LS Last descriptor of the frame
  1605. * @arg ETH_DMA_RX_DESC_IPV4HCE IPC Checksum Error/Giant Frame: Rx Ipv4 header checksum error
  1606. * @arg ETH_DMA_RX_DESC_LC Late collision occurred during reception
  1607. * @arg ETH_DMA_RX_DESC_FT Frame type - Ethernet, otherwise 802.3
  1608. * @arg ETH_DMA_RX_DESC_RWT Receive Watchdog Timeout: watchdog timer expired during reception
  1609. * @arg ETH_DMA_RX_DESC_RE Receive error: error reported by MII interface
  1610. * @arg ETH_DMA_RX_DESC_DE Dribble bit error: frame contains non int multiple of 8 bits
  1611. * @arg ETH_DMA_RX_DESC_CE CRC error
  1612. * @arg ETH_DMA_RX_DESC_RMAPCE Rx MAC Address/Payload Checksum Error: Rx MAC address matched/ Rx Payload Checksum
  1613. * Error
  1614. * @return The new state of ETH_DMARxDescFlag (SET or RESET).
  1615. */
  1616. FlagStatus ETH_GetDmaRxDescFlagStatus(ETH_DMADescType* DMARxDesc, uint32_t ETH_DMARxDescFlag)
  1617. {
  1618. FlagStatus bitstatus = RESET;
  1619. /* Check the parameters */
  1620. assert_param(IS_ETH_DMA_RX_DESC_GET_FLAG(ETH_DMARxDescFlag));
  1621. if ((DMARxDesc->Status & ETH_DMARxDescFlag) != (uint32_t)RESET)
  1622. {
  1623. bitstatus = SET;
  1624. }
  1625. else
  1626. {
  1627. bitstatus = RESET;
  1628. }
  1629. return bitstatus;
  1630. }
  1631. /**
  1632. * @brief Set the specified DMA Rx Desc Own bit.
  1633. * @param DMARxDesc Pointer on a Rx desc
  1634. */
  1635. void ETH_SetDmaRxDescOwn(ETH_DMADescType* DMARxDesc)
  1636. {
  1637. /* Set the DMA Rx Desc Own bit */
  1638. DMARxDesc->Status |= ETH_DMA_RX_DESC_OWN;
  1639. }
  1640. /**
  1641. * @brief Returns the specified DMA Rx Desc frame length.
  1642. * @param DMARxDesc pointer on a DMA Rx descriptor
  1643. * @return The Rx descriptor received frame length.
  1644. */
  1645. uint32_t ETH_GetDmaRxDescFrameLen(__IO ETH_DMADescType* DMARxDesc)
  1646. {
  1647. /* Return the Receive descriptor frame length */
  1648. return ((DMARxDesc->Status & ETH_DMA_RX_DESC_FL) >> ETH_DMA_RX_DESC_FRAME_LEN_SHIFT);
  1649. }
  1650. /**
  1651. * @brief Enables or disables the specified DMA Rx Desc receive interrupt.
  1652. * @param DMARxDesc Pointer on a Rx desc
  1653. * @param Cmd new state of the specified DMA Rx Desc interrupt.
  1654. * This parameter can be: ENABLE or DISABLE.
  1655. */
  1656. void ETH_EnableDmaRxDescReceiveInt(ETH_DMADescType* DMARxDesc, FunctionalState Cmd)
  1657. {
  1658. /* Check the parameters */
  1659. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1660. if (Cmd != DISABLE)
  1661. {
  1662. /* Enable the DMA Rx Desc receive interrupt */
  1663. DMARxDesc->CtrlOrBufSize &= (~(uint32_t)ETH_DMA_RX_DESC_DIC);
  1664. }
  1665. else
  1666. {
  1667. /* Disable the DMA Rx Desc receive interrupt */
  1668. DMARxDesc->CtrlOrBufSize |= ETH_DMA_RX_DESC_DIC;
  1669. }
  1670. }
  1671. /**
  1672. * @brief Enables or disables the DMA Rx Desc end of ring.
  1673. * @param DMARxDesc pointer on a DMA Rx descriptor
  1674. * @param Cmd new state of the specified DMA Rx Desc end of ring.
  1675. * This parameter can be: ENABLE or DISABLE.
  1676. */
  1677. void ETH_EnableDmaRxDescEndOfRing(ETH_DMADescType* DMARxDesc, FunctionalState Cmd)
  1678. {
  1679. /* Check the parameters */
  1680. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1681. if (Cmd != DISABLE)
  1682. {
  1683. /* Enable the selected DMA Rx Desc end of ring */
  1684. DMARxDesc->CtrlOrBufSize |= ETH_DMA_RX_DESC_RER;
  1685. }
  1686. else
  1687. {
  1688. /* Disable the selected DMA Rx Desc end of ring */
  1689. DMARxDesc->CtrlOrBufSize &= (~(uint32_t)ETH_DMA_RX_DESC_RER);
  1690. }
  1691. }
  1692. /**
  1693. * @brief Enables or disables the DMA Rx Desc second address chained.
  1694. * @param DMARxDesc pointer on a DMA Rx descriptor
  1695. * @param Cmd new state of the specified DMA Rx Desc second address chained.
  1696. * This parameter can be: ENABLE or DISABLE.
  1697. */
  1698. void ETH_EnableDmaRxDescSecondAddrChained(ETH_DMADescType* DMARxDesc, FunctionalState Cmd)
  1699. {
  1700. /* Check the parameters */
  1701. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1702. if (Cmd != DISABLE)
  1703. {
  1704. /* Enable the selected DMA Rx Desc second address chained */
  1705. DMARxDesc->CtrlOrBufSize |= ETH_DMA_RX_DESC_RCH;
  1706. }
  1707. else
  1708. {
  1709. /* Disable the selected DMA Rx Desc second address chained */
  1710. DMARxDesc->CtrlOrBufSize &= (~(uint32_t)ETH_DMA_RX_DESC_RCH);
  1711. }
  1712. }
  1713. /**
  1714. * @brief Returns the specified ETHERNET DMA Rx Desc buffer size.
  1715. * @param DMARxDesc pointer on a DMA Rx descriptor
  1716. * @param DMARxDesc_Buffer specifies the DMA Rx Desc buffer.
  1717. * This parameter can be any one of the following values:
  1718. * @arg ETH_DMA_RX_DESC_BUFFER1 DMA Rx Desc Buffer1
  1719. * @arg ETH_DMA_RX_DESC_BUFFER2 DMA Rx Desc Buffer2
  1720. * @return The Receive descriptor frame length.
  1721. */
  1722. uint32_t ETH_GetDmaRxDescBufSize(ETH_DMADescType* DMARxDesc, uint32_t DMARxDesc_Buffer)
  1723. {
  1724. /* Check the parameters */
  1725. assert_param(IS_ETH_DMA_RXDESC_BUFFER(DMARxDesc_Buffer));
  1726. if (DMARxDesc_Buffer != ETH_DMA_RX_DESC_BUFFER1)
  1727. {
  1728. /* Return the DMA Rx Desc buffer2 size */
  1729. return ((DMARxDesc->CtrlOrBufSize & ETH_DMA_RX_DESC_RBS2) >> ETH_DMA_RX_DESC_BUF2_SIZE_SHIFT);
  1730. }
  1731. else
  1732. {
  1733. /* Return the DMA Rx Desc buffer1 size */
  1734. return (DMARxDesc->CtrlOrBufSize & ETH_DMA_RX_DESC_RBS1);
  1735. }
  1736. }
  1737. /*--------------------------------- DMA ------------------------------------*/
  1738. /**
  1739. * @brief Resets all MAC subsystem internal registers and logic.
  1740. */
  1741. void ETH_SoftwareReset(void)
  1742. {
  1743. /* Set the SWR bit: resets all MAC subsystem internal registers and logic */
  1744. /* After reset all the registers holds their respective reset values */
  1745. ETH->DMABUSMOD |= ETH_DMABUSMOD_SWR;
  1746. }
  1747. /**
  1748. * @brief Checks whether the ETHERNET software reset bit is set or not.
  1749. * @return The new state of DMA Bus Mode register STS bit (SET or RESET).
  1750. */
  1751. FlagStatus ETH_GetSoftwareResetStatus(void)
  1752. {
  1753. FlagStatus bitstatus = RESET;
  1754. if ((ETH->DMABUSMOD & ETH_DMABUSMOD_SWR) != (uint32_t)RESET)
  1755. {
  1756. bitstatus = SET;
  1757. }
  1758. else
  1759. {
  1760. bitstatus = RESET;
  1761. }
  1762. return bitstatus;
  1763. }
  1764. /**
  1765. * @brief Checks whether the specified ETHERNET DMA flag is set or not.
  1766. * @param ETH_DMA_FLAG specifies the flag to check.
  1767. * This parameter can be one of the following values:
  1768. * @arg ETH_DMA_FLAG_TST Time-stamp trigger flag
  1769. * @arg ETH_DMA_FLAG_PMT PMT flag
  1770. * @arg ETH_DMA_FLAG_MMC MMC flag
  1771. * @arg ETH_DMA_FLAG_DATA_TRANSFER_ERROR Error bits 0-data buffer, 1-desc. access
  1772. * @arg ETH_DMA_FLAG_READ_WRITE_ERROR Error bits 0-write trnsf, 1-read transfr
  1773. * @arg ETH_DMA_FLAG_ACCESS_ERROR Error bits 0-Rx DMA, 1-Tx DMA
  1774. * @arg ETH_DMA_FLAG_NIS Normal interrupt summary flag
  1775. * @arg ETH_DMA_FLAG_AIS Abnormal interrupt summary flag
  1776. * @arg ETH_DMA_FLAG_EARLY_RX Early receive flag
  1777. * @arg ETH_DMA_FLAG_FATAL_BUS_ERROR Fatal bus error flag
  1778. * @arg ETH_DMA_FLAG_EARLY_TX Early transmit flag
  1779. * @arg ETH_DMA_FLAG_RX_WDG_TIMEOUT Receive watchdog timeout flag
  1780. * @arg ETH_DMA_FLAG_RX_PROC_STOP Receive process stopped flag
  1781. * @arg ETH_DMA_FLAG_RX_BUF_UA Receive buffer unavailable flag
  1782. * @arg ETH_DMA_FLAG_RX Receive flag
  1783. * @arg ETH_DMA_FLAG_TX_UNDERFLOW Underflow flag
  1784. * @arg ETH_DMA_FLAG_RX_OVERFLOW Overflow flag
  1785. * @arg ETH_DMA_FLAG_TX_JABBER_TIMEOUT Transmit jabber timeout flag
  1786. * @arg ETH_DMA_FLAG_TX_BUF_UA Transmit buffer unavailable flag
  1787. * @arg ETH_DMA_FLAG_TX_PROC_STOP Transmit process stopped flag
  1788. * @arg ETH_DMA_FLAG_TX Transmit flag
  1789. * @return The new state of ETH_DMA_FLAG (SET or RESET).
  1790. */
  1791. FlagStatus ETH_GetDmaFlagStatus(uint32_t ETH_DMA_FLAG)
  1792. {
  1793. FlagStatus bitstatus = RESET;
  1794. /* Check the parameters */
  1795. assert_param(IS_ETH_DMA_GET_INT(ETH_DMA_FLAG));
  1796. if ((ETH->DMASTS & ETH_DMA_FLAG) != (uint32_t)RESET)
  1797. {
  1798. bitstatus = SET;
  1799. }
  1800. else
  1801. {
  1802. bitstatus = RESET;
  1803. }
  1804. return bitstatus;
  1805. }
  1806. /**
  1807. * @brief Clears the ETHERNET's DMA pending flag.
  1808. * @param ETH_DMA_FLAG specifies the flag to clear.
  1809. * This parameter can be any combination of the following values:
  1810. * @arg ETH_DMA_FLAG_NIS Normal interrupt summary flag
  1811. * @arg ETH_DMA_FLAG_AIS Abnormal interrupt summary flag
  1812. * @arg ETH_DMA_FLAG_EARLY_RX Early receive flag
  1813. * @arg ETH_DMA_FLAG_FATAL_BUS_ERROR Fatal bus error flag
  1814. * @arg ETH_DMA_FLAG_ETI Early transmit flag
  1815. * @arg ETH_DMA_FLAG_RX_WDG_TIMEOUT Receive watchdog timeout flag
  1816. * @arg ETH_DMA_FLAG_RX_PROC_STOP Receive process stopped flag
  1817. * @arg ETH_DMA_FLAG_RX_BUF_UA Receive buffer unavailable flag
  1818. * @arg ETH_DMA_FLAG_RX Receive flag
  1819. * @arg ETH_DMA_FLAG_TX_UNDERFLOW Transmit Underflow flag
  1820. * @arg ETH_DMA_FLAG_RX_OVERFLOW Receive Overflow flag
  1821. * @arg ETH_DMA_FLAG_TX_JABBER_TIMEOUT Transmit jabber timeout flag
  1822. * @arg ETH_DMA_FLAG_TX_BUF_UA Transmit buffer unavailable flag
  1823. * @arg ETH_DMA_FLAG_TX_PROC_STOP Transmit process stopped flag
  1824. * @arg ETH_DMA_FLAG_TX Transmit flag
  1825. */
  1826. void ETH_ClrDmaFlag(uint32_t ETH_DMA_FLAG)
  1827. {
  1828. /* Check the parameters */
  1829. assert_param(IS_ETH_DMA_FLAG(ETH_DMA_FLAG));
  1830. /* Clear the selected ETHERNET DMA FLAG */
  1831. ETH->DMASTS = (uint32_t)ETH_DMA_FLAG;
  1832. }
  1833. /**
  1834. * @brief Checks whether the specified ETHERNET DMA interrupt has occured or not.
  1835. * @param ETH_DMA_IT specifies the interrupt source to check.
  1836. * This parameter can be one of the following values:
  1837. * @arg ETH_DMA_INT_TST Time-stamp trigger interrupt
  1838. * @arg ETH_DMA_INT_PMT PMT interrupt
  1839. * @arg ETH_DMA_INT_MMC MMC interrupt
  1840. * @arg ETH_DMA_INT_NIS Normal interrupt summary
  1841. * @arg ETH_DMA_INT_AIS Abnormal interrupt summary
  1842. * @arg ETH_DMA_INT_EARLY_RX Early receive interrupt
  1843. * @arg ETH_DMA_INT_FATAL_BUS_ERROR Fatal bus error interrupt
  1844. * @arg ETH_DMA_INT_EARLY_TX Early transmit interrupt
  1845. * @arg ETH_DMA_INT_RX_WDG_TIMEOUT Receive watchdog timeout interrupt
  1846. * @arg ETH_DMA_INT_RX_PROC_STOP Receive process stopped interrupt
  1847. * @arg ETH_DMA_INT_RX_BUF_UA Receive buffer unavailable interrupt
  1848. * @arg ETH_DMA_INT_RX Receive interrupt
  1849. * @arg ETH_DMA_INT_TX_UNDERFLOW Underflow interrupt
  1850. * @arg ETH_DMA_INT_RX_OVERFLOW Overflow interrupt
  1851. * @arg ETH_DMA_INT_TX_JABBER_TIMEOUT Transmit jabber timeout interrupt
  1852. * @arg ETH_DMA_INT_TX_BUF_UA Transmit buffer unavailable interrupt
  1853. * @arg ETH_DMA_INT_TX_PROC_STOP Transmit process stopped interrupt
  1854. * @arg ETH_DMA_INT_TX Transmit interrupt
  1855. * @return The new state of ETH_DMA_IT (SET or RESET).
  1856. */
  1857. INTStatus ETH_GetDmaIntStatus(uint32_t ETH_DMA_IT)
  1858. {
  1859. INTStatus bitstatus = RESET;
  1860. /* Check the parameters */
  1861. assert_param(IS_ETH_DMA_GET_INT(ETH_DMA_IT));
  1862. if ((ETH->DMASTS & ETH_DMA_IT) != (uint32_t)RESET)
  1863. {
  1864. bitstatus = SET;
  1865. }
  1866. else
  1867. {
  1868. bitstatus = RESET;
  1869. }
  1870. return bitstatus;
  1871. }
  1872. /**
  1873. * @brief Clears the ETHERNET's DMA IT pending bit.
  1874. * @param ETH_DMA_IT specifies the interrupt pending bit to clear.
  1875. * This parameter can be any combination of the following values:
  1876. * @arg ETH_DMA_INT_NIS Normal interrupt summary
  1877. * @arg ETH_DMA_INT_AIS Abnormal interrupt summary
  1878. * @arg ETH_DMA_INT_EARLY_RX Early receive interrupt
  1879. * @arg ETH_DMA_INT_FATAL_BUS_ERROR Fatal bus error interrupt
  1880. * @arg ETH_DMA_IT_ETI Early transmit interrupt
  1881. * @arg ETH_DMA_INT_RX_WDG_TIMEOUT Receive watchdog timeout interrupt
  1882. * @arg ETH_DMA_INT_RX_PROC_STOP Receive process stopped interrupt
  1883. * @arg ETH_DMA_INT_RX_BUF_UA Receive buffer unavailable interrupt
  1884. * @arg ETH_DMA_INT_RX Receive interrupt
  1885. * @arg ETH_DMA_INT_TX_UNDERFLOW Transmit Underflow interrupt
  1886. * @arg ETH_DMA_INT_RX_OVERFLOW Receive Overflow interrupt
  1887. * @arg ETH_DMA_INT_TX_JABBER_TIMEOUT Transmit jabber timeout interrupt
  1888. * @arg ETH_DMA_INT_TX_BUF_UA Transmit buffer unavailable interrupt
  1889. * @arg ETH_DMA_INT_TX_PROC_STOP Transmit process stopped interrupt
  1890. * @arg ETH_DMA_INT_TX Transmit interrupt
  1891. */
  1892. void ETH_ClrDmaIntPendingBit(uint32_t ETH_DMA_IT)
  1893. {
  1894. /* Check the parameters */
  1895. assert_param(IS_ETH_DMA_INT(ETH_DMA_IT));
  1896. /* Clear the selected ETHERNET DMA IT */
  1897. ETH->DMASTS = (uint32_t)ETH_DMA_IT;
  1898. }
  1899. /**
  1900. * @brief Returns the ETHERNET DMA Transmit Process State.
  1901. * @return The new ETHERNET DMA Transmit Process State:
  1902. * This can be one of the following values:
  1903. * - ETH_DMA_TX_PROC_STOPPED : Stopped - Reset or Stop Tx Command issued
  1904. * - ETH_DMA_TX_PROC_FETCHING : Running - fetching the Tx descriptor
  1905. * - ETH_DMA_TX_PROC_WAITING : Running - waiting for status
  1906. * - ETH_DMA_TX_PROC_READING : unning - reading the data from host memory
  1907. * - ETH_DMA_TX_PROC_SUSPENDED : Suspended - Tx Desciptor unavailabe
  1908. * - ETH_DMA_TX_PROC_CLOSING : Running - closing Rx descriptor
  1909. */
  1910. uint32_t ETH_GetTxProcState(void)
  1911. {
  1912. return ((uint32_t)(ETH->DMASTS & ETH_DMASTS_TI));
  1913. }
  1914. /**
  1915. * @brief Returns the ETHERNET DMA Receive Process State.
  1916. * @return The new ETHERNET DMA Receive Process State:
  1917. * This can be one of the following values:
  1918. * - ETH_DMA_RX_PROC_STOPPED : Stopped - Reset or Stop Rx Command issued
  1919. * - ETH_DMA_RX_PROC_FETCHING : Running - fetching the Rx descriptor
  1920. * - ETH_DMA_RX_PROC_WAITING : Running - waiting for packet
  1921. * - ETH_DMA_RX_PROC_SUSPENDED : Suspended - Rx Desciptor unavailable
  1922. * - ETH_DMA_RX_PROC_CLOSING : Running - closing descriptor
  1923. * - ETH_DMA_RX_PROC_QUEUING : Running - queuing the recieve frame into host memory
  1924. */
  1925. uint32_t ETH_GetRxProcState(void)
  1926. {
  1927. return ((uint32_t)(ETH->DMASTS & ETH_DMASTS_RI));
  1928. }
  1929. /**
  1930. * @brief Clears the ETHERNET transmit DATFIFO.
  1931. */
  1932. void ETH_FlushTxFifo(void)
  1933. {
  1934. /* Set the Flush Transmit DATFIFO bit */
  1935. ETH->DMAOPMOD |= ETH_DMAOPMOD_FTF;
  1936. }
  1937. /**
  1938. * @brief Checks whether the ETHERNET transmit DATFIFO bit is cleared or not.
  1939. * @return The new state of ETHERNET flush transmit DATFIFO bit (SET or RESET).
  1940. */
  1941. FlagStatus ETH_GetFlushTxFifoStatus(void)
  1942. {
  1943. FlagStatus bitstatus = RESET;
  1944. if ((ETH->DMAOPMOD & ETH_DMAOPMOD_FTF) != (uint32_t)RESET)
  1945. {
  1946. bitstatus = SET;
  1947. }
  1948. else
  1949. {
  1950. bitstatus = RESET;
  1951. }
  1952. return bitstatus;
  1953. }
  1954. /**
  1955. * @brief Enables or disables the DMA transmission.
  1956. * @param Cmd new state of the DMA transmission.
  1957. * This parameter can be: ENABLE or DISABLE.
  1958. */
  1959. void ETH_EnableDmaTx(FunctionalState Cmd)
  1960. {
  1961. /* Check the parameters */
  1962. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1963. if (Cmd != DISABLE)
  1964. {
  1965. /* Enable the DMA transmission */
  1966. ETH->DMAOPMOD |= ETH_DMAOPMOD_ST;
  1967. }
  1968. else
  1969. {
  1970. /* Disable the DMA transmission */
  1971. ETH->DMAOPMOD &= ~ETH_DMAOPMOD_ST;
  1972. }
  1973. }
  1974. /**
  1975. * @brief Enables or disables the DMA reception.
  1976. * @param Cmd new state of the DMA reception.
  1977. * This parameter can be: ENABLE or DISABLE.
  1978. */
  1979. void ETH_EnableDmaRx(FunctionalState Cmd)
  1980. {
  1981. /* Check the parameters */
  1982. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1983. if (Cmd != DISABLE)
  1984. {
  1985. /* Enable the DMA reception */
  1986. ETH->DMAOPMOD |= ETH_DMAOPMOD_SR;
  1987. }
  1988. else
  1989. {
  1990. /* Disable the DMA reception */
  1991. ETH->DMAOPMOD &= ~ETH_DMAOPMOD_SR;
  1992. }
  1993. }
  1994. /**
  1995. * @brief Enables or disables the specified ETHERNET DMA interrupts.
  1996. * @param ETH_DMA_IT specifies the ETHERNET DMA interrupt sources to be
  1997. * enabled or disabled.
  1998. * This parameter can be any combination of the following values:
  1999. * @arg ETH_DMA_INT_NIS Normal interrupt summary
  2000. * @arg ETH_DMA_INT_AIS Abnormal interrupt summary
  2001. * @arg ETH_DMA_INT_EARLY_RX Early receive interrupt
  2002. * @arg ETH_DMA_INT_FATAL_BUS_ERROR Fatal bus error interrupt
  2003. * @arg ETH_DMA_INT_EARLY_TX Early transmit interrupt
  2004. * @arg ETH_DMA_INT_RX_WDG_TIMEOUT Receive watchdog timeout interrupt
  2005. * @arg ETH_DMA_INT_RX_PROC_STOP Receive process stopped interrupt
  2006. * @arg ETH_DMA_INT_RX_BUF_UA Receive buffer unavailable interrupt
  2007. * @arg ETH_DMA_INT_RX Receive interrupt
  2008. * @arg ETH_DMA_INT_TX_UNDERFLOW Underflow interrupt
  2009. * @arg ETH_DMA_INT_RX_OVERFLOW Overflow interrupt
  2010. * @arg ETH_DMA_INT_TX_JABBER_TIMEOUT Transmit jabber timeout interrupt
  2011. * @arg ETH_DMA_INT_TX_BUF_UA Transmit buffer unavailable interrupt
  2012. * @arg ETH_DMA_INT_TX_PROC_STOP Transmit process stopped interrupt
  2013. * @arg ETH_DMA_INT_TX Transmit interrupt
  2014. * @param Cmd new state of the specified ETHERNET DMA interrupts.
  2015. * This parameter can be: ENABLE or DISABLE.
  2016. */
  2017. void ETH_EnableDmaInt(uint32_t ETH_DMA_IT, FunctionalState Cmd)
  2018. {
  2019. /* Check the parameters */
  2020. assert_param(IS_ETH_DMA_INT(ETH_DMA_IT));
  2021. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  2022. if (Cmd != DISABLE)
  2023. {
  2024. /* Enable the selected ETHERNET DMA interrupts */
  2025. ETH->DMAINTEN |= ETH_DMA_IT;
  2026. }
  2027. else
  2028. {
  2029. /* Disable the selected ETHERNET DMA interrupts */
  2030. ETH->DMAINTEN &= (~(uint32_t)ETH_DMA_IT);
  2031. }
  2032. }
  2033. /**
  2034. * @brief Checks whether the specified ETHERNET DMA overflow flag is set or not.
  2035. * @param ETH_DMA_Overflow specifies the DMA overflow flag to check.
  2036. * This parameter can be one of the following values:
  2037. * @arg ETH_DMA_OVERFLOW_RX_FIFO_COUNTER Overflow for DATFIFO Overflow Counter
  2038. * @arg ETH_DMA_OVERFLOW_MISSED_FRAME_COUNTER Overflow for Missed Frame Counter
  2039. * @return The new state of ETHERNET DMA overflow Flag (SET or RESET).
  2040. */
  2041. FlagStatus ETH_GetDmaOverflowStatus(uint32_t ETH_DMA_Overflow)
  2042. {
  2043. FlagStatus bitstatus = RESET;
  2044. /* Check the parameters */
  2045. assert_param(IS_ETH_DMA_GET_OVERFLOW(ETH_DMA_Overflow));
  2046. if ((ETH->DMAMFBOCNT & ETH_DMA_Overflow) != (uint32_t)RESET)
  2047. {
  2048. bitstatus = SET;
  2049. }
  2050. else
  2051. {
  2052. bitstatus = RESET;
  2053. }
  2054. return bitstatus;
  2055. }
  2056. /**
  2057. * @brief Get the ETHERNET DMA Rx Overflow Missed Frame Counter value.
  2058. * @return The value of Rx overflow Missed Frame Counter.
  2059. */
  2060. uint32_t ETH_GetRxOverflowMissedFrameCounter(void)
  2061. {
  2062. return ((uint32_t)((ETH->DMAMFBOCNT & ETH_DMAMFBOCNT_OVFFRMCNT) >> ETH_DMA_RX_OVERFLOW_MISSEDFRAMES_COUNTER_SHIFT));
  2063. }
  2064. /**
  2065. * @brief Get the ETHERNET DMA Buffer Unavailable Missed Frame Counter value.
  2066. * @return The value of Buffer unavailable Missed Frame Counter.
  2067. */
  2068. uint32_t ETH_GetBufUnavailableMissedFrameCounter(void)
  2069. {
  2070. return ((uint32_t)(ETH->DMAMFBOCNT) & ETH_DMAMFBOCNT_MISFRMCNT);
  2071. }
  2072. /**
  2073. * @brief Get the ETHERNET DMA DMACHTXDESC register value.
  2074. * @return The value of the current Tx desc start address.
  2075. */
  2076. uint32_t ETH_GetCurrentTxDescAddr(void)
  2077. {
  2078. return ((uint32_t)(ETH->DMACHTXDESC));
  2079. }
  2080. /**
  2081. * @brief Get the ETHERNET DMA DMACHRXDESC register value.
  2082. * @return The value of the current Rx desc start address.
  2083. */
  2084. uint32_t ETH_GetCurrentRxDescAddr(void)
  2085. {
  2086. return ((uint32_t)(ETH->DMACHRXDESC));
  2087. }
  2088. /**
  2089. * @brief Get the ETHERNET DMA DMACHTXBADDR register value.
  2090. * @return The value of the current Tx buffer address.
  2091. */
  2092. uint32_t ETH_GetCurrentTxBufAddr(void)
  2093. {
  2094. return ((uint32_t)(ETH->DMACHTXBADDR));
  2095. }
  2096. /**
  2097. * @brief Get the ETHERNET DMA DMACHRXBADDR register value.
  2098. * @return The value of the current Rx buffer address.
  2099. */
  2100. uint32_t ETH_GetCurrentRxBufAddr(void)
  2101. {
  2102. return ((uint32_t)(ETH->DMACHRXBADDR));
  2103. }
  2104. /**
  2105. * @brief Resumes the DMA Transmission by writing to the DmaTxPollDemand register
  2106. * (the data written could be anything). This forces the DMA to resume transmission.
  2107. */
  2108. void ETH_ResumeDmaTx(void)
  2109. {
  2110. ETH->DMATXPD = 0;
  2111. }
  2112. /**
  2113. * @brief Resumes the DMA Transmission by writing to the DmaRxPollDemand register
  2114. * (the data written could be anything). This forces the DMA to resume reception.
  2115. */
  2116. void ETH_ResumeDmaRx(void)
  2117. {
  2118. ETH->DMARXPD = 0;
  2119. }
  2120. /*--------------------------------- PMT ------------------------------------*/
  2121. /**
  2122. * @brief Reset Wakeup frame filter register pointer.
  2123. */
  2124. void ETH_ResetWakeUpFrameFilter(void)
  2125. {
  2126. /* Resets the Remote Wake-up Frame Filter register pointer to 0x0000 */
  2127. ETH->MACPMTCTRLSTS |= ETH_MACPMTCTRLSTS_RWKUPFLTRST;
  2128. }
  2129. /**
  2130. * @brief Populates the remote wakeup frame registers.
  2131. * @param Buffer Pointer on remote WakeUp Frame Filter Register buffer data (8 words).
  2132. */
  2133. void ETH_SetWakeUpFrameFilter(uint32_t* Buffer)
  2134. {
  2135. uint32_t i = 0;
  2136. /* Fill Remote Wake-up Frame Filter register with Buffer data */
  2137. for (i = 0; i < ETH_WAKEUP_REG_LEN; i++)
  2138. {
  2139. /* Write each time to the same register */
  2140. ETH->MACRMTWUFRMFLT = Buffer[i];
  2141. }
  2142. }
  2143. /**
  2144. * @brief Enables or disables any unicast packet filtered by the MAC address
  2145. * recognition to be a wake-up frame.
  2146. * @param Cmd new state of the MAC Global Unicast Wake-Up.
  2147. * This parameter can be: ENABLE or DISABLE.
  2148. */
  2149. void ETH_EnableGlobalUnicastWakeUp(FunctionalState Cmd)
  2150. {
  2151. /* Check the parameters */
  2152. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  2153. if (Cmd != DISABLE)
  2154. {
  2155. /* Enable the MAC Global Unicast Wake-Up */
  2156. ETH->MACPMTCTRLSTS |= ETH_MACPMTCTRLSTS_GLBLUCAST;
  2157. }
  2158. else
  2159. {
  2160. /* Disable the MAC Global Unicast Wake-Up */
  2161. ETH->MACPMTCTRLSTS &= ~ETH_MACPMTCTRLSTS_GLBLUCAST;
  2162. }
  2163. }
  2164. /**
  2165. * @brief Checks whether the specified ETHERNET PMT flag is set or not.
  2166. * @param ETH_PMT_FLAG specifies the flag to check.
  2167. * This parameter can be one of the following values:
  2168. * @arg ETH_PMT_FLAG_RWKUPFILTRST Wake-Up Frame Filter Register Poniter Reset
  2169. * @arg ETH_PMT_FLAG_RWKPRCVD Wake-Up Frame Received
  2170. * @arg ETH_PMT_FLAG_MGKPRCVD Magic Packet Received
  2171. * @return The new state of ETHERNET PMT Flag (SET or RESET).
  2172. */
  2173. FlagStatus ETH_GetPmtFlagStatus(uint32_t ETH_PMT_FLAG)
  2174. {
  2175. FlagStatus bitstatus = RESET;
  2176. /* Check the parameters */
  2177. assert_param(IS_ETH_PMT_GET_FLAG(ETH_PMT_FLAG));
  2178. if ((ETH->MACPMTCTRLSTS & ETH_PMT_FLAG) != (uint32_t)RESET)
  2179. {
  2180. bitstatus = SET;
  2181. }
  2182. else
  2183. {
  2184. bitstatus = RESET;
  2185. }
  2186. return bitstatus;
  2187. }
  2188. /**
  2189. * @brief Enables or disables the MAC Wake-Up Frame Detection.
  2190. * @param Cmd new state of the MAC Wake-Up Frame Detection.
  2191. * This parameter can be: ENABLE or DISABLE.
  2192. */
  2193. void ETH_EnableWakeUpFrameDetection(FunctionalState Cmd)
  2194. {
  2195. /* Check the parameters */
  2196. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  2197. if (Cmd != DISABLE)
  2198. {
  2199. /* Enable the MAC Wake-Up Frame Detection */
  2200. ETH->MACPMTCTRLSTS |= ETH_MACPMTCTRLSTS_RWKPKTEN;
  2201. }
  2202. else
  2203. {
  2204. /* Disable the MAC Wake-Up Frame Detection */
  2205. ETH->MACPMTCTRLSTS &= ~ETH_MACPMTCTRLSTS_RWKPKTEN;
  2206. }
  2207. }
  2208. /**
  2209. * @brief Enables or disables the MAC Magic Packet Detection.
  2210. * @param Cmd new state of the MAC Magic Packet Detection.
  2211. * This parameter can be: ENABLE or DISABLE.
  2212. */
  2213. void ETH_EnableMagicPacketDetection(FunctionalState Cmd)
  2214. {
  2215. /* Check the parameters */
  2216. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  2217. if (Cmd != DISABLE)
  2218. {
  2219. /* Enable the MAC Magic Packet Detection */
  2220. ETH->MACPMTCTRLSTS |= ETH_MACPMTCTRLSTS_MGKPKTEN;
  2221. }
  2222. else
  2223. {
  2224. /* Disable the MAC Magic Packet Detection */
  2225. ETH->MACPMTCTRLSTS &= ~ETH_MACPMTCTRLSTS_MGKPKTEN;
  2226. }
  2227. }
  2228. /**
  2229. * @brief Enables or disables the MAC Power Down.
  2230. * @param Cmd new state of the MAC Power Down.
  2231. * This parameter can be: ENABLE or DISABLE.
  2232. */
  2233. void ETH_EnablePowerDown(FunctionalState Cmd)
  2234. {
  2235. /* Check the parameters */
  2236. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  2237. if (Cmd != DISABLE)
  2238. {
  2239. /* Enable the MAC Power Down */
  2240. /* This puts the MAC in power down mode */
  2241. ETH->MACPMTCTRLSTS |= ETH_MACPMTCTRLSTS_PWRDWN;
  2242. }
  2243. else
  2244. {
  2245. /* Disable the MAC Power Down */
  2246. ETH->MACPMTCTRLSTS &= ~ETH_MACPMTCTRLSTS_PWRDWN;
  2247. }
  2248. }
  2249. /*--------------------------------- MMC ------------------------------------*/
  2250. /**
  2251. * @brief Enables or disables the MMC Counter Freeze.
  2252. * @param Cmd new state of the MMC Counter Freeze.
  2253. * This parameter can be: ENABLE or DISABLE.
  2254. */
  2255. void ETH_EnableMmcCounterFreeze(FunctionalState Cmd)
  2256. {
  2257. /* Check the parameters */
  2258. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  2259. if (Cmd != DISABLE)
  2260. {
  2261. /* Enable the MMC Counter Freeze */
  2262. ETH->MMCCTRL |= ETH_MMCCTRL_CNTFREEZ;
  2263. }
  2264. else
  2265. {
  2266. /* Disable the MMC Counter Freeze */
  2267. ETH->MMCCTRL &= ~ETH_MMCCTRL_CNTFREEZ;
  2268. }
  2269. }
  2270. /**
  2271. * @brief Enables or disables the MMC Reset On Read.
  2272. * @param Cmd new state of the MMC Reset On Read.
  2273. * This parameter can be: ENABLE or DISABLE.
  2274. */
  2275. void ETH_EnableMmcResetOnRead(FunctionalState Cmd)
  2276. {
  2277. /* Check the parameters */
  2278. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  2279. if (Cmd != DISABLE)
  2280. {
  2281. /* Enable the MMC Counter reset on read */
  2282. ETH->MMCCTRL |= ETH_MMCCTRL_RSTONRD;
  2283. }
  2284. else
  2285. {
  2286. /* Disable the MMC Counter reset on read */
  2287. ETH->MMCCTRL &= ~ETH_MMCCTRL_RSTONRD;
  2288. }
  2289. }
  2290. /**
  2291. * @brief Enables or disables the MMC Counter Stop Rollover.
  2292. * @param Cmd new state of the MMC Counter Stop Rollover.
  2293. * This parameter can be: ENABLE or DISABLE.
  2294. */
  2295. void ETH_EnableMmcCounterRollover(FunctionalState Cmd)
  2296. {
  2297. /* Check the parameters */
  2298. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  2299. if (Cmd != DISABLE)
  2300. {
  2301. /* Disable the MMC Counter Stop Rollover */
  2302. ETH->MMCCTRL &= ~ETH_MMCCTRL_CNTSTOPRO;
  2303. }
  2304. else
  2305. {
  2306. /* Enable the MMC Counter Stop Rollover */
  2307. ETH->MMCCTRL |= ETH_MMCCTRL_CNTSTOPRO;
  2308. }
  2309. }
  2310. /**
  2311. * @brief Resets the MMC Counters.
  2312. */
  2313. void ETH_ResetMmcCounters(void)
  2314. {
  2315. /* Resets the MMC Counters */
  2316. ETH->MMCCTRL |= ETH_MMCCTRL_CNTRST;
  2317. }
  2318. /**
  2319. * @brief Enables or disables the specified ETHERNET MMC interrupts.
  2320. * @param ETH_MMC_IT specifies the ETHERNET MMC interrupt sources to be enabled or disabled.
  2321. * This parameter can be any combination of Tx interrupt or
  2322. * any combination of Rx interrupt (but not both)of the following values:
  2323. * @arg ETH_MMC_INT_TXGFRMIS When Tx good frame counter reaches half the maximum value
  2324. * @arg ETH_MMC_INT_TXMCOLGFIS When Tx good multi col counter reaches half the maximum value
  2325. * @arg ETH_MMC_INT_TXSCOLGFIS When Tx good single col counter reaches half the maximum value
  2326. * @arg ETH_MMC_INT_RXUCGFIS When Rx good unicast frames counter reaches half the maximum value
  2327. * @arg ETH_MMC_INT_RXALGNERFIS When Rx alignment error counter reaches half the maximum value
  2328. * @arg ETH_MMC_INT_RXCRCERFIS When Rx crc error counter reaches half the maximum value
  2329. * @param Cmd new state of the specified ETHERNET MMC interrupts.
  2330. * This parameter can be: ENABLE or DISABLE.
  2331. */
  2332. void ETH_EnableMmcInt(uint32_t ETH_MMC_IT, FunctionalState Cmd)
  2333. {
  2334. /* Check the parameters */
  2335. assert_param(IS_ETH_MMC_INT(ETH_MMC_IT));
  2336. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  2337. if ((ETH_MMC_IT & (uint32_t)0x10000000) != (uint32_t)RESET)
  2338. {
  2339. /* Remove egister mak from IT */
  2340. ETH_MMC_IT &= 0xEFFFFFFF;
  2341. /* ETHERNET MMC Rx interrupts selected */
  2342. if (Cmd != DISABLE)
  2343. {
  2344. /* Enable the selected ETHERNET MMC interrupts */
  2345. ETH->MMCRXINTMSK &= (~(uint32_t)ETH_MMC_IT);
  2346. }
  2347. else
  2348. {
  2349. /* Disable the selected ETHERNET MMC interrupts */
  2350. ETH->MMCRXINTMSK |= ETH_MMC_IT;
  2351. }
  2352. }
  2353. else
  2354. {
  2355. /* ETHERNET MMC Tx interrupts selected */
  2356. if (Cmd != DISABLE)
  2357. {
  2358. /* Enable the selected ETHERNET MMC interrupts */
  2359. ETH->MMCTXINTMSK &= (~(uint32_t)ETH_MMC_IT);
  2360. }
  2361. else
  2362. {
  2363. /* Disable the selected ETHERNET MMC interrupts */
  2364. ETH->MMCTXINTMSK |= ETH_MMC_IT;
  2365. }
  2366. }
  2367. }
  2368. /**
  2369. * @brief Checks whether the specified ETHERNET MMC IT is set or not.
  2370. * @param ETH_MMC_IT specifies the ETHERNET MMC interrupt.
  2371. * This parameter can be one of the following values:
  2372. * @arg ETH_MMC_IT_TxFCGC When Tx good frame counter reaches half the maximum value
  2373. * @arg ETH_MMC_IT_TxMCGC When Tx good multi col counter reaches half the maximum value
  2374. * @arg ETH_MMC_IT_TxSCGC When Tx good single col counter reaches half the maximum value
  2375. * @arg ETH_MMC_IT_RxUGFC When Rx good unicast frames counter reaches half the maximum value
  2376. * @arg ETH_MMC_IT_RxAEC When Rx alignment error counter reaches half the maximum value
  2377. * @arg ETH_MMC_IT_RxCEC When Rx crc error counter reaches half the maximum value
  2378. * @return The value of ETHERNET MMC IT (SET or RESET).
  2379. */
  2380. INTStatus ETH_GetMmcIntStatus(uint32_t ETH_MMC_IT)
  2381. {
  2382. INTStatus bitstatus = RESET;
  2383. /* Check the parameters */
  2384. assert_param(IS_ETH_MMC_GET_INT(ETH_MMC_IT));
  2385. if ((ETH_MMC_IT & (uint32_t)0x10000000) != (uint32_t)RESET)
  2386. {
  2387. /* ETHERNET MMC Rx interrupts selected */
  2388. /* Check if the ETHERNET MMC Rx selected interrupt is enabled and occured */
  2389. if ((((ETH->MMCRXINT & ETH_MMC_IT) != (uint32_t)RESET)) && ((ETH->MMCRXINTMSK & ETH_MMC_IT) != (uint32_t)RESET))
  2390. {
  2391. bitstatus = SET;
  2392. }
  2393. else
  2394. {
  2395. bitstatus = RESET;
  2396. }
  2397. }
  2398. else
  2399. {
  2400. /* ETHERNET MMC Tx interrupts selected */
  2401. /* Check if the ETHERNET MMC Tx selected interrupt is enabled and occured */
  2402. if ((((ETH->MMCTXINT & ETH_MMC_IT) != (uint32_t)RESET)) && ((ETH->MMCRXINTMSK & ETH_MMC_IT) != (uint32_t)RESET))
  2403. {
  2404. bitstatus = SET;
  2405. }
  2406. else
  2407. {
  2408. bitstatus = RESET;
  2409. }
  2410. }
  2411. return bitstatus;
  2412. }
  2413. /**
  2414. * @brief Get the specified ETHERNET MMC register value.
  2415. * @param ETH_MMCReg specifies the ETHERNET MMC register.
  2416. * This parameter can be one of the following values:
  2417. * @arg ETH_MMCCTRL MMC CTRL register
  2418. * @arg ETH_MMCRXINT MMC RIR register
  2419. * @arg ETH_MMCTXINT MMC TIR register
  2420. * @arg ETH_MMCRXINTMSK MMC RIMR register
  2421. * @arg ETH_MMCTXINTMSK MMC TIMR register
  2422. * @arg ETH_MMCTXGFASCCNT MMC TGFSCCR register
  2423. * @arg ETH_MMCTXGFAMSCCNT MMC TGFMSCCR register
  2424. * @arg ETH_MMCTXGFCNT MMC TGFCR register
  2425. * @arg ETH_MMCRXFCECNT MMC RFCECR register
  2426. * @arg ETH_MMCRXFAECNT MMC RFAECR register
  2427. * @arg ETH_MMCRXGUFCNT MMC RGUFCRregister
  2428. * @return The value of ETHERNET MMC Register value.
  2429. */
  2430. uint32_t ETH_GetMmcRegisterValue(uint32_t ETH_MMCReg)
  2431. {
  2432. /* Check the parameters */
  2433. assert_param(IS_ETH_MMC_REGISTER(ETH_MMCReg));
  2434. /* Return the selected register value */
  2435. return (*(__IO uint32_t*)(ETH_MAC_BASE + ETH_MMCReg));
  2436. }
  2437. /*--------------------------------- PTP ------------------------------------*/
  2438. /**
  2439. * @brief Updated the PTP block for fine correction with the Time Stamp Addend register value.
  2440. */
  2441. void ETH_UpdatePtpTimeStampAddend(void)
  2442. {
  2443. /* Enable the PTP block update with the Time Stamp Addend register value */
  2444. ETH->PTPTSCTRL |= ETH_PTPTSCTRL_TSADDREG;
  2445. }
  2446. /**
  2447. * @brief Enable the PTP Time Stamp interrupt trigger
  2448. */
  2449. void ETH_EnablePtpTimeStampIntTrigger(void)
  2450. {
  2451. /* Enable the PTP target time interrupt */
  2452. ETH->PTPTSCTRL |= ETH_PTPTSCTRL_TSTRIG;
  2453. }
  2454. /**
  2455. * @brief Updated the PTP system time with the Time Stamp Update register value.
  2456. */
  2457. void ETH_UpdatePtpTimeStamp(void)
  2458. {
  2459. /* Enable the PTP system time update with the Time Stamp Update register value */
  2460. ETH->PTPTSCTRL |= ETH_PTPTSCTRL_TSUPDT;
  2461. }
  2462. /**
  2463. * @brief Initialize the PTP Time Stamp
  2464. */
  2465. void ETH_InitPtpTimeStamp(void)
  2466. {
  2467. /* Initialize the PTP Time Stamp */
  2468. ETH->PTPTSCTRL |= ETH_PTPTSCTRL_TSINIT;
  2469. }
  2470. /**
  2471. * @brief Selects the PTP Update method
  2472. * @param UpdateMethod the PTP Update method
  2473. * This parameter can be one of the following values:
  2474. * @arg ETH_PTP_FINE_UPDATE Fine Update method
  2475. * @arg ETH_PTP_COARSE_UPDATE Coarse Update method
  2476. */
  2477. void ETH_ConfigPtpUpdateMethod(uint32_t UpdateMethod)
  2478. {
  2479. /* Check the parameters */
  2480. assert_param(IS_ETH_PTP_UPDATE(UpdateMethod));
  2481. if (UpdateMethod != ETH_PTP_COARSE_UPDATE)
  2482. {
  2483. /* Enable the PTP Fine Update method */
  2484. ETH->PTPTSCTRL |= ETH_PTPTSCTRL_TSCFUPDT;
  2485. }
  2486. else
  2487. {
  2488. /* Disable the PTP Coarse Update method */
  2489. ETH->PTPTSCTRL &= (~(uint32_t)ETH_PTPTSCTRL_TSCFUPDT);
  2490. }
  2491. }
  2492. /**
  2493. * @brief Enables or disables the PTP time stamp for transmit and receive frames.
  2494. * @param Cmd new state of the PTP time stamp for transmit and receive frames
  2495. * This parameter can be: ENABLE or DISABLE.
  2496. */
  2497. void ETH_StartPTPTimeStamp(FunctionalState Cmd)
  2498. {
  2499. /* Check the parameters */
  2500. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  2501. if (Cmd != DISABLE)
  2502. {
  2503. /* Enable the PTP time stamp for transmit and receive frames */
  2504. ETH->PTPTSCTRL |= ETH_PTPTSCTRL_TSENA;
  2505. }
  2506. else
  2507. {
  2508. /* Disable the PTP time stamp for transmit and receive frames */
  2509. ETH->PTPTSCTRL &= (~(uint32_t)ETH_PTPTSCTRL_TSENA);
  2510. }
  2511. }
  2512. /**
  2513. * @brief Checks whether the specified ETHERNET PTP flag is set or not.
  2514. * @param ETH_PTP_FLAG specifies the flag to check.
  2515. * This parameter can be one of the following values:
  2516. * @arg ETH_PTP_FLAG_TSADDREG Addend Register Update
  2517. * @arg ETH_PTP_FLAG_TSTRIG Time Stamp Interrupt Trigger Enable
  2518. * @arg ETH_PTP_FLAG_TSUPDT Time Stamp Update
  2519. * @arg ETH_PTP_FLAG_TSINIT Time Stamp Initialize
  2520. * @return The new state of ETHERNET PTP Flag (SET or RESET).
  2521. */
  2522. FlagStatus ETH_GetPtpFlagStatus(uint32_t ETH_PTP_FLAG)
  2523. {
  2524. FlagStatus bitstatus = RESET;
  2525. /* Check the parameters */
  2526. assert_param(IS_ETH_PTP_GET_FLAG(ETH_PTP_FLAG));
  2527. if ((ETH->PTPTSCTRL & ETH_PTP_FLAG) != (uint32_t)RESET)
  2528. {
  2529. bitstatus = SET;
  2530. }
  2531. else
  2532. {
  2533. bitstatus = RESET;
  2534. }
  2535. return bitstatus;
  2536. }
  2537. /**
  2538. * @brief Sets the system time Sub-Second Increment value.
  2539. * @param SubSecondValue specifies the PTP Sub-Second Increment Register value.
  2540. */
  2541. void ETH_SetPtpSubSecondInc(uint32_t SubSecondValue)
  2542. {
  2543. /* Check the parameters */
  2544. assert_param(IS_ETH_PTP_SUBSECOND_INCREMENT(SubSecondValue));
  2545. /* Set the PTP Sub-Second Increment Register */
  2546. ETH->PTPSSINC = SubSecondValue;
  2547. }
  2548. /**
  2549. * @brief Sets the Time Stamp update sign and values.
  2550. * @param Sign specifies the PTP Time update value sign.
  2551. * This parameter can be one of the following values:
  2552. * @arg ETH_PTP_POSITIVE_TIME positive time value.
  2553. * @arg ETH_PTP_NEGATIVE_TIME negative time value.
  2554. * @param SecondValue specifies the PTP Time update second value.
  2555. * @param SubSecondValue specifies the PTP Time update sub-second value.
  2556. * This parameter is a 31 bit value, bit32 correspond to the sign.
  2557. */
  2558. void ETH_SetPtpTimeStampUpdate(uint32_t Sign, uint32_t SecondValue, uint32_t SubSecondValue)
  2559. {
  2560. /* Check the parameters */
  2561. assert_param(IS_ETH_PTP_TIME_SIGN(Sign));
  2562. assert_param(IS_ETH_PTP_TIME_STAMP_UPDATE_SUBSECOND(SubSecondValue));
  2563. /* Set the PTP Time Update High Register */
  2564. ETH->PTPSECUP = SecondValue;
  2565. /* Set the PTP Time Update Low Register with sign */
  2566. ETH->PTPNSUP = Sign | SubSecondValue;
  2567. }
  2568. /**
  2569. * @brief Sets the Time Stamp Addend value.
  2570. * @param Value specifies the PTP Time Stamp Addend Register value.
  2571. */
  2572. void ETH_SetPtpTimeStampAddend(uint32_t Value)
  2573. {
  2574. /* Set the PTP Time Stamp Addend Register */
  2575. ETH->PTPTSADD = Value;
  2576. }
  2577. /**
  2578. * @brief Sets the Target Time registers values.
  2579. * @param HighValue specifies the PTP Target Time High Register value.
  2580. * @param LowValue specifies the PTP Target Time Low Register value.
  2581. */
  2582. void ETH_SetPtpTargetTime(uint32_t HighValue, uint32_t LowValue)
  2583. {
  2584. /* Set the PTP Target Time High Register */
  2585. ETH->PTPTTSEC = HighValue;
  2586. /* Set the PTP Target Time Low Register */
  2587. ETH->PTPTTNS = LowValue;
  2588. }
  2589. /**
  2590. * @brief Get the specified ETHERNET PTP register value.
  2591. * @param ETH_PTPReg specifies the ETHERNET PTP register.
  2592. * This parameter can be one of the following values:
  2593. * @arg ETH_PTPTSCTRL Sub-Second Increment Register
  2594. * @arg ETH_PTPSSINC Sub-Second Increment Register
  2595. * @arg ETH_PTPSEC Time Stamp High Register
  2596. * @arg ETH_PTPNS Time Stamp Low Register
  2597. * @arg ETH_PTPSECUP Time Stamp High Update Register
  2598. * @arg ETH_PTPNSUP Time Stamp Low Update Register
  2599. * @arg ETH_PTPTSADD Time Stamp Addend Register
  2600. * @arg ETH_PTPTTSEC Target Time High Register
  2601. * @arg ETH_PTPTTNS Target Time Low Register
  2602. * @return The value of ETHERNET PTP Register value.
  2603. */
  2604. uint32_t ETH_GetPtpRegisterValue(uint32_t ETH_PTPReg)
  2605. {
  2606. /* Check the parameters */
  2607. assert_param(IS_ETH_PTP_REGISTER(ETH_PTPReg));
  2608. /* Return the selected register value */
  2609. return (*(__IO uint32_t*)(ETH_MAC_BASE + ETH_PTPReg));
  2610. }
  2611. /**
  2612. * @brief Initializes the DMA Tx descriptors in chain mode with PTP.
  2613. * @param DMATxDescTab Pointer on the first Tx desc list
  2614. * @param DMAPTPTxDescTab Pointer on the first PTP Tx desc list
  2615. * @param TxBuff Pointer on the first TxBuffer list
  2616. * @param TxBuffCount Number of the used Tx desc in the list
  2617. */
  2618. void ETH_ConfigDmaPtpTxDescInChainMode(ETH_DMADescType* DMATxDescTab,
  2619. ETH_DMADescType* DMAPTPTxDescTab,
  2620. uint8_t* TxBuff,
  2621. uint32_t TxBuffCount)
  2622. {
  2623. uint32_t i = 0;
  2624. ETH_DMADescType* DMATxDesc;
  2625. /* Set the DMATxDescToSet pointer with the first one of the DMATxDescTab list */
  2626. DMATxDescToSet = DMATxDescTab;
  2627. DMAPTPTxDescToSet = DMAPTPTxDescTab;
  2628. /* Fill each DMATxDesc descriptor with the right values */
  2629. for (i = 0; i < TxBuffCount; i++)
  2630. {
  2631. /* Get the pointer on the ith member of the Tx Desc list */
  2632. DMATxDesc = DMATxDescTab + i;
  2633. /* Set Second Address Chained bit and enable PTP */
  2634. DMATxDesc->Status = 0;
  2635. DMATxDesc->CtrlOrBufSize = ETH_DMA_TX_DESC_TCH | ETH_DMA_TX_DESC_TTSE;
  2636. /* Set Buffer1 address pointer */
  2637. DMATxDesc->Buf1Addr = (uint32_t)(&TxBuff[i * ETH_MAX_PACKET_SIZE]);
  2638. /* Initialize the next descriptor with the Next Desciptor Polling Enable */
  2639. if (i < (TxBuffCount - 1))
  2640. {
  2641. /* Set next descriptor address register with next descriptor base address */
  2642. DMATxDesc->Buf2OrNextDescAddr = (uint32_t)(DMATxDescTab + i + 1);
  2643. }
  2644. else
  2645. {
  2646. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  2647. DMATxDesc->Buf2OrNextDescAddr = (uint32_t)DMATxDescTab;
  2648. }
  2649. /* make DMAPTPTxDescTab points to the same addresses as DMATxDescTab */
  2650. (&DMAPTPTxDescTab[i])->Buf1Addr = DMATxDesc->Buf1Addr;
  2651. (&DMAPTPTxDescTab[i])->Buf2OrNextDescAddr = DMATxDesc->Buf2OrNextDescAddr;
  2652. }
  2653. /* Store on the last DMAPTPTxDescTab desc status record the first list address */
  2654. (&DMAPTPTxDescTab[i - 1])->Status = (uint32_t)DMAPTPTxDescTab;
  2655. /* Set Transmit Desciptor List Address Register */
  2656. ETH->DMATXDLADDR = (uint32_t)DMATxDescTab;
  2657. }
  2658. /**
  2659. * @brief Initializes the DMA Rx descriptors in chain mode.
  2660. * @param DMARxDescTab Pointer on the first Rx desc list
  2661. * @param DMAPTPRxDescTab Pointer on the first PTP Rx desc list
  2662. * @param RxBuff Pointer on the first RxBuffer list
  2663. * @param RxBuffCount Number of the used Rx desc in the list
  2664. */
  2665. void ETH_ConfigDmaPtpRxDescInChainMode(ETH_DMADescType* DMARxDescTab,
  2666. ETH_DMADescType* DMAPTPRxDescTab,
  2667. uint8_t* RxBuff,
  2668. uint32_t RxBuffCount)
  2669. {
  2670. uint32_t i = 0;
  2671. ETH_DMADescType* DMARxDesc;
  2672. /* Set the DMARxDescToGet pointer with the first one of the DMARxDescTab list */
  2673. DMARxDescToGet = DMARxDescTab;
  2674. DMAPTPRxDescToGet = DMAPTPRxDescTab;
  2675. /* Fill each DMARxDesc descriptor with the right values */
  2676. for (i = 0; i < RxBuffCount; i++)
  2677. {
  2678. /* Get the pointer on the ith member of the Rx Desc list */
  2679. DMARxDesc = DMARxDescTab + i;
  2680. /* Set Own bit of the Rx descriptor Status */
  2681. DMARxDesc->Status = ETH_DMA_RX_DESC_OWN;
  2682. /* Set Buffer1 size and Second Address Chained bit */
  2683. DMARxDesc->CtrlOrBufSize = ETH_DMA_RX_DESC_RCH | (uint32_t)ETH_MAX_PACKET_SIZE;
  2684. /* Set Buffer1 address pointer */
  2685. DMARxDesc->Buf1Addr = (uint32_t)(&RxBuff[i * ETH_MAX_PACKET_SIZE]);
  2686. /* Initialize the next descriptor with the Next Desciptor Polling Enable */
  2687. if (i < (RxBuffCount - 1))
  2688. {
  2689. /* Set next descriptor address register with next descriptor base address */
  2690. DMARxDesc->Buf2OrNextDescAddr = (uint32_t)(DMARxDescTab + i + 1);
  2691. }
  2692. else
  2693. {
  2694. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  2695. DMARxDesc->Buf2OrNextDescAddr = (uint32_t)(DMARxDescTab);
  2696. }
  2697. /* Make DMAPTPRxDescTab points to the same addresses as DMARxDescTab */
  2698. (&DMAPTPRxDescTab[i])->Buf1Addr = DMARxDesc->Buf1Addr;
  2699. (&DMAPTPRxDescTab[i])->Buf2OrNextDescAddr = DMARxDesc->Buf2OrNextDescAddr;
  2700. }
  2701. /* Store on the last DMAPTPRxDescTab desc status record the first list address */
  2702. (&DMAPTPRxDescTab[i - 1])->Status = (uint32_t)DMAPTPRxDescTab;
  2703. /* Set Receive Desciptor List Address Register */
  2704. ETH->DMARXDLADDR = (uint32_t)DMARxDescTab;
  2705. }
  2706. /**
  2707. * @brief Transmits a packet, from application buffer, pointed by ppkt with Time Stamp values.
  2708. * @param ppkt pointer to application packet buffer to transmit.
  2709. * @param FrameLength Tx Packet size.
  2710. * @param PTPTxTab Pointer on the first PTP Tx table to store Time stamp values.
  2711. * @return ETH_ERROR: in case of Tx desc owned by DMA
  2712. * ETH_SUCCESS: for correct transmission
  2713. */
  2714. uint32_t ETH_TxPtpPacket(uint8_t* ppkt, uint16_t FrameLength, uint32_t* PTPTxTab)
  2715. {
  2716. uint32_t offset = 0, timeout = 0;
  2717. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  2718. if ((DMATxDescToSet->Status & ETH_DMA_TX_DESC_OWN) != (uint32_t)RESET)
  2719. {
  2720. /* Return ERROR: OWN bit set */
  2721. return ETH_ERROR;
  2722. }
  2723. /* Copy the frame to be sent into memory pointed by the current ETHERNET DMA Tx descriptor */
  2724. for (offset = 0; offset < FrameLength; offset++)
  2725. {
  2726. (*(__IO uint8_t*)((DMAPTPTxDescToSet->Buf1Addr) + offset)) = (*(ppkt + offset));
  2727. }
  2728. /* Setting the Frame Length: bits[10:0] */
  2729. DMATxDescToSet->CtrlOrBufSize &= (~ETH_DMA_TX_DESC_TBS1);
  2730. DMATxDescToSet->CtrlOrBufSize |= (FrameLength & ETH_DMA_TX_DESC_TBS1);
  2731. /* Setting the last segment and first segment bits (in this case a frame is transmitted in one descriptor) */
  2732. DMATxDescToSet->CtrlOrBufSize |= ETH_DMA_TX_DESC_LS | ETH_DMA_TX_DESC_FS;
  2733. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  2734. DMATxDescToSet->Status |= ETH_DMA_TX_DESC_OWN;
  2735. /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
  2736. if ((ETH->DMASTS & ETH_DMASTS_TU) != (uint32_t)RESET)
  2737. {
  2738. /* Clear TBUS ETHERNET DMA flag */
  2739. ETH->DMASTS = ETH_DMASTS_TU;
  2740. /* Resume DMA transmission*/
  2741. ETH->DMATXPD = 0;
  2742. }
  2743. /* Wait for ETH_DMA_TX_DESC_TTSS flag to be set */
  2744. do
  2745. {
  2746. timeout++;
  2747. } while (!(DMATxDescToSet->Status & ETH_DMA_TX_DESC_TTSS) && (timeout < 0xFFFF));
  2748. /* Return ERROR in case of timeout */
  2749. if (timeout == PHY_READ_TO)
  2750. {
  2751. return ETH_ERROR;
  2752. }
  2753. /* Clear the DMATxDescToSet status register TTSS flag */
  2754. DMATxDescToSet->Status &= ~ETH_DMA_TX_DESC_TTSS;
  2755. *PTPTxTab++ = DMATxDescToSet->Buf1Addr;
  2756. *PTPTxTab = DMATxDescToSet->Buf2OrNextDescAddr;
  2757. /* Update the ENET DMA current descriptor */
  2758. /* Chained Mode */
  2759. if ((DMATxDescToSet->CtrlOrBufSize & ETH_DMA_TX_DESC_TCH) != (uint32_t)RESET)
  2760. {
  2761. /* Selects the next DMA Tx descriptor list for next buffer read */
  2762. DMATxDescToSet = (ETH_DMADescType*)(DMAPTPTxDescToSet->Buf2OrNextDescAddr);
  2763. if (DMAPTPTxDescToSet->Status != 0)
  2764. {
  2765. DMAPTPTxDescToSet = (ETH_DMADescType*)(DMAPTPTxDescToSet->Status);
  2766. }
  2767. else
  2768. {
  2769. DMAPTPTxDescToSet++;
  2770. }
  2771. }
  2772. else /* Ring Mode */
  2773. {
  2774. if ((DMATxDescToSet->CtrlOrBufSize & ETH_DMA_TX_DESC_TER) != (uint32_t)RESET)
  2775. {
  2776. /* Selects the next DMA Tx descriptor list for next buffer read: this will
  2777. be the first Tx descriptor in this case */
  2778. DMATxDescToSet = (ETH_DMADescType*)(ETH->DMATXDLADDR);
  2779. DMAPTPTxDescToSet = (ETH_DMADescType*)(ETH->DMATXDLADDR);
  2780. }
  2781. else
  2782. {
  2783. /* Selects the next DMA Tx descriptor list for next buffer read */
  2784. DMATxDescToSet =
  2785. (ETH_DMADescType*)((uint32_t)DMATxDescToSet + 0x10 + ((ETH->DMABUSMOD & ETH_DMABUSMOD_DSL)));
  2786. DMAPTPTxDescToSet =
  2787. (ETH_DMADescType*)((uint32_t)DMAPTPTxDescToSet + 0x10 + ((ETH->DMABUSMOD & ETH_DMABUSMOD_DSL)));
  2788. }
  2789. }
  2790. /* Return SUCCESS */
  2791. return ETH_SUCCESS;
  2792. }
  2793. /**
  2794. * @brief Receives a packet and copies it to memory pointed by ppkt with Time Stamp values.
  2795. * @param ppkt pointer to application packet receive buffer.
  2796. * @param PTPRxTab Pointer on the first PTP Rx table to store Time stamp values.
  2797. * @return ETH_ERROR: if there is error in reception
  2798. * framelength: received packet size if packet reception is correct
  2799. */
  2800. uint32_t ETH_RxPtpPacket(uint8_t* ppkt, uint32_t* PTPRxTab)
  2801. {
  2802. uint32_t offset = 0, framelength = 0;
  2803. /* Check if the descriptor is owned by the ENET or CPU */
  2804. if ((DMARxDescToGet->Status & ETH_DMA_RX_DESC_OWN) != (uint32_t)RESET)
  2805. {
  2806. /* Return error: OWN bit set */
  2807. return ETH_ERROR;
  2808. }
  2809. if (((DMARxDescToGet->Status & ETH_DMA_RX_DESC_ES) == (uint32_t)RESET)
  2810. && ((DMARxDescToGet->Status & ETH_DMA_RX_DESC_LS) != (uint32_t)RESET)
  2811. && ((DMARxDescToGet->Status & ETH_DMA_RX_DESC_FS) != (uint32_t)RESET))
  2812. {
  2813. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  2814. framelength = ((DMARxDescToGet->Status & ETH_DMA_RX_DESC_FL) >> ETH_DMA_RX_DESC_FRAME_LEN_SHIFT) - 4;
  2815. /* Copy the received frame into buffer from memory pointed by the current ETHERNET DMA Rx descriptor */
  2816. for (offset = 0; offset < framelength; offset++)
  2817. {
  2818. (*(ppkt + offset)) = (*(__IO uint8_t*)((DMAPTPRxDescToGet->Buf1Addr) + offset));
  2819. }
  2820. }
  2821. else
  2822. {
  2823. /* Return ERROR */
  2824. framelength = ETH_ERROR;
  2825. }
  2826. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  2827. if ((ETH->DMASTS & ETH_DMASTS_RU) != (uint32_t)RESET)
  2828. {
  2829. /* Clear RBUS ETHERNET DMA flag */
  2830. ETH->DMASTS = ETH_DMASTS_RU;
  2831. /* Resume DMA reception */
  2832. ETH->DMARXPD = 0;
  2833. }
  2834. *PTPRxTab++ = DMARxDescToGet->Buf1Addr;
  2835. *PTPRxTab = DMARxDescToGet->Buf2OrNextDescAddr;
  2836. /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */
  2837. DMARxDescToGet->Status |= ETH_DMA_RX_DESC_OWN;
  2838. /* Update the ETHERNET DMA global Rx descriptor with next Rx decriptor */
  2839. /* Chained Mode */
  2840. if ((DMARxDescToGet->CtrlOrBufSize & ETH_DMA_RX_DESC_RCH) != (uint32_t)RESET)
  2841. {
  2842. /* Selects the next DMA Rx descriptor list for next buffer read */
  2843. DMARxDescToGet = (ETH_DMADescType*)(DMAPTPRxDescToGet->Buf2OrNextDescAddr);
  2844. if (DMAPTPRxDescToGet->Status != 0)
  2845. {
  2846. DMAPTPRxDescToGet = (ETH_DMADescType*)(DMAPTPRxDescToGet->Status);
  2847. }
  2848. else
  2849. {
  2850. DMAPTPRxDescToGet++;
  2851. }
  2852. }
  2853. else /* Ring Mode */
  2854. {
  2855. if ((DMARxDescToGet->CtrlOrBufSize & ETH_DMA_RX_DESC_RER) != (uint32_t)RESET)
  2856. {
  2857. /* Selects the first DMA Rx descriptor for next buffer to read: last Rx descriptor was used */
  2858. DMARxDescToGet = (ETH_DMADescType*)(ETH->DMARXDLADDR);
  2859. }
  2860. else
  2861. {
  2862. /* Selects the next DMA Rx descriptor list for next buffer to read */
  2863. DMARxDescToGet =
  2864. (ETH_DMADescType*)((uint32_t)DMARxDescToGet + 0x10 + ((ETH->DMABUSMOD & ETH_DMABUSMOD_DSL)));
  2865. }
  2866. }
  2867. /* Return Frame Length/ERROR */
  2868. return (framelength);
  2869. }
  2870. /**
  2871. * @}
  2872. */
  2873. /**
  2874. * @}
  2875. */
  2876. /**
  2877. * @}
  2878. */