5
5
//!
6
6
//! Note that this module isn't an example by itself.
7
7
8
+ use core:: mem:: MaybeUninit ;
9
+
8
10
use defmt_rtt as _;
9
11
use panic_probe as _;
10
12
11
13
use stm32_eth:: {
12
- hal:: { gpio:: GpioExt , rcc:: Clocks } ,
14
+ dma:: { RxDescriptor , RxDescriptorRing , TxDescriptor , TxDescriptorRing , MTU } ,
15
+ hal:: gpio:: GpioExt ,
13
16
PartsIn ,
14
17
} ;
15
18
19
+ #[ cfg( feature = "f-series" ) ]
20
+ use stm32_eth:: hal:: rcc:: Clocks ;
21
+
22
+ #[ cfg( feature = "stm32h7xx-hal" ) ]
23
+ use stm32_eth:: hal:: rcc:: CoreClocks as Clocks ;
24
+
16
25
pub use pins:: { setup_pins, Gpio } ;
17
26
18
27
use fugit:: RateExtU32 ;
@@ -24,6 +33,33 @@ use stm32_eth::hal::rcc::RccExt;
24
33
#[ allow( unused) ]
25
34
fn main ( ) { }
26
35
36
+ const NUM_DESCRIPTORS : usize = 4 ;
37
+
38
+ // On H7s, the ethernet DMA does not have access to the normal ram
39
+ // so we must explicitly put them in SRAM.
40
+ #[ cfg_attr( feature = "stm32h7xx-hal" , link_section = ".sram1.eth" ) ]
41
+ static mut TX_DESCRIPTORS : MaybeUninit < [ TxDescriptor ; NUM_DESCRIPTORS ] > = MaybeUninit :: uninit ( ) ;
42
+ #[ cfg_attr( feature = "stm32h7xx-hal" , link_section = ".sram1.eth" ) ]
43
+ static mut TX_BUFFERS : MaybeUninit < [ [ u8 ; MTU + 2 ] ; NUM_DESCRIPTORS ] > = MaybeUninit :: uninit ( ) ;
44
+ #[ cfg_attr( feature = "stm32h7xx-hal" , link_section = ".sram1.eth" ) ]
45
+ static mut RX_DESCRIPTORS : MaybeUninit < [ RxDescriptor ; NUM_DESCRIPTORS ] > = MaybeUninit :: uninit ( ) ;
46
+ #[ cfg_attr( feature = "stm32h7xx-hal" , link_section = ".sram1.eth" ) ]
47
+ static mut RX_BUFFERS : MaybeUninit < [ [ u8 ; MTU + 2 ] ; NUM_DESCRIPTORS ] > = MaybeUninit :: uninit ( ) ;
48
+
49
+ /// Set up the buffers to be used
50
+ pub fn setup_rings ( ) -> ( TxDescriptorRing < ' static > , RxDescriptorRing < ' static > ) {
51
+ let tx_desc = unsafe { TX_DESCRIPTORS . write ( [ TxDescriptor :: new ( ) ; NUM_DESCRIPTORS ] ) } ;
52
+ let tx_buf = unsafe { TX_BUFFERS . write ( [ [ 0u8 ; MTU + 2 ] ; NUM_DESCRIPTORS ] ) } ;
53
+
54
+ let rx_desc = unsafe { RX_DESCRIPTORS . write ( [ RxDescriptor :: new ( ) ; NUM_DESCRIPTORS ] ) } ;
55
+ let rx_buf = unsafe { RX_BUFFERS . write ( [ [ 0u8 ; MTU + 2 ] ; NUM_DESCRIPTORS ] ) } ;
56
+
57
+ (
58
+ TxDescriptorRing :: new ( tx_desc, tx_buf) ,
59
+ RxDescriptorRing :: new ( rx_desc, rx_buf) ,
60
+ )
61
+ }
62
+
27
63
/// Setup the clocks and return clocks and a GPIO struct that
28
64
/// can be used to set up all of the pins.
29
65
///
@@ -33,8 +69,11 @@ pub fn setup_peripherals(p: stm32_eth::stm32::Peripherals) -> (Clocks, Gpio, Par
33
69
let ethernet = PartsIn {
34
70
dma : p. ETHERNET_DMA ,
35
71
mac : p. ETHERNET_MAC ,
72
+ #[ cfg( feature = "stm32h7xx-hal" ) ]
73
+ mtl : p. ETHERNET_MTL ,
74
+ #[ cfg( feature = "f-series" ) ]
36
75
mmc : p. ETHERNET_MMC ,
37
- #[ cfg( feature = "ptp" ) ]
76
+ #[ cfg( all ( feature = "ptp" , feature = "f-series" ) ) ]
38
77
ptp : p. ETHERNET_PTP ,
39
78
} ;
40
79
@@ -109,6 +148,40 @@ pub fn setup_peripherals(p: stm32_eth::stm32::Peripherals) -> (Clocks, Gpio, Par
109
148
110
149
( clocks, gpio, ethernet)
111
150
}
151
+
152
+ #[ cfg( feature = "stm32h7xx-hal" ) ]
153
+ {
154
+ use stm32_eth:: hal:: pwr:: PwrExt ;
155
+
156
+ let rcc = p. RCC . constrain ( ) ;
157
+ let pwr = p. PWR . constrain ( ) ;
158
+
159
+ let syscfg = p. SYSCFG ;
160
+
161
+ let pwrcfg = pwr. vos1 ( ) . freeze ( ) ;
162
+
163
+ let rcc = rcc. hclk ( 200 . MHz ( ) ) . sys_ck ( 200 . MHz ( ) ) ;
164
+
165
+ let rcc = if cfg ! ( hse = "bypass" ) {
166
+ rcc. bypass_hse ( ) . use_hse ( 8 . MHz ( ) )
167
+ } else if cfg ! ( hse = "oscillator" ) {
168
+ rcc. use_hse ( 8 . MHz ( ) )
169
+ } else {
170
+ rcc
171
+ } ;
172
+
173
+ let ccdr = rcc. freeze ( pwrcfg, & syscfg) ;
174
+ let clocks = ccdr. clocks ;
175
+
176
+ let gpio = Gpio {
177
+ gpioa : p. GPIOA . split ( ccdr. peripheral . GPIOA ) ,
178
+ gpiob : p. GPIOB . split ( ccdr. peripheral . GPIOB ) ,
179
+ gpioc : p. GPIOC . split ( ccdr. peripheral . GPIOC ) ,
180
+ gpiog : p. GPIOG . split ( ccdr. peripheral . GPIOG ) ,
181
+ } ;
182
+
183
+ ( clocks, gpio, ethernet)
184
+ }
112
185
}
113
186
114
187
pub use pins:: * ;
@@ -290,6 +363,96 @@ mod pins {
290
363
}
291
364
}
292
365
366
+ #[ cfg( feature = "stm32h7xx-hal" ) ]
367
+ mod pins {
368
+ use stm32_eth:: {
369
+ hal:: gpio:: { Input , PushPull , * } ,
370
+ EthPins ,
371
+ } ;
372
+
373
+ pub struct Gpio {
374
+ pub gpioa : gpioa:: Parts ,
375
+ pub gpiob : gpiob:: Parts ,
376
+ pub gpioc : gpioc:: Parts ,
377
+ pub gpiog : gpiog:: Parts ,
378
+ }
379
+
380
+ pub type RefClk = PA1 < Input > ;
381
+ pub type Crs = PA7 < Input > ;
382
+
383
+ #[ cfg( pins = "nucleo" ) ]
384
+ pub type TxEn = PG11 < Input > ;
385
+ #[ cfg( pins = "nucleo" ) ]
386
+ pub type TxD0 = PG13 < Input > ;
387
+
388
+ #[ cfg( not( pins = "nucleo" ) ) ]
389
+ pub type TxEn = PB11 < Input > ;
390
+ #[ cfg( not( pins = "nucleo" ) ) ]
391
+ pub type TxD0 = PB12 < Input > ;
392
+
393
+ pub type TxD1 = PB13 < Input > ;
394
+ pub type RxD0 = PC4 < Input > ;
395
+ pub type RxD1 = PC5 < Input > ;
396
+
397
+ #[ cfg( not( pps = "alternate" ) ) ]
398
+ pub type Pps = PB5 < Output < PushPull > > ;
399
+ #[ cfg( pps = "alternate" ) ]
400
+ pub type Pps = PG5 < Output < PushPull > > ;
401
+
402
+ pub type Mdio = PA2 < Alternate < 11 > > ;
403
+ pub type Mdc = PC1 < Alternate < 11 > > ;
404
+
405
+ pub fn setup_pins (
406
+ gpio : Gpio ,
407
+ ) -> (
408
+ EthPins < RefClk , Crs , TxEn , TxD0 , TxD1 , RxD0 , RxD1 > ,
409
+ Mdio ,
410
+ Mdc ,
411
+ Pps ,
412
+ ) {
413
+ #[ allow( unused_variables) ]
414
+ let Gpio {
415
+ gpioa,
416
+ gpiob,
417
+ gpioc,
418
+ gpiog,
419
+ } = gpio;
420
+
421
+ let ref_clk = gpioa. pa1 . into_input ( ) ;
422
+ let crs = gpioa. pa7 . into_input ( ) ;
423
+ let rx_d0 = gpioc. pc4 . into_input ( ) ;
424
+ let rx_d1 = gpioc. pc5 . into_input ( ) ;
425
+ let tx_d1 = gpiob. pb13 . into_input ( ) ;
426
+
427
+ #[ cfg( not( pins = "nucleo" ) ) ]
428
+ let ( tx_en, tx_d0) = { ( gpiob. pb11 . into_input ( ) , gpiob. pb12 . into_input ( ) ) } ;
429
+
430
+ #[ cfg( pins = "nucleo" ) ]
431
+ let ( tx_en, tx_d0) = { ( gpiog. pg11 . into_input ( ) , gpiog. pg13 . into_input ( ) ) } ;
432
+
433
+ let mdio = gpioa. pa2 . into_alternate ( ) ;
434
+ let mdc = gpioc. pc1 . into_alternate ( ) ;
435
+
436
+ #[ cfg( not( pps = "alternate" ) ) ]
437
+ let pps = gpiob. pb5 . into_push_pull_output ( ) ;
438
+
439
+ #[ cfg( pps = "alternate" ) ]
440
+ let pps = gpiog. pg5 . into_push_pull_output ( ) ;
441
+
442
+ let pins = EthPins {
443
+ ref_clk,
444
+ crs,
445
+ tx_en,
446
+ tx_d0,
447
+ tx_d1,
448
+ rx_d0,
449
+ rx_d1,
450
+ } ;
451
+
452
+ ( pins, mdio, mdc, pps)
453
+ }
454
+ }
455
+
293
456
use ieee802_3_miim:: {
294
457
phy:: {
295
458
lan87xxa:: { LAN8720A , LAN8742A } ,
0 commit comments