55//!
66//! Note that this module isn't an example by itself.
77
8+ use core:: mem:: MaybeUninit ;
9+
810use defmt_rtt as _;
911use panic_probe as _;
1012
1113use stm32_eth:: {
12- hal:: { gpio:: GpioExt , rcc:: Clocks } ,
14+ dma:: { RxDescriptor , RxDescriptorRing , TxDescriptor , TxDescriptorRing , MTU } ,
15+ hal:: gpio:: GpioExt ,
1316 PartsIn ,
1417} ;
1518
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+
1625pub use pins:: { setup_pins, Gpio } ;
1726
1827use fugit:: RateExtU32 ;
@@ -24,6 +33,33 @@ use stm32_eth::hal::rcc::RccExt;
2433#[ allow( unused) ]
2534fn main ( ) { }
2635
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+
2763/// Setup the clocks and return clocks and a GPIO struct that
2864/// can be used to set up all of the pins.
2965///
@@ -33,8 +69,11 @@ pub fn setup_peripherals(p: stm32_eth::stm32::Peripherals) -> (Clocks, Gpio, Par
3369 let ethernet = PartsIn {
3470 dma : p. ETHERNET_DMA ,
3571 mac : p. ETHERNET_MAC ,
72+ #[ cfg( feature = "stm32h7xx-hal" ) ]
73+ mtl : p. ETHERNET_MTL ,
74+ #[ cfg( feature = "f-series" ) ]
3675 mmc : p. ETHERNET_MMC ,
37- #[ cfg( feature = "ptp" ) ]
76+ #[ cfg( all ( feature = "ptp" , feature = "f-series" ) ) ]
3877 ptp : p. ETHERNET_PTP ,
3978 } ;
4079
@@ -109,6 +148,40 @@ pub fn setup_peripherals(p: stm32_eth::stm32::Peripherals) -> (Clocks, Gpio, Par
109148
110149 ( clocks, gpio, ethernet)
111150 }
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+ }
112185}
113186
114187pub use pins:: * ;
@@ -290,6 +363,96 @@ mod pins {
290363 }
291364}
292365
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+
293456use ieee802_3_miim:: {
294457 phy:: {
295458 lan87xxa:: { LAN8720A , LAN8742A } ,
0 commit comments