#![allow(unused_attributes)]
#![allow(unexpected_cfgs)]
#![no_std]
//! Common features used in examples.
//!
//! Note that this module isn't an example by itself.
use defmt_rtt as _;
use panic_probe as _;
use stm32_eth::{
hal::{gpio::GpioExt, rcc::Clocks},
PartsIn,
};
pub use pins::{setup_pins, Gpio};
use fugit::RateExtU32;
use stm32_eth::hal::rcc::RccExt;
// This is here so that we can build the `common.rs` example individually for
// each supported MCU family. That way we can be reasonably sure that all examples
// compile, without having to compile all of them each time.
#[allow(unused)]
fn main() {}
/// Setup the clocks and return clocks and a GPIO struct that
/// can be used to set up all of the pins.
///
/// This configures HCLK to be at least 25 MHz, which is the minimum required
/// for ethernet operation to be valid.
pub fn setup_peripherals(p: stm32_eth::stm32::Peripherals) -> (Clocks, Gpio, PartsIn) {
let ethernet = PartsIn {
dma: p.ETHERNET_DMA,
mac: p.ETHERNET_MAC,
mmc: p.ETHERNET_MMC,
#[cfg(feature = "ptp")]
ptp: p.ETHERNET_PTP,
};
#[cfg(any(feature = "stm32f7xx-hal", feature = "stm32f4xx-hal"))]
{
let rcc = p.RCC.constrain();
let clocks = rcc.cfgr.sysclk(96.MHz()).hclk(96.MHz());
#[cfg(feature = "stm32f4xx-hal")]
let clocks = {
if cfg!(hse = "bypass") {
clocks.use_hse(8.MHz()).bypass_hse_oscillator()
} else if cfg!(hse = "oscillator") {
clocks.use_hse(8.MHz())
} else {
clocks
}
};
#[cfg(feature = "stm32f7xx-hal")]
let clocks = {
if cfg!(hse = "bypass") {
clocks.hse(stm32_eth::hal::rcc::HSEClock::new(
8.MHz(),
stm32_eth::hal::rcc::HSEClockMode::Bypass,
))
} else if cfg!(hse = "oscillator") {
clocks.hse(stm32_eth::hal::rcc::HSEClock::new(
8.MHz(),
stm32_eth::hal::rcc::HSEClockMode::Oscillator,
))
} else {
clocks
}
};
let clocks = clocks.freeze();
let gpio = Gpio {
gpioa: p.GPIOA.split(),
gpiob: p.GPIOB.split(),
gpioc: p.GPIOC.split(),
gpiog: p.GPIOG.split(),
};
(clocks, gpio, ethernet)
}
#[cfg(feature = "stm32f1xx-hal")]
{
use stm32_eth::hal::flash::FlashExt;
let rcc = p.RCC.constrain();
let mut flash = p.FLASH.constrain();
let clocks = rcc.cfgr.sysclk(72.MHz()).hclk(72.MHz());
let clocks = if cfg!(hse = "bypass") || cfg!(hse = "oscillator") {
clocks.use_hse(8.MHz())
} else {
clocks
};
let clocks = clocks.freeze(&mut flash.acr);
let gpio = Gpio {
gpioa: p.GPIOA.split(),
gpiob: p.GPIOB.split(),
gpioc: p.GPIOC.split(),
};
(clocks, gpio, ethernet)
}
}
#[allow(unused_imports)]
pub use pins::*;
#[cfg(any(feature = "stm32f4xx-hal", feature = "stm32f7xx-hal",))]
mod pins {
use stm32_eth::{hal::gpio::*, EthPins};
pub struct Gpio {
pub gpioa: gpioa::Parts,
pub gpiob: gpiob::Parts,
pub gpioc: gpioc::Parts,
pub gpiog: gpiog::Parts,
}
pub type RefClk = PA1;
pub type Crs = PA7;
pub type TxD1 = PB13;
pub type RxD0 = PC4;
pub type RxD1 = PC5;
#[cfg(not(pins = "nucleo"))]
pub type TxEn = PB11;
#[cfg(not(pins = "nucleo"))]
pub type TxD0 = PB12;
#[cfg(pins = "nucleo")]
pub type TxEn = PG11;
#[cfg(pins = "nucleo")]
pub type TxD0 = PG13;
pub type Mdio = PA2>;
pub type Mdc = PC1>;
#[cfg(not(pps = "alternate"))]
pub type Pps = PB5