stm32f4x7_eth.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4x7_eth.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 14-October-2011
  7. * @brief This file is the low level driver for STM32F407xx/417xx Ethernet Controller.
  8. * This driver does not include low level functions for PTP time-stamp.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * <h2><center>&copy; Portions COPYRIGHT 2011 STMicroelectronics</center></h2>
  20. ******************************************************************************
  21. */
  22. /**
  23. ******************************************************************************
  24. * <h2><center>&copy; Portions COPYRIGHT 2012 Embest Tech. Co., Ltd.</center></h2>
  25. * @file stm32f4x7_eth.c
  26. * @author CMP Team
  27. * @version V1.0.0
  28. * @date 28-December-2012
  29. * @brief This file is the low level driver for STM32F407xx/417xx Ethernet Controller.
  30. * This driver does not include low level functions for PTP time-stamp.
  31. ******************************************************************************
  32. * @attention
  33. *
  34. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  35. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  36. * TIME. AS A RESULT, Embest SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT
  37. * OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
  38. * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
  39. * CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  40. ******************************************************************************
  41. */
  42. /* Includes ------------------------------------------------------------------*/
  43. #include "stm32f4x7_eth.h"
  44. #include "stm32f4xx_rcc.h"
  45. #include <string.h>
  46. /** @addtogroup STM32F4x7_ETH_Driver
  47. * @brief ETH driver modules
  48. * @{
  49. */
  50. /** @defgroup ETH_Private_TypesDefinitions
  51. * @{
  52. */
  53. /**
  54. * @}
  55. */
  56. /** @defgroup ETH_Private_Defines
  57. * @{
  58. */
  59. /**
  60. * @}
  61. */
  62. /** @defgroup ETH_Private_Macros
  63. * @{
  64. */
  65. /**
  66. * @}
  67. */
  68. /** @defgroup ETH_Private_Variables
  69. * @{
  70. */
  71. #if defined (__CC_ARM) /*!< ARM Compiler */
  72. __align(4)
  73. ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB];/* Ethernet Rx MA Descriptor */
  74. __align(4)
  75. ETH_DMADESCTypeDef DMATxDscrTab[ETH_TXBUFNB];/* Ethernet Tx DMA Descriptor */
  76. __align(4)
  77. uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE]; /* Ethernet Receive Buffer */
  78. __align(4)
  79. uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE]; /* Ethernet Transmit Buffer */
  80. #elif defined ( __ICCARM__ ) /*!< IAR Compiler */
  81. #pragma data_alignment=4
  82. ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB];/* Ethernet Rx MA Descriptor */
  83. #pragma data_alignment=4
  84. ETH_DMADESCTypeDef DMATxDscrTab[ETH_TXBUFNB];/* Ethernet Tx DMA Descriptor */
  85. #pragma data_alignment=4
  86. uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE]; /* Ethernet Receive Buffer */
  87. #pragma data_alignment=4
  88. uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE]; /* Ethernet Transmit Buffer */
  89. #elif defined (__GNUC__) /*!< GNU Compiler */
  90. ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB] __attribute__ ((aligned (4))); /* Ethernet Rx DMA Descriptor */
  91. ETH_DMADESCTypeDef DMATxDscrTab[ETH_TXBUFNB] __attribute__ ((aligned (4))); /* Ethernet Tx DMA Descriptor */
  92. uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE] __attribute__ ((aligned (4))); /* Ethernet Receive Buffer */
  93. uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE] __attribute__ ((aligned (4))); /* Ethernet Transmit Buffer */
  94. #elif defined (__TASKING__) /*!< TASKING Compiler */
  95. __align(4)
  96. ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB];/* Ethernet Rx MA Descriptor */
  97. __align(4)
  98. ETH_DMADESCTypeDef DMATxDscrTab[ETH_TXBUFNB];/* Ethernet Tx DMA Descriptor */
  99. __align(4)
  100. uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE]; /* Ethernet Receive Buffer */
  101. __align(4)
  102. uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE]; /* Ethernet Transmit Buffer */
  103. #endif /* __CC_ARM */
  104. /* Global pointers on Tx and Rx descriptor used to track transmit and receive descriptors */
  105. __IO ETH_DMADESCTypeDef *DMATxDescToSet;
  106. __IO ETH_DMADESCTypeDef *DMARxDescToGet;
  107. /* Structure used to hold the last received packet descriptors info */
  108. ETH_DMA_Rx_Frame_infos RX_Frame_Descriptor;
  109. __IO ETH_DMA_Rx_Frame_infos *DMA_RX_FRAME_infos;
  110. __IO uint32_t Frame_Rx_index;
  111. /**
  112. * @}
  113. */
  114. /** @defgroup ETH_Private_FunctionPrototypes
  115. * @{
  116. */
  117. /**
  118. * @}
  119. */
  120. /** @defgroup ETH_Private_Functions
  121. * @{
  122. */
  123. #ifndef USE_Delay
  124. /**
  125. * @brief Inserts a delay time.
  126. * @param nCount: specifies the delay time length.
  127. * @retval None
  128. */
  129. static void ETH_Delay(__IO uint32_t nCount)
  130. {
  131. __IO uint32_t index = 0;
  132. for(index = nCount; index != 0; index--)
  133. {
  134. }
  135. }
  136. #endif /* USE_Delay*/
  137. /******************************************************************************/
  138. /* Global ETH MAC/DMA functions */
  139. /******************************************************************************/
  140. /**
  141. * @brief Deinitializes the ETHERNET peripheral registers to their default reset values.
  142. * @param None
  143. * @retval None
  144. */
  145. void ETH_DeInit(void)
  146. {
  147. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_ETH_MAC, ENABLE);
  148. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_ETH_MAC, DISABLE);
  149. }
  150. /**
  151. * @brief Fills each ETH_InitStruct member with its default value.
  152. * @param ETH_InitStruct: pointer to a ETH_InitTypeDef structure which will be initialized.
  153. * @retval None
  154. */
  155. void ETH_StructInit(ETH_InitTypeDef* ETH_InitStruct)
  156. {
  157. /* ETH_InitStruct members default value */
  158. /*------------------------ MAC Configuration ---------------------------*/
  159. /* PHY Auto-negotiation enabled */
  160. ETH_InitStruct->ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
  161. /* MAC watchdog enabled: cuts-off long frame */
  162. ETH_InitStruct->ETH_Watchdog = ETH_Watchdog_Enable;
  163. /* MAC Jabber enabled in Half-duplex mode */
  164. ETH_InitStruct->ETH_Jabber = ETH_Jabber_Enable;
  165. /* Ethernet interframe gap set to 96 bits */
  166. ETH_InitStruct->ETH_InterFrameGap = ETH_InterFrameGap_96Bit;
  167. /* Carrier Sense Enabled in Half-Duplex mode */
  168. ETH_InitStruct->ETH_CarrierSense = ETH_CarrierSense_Enable;
  169. /* PHY speed configured to 100Mbit/s */
  170. ETH_InitStruct->ETH_Speed = ETH_Speed_100M;
  171. /* Receive own Frames in Half-Duplex mode enabled */
  172. ETH_InitStruct->ETH_ReceiveOwn = ETH_ReceiveOwn_Enable;
  173. /* MAC MII loopback disabled */
  174. ETH_InitStruct->ETH_LoopbackMode = ETH_LoopbackMode_Disable;
  175. /* Full-Duplex mode selected */
  176. ETH_InitStruct->ETH_Mode = ETH_Mode_FullDuplex;
  177. /* IPv4 and TCP/UDP/ICMP frame Checksum Offload disabled */
  178. ETH_InitStruct->ETH_ChecksumOffload = ETH_ChecksumOffload_Disable;
  179. /* Retry Transmission enabled for half-duplex mode */
  180. ETH_InitStruct->ETH_RetryTransmission = ETH_RetryTransmission_Enable;
  181. /* Automatic PAD/CRC strip disabled*/
  182. ETH_InitStruct->ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
  183. /* half-duplex mode retransmission Backoff time_limit = 10 slot times*/
  184. ETH_InitStruct->ETH_BackOffLimit = ETH_BackOffLimit_10;
  185. /* half-duplex mode Deferral check disabled */
  186. ETH_InitStruct->ETH_DeferralCheck = ETH_DeferralCheck_Disable;
  187. /* Receive all frames disabled */
  188. ETH_InitStruct->ETH_ReceiveAll = ETH_ReceiveAll_Disable;
  189. /* Source address filtering (on the optional MAC addresses) disabled */
  190. ETH_InitStruct->ETH_SourceAddrFilter = ETH_SourceAddrFilter_Disable;
  191. /* Do not forward control frames that do not pass the address filtering */
  192. ETH_InitStruct->ETH_PassControlFrames = ETH_PassControlFrames_BlockAll;
  193. /* Disable reception of Broadcast frames */
  194. ETH_InitStruct->ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Disable;
  195. /* Normal Destination address filtering (not reverse addressing) */
  196. ETH_InitStruct->ETH_DestinationAddrFilter = ETH_DestinationAddrFilter_Normal;
  197. /* Promiscuous address filtering mode disabled */
  198. ETH_InitStruct->ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
  199. /* Perfect address filtering for multicast addresses */
  200. ETH_InitStruct->ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
  201. /* Perfect address filtering for unicast addresses */
  202. ETH_InitStruct->ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
  203. /* Initialize hash table high and low regs */
  204. ETH_InitStruct->ETH_HashTableHigh = 0x0;
  205. ETH_InitStruct->ETH_HashTableLow = 0x0;
  206. /* Flow control config (flow control disabled)*/
  207. ETH_InitStruct->ETH_PauseTime = 0x0;
  208. ETH_InitStruct->ETH_ZeroQuantaPause = ETH_ZeroQuantaPause_Disable;
  209. ETH_InitStruct->ETH_PauseLowThreshold = ETH_PauseLowThreshold_Minus4;
  210. ETH_InitStruct->ETH_UnicastPauseFrameDetect = ETH_UnicastPauseFrameDetect_Disable;
  211. ETH_InitStruct->ETH_ReceiveFlowControl = ETH_ReceiveFlowControl_Disable;
  212. ETH_InitStruct->ETH_TransmitFlowControl = ETH_TransmitFlowControl_Disable;
  213. /* VLANtag config (VLAN field not checked) */
  214. ETH_InitStruct->ETH_VLANTagComparison = ETH_VLANTagComparison_16Bit;
  215. ETH_InitStruct->ETH_VLANTagIdentifier = 0x0;
  216. /*---------------------- DMA Configuration -------------------------------*/
  217. /* Drops frames with with TCP/IP checksum errors */
  218. ETH_InitStruct->ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Disable;
  219. /* Store and forward mode enabled for receive */
  220. ETH_InitStruct->ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
  221. /* Flush received frame that created FIFO overflow */
  222. ETH_InitStruct->ETH_FlushReceivedFrame = ETH_FlushReceivedFrame_Enable;
  223. /* Store and forward mode enabled for transmit */
  224. ETH_InitStruct->ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
  225. /* Threshold TXFIFO level set to 64 bytes (used when threshold mode is enabled) */
  226. ETH_InitStruct->ETH_TransmitThresholdControl = ETH_TransmitThresholdControl_64Bytes;
  227. /* Disable forwarding frames with errors (short frames, CRC,...)*/
  228. ETH_InitStruct->ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
  229. /* Disable undersized good frames */
  230. ETH_InitStruct->ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
  231. /* Threshold RXFIFO level set to 64 bytes (used when Cut-through mode is enabled) */
  232. ETH_InitStruct->ETH_ReceiveThresholdControl = ETH_ReceiveThresholdControl_64Bytes;
  233. /* Disable Operate on second frame (transmit a second frame to FIFO without
  234. waiting status of previous frame*/
  235. ETH_InitStruct->ETH_SecondFrameOperate = ETH_SecondFrameOperate_Disable;
  236. /* DMA works on 32-bit aligned start source and destinations addresses */
  237. ETH_InitStruct->ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
  238. /* Enabled Fixed Burst Mode (mix of INC4, INC8, INC16 and SINGLE DMA transactions */
  239. ETH_InitStruct->ETH_FixedBurst = ETH_FixedBurst_Enable;
  240. /* DMA transfer max burst length = 32 beats = 32 x 32bits */
  241. ETH_InitStruct->ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
  242. ETH_InitStruct->ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
  243. /* DMA Ring mode skip length = 0 */
  244. ETH_InitStruct->ETH_DescriptorSkipLength = 0x0;
  245. /* Equal priority (round-robin) between transmit and receive DMA engines */
  246. ETH_InitStruct->ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_1_1;
  247. }
  248. /**
  249. * @brief Initializes the ETHERNET peripheral according to the specified
  250. * parameters in the ETH_InitStruct .
  251. * @param ETH_InitStruct: pointer to a ETH_InitTypeDef structure that contains
  252. * the configuration information for the specified ETHERNET peripheral.
  253. * @param PHYAddress: external PHY address
  254. * @retval ETH_ERROR: Ethernet initialization failed
  255. * ETH_SUCCESS: Ethernet successfully initialized
  256. */
  257. uint32_t ETH_Init(ETH_InitTypeDef* ETH_InitStruct, uint16_t PHYAddress)
  258. {
  259. uint32_t RegValue = 0, tmpreg = 0;
  260. __IO uint32_t i = 0;
  261. RCC_ClocksTypeDef rcc_clocks;
  262. uint32_t hclk = 60000000;
  263. __IO uint32_t timeout = 0;
  264. /* Check the parameters */
  265. /* MAC --------------------------*/
  266. assert_param(IS_ETH_AUTONEGOTIATION(ETH_InitStruct->ETH_AutoNegotiation));
  267. assert_param(IS_ETH_WATCHDOG(ETH_InitStruct->ETH_Watchdog));
  268. assert_param(IS_ETH_JABBER(ETH_InitStruct->ETH_Jabber));
  269. assert_param(IS_ETH_INTER_FRAME_GAP(ETH_InitStruct->ETH_InterFrameGap));
  270. assert_param(IS_ETH_CARRIER_SENSE(ETH_InitStruct->ETH_CarrierSense));
  271. assert_param(IS_ETH_SPEED(ETH_InitStruct->ETH_Speed));
  272. assert_param(IS_ETH_RECEIVE_OWN(ETH_InitStruct->ETH_ReceiveOwn));
  273. assert_param(IS_ETH_LOOPBACK_MODE(ETH_InitStruct->ETH_LoopbackMode));
  274. assert_param(IS_ETH_DUPLEX_MODE(ETH_InitStruct->ETH_Mode));
  275. assert_param(IS_ETH_CHECKSUM_OFFLOAD(ETH_InitStruct->ETH_ChecksumOffload));
  276. assert_param(IS_ETH_RETRY_TRANSMISSION(ETH_InitStruct->ETH_RetryTransmission));
  277. assert_param(IS_ETH_AUTOMATIC_PADCRC_STRIP(ETH_InitStruct->ETH_AutomaticPadCRCStrip));
  278. assert_param(IS_ETH_BACKOFF_LIMIT(ETH_InitStruct->ETH_BackOffLimit));
  279. assert_param(IS_ETH_DEFERRAL_CHECK(ETH_InitStruct->ETH_DeferralCheck));
  280. assert_param(IS_ETH_RECEIVE_ALL(ETH_InitStruct->ETH_ReceiveAll));
  281. assert_param(IS_ETH_SOURCE_ADDR_FILTER(ETH_InitStruct->ETH_SourceAddrFilter));
  282. assert_param(IS_ETH_CONTROL_FRAMES(ETH_InitStruct->ETH_PassControlFrames));
  283. assert_param(IS_ETH_BROADCAST_FRAMES_RECEPTION(ETH_InitStruct->ETH_BroadcastFramesReception));
  284. assert_param(IS_ETH_DESTINATION_ADDR_FILTER(ETH_InitStruct->ETH_DestinationAddrFilter));
  285. assert_param(IS_ETH_PROMISCIOUS_MODE(ETH_InitStruct->ETH_PromiscuousMode));
  286. assert_param(IS_ETH_MULTICAST_FRAMES_FILTER(ETH_InitStruct->ETH_MulticastFramesFilter));
  287. assert_param(IS_ETH_UNICAST_FRAMES_FILTER(ETH_InitStruct->ETH_UnicastFramesFilter));
  288. assert_param(IS_ETH_PAUSE_TIME(ETH_InitStruct->ETH_PauseTime));
  289. assert_param(IS_ETH_ZEROQUANTA_PAUSE(ETH_InitStruct->ETH_ZeroQuantaPause));
  290. assert_param(IS_ETH_PAUSE_LOW_THRESHOLD(ETH_InitStruct->ETH_PauseLowThreshold));
  291. assert_param(IS_ETH_UNICAST_PAUSE_FRAME_DETECT(ETH_InitStruct->ETH_UnicastPauseFrameDetect));
  292. assert_param(IS_ETH_RECEIVE_FLOWCONTROL(ETH_InitStruct->ETH_ReceiveFlowControl));
  293. assert_param(IS_ETH_TRANSMIT_FLOWCONTROL(ETH_InitStruct->ETH_TransmitFlowControl));
  294. assert_param(IS_ETH_VLAN_TAG_COMPARISON(ETH_InitStruct->ETH_VLANTagComparison));
  295. assert_param(IS_ETH_VLAN_TAG_IDENTIFIER(ETH_InitStruct->ETH_VLANTagIdentifier));
  296. /* DMA --------------------------*/
  297. assert_param(IS_ETH_DROP_TCPIP_CHECKSUM_FRAME(ETH_InitStruct->ETH_DropTCPIPChecksumErrorFrame));
  298. assert_param(IS_ETH_RECEIVE_STORE_FORWARD(ETH_InitStruct->ETH_ReceiveStoreForward));
  299. assert_param(IS_ETH_FLUSH_RECEIVE_FRAME(ETH_InitStruct->ETH_FlushReceivedFrame));
  300. assert_param(IS_ETH_TRANSMIT_STORE_FORWARD(ETH_InitStruct->ETH_TransmitStoreForward));
  301. assert_param(IS_ETH_TRANSMIT_THRESHOLD_CONTROL(ETH_InitStruct->ETH_TransmitThresholdControl));
  302. assert_param(IS_ETH_FORWARD_ERROR_FRAMES(ETH_InitStruct->ETH_ForwardErrorFrames));
  303. assert_param(IS_ETH_FORWARD_UNDERSIZED_GOOD_FRAMES(ETH_InitStruct->ETH_ForwardUndersizedGoodFrames));
  304. assert_param(IS_ETH_RECEIVE_THRESHOLD_CONTROL(ETH_InitStruct->ETH_ReceiveThresholdControl));
  305. assert_param(IS_ETH_SECOND_FRAME_OPERATE(ETH_InitStruct->ETH_SecondFrameOperate));
  306. assert_param(IS_ETH_ADDRESS_ALIGNED_BEATS(ETH_InitStruct->ETH_AddressAlignedBeats));
  307. assert_param(IS_ETH_FIXED_BURST(ETH_InitStruct->ETH_FixedBurst));
  308. assert_param(IS_ETH_RXDMA_BURST_LENGTH(ETH_InitStruct->ETH_RxDMABurstLength));
  309. assert_param(IS_ETH_TXDMA_BURST_LENGTH(ETH_InitStruct->ETH_TxDMABurstLength));
  310. assert_param(IS_ETH_DMA_DESC_SKIP_LENGTH(ETH_InitStruct->ETH_DescriptorSkipLength));
  311. assert_param(IS_ETH_DMA_ARBITRATION_ROUNDROBIN_RXTX(ETH_InitStruct->ETH_DMAArbitration));
  312. /*-------------------------------- MAC Config ------------------------------*/
  313. /*---------------------- ETHERNET MACMIIAR Configuration -------------------*/
  314. /* Get the ETHERNET MACMIIAR value */
  315. tmpreg = ETH->MACMIIAR;
  316. /* Clear CSR Clock Range CR[2:0] bits */
  317. tmpreg &= MACMIIAR_CR_MASK;
  318. /* Get hclk frequency value */
  319. RCC_GetClocksFreq(&rcc_clocks);
  320. hclk = rcc_clocks.HCLK_Frequency;
  321. /* Set CR bits depending on hclk value */
  322. if((hclk >= 20000000)&&(hclk < 35000000))
  323. {
  324. /* CSR Clock Range between 20-35 MHz */
  325. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div16;
  326. }
  327. else if((hclk >= 35000000)&&(hclk < 60000000))
  328. {
  329. /* CSR Clock Range between 35-60 MHz */
  330. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div26;
  331. }
  332. else if((hclk >= 60000000)&&(hclk < 100000000))
  333. {
  334. /* CSR Clock Range between 60-100 MHz */
  335. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div42;
  336. }
  337. else if((hclk >= 100000000)&&(hclk < 150000000))
  338. {
  339. /* CSR Clock Range between 100-150 MHz */
  340. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div62;
  341. }
  342. else /* ((hclk >= 150000000)&&(hclk <= 168000000)) */
  343. {
  344. /* CSR Clock Range between 150-168 MHz */
  345. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div102;
  346. }
  347. /* Write to ETHERNET MAC MIIAR: Configure the ETHERNET CSR Clock Range */
  348. ETH->MACMIIAR = (uint32_t)tmpreg;
  349. /*-------------------- PHY initialization and configuration ----------------*/
  350. /* Put the PHY in reset mode */
  351. if(!(ETH_WritePHYRegister(PHYAddress, PHY_BCR, PHY_Reset)))
  352. {
  353. /* Return ERROR in case of write timeout */
  354. return ETH_ERROR;
  355. }
  356. /* Delay to assure PHY reset */
  357. _eth_delay_(PHY_RESET_DELAY);
  358. if(ETH_InitStruct->ETH_AutoNegotiation != ETH_AutoNegotiation_Disable)
  359. {
  360. /* We wait for linked status... */
  361. do
  362. {
  363. timeout++;
  364. } while (!(ETH_ReadPHYRegister(PHYAddress, PHY_BSR) & PHY_Linked_Status) && (timeout < PHY_READ_TO));
  365. /* Return ERROR in case of timeout */
  366. if(timeout == PHY_READ_TO)
  367. {
  368. return ETH_ERROR;
  369. }
  370. /* Reset Timeout counter */
  371. timeout = 0;
  372. /* Enable Auto-Negotiation */
  373. if(!(ETH_WritePHYRegister(PHYAddress, PHY_BCR, PHY_AutoNegotiation)))
  374. {
  375. /* Return ERROR in case of write timeout */
  376. return ETH_ERROR;
  377. }
  378. /* Wait until the auto-negotiation will be completed */
  379. do
  380. {
  381. timeout++;
  382. } while (!(ETH_ReadPHYRegister(PHYAddress, PHY_BSR) & PHY_AutoNego_Complete) && (timeout < (uint32_t)PHY_READ_TO));
  383. /* Return ERROR in case of timeout */
  384. if(timeout == PHY_READ_TO)
  385. {
  386. return ETH_ERROR;
  387. }
  388. /* Reset Timeout counter */
  389. timeout = 0;
  390. /* Read the result of the auto-negotiation */
  391. RegValue = ETH_ReadPHYRegister(PHYAddress, PHY_SR);
  392. switch (RegValue & PHY_DUPLEX_SPEED_STATUS_MASK)
  393. {
  394. case PHY_100BTX_FULL:
  395. ETH_InitStruct->ETH_Mode = ETH_Mode_FullDuplex;
  396. ETH_InitStruct->ETH_Speed = ETH_Speed_100M;
  397. break;
  398. case PHY_100BTX_HALF:
  399. ETH_InitStruct->ETH_Mode = ETH_Mode_HalfDuplex;
  400. ETH_InitStruct->ETH_Speed = ETH_Speed_100M;
  401. break;
  402. case PHY_10M_FULL:
  403. ETH_InitStruct->ETH_Mode = ETH_Mode_FullDuplex;
  404. ETH_InitStruct->ETH_Speed = ETH_Speed_10M;
  405. break;
  406. case PHY_10M_HALF:
  407. ETH_InitStruct->ETH_Mode = ETH_Mode_HalfDuplex;
  408. ETH_InitStruct->ETH_Speed = ETH_Speed_10M;
  409. break;
  410. default:
  411. break;
  412. }
  413. }
  414. else
  415. {
  416. if(!ETH_WritePHYRegister(PHYAddress, PHY_BCR, ((uint16_t)(ETH_InitStruct->ETH_Mode >> 3) |
  417. (uint16_t)(ETH_InitStruct->ETH_Speed >> 1))))
  418. {
  419. /* Return ERROR in case of write timeout */
  420. return ETH_ERROR;
  421. }
  422. /* Delay to assure PHY configuration */
  423. _eth_delay_(PHY_CONFIG_DELAY);
  424. }
  425. /*------------------------ ETHERNET MACCR Configuration --------------------*/
  426. /* Get the ETHERNET MACCR value */
  427. tmpreg = ETH->MACCR;
  428. /* Clear WD, PCE, PS, TE and RE bits */
  429. tmpreg &= MACCR_CLEAR_MASK;
  430. /* Set the WD bit according to ETH_Watchdog value */
  431. /* Set the JD: bit according to ETH_Jabber value */
  432. /* Set the IFG bit according to ETH_InterFrameGap value */
  433. /* Set the DCRS bit according to ETH_CarrierSense value */
  434. /* Set the FES bit according to ETH_Speed value */
  435. /* Set the DO bit according to ETH_ReceiveOwn value */
  436. /* Set the LM bit according to ETH_LoopbackMode value */
  437. /* Set the DM bit according to ETH_Mode value */
  438. /* Set the IPCO bit according to ETH_ChecksumOffload value */
  439. /* Set the DR bit according to ETH_RetryTransmission value */
  440. /* Set the ACS bit according to ETH_AutomaticPadCRCStrip value */
  441. /* Set the BL bit according to ETH_BackOffLimit value */
  442. /* Set the DC bit according to ETH_DeferralCheck value */
  443. tmpreg |= (uint32_t)(ETH_InitStruct->ETH_Watchdog |
  444. ETH_InitStruct->ETH_Jabber |
  445. ETH_InitStruct->ETH_InterFrameGap |
  446. ETH_InitStruct->ETH_CarrierSense |
  447. ETH_InitStruct->ETH_Speed |
  448. ETH_InitStruct->ETH_ReceiveOwn |
  449. ETH_InitStruct->ETH_LoopbackMode |
  450. ETH_InitStruct->ETH_Mode |
  451. ETH_InitStruct->ETH_ChecksumOffload |
  452. ETH_InitStruct->ETH_RetryTransmission |
  453. ETH_InitStruct->ETH_AutomaticPadCRCStrip |
  454. ETH_InitStruct->ETH_BackOffLimit |
  455. ETH_InitStruct->ETH_DeferralCheck);
  456. /* Write to ETHERNET MACCR */
  457. ETH->MACCR = (uint32_t)tmpreg;
  458. /*----------------------- ETHERNET MACFFR Configuration --------------------*/
  459. /* Set the RA bit according to ETH_ReceiveAll value */
  460. /* Set the SAF and SAIF bits according to ETH_SourceAddrFilter value */
  461. /* Set the PCF bit according to ETH_PassControlFrames value */
  462. /* Set the DBF bit according to ETH_BroadcastFramesReception value */
  463. /* Set the DAIF bit according to ETH_DestinationAddrFilter value */
  464. /* Set the PR bit according to ETH_PromiscuousMode value */
  465. /* Set the PM, HMC and HPF bits according to ETH_MulticastFramesFilter value */
  466. /* Set the HUC and HPF bits according to ETH_UnicastFramesFilter value */
  467. /* Write to ETHERNET MACFFR */
  468. ETH->MACFFR = (uint32_t)(ETH_InitStruct->ETH_ReceiveAll |
  469. ETH_InitStruct->ETH_SourceAddrFilter |
  470. ETH_InitStruct->ETH_PassControlFrames |
  471. ETH_InitStruct->ETH_BroadcastFramesReception |
  472. ETH_InitStruct->ETH_DestinationAddrFilter |
  473. ETH_InitStruct->ETH_PromiscuousMode |
  474. ETH_InitStruct->ETH_MulticastFramesFilter |
  475. ETH_InitStruct->ETH_UnicastFramesFilter);
  476. /*--------------- ETHERNET MACHTHR and MACHTLR Configuration ---------------*/
  477. /* Write to ETHERNET MACHTHR */
  478. ETH->MACHTHR = (uint32_t)ETH_InitStruct->ETH_HashTableHigh;
  479. /* Write to ETHERNET MACHTLR */
  480. ETH->MACHTLR = (uint32_t)ETH_InitStruct->ETH_HashTableLow;
  481. /*----------------------- ETHERNET MACFCR Configuration --------------------*/
  482. /* Get the ETHERNET MACFCR value */
  483. tmpreg = ETH->MACFCR;
  484. /* Clear xx bits */
  485. tmpreg &= MACFCR_CLEAR_MASK;
  486. /* Set the PT bit according to ETH_PauseTime value */
  487. /* Set the DZPQ bit according to ETH_ZeroQuantaPause value */
  488. /* Set the PLT bit according to ETH_PauseLowThreshold value */
  489. /* Set the UP bit according to ETH_UnicastPauseFrameDetect value */
  490. /* Set the RFE bit according to ETH_ReceiveFlowControl value */
  491. /* Set the TFE bit according to ETH_TransmitFlowControl value */
  492. tmpreg |= (uint32_t)((ETH_InitStruct->ETH_PauseTime << 16) |
  493. ETH_InitStruct->ETH_ZeroQuantaPause |
  494. ETH_InitStruct->ETH_PauseLowThreshold |
  495. ETH_InitStruct->ETH_UnicastPauseFrameDetect |
  496. ETH_InitStruct->ETH_ReceiveFlowControl |
  497. ETH_InitStruct->ETH_TransmitFlowControl);
  498. /* Write to ETHERNET MACFCR */
  499. ETH->MACFCR = (uint32_t)tmpreg;
  500. /*----------------------- ETHERNET MACVLANTR Configuration -----------------*/
  501. /* Set the ETV bit according to ETH_VLANTagComparison value */
  502. /* Set the VL bit according to ETH_VLANTagIdentifier value */
  503. ETH->MACVLANTR = (uint32_t)(ETH_InitStruct->ETH_VLANTagComparison |
  504. ETH_InitStruct->ETH_VLANTagIdentifier);
  505. /*-------------------------------- DMA Config ------------------------------*/
  506. /*----------------------- ETHERNET DMAOMR Configuration --------------------*/
  507. /* Get the ETHERNET DMAOMR value */
  508. tmpreg = ETH->DMAOMR;
  509. /* Clear xx bits */
  510. tmpreg &= DMAOMR_CLEAR_MASK;
  511. /* Set the DT bit according to ETH_DropTCPIPChecksumErrorFrame value */
  512. /* Set the RSF bit according to ETH_ReceiveStoreForward value */
  513. /* Set the DFF bit according to ETH_FlushReceivedFrame value */
  514. /* Set the TSF bit according to ETH_TransmitStoreForward value */
  515. /* Set the TTC bit according to ETH_TransmitThresholdControl value */
  516. /* Set the FEF bit according to ETH_ForwardErrorFrames value */
  517. /* Set the FUF bit according to ETH_ForwardUndersizedGoodFrames value */
  518. /* Set the RTC bit according to ETH_ReceiveThresholdControl value */
  519. /* Set the OSF bit according to ETH_SecondFrameOperate value */
  520. tmpreg |= (uint32_t)(ETH_InitStruct->ETH_DropTCPIPChecksumErrorFrame |
  521. ETH_InitStruct->ETH_ReceiveStoreForward |
  522. ETH_InitStruct->ETH_FlushReceivedFrame |
  523. ETH_InitStruct->ETH_TransmitStoreForward |
  524. ETH_InitStruct->ETH_TransmitThresholdControl |
  525. ETH_InitStruct->ETH_ForwardErrorFrames |
  526. ETH_InitStruct->ETH_ForwardUndersizedGoodFrames |
  527. ETH_InitStruct->ETH_ReceiveThresholdControl |
  528. ETH_InitStruct->ETH_SecondFrameOperate);
  529. /* Write to ETHERNET DMAOMR */
  530. ETH->DMAOMR = (uint32_t)tmpreg;
  531. /*----------------------- ETHERNET DMABMR Configuration --------------------*/
  532. /* Set the AAL bit according to ETH_AddressAlignedBeats value */
  533. /* Set the FB bit according to ETH_FixedBurst value */
  534. /* Set the RPBL and 4*PBL bits according to ETH_RxDMABurstLength value */
  535. /* Set the PBL and 4*PBL bits according to ETH_TxDMABurstLength value */
  536. /* Set the DSL bit according to ETH_DesciptorSkipLength value */
  537. /* Set the PR and DA bits according to ETH_DMAArbitration value */
  538. ETH->DMABMR = (uint32_t)(ETH_InitStruct->ETH_AddressAlignedBeats |
  539. ETH_InitStruct->ETH_FixedBurst |
  540. ETH_InitStruct->ETH_RxDMABurstLength | /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */
  541. ETH_InitStruct->ETH_TxDMABurstLength |
  542. (ETH_InitStruct->ETH_DescriptorSkipLength << 2) |
  543. ETH_InitStruct->ETH_DMAArbitration |
  544. ETH_DMABMR_USP); /* Enable use of separate PBL for Rx and Tx */
  545. #ifdef USE_ENHANCED_DMA_DESCRIPTORS
  546. /* Enable the Enhanced DMA descriptors */
  547. ETH->DMABMR |= ETH_DMABMR_EDE;
  548. #endif /* USE_ENHANCED_DMA_DESCRIPTORS */
  549. /* Return Ethernet configuration success */
  550. return ETH_SUCCESS;
  551. }
  552. /**
  553. * @brief Enables ENET MAC and DMA reception/transmission
  554. * @param None
  555. * @retval None
  556. */
  557. void ETH_Start(void)
  558. {
  559. /* Enable transmit state machine of the MAC for transmission on the MII */
  560. ETH_MACTransmissionCmd(ENABLE);
  561. /* Flush Transmit FIFO */
  562. ETH_FlushTransmitFIFO();
  563. /* Enable receive state machine of the MAC for reception from the MII */
  564. ETH_MACReceptionCmd(ENABLE);
  565. /* Start DMA transmission */
  566. ETH_DMATransmissionCmd(ENABLE);
  567. /* Start DMA reception */
  568. ETH_DMAReceptionCmd(ENABLE);
  569. }
  570. /**
  571. * @brief Enables or disables the MAC transmission.
  572. * @param NewState: new state of the MAC transmission.
  573. * This parameter can be: ENABLE or DISABLE.
  574. * @retval None
  575. */
  576. void ETH_MACTransmissionCmd(FunctionalState NewState)
  577. {
  578. /* Check the parameters */
  579. assert_param(IS_FUNCTIONAL_STATE(NewState));
  580. if (NewState != DISABLE)
  581. {
  582. /* Enable the MAC transmission */
  583. ETH->MACCR |= ETH_MACCR_TE;
  584. }
  585. else
  586. {
  587. /* Disable the MAC transmission */
  588. ETH->MACCR &= ~ETH_MACCR_TE;
  589. }
  590. }
  591. /**
  592. * @brief Enables or disables the MAC reception.
  593. * @param NewState: new state of the MAC reception.
  594. * This parameter can be: ENABLE or DISABLE.
  595. * @retval None
  596. */
  597. void ETH_MACReceptionCmd(FunctionalState NewState)
  598. {
  599. /* Check the parameters */
  600. assert_param(IS_FUNCTIONAL_STATE(NewState));
  601. if (NewState != DISABLE)
  602. {
  603. /* Enable the MAC reception */
  604. ETH->MACCR |= ETH_MACCR_RE;
  605. }
  606. else
  607. {
  608. /* Disable the MAC reception */
  609. ETH->MACCR &= ~ETH_MACCR_RE;
  610. }
  611. }
  612. /**
  613. * @brief Checks whether the ETHERNET flow control busy bit is set or not.
  614. * @param None
  615. * @retval The new state of flow control busy status bit (SET or RESET).
  616. */
  617. FlagStatus ETH_GetFlowControlBusyStatus(void)
  618. {
  619. FlagStatus bitstatus = RESET;
  620. /* The Flow Control register should not be written to until this bit is cleared */
  621. if ((ETH->MACFCR & ETH_MACFCR_FCBBPA) != (uint32_t)RESET)
  622. {
  623. bitstatus = SET;
  624. }
  625. else
  626. {
  627. bitstatus = RESET;
  628. }
  629. return bitstatus;
  630. }
  631. /**
  632. * @brief Initiate a Pause Control Frame (Full-duplex only).
  633. * @param None
  634. * @retval None
  635. */
  636. void ETH_InitiatePauseControlFrame(void)
  637. {
  638. /* When Set In full duplex MAC initiates pause control frame */
  639. ETH->MACFCR |= ETH_MACFCR_FCBBPA;
  640. }
  641. /**
  642. * @brief Enables or disables the MAC BackPressure operation activation (Half-duplex only).
  643. * @param NewState: new state of the MAC BackPressure operation activation.
  644. * This parameter can be: ENABLE or DISABLE.
  645. * @retval None
  646. */
  647. void ETH_BackPressureActivationCmd(FunctionalState NewState)
  648. {
  649. /* Check the parameters */
  650. assert_param(IS_FUNCTIONAL_STATE(NewState));
  651. if (NewState != DISABLE)
  652. {
  653. /* Activate the MAC BackPressure operation */
  654. /* In Half duplex: during backpressure, when the MAC receives a new frame,
  655. the transmitter starts sending a JAM pattern resulting in a collision */
  656. ETH->MACFCR |= ETH_MACFCR_FCBBPA;
  657. }
  658. else
  659. {
  660. /* Desactivate the MAC BackPressure operation */
  661. ETH->MACFCR &= ~ETH_MACFCR_FCBBPA;
  662. }
  663. }
  664. /**
  665. * @brief Checks whether the specified ETHERNET MAC flag is set or not.
  666. * @param ETH_MAC_FLAG: specifies the flag to check.
  667. * This parameter can be one of the following values:
  668. * @arg ETH_MAC_FLAG_TST : Time stamp trigger flag
  669. * @arg ETH_MAC_FLAG_MMCT : MMC transmit flag
  670. * @arg ETH_MAC_FLAG_MMCR : MMC receive flag
  671. * @arg ETH_MAC_FLAG_MMC : MMC flag
  672. * @arg ETH_MAC_FLAG_PMT : PMT flag
  673. * @retval The new state of ETHERNET MAC flag (SET or RESET).
  674. */
  675. FlagStatus ETH_GetMACFlagStatus(uint32_t ETH_MAC_FLAG)
  676. {
  677. FlagStatus bitstatus = RESET;
  678. /* Check the parameters */
  679. assert_param(IS_ETH_MAC_GET_FLAG(ETH_MAC_FLAG));
  680. if ((ETH->MACSR & ETH_MAC_FLAG) != (uint32_t)RESET)
  681. {
  682. bitstatus = SET;
  683. }
  684. else
  685. {
  686. bitstatus = RESET;
  687. }
  688. return bitstatus;
  689. }
  690. /**
  691. * @brief Checks whether the specified ETHERNET MAC interrupt has occurred or not.
  692. * @param ETH_MAC_IT: specifies the interrupt source to check.
  693. * This parameter can be one of the following values:
  694. * @arg ETH_MAC_IT_TST : Time stamp trigger interrupt
  695. * @arg ETH_MAC_IT_MMCT : MMC transmit interrupt
  696. * @arg ETH_MAC_IT_MMCR : MMC receive interrupt
  697. * @arg ETH_MAC_IT_MMC : MMC interrupt
  698. * @arg ETH_MAC_IT_PMT : PMT interrupt
  699. * @retval The new state of ETHERNET MAC interrupt (SET or RESET).
  700. */
  701. ITStatus ETH_GetMACITStatus(uint32_t ETH_MAC_IT)
  702. {
  703. ITStatus bitstatus = RESET;
  704. /* Check the parameters */
  705. assert_param(IS_ETH_MAC_GET_IT(ETH_MAC_IT));
  706. if ((ETH->MACSR & ETH_MAC_IT) != (uint32_t)RESET)
  707. {
  708. bitstatus = SET;
  709. }
  710. else
  711. {
  712. bitstatus = RESET;
  713. }
  714. return bitstatus;
  715. }
  716. /**
  717. * @brief Enables or disables the specified ETHERNET MAC interrupts.
  718. * @param ETH_MAC_IT: specifies the ETHERNET MAC interrupt sources to be
  719. * enabled or disabled.
  720. * This parameter can be any combination of the following values:
  721. * @arg ETH_MAC_IT_TST : Time stamp trigger interrupt
  722. * @arg ETH_MAC_IT_PMT : PMT interrupt
  723. * @param NewState: new state of the specified ETHERNET MAC interrupts.
  724. * This parameter can be: ENABLE or DISABLE.
  725. * @retval None
  726. */
  727. void ETH_MACITConfig(uint32_t ETH_MAC_IT, FunctionalState NewState)
  728. {
  729. /* Check the parameters */
  730. assert_param(IS_ETH_MAC_IT(ETH_MAC_IT));
  731. assert_param(IS_FUNCTIONAL_STATE(NewState));
  732. if (NewState != DISABLE)
  733. {
  734. /* Enable the selected ETHERNET MAC interrupts */
  735. ETH->MACIMR &= (~(uint32_t)ETH_MAC_IT);
  736. }
  737. else
  738. {
  739. /* Disable the selected ETHERNET MAC interrupts */
  740. ETH->MACIMR |= ETH_MAC_IT;
  741. }
  742. }
  743. /**
  744. * @brief Configures the selected MAC address.
  745. * @param MacAddr: The MAC address to configure.
  746. * This parameter can be one of the following values:
  747. * @arg ETH_MAC_Address0 : MAC Address0
  748. * @arg ETH_MAC_Address1 : MAC Address1
  749. * @arg ETH_MAC_Address2 : MAC Address2
  750. * @arg ETH_MAC_Address3 : MAC Address3
  751. * @param Addr: Pointer on MAC address buffer data (6 bytes).
  752. * @retval None
  753. */
  754. void ETH_MACAddressConfig(uint32_t MacAddr, uint8_t *Addr)
  755. {
  756. uint32_t tmpreg;
  757. /* Check the parameters */
  758. assert_param(IS_ETH_MAC_ADDRESS0123(MacAddr));
  759. /* Calculate the selected MAC address high register */
  760. tmpreg = ((uint32_t)Addr[5] << 8) | (uint32_t)Addr[4];
  761. /* Load the selected MAC address high register */
  762. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) = tmpreg;
  763. /* Calculate the selected MAC address low register */
  764. tmpreg = ((uint32_t)Addr[3] << 24) | ((uint32_t)Addr[2] << 16) | ((uint32_t)Addr[1] << 8) | Addr[0];
  765. /* Load the selected MAC address low register */
  766. (*(__IO uint32_t *) (ETH_MAC_ADDR_LBASE + MacAddr)) = tmpreg;
  767. }
  768. /**
  769. * @brief Get the selected MAC address.
  770. * @param MacAddr: The MAC address to return.
  771. * This parameter can be one of the following values:
  772. * @arg ETH_MAC_Address0 : MAC Address0
  773. * @arg ETH_MAC_Address1 : MAC Address1
  774. * @arg ETH_MAC_Address2 : MAC Address2
  775. * @arg ETH_MAC_Address3 : MAC Address3
  776. * @param Addr: Pointer on MAC address buffer data (6 bytes).
  777. * @retval None
  778. */
  779. void ETH_GetMACAddress(uint32_t MacAddr, uint8_t *Addr)
  780. {
  781. uint32_t tmpreg;
  782. /* Check the parameters */
  783. assert_param(IS_ETH_MAC_ADDRESS0123(MacAddr));
  784. /* Get the selected MAC address high register */
  785. tmpreg =(*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr));
  786. /* Calculate the selected MAC address buffer */
  787. Addr[5] = ((tmpreg >> 8) & (uint8_t)0xFF);
  788. Addr[4] = (tmpreg & (uint8_t)0xFF);
  789. /* Load the selected MAC address low register */
  790. tmpreg =(*(__IO uint32_t *) (ETH_MAC_ADDR_LBASE + MacAddr));
  791. /* Calculate the selected MAC address buffer */
  792. Addr[3] = ((tmpreg >> 24) & (uint8_t)0xFF);
  793. Addr[2] = ((tmpreg >> 16) & (uint8_t)0xFF);
  794. Addr[1] = ((tmpreg >> 8 ) & (uint8_t)0xFF);
  795. Addr[0] = (tmpreg & (uint8_t)0xFF);
  796. }
  797. /**
  798. * @brief Enables or disables the Address filter module uses the specified
  799. * ETHERNET MAC address for perfect filtering
  800. * @param MacAddr: specifies the ETHERNET MAC address to be used for perfect filtering.
  801. * This parameter can be one of the following values:
  802. * @arg ETH_MAC_Address1 : MAC Address1
  803. * @arg ETH_MAC_Address2 : MAC Address2
  804. * @arg ETH_MAC_Address3 : MAC Address3
  805. * @param NewState: new state of the specified ETHERNET MAC address use.
  806. * This parameter can be: ENABLE or DISABLE.
  807. * @retval None
  808. */
  809. void ETH_MACAddressPerfectFilterCmd(uint32_t MacAddr, FunctionalState NewState)
  810. {
  811. /* Check the parameters */
  812. assert_param(IS_ETH_MAC_ADDRESS123(MacAddr));
  813. assert_param(IS_FUNCTIONAL_STATE(NewState));
  814. if (NewState != DISABLE)
  815. {
  816. /* Enable the selected ETHERNET MAC address for perfect filtering */
  817. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) |= ETH_MACA1HR_AE;
  818. }
  819. else
  820. {
  821. /* Disable the selected ETHERNET MAC address for perfect filtering */
  822. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) &=(~(uint32_t)ETH_MACA1HR_AE);
  823. }
  824. }
  825. /**
  826. * @brief Set the filter type for the specified ETHERNET MAC address
  827. * @param MacAddr: specifies the ETHERNET MAC address
  828. * This parameter can be one of the following values:
  829. * @arg ETH_MAC_Address1 : MAC Address1
  830. * @arg ETH_MAC_Address2 : MAC Address2
  831. * @arg ETH_MAC_Address3 : MAC Address3
  832. * @param Filter: specifies the used frame received field for comparison
  833. * This parameter can be one of the following values:
  834. * @arg ETH_MAC_AddressFilter_SA : MAC Address is used to compare with the
  835. * SA fields of the received frame.
  836. * @arg ETH_MAC_AddressFilter_DA : MAC Address is used to compare with the
  837. * DA fields of the received frame.
  838. * @retval None
  839. */
  840. void ETH_MACAddressFilterConfig(uint32_t MacAddr, uint32_t Filter)
  841. {
  842. /* Check the parameters */
  843. assert_param(IS_ETH_MAC_ADDRESS123(MacAddr));
  844. assert_param(IS_ETH_MAC_ADDRESS_FILTER(Filter));
  845. if (Filter != ETH_MAC_AddressFilter_DA)
  846. {
  847. /* The selected ETHERNET MAC address is used to compare with the SA fields of the
  848. received frame. */
  849. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) |= ETH_MACA1HR_SA;
  850. }
  851. else
  852. {
  853. /* The selected ETHERNET MAC address is used to compare with the DA fields of the
  854. received frame. */
  855. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) &=(~(uint32_t)ETH_MACA1HR_SA);
  856. }
  857. }
  858. /**
  859. * @brief Set the filter type for the specified ETHERNET MAC address
  860. * @param MacAddr: specifies the ETHERNET MAC address
  861. * This parameter can be one of the following values:
  862. * @arg ETH_MAC_Address1 : MAC Address1
  863. * @arg ETH_MAC_Address2 : MAC Address2
  864. * @arg ETH_MAC_Address3 : MAC Address3
  865. * @param MaskByte: specifies the used address bytes for comparison
  866. * This parameter can be any combination of the following values:
  867. * @arg ETH_MAC_AddressMask_Byte6 : Mask MAC Address high reg bits [15:8].
  868. * @arg ETH_MAC_AddressMask_Byte5 : Mask MAC Address high reg bits [7:0].
  869. * @arg ETH_MAC_AddressMask_Byte4 : Mask MAC Address low reg bits [31:24].
  870. * @arg ETH_MAC_AddressMask_Byte3 : Mask MAC Address low reg bits [23:16].
  871. * @arg ETH_MAC_AddressMask_Byte2 : Mask MAC Address low reg bits [15:8].
  872. * @arg ETH_MAC_AddressMask_Byte1 : Mask MAC Address low reg bits [7:0].
  873. * @retval None
  874. */
  875. void ETH_MACAddressMaskBytesFilterConfig(uint32_t MacAddr, uint32_t MaskByte)
  876. {
  877. /* Check the parameters */
  878. assert_param(IS_ETH_MAC_ADDRESS123(MacAddr));
  879. assert_param(IS_ETH_MAC_ADDRESS_MASK(MaskByte));
  880. /* Clear MBC bits in the selected MAC address high register */
  881. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) &=(~(uint32_t)ETH_MACA1HR_MBC);
  882. /* Set the selected Filter mask bytes */
  883. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) |= MaskByte;
  884. }
  885. /******************************************************************************/
  886. /* DMA Descriptors functions */
  887. /******************************************************************************/
  888. /**
  889. * @brief This function should be called to get the received frame (to be used
  890. * with polling method only).
  891. * @param none
  892. * @retval Structure of type FrameTypeDef
  893. */
  894. FrameTypeDef ETH_Get_Received_Frame(void)
  895. {
  896. uint32_t framelength = 0;
  897. FrameTypeDef frame = {0,0,0};
  898. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  899. framelength = ((DMARxDescToGet->Status & ETH_DMARxDesc_FL) >> ETH_DMARxDesc_FrameLengthShift) - 4;
  900. frame.length = framelength;
  901. /* Get the address of the buffer start address */
  902. /* Check if more than one segment in the frame */
  903. if (DMA_RX_FRAME_infos->Seg_Count >1)
  904. {
  905. frame.buffer =(DMA_RX_FRAME_infos->FS_Rx_Desc)->Buffer1Addr;
  906. }
  907. else
  908. {
  909. frame.buffer = DMARxDescToGet->Buffer1Addr;
  910. }
  911. frame.descriptor = DMARxDescToGet;
  912. /* Update the ETHERNET DMA global Rx descriptor with next Rx descriptor */
  913. /* Chained Mode */
  914. /* Selects the next DMA Rx descriptor list for next buffer to read */
  915. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  916. /* Return Frame */
  917. return (frame);
  918. }
  919. /**
  920. * @brief This function should be called when a frame is received using DMA
  921. * Receive interrupt, it allows scanning of Rx descriptors to get the
  922. * the receive frame (should be used with interrupt mode only)
  923. * @param None
  924. * @retval Structure of type FrameTypeDef
  925. */
  926. FrameTypeDef ETH_Get_Received_Frame_interrupt(void)
  927. {
  928. FrameTypeDef frame={0,0,0};
  929. __IO uint32_t descriptor_scan_counter = 0;
  930. /* scan descriptors owned by CPU */
  931. while (((DMARxDescToGet->Status & ETH_DMARxDesc_OWN) == (uint32_t)RESET)&&
  932. (descriptor_scan_counter<ETH_RXBUFNB))
  933. {
  934. /* Just by security */
  935. descriptor_scan_counter++;
  936. /* check if first segment in frame */
  937. if(((DMARxDescToGet->Status & ETH_DMARxDesc_FS) != (uint32_t)RESET)&&
  938. ((DMARxDescToGet->Status & ETH_DMARxDesc_LS) == (uint32_t)RESET))
  939. {
  940. DMA_RX_FRAME_infos->FS_Rx_Desc = DMARxDescToGet;
  941. DMA_RX_FRAME_infos->Seg_Count = 1;
  942. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  943. }
  944. /* check if intermediate segment */
  945. else if (((DMARxDescToGet->Status & ETH_DMARxDesc_LS) == (uint32_t)RESET)&&
  946. ((DMARxDescToGet->Status & ETH_DMARxDesc_FS) == (uint32_t)RESET))
  947. {
  948. (DMA_RX_FRAME_infos->Seg_Count) ++;
  949. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  950. }
  951. /* should be last segment */
  952. else
  953. {
  954. /* last segment */
  955. DMA_RX_FRAME_infos->LS_Rx_Desc = DMARxDescToGet;
  956. (DMA_RX_FRAME_infos->Seg_Count)++;
  957. /* first segment is last segment */
  958. if ((DMA_RX_FRAME_infos->Seg_Count)==1)
  959. DMA_RX_FRAME_infos->FS_Rx_Desc = DMARxDescToGet;
  960. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  961. frame.length = ((DMARxDescToGet->Status & ETH_DMARxDesc_FL) >> ETH_DMARxDesc_FrameLengthShift) - 4;
  962. /* Get the address of the buffer start address */
  963. /* Check if more than one segment in the frame */
  964. if (DMA_RX_FRAME_infos->Seg_Count >1)
  965. {
  966. frame.buffer =(DMA_RX_FRAME_infos->FS_Rx_Desc)->Buffer1Addr;
  967. }
  968. else
  969. {
  970. frame.buffer = DMARxDescToGet->Buffer1Addr;
  971. }
  972. frame.descriptor = DMARxDescToGet;
  973. /* Update the ETHERNET DMA global Rx descriptor with next Rx descriptor */
  974. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  975. /* Return Frame */
  976. return (frame);
  977. }
  978. }
  979. return (frame);
  980. }
  981. /**
  982. * @brief Prepares DMA Tx descriptors to transmit an ethernet frame
  983. * @param FrameLength : length of the frame to send
  984. * @retval error status
  985. */
  986. uint32_t ETH_Prepare_Transmit_Descriptors(u16 FrameLength)
  987. {
  988. uint32_t buf_count =0, size=0,i=0;
  989. __IO ETH_DMADESCTypeDef *DMATxNextDesc;
  990. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  991. if((DMATxDescToSet->Status & ETH_DMATxDesc_OWN) != (u32)RESET)
  992. {
  993. /* Return ERROR: OWN bit set */
  994. return ETH_ERROR;
  995. }
  996. DMATxNextDesc = DMATxDescToSet;
  997. if (FrameLength > ETH_TX_BUF_SIZE)
  998. {
  999. buf_count = FrameLength/ETH_TX_BUF_SIZE;
  1000. if (FrameLength%ETH_TX_BUF_SIZE) buf_count++;
  1001. }
  1002. else buf_count =1;
  1003. if (buf_count ==1)
  1004. {
  1005. /*set LAST and FIRST segment */
  1006. DMATxDescToSet->Status |=ETH_DMATxDesc_FS|ETH_DMATxDesc_LS;
  1007. /* Set frame size */
  1008. DMATxDescToSet->ControlBufferSize = (FrameLength& ETH_DMATxDesc_TBS1);
  1009. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  1010. DMATxDescToSet->Status |= ETH_DMATxDesc_OWN;
  1011. DMATxDescToSet= (ETH_DMADESCTypeDef *)(DMATxDescToSet->Buffer2NextDescAddr);
  1012. }
  1013. else
  1014. {
  1015. for (i=0; i< buf_count; i++)
  1016. {
  1017. if (i==0)
  1018. {
  1019. /* Setting the first segment bit */
  1020. DMATxDescToSet->Status |= ETH_DMATxDesc_FS;
  1021. }
  1022. /* Program size */
  1023. DMATxNextDesc->ControlBufferSize = (ETH_TX_BUF_SIZE & ETH_DMATxDesc_TBS1);
  1024. if (i== (buf_count-1))
  1025. {
  1026. /* Setting the last segment bit */
  1027. DMATxNextDesc->Status |= ETH_DMATxDesc_LS;
  1028. size = FrameLength - (buf_count-1)*ETH_TX_BUF_SIZE;
  1029. DMATxNextDesc->ControlBufferSize = (size & ETH_DMATxDesc_TBS1);
  1030. }
  1031. /*give back descriptor to DMA */
  1032. DMATxNextDesc->Status |= ETH_DMATxDesc_OWN;
  1033. DMATxNextDesc = (ETH_DMADESCTypeDef *)(DMATxNextDesc->Buffer2NextDescAddr);
  1034. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  1035. }
  1036. DMATxDescToSet = DMATxNextDesc ;
  1037. }
  1038. /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
  1039. if ((ETH->DMASR & ETH_DMASR_TBUS) != (u32)RESET)
  1040. {
  1041. /* Clear TBUS ETHERNET DMA flag */
  1042. ETH->DMASR = ETH_DMASR_TBUS;
  1043. /* Resume DMA transmission*/
  1044. ETH->DMATPDR = 0;
  1045. }
  1046. /* Return SUCCESS */
  1047. return ETH_SUCCESS;
  1048. }
  1049. /**
  1050. * @brief Initializes the DMA Rx descriptors in chain mode.
  1051. * @param DMARxDescTab: Pointer on the first Rx desc list
  1052. * @param RxBuff: Pointer on the first RxBuffer list
  1053. * @param RxBuffCount: Number of the used Rx desc in the list
  1054. * @retval None
  1055. */
  1056. void ETH_DMARxDescChainInit(ETH_DMADESCTypeDef *DMARxDescTab, uint8_t *RxBuff, uint32_t RxBuffCount)
  1057. {
  1058. uint32_t i = 0;
  1059. ETH_DMADESCTypeDef *DMARxDesc;
  1060. /* Set the DMARxDescToGet pointer with the first one of the DMARxDescTab list */
  1061. DMARxDescToGet = DMARxDescTab;
  1062. /* Fill each DMARxDesc descriptor with the right values */
  1063. for(i=0; i < RxBuffCount; i++)
  1064. {
  1065. /* Get the pointer on the ith member of the Rx Desc list */
  1066. DMARxDesc = DMARxDescTab+i;
  1067. /* Set Own bit of the Rx descriptor Status */
  1068. DMARxDesc->Status = ETH_DMARxDesc_OWN;
  1069. /* Set Buffer1 size and Second Address Chained bit */
  1070. DMARxDesc->ControlBufferSize = ETH_DMARxDesc_RCH | (uint32_t)ETH_RX_BUF_SIZE;
  1071. /* Set Buffer1 address pointer */
  1072. DMARxDesc->Buffer1Addr = (uint32_t)(&RxBuff[i*ETH_RX_BUF_SIZE]);
  1073. /* Initialize the next descriptor with the Next Descriptor Polling Enable */
  1074. if(i < (RxBuffCount-1))
  1075. {
  1076. /* Set next descriptor address register with next descriptor base address */
  1077. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab+i+1);
  1078. }
  1079. else
  1080. {
  1081. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  1082. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab);
  1083. }
  1084. }
  1085. /* Set Receive Descriptor List Address Register */
  1086. ETH->DMARDLAR = (uint32_t) DMARxDescTab;
  1087. DMA_RX_FRAME_infos = &RX_Frame_Descriptor;
  1088. }
  1089. /**
  1090. * @brief This function polls for a frame reception
  1091. * @param None
  1092. * @retval Returns 1 when a frame is received, 0 if none.
  1093. */
  1094. uint32_t ETH_CheckFrameReceived(void)
  1095. {
  1096. /* check if last segment */
  1097. if(((DMARxDescToGet->Status & ETH_DMARxDesc_OWN) == (uint32_t)RESET) &&
  1098. ((DMARxDescToGet->Status & ETH_DMARxDesc_LS) != (uint32_t)RESET))
  1099. {
  1100. DMA_RX_FRAME_infos->LS_Rx_Desc = DMARxDescToGet;
  1101. DMA_RX_FRAME_infos->Seg_Count++;
  1102. return 1;
  1103. }
  1104. /* check if first segment */
  1105. else if(((DMARxDescToGet->Status & ETH_DMARxDesc_OWN) == (uint32_t)RESET) &&
  1106. ((DMARxDescToGet->Status & ETH_DMARxDesc_FS) != (uint32_t)RESET)&&
  1107. ((DMARxDescToGet->Status & ETH_DMARxDesc_LS) == (uint32_t)RESET))
  1108. {
  1109. DMA_RX_FRAME_infos->FS_Rx_Desc = DMARxDescToGet;
  1110. DMA_RX_FRAME_infos->LS_Rx_Desc = NULL;
  1111. DMA_RX_FRAME_infos->Seg_Count = 1;
  1112. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  1113. }
  1114. /* check if intermediate segment */
  1115. else if(((DMARxDescToGet->Status & ETH_DMARxDesc_OWN) == (uint32_t)RESET) &&
  1116. ((DMARxDescToGet->Status & ETH_DMARxDesc_FS) == (uint32_t)RESET)&&
  1117. ((DMARxDescToGet->Status & ETH_DMARxDesc_LS) == (uint32_t)RESET))
  1118. {
  1119. (DMA_RX_FRAME_infos->Seg_Count) ++;
  1120. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  1121. }
  1122. return 0;
  1123. }
  1124. /**
  1125. * @brief Initializes the DMA Tx descriptors in chain mode.
  1126. * @param DMATxDescTab: Pointer on the first Tx desc list
  1127. * @param TxBuff: Pointer on the first TxBuffer list
  1128. * @param TxBuffCount: Number of the used Tx desc in the list
  1129. * @retval None
  1130. */
  1131. void ETH_DMATxDescChainInit(ETH_DMADESCTypeDef *DMATxDescTab, uint8_t* TxBuff, uint32_t TxBuffCount)
  1132. {
  1133. uint32_t i = 0;
  1134. ETH_DMADESCTypeDef *DMATxDesc;
  1135. /* Set the DMATxDescToSet pointer with the first one of the DMATxDescTab list */
  1136. DMATxDescToSet = DMATxDescTab;
  1137. /* Fill each DMATxDesc descriptor with the right values */
  1138. for(i=0; i < TxBuffCount; i++)
  1139. {
  1140. /* Get the pointer on the ith member of the Tx Desc list */
  1141. DMATxDesc = DMATxDescTab + i;
  1142. /* Set Second Address Chained bit */
  1143. DMATxDesc->Status = ETH_DMATxDesc_TCH;
  1144. /* Set Buffer1 address pointer */
  1145. DMATxDesc->Buffer1Addr = (uint32_t)(&TxBuff[i*ETH_TX_BUF_SIZE]);
  1146. /* Initialize the next descriptor with the Next Descriptor Polling Enable */
  1147. if(i < (TxBuffCount-1))
  1148. {
  1149. /* Set next descriptor address register with next descriptor base address */
  1150. DMATxDesc->Buffer2NextDescAddr = (uint32_t)(DMATxDescTab+i+1);
  1151. }
  1152. else
  1153. {
  1154. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  1155. DMATxDesc->Buffer2NextDescAddr = (uint32_t) DMATxDescTab;
  1156. }
  1157. }
  1158. /* Set Transmit Desciptor List Address Register */
  1159. ETH->DMATDLAR = (uint32_t) DMATxDescTab;
  1160. }
  1161. /**
  1162. * @brief Checks whether the specified ETHERNET DMA Tx Desc flag is set or not.
  1163. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1164. * @param ETH_DMATxDescFlag: specifies the flag to check.
  1165. * This parameter can be one of the following values:
  1166. * @arg ETH_DMATxDesc_OWN : OWN bit: descriptor is owned by DMA engine
  1167. * @arg ETH_DMATxDesc_IC : Interrupt on completion
  1168. * @arg ETH_DMATxDesc_LS : Last Segment
  1169. * @arg ETH_DMATxDesc_FS : First Segment
  1170. * @arg ETH_DMATxDesc_DC : Disable CRC
  1171. * @arg ETH_DMATxDesc_DP : Disable Pad
  1172. * @arg ETH_DMATxDesc_TTSE: Transmit Time Stamp Enable
  1173. * @arg ETH_DMATxDesc_CIC : Checksum insertion control
  1174. * @arg ETH_DMATxDesc_TER : Transmit End of Ring
  1175. * @arg ETH_DMATxDesc_TCH : Second Address Chained
  1176. * @arg ETH_DMATxDesc_TTSS: Tx Time Stamp Status
  1177. * @arg ETH_DMATxDesc_IHE : IP Header Error
  1178. * @arg ETH_DMATxDesc_ES : Error summary
  1179. * @arg ETH_DMATxDesc_JT : Jabber Timeout
  1180. * @arg ETH_DMATxDesc_FF : Frame Flushed: DMA/MTL flushed the frame due to SW flush
  1181. * @arg ETH_DMATxDesc_PCE : Payload Checksum Error
  1182. * @arg ETH_DMATxDesc_LCA : Loss of Carrier: carrier lost during transmission
  1183. * @arg ETH_DMATxDesc_NC : No Carrier: no carrier signal from the transceiver
  1184. * @arg ETH_DMATxDesc_LCO : Late Collision: transmission aborted due to collision
  1185. * @arg ETH_DMATxDesc_EC : Excessive Collision: transmission aborted after 16 collisions
  1186. * @arg ETH_DMATxDesc_VF : VLAN Frame
  1187. * @arg ETH_DMATxDesc_CC : Collision Count
  1188. * @arg ETH_DMATxDesc_ED : Excessive Deferral
  1189. * @arg ETH_DMATxDesc_UF : Underflow Error: late data arrival from the memory
  1190. * @arg ETH_DMATxDesc_DB : Deferred Bit
  1191. * @retval The new state of ETH_DMATxDescFlag (SET or RESET).
  1192. */
  1193. FlagStatus ETH_GetDMATxDescFlagStatus(ETH_DMADESCTypeDef *DMATxDesc, uint32_t ETH_DMATxDescFlag)
  1194. {
  1195. FlagStatus bitstatus = RESET;
  1196. /* Check the parameters */
  1197. assert_param(IS_ETH_DMATxDESC_GET_FLAG(ETH_DMATxDescFlag));
  1198. if ((DMATxDesc->Status & ETH_DMATxDescFlag) != (uint32_t)RESET)
  1199. {
  1200. bitstatus = SET;
  1201. }
  1202. else
  1203. {
  1204. bitstatus = RESET;
  1205. }
  1206. return bitstatus;
  1207. }
  1208. /**
  1209. * @brief Returns the specified ETHERNET DMA Tx Desc collision count.
  1210. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1211. * @retval The Transmit descriptor collision counter value.
  1212. */
  1213. uint32_t ETH_GetDMATxDescCollisionCount(ETH_DMADESCTypeDef *DMATxDesc)
  1214. {
  1215. /* Return the Receive descriptor frame length */
  1216. return ((DMATxDesc->Status & ETH_DMATxDesc_CC) >> ETH_DMATXDESC_COLLISION_COUNTSHIFT);
  1217. }
  1218. /**
  1219. * @brief Set the specified DMA Tx Desc Own bit.
  1220. * @param DMATxDesc: Pointer on a Tx desc
  1221. * @retval None
  1222. */
  1223. void ETH_SetDMATxDescOwnBit(ETH_DMADESCTypeDef *DMATxDesc)
  1224. {
  1225. /* Set the DMA Tx Desc Own bit */
  1226. DMATxDesc->Status |= ETH_DMATxDesc_OWN;
  1227. }
  1228. /**
  1229. * @brief Enables or disables the specified DMA Tx Desc Transmit interrupt.
  1230. * @param DMATxDesc: Pointer on a Tx desc
  1231. * @param NewState: new state of the DMA Tx Desc transmit interrupt.
  1232. * This parameter can be: ENABLE or DISABLE.
  1233. * @retval None
  1234. */
  1235. void ETH_DMATxDescTransmitITConfig(ETH_DMADESCTypeDef *DMATxDesc, FunctionalState NewState)
  1236. {
  1237. /* Check the parameters */
  1238. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1239. if (NewState != DISABLE)
  1240. {
  1241. /* Enable the DMA Tx Desc Transmit interrupt */
  1242. DMATxDesc->Status |= ETH_DMATxDesc_IC;
  1243. }
  1244. else
  1245. {
  1246. /* Disable the DMA Tx Desc Transmit interrupt */
  1247. DMATxDesc->Status &=(~(uint32_t)ETH_DMATxDesc_IC);
  1248. }
  1249. }
  1250. /**
  1251. * @brief configure Tx descriptor as last or first segment
  1252. * @param DMATxDesc: Pointer on a Tx desc
  1253. * @param DMATxDesc_FrameSegment: specifies is the actual Tx desc contain last or first segment.
  1254. * This parameter can be one of the following values:
  1255. * @arg ETH_DMATxDesc_LastSegment : actual Tx desc contain last segment
  1256. * @arg ETH_DMATxDesc_FirstSegment : actual Tx desc contain first segment
  1257. * @retval None
  1258. */
  1259. void ETH_DMATxDescFrameSegmentConfig(ETH_DMADESCTypeDef *DMATxDesc, uint32_t DMATxDesc_FrameSegment)
  1260. {
  1261. /* Check the parameters */
  1262. assert_param(IS_ETH_DMA_TXDESC_SEGMENT(DMATxDesc_FrameSegment));
  1263. /* Selects the DMA Tx Desc Frame segment */
  1264. DMATxDesc->Status |= DMATxDesc_FrameSegment;
  1265. }
  1266. /**
  1267. * @brief Selects the specified ETHERNET DMA Tx Desc Checksum Insertion.
  1268. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1269. * @param DMATxDesc_Checksum: specifies is the DMA Tx desc checksum insertion.
  1270. * This parameter can be one of the following values:
  1271. * @arg ETH_DMATxDesc_ChecksumByPass : Checksum bypass
  1272. * @arg ETH_DMATxDesc_ChecksumIPV4Header : IPv4 header checksum
  1273. * @arg ETH_DMATxDesc_ChecksumTCPUDPICMPSegment : TCP/UDP/ICMP checksum. Pseudo header checksum is assumed to be present
  1274. * @arg ETH_DMATxDesc_ChecksumTCPUDPICMPFull : TCP/UDP/ICMP checksum fully in hardware including pseudo header
  1275. * @retval None
  1276. */
  1277. void ETH_DMATxDescChecksumInsertionConfig(ETH_DMADESCTypeDef *DMATxDesc, uint32_t DMATxDesc_Checksum)
  1278. {
  1279. /* Check the parameters */
  1280. assert_param(IS_ETH_DMA_TXDESC_CHECKSUM(DMATxDesc_Checksum));
  1281. /* Set the selected DMA Tx desc checksum insertion control */
  1282. DMATxDesc->Status |= DMATxDesc_Checksum;
  1283. }
  1284. /**
  1285. * @brief Enables or disables the DMA Tx Desc CRC.
  1286. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1287. * @param NewState: new state of the specified DMA Tx Desc CRC.
  1288. * This parameter can be: ENABLE or DISABLE.
  1289. * @retval None
  1290. */
  1291. void ETH_DMATxDescCRCCmd(ETH_DMADESCTypeDef *DMATxDesc, FunctionalState NewState)
  1292. {
  1293. /* Check the parameters */
  1294. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1295. if (NewState != DISABLE)
  1296. {
  1297. /* Enable the selected DMA Tx Desc CRC */
  1298. DMATxDesc->Status &= (~(uint32_t)ETH_DMATxDesc_DC);
  1299. }
  1300. else
  1301. {
  1302. /* Disable the selected DMA Tx Desc CRC */
  1303. DMATxDesc->Status |= ETH_DMATxDesc_DC;
  1304. }
  1305. }
  1306. /**
  1307. * @brief Enables or disables the DMA Tx Desc second address chained.
  1308. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1309. * @param NewState: new state of the specified DMA Tx Desc second address chained.
  1310. * This parameter can be: ENABLE or DISABLE.
  1311. * @retval None
  1312. */
  1313. void ETH_DMATxDescSecondAddressChainedCmd(ETH_DMADESCTypeDef *DMATxDesc, FunctionalState NewState)
  1314. {
  1315. /* Check the parameters */
  1316. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1317. if (NewState != DISABLE)
  1318. {
  1319. /* Enable the selected DMA Tx Desc second address chained */
  1320. DMATxDesc->Status |= ETH_DMATxDesc_TCH;
  1321. }
  1322. else
  1323. {
  1324. /* Disable the selected DMA Tx Desc second address chained */
  1325. DMATxDesc->Status &=(~(uint32_t)ETH_DMATxDesc_TCH);
  1326. }
  1327. }
  1328. /**
  1329. * @brief Enables or disables the DMA Tx Desc padding for frame shorter than 64 bytes.
  1330. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1331. * @param NewState: new state of the specified DMA Tx Desc padding for frame shorter than 64 bytes.
  1332. * This parameter can be: ENABLE or DISABLE.
  1333. * @retval None
  1334. */
  1335. void ETH_DMATxDescShortFramePaddingCmd(ETH_DMADESCTypeDef *DMATxDesc, FunctionalState NewState)
  1336. {
  1337. /* Check the parameters */
  1338. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1339. if (NewState != DISABLE)
  1340. {
  1341. /* Enable the selected DMA Tx Desc padding for frame shorter than 64 bytes */
  1342. DMATxDesc->Status &= (~(uint32_t)ETH_DMATxDesc_DP);
  1343. }
  1344. else
  1345. {
  1346. /* Disable the selected DMA Tx Desc padding for frame shorter than 64 bytes*/
  1347. DMATxDesc->Status |= ETH_DMATxDesc_DP;
  1348. }
  1349. }
  1350. /**
  1351. * @brief Configures the specified DMA Tx Desc buffer1 and buffer2 sizes.
  1352. * @param DMATxDesc: Pointer on a Tx desc
  1353. * @param BufferSize1: specifies the Tx desc buffer1 size.
  1354. * @param BufferSize2: specifies the Tx desc buffer2 size (put "0" if not used).
  1355. * @retval None
  1356. */
  1357. void ETH_DMATxDescBufferSizeConfig(ETH_DMADESCTypeDef *DMATxDesc, uint32_t BufferSize1, uint32_t BufferSize2)
  1358. {
  1359. /* Check the parameters */
  1360. assert_param(IS_ETH_DMATxDESC_BUFFER_SIZE(BufferSize1));
  1361. assert_param(IS_ETH_DMATxDESC_BUFFER_SIZE(BufferSize2));
  1362. /* Set the DMA Tx Desc buffer1 and buffer2 sizes values */
  1363. DMATxDesc->ControlBufferSize |= (BufferSize1 | (BufferSize2 << ETH_DMATXDESC_BUFFER2_SIZESHIFT));
  1364. }
  1365. /**
  1366. * @brief Checks whether the specified ETHERNET Rx Desc flag is set or not.
  1367. * @param DMARxDesc: pointer on a DMA Rx descriptor
  1368. * @param ETH_DMARxDescFlag: specifies the flag to check.
  1369. * This parameter can be one of the following values:
  1370. * @arg ETH_DMARxDesc_OWN: OWN bit: descriptor is owned by DMA engine
  1371. * @arg ETH_DMARxDesc_AFM: DA Filter Fail for the rx frame
  1372. * @arg ETH_DMARxDesc_ES: Error summary
  1373. * @arg ETH_DMARxDesc_DE: Descriptor error: no more descriptors for receive frame
  1374. * @arg ETH_DMARxDesc_SAF: SA Filter Fail for the received frame
  1375. * @arg ETH_DMARxDesc_LE: Frame size not matching with length field
  1376. * @arg ETH_DMARxDesc_OE: Overflow Error: Frame was damaged due to buffer overflow
  1377. * @arg ETH_DMARxDesc_VLAN: VLAN Tag: received frame is a VLAN frame
  1378. * @arg ETH_DMARxDesc_FS: First descriptor of the frame
  1379. * @arg ETH_DMARxDesc_LS: Last descriptor of the frame
  1380. * @arg ETH_DMARxDesc_IPV4HCE: IPC Checksum Error/Giant Frame: Rx Ipv4 header checksum error
  1381. * @arg ETH_DMARxDesc_LC: Late collision occurred during reception
  1382. * @arg ETH_DMARxDesc_FT: Frame type - Ethernet, otherwise 802.3
  1383. * @arg ETH_DMARxDesc_RWT: Receive Watchdog Timeout: watchdog timer expired during reception
  1384. * @arg ETH_DMARxDesc_RE: Receive error: error reported by MII interface
  1385. * @arg ETH_DMARxDesc_DE: Dribble bit error: frame contains non int multiple of 8 bits
  1386. * @arg ETH_DMARxDesc_CE: CRC error
  1387. * @arg ETH_DMARxDesc_MAMPCE: Rx MAC Address/Payload Checksum Error: Rx MAC address matched/ Rx Payload Checksum Error
  1388. * @retval The new state of ETH_DMARxDescFlag (SET or RESET).
  1389. */
  1390. FlagStatus ETH_GetDMARxDescFlagStatus(ETH_DMADESCTypeDef *DMARxDesc, uint32_t ETH_DMARxDescFlag)
  1391. {
  1392. FlagStatus bitstatus = RESET;
  1393. /* Check the parameters */
  1394. assert_param(IS_ETH_DMARxDESC_GET_FLAG(ETH_DMARxDescFlag));
  1395. if ((DMARxDesc->Status & ETH_DMARxDescFlag) != (uint32_t)RESET)
  1396. {
  1397. bitstatus = SET;
  1398. }
  1399. else
  1400. {
  1401. bitstatus = RESET;
  1402. }
  1403. return bitstatus;
  1404. }
  1405. #ifdef USE_ENHANCED_DMA_DESCRIPTORS
  1406. /**
  1407. * @brief Checks whether the specified ETHERNET PTP Rx Desc extended flag is set or not.
  1408. * @param DMAPTPRxDesc: pointer on a DMA PTP Rx descriptor
  1409. * @param ETH_DMAPTPRxDescFlag: specifies the extended flag to check.
  1410. * This parameter can be one of the following values:
  1411. * @arg ETH_DMAPTPRxDesc_PTPV: PTP version
  1412. * @arg ETH_DMAPTPRxDesc_PTPFT: PTP frame type
  1413. * @arg ETH_DMAPTPRxDesc_PTPMT: PTP message type
  1414. * @arg ETH_DMAPTPRxDesc_IPV6PR: IPv6 packet received
  1415. * @arg ETH_DMAPTPRxDesc_IPV4PR: IPv4 packet received
  1416. * @arg ETH_DMAPTPRxDesc_IPCB: IP checksum bypassed
  1417. * @arg ETH_DMAPTPRxDesc_IPPE: IP payload error
  1418. * @arg ETH_DMAPTPRxDesc_IPHE: IP header error
  1419. * @arg ETH_DMAPTPRxDesc_IPPT: IP payload type
  1420. * @retval The new state of ETH_DMAPTPRxDescExtendedFlag (SET or RESET).
  1421. */
  1422. FlagStatus ETH_GetDMAPTPRxDescExtendedFlagStatus(ETH_DMADESCTypeDef *DMAPTPRxDesc, uint32_t ETH_DMAPTPRxDescExtendedFlag)
  1423. {
  1424. FlagStatus bitstatus = RESET;
  1425. /* Check the parameters */
  1426. assert_param(IS_ETH_DMAPTPRxDESC_GET_EXTENDED_FLAG(ETH_DMAPTPRxDescExtendedFlag));
  1427. if ((DMAPTPRxDesc->ExtendedStatus & ETH_DMAPTPRxDescExtendedFlag) != (uint32_t)RESET)
  1428. {
  1429. bitstatus = SET;
  1430. }
  1431. else
  1432. {
  1433. bitstatus = RESET;
  1434. }
  1435. return bitstatus;
  1436. }
  1437. #endif /* USE_ENHANCED_DMA_DESCRIPTORS */
  1438. /**
  1439. * @brief Set the specified DMA Rx Desc Own bit.
  1440. * @param DMARxDesc: Pointer on a Rx desc
  1441. * @retval None
  1442. */
  1443. void ETH_SetDMARxDescOwnBit(ETH_DMADESCTypeDef *DMARxDesc)
  1444. {
  1445. /* Set the DMA Rx Desc Own bit */
  1446. DMARxDesc->Status |= ETH_DMARxDesc_OWN;
  1447. }
  1448. /**
  1449. * @brief Returns the specified DMA Rx Desc frame length.
  1450. * @param DMARxDesc: pointer on a DMA Rx descriptor
  1451. * @retval The Rx descriptor received frame length.
  1452. */
  1453. uint32_t ETH_GetDMARxDescFrameLength(ETH_DMADESCTypeDef *DMARxDesc)
  1454. {
  1455. /* Return the Receive descriptor frame length */
  1456. return ((DMARxDesc->Status & ETH_DMARxDesc_FL) >> ETH_DMARXDESC_FRAME_LENGTHSHIFT);
  1457. }
  1458. /**
  1459. * @brief Enables or disables the specified DMA Rx Desc receive interrupt.
  1460. * @param DMARxDesc: Pointer on a Rx desc
  1461. * @param NewState: new state of the specified DMA Rx Desc interrupt.
  1462. * This parameter can be: ENABLE or DISABLE.
  1463. * @retval None
  1464. */
  1465. void ETH_DMARxDescReceiveITConfig(ETH_DMADESCTypeDef *DMARxDesc, FunctionalState NewState)
  1466. {
  1467. /* Check the parameters */
  1468. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1469. if (NewState != DISABLE)
  1470. {
  1471. /* Enable the DMA Rx Desc receive interrupt */
  1472. DMARxDesc->ControlBufferSize &=(~(uint32_t)ETH_DMARxDesc_DIC);
  1473. }
  1474. else
  1475. {
  1476. /* Disable the DMA Rx Desc receive interrupt */
  1477. DMARxDesc->ControlBufferSize |= ETH_DMARxDesc_DIC;
  1478. }
  1479. }
  1480. /**
  1481. * @brief Returns the specified ETHERNET DMA Rx Desc buffer size.
  1482. * @param DMARxDesc: pointer on a DMA Rx descriptor
  1483. * @param DMARxDesc_Buffer: specifies the DMA Rx Desc buffer.
  1484. * This parameter can be any one of the following values:
  1485. * @arg ETH_DMARxDesc_Buffer1 : DMA Rx Desc Buffer1
  1486. * @arg ETH_DMARxDesc_Buffer2 : DMA Rx Desc Buffer2
  1487. * @retval The Receive descriptor frame length.
  1488. */
  1489. uint32_t ETH_GetDMARxDescBufferSize(ETH_DMADESCTypeDef *DMARxDesc, uint32_t DMARxDesc_Buffer)
  1490. {
  1491. /* Check the parameters */
  1492. assert_param(IS_ETH_DMA_RXDESC_BUFFER(DMARxDesc_Buffer));
  1493. if(DMARxDesc_Buffer != ETH_DMARxDesc_Buffer1)
  1494. {
  1495. /* Return the DMA Rx Desc buffer2 size */
  1496. return ((DMARxDesc->ControlBufferSize & ETH_DMARxDesc_RBS2) >> ETH_DMARXDESC_BUFFER2_SIZESHIFT);
  1497. }
  1498. else
  1499. {
  1500. /* Return the DMA Rx Desc buffer1 size */
  1501. return (DMARxDesc->ControlBufferSize & ETH_DMARxDesc_RBS1);
  1502. }
  1503. }
  1504. /**
  1505. * @brief Get the size of the received packet.
  1506. * @param None
  1507. * @retval framelength: received packet size
  1508. */
  1509. uint32_t ETH_GetRxPktSize(ETH_DMADESCTypeDef *DMARxDesc)
  1510. {
  1511. uint32_t frameLength = 0;
  1512. if(((DMARxDesc->Status & ETH_DMARxDesc_OWN) == (uint32_t)RESET) &&
  1513. ((DMARxDesc->Status & ETH_DMARxDesc_ES) == (uint32_t)RESET) &&
  1514. ((DMARxDesc->Status & ETH_DMARxDesc_LS) != (uint32_t)RESET))
  1515. {
  1516. /* Get the size of the packet: including 4 bytes of the CRC */
  1517. frameLength = ETH_GetDMARxDescFrameLength(DMARxDesc);
  1518. }
  1519. /* Return Frame Length */
  1520. return frameLength;
  1521. }
  1522. #ifdef USE_ENHANCED_DMA_DESCRIPTORS
  1523. /**
  1524. * @brief Enables or disables the Enhanced descriptor structure.
  1525. * @param NewState: new state of the Enhanced descriptor structure.
  1526. * This parameter can be: ENABLE or DISABLE.
  1527. * @retval None
  1528. */
  1529. void ETH_EnhancedDescriptorCmd(FunctionalState NewState)
  1530. {
  1531. /* Check the parameters */
  1532. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1533. if (NewState != DISABLE)
  1534. {
  1535. /* Enable enhanced descriptor structure */
  1536. ETH->DMABMR |= ETH_DMABMR_EDE;
  1537. }
  1538. else
  1539. {
  1540. /* Disable enhanced descriptor structure */
  1541. ETH->DMABMR &= ~ETH_DMABMR_EDE;
  1542. }
  1543. }
  1544. #endif /* USE_ENHANCED_DMA_DESCRIPTORS */
  1545. /******************************************************************************/
  1546. /* DMA functions */
  1547. /******************************************************************************/
  1548. /**
  1549. * @brief Resets all MAC subsystem internal registers and logic.
  1550. * @param None
  1551. * @retval None
  1552. */
  1553. void ETH_SoftwareReset(void)
  1554. {
  1555. /* Set the SWR bit: resets all MAC subsystem internal registers and logic */
  1556. /* After reset all the registers holds their respective reset values */
  1557. ETH->DMABMR |= ETH_DMABMR_SR;
  1558. }
  1559. /**
  1560. * @brief Checks whether the ETHERNET software reset bit is set or not.
  1561. * @param None
  1562. * @retval The new state of DMA Bus Mode register SR bit (SET or RESET).
  1563. */
  1564. FlagStatus ETH_GetSoftwareResetStatus(void)
  1565. {
  1566. FlagStatus bitstatus = RESET;
  1567. if((ETH->DMABMR & ETH_DMABMR_SR) != (uint32_t)RESET)
  1568. {
  1569. bitstatus = SET;
  1570. }
  1571. else
  1572. {
  1573. bitstatus = RESET;
  1574. }
  1575. return bitstatus;
  1576. }
  1577. /**
  1578. * @brief Checks whether the specified ETHERNET DMA flag is set or not.
  1579. * @param ETH_DMA_FLAG: specifies the flag to check.
  1580. * This parameter can be one of the following values:
  1581. * @arg ETH_DMA_FLAG_TST : Time-stamp trigger flag
  1582. * @arg ETH_DMA_FLAG_PMT : PMT flag
  1583. * @arg ETH_DMA_FLAG_MMC : MMC flag
  1584. * @arg ETH_DMA_FLAG_DataTransferError : Error bits 0-data buffer, 1-desc. access
  1585. * @arg ETH_DMA_FLAG_ReadWriteError : Error bits 0-write trnsf, 1-read transfr
  1586. * @arg ETH_DMA_FLAG_AccessError : Error bits 0-Rx DMA, 1-Tx DMA
  1587. * @arg ETH_DMA_FLAG_NIS : Normal interrupt summary flag
  1588. * @arg ETH_DMA_FLAG_AIS : Abnormal interrupt summary flag
  1589. * @arg ETH_DMA_FLAG_ER : Early receive flag
  1590. * @arg ETH_DMA_FLAG_FBE : Fatal bus error flag
  1591. * @arg ETH_DMA_FLAG_ET : Early transmit flag
  1592. * @arg ETH_DMA_FLAG_RWT : Receive watchdog timeout flag
  1593. * @arg ETH_DMA_FLAG_RPS : Receive process stopped flag
  1594. * @arg ETH_DMA_FLAG_RBU : Receive buffer unavailable flag
  1595. * @arg ETH_DMA_FLAG_R : Receive flag
  1596. * @arg ETH_DMA_FLAG_TU : Underflow flag
  1597. * @arg ETH_DMA_FLAG_RO : Overflow flag
  1598. * @arg ETH_DMA_FLAG_TJT : Transmit jabber timeout flag
  1599. * @arg ETH_DMA_FLAG_TBU : Transmit buffer unavailable flag
  1600. * @arg ETH_DMA_FLAG_TPS : Transmit process stopped flag
  1601. * @arg ETH_DMA_FLAG_T : Transmit flag
  1602. * @retval The new state of ETH_DMA_FLAG (SET or RESET).
  1603. */
  1604. FlagStatus ETH_GetDMAFlagStatus(uint32_t ETH_DMA_FLAG)
  1605. {
  1606. FlagStatus bitstatus = RESET;
  1607. /* Check the parameters */
  1608. assert_param(IS_ETH_DMA_GET_IT(ETH_DMA_FLAG));
  1609. if ((ETH->DMASR & ETH_DMA_FLAG) != (uint32_t)RESET)
  1610. {
  1611. bitstatus = SET;
  1612. }
  1613. else
  1614. {
  1615. bitstatus = RESET;
  1616. }
  1617. return bitstatus;
  1618. }
  1619. /**
  1620. * @brief Clears the ETHERNET’s DMA pending flag.
  1621. * @param ETH_DMA_FLAG: specifies the flag to clear.
  1622. * This parameter can be any combination of the following values:
  1623. * @arg ETH_DMA_FLAG_NIS : Normal interrupt summary flag
  1624. * @arg ETH_DMA_FLAG_AIS : Abnormal interrupt summary flag
  1625. * @arg ETH_DMA_FLAG_ER : Early receive flag
  1626. * @arg ETH_DMA_FLAG_FBE : Fatal bus error flag
  1627. * @arg ETH_DMA_FLAG_ETI : Early transmit flag
  1628. * @arg ETH_DMA_FLAG_RWT : Receive watchdog timeout flag
  1629. * @arg ETH_DMA_FLAG_RPS : Receive process stopped flag
  1630. * @arg ETH_DMA_FLAG_RBU : Receive buffer unavailable flag
  1631. * @arg ETH_DMA_FLAG_R : Receive flag
  1632. * @arg ETH_DMA_FLAG_TU : Transmit Underflow flag
  1633. * @arg ETH_DMA_FLAG_RO : Receive Overflow flag
  1634. * @arg ETH_DMA_FLAG_TJT : Transmit jabber timeout flag
  1635. * @arg ETH_DMA_FLAG_TBU : Transmit buffer unavailable flag
  1636. * @arg ETH_DMA_FLAG_TPS : Transmit process stopped flag
  1637. * @arg ETH_DMA_FLAG_T : Transmit flag
  1638. * @retval None
  1639. */
  1640. void ETH_DMAClearFlag(uint32_t ETH_DMA_FLAG)
  1641. {
  1642. /* Check the parameters */
  1643. assert_param(IS_ETH_DMA_FLAG(ETH_DMA_FLAG));
  1644. /* Clear the selected ETHERNET DMA FLAG */
  1645. ETH->DMASR = (uint32_t) ETH_DMA_FLAG;
  1646. }
  1647. /**
  1648. * @brief Enables or disables the specified ETHERNET DMA interrupts.
  1649. * @param ETH_DMA_IT: specifies the ETHERNET DMA interrupt sources to be
  1650. * enabled or disabled.
  1651. * This parameter can be any combination of the following values:
  1652. * @arg ETH_DMA_IT_NIS : Normal interrupt summary
  1653. * @arg ETH_DMA_IT_AIS : Abnormal interrupt summary
  1654. * @arg ETH_DMA_IT_ER : Early receive interrupt
  1655. * @arg ETH_DMA_IT_FBE : Fatal bus error interrupt
  1656. * @arg ETH_DMA_IT_ET : Early transmit interrupt
  1657. * @arg ETH_DMA_IT_RWT : Receive watchdog timeout interrupt
  1658. * @arg ETH_DMA_IT_RPS : Receive process stopped interrupt
  1659. * @arg ETH_DMA_IT_RBU : Receive buffer unavailable interrupt
  1660. * @arg ETH_DMA_IT_R : Receive interrupt
  1661. * @arg ETH_DMA_IT_TU : Underflow interrupt
  1662. * @arg ETH_DMA_IT_RO : Overflow interrupt
  1663. * @arg ETH_DMA_IT_TJT : Transmit jabber timeout interrupt
  1664. * @arg ETH_DMA_IT_TBU : Transmit buffer unavailable interrupt
  1665. * @arg ETH_DMA_IT_TPS : Transmit process stopped interrupt
  1666. * @arg ETH_DMA_IT_T : Transmit interrupt
  1667. * @param NewState: new state of the specified ETHERNET DMA interrupts.
  1668. * This parameter can be: ENABLE or DISABLE.
  1669. * @retval None
  1670. */
  1671. void ETH_DMAITConfig(uint32_t ETH_DMA_IT, FunctionalState NewState)
  1672. {
  1673. /* Check the parameters */
  1674. assert_param(IS_ETH_DMA_IT(ETH_DMA_IT));
  1675. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1676. if (NewState != DISABLE)
  1677. {
  1678. /* Enable the selected ETHERNET DMA interrupts */
  1679. ETH->DMAIER |= ETH_DMA_IT;
  1680. }
  1681. else
  1682. {
  1683. /* Disable the selected ETHERNET DMA interrupts */
  1684. ETH->DMAIER &=(~(uint32_t)ETH_DMA_IT);
  1685. }
  1686. }
  1687. /**
  1688. * @brief Checks whether the specified ETHERNET DMA interrupt has occurred or not.
  1689. * @param ETH_DMA_IT: specifies the interrupt source to check.
  1690. * This parameter can be one of the following values:
  1691. * @arg ETH_DMA_IT_TST : Time-stamp trigger interrupt
  1692. * @arg ETH_DMA_IT_PMT : PMT interrupt
  1693. * @arg ETH_DMA_IT_MMC : MMC interrupt
  1694. * @arg ETH_DMA_IT_NIS : Normal interrupt summary
  1695. * @arg ETH_DMA_IT_AIS : Abnormal interrupt summary
  1696. * @arg ETH_DMA_IT_ER : Early receive interrupt
  1697. * @arg ETH_DMA_IT_FBE : Fatal bus error interrupt
  1698. * @arg ETH_DMA_IT_ET : Early transmit interrupt
  1699. * @arg ETH_DMA_IT_RWT : Receive watchdog timeout interrupt
  1700. * @arg ETH_DMA_IT_RPS : Receive process stopped interrupt
  1701. * @arg ETH_DMA_IT_RBU : Receive buffer unavailable interrupt
  1702. * @arg ETH_DMA_IT_R : Receive interrupt
  1703. * @arg ETH_DMA_IT_TU : Underflow interrupt
  1704. * @arg ETH_DMA_IT_RO : Overflow interrupt
  1705. * @arg ETH_DMA_IT_TJT : Transmit jabber timeout interrupt
  1706. * @arg ETH_DMA_IT_TBU : Transmit buffer unavailable interrupt
  1707. * @arg ETH_DMA_IT_TPS : Transmit process stopped interrupt
  1708. * @arg ETH_DMA_IT_T : Transmit interrupt
  1709. * @retval The new state of ETH_DMA_IT (SET or RESET).
  1710. */
  1711. ITStatus ETH_GetDMAITStatus(uint32_t ETH_DMA_IT)
  1712. {
  1713. ITStatus bitstatus = RESET;
  1714. /* Check the parameters */
  1715. assert_param(IS_ETH_DMA_GET_IT(ETH_DMA_IT));
  1716. if ((ETH->DMASR & ETH_DMA_IT) != (uint32_t)RESET)
  1717. {
  1718. bitstatus = SET;
  1719. }
  1720. else
  1721. {
  1722. bitstatus = RESET;
  1723. }
  1724. return bitstatus;
  1725. }
  1726. /**
  1727. * @brief Clears the ETHERNET’s DMA IT pending bit.
  1728. * @param ETH_DMA_IT: specifies the interrupt pending bit to clear.
  1729. * This parameter can be any combination of the following values:
  1730. * @arg ETH_DMA_IT_NIS : Normal interrupt summary
  1731. * @arg ETH_DMA_IT_AIS : Abnormal interrupt summary
  1732. * @arg ETH_DMA_IT_ER : Early receive interrupt
  1733. * @arg ETH_DMA_IT_FBE : Fatal bus error interrupt
  1734. * @arg ETH_DMA_IT_ETI : Early transmit interrupt
  1735. * @arg ETH_DMA_IT_RWT : Receive watchdog timeout interrupt
  1736. * @arg ETH_DMA_IT_RPS : Receive process stopped interrupt
  1737. * @arg ETH_DMA_IT_RBU : Receive buffer unavailable interrupt
  1738. * @arg ETH_DMA_IT_R : Receive interrupt
  1739. * @arg ETH_DMA_IT_TU : Transmit Underflow interrupt
  1740. * @arg ETH_DMA_IT_RO : Receive Overflow interrupt
  1741. * @arg ETH_DMA_IT_TJT : Transmit jabber timeout interrupt
  1742. * @arg ETH_DMA_IT_TBU : Transmit buffer unavailable interrupt
  1743. * @arg ETH_DMA_IT_TPS : Transmit process stopped interrupt
  1744. * @arg ETH_DMA_IT_T : Transmit interrupt
  1745. * @retval None
  1746. */
  1747. void ETH_DMAClearITPendingBit(uint32_t ETH_DMA_IT)
  1748. {
  1749. /* Check the parameters */
  1750. assert_param(IS_ETH_DMA_IT(ETH_DMA_IT));
  1751. /* Clear the selected ETHERNET DMA IT */
  1752. ETH->DMASR = (uint32_t) ETH_DMA_IT;
  1753. }
  1754. /**
  1755. * @brief Returns the ETHERNET DMA Transmit Process State.
  1756. * @param None
  1757. * @retval The new ETHERNET DMA Transmit Process State:
  1758. * This can be one of the following values:
  1759. * - ETH_DMA_TransmitProcess_Stopped : Stopped - Reset or Stop Tx Command issued
  1760. * - ETH_DMA_TransmitProcess_Fetching : Running - fetching the Tx descriptor
  1761. * - ETH_DMA_TransmitProcess_Waiting : Running - waiting for status
  1762. * - ETH_DMA_TransmitProcess_Reading : Running - reading the data from host memory
  1763. * - ETH_DMA_TransmitProcess_Suspended : Suspended - Tx Descriptor unavailable
  1764. * - ETH_DMA_TransmitProcess_Closing : Running - closing Rx descriptor
  1765. */
  1766. uint32_t ETH_GetTransmitProcessState(void)
  1767. {
  1768. return ((uint32_t)(ETH->DMASR & ETH_DMASR_TS));
  1769. }
  1770. /**
  1771. * @brief Returns the ETHERNET DMA Receive Process State.
  1772. * @param None
  1773. * @retval The new ETHERNET DMA Receive Process State:
  1774. * This can be one of the following values:
  1775. * - ETH_DMA_ReceiveProcess_Stopped : Stopped - Reset or Stop Rx Command issued
  1776. * - ETH_DMA_ReceiveProcess_Fetching : Running - fetching the Rx descriptor
  1777. * - ETH_DMA_ReceiveProcess_Waiting : Running - waiting for packet
  1778. * - ETH_DMA_ReceiveProcess_Suspended : Suspended - Rx Descriptor unavailable
  1779. * - ETH_DMA_ReceiveProcess_Closing : Running - closing descriptor
  1780. * - ETH_DMA_ReceiveProcess_Queuing : Running - queuing the receive frame into host memory
  1781. */
  1782. uint32_t ETH_GetReceiveProcessState(void)
  1783. {
  1784. return ((uint32_t)(ETH->DMASR & ETH_DMASR_RS));
  1785. }
  1786. /**
  1787. * @brief Clears the ETHERNET transmit FIFO.
  1788. * @param None
  1789. * @retval None
  1790. */
  1791. void ETH_FlushTransmitFIFO(void)
  1792. {
  1793. /* Set the Flush Transmit FIFO bit */
  1794. ETH->DMAOMR |= ETH_DMAOMR_FTF;
  1795. }
  1796. /**
  1797. * @brief Checks whether the ETHERNET flush transmit FIFO bit is cleared or not.
  1798. * @param None
  1799. * @retval The new state of ETHERNET flush transmit FIFO bit (SET or RESET).
  1800. */
  1801. FlagStatus ETH_GetFlushTransmitFIFOStatus(void)
  1802. {
  1803. FlagStatus bitstatus = RESET;
  1804. if ((ETH->DMAOMR & ETH_DMAOMR_FTF) != (uint32_t)RESET)
  1805. {
  1806. bitstatus = SET;
  1807. }
  1808. else
  1809. {
  1810. bitstatus = RESET;
  1811. }
  1812. return bitstatus;
  1813. }
  1814. /**
  1815. * @brief Enables or disables the DMA transmission.
  1816. * @param NewState: new state of the DMA transmission.
  1817. * This parameter can be: ENABLE or DISABLE.
  1818. * @retval None
  1819. */
  1820. void ETH_DMATransmissionCmd(FunctionalState NewState)
  1821. {
  1822. /* Check the parameters */
  1823. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1824. if (NewState != DISABLE)
  1825. {
  1826. /* Enable the DMA transmission */
  1827. ETH->DMAOMR |= ETH_DMAOMR_ST;
  1828. }
  1829. else
  1830. {
  1831. /* Disable the DMA transmission */
  1832. ETH->DMAOMR &= ~ETH_DMAOMR_ST;
  1833. }
  1834. }
  1835. /**
  1836. * @brief Enables or disables the DMA reception.
  1837. * @param NewState: new state of the DMA reception.
  1838. * This parameter can be: ENABLE or DISABLE.
  1839. * @retval None
  1840. */
  1841. void ETH_DMAReceptionCmd(FunctionalState NewState)
  1842. {
  1843. /* Check the parameters */
  1844. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1845. if (NewState != DISABLE)
  1846. {
  1847. /* Enable the DMA reception */
  1848. ETH->DMAOMR |= ETH_DMAOMR_SR;
  1849. }
  1850. else
  1851. {
  1852. /* Disable the DMA reception */
  1853. ETH->DMAOMR &= ~ETH_DMAOMR_SR;
  1854. }
  1855. }
  1856. /**
  1857. * @brief Checks whether the specified ETHERNET DMA overflow flag is set or not.
  1858. * @param ETH_DMA_Overflow: specifies the DMA overflow flag to check.
  1859. * This parameter can be one of the following values:
  1860. * @arg ETH_DMA_Overflow_RxFIFOCounter : Overflow for FIFO Overflows Counter
  1861. * @arg ETH_DMA_Overflow_MissedFrameCounter : Overflow for Buffer Unavailable Missed Frame Counter
  1862. * @retval The new state of ETHERNET DMA overflow Flag (SET or RESET).
  1863. */
  1864. FlagStatus ETH_GetDMAOverflowStatus(uint32_t ETH_DMA_Overflow)
  1865. {
  1866. FlagStatus bitstatus = RESET;
  1867. /* Check the parameters */
  1868. assert_param(IS_ETH_DMA_GET_OVERFLOW(ETH_DMA_Overflow));
  1869. if ((ETH->DMAMFBOCR & ETH_DMA_Overflow) != (uint32_t)RESET)
  1870. {
  1871. bitstatus = SET;
  1872. }
  1873. else
  1874. {
  1875. bitstatus = RESET;
  1876. }
  1877. return bitstatus;
  1878. }
  1879. /**
  1880. * @brief Get the ETHERNET DMA Rx Overflow Missed Frame Counter value.
  1881. * @param None
  1882. * @retval The value of Rx overflow Missed Frame Counter.
  1883. */
  1884. uint32_t ETH_GetRxOverflowMissedFrameCounter(void)
  1885. {
  1886. return ((uint32_t)((ETH->DMAMFBOCR & ETH_DMAMFBOCR_MFA)>>ETH_DMA_RX_OVERFLOW_MISSEDFRAMES_COUNTERSHIFT));
  1887. }
  1888. /**
  1889. * @brief Get the ETHERNET DMA Buffer Unavailable Missed Frame Counter value.
  1890. * @param None
  1891. * @retval The value of Buffer unavailable Missed Frame Counter.
  1892. */
  1893. uint32_t ETH_GetBufferUnavailableMissedFrameCounter(void)
  1894. {
  1895. return ((uint32_t)(ETH->DMAMFBOCR) & ETH_DMAMFBOCR_MFC);
  1896. }
  1897. /**
  1898. * @brief Get the ETHERNET DMA DMACHTDR register value.
  1899. * @param None
  1900. * @retval The value of the current Tx desc start address.
  1901. */
  1902. uint32_t ETH_GetCurrentTxDescStartAddress(void)
  1903. {
  1904. return ((uint32_t)(ETH->DMACHTDR));
  1905. }
  1906. /**
  1907. * @brief Get the ETHERNET DMA DMACHRDR register value.
  1908. * @param None
  1909. * @retval The value of the current Rx desc start address.
  1910. */
  1911. uint32_t ETH_GetCurrentRxDescStartAddress(void)
  1912. {
  1913. return ((uint32_t)(ETH->DMACHRDR));
  1914. }
  1915. /**
  1916. * @brief Get the ETHERNET DMA DMACHTBAR register value.
  1917. * @param None
  1918. * @retval The value of the current transmit descriptor data buffer address.
  1919. */
  1920. uint32_t ETH_GetCurrentTxBufferAddress(void)
  1921. {
  1922. return ((uint32_t)(ETH->DMACHTBAR));
  1923. }
  1924. /**
  1925. * @brief Get the ETHERNET DMA DMACHRBAR register value.
  1926. * @param None
  1927. * @retval The value of the current receive descriptor data buffer address.
  1928. */
  1929. uint32_t ETH_GetCurrentRxBufferAddress(void)
  1930. {
  1931. return ((uint32_t)(ETH->DMACHRBAR));
  1932. }
  1933. /**
  1934. * @brief Resumes the DMA Transmission by writing to the DmaTxPollDemand register
  1935. * (the data written could be anything). This forces the DMA to resume transmission.
  1936. * @param None
  1937. * @retval None.
  1938. */
  1939. void ETH_ResumeDMATransmission(void)
  1940. {
  1941. ETH->DMATPDR = 0;
  1942. }
  1943. /**
  1944. * @brief Resumes the DMA Transmission by writing to the DmaRxPollDemand register
  1945. * (the data written could be anything). This forces the DMA to resume reception.
  1946. * @param None
  1947. * @retval None.
  1948. */
  1949. void ETH_ResumeDMAReception(void)
  1950. {
  1951. ETH->DMARPDR = 0;
  1952. }
  1953. /**
  1954. * @brief Set the DMA Receive status watchdog timer register value
  1955. * @param Value: DMA Receive status watchdog timer register value
  1956. * @retval None
  1957. */
  1958. void ETH_SetReceiveWatchdogTimer(uint8_t Value)
  1959. {
  1960. /* Set the DMA Receive status watchdog timer register */
  1961. ETH->DMARSWTR = Value;
  1962. }
  1963. /******************************************************************************/
  1964. /* PHY functions */
  1965. /******************************************************************************/
  1966. /**
  1967. * @brief Read a PHY register
  1968. * @param PHYAddress: PHY device address, is the index of one of supported 32 PHY devices.
  1969. * This parameter can be one of the following values: 0,..,31
  1970. * @param PHYReg: PHY register address, is the index of one of the 32 PHY register.
  1971. * This parameter can be one of the following values:
  1972. * @arg PHY_BCR: Transceiver Basic Control Register
  1973. * @arg PHY_BSR: Transceiver Basic Status Register
  1974. * @arg PHY_SR : Transceiver Status Register
  1975. * @arg More PHY register could be read depending on the used PHY
  1976. * @retval ETH_ERROR: in case of timeout
  1977. * MAC MIIDR register value: Data read from the selected PHY register (correct read )
  1978. */
  1979. uint16_t ETH_ReadPHYRegister(uint16_t PHYAddress, uint16_t PHYReg)
  1980. {
  1981. uint32_t tmpreg = 0;
  1982. __IO uint32_t timeout = 0;
  1983. /* Check the parameters */
  1984. assert_param(IS_ETH_PHY_ADDRESS(PHYAddress));
  1985. assert_param(IS_ETH_PHY_REG(PHYReg));
  1986. /* Get the ETHERNET MACMIIAR value */
  1987. tmpreg = ETH->MACMIIAR;
  1988. /* Keep only the CSR Clock Range CR[2:0] bits value */
  1989. tmpreg &= ~MACMIIAR_CR_MASK;
  1990. /* Prepare the MII address register value */
  1991. tmpreg |=(((uint32_t)PHYAddress<<11) & ETH_MACMIIAR_PA); /* Set the PHY device address */
  1992. tmpreg |=(((uint32_t)PHYReg<<6) & ETH_MACMIIAR_MR); /* Set the PHY register address */
  1993. tmpreg &= ~ETH_MACMIIAR_MW; /* Set the read mode */
  1994. tmpreg |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
  1995. /* Write the result value into the MII Address register */
  1996. ETH->MACMIIAR = tmpreg;
  1997. /* Check for the Busy flag */
  1998. do
  1999. {
  2000. timeout++;
  2001. tmpreg = ETH->MACMIIAR;
  2002. } while ((tmpreg & ETH_MACMIIAR_MB) && (timeout < (uint32_t)PHY_READ_TO));
  2003. /* Return ERROR in case of timeout */
  2004. if(timeout == PHY_READ_TO)
  2005. {
  2006. return (uint16_t)ETH_ERROR;
  2007. }
  2008. /* Return data register value */
  2009. return (uint16_t)(ETH->MACMIIDR);
  2010. }
  2011. /**
  2012. * @brief Write to a PHY register
  2013. * @param PHYAddress: PHY device address, is the index of one of supported 32 PHY devices.
  2014. * This parameter can be one of the following values: 0,..,31
  2015. * @param PHYReg: PHY register address, is the index of one of the 32 PHY register.
  2016. * This parameter can be one of the following values:
  2017. * @arg PHY_BCR : Transceiver Control Register
  2018. * @arg More PHY register could be written depending on the used PHY
  2019. * @param PHYValue: the value to write
  2020. * @retval ETH_ERROR: in case of timeout
  2021. * ETH_SUCCESS: for correct write
  2022. */
  2023. uint32_t ETH_WritePHYRegister(uint16_t PHYAddress, uint16_t PHYReg, uint16_t PHYValue)
  2024. {
  2025. uint32_t tmpreg = 0;
  2026. __IO uint32_t timeout = 0;
  2027. /* Check the parameters */
  2028. assert_param(IS_ETH_PHY_ADDRESS(PHYAddress));
  2029. assert_param(IS_ETH_PHY_REG(PHYReg));
  2030. /* Get the ETHERNET MACMIIAR value */
  2031. tmpreg = ETH->MACMIIAR;
  2032. /* Keep only the CSR Clock Range CR[2:0] bits value */
  2033. tmpreg &= ~MACMIIAR_CR_MASK;
  2034. /* Prepare the MII register address value */
  2035. tmpreg |=(((uint32_t)PHYAddress<<11) & ETH_MACMIIAR_PA); /* Set the PHY device address */
  2036. tmpreg |=(((uint32_t)PHYReg<<6) & ETH_MACMIIAR_MR); /* Set the PHY register address */
  2037. tmpreg |= ETH_MACMIIAR_MW; /* Set the write mode */
  2038. tmpreg |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
  2039. /* Give the value to the MII data register */
  2040. ETH->MACMIIDR = PHYValue;
  2041. /* Write the result value into the MII Address register */
  2042. ETH->MACMIIAR = tmpreg;
  2043. /* Check for the Busy flag */
  2044. do
  2045. {
  2046. timeout++;
  2047. tmpreg = ETH->MACMIIAR;
  2048. } while ((tmpreg & ETH_MACMIIAR_MB) && (timeout < (uint32_t)PHY_WRITE_TO));
  2049. /* Return ERROR in case of timeout */
  2050. if(timeout == PHY_WRITE_TO)
  2051. {
  2052. return ETH_ERROR;
  2053. }
  2054. /* Return SUCCESS */
  2055. return ETH_SUCCESS;
  2056. }
  2057. /**
  2058. * @brief Enables or disables the PHY loopBack mode.
  2059. * @Note: Don't be confused with ETH_MACLoopBackCmd function which enables internal
  2060. * loopback at MII level
  2061. * @param PHYAddress: PHY device address, is the index of one of supported 32 PHY devices.
  2062. * @param NewState: new state of the PHY loopBack mode.
  2063. * This parameter can be: ENABLE or DISABLE.
  2064. * @retval ETH_ERROR: in case of bad PHY configuration
  2065. * ETH_SUCCESS: for correct PHY configuration
  2066. */
  2067. uint32_t ETH_PHYLoopBackCmd(uint16_t PHYAddress, FunctionalState NewState)
  2068. {
  2069. uint16_t tmpreg = 0;
  2070. /* Check the parameters */
  2071. assert_param(IS_ETH_PHY_ADDRESS(PHYAddress));
  2072. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2073. /* Get the PHY configuration to update it */
  2074. tmpreg = ETH_ReadPHYRegister(PHYAddress, PHY_BCR);
  2075. if (NewState != DISABLE)
  2076. {
  2077. /* Enable the PHY loopback mode */
  2078. tmpreg |= PHY_Loopback;
  2079. }
  2080. else
  2081. {
  2082. /* Disable the PHY loopback mode: normal mode */
  2083. tmpreg &= (uint16_t)(~(uint16_t)PHY_Loopback);
  2084. }
  2085. /* Update the PHY control register with the new configuration */
  2086. if(ETH_WritePHYRegister(PHYAddress, PHY_BCR, tmpreg) != (uint32_t)RESET)
  2087. {
  2088. return ETH_SUCCESS;
  2089. }
  2090. else
  2091. {
  2092. /* Return SUCCESS */
  2093. return ETH_ERROR;
  2094. }
  2095. }
  2096. /******************************************************************************/
  2097. /* Power Management(PMT) functions */
  2098. /******************************************************************************/
  2099. /**
  2100. * @brief Reset Wakeup frame filter register pointer.
  2101. * @param None
  2102. * @retval None
  2103. */
  2104. void ETH_ResetWakeUpFrameFilterRegisterPointer(void)
  2105. {
  2106. /* Resets the Remote Wake-up Frame Filter register pointer to 0x0000 */
  2107. ETH->MACPMTCSR |= ETH_MACPMTCSR_WFFRPR;
  2108. }
  2109. /**
  2110. * @brief Populates the remote wakeup frame registers.
  2111. * @param Buffer: Pointer on remote WakeUp Frame Filter Register buffer data (8 words).
  2112. * @retval None
  2113. */
  2114. void ETH_SetWakeUpFrameFilterRegister(uint32_t *Buffer)
  2115. {
  2116. uint32_t i = 0;
  2117. /* Fill Remote Wake-up Frame Filter register with Buffer data */
  2118. for(i =0; i<ETH_WAKEUP_REGISTER_LENGTH; i++)
  2119. {
  2120. /* Write each time to the same register */
  2121. ETH->MACRWUFFR = Buffer[i];
  2122. }
  2123. }
  2124. /**
  2125. * @brief Enables or disables any unicast packet filtered by the MAC address
  2126. * recognition to be a wake-up frame.
  2127. * @param NewState: new state of the MAC Global Unicast Wake-Up.
  2128. * This parameter can be: ENABLE or DISABLE.
  2129. * @retval None
  2130. */
  2131. void ETH_GlobalUnicastWakeUpCmd(FunctionalState NewState)
  2132. {
  2133. /* Check the parameters */
  2134. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2135. if (NewState != DISABLE)
  2136. {
  2137. /* Enable the MAC Global Unicast Wake-Up */
  2138. ETH->MACPMTCSR |= ETH_MACPMTCSR_GU;
  2139. }
  2140. else
  2141. {
  2142. /* Disable the MAC Global Unicast Wake-Up */
  2143. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_GU;
  2144. }
  2145. }
  2146. /**
  2147. * @brief Checks whether the specified ETHERNET PMT flag is set or not.
  2148. * @param ETH_PMT_FLAG: specifies the flag to check.
  2149. * This parameter can be one of the following values:
  2150. * @arg ETH_PMT_FLAG_WUFFRPR : Wake-Up Frame Filter Register Pointer Reset
  2151. * @arg ETH_PMT_FLAG_WUFR : Wake-Up Frame Received
  2152. * @arg ETH_PMT_FLAG_MPR : Magic Packet Received
  2153. * @retval The new state of ETHERNET PMT Flag (SET or RESET).
  2154. */
  2155. FlagStatus ETH_GetPMTFlagStatus(uint32_t ETH_PMT_FLAG)
  2156. {
  2157. FlagStatus bitstatus = RESET;
  2158. /* Check the parameters */
  2159. assert_param(IS_ETH_PMT_GET_FLAG(ETH_PMT_FLAG));
  2160. if ((ETH->MACPMTCSR & ETH_PMT_FLAG) != (uint32_t)RESET)
  2161. {
  2162. bitstatus = SET;
  2163. }
  2164. else
  2165. {
  2166. bitstatus = RESET;
  2167. }
  2168. return bitstatus;
  2169. }
  2170. /**
  2171. * @brief Enables or disables the MAC Wake-Up Frame Detection.
  2172. * @param NewState: new state of the MAC Wake-Up Frame Detection.
  2173. * This parameter can be: ENABLE or DISABLE.
  2174. * @retval None
  2175. */
  2176. void ETH_WakeUpFrameDetectionCmd(FunctionalState NewState)
  2177. {
  2178. /* Check the parameters */
  2179. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2180. if (NewState != DISABLE)
  2181. {
  2182. /* Enable the MAC Wake-Up Frame Detection */
  2183. ETH->MACPMTCSR |= ETH_MACPMTCSR_WFE;
  2184. }
  2185. else
  2186. {
  2187. /* Disable the MAC Wake-Up Frame Detection */
  2188. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_WFE;
  2189. }
  2190. }
  2191. /**
  2192. * @brief Enables or disables the MAC Magic Packet Detection.
  2193. * @param NewState: new state of the MAC Magic Packet Detection.
  2194. * This parameter can be: ENABLE or DISABLE.
  2195. * @retval None
  2196. */
  2197. void ETH_MagicPacketDetectionCmd(FunctionalState NewState)
  2198. {
  2199. /* Check the parameters */
  2200. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2201. if (NewState != DISABLE)
  2202. {
  2203. /* Enable the MAC Magic Packet Detection */
  2204. ETH->MACPMTCSR |= ETH_MACPMTCSR_MPE;
  2205. }
  2206. else
  2207. {
  2208. /* Disable the MAC Magic Packet Detection */
  2209. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_MPE;
  2210. }
  2211. }
  2212. /**
  2213. * @brief Enables or disables the MAC Power Down.
  2214. * @param NewState: new state of the MAC Power Down.
  2215. * This parameter can be: ENABLE or DISABLE.
  2216. * @retval None
  2217. */
  2218. void ETH_PowerDownCmd(FunctionalState NewState)
  2219. {
  2220. /* Check the parameters */
  2221. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2222. if (NewState != DISABLE)
  2223. {
  2224. /* Enable the MAC Power Down */
  2225. /* This puts the MAC in power down mode */
  2226. ETH->MACPMTCSR |= ETH_MACPMTCSR_PD;
  2227. }
  2228. else
  2229. {
  2230. /* Disable the MAC Power Down */
  2231. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_PD;
  2232. }
  2233. }
  2234. /******************************************************************************/
  2235. /* MMC functions */
  2236. /******************************************************************************/
  2237. /**
  2238. * @brief Preset and Initialize the MMC counters to almost-full value: 0xFFFF_FFF0 (full - 16)
  2239. * @param None
  2240. * @retval None
  2241. */
  2242. void ETH_MMCCounterFullPreset(void)
  2243. {
  2244. /* Preset and Initialize the MMC counters to almost-full value */
  2245. ETH->MMCCR |= ETH_MMCCR_MCFHP | ETH_MMCCR_MCP;
  2246. }
  2247. /**
  2248. * @brief Preset and Initialize the MMC counters to almost-hal value: 0x7FFF_FFF0 (half - 16).
  2249. * @param None
  2250. * @retval None
  2251. */
  2252. void ETH_MMCCounterHalfPreset(void)
  2253. {
  2254. /* Preset the MMC counters to almost-full value */
  2255. ETH->MMCCR &= ~ETH_MMCCR_MCFHP;
  2256. /* Initialize the MMC counters to almost-half value */
  2257. ETH->MMCCR |= ETH_MMCCR_MCP;
  2258. }
  2259. /**
  2260. * @brief Enables or disables the MMC Counter Freeze.
  2261. * @param NewState: new state of the MMC Counter Freeze.
  2262. * This parameter can be: ENABLE or DISABLE.
  2263. * @retval None
  2264. */
  2265. void ETH_MMCCounterFreezeCmd(FunctionalState NewState)
  2266. {
  2267. /* Check the parameters */
  2268. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2269. if (NewState != DISABLE)
  2270. {
  2271. /* Enable the MMC Counter Freeze */
  2272. ETH->MMCCR |= ETH_MMCCR_MCF;
  2273. }
  2274. else
  2275. {
  2276. /* Disable the MMC Counter Freeze */
  2277. ETH->MMCCR &= ~ETH_MMCCR_MCF;
  2278. }
  2279. }
  2280. /**
  2281. * @brief Enables or disables the MMC Reset On Read.
  2282. * @param NewState: new state of the MMC Reset On Read.
  2283. * This parameter can be: ENABLE or DISABLE.
  2284. * @retval None
  2285. */
  2286. void ETH_MMCResetOnReadCmd(FunctionalState NewState)
  2287. {
  2288. /* Check the parameters */
  2289. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2290. if (NewState != DISABLE)
  2291. {
  2292. /* Enable the MMC Counter reset on read */
  2293. ETH->MMCCR |= ETH_MMCCR_ROR;
  2294. }
  2295. else
  2296. {
  2297. /* Disable the MMC Counter reset on read */
  2298. ETH->MMCCR &= ~ETH_MMCCR_ROR;
  2299. }
  2300. }
  2301. /**
  2302. * @brief Enables or disables the MMC Counter Stop Rollover.
  2303. * @param NewState: new state of the MMC Counter Stop Rollover.
  2304. * This parameter can be: ENABLE or DISABLE.
  2305. * @retval None
  2306. */
  2307. void ETH_MMCCounterRolloverCmd(FunctionalState NewState)
  2308. {
  2309. /* Check the parameters */
  2310. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2311. if (NewState != DISABLE)
  2312. {
  2313. /* Disable the MMC Counter Stop Rollover */
  2314. ETH->MMCCR &= ~ETH_MMCCR_CSR;
  2315. }
  2316. else
  2317. {
  2318. /* Enable the MMC Counter Stop Rollover */
  2319. ETH->MMCCR |= ETH_MMCCR_CSR;
  2320. }
  2321. }
  2322. /**
  2323. * @brief Resets the MMC Counters.
  2324. * @param None
  2325. * @retval None
  2326. */
  2327. void ETH_MMCCountersReset(void)
  2328. {
  2329. /* Resets the MMC Counters */
  2330. ETH->MMCCR |= ETH_MMCCR_CR;
  2331. }
  2332. /**
  2333. * @brief Enables or disables the specified ETHERNET MMC interrupts.
  2334. * @param ETH_MMC_IT: specifies the ETHERNET MMC interrupt sources to be enabled or disabled.
  2335. * This parameter can be any combination of Tx interrupt or
  2336. * any combination of Rx interrupt (but not both)of the following values:
  2337. * @arg ETH_MMC_IT_TGF : When Tx good frame counter reaches half the maximum value
  2338. * @arg ETH_MMC_IT_TGFMSC: When Tx good multi col counter reaches half the maximum value
  2339. * @arg ETH_MMC_IT_TGFSC : When Tx good single col counter reaches half the maximum value
  2340. * @arg ETH_MMC_IT_RGUF : When Rx good unicast frames counter reaches half the maximum value
  2341. * @arg ETH_MMC_IT_RFAE : When Rx alignment error counter reaches half the maximum value
  2342. * @arg ETH_MMC_IT_RFCE : When Rx crc error counter reaches half the maximum value
  2343. * @param NewState: new state of the specified ETHERNET MMC interrupts.
  2344. * This parameter can be: ENABLE or DISABLE.
  2345. * @retval None
  2346. */
  2347. void ETH_MMCITConfig(uint32_t ETH_MMC_IT, FunctionalState NewState)
  2348. {
  2349. /* Check the parameters */
  2350. assert_param(IS_ETH_MMC_IT(ETH_MMC_IT));
  2351. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2352. if ((ETH_MMC_IT & (uint32_t)0x10000000) != (uint32_t)RESET)
  2353. {
  2354. /* Remove Register mak from IT */
  2355. ETH_MMC_IT &= 0xEFFFFFFF;
  2356. /* ETHERNET MMC Rx interrupts selected */
  2357. if (NewState != DISABLE)
  2358. {
  2359. /* Enable the selected ETHERNET MMC interrupts */
  2360. ETH->MMCRIMR &=(~(uint32_t)ETH_MMC_IT);
  2361. }
  2362. else
  2363. {
  2364. /* Disable the selected ETHERNET MMC interrupts */
  2365. ETH->MMCRIMR |= ETH_MMC_IT;
  2366. }
  2367. }
  2368. else
  2369. {
  2370. /* ETHERNET MMC Tx interrupts selected */
  2371. if (NewState != DISABLE)
  2372. {
  2373. /* Enable the selected ETHERNET MMC interrupts */
  2374. ETH->MMCTIMR &=(~(uint32_t)ETH_MMC_IT);
  2375. }
  2376. else
  2377. {
  2378. /* Disable the selected ETHERNET MMC interrupts */
  2379. ETH->MMCTIMR |= ETH_MMC_IT;
  2380. }
  2381. }
  2382. }
  2383. /**
  2384. * @brief Checks whether the specified ETHERNET MMC IT is set or not.
  2385. * @param ETH_MMC_IT: specifies the ETHERNET MMC interrupt.
  2386. * This parameter can be one of the following values:
  2387. * @arg ETH_MMC_IT_TxFCGC: When Tx good frame counter reaches half the maximum value
  2388. * @arg ETH_MMC_IT_TxMCGC: When Tx good multi col counter reaches half the maximum value
  2389. * @arg ETH_MMC_IT_TxSCGC: When Tx good single col counter reaches half the maximum value
  2390. * @arg ETH_MMC_IT_RxUGFC: When Rx good unicast frames counter reaches half the maximum value
  2391. * @arg ETH_MMC_IT_RxAEC : When Rx alignment error counter reaches half the maximum value
  2392. * @arg ETH_MMC_IT_RxCEC : When Rx crc error counter reaches half the maximum value
  2393. * @retval The value of ETHERNET MMC IT (SET or RESET).
  2394. */
  2395. ITStatus ETH_GetMMCITStatus(uint32_t ETH_MMC_IT)
  2396. {
  2397. ITStatus bitstatus = RESET;
  2398. /* Check the parameters */
  2399. assert_param(IS_ETH_MMC_GET_IT(ETH_MMC_IT));
  2400. if ((ETH_MMC_IT & (uint32_t)0x10000000) != (uint32_t)RESET)
  2401. {
  2402. /* ETHERNET MMC Rx interrupts selected */
  2403. /* Check if the ETHERNET MMC Rx selected interrupt is enabled and occurred */
  2404. if ((((ETH->MMCRIR & ETH_MMC_IT) != (uint32_t)RESET)) && ((ETH->MMCRIMR & ETH_MMC_IT) == (uint32_t)RESET))
  2405. {
  2406. bitstatus = SET;
  2407. }
  2408. else
  2409. {
  2410. bitstatus = RESET;
  2411. }
  2412. }
  2413. else
  2414. {
  2415. /* ETHERNET MMC Tx interrupts selected */
  2416. /* Check if the ETHERNET MMC Tx selected interrupt is enabled and occurred */
  2417. if ((((ETH->MMCTIR & ETH_MMC_IT) != (uint32_t)RESET)) && ((ETH->MMCRIMR & ETH_MMC_IT) == (uint32_t)RESET))
  2418. {
  2419. bitstatus = SET;
  2420. }
  2421. else
  2422. {
  2423. bitstatus = RESET;
  2424. }
  2425. }
  2426. return bitstatus;
  2427. }
  2428. /**
  2429. * @brief Get the specified ETHERNET MMC register value.
  2430. * @param ETH_MMCReg: specifies the ETHERNET MMC register.
  2431. * This parameter can be one of the following values:
  2432. * @arg ETH_MMCCR : MMC CR register
  2433. * @arg ETH_MMCRIR : MMC RIR register
  2434. * @arg ETH_MMCTIR : MMC TIR register
  2435. * @arg ETH_MMCRIMR : MMC RIMR register
  2436. * @arg ETH_MMCTIMR : MMC TIMR register
  2437. * @arg ETH_MMCTGFSCCR : MMC TGFSCCR register
  2438. * @arg ETH_MMCTGFMSCCR: MMC TGFMSCCR register
  2439. * @arg ETH_MMCTGFCR : MMC TGFCR register
  2440. * @arg ETH_MMCRFCECR : MMC RFCECR register
  2441. * @arg ETH_MMCRFAECR : MMC RFAECR register
  2442. * @arg ETH_MMCRGUFCR : MMC RGUFCRregister
  2443. * @retval The value of ETHERNET MMC Register value.
  2444. */
  2445. uint32_t ETH_GetMMCRegister(uint32_t ETH_MMCReg)
  2446. {
  2447. /* Check the parameters */
  2448. assert_param(IS_ETH_MMC_REGISTER(ETH_MMCReg));
  2449. /* Return the selected register value */
  2450. return (*(__IO uint32_t *)(ETH_MAC_BASE + ETH_MMCReg));
  2451. }
  2452. /**
  2453. * @}
  2454. */
  2455. /**
  2456. * @}
  2457. */