diff options
Diffstat (limited to 'embassy-stm32/src/eth/v1/mod.rs')
| -rw-r--r-- | embassy-stm32/src/eth/v1/mod.rs | 265 |
1 files changed, 120 insertions, 145 deletions
diff --git a/embassy-stm32/src/eth/v1/mod.rs b/embassy-stm32/src/eth/v1/mod.rs index 5be1c9739..8de26ce9d 100644 --- a/embassy-stm32/src/eth/v1/mod.rs +++ b/embassy-stm32/src/eth/v1/mod.rs | |||
| @@ -3,11 +3,10 @@ | |||
| 3 | mod rx_desc; | 3 | mod rx_desc; |
| 4 | mod tx_desc; | 4 | mod tx_desc; |
| 5 | 5 | ||
| 6 | use core::marker::PhantomData; | 6 | use core::sync::atomic::{Ordering, fence}; |
| 7 | use core::sync::atomic::{fence, Ordering}; | ||
| 8 | 7 | ||
| 9 | use embassy_hal_internal::Peri; | 8 | use embassy_hal_internal::Peri; |
| 10 | use stm32_metapac::eth::vals::{Apcs, Cr, Dm, DmaomrSr, Fes, Ftf, Ifg, MbProgress, Mw, Pbl, Rsf, St, Tsf}; | 9 | use stm32_metapac::eth::vals::{Apcs, Dm, DmaomrSr, Fes, Ftf, Ifg, Pbl, Rsf, St, Tsf}; |
| 11 | 10 | ||
| 12 | pub(crate) use self::rx_desc::{RDes, RDesRing}; | 11 | pub(crate) use self::rx_desc::{RDes, RDesRing}; |
| 13 | pub(crate) use self::tx_desc::{TDes, TDesRing}; | 12 | pub(crate) use self::tx_desc::{TDes, TDesRing}; |
| @@ -22,7 +21,6 @@ use crate::pac::AFIO; | |||
| 22 | #[cfg(any(eth_v1b, eth_v1c))] | 21 | #[cfg(any(eth_v1b, eth_v1c))] |
| 23 | use crate::pac::SYSCFG; | 22 | use crate::pac::SYSCFG; |
| 24 | use crate::pac::{ETH, RCC}; | 23 | use crate::pac::{ETH, RCC}; |
| 25 | use crate::rcc::SealedRccPeripheral; | ||
| 26 | 24 | ||
| 27 | /// Interrupt handler. | 25 | /// Interrupt handler. |
| 28 | pub struct InterruptHandler {} | 26 | pub struct InterruptHandler {} |
| @@ -53,14 +51,13 @@ pub struct Ethernet<'d, T: Instance, P: Phy> { | |||
| 53 | 51 | ||
| 54 | pins: Pins<'d>, | 52 | pins: Pins<'d>, |
| 55 | pub(crate) phy: P, | 53 | pub(crate) phy: P, |
| 56 | pub(crate) station_management: EthernetStationManagement<T>, | ||
| 57 | pub(crate) mac_addr: [u8; 6], | 54 | pub(crate) mac_addr: [u8; 6], |
| 58 | } | 55 | } |
| 59 | 56 | ||
| 60 | /// Pins of ethernet driver. | 57 | /// Pins of ethernet driver. |
| 61 | enum Pins<'d> { | 58 | enum Pins<'d> { |
| 62 | Rmii([Peri<'d, AnyPin>; 9]), | 59 | Rmii([Peri<'d, AnyPin>; 7]), |
| 63 | Mii([Peri<'d, AnyPin>; 14]), | 60 | Mii([Peri<'d, AnyPin>; 12]), |
| 64 | } | 61 | } |
| 65 | 62 | ||
| 66 | #[cfg(eth_v1a)] | 63 | #[cfg(eth_v1a)] |
| @@ -97,68 +94,105 @@ macro_rules! config_pins { | |||
| 97 | }; | 94 | }; |
| 98 | } | 95 | } |
| 99 | 96 | ||
| 100 | impl<'d, T: Instance, P: Phy> Ethernet<'d, T, P> { | 97 | impl<'d, T: Instance, SMA: sma::Instance> Ethernet<'d, T, GenericPhy<Sma<'d, SMA>>> { |
| 98 | /// Create a new RMII ethernet driver using 7 pins. | ||
| 99 | /// | ||
| 100 | /// This function uses a [`GenericPhy::new_auto`] as PHY, created using the | ||
| 101 | /// provided [`SMA`](sma::Instance), and MDIO and MDC pins. | ||
| 102 | /// | ||
| 103 | /// See [`Ethernet::new_with_phy`] for creating an RMII ethernet | ||
| 104 | /// river with a non-standard PHY. | ||
| 105 | /// | ||
| 101 | /// safety: the returned instance is not leak-safe | 106 | /// safety: the returned instance is not leak-safe |
| 102 | pub fn new<const TX: usize, const RX: usize, #[cfg(afio)] A>( | 107 | pub fn new<const TX: usize, const RX: usize, #[cfg(afio)] A>( |
| 103 | queue: &'d mut PacketQueue<TX, RX>, | 108 | queue: &'d mut PacketQueue<TX, RX>, |
| 104 | peri: Peri<'d, T>, | 109 | peri: Peri<'d, T>, |
| 105 | irq: impl interrupt::typelevel::Binding<interrupt::typelevel::ETH, InterruptHandler> + 'd, | 110 | irq: impl interrupt::typelevel::Binding<interrupt::typelevel::ETH, InterruptHandler> + 'd, |
| 106 | ref_clk: Peri<'d, if_afio!(impl RefClkPin<T, A>)>, | 111 | ref_clk: Peri<'d, if_afio!(impl RefClkPin<T, A>)>, |
| 107 | mdio: Peri<'d, if_afio!(impl MDIOPin<T, A>)>, | ||
| 108 | mdc: Peri<'d, if_afio!(impl MDCPin<T, A>)>, | ||
| 109 | crs: Peri<'d, if_afio!(impl CRSPin<T, A>)>, | 112 | crs: Peri<'d, if_afio!(impl CRSPin<T, A>)>, |
| 110 | rx_d0: Peri<'d, if_afio!(impl RXD0Pin<T, A>)>, | 113 | rx_d0: Peri<'d, if_afio!(impl RXD0Pin<T, A>)>, |
| 111 | rx_d1: Peri<'d, if_afio!(impl RXD1Pin<T, A>)>, | 114 | rx_d1: Peri<'d, if_afio!(impl RXD1Pin<T, A>)>, |
| 112 | tx_d0: Peri<'d, if_afio!(impl TXD0Pin<T, A>)>, | 115 | tx_d0: Peri<'d, if_afio!(impl TXD0Pin<T, A>)>, |
| 113 | tx_d1: Peri<'d, if_afio!(impl TXD1Pin<T, A>)>, | 116 | tx_d1: Peri<'d, if_afio!(impl TXD1Pin<T, A>)>, |
| 114 | tx_en: Peri<'d, if_afio!(impl TXEnPin<T, A>)>, | 117 | tx_en: Peri<'d, if_afio!(impl TXEnPin<T, A>)>, |
| 115 | phy: P, | ||
| 116 | mac_addr: [u8; 6], | 118 | mac_addr: [u8; 6], |
| 119 | sma: Peri<'d, SMA>, | ||
| 120 | mdio: Peri<'d, if_afio!(impl MDIOPin<SMA, A>)>, | ||
| 121 | mdc: Peri<'d, if_afio!(impl MDCPin<SMA, A>)>, | ||
| 117 | ) -> Self { | 122 | ) -> Self { |
| 118 | // Enable the necessary Clocks | 123 | let sma = Sma::new(sma, mdio, mdc); |
| 119 | #[cfg(eth_v1a)] | 124 | let phy = GenericPhy::new_auto(sma); |
| 120 | critical_section::with(|_| { | ||
| 121 | RCC.apb2enr().modify(|w| w.set_afioen(true)); | ||
| 122 | |||
| 123 | // Select RMII (Reduced Media Independent Interface) | ||
| 124 | // Must be done prior to enabling peripheral clock | ||
| 125 | AFIO.mapr().modify(|w| { | ||
| 126 | w.set_mii_rmii_sel(true); | ||
| 127 | w.set_swj_cfg(crate::pac::afio::vals::SwjCfg::NO_OP); | ||
| 128 | }); | ||
| 129 | 125 | ||
| 130 | RCC.ahbenr().modify(|w| { | 126 | Self::new_with_phy( |
| 131 | w.set_ethen(true); | 127 | queue, peri, irq, ref_clk, crs, rx_d0, rx_d1, tx_d0, tx_d1, tx_en, mac_addr, phy, |
| 132 | w.set_ethtxen(true); | 128 | ) |
| 133 | w.set_ethrxen(true); | 129 | } |
| 134 | }); | ||
| 135 | }); | ||
| 136 | 130 | ||
| 137 | #[cfg(any(eth_v1b, eth_v1c))] | 131 | /// Create a new MII ethernet driver using 14 pins. |
| 138 | critical_section::with(|_| { | 132 | /// |
| 139 | RCC.ahb1enr().modify(|w| { | 133 | /// This function uses a [`GenericPhy::new_auto`] as PHY, created using the |
| 140 | w.set_ethen(true); | 134 | /// provided [`SMA`](sma::Instance), and MDIO and MDC pins. |
| 141 | w.set_ethtxen(true); | 135 | /// |
| 142 | w.set_ethrxen(true); | 136 | /// See [`Ethernet::new_mii_with_phy`] for creating an RMII ethernet |
| 143 | }); | 137 | /// river with a non-standard PHY. |
| 138 | pub fn new_mii<const TX: usize, const RX: usize, #[cfg(afio)] A>( | ||
| 139 | queue: &'d mut PacketQueue<TX, RX>, | ||
| 140 | peri: Peri<'d, T>, | ||
| 141 | irq: impl interrupt::typelevel::Binding<interrupt::typelevel::ETH, InterruptHandler> + 'd, | ||
| 142 | rx_clk: Peri<'d, if_afio!(impl RXClkPin<T, A>)>, | ||
| 143 | tx_clk: Peri<'d, if_afio!(impl TXClkPin<T, A>)>, | ||
| 144 | rxdv: Peri<'d, if_afio!(impl RXDVPin<T, A>)>, | ||
| 145 | rx_d0: Peri<'d, if_afio!(impl RXD0Pin<T, A>)>, | ||
| 146 | rx_d1: Peri<'d, if_afio!(impl RXD1Pin<T, A>)>, | ||
| 147 | rx_d2: Peri<'d, if_afio!(impl RXD2Pin<T, A>)>, | ||
| 148 | rx_d3: Peri<'d, if_afio!(impl RXD3Pin<T, A>)>, | ||
| 149 | tx_d0: Peri<'d, if_afio!(impl TXD0Pin<T, A>)>, | ||
| 150 | tx_d1: Peri<'d, if_afio!(impl TXD1Pin<T, A>)>, | ||
| 151 | tx_d2: Peri<'d, if_afio!(impl TXD2Pin<T, A>)>, | ||
| 152 | tx_d3: Peri<'d, if_afio!(impl TXD3Pin<T, A>)>, | ||
| 153 | tx_en: Peri<'d, if_afio!(impl TXEnPin<T, A>)>, | ||
| 154 | mac_addr: [u8; 6], | ||
| 155 | sma: Peri<'d, SMA>, | ||
| 156 | mdio: Peri<'d, if_afio!(impl MDIOPin<SMA, A>)>, | ||
| 157 | mdc: Peri<'d, if_afio!(impl MDCPin<SMA, A>)>, | ||
| 158 | ) -> Self { | ||
| 159 | let sma = Sma::new(sma, mdio, mdc); | ||
| 160 | let phy = GenericPhy::new_auto(sma); | ||
| 144 | 161 | ||
| 145 | // RMII (Reduced Media Independent Interface) | 162 | Self::new_mii_with_phy( |
| 146 | SYSCFG.pmc().modify(|w| w.set_mii_rmii_sel(true)); | 163 | queue, peri, irq, rx_clk, tx_clk, rxdv, rx_d0, rx_d1, rx_d2, rx_d3, tx_d0, tx_d1, tx_d2, tx_d3, tx_en, |
| 147 | }); | 164 | mac_addr, phy, |
| 165 | ) | ||
| 166 | } | ||
| 167 | } | ||
| 148 | 168 | ||
| 169 | impl<'d, T: Instance, P: Phy> Ethernet<'d, T, P> { | ||
| 170 | /// safety: the returned instance is not leak-safe | ||
| 171 | pub fn new_with_phy<const TX: usize, const RX: usize, #[cfg(afio)] A>( | ||
| 172 | queue: &'d mut PacketQueue<TX, RX>, | ||
| 173 | peri: Peri<'d, T>, | ||
| 174 | irq: impl interrupt::typelevel::Binding<interrupt::typelevel::ETH, InterruptHandler> + 'd, | ||
| 175 | ref_clk: Peri<'d, if_afio!(impl RefClkPin<T, A>)>, | ||
| 176 | crs: Peri<'d, if_afio!(impl CRSPin<T, A>)>, | ||
| 177 | rx_d0: Peri<'d, if_afio!(impl RXD0Pin<T, A>)>, | ||
| 178 | rx_d1: Peri<'d, if_afio!(impl RXD1Pin<T, A>)>, | ||
| 179 | tx_d0: Peri<'d, if_afio!(impl TXD0Pin<T, A>)>, | ||
| 180 | tx_d1: Peri<'d, if_afio!(impl TXD1Pin<T, A>)>, | ||
| 181 | tx_en: Peri<'d, if_afio!(impl TXEnPin<T, A>)>, | ||
| 182 | mac_addr: [u8; 6], | ||
| 183 | phy: P, | ||
| 184 | ) -> Self { | ||
| 149 | #[cfg(eth_v1a)] | 185 | #[cfg(eth_v1a)] |
| 150 | { | 186 | { |
| 151 | config_in_pins!(ref_clk, rx_d0, rx_d1); | 187 | config_in_pins!(ref_clk, rx_d0, rx_d1); |
| 152 | config_af_pins!(mdio, mdc, tx_d0, tx_d1, tx_en); | 188 | config_af_pins!(tx_d0, tx_d1, tx_en); |
| 153 | } | 189 | } |
| 154 | 190 | ||
| 155 | #[cfg(any(eth_v1b, eth_v1c))] | 191 | #[cfg(any(eth_v1b, eth_v1c))] |
| 156 | config_pins!(ref_clk, mdio, mdc, crs, rx_d0, rx_d1, tx_d0, tx_d1, tx_en); | 192 | config_pins!(ref_clk, crs, rx_d0, rx_d1, tx_d0, tx_d1, tx_en); |
| 157 | 193 | ||
| 158 | let pins = Pins::Rmii([ | 194 | let pins = Pins::Rmii([ |
| 159 | ref_clk.into(), | 195 | ref_clk.into(), |
| 160 | mdio.into(), | ||
| 161 | mdc.into(), | ||
| 162 | crs.into(), | 196 | crs.into(), |
| 163 | rx_d0.into(), | 197 | rx_d0.into(), |
| 164 | rx_d1.into(), | 198 | rx_d1.into(), |
| @@ -167,7 +201,7 @@ impl<'d, T: Instance, P: Phy> Ethernet<'d, T, P> { | |||
| 167 | tx_en.into(), | 201 | tx_en.into(), |
| 168 | ]); | 202 | ]); |
| 169 | 203 | ||
| 170 | Self::new_inner(queue, peri, irq, pins, phy, mac_addr) | 204 | Self::new_inner(queue, peri, irq, pins, phy, mac_addr, true) |
| 171 | } | 205 | } |
| 172 | 206 | ||
| 173 | fn new_inner<const TX: usize, const RX: usize>( | 207 | fn new_inner<const TX: usize, const RX: usize>( |
| @@ -177,7 +211,39 @@ impl<'d, T: Instance, P: Phy> Ethernet<'d, T, P> { | |||
| 177 | pins: Pins<'d>, | 211 | pins: Pins<'d>, |
| 178 | phy: P, | 212 | phy: P, |
| 179 | mac_addr: [u8; 6], | 213 | mac_addr: [u8; 6], |
| 214 | rmii_mii_sel: bool, | ||
| 180 | ) -> Self { | 215 | ) -> Self { |
| 216 | // Enable the necessary Clocks | ||
| 217 | #[cfg(eth_v1a)] | ||
| 218 | critical_section::with(|_| { | ||
| 219 | RCC.apb2enr().modify(|w| w.set_afioen(true)); | ||
| 220 | |||
| 221 | // Select (R)MII (Reduced Media Independent Interface) | ||
| 222 | // Must be done prior to enabling peripheral clock | ||
| 223 | AFIO.mapr().modify(|w| { | ||
| 224 | w.set_mii_rmii_sel(rmii_mii_sel); | ||
| 225 | w.set_swj_cfg(crate::pac::afio::vals::SwjCfg::NO_OP); | ||
| 226 | }); | ||
| 227 | |||
| 228 | RCC.ahbenr().modify(|w| { | ||
| 229 | w.set_ethen(true); | ||
| 230 | w.set_ethtxen(true); | ||
| 231 | w.set_ethrxen(true); | ||
| 232 | }); | ||
| 233 | }); | ||
| 234 | |||
| 235 | #[cfg(any(eth_v1b, eth_v1c))] | ||
| 236 | critical_section::with(|_| { | ||
| 237 | RCC.ahb1enr().modify(|w| { | ||
| 238 | w.set_ethen(true); | ||
| 239 | w.set_ethtxen(true); | ||
| 240 | w.set_ethrxen(true); | ||
| 241 | }); | ||
| 242 | |||
| 243 | // (R)MII ((Reduced) Media Independent Interface) | ||
| 244 | SYSCFG.pmc().modify(|w| w.set_mii_rmii_sel(rmii_mii_sel)); | ||
| 245 | }); | ||
| 246 | |||
| 181 | let dma = T::regs().ethernet_dma(); | 247 | let dma = T::regs().ethernet_dma(); |
| 182 | let mac = T::regs().ethernet_mac(); | 248 | let mac = T::regs().ethernet_mac(); |
| 183 | 249 | ||
| @@ -190,7 +256,7 @@ impl<'d, T: Instance, P: Phy> Ethernet<'d, T, P> { | |||
| 190 | w.set_apcs(Apcs::STRIP); // automatic padding and crc stripping | 256 | w.set_apcs(Apcs::STRIP); // automatic padding and crc stripping |
| 191 | w.set_fes(Fes::FES100); // fast ethernet speed | 257 | w.set_fes(Fes::FES100); // fast ethernet speed |
| 192 | w.set_dm(Dm::FULL_DUPLEX); // full duplex | 258 | w.set_dm(Dm::FULL_DUPLEX); // full duplex |
| 193 | // TODO: Carrier sense ? ECRSFD | 259 | // TODO: Carrier sense ? ECRSFD |
| 194 | }); | 260 | }); |
| 195 | 261 | ||
| 196 | // Set the mac to pass all multicast packets | 262 | // Set the mac to pass all multicast packets |
| @@ -226,30 +292,10 @@ impl<'d, T: Instance, P: Phy> Ethernet<'d, T, P> { | |||
| 226 | 292 | ||
| 227 | // TODO MTU size setting not found for v1 ethernet, check if correct | 293 | // TODO MTU size setting not found for v1 ethernet, check if correct |
| 228 | 294 | ||
| 229 | let hclk = <T as SealedRccPeripheral>::frequency(); | ||
| 230 | let hclk_mhz = hclk.0 / 1_000_000; | ||
| 231 | |||
| 232 | // Set the MDC clock frequency in the range 1MHz - 2.5MHz | ||
| 233 | let clock_range = match hclk_mhz { | ||
| 234 | 0..=24 => panic!("Invalid HCLK frequency - should be at least 25 MHz."), | ||
| 235 | 25..=34 => Cr::CR_20_35, // Divide by 16 | ||
| 236 | 35..=59 => Cr::CR_35_60, // Divide by 26 | ||
| 237 | 60..=99 => Cr::CR_60_100, // Divide by 42 | ||
| 238 | 100..=149 => Cr::CR_100_150, // Divide by 62 | ||
| 239 | 150..=216 => Cr::CR_150_168, // Divide by 102 | ||
| 240 | _ => { | ||
| 241 | panic!("HCLK results in MDC clock > 2.5MHz even for the highest CSR clock divider") | ||
| 242 | } | ||
| 243 | }; | ||
| 244 | |||
| 245 | let mut this = Self { | 295 | let mut this = Self { |
| 246 | _peri: peri, | 296 | _peri: peri, |
| 247 | pins, | 297 | pins, |
| 248 | phy: phy, | 298 | phy: phy, |
| 249 | station_management: EthernetStationManagement { | ||
| 250 | peri: PhantomData, | ||
| 251 | clock_range: clock_range, | ||
| 252 | }, | ||
| 253 | mac_addr, | 299 | mac_addr, |
| 254 | tx: TDesRing::new(&mut queue.tx_desc, &mut queue.tx_buf), | 300 | tx: TDesRing::new(&mut queue.tx_desc, &mut queue.tx_buf), |
| 255 | rx: RDesRing::new(&mut queue.rx_desc, &mut queue.rx_buf), | 301 | rx: RDesRing::new(&mut queue.rx_desc, &mut queue.rx_buf), |
| @@ -279,8 +325,8 @@ impl<'d, T: Instance, P: Phy> Ethernet<'d, T, P> { | |||
| 279 | w.set_tie(true); | 325 | w.set_tie(true); |
| 280 | }); | 326 | }); |
| 281 | 327 | ||
| 282 | this.phy.phy_reset(&mut this.station_management); | 328 | this.phy.phy_reset(); |
| 283 | this.phy.phy_init(&mut this.station_management); | 329 | this.phy.phy_init(); |
| 284 | 330 | ||
| 285 | interrupt::ETH.unpend(); | 331 | interrupt::ETH.unpend(); |
| 286 | unsafe { interrupt::ETH.enable() }; | 332 | unsafe { interrupt::ETH.enable() }; |
| @@ -288,15 +334,13 @@ impl<'d, T: Instance, P: Phy> Ethernet<'d, T, P> { | |||
| 288 | this | 334 | this |
| 289 | } | 335 | } |
| 290 | 336 | ||
| 291 | /// Create a new MII ethernet driver using 14 pins. | 337 | /// Create a new MII ethernet driver using 12 pins. |
| 292 | pub fn new_mii<const TX: usize, const RX: usize, #[cfg(afio)] A>( | 338 | pub fn new_mii_with_phy<const TX: usize, const RX: usize, #[cfg(afio)] A>( |
| 293 | queue: &'d mut PacketQueue<TX, RX>, | 339 | queue: &'d mut PacketQueue<TX, RX>, |
| 294 | peri: Peri<'d, T>, | 340 | peri: Peri<'d, T>, |
| 295 | irq: impl interrupt::typelevel::Binding<interrupt::typelevel::ETH, InterruptHandler> + 'd, | 341 | irq: impl interrupt::typelevel::Binding<interrupt::typelevel::ETH, InterruptHandler> + 'd, |
| 296 | rx_clk: Peri<'d, if_afio!(impl RXClkPin<T, A>)>, | 342 | rx_clk: Peri<'d, if_afio!(impl RXClkPin<T, A>)>, |
| 297 | tx_clk: Peri<'d, if_afio!(impl TXClkPin<T, A>)>, | 343 | tx_clk: Peri<'d, if_afio!(impl TXClkPin<T, A>)>, |
| 298 | mdio: Peri<'d, if_afio!(impl MDIOPin<T, A>)>, | ||
| 299 | mdc: Peri<'d, if_afio!(impl MDCPin<T, A>)>, | ||
| 300 | rxdv: Peri<'d, if_afio!(impl RXDVPin<T, A>)>, | 344 | rxdv: Peri<'d, if_afio!(impl RXDVPin<T, A>)>, |
| 301 | rx_d0: Peri<'d, if_afio!(impl RXD0Pin<T, A>)>, | 345 | rx_d0: Peri<'d, if_afio!(impl RXD0Pin<T, A>)>, |
| 302 | rx_d1: Peri<'d, if_afio!(impl RXD1Pin<T, A>)>, | 346 | rx_d1: Peri<'d, if_afio!(impl RXD1Pin<T, A>)>, |
| @@ -307,56 +351,23 @@ impl<'d, T: Instance, P: Phy> Ethernet<'d, T, P> { | |||
| 307 | tx_d2: Peri<'d, if_afio!(impl TXD2Pin<T, A>)>, | 351 | tx_d2: Peri<'d, if_afio!(impl TXD2Pin<T, A>)>, |
| 308 | tx_d3: Peri<'d, if_afio!(impl TXD3Pin<T, A>)>, | 352 | tx_d3: Peri<'d, if_afio!(impl TXD3Pin<T, A>)>, |
| 309 | tx_en: Peri<'d, if_afio!(impl TXEnPin<T, A>)>, | 353 | tx_en: Peri<'d, if_afio!(impl TXEnPin<T, A>)>, |
| 310 | phy: P, | ||
| 311 | mac_addr: [u8; 6], | 354 | mac_addr: [u8; 6], |
| 355 | phy: P, | ||
| 312 | ) -> Self { | 356 | ) -> Self { |
| 313 | // TODO: Handle optional signals like CRS, MII_COL, RX_ER? | ||
| 314 | |||
| 315 | // Enable the necessary Clocks | ||
| 316 | #[cfg(eth_v1a)] | ||
| 317 | critical_section::with(|_| { | ||
| 318 | RCC.apb2enr().modify(|w| w.set_afioen(true)); | ||
| 319 | |||
| 320 | // Select MII (Media Independent Interface) | ||
| 321 | // Must be done prior to enabling peripheral clock | ||
| 322 | AFIO.mapr().modify(|w| { | ||
| 323 | w.set_mii_rmii_sel(false); | ||
| 324 | w.set_swj_cfg(crate::pac::afio::vals::SwjCfg::NO_OP); | ||
| 325 | }); | ||
| 326 | |||
| 327 | RCC.ahbenr().modify(|w| { | ||
| 328 | w.set_ethen(true); | ||
| 329 | w.set_ethtxen(true); | ||
| 330 | w.set_ethrxen(true); | ||
| 331 | }); | ||
| 332 | }); | ||
| 333 | |||
| 334 | #[cfg(any(eth_v1b, eth_v1c))] | ||
| 335 | critical_section::with(|_| { | ||
| 336 | RCC.ahb1enr().modify(|w| { | ||
| 337 | w.set_ethen(true); | ||
| 338 | w.set_ethtxen(true); | ||
| 339 | w.set_ethrxen(true); | ||
| 340 | }); | ||
| 341 | |||
| 342 | // MII (Media Independent Interface) | ||
| 343 | SYSCFG.pmc().modify(|w| w.set_mii_rmii_sel(false)); | ||
| 344 | }); | ||
| 345 | |||
| 346 | #[cfg(eth_v1a)] | 357 | #[cfg(eth_v1a)] |
| 347 | { | 358 | { |
| 348 | config_in_pins!(rx_clk, tx_clk, rx_d0, rx_d1, rx_d2, rx_d3, rxdv); | 359 | config_in_pins!(rx_clk, tx_clk, rx_d0, rx_d1, rx_d2, rx_d3, rxdv); |
| 349 | config_af_pins!(mdio, mdc, tx_d0, tx_d1, tx_d2, tx_d3, tx_en); | 360 | config_af_pins!(tx_d0, tx_d1, tx_d2, tx_d3, tx_en); |
| 350 | } | 361 | } |
| 351 | 362 | ||
| 352 | #[cfg(any(eth_v1b, eth_v1c))] | 363 | #[cfg(any(eth_v1b, eth_v1c))] |
| 353 | config_pins!(rx_clk, tx_clk, mdio, mdc, rxdv, rx_d0, rx_d1, rx_d2, rx_d3, tx_d0, tx_d1, tx_d2, tx_d3, tx_en); | 364 | config_pins!( |
| 365 | rx_clk, tx_clk, rxdv, rx_d0, rx_d1, rx_d2, rx_d3, tx_d0, tx_d1, tx_d2, tx_d3, tx_en | ||
| 366 | ); | ||
| 354 | 367 | ||
| 355 | let pins = Pins::Mii([ | 368 | let pins = Pins::Mii([ |
| 356 | rx_clk.into(), | 369 | rx_clk.into(), |
| 357 | tx_clk.into(), | 370 | tx_clk.into(), |
| 358 | mdio.into(), | ||
| 359 | mdc.into(), | ||
| 360 | rxdv.into(), | 371 | rxdv.into(), |
| 361 | rx_d0.into(), | 372 | rx_d0.into(), |
| 362 | rx_d1.into(), | 373 | rx_d1.into(), |
| @@ -369,43 +380,7 @@ impl<'d, T: Instance, P: Phy> Ethernet<'d, T, P> { | |||
| 369 | tx_en.into(), | 380 | tx_en.into(), |
| 370 | ]); | 381 | ]); |
| 371 | 382 | ||
| 372 | Self::new_inner(queue, peri, irq, pins, phy, mac_addr) | 383 | Self::new_inner(queue, peri, irq, pins, phy, mac_addr, false) |
| 373 | } | ||
| 374 | } | ||
| 375 | |||
| 376 | /// Ethernet station management interface. | ||
| 377 | pub(crate) struct EthernetStationManagement<T: Instance> { | ||
| 378 | peri: PhantomData<T>, | ||
| 379 | clock_range: Cr, | ||
| 380 | } | ||
| 381 | |||
| 382 | impl<T: Instance> StationManagement for EthernetStationManagement<T> { | ||
| 383 | fn smi_read(&mut self, phy_addr: u8, reg: u8) -> u16 { | ||
| 384 | let mac = T::regs().ethernet_mac(); | ||
| 385 | |||
| 386 | mac.macmiiar().modify(|w| { | ||
| 387 | w.set_pa(phy_addr); | ||
| 388 | w.set_mr(reg); | ||
| 389 | w.set_mw(Mw::READ); // read operation | ||
| 390 | w.set_cr(self.clock_range); | ||
| 391 | w.set_mb(MbProgress::BUSY); // indicate that operation is in progress | ||
| 392 | }); | ||
| 393 | while mac.macmiiar().read().mb() == MbProgress::BUSY {} | ||
| 394 | mac.macmiidr().read().md() | ||
| 395 | } | ||
| 396 | |||
| 397 | fn smi_write(&mut self, phy_addr: u8, reg: u8, val: u16) { | ||
| 398 | let mac = T::regs().ethernet_mac(); | ||
| 399 | |||
| 400 | mac.macmiidr().write(|w| w.set_md(val)); | ||
| 401 | mac.macmiiar().modify(|w| { | ||
| 402 | w.set_pa(phy_addr); | ||
| 403 | w.set_mr(reg); | ||
| 404 | w.set_mw(Mw::WRITE); // write | ||
| 405 | w.set_cr(self.clock_range); | ||
| 406 | w.set_mb(MbProgress::BUSY); | ||
| 407 | }); | ||
| 408 | while mac.macmiiar().read().mb() == MbProgress::BUSY {} | ||
| 409 | } | 384 | } |
| 410 | } | 385 | } |
| 411 | 386 | ||
