前景描述& O' ^" p3 U0 p; Y
我司的以太网实现是LAN8720搭配片上MAC层做的,协议栈用LWIP* t2 M( `7 Y( a& M6 r
: v+ [- P. Y9 @# g& f
这种做法应该还是蛮常见的,反正不是LAN8720就是DP83864,再不然就是硬件协议栈的W5500了
# c8 B! g7 V6 \: s& ~: O; O& V5 p) X$ N; W. ]0 Z$ R
想想还是有MAC层过滤比较好,随手翻了翻它的手册,W5500好像是没得MAC层过滤的
1 [, E5 j$ M3 P, j0 [$ S# Y0 m: r+ E. N( I: `, |
假如数据速率比较高,数据量大,而且外面还有乱七八糟无关的数据往板卡送,我也不知道用这个片子到底好不好- {2 I D" `7 z' r
+ F' ?, v# \! N) T8 l. b
LAN8720也不好改了,就继续用吧
4 l) [# b+ e# q# r6 N1 k9 u0 P$ q) Q; H- t( `5 a) Q
好好在,发给板卡的数据的MAC还是有特征的,我只需要过滤发送方MAC地址的前3个字节,也就是特定的厂家那段
* B5 w& U) X- y2 }) ~* F0 G. H
, Q- ]! J5 y" F, }( U# o4 MMAC地址的科普知识这里就不过多描述了,我们直奔主题
( r1 _- w, s0 t( ^
. C+ z5 j |0 D7 u9 U* J8 w怎么用STM32 F7的片上MAC做MAC层过滤# q! \1 X% R/ V* ~+ f9 i8 O, H
) u) s7 n) u- ^2 N
写这个的目的,就是我搜了搜,没看见有人写这个MAC层过滤功能,具体代码怎么写才能用3 S( o3 u9 c' t) v
% }8 G7 h' z/ b( }) r& W知识科普-STM32的片上MAC
, V0 M# D! U" U3 i2 h2 c3 QSTM32 F7的MAC层过滤方式有三种(说人话的直白解释,中文手册其实也翻译的不好)
) g/ O: l# F' m9 H8 I( X/ K精确过滤(手册上叫单播目标/源地址过滤)8 V5 @9 g9 d* v0 |( W# m9 c: p/ |0 k# U! ]
粗糙过滤(手册上叫Hash表过滤)
7 b1 A. R# d0 O! k* ]! g% A- _特殊过滤(广播,组播过滤等等特殊的过滤)# B# S! G4 u# b9 A8 g$ x; S
我这里着重说第1种和第3种- K7 E% ?7 S0 E, ?( E
3 _: m0 v1 F S9 D) O5 ?& ^( [0 Q
STM32 F7可以存4个MAC地址. b. o8 w2 S3 |: K1 F
其中MAC地址0是自己的MAC,其他三个可以用来做过滤
1 S) z8 k3 s) {, k$ O0 V. s( F4 K9 Z% q: c8 S
网络数据包通过PHY进入STM32的MAC,MAC层是可以对目的地址/源地址检查并过滤掉没得用的数据的,默认的HAL库库函数HAL_ETH_Init中
0 T3 Y8 h7 X6 F* K. f' U1 {0 K3 g有一个static的函数
: C Y. b2 W, c, c+ estatic void ETH_MACDMAConfig(ETH_HandleTypeDef *heth, uint32_t err)7 T% C* q( `* J7 x; G' P. c% ~
会帮你在初始化网络功能时初始化默认的MAC层功能:
! l# o, j% r; ]* L关闭源地址过滤
7 J9 V: w# M. @" X& u不接收所有数据
0 h" ~3 L) `9 Z接收广播帧
6 n2 i H2 q% s. Y: n/ j9 J2 I打开目的地地址过滤
o$ N- l# ~3 N$ u d单播数据精确过滤3 x0 e. D# x7 R9 P
组播数据精确过滤. p3 }8 T" W8 [* |
Hash表过滤关闭# l1 a6 _, W3 V
2 i! \" _5 s: B6 ?+ ~. o
其他的功能自行研究,我这里捡重要的说2 j& V) W5 \. G2 \5 b
- static void ETH_MACDMAConfig(ETH_HandleTypeDef *heth, uint32_t err)( i0 |2 o# m' Q7 y
- {6 [( y( C' k4 o3 b# `5 O% o
- ETH_MACInitTypeDef macinit;
3 y: a) g) h+ e - ETH_DMAInitTypeDef dmainit;: N7 @" m5 f1 N) |
- uint32_t tmpreg = 0;% Q, n# T' g( k% a$ i+ S& }* t
- * Z$ B+ v" l$ U* N N5 u/ d1 x
- if (err != ETH_SUCCESS) /* Auto-negotiation failed */
0 U0 S% ^& r5 `: ?6 E9 \, R - {
* Y# d2 F- L, h: G( ?" j, [ - /* Set Ethernet duplex mode to Full-duplex */
. u/ [8 ^9 A0 z) ?- [ - (heth->Init).DuplexMode = ETH_MODE_FULLDUPLEX;$ A2 P/ N, O1 |3 n6 m. n; p) N
- + k3 t5 f2 i5 N4 A8 V1 B3 }
- /* Set Ethernet speed to 100M */' B+ i6 y/ |/ ~/ D
- (heth->Init).Speed = ETH_SPEED_100M;
1 x q8 k6 ^. {/ ]0 c - }
1 F' H6 n" O2 }+ i% ?
0 ~# e( t1 c3 I: Q$ |& n- /* Ethernet MAC default initialization **************************************/
5 l" J" E% |; K( @* H& M$ K. P- C - macinit.Watchdog = ETH_WATCHDOG_ENABLE;
( s* g7 a" w1 N6 b0 [( n8 D# y - macinit.Jabber = ETH_JABBER_ENABLE;
: y$ n7 e+ z& [% L - macinit.InterFrameGap = ETH_INTERFRAMEGAP_96BIT;( F" b6 j& s) y1 E
- macinit.CarrierSense = ETH_CARRIERSENCE_ENABLE;- w. y3 W! k( K( ?* k
- macinit.ReceiveOwn = ETH_RECEIVEOWN_ENABLE;
8 W& S2 s+ _; |7 x4 E - macinit.LoopbackMode = ETH_LOOPBACKMODE_DISABLE;
( t8 ^) U- k+ y( g3 C7 c5 R4 ]9 H - if(heth->Init.ChecksumMode == ETH_CHECKSUM_BY_HARDWARE)
+ M" k! T. m' H( q k0 a3 d - {
3 u1 _( J7 ^0 M8 t; H) G$ z, @$ N - macinit.ChecksumOffload = ETH_CHECKSUMOFFLAOD_ENABLE;
1 g# [8 g7 c2 a( K. D- C- N" N - }
1 A. o! C4 }1 k5 e! t6 B - else8 O& ]' K' s: F& S+ ^4 Q
- {
; \, q T7 v) \3 }( H7 G( V - macinit.ChecksumOffload = ETH_CHECKSUMOFFLAOD_DISABLE;
/ I2 g" o4 U$ N+ g4 t& a3 ? - }" J7 R( n) s3 \9 f7 d6 |" D9 o
- //这一段是MAC层过滤的设置,下面是DMA的,那个用默认的就可以
+ p1 ]% ]* y% D8 Q4 [ - macinit.RetryTransmission = ETH_RETRYTRANSMISSION_DISABLE;
' E" O4 w* \3 K! V& W - macinit.AutomaticPadCRCStrip = ETH_AUTOMATICPADCRCSTRIP_DISABLE; Q6 {! E( Y- l! C! ~! H4 X
- macinit.BackOffLimit = ETH_BACKOFFLIMIT_10;
! t, T8 x1 U8 V4 Z - macinit.DeferralCheck = ETH_DEFFERRALCHECK_DISABLE;
: d3 t; a5 u8 Y, X - macinit.ReceiveAll = ETH_RECEIVEAll_DISABLE;' \6 v$ r& F2 P/ ] ?3 w' o
- macinit.SourceAddrFilter = ETH_SOURCEADDRFILTER_DISABLE; P y* j8 @- m& V/ u
- macinit.PassControlFrames = ETH_PASSCONTROLFRAMES_BLOCKALL;
+ e9 T' u ]' U+ N: t7 p% k - macinit.BroadcastFramesReception = ETH_BROADCASTFRAMESRECEPTION_ENABLE;! U2 k. w9 A5 i3 v3 i
- macinit.DestinationAddrFilter = ETH_DESTINATIONADDRFILTER_NORMAL;4 G7 `$ {2 g& w# D$ ~' J" X2 d
- macinit.PromiscuousMode = ETH_PROMISCUOUS_MODE_DISABLE;4 a) n# V4 i6 U a) z
- macinit.MulticastFramesFilter = ETH_MULTICASTFRAMESFILTER_PERFECT;
0 I4 G6 z, ?- X: U - macinit.UnicastFramesFilter = ETH_UNICASTFRAMESFILTER_PERFECT;+ |2 M* C m5 a2 n( Z
- macinit.HashTableHigh = 0x0;4 j2 t L* W! M& X9 ?0 j
- macinit.HashTableLow = 0x0;
. U; W5 P; w5 ` - macinit.PauseTime = 0x0;8 L$ w3 W- Q9 M- F( m7 `5 y
- macinit.ZeroQuantaPause = ETH_ZEROQUANTAPAUSE_DISABLE;" e+ a9 u9 ]' s1 d
- macinit.PauseLowThreshold = ETH_PAUSELOWTHRESHOLD_MINUS4;4 s# H3 G* I! X4 K5 o0 R! @
- macinit.UnicastPauseFrameDetect = ETH_UNICASTPAUSEFRAMEDETECT_DISABLE;5 d4 g0 i! y( ?- F' Z, w7 l7 b
- macinit.ReceiveFlowControl = ETH_RECEIVEFLOWCONTROL_DISABLE;
6 b$ y z: [4 f: f; I; z - macinit.TransmitFlowControl = ETH_TRANSMITFLOWCONTROL_DISABLE;. F0 p% h9 R7 I0 i. t6 m
- macinit.VLANTagComparison = ETH_VLANTAGCOMPARISON_16BIT;+ T9 m1 H# |6 \- C$ [. B
- macinit.VLANTagIdentifier = 0x0;, V% [. k8 I# ]5 t* u
- 4 n& ]4 p( R" z2 v5 h
- /*------------------------ ETHERNET MACCR Configuration --------------------*/
/ ~2 E) [6 `7 C+ G3 v) Q - /* Get the ETHERNET MACCR value *// S0 H7 }- r8 V% b
- tmpreg = (heth->Instance)->MACCR;
7 s# K$ ~3 {6 J- [ - /* Clear WD, PCE, PS, TE and RE bits */
" p) y' \* U( {0 d- h9 B5 y - tmpreg &= ETH_MACCR_CLEAR_MASK;3 P! ?; f6 i1 r% n! R
- /* Set the WD bit according to ETH Watchdog value */
( Q5 o) x" S, J8 P- U2 ~ - /* Set the JD: bit according to ETH Jabber value */* f8 u5 F) E$ U" x- {# }
- /* Set the IFG bit according to ETH InterFrameGap value */
8 H7 ]+ f2 f, M4 G( s - /* Set the DCRS bit according to ETH CarrierSense value */
, q; _$ t/ k) S9 h; A - /* Set the FES bit according to ETH Speed value */3 w( n9 @7 H( z+ D5 _+ I) g$ K
- /* Set the DO bit according to ETH ReceiveOwn value */. g9 X# F0 M3 W2 ?- Z
- /* Set the LM bit according to ETH LoopbackMode value */$ }, W8 S' }$ a! i8 d6 ]
- /* Set the DM bit according to ETH Mode value */" i5 f6 r5 K/ [. {' B8 N
- /* Set the IPCO bit according to ETH ChecksumOffload value */2 ?3 _) N4 @2 F
- /* Set the DR bit according to ETH RetryTransmission value */
' b1 p( T- ^5 i/ z - /* Set the ACS bit according to ETH AutomaticPadCRCStrip value */
! V, t: h' V* T$ w' @ - /* Set the BL bit according to ETH BackOffLimit value */. S/ U5 Y8 z% m/ W( @
- /* Set the DC bit according to ETH DeferralCheck value */* J% P. V( A3 c) R0 }1 s
- tmpreg |= (uint32_t)(macinit.Watchdog |
: Y4 D1 S4 S1 K, o- _+ X - macinit.Jabber |
4 s9 r1 B0 ?7 l) p - macinit.InterFrameGap |' _+ t5 m- w2 D$ p5 b' p& a
- macinit.CarrierSense |, h% t: A, H% h- `/ Z
- (heth->Init).Speed |
* Y1 ^" c1 N) e9 a - macinit.ReceiveOwn |; u* w8 ~/ f& V1 |: v( l; n# g3 o* ]
- macinit.LoopbackMode |
0 u" b- R; U$ x$ ]- X - (heth->Init).DuplexMode |
' w& l1 l C' `4 l3 z8 X9 @ - macinit.ChecksumOffload | J A8 Q; g: I: S E
- macinit.RetryTransmission |6 B% Q6 J' x& s8 @2 a6 J
- macinit.AutomaticPadCRCStrip |
* o# s& f6 p; F) c4 \ - macinit.BackOffLimit |
1 D: U, b: l$ a: F - macinit.DeferralCheck);3 I2 h' T' N! g# b5 M& x
7 {7 I$ I2 q( @3 A$ j9 V8 F- /* Write to ETHERNET MACCR */
, q' z( H8 C/ b3 R/ W - (heth->Instance)->MACCR = (uint32_t)tmpreg;4 p9 T* I9 H4 b g* u7 M5 Q
- 1 }( E. @ g4 [9 c5 H) t
- /* Wait until the write operation will be taken into account:/ S! N2 h c- a" N( p5 x6 B; O8 e
- at least four TX_CLK/RX_CLK clock cycles */
# O" _' j: J( |+ U: W9 d$ v. J - tmpreg = (heth->Instance)->MACCR;* k2 x. z6 @& ~; G
- HAL_Delay(ETH_REG_WRITE_DELAY);" J- {* h0 n# X9 e2 n0 Q7 A7 E& R
- (heth->Instance)->MACCR = tmpreg;
* I8 M9 M2 e6 T0 r9 E
; g( X5 u& G2 o% o- d6 ^& O9 S( y- /*----------------------- ETHERNET MACFFR Configuration --------------------*/, [% T& {7 g7 e. H; X0 k
- /* Set the RA bit according to ETH ReceiveAll value */
3 W* j6 e. c: z% z7 C - /* Set the SAF and SAIF bits according to ETH SourceAddrFilter value */
$ v" H8 f3 x4 v5 g - /* Set the PCF bit according to ETH PassControlFrames value */
5 G! y B: Q( N9 U - /* Set the DBF bit according to ETH BroadcastFramesReception value */" G) F. j( D* _/ E
- /* Set the DAIF bit according to ETH DestinationAddrFilter value */
! }) g1 c: w, V - /* Set the PR bit according to ETH PromiscuousMode value */0 H) \' M9 t/ L" Y4 D5 u
- /* Set the PM, HMC and HPF bits according to ETH MulticastFramesFilter value */
/ H# w, @5 @% d5 D% C' @ - /* Set the HUC and HPF bits according to ETH UnicastFramesFilter value */* E4 T! k9 R4 w+ [$ u2 @: X# d5 W
- /* Write to ETHERNET MACFFR */8 X& `/ Z$ X0 A8 E! I
- (heth->Instance)->MACFFR = (uint32_t)(macinit.ReceiveAll | a5 V. d; G5 S# S# A: }
- macinit.SourceAddrFilter |; N3 @5 Q1 ?3 [" i, ?+ P, {
- macinit.PassControlFrames |
) M3 d) ]3 Q8 I - macinit.BroadcastFramesReception |
' L5 R. h7 L3 P. Y. A2 G: r - macinit.DestinationAddrFilter |
4 B. Q0 i, G& ^7 i - macinit.PromiscuousMode |
; F4 W0 E( n; m" c: e1 E. G+ E7 Y* L - macinit.MulticastFramesFilter |* _/ r$ p i; D
- macinit.UnicastFramesFilter);
% j3 {- t) M1 g: p' U. c
" N6 h2 N4 p: w' H, g2 f" ^5 @6 p- /* Wait until the write operation will be taken into account:
* o* f& S$ B, Q# I- o/ y - at least four TX_CLK/RX_CLK clock cycles */
' g: v2 p8 V& _: S3 [ - tmpreg = (heth->Instance)->MACFFR;
* {5 ^ _! d c/ l6 v; ?4 g - HAL_Delay(ETH_REG_WRITE_DELAY);) P5 Q# i ]; ?2 w Z# C' j
- (heth->Instance)->MACFFR = tmpreg;
' W3 {0 f, c3 d2 c! S' H& [1 I- n- ~
/ H [8 I2 s$ J' s- /*--------------- ETHERNET MACHTHR and MACHTLR Configuration --------------*/
* q- v4 H2 p5 X' M( s - /* Write to ETHERNET MACHTHR */
5 S: e2 _5 X* w$ c) F - (heth->Instance)->MACHTHR = (uint32_t)macinit.HashTableHigh;
0 O* {# ?. c" Y5 X, @
+ Q% C& b& X# G6 u- /* Write to ETHERNET MACHTLR */) }- L' j6 r( d
- (heth->Instance)->MACHTLR = (uint32_t)macinit.HashTableLow;$ F7 A+ u2 Y! n, _. ~7 D% c# O
- /*----------------------- ETHERNET MACFCR Configuration -------------------*/3 H2 ]5 D" t& B
- ) T! V) k6 _, c n# [7 M8 C3 ~" y: \* F
- /* Get the ETHERNET MACFCR value */
/ I1 j. ]" V4 s7 o) P7 @ - tmpreg = (heth->Instance)->MACFCR;
! n" W* `. o, } - /* Clear xx bits */
8 ^ w4 u5 J; ^' O- z$ l3 t$ A - tmpreg &= ETH_MACFCR_CLEAR_MASK; M5 T* d* p; c: v
2 Q) G. g+ R* P- /* Set the PT bit according to ETH PauseTime value */
# Q5 }/ k9 R7 M& B X: ^2 e - /* Set the DZPQ bit according to ETH ZeroQuantaPause value */8 v0 ^# d) ]- w! J7 n6 l
- /* Set the PLT bit according to ETH PauseLowThreshold value */
; D/ _: R2 p6 ?/ ]' T - /* Set the UP bit according to ETH UnicastPauseFrameDetect value */1 \. c9 q, L- V( D$ @3 u% l
- /* Set the RFE bit according to ETH ReceiveFlowControl value */4 H& e; X: X8 n. M2 q R2 ^
- /* Set the TFE bit according to ETH TransmitFlowControl value */
$ u0 \' r7 n7 s3 z - tmpreg |= (uint32_t)((macinit.PauseTime Instance)->MACFCR = (uint32_t)tmpreg;
. V7 F/ D. H% x% u% f - 4 P4 l3 k0 S- u
- /* Wait until the write operation will be taken into account:
3 W( Q! t! a4 T1 [ - at least four TX_CLK/RX_CLK clock cycles */
; W! ^2 j3 |9 w5 t6 o) N - tmpreg = (heth->Instance)->MACFCR;
9 c) d# d2 m4 F1 O' D0 d. Y - HAL_Delay(ETH_REG_WRITE_DELAY);
; L6 f* l' }( i# @8 f7 h5 S - (heth->Instance)->MACFCR = tmpreg;2 I! M7 J( ^5 Z$ o6 y
- 2 Y6 {3 U8 w0 p: F% v3 o w6 `- r
- /*----------------------- ETHERNET MACVLANTR Configuration ----------------*/" a: J7 B7 K8 c0 j
- /* Set the ETV bit according to ETH VLANTagComparison value */% z) t) O7 r) X* D
- /* Set the VL bit according to ETH VLANTagIdentifier value */+ I4 I4 t+ Y: m* m" C: o
- (heth->Instance)->MACVLANTR = (uint32_t)(macinit.VLANTagComparison |' @/ u& {: P" r7 t
- macinit.VLANTagIdentifier);
1 `3 a4 H. r; U' H
0 s+ o: a9 i/ d' t3 y, \/ s3 x- /* Wait until the write operation will be taken into account: D) g8 z" P) g5 G1 q8 ~; Y/ i
- at least four TX_CLK/RX_CLK clock cycles */7 ~' K- U8 s& X, H; | W! n
- tmpreg = (heth->Instance)->MACVLANTR;% c0 A( }8 k4 g0 V' W1 E
- HAL_Delay(ETH_REG_WRITE_DELAY);! g: p4 h5 J, c2 h, u$ R* D
- (heth->Instance)->MACVLANTR = tmpreg;
6 T o5 B. ?( B) K - ; O8 {- R( H* J4 E& x" w* Y
- /* Ethernet DMA default initialization ************************************/! q$ F0 S1 j9 G5 [: ~) ?! |4 D) Y
- dmainit.DropTCPIPChecksumErrorFrame = ETH_DROPTCPIPCHECKSUMERRORFRAME_ENABLE;4 x0 x4 }4 M7 b+ x, V: w- K
- dmainit.ReceiveStoreForward = ETH_RECEIVESTOREFORWARD_ENABLE;. \) |- e, d, U% U5 W: q3 J
- dmainit.FlushReceivedFrame = ETH_FLUSHRECEIVEDFRAME_ENABLE;
& B9 Y( x& S7 b) |& ?' a - dmainit.TransmitStoreForward = ETH_TRANSMITSTOREFORWARD_ENABLE;
/ Y6 u: T X) ~, K/ H; F5 B- W - dmainit.TransmitThresholdControl = ETH_TRANSMITTHRESHOLDCONTROL_64BYTES;
: V6 X& ^/ I$ q1 n- r5 W' t - dmainit.ForwardErrorFrames = ETH_FORWARDERRORFRAMES_DISABLE;5 U2 y f5 ]: w7 m. C' R! _# x2 p
- dmainit.ForwardUndersizedGoodFrames = ETH_FORWARDUNDERSIZEDGOODFRAMES_DISABLE;
# @; n5 q( l/ f0 X - dmainit.ReceiveThresholdControl = ETH_RECEIVEDTHRESHOLDCONTROL_64BYTES;) Z3 r, {0 } S+ Z8 Y& G- y
- dmainit.SecondFrameOperate = ETH_SECONDFRAMEOPERARTE_ENABLE;1 Z' X! o- q" Q
- dmainit.AddressAlignedBeats = ETH_ADDRESSALIGNEDBEATS_ENABLE;) ]7 B! q _5 }. J0 F% r1 l
- dmainit.FixedBurst = ETH_FIXEDBURST_ENABLE;% L0 H* r. c( Q6 O# y
- dmainit.RxDMABurstLength = ETH_RXDMABURSTLENGTH_32BEAT;
8 b: V7 u% N6 q) K7 |- F - dmainit.TxDMABurstLength = ETH_TXDMABURSTLENGTH_32BEAT;
% T L2 [4 G, k$ y ?+ R& } - dmainit.EnhancedDescriptorFormat = ETH_DMAENHANCEDDESCRIPTOR_ENABLE;
" b& G* T% f& X( ~& s" i$ v6 J - dmainit.DescriptorSkipLength = 0x0;
: q g% H2 L* _ - dmainit.DMAArbitration = ETH_DMAARBITRATION_ROUNDROBIN_RXTX_1_1;
: k- D& F- d1 Y8 i$ Q$ `! p D
( G4 O" A3 \% q" H& R2 d i- /* Get the ETHERNET DMAOMR value */
+ Z! O5 q) f- K( [ m. r - tmpreg = (heth->Instance)->DMAOMR;
8 W" c( i+ R# Q8 V! D - /* Clear xx bits */
& W' F- E" L3 z$ S1 q$ e1 k - tmpreg &= ETH_DMAOMR_CLEAR_MASK;' a5 k% _: m6 j
3 w/ _1 q1 V5 @1 }# a- /* Set the DT bit according to ETH DropTCPIPChecksumErrorFrame value */
$ R7 ]1 D S% b) p g6 H' X+ i - /* Set the RSF bit according to ETH ReceiveStoreForward value */
; m7 J6 e( T9 s9 M( q! ~; ? - /* Set the DFF bit according to ETH FlushReceivedFrame value */% \ S' K O* \# h+ B: R7 g5 G
- /* Set the TSF bit according to ETH TransmitStoreForward value */. ] c4 b( m( b, G# V( c/ H
- /* Set the TTC bit according to ETH TransmitThresholdControl value */
7 t _$ t7 B5 q - /* Set the FEF bit according to ETH ForwardErrorFrames value */
) w0 Q2 K7 f! N& E! I! c - /* Set the FUF bit according to ETH ForwardUndersizedGoodFrames value */+ Y, r& E1 W, k X' y
- /* Set the RTC bit according to ETH ReceiveThresholdControl value */. z3 k7 T- r7 ~) {+ X
- /* Set the OSF bit according to ETH SecondFrameOperate value */. ?' f ]1 g) Z' e
- tmpreg |= (uint32_t)(dmainit.DropTCPIPChecksumErrorFrame |
9 G3 s2 P( |1 R1 l7 j$ j, d - dmainit.ReceiveStoreForward |* h, o; o" X- I8 T% h6 H, ]/ f( u. t
- dmainit.FlushReceivedFrame |: H! i! f& P' Z
- dmainit.TransmitStoreForward |) }+ y& A4 J: l
- dmainit.TransmitThresholdControl |
p# `1 X9 e3 N s5 c - dmainit.ForwardErrorFrames |2 g! h0 c7 E2 t# O3 _ k. s
- dmainit.ForwardUndersizedGoodFrames |
8 [& @% b9 b4 {0 W% M: e B5 O) k - dmainit.ReceiveThresholdControl |; K; v) j w" G
- dmainit.SecondFrameOperate);/ d. f4 H I; k$ ^1 |5 a8 J
- - V* s% N0 F# d8 A6 K
- /* Write to ETHERNET DMAOMR */) u3 c! V; H1 D- H. ~& d
- (heth->Instance)->DMAOMR = (uint32_t)tmpreg;
+ l3 v. Y5 r& `6 b/ b - 8 U( T) D( j9 ~. \, J
- /* Wait until the write operation will be taken into account:; B* T9 p4 }) b1 L0 H
- at least four TX_CLK/RX_CLK clock cycles */
+ s( Z; [+ e- u6 b! w* w3 w - tmpreg = (heth->Instance)->DMAOMR;
, `( S& m8 J" _' c) @" Q8 c" K - HAL_Delay(ETH_REG_WRITE_DELAY);
* E+ x8 A) M) S5 z - (heth->Instance)->DMAOMR = tmpreg;
3 i) H0 f) w9 z
( u; k3 r0 r, p3 K5 l- /*----------------------- ETHERNET DMABMR Configuration ------------------*/6 G6 G; Y- r. M) x
- /* Set the AAL bit according to ETH AddressAlignedBeats value */# P Q ^1 n/ R! W
- /* Set the FB bit according to ETH FixedBurst value */9 R% A6 L# h2 H) \2 m0 M
- /* Set the RPBL and 4*PBL bits according to ETH RxDMABurstLength value */
" x% c3 n5 ]$ J7 F) g% Z3 A6 k5 L - /* Set the PBL and 4*PBL bits according to ETH TxDMABurstLength value */+ F b4 I& E+ S% I/ A, J/ T! i9 K
- /* Set the Enhanced DMA descriptors bit according to ETH EnhancedDescriptorFormat value*/
% V. V: X+ S* u2 f - /* Set the DSL bit according to ETH DesciptorSkipLength value */% K/ Y9 a* e$ b# u- G
- /* Set the PR and DA bits according to ETH DMAArbitration value */( J% J9 V9 Q" Q) V/ M
- (heth->Instance)->DMABMR = (uint32_t)(dmainit.AddressAlignedBeats |
3 o% u. Q; i1 m x' J+ K - dmainit.FixedBurst |
& S' O* l5 `% U - dmainit.RxDMABurstLength | /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */
" L; ?! E; h9 J - dmainit.TxDMABurstLength |5 }# L: `) q7 H2 D
- dmainit.EnhancedDescriptorFormat |* K; P @7 y6 E& k; c+ o% f
- (dmainit.DescriptorSkipLength Instance)->DMABMR;2 @# w. D$ j1 j3 N# ~' t; A
- HAL_Delay(ETH_REG_WRITE_DELAY);
# i6 B) s5 X1 a - (heth->Instance)->DMABMR = tmpreg;
' E. f( o# t0 Q) Q
' ]' G1 O! h4 L& `# H1 T- if((heth->Init).RxMode == ETH_RXINTERRUPT_MODE)
& ?/ t- H4 p& F, U - {- E4 r+ f, K1 o. [
- /* Enable the Ethernet Rx Interrupt */
/ U( L8 W9 F$ N7 U$ x, E: f) Z# G - __HAL_ETH_DMA_ENABLE_IT((heth), ETH_DMA_IT_NIS | ETH_DMA_IT_R);- h$ ^3 e( k; r2 a) x) G5 g
- }) h4 x5 g9 j/ H$ P
- ; L; {% E1 ?* p7 M: {. q) ~& T3 q
- /* Initialize MAC address in ethernet MAC */& D* [; x& a! K
- ETH_MACAddressConfig(heth, ETH_MAC_ADDRESS0, heth->Init.MACAddr);
. _( e# H# m6 G0 q9 K, K - }
复制代码 ) [" b6 z" G6 U/ e5 x( V, V
这里我就要稍微吐槽一下HAL库了
7 l: j) E5 J' _- g! L( k7 UMAC层过滤的设置没有预留接口出来,有设置MAC地址的函数
4 B- f- H: @( {8 W( A ]1 d
7 J" m2 x9 ^ l: D& K Ustatic void ETH_MACAddressConfig(ETH_HandleTypeDef *heth, uint32_t MacAddr, uint8_t *Addr)
: t9 Q6 [- L N6 W" w* B/ P' t" v' |8 [3 }
就不多做一个设置MAC层过滤的函数嘛,连后面用到的宏定义都有~~
3 p; c" P# ]. D' ]+ @6 v
. M1 j" q8 G2 R木有办法,自己编一个初始化MAC层过滤的函数吧* K2 O2 c" d7 ~2 q/ q9 x
7 ?0 Y m: z, |) G+ W% E其实也就是把上面这个ETH_MACDMAConfig函数分离成两个单独的设置函数,再编一个设置MAC层过滤的函数 f9 S* }% K5 H7 w2 p
" @4 o$ y. v( y7 S# E6 i9 u
开启MAC层过滤的初始化代码
. ]: D/ H) F# L% s! s; U- uint8_t ETH_MAC_Setting(ETH_HandleTypeDef *heth)
t2 D6 S3 K$ K- f: ?- [; p - {
: _2 G; |) T! F: y+ B - ETH_MACInitTypeDef macinit;
( |7 f7 t y( N5 X' B! ~: H( g( q - /* Ethernet MAC default initialization **************************************/
5 b u% ~; |: I - macinit.Watchdog = ETH_WATCHDOG_ENABLE;( a7 k- T: F# U } U) o
- macinit.Jabber = ETH_JABBER_ENABLE;
; D5 q9 q6 d: k! P! g g - macinit.InterFrameGap = ETH_INTERFRAMEGAP_96BIT;) S' C) ~) `- H F% |4 @+ h
- macinit.CarrierSense = ETH_CARRIERSENCE_ENABLE;
/ o5 Z, W0 u ]7 [5 D; |. _1 A$ @6 u1 O - macinit.ReceiveOwn = ETH_RECEIVEOWN_ENABLE;
6 J ~; d7 V* ?2 `1 l! v% M - macinit.LoopbackMode = ETH_LOOPBACKMODE_DISABLE;0 h6 ]7 e5 ?7 M) a7 i2 w8 G
- if (heth->Init.ChecksumMode == ETH_CHECKSUM_BY_HARDWARE)0 w& ^0 p4 X7 k) }- X
- {
7 i! J( Y' Q6 U: m3 H3 d" ^# ? - macinit.ChecksumOffload = ETH_CHECKSUMOFFLAOD_ENABLE;
8 f0 M, ?5 V- E- O, } - }
3 w7 `$ {. G' J0 a2 i7 n3 I - else
! }% m: R4 j2 I' s& \. _; M9 u - {
9 G' ` I$ x2 X T& _ - macinit.ChecksumOffload = ETH_CHECKSUMOFFLAOD_DISABLE;5 }4 |7 p: u; R6 E4 a: ]9 U
- }' Z- g/ V) L4 W: d% L
- macinit.RetryTransmission = ETH_RETRYTRANSMISSION_ENABLE; //使能重传1 Y* D9 t6 m: c5 ~2 @% p w( g5 g, P
- macinit.AutomaticPadCRCStrip = ETH_AUTOMATICPADCRCSTRIP_ENABLE; //使能自动去除8 \6 Y# e; ?3 Z' c0 ]
- macinit.BackOffLimit = ETH_BACKOFFLIMIT_10;
. O: B7 a1 o6 \ - macinit.DeferralCheck = ETH_DEFFERRALCHECK_DISABLE;
# J& X, M" V. L2 M) ~* x - macinit.ReceiveAll = ETH_RECEIVEAll_DISABLE; ///不接收所有包/ M- L H. U E. f Q
- macinit.SourceAddrFilter = ETH_SOURCEADDRFILTER_NORMAL_ENABLE; //源地址过滤0 o1 D; g1 x* s& u- u8 c
- macinit.PassControlFrames = ETH_PASSCONTROLFRAMES_BLOCKALL;1 `9 c: w' _' d) O% k
- macinit.BroadcastFramesReception = ETH_BROADCASTFRAMESRECEPTION_DISABLE; //广播不接收
* i) d/ Y; p9 S9 m% B: x - macinit.DestinationAddrFilter = ETH_DESTINATIONADDRFILTER_NORMAL; //目的地址过滤
. j) W9 T- ]# Z' t. k - macinit.PromiscuousMode = ETH_PROMISCUOUS_MODE_DISABLE; //混乱模式' w: }. z, Y7 ^! I" H
- macinit.MulticastFramesFilter = ETH_MULTICASTFRAMESFILTER_PERFECT; //多播过滤% P) M2 r1 _. P, m/ f+ G" x; a
- macinit.UnicastFramesFilter = ETH_UNICASTFRAMESFILTER_PERFECT; //单播过滤
: {1 B, E( w/ L6 I4 E5 h4 Z' N - macinit.HashTableHigh = 0x0; //Hash表高位; O X- _5 Y' q- Z( S6 D" N
- macinit.HashTableLow = 0x0; //Hash表低位, @7 a% M6 i: d) F0 S4 x/ e
- macinit.PauseTime = 0x0;1 O/ I7 L7 V( d% Q& t! l( i
- macinit.ZeroQuantaPause = ETH_ZEROQUANTAPAUSE_DISABLE;; r+ P8 z4 K% v3 z
- macinit.PauseLowThreshold = ETH_PAUSELOWTHRESHOLD_MINUS4;
( s) t& x9 P/ k7 a( o6 g& { ^ - macinit.UnicastPauseFrameDetect = ETH_UNICASTPAUSEFRAMEDETECT_ENABLE;% X. E* a+ u3 b6 Q2 x$ K% S
- macinit.ReceiveFlowControl = ETH_RECEIVEFLOWCONTROL_DISABLE; //接收流控
1 \7 _3 p9 y3 }; R - macinit.TransmitFlowControl = ETH_TRANSMITFLOWCONTROL_DISABLE; //发送流控
/ d: L4 C& S$ C0 X - macinit.VLANTagComparison = ETH_VLANTAGCOMPARISON_16BIT;# @, F: z- N& [8 h1 i
- macinit.VLANTagIdentifier = 0x0;
. Q8 t5 _* m$ w# y - . l; g9 M1 p9 r
- return HAL_ETH_ConfigMAC(heth, &macinit);& V8 k" C* f, |" L4 d! O/ a
- }
复制代码
9 |$ x* y5 m" t% y0 L. B默认的MAC层 DMA初始化函数. v# o- n' _. e: S z7 K+ t
- uint8_t ETH_DMA_Setting(ETH_HandleTypeDef *heth)
8 C7 j4 g7 Z: p& T4 J1 l - {' X. \8 d8 |% {$ c" }
- ETH_DMAInitTypeDef dmainit;* B- G4 x" i( Z( M/ T& N$ w
- /* Ethernet DMA default initialization ************************************/ J+ { ~1 n# f* m1 F" d! j; n9 s( y4 K
- dmainit.DropTCPIPChecksumErrorFrame = ETH_DROPTCPIPCHECKSUMERRORFRAME_ENABLE;
( O, @; L7 {" n1 G, \4 E - dmainit.ReceiveStoreForward = ETH_RECEIVESTOREFORWARD_ENABLE;
/ a( I) X- Y3 B2 F# z - dmainit.FlushReceivedFrame = ETH_FLUSHRECEIVEDFRAME_ENABLE;' c, V# g$ t3 s8 [! ?
- dmainit.TransmitStoreForward = ETH_TRANSMITSTOREFORWARD_ENABLE;
% n$ j9 W. ~- `/ V! ^7 Y2 s - dmainit.TransmitThresholdControl = ETH_TRANSMITTHRESHOLDCONTROL_64BYTES;/ l# p1 X: }' B; `9 ?) ^5 R
- dmainit.ForwardErrorFrames = ETH_FORWARDERRORFRAMES_DISABLE;6 g8 A* |3 W% A7 b$ z7 k2 |
- dmainit.ForwardUndersizedGoodFrames = ETH_FORWARDUNDERSIZEDGOODFRAMES_DISABLE;
$ {; p$ \) ] Z: z$ Q* O% X% { - dmainit.ReceiveThresholdControl = ETH_RECEIVEDTHRESHOLDCONTROL_64BYTES;! o) m% C9 N) ]5 ]
- dmainit.SecondFrameOperate = ETH_SECONDFRAMEOPERARTE_ENABLE;# t9 y# K6 n# p
- dmainit.AddressAlignedBeats = ETH_ADDRESSALIGNEDBEATS_ENABLE;
0 @* {! s! P( l* } - dmainit.FixedBurst = ETH_FIXEDBURST_ENABLE;
2 d+ |3 \' b2 j4 E7 ^* m6 w - dmainit.RxDMABurstLength = ETH_RXDMABURSTLENGTH_32BEAT;! w3 i0 ]. T" {$ a+ O4 ^8 T
- dmainit.TxDMABurstLength = ETH_TXDMABURSTLENGTH_32BEAT;
. I, A! w: a; r/ S/ Q* {* l - dmainit.EnhancedDescriptorFormat = ETH_DMAENHANCEDDESCRIPTOR_ENABLE;5 L4 F' H" P, ?9 F6 t7 z& M% E
- dmainit.DescriptorSkipLength = 0x0;3 c* o9 k0 y9 J) m
- dmainit.DMAArbitration = ETH_DMAARBITRATION_ROUNDROBIN_RXTX_1_1;
6 @, [3 l+ o/ d: Y1 v# h - uint8_t res = HAL_ETH_ConfigDMA(heth, &dmainit); Y) T- K3 U& v, f; l3 L4 P3 W
- if ((heth->Init).RxMode == ETH_RXINTERRUPT_MODE)
' F1 A- k/ v0 e8 V - {
. F8 H* {3 H8 h4 ~4 y3 b2 [+ g$ ? - /* Enable the Ethernet Rx Interrupt */; r& H# u5 D4 d. G
- __HAL_ETH_DMA_ENABLE_IT((heth), ETH_DMA_IT_NIS | ETH_DMA_IT_R);
- t' C: b6 q: L1 m0 z - }
! {6 e8 o3 r( j% C9 g! } - return res;
/ w# [0 X* [+ p# @% J - }
复制代码 ! h7 m+ W$ J+ P) y& Q
用来设置MAC地址的函数(其实就是把库函数的static函数取出来直接用了)- <blockquote>//heth: HAL句柄
复制代码
" h8 l% P. H. E- G, C然后到最最重要的MAC层过滤设置* F! A+ B4 J7 m6 w* L8 d
以MAC1为例6 n5 ]# f1 B! y( ~' i5 m( _3 J
/ z% E/ |( J T
7 }/ z* R6 b( q! H H1 ?: I
* ?6 S5 ]( i* H5 V c$ t# J2 L) i4 M5 G6 l0 l2 U
- ~0 R5 h0 \' }$ h' ~ o+ z3 F注意看红框的描述
( q+ T9 K4 S3 Y5 C( o4 v. A+ z功能很好用,可以过滤源地址/目的地址,还可以做掩码功能
4 ], E4 q0 @) z' Y: H设置MAC层过滤的代码1 j1 r A$ ~! T& |: O. `; ^4 J
- static void ETH_MAC_Filter_Setting(ETH_HandleTypeDef *heth,ST_ETH_PAR * para): I9 i% r" s7 l+ ~' k* }' E, m
- {4 X6 n. Q- h: N. S. |* m# M! r
- //设置本机MAC地址
0 m; C7 u0 x+ O3 W# J% c - ETH_MAC_ADDR_Config(heth, ETH_MAC_ADDRESS0, heth->Init.MACAddr);
# D F, g4 e' T& D9 n6 H - //设置UDP-A的对方MAC地址4 B+ M) K+ H6 S) k% I
- ETH_MAC_ADDR_Config(heth, ETH_MAC_ADDRESS1,para->UDP_A_MAC);( P# } X! d* x: F+ \
- (heth->Instance)->MACA1HR|=0xC0000000;//需要完全匹配UDP-A的对方MAC才接收' I0 J1 i- o. G- M
- //设置UDP-B的对方MAC地址
, n- B# {% @" d6 I. r k- w7 @ - ETH_MAC_ADDR_Config(heth, ETH_MAC_ADDRESS2,para->UDP_B_MAC);' h5 z# F! f0 k
- (heth->Instance)->MACA2HR|=0xC0000000;//需要完全匹配UDP-A的对方MAC才接收" r1 L& ^$ ]( T3 Y, L7 y
- }
复制代码 Y- \0 j6 ?% x6 j8 L$ O
我这里是希望为我的两个UDP精确过滤接收到的源地址
" I2 b1 m' ^) f- P1 `+ v* i所以AE位设置为1,SA设置为1,MBC全部为0(MAC地址全部检查)
% x" f$ u+ F7 c6 f# s; i6 F
* R7 K5 U+ L+ A+ |7 P, p0 |假如我希望过滤MAC中特定的厂家字段
0 T/ n5 e3 u+ ~& r3 M6 a) U" A例如某个MAC地址4 |9 E0 }# {! o6 b/ b! a7 e
01-02-03-04-05-06+ p Y, Y; U7 t$ C7 @
其中01-02-03就是厂家字段
7 E) D. g: C% Q( {0 s4 L
( S3 L% M. p' |2 |) y那么代码要这样修改(MBC的第29位,28位,27位置1)* o; L, w; s" ~& B: C1 q# @
这样MAC层就不会检查MAC地址里后3个字节(04-05-06)
: P' D$ U# l) A0 m+ z* O但是会检查前3个字节,也就是厂家字段% d1 R& j& y; \- L) ^$ e; _% Y2 W
这里需要注意MAC地址的字节序2 f/ ]. H' L" o+ i6 X
- static void ETH_MAC_Filter_Setting(ETH_HandleTypeDef *heth,ST_ETH_PAR * para). C6 B; G1 z$ C/ E! d" j% K
- {
+ ^; q$ ^, L4 [/ V _/ T - //设置本机MAC地址6 [& J! E. u' e
- ETH_MAC_ADDR_Config(heth, ETH_MAC_ADDRESS0, heth->Init.MACAddr);: C% Z7 `, m0 t9 }
- //设置UDP-A的对方MAC地址
t' ?, p' A, }0 N2 }. s - ETH_MAC_ADDR_Config(heth, ETH_MAC_ADDRESS1,para->UDP_A_MAC);
: t( R6 C8 P" n* s) C' E - (heth->Instance)->MACA1HR|=0xF8000000;//需要匹配厂家字段8 K3 }) S! L* {6 s+ V0 a9 `
- //设置UDP-B的对方MAC地址/ q9 e- ^6 @& a! @2 m7 W K
- ETH_MAC_ADDR_Config(heth, ETH_MAC_ADDRESS2,para->UDP_B_MAC);
# Z$ R% Q2 [* j4 m5 h+ c# V - (heth->Instance)->MACA2HR|=0xF8000000;//需要匹配厂家字段
T. f% U) W. w N2 x - }
复制代码
6 t2 V' {; M+ r; q& T8 T& S然后这些函数的调用顺序按:
" _: \ }! d3 i; S4 }) M [3 R; `1. HAL_ETH_Init
1 J# c1 ]# w; M" P2. ETH_MAC_Setting6 x% \4 H! t: O# K
3. ETH_DMA_Setting
+ m9 \' Y2 O" _, ~. R7 U0 Z4. ETH_MAC_Filter_Setting1 e& j; R% M$ V* ^2 j
( K+ ^- B0 H' U3 [6 Z: X
用你希望过滤的MAC传递到一个Hash计算函数里,得到一个值,通过这个值产生“映射”关系来过滤MAC地址,所以这个并不是精确的,因为可能会重复~~6 C# l+ g0 Y* a; J# Q
, K+ u! q8 a* m7 q6 o$ B
HASH函数
/ C# e) D* ] I函数
$ K; x* U& H. P% t' \- _0 q7 o$ S$ P- / STM32 MACHASH - Copyright (C) 2014-2018 Clive Turvey (<a href="mailto:sourcer32@gmail.com">sourcer32@gmail.com</a>)0 I5 s6 C# V6 N; g3 N0 p9 Q" A% ^% u0 a
- // All Rights Reserved
. G6 l6 x- j1 B E - 5 i1 P" R' U5 |% \; \4 {
- #include <windows.h>
: [, |/ F: s2 k: o - % m0 i" l. E9 M* i4 S( p3 M
- #include <stdio.h>% \( M3 L6 O1 l2 F9 J9 ^& _
- #include <stdlib.h>8 k& W0 G: o0 {" p2 `
- ; N4 x r; \* V W
- typedef unsigned char uint8_t;
4 |$ R3 s% ~: W - typedef unsigned long uint32_t;" T8 y! i C# c7 B7 F8 C: X
- ( n; j; X$ y$ u* e' ^' o+ f
- uint32_t Rev32(uint32_t x)
0 O/ Z1 R, d- s: G) `0 a' a - {6 W$ m- R) P; i
- uint32_t y;& g, p7 m, x: t3 ~* z
- int i;0 ^! Q4 @2 N5 z5 G
7 ~, I! Q+ V6 O+ c6 S" p- y = 0;
- j* ^8 I6 X# ]. v$ X2 h - $ i9 Q$ V3 y( p, Q$ H
- for(i=0; i<32; i++)
! k) _# g- I/ r. c - if (x & (1 << i))3 R- \5 L5 _4 B2 C8 I' S0 J% y7 I
- y |= 1 << (31 - i);
+ A" n2 d, A. @! K - # I' F- L5 V6 a$ g- }+ R
- return(y);
1 I2 d6 O8 E4 q' V - }
% g7 T( K3 }( f. }4 F- [9 ], ]
7 s& q' N D8 V! Z- uint32_t MacHash(const uint8_t *Mac) // <a href="mailto:sourcer32@gmail.com">sourcer32@gmail.com</a>
8 q& z4 s; L+ U3 ]: Q* R - {) _1 c! p$ O L: _
- int i, j;2 x$ h! y3 P2 E
- uint32_t Crc;* W0 y: l/ x; c, I8 ~. o# k
' ^( }% {* a5 f4 z& J8 W- Crc = 0xFFFFFFFF;
: A4 X: o9 y' W! s5 d - 8 W, w, t' l7 S2 ~
- for(j=0; j<6; j++) \- Y- k7 a. X+ z
- {
, D a6 M( Z8 { S/ J/ w - Crc = Crc ^ (uint32_t)Mac[j];
# A: q: P* k. }. O" y( J( P x
- g% p1 u/ [1 ^- for(i=0; i<8; i++)
: N) ]' z( a6 ]. _5 H- J - if (Crc & 1), \# f8 j4 r+ i5 B7 ^8 q
- Crc = (Crc >> 1) ^ 0xEDB88320; // Reversed 0x04C11DB7* c" e* c" w6 B9 Q7 c/ L" E1 b
- else
]- e/ C( Z9 C$ c6 v% A - Crc = (Crc >> 1);
* \4 g, F# v- b, s% F' G - }2 b3 I H0 I3 E( i
- ! m4 i5 f/ W/ Q, X
- return(Rev32(~Crc) >> 26); // Get High order 6-bit in reversed/inverted CRC {) p9 p9 U7 d$ s/ y7 k2 D$ k2 @: p1 Z
- }
/ q5 M0 I$ R0 @; g- ^6 C$ S
- w# W) b. P- H) ]* t8 p- uint32_t MacHashFast(const uint8_t *Mac) // <a href="mailto:sourcer32@gmail.com">sourcer32@gmail.com</a>
3 v1 ?1 v( @7 n1 v8 R( B- P9 E - {, g$ R0 ~& s) F& Z- n/ ?
- static const uint32_t Rev6Tbl[] = {
: |9 K% o B& W. j' f; d: Q; C - 0x00,0x20,0x10,0x30,0x08,0x28,0x18,0x38,% r8 c# U4 H o0 y; h; p4 P: y
- 0x04,0x24,0x14,0x34,0x0C,0x2C,0x1C,0x3C,$ T" m5 q- Z. s) c6 p
- 0x02,0x22,0x12,0x32,0x0A,0x2A,0x1A,0x3A,
0 t h0 h; d/ U/ _! \6 ~$ { - 0x06,0x26,0x16,0x36,0x0E,0x2E,0x1E,0x3E,
: @1 c8 R. _3 C, c" b Z7 d - 0x01,0x21,0x11,0x31,0x09,0x29,0x19,0x39, F3 O1 u& O0 l* I' v0 `% Z% z
- 0x05,0x25,0x15,0x35,0x0D,0x2D,0x1D,0x3D,
/ ?1 |4 \8 u# E4 ^* X( W# j - 0x03,0x23,0x13,0x33,0x0B,0x2B,0x1B,0x3B,
: ]# B- Z( ~* P) Y0 C8 s8 C- g - 0x07,0x27,0x17,0x37,0x0F,0x2F,0x1F,0x3F };
+ R( t4 |+ j$ W: C. V7 q6 v# U1 H - 5 |. }" t" v) |
- static const uint32_t Crc32Tbl[] = {
: L+ l' ?8 D; W& B" ~/ ^ - 0x4DBDF21C, 0x500AE278, 0x76D3D2D4, 0x6B64C2B0,3 P! I( c- `5 C" r I! y1 i! ]* l' W
- 0x3B61B38C, 0x26D6A3E8, 0x000F9344, 0x1DB88320,
- o) i; e' j, y2 h: L - 0xA005713C, 0xBDB26158, 0x9B6B51F4, 0x86DC4190,
/ B Q, M) r" x1 {( `7 w( Y - 0xD6D930AC, 0xCB6E20C8, 0xEDB71064, 0xF0000000 };
4 A+ v# q# [+ F) N/ P
8 k( D/ S+ K! b* [4 b& l- int i;
% w7 L n' e' }4 } - uint32_t Crc;
- d, X- y! U8 N) c - 9 Y0 [" B& m, z9 _6 s/ O3 i0 }' v
- Crc = 0;
* o6 ]( V3 d6 R+ z3 H" b
1 s. T% N' q! a0 l7 f: M- C- for(i=0; i<6; i++); D0 e* [3 l) ]& H; h+ h
- {
2 [+ D; P, S6 L3 c- F8 z8 L - Crc = Crc ^ (uint32_t)Mac<i>; S( p) p3 s( n& s6 z5 \' X( a
; z# J1 |7 ]' \5 u3 u, \- </i> Crc = (Crc >> 4) ^ Crc32Tbl[Crc & 0x0F]; /* lower nibble */
$ s8 }; C; o# C6 l - Crc = (Crc >> 4) ^ Crc32Tbl[Crc & 0x0F]; /* upper nibble */9 y; r" s% n1 h ]; |2 j
- }
* [1 d E8 F/ Y. P* g
1 J0 |8 S) P/ M* u" d- return(Rev6Tbl[Crc & 0x3F]);
$ Z" [4 k8 `5 ?, ? X' d5 | - }
0 P6 o3 R! j' i( c# x - , S8 e( I9 h9 T5 N
- int main(int argc, char **argv)
8 G% b! S G6 e! r - {5 M4 o5 B6 n# Y# B( |
- static const uint8_t Test1[] = { 0x1F, 0x52, 0x41, 0x9C, 0xB6, 0xAF }; // 0x2C4 i2 q" S& Y6 x7 Z( K( X
- static const uint8_t Test2[] = { 0xA0, 0x0A, 0x98, 0x00, 0x00, 0x45 }; // 0x07
) ]) ~) ?8 c0 M" z3 ?, {% @" I/ X6 I& j - static const uint8_t Test3[] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x21 }; // 0x24: i W6 J- K6 [$ a% i# |
* h0 P! x: r/ [( n& b4 U' \% S- printf("MacHash %02X\n", MacHash(Test1));* R% v! r0 N& |) G6 d
- printf("MacHash %02X\n", MacHash(Test2));
6 f* C) b" y: c4 Z - printf("MacHash %02X\n", MacHash(Test3));$ m m( ^8 c( l9 ~% ?5 v N
' x0 P ]$ n% W/ s- printf("MacHashFast %02X\n", MacHashFast(Test1));. }1 m9 q4 D# P6 v: y Z+ D+ H
- printf("MacHashFast %02X\n", MacHashFast(Test2));+ f' }0 z" K7 }! i
- printf("MacHashFast %02X\n", MacHashFast(Test3));
- x: d# \+ B' y( h
9 r: K2 Y8 g7 V- return(1);9 ~. }8 Z/ b ^# R
- }
复制代码
2 P0 c, g" |+ ?3 }) D8 q; {0 l4 h$ }9 ?; @/ F3 d$ m
回到手册; t/ L5 c* W) h! y" {
# O& Q5 K) u0 ^" s
0 u# d" Y/ P# D1 U6 q8 e+ N+ }$ `1 n8 D3 r8 J5 Y' Y
; H3 F' Z2 M, k
2 _; o$ \7 r* O5 `! {
g7 Z8 \- M8 x* J" Y上面的函数算出来一个数,假如是手册上的那个0x2c,二进制为101100B
( o: J. S2 i- D ~. A; }8 R. l换句看得懂的人话···就是:9 w5 h- e% |1 E8 V4 u+ C6 O$ l$ T
算出来的数先看第6位- n- n2 r5 I* q. s- L4 x5 l/ a* |- `: c
是1的话说明用到的是HASH表高位寄存器+ G1 y* V" J. ^- r9 _
否则用到的是HASH表低位寄存器
' u. W1 ]$ O }, D然后剩下的5位,转成十进制不是12嘛$ c# M0 i5 V- x& d; H
如果你希望过滤这个MAC
" a5 U1 w3 ?& R4 c; ]4 I& n; d那么你就把对应的HASH表高/低位寄存器的对应位置1,否则置02 V' Q% z' P# m0 T0 y- b
这里的0x2c就是要把
. Y5 E5 ~8 @, M6 _. B0 qHASH表高位寄存器的第12位置13 P4 N7 i4 \- v U7 M
就可以过滤这条MAC了
1 [4 ?8 P' H5 |! E然后把你希望的MAC地址都按照上面说的方法弄完4 I) _' h0 {$ [8 T
; g5 M3 x# p- p" x& F/ R回到这个函数稍微改一下
7 S8 ^0 i" H k3 F- uint8_t ETH_MAC_Setting(ETH_HandleTypeDef *heth)" J3 M" t- H; L, n
- {
# d6 z5 I) s, f- y( x - ETH_MACInitTypeDef macinit;( @5 |) u: k8 {% ^/ v4 _) G* |
- /* Ethernet MAC default initialization **************************************/& R: u# [* Y" e( [
- macinit.Watchdog = ETH_WATCHDOG_ENABLE;
; V. C& h U. x - macinit.Jabber = ETH_JABBER_ENABLE;1 R, m) @9 }# i
- macinit.InterFrameGap = ETH_INTERFRAMEGAP_96BIT;. D1 k( }) ]/ V3 Q! c4 t
- macinit.CarrierSense = ETH_CARRIERSENCE_ENABLE;
( \+ h/ o* v4 e ~' Q0 ] - macinit.ReceiveOwn = ETH_RECEIVEOWN_ENABLE;) O S! g! L7 d" p/ l! D' k
- macinit.LoopbackMode = ETH_LOOPBACKMODE_DISABLE;0 t- t7 K- }- l
- if (heth->Init.ChecksumMode == ETH_CHECKSUM_BY_HARDWARE) N/ ?) M; _4 q9 L+ i
- {
) d+ C! l, \( L$ e* K - macinit.ChecksumOffload = ETH_CHECKSUMOFFLAOD_ENABLE;
4 N4 d) y. W4 l" s, E$ g2 N5 s1 J9 `1 J - }
! A. D. V$ K1 h5 R9 {- x - else
( E+ O; e) e+ c2 i2 K. t - {. S8 _/ Z @& ~
- macinit.ChecksumOffload = ETH_CHECKSUMOFFLAOD_DISABLE;
) H+ k8 R2 Z! s - }
$ `/ K9 I+ E& T o - macinit.RetryTransmission = ETH_RETRYTRANSMISSION_ENABLE; //使能重传4 a p" G0 K% z7 T: A+ z( i
- macinit.AutomaticPadCRCStrip = ETH_AUTOMATICPADCRCSTRIP_ENABLE; //使能自动去除/ k6 s) O- l( I# T: N: }
- macinit.BackOffLimit = ETH_BACKOFFLIMIT_10;4 r: C7 ^. V2 h
- macinit.DeferralCheck = ETH_DEFFERRALCHECK_DISABLE;1 B9 e. H% O) e# J# M8 E1 \# Y: ^* H
- macinit.ReceiveAll = ETH_RECEIVEAll_DISABLE; ///不接收所有包$ ^! c% w6 U( ]; ^. {( F. m$ `
- macinit.SourceAddrFilter = ETH_SOURCEADDRFILTER_NORMAL_ENABLE; //源地址过滤3 Z9 }' k; R! a2 a6 B% _
- macinit.PassControlFrames = ETH_PASSCONTROLFRAMES_BLOCKALL;; d. ?, Z5 _. l \/ x3 m Z
- macinit.BroadcastFramesReception = ETH_BROADCASTFRAMESRECEPTION_DISABLE; //广播不接收" F0 r- Q# D& l/ \* g
- macinit.DestinationAddrFilter = ETH_DESTINATIONADDRFILTER_NORMAL; //目的地址过滤
9 l! s! g+ m3 w$ y* s - macinit.PromiscuousMode = ETH_PROMISCUOUS_MODE_DISABLE; //混乱模式
3 k4 o4 j2 K$ N- L - macinit.MulticastFramesFilter = ETH_MULTICASTFRAMESFILTER_PERFECTHASHTABLE; //!!多播HASH完美过滤
# E% r+ w& Q% c! p. V6 _6 {- q - macinit.UnicastFramesFilter = ETH_UNICASTFRAMESFILTER_PERFECTHASHTABLE; //!!单播HASH完美过滤2 ?$ l _& W8 Z0 b4 Y
- macinit.HashTableHigh = 0x0; //!!填你最后算完的Hash表高位/ l& L& H, C4 D, |
- macinit.HashTableLow = 0x0; //!!填你最后算完的Hash表低位% Y& Q2 X" Y/ p' d/ V
- macinit.PauseTime = 0x0;
6 }1 N) e# e3 _- D - macinit.ZeroQuantaPause = ETH_ZEROQUANTAPAUSE_DISABLE;
4 Y* S s$ S8 o. o1 _3 p5 U- }: a - macinit.PauseLowThreshold = ETH_PAUSELOWTHRESHOLD_MINUS4;
- K9 k( d- q. H% U/ Q - macinit.UnicastPauseFrameDetect = ETH_UNICASTPAUSEFRAMEDETECT_ENABLE;
+ \ y4 k9 i" R - macinit.ReceiveFlowControl = ETH_RECEIVEFLOWCONTROL_DISABLE; //接收流控1 s0 P0 v2 O/ p4 ^. v$ C
- macinit.TransmitFlowControl = ETH_TRANSMITFLOWCONTROL_DISABLE; //发送流控
6 r) d% d* C( r/ w+ K4 a! G+ J* S1 p7 \ - macinit.VLANTagComparison = ETH_VLANTAGCOMPARISON_16BIT;( l2 x$ \6 }5 Y d
- macinit.VLANTagIdentifier = 0x0;
" J: a; b0 T- Q! t3 R
- R$ ~, ^9 O9 U: e& @. c- return HAL_ETH_ConfigMAC(heth, &macinit);+ {+ y4 a5 A0 ^, ~
- }
复制代码
+ e+ S# F0 o$ a0 k9 ^大概MAC层过滤就这些东西了
2 n0 E# L- G9 O2 ]5 X6 B t5 M& V如果有条件弄MAC层过滤真的强烈建议开启,可以大幅减轻协议栈的处理负担,板卡不会因为处理大量无用的数据而白白浪费处理性能。
! H% F n; k/ Q. r9 M" m4 d: w$ f/ t- O' ~( D4 `- T6 T
0 o8 F. i. I. k5 T" ^5 F' I0 P. z* d) P; R4 @" W
1 t' i0 Z/ P" W9 V$ j( x |