1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//! Pin definitions and setup functionality.
//!
//! This module contains the unsafe traits that determine
//! which pins can have a specific function, and provides
//! functionality for setting up clocks and the MAC peripheral

#[cfg(feature = "stm32f4xx-hal")]
use stm32f4xx_hal::{
    bb,
    gpio::{
        gpioa::{PA1, PA7},
        gpiob::{PB11, PB12, PB13},
        gpioc::{PC4, PC5},
        gpiog::{PG11, PG13, PG14},
        Input,
        Speed::VeryHigh,
    },
    pac::{RCC, SYSCFG},
};

#[cfg(feature = "stm32f7xx-hal")]
use cortex_m::interrupt;

#[cfg(feature = "stm32f7xx-hal")]
use stm32f7xx_hal::{
    gpio::{
        gpioa::{PA1, PA7},
        gpiob::{PB11, PB12, PB13},
        gpioc::{PC4, PC5},
        gpiog::{PG11, PG13, PG14},
        Input,
        Speed::VeryHigh,
    },
    pac::{RCC, SYSCFG},
};

use crate::{
    dma::EthernetDMA,
    stm32::{ETHERNET_DMA, ETHERNET_MAC, ETHERNET_MMC},
};

#[cfg(feature = "ptp")]
use crate::{ptp::EthernetPTP, stm32::ETHERNET_PTP};

// Enable syscfg and ethernet clocks. Reset the Ethernet MAC.
pub(crate) fn setup() {
    #[cfg(feature = "stm32f4xx-hal")]
    unsafe {
        const SYSCFG_BIT: u8 = 14;
        const ETH_MAC_BIT: u8 = 25;
        const ETH_TX_BIT: u8 = 26;
        const ETH_RX_BIT: u8 = 27;
        const MII_RMII_BIT: u8 = 23;

        //NOTE(unsafe) This will only be used for atomic writes with no side-effects
        let rcc = &*RCC::ptr();
        let syscfg = &*SYSCFG::ptr();

        // Enable syscfg clock
        bb::set(&rcc.apb2enr, SYSCFG_BIT);

        if rcc.ahb1enr.read().ethmacen().bit_is_set() {
            // pmc must be changed with the ethernet controller disabled or under reset
            bb::clear(&rcc.ahb1enr, ETH_MAC_BIT);
        }
        // select MII or RMII mode
        // 0 = MII, 1 = RMII
        bb::set(&syscfg.pmc, MII_RMII_BIT);

        // enable ethernet clocks
        bb::set(&rcc.ahb1enr, ETH_MAC_BIT);
        bb::set(&rcc.ahb1enr, ETH_TX_BIT);
        bb::set(&rcc.ahb1enr, ETH_RX_BIT);

        // reset pulse
        bb::set(&rcc.ahb1rstr, ETH_MAC_BIT);
        bb::clear(&rcc.ahb1rstr, ETH_MAC_BIT);
    }
    #[cfg(feature = "stm32f7xx-hal")]
    //stm32f7xx-hal does not currently have bitbanding
    interrupt::free(|_| unsafe {
        //NOTE(unsafe) Interrupt free and we only modify mac bits
        let rcc = &*RCC::ptr();
        let syscfg = &*SYSCFG::ptr();
        // enable syscfg clock
        rcc.apb2enr.modify(|_, w| w.syscfgen().set_bit());

        if rcc.ahb1enr.read().ethmacen().bit_is_set() {
            // pmc must be changed with the ethernet controller disabled or under reset
            rcc.ahb1enr.modify(|_, w| w.ethmacen().clear_bit());
        }

        // select MII or RMII mode
        // 0 = MII, 1 = RMII
        syscfg.pmc.modify(|_, w| w.mii_rmii_sel().set_bit());

        // enable ethernet clocks
        rcc.ahb1enr.modify(|_, w| {
            w.ethmacen()
                .set_bit()
                .ethmactxen()
                .set_bit()
                .ethmacrxen()
                .set_bit()
        });

        //reset pulse
        rcc.ahb1rstr.modify(|_, w| w.ethmacrst().set_bit());
        rcc.ahb1rstr.modify(|_, w| w.ethmacrst().clear_bit());
    });

    #[cfg(feature = "stm32f1xx-hal")]
    cortex_m::interrupt::free(|_| unsafe {
        let afio = &*crate::stm32::AFIO::ptr();
        let rcc = &*crate::stm32::RCC::ptr();

        // enable AFIO clock
        rcc.apb2enr.modify(|_, w| w.afioen().set_bit());

        if rcc.ahbenr.read().ethmacen().bit_is_set() {
            // ethernet controller must be disabled when configuring mapr
            rcc.ahbenr.modify(|_, w| w.ethmacen().clear_bit());
        }

        // select MII or RMII mode
        // 0 = MII, 1 = RMII
        afio.mapr.modify(|_, w| w.mii_rmii_sel().set_bit());

        // enable ethernet clocks
        rcc.ahbenr.modify(|_, w| {
            w.ethmacen()
                .set_bit()
                .ethmactxen()
                .set_bit()
                .ethmacrxen()
                .set_bit()
                .ethmacen()
                .set_bit()
        });

        // Reset pulse.
        rcc.ahbrstr.modify(|_, w| w.ethmacrst().set_bit());
        rcc.ahbrstr.modify(|_, w| w.ethmacrst().clear_bit());

        // Workaround for the issue mentioned in the Errata (2.20.11) related to wfi:
        //
        // "
        // If a WFI/WFE instruction is executed to put the system in sleep mode while the Ethernet
        // MAC master clock on the AHB bus matrix is ON and all remaining masters clocks are OFF,
        // the Ethernet DMA is unable to perform any AHB master accesses during sleep mode.
        //
        // Workaround: Enable DMA1 or DMA2 clocks in the RCC_AHBENR register before executing the
        // WFI/WFE instruction.
        // "
        if rcc.ahbenr.read().dma1en().is_disabled() && rcc.ahbenr.read().dma2en().is_disabled() {
            rcc.ahbenr.modify(|_, w| w.dma2en().enabled());
            while rcc.ahbenr.read().dma2en().is_disabled() {}
        }
    });
}

macro_rules ! pin_trait {
    ($([$name:ident, $doc:literal, $rm_name:literal]),*) => {
        $(
        #[doc = concat!($doc, "\n# Safety\nOnly pins specified as `ETH_RMII_", $rm_name, "` in a part's Reference Manual\nmay implement this trait.")]
        pub unsafe trait $name {}
        )*
    }
}

pin_trait!(
    [RmiiRefClk, "RMII Reference Clock", "REF_CLK"],
    [RmiiCrsDv, "RMII Rx Data Valid", "CRS_DV"],
    [RmiiTxEN, "RMII TX Enable", "TX_EN"],
    [RmiiTxD0, "RMII TX Data Pin 0", "TXD0"],
    [RmiiTxD1, "RMII TX Data Pin 1", "TXD1"],
    [RmiiRxD0, "RMII RX Data Pin 0", "RXD0"],
    [RmiiRxD1, "RMII RX Data Pin 1", "RXD1"]
);

/// Trait needed to setup the pins for the Ethernet peripheral.
pub trait AlternateVeryHighSpeed {
    /// Puts the pin in the Alternate Function 11 with Very High Speed.
    fn into_af11_very_high_speed(self);
}

/// A struct that contains all peripheral parts required to configure
/// the ethernet peripheral.
#[allow(missing_docs)]
pub struct PartsIn {
    pub mac: ETHERNET_MAC,
    pub mmc: ETHERNET_MMC,
    pub dma: ETHERNET_DMA,
    #[cfg(feature = "ptp")]
    pub ptp: ETHERNET_PTP,
}

#[cfg(feature = "ptp")]
impl From<(ETHERNET_MAC, ETHERNET_MMC, ETHERNET_DMA, ETHERNET_PTP)> for PartsIn {
    fn from(value: (ETHERNET_MAC, ETHERNET_MMC, ETHERNET_DMA, ETHERNET_PTP)) -> Self {
        Self {
            mac: value.0,
            mmc: value.1,
            dma: value.2,
            ptp: value.3,
        }
    }
}

#[cfg(not(feature = "ptp"))]
impl From<(ETHERNET_MAC, ETHERNET_MMC, ETHERNET_DMA)> for PartsIn {
    fn from(value: (ETHERNET_MAC, ETHERNET_MMC, ETHERNET_DMA)) -> Self {
        Self {
            mac: value.0,
            mmc: value.1,
            dma: value.2,
        }
    }
}

/// Access to all configured parts of the ethernet peripheral.
pub struct Parts<'rx, 'tx, T> {
    /// Access to and control over the ethernet MAC.
    pub mac: T,
    /// Access to and control over the ethernet DMA.
    pub dma: EthernetDMA<'rx, 'tx>,
    /// Access to and control over the ethernet PTP module.
    #[cfg(feature = "ptp")]
    pub ptp: EthernetPTP,
}

#[cfg(feature = "ptp")]
impl<'rx, 'tx, T> Parts<'rx, 'tx, T> {
    /// Split this [`Parts`] into its components.
    pub fn split(self) -> (T, EthernetDMA<'rx, 'tx>, EthernetPTP) {
        (self.mac, self.dma, self.ptp)
    }
}

#[cfg(not(feature = "ptp"))]
impl<'rx, 'tx, T> Parts<'rx, 'tx, T> {
    /// Split this [`Parts`] into its components.
    pub fn split(self) -> (T, EthernetDMA<'rx, 'tx>) {
        (self.mac, self.dma)
    }
}

/// A struct that represents a combination of pins to be used
/// as RMII pins for the ethernet peripheral(s)
// NOTE(missing_docs): all fields of this struct are self-explanatory
#[allow(missing_docs)]
pub struct EthPins<REFCLK, CRS, TXEN, TXD0, TXD1, RXD0, RXD1> {
    pub ref_clk: REFCLK,
    pub crs: CRS,
    pub tx_en: TXEN,
    pub tx_d0: TXD0,
    pub tx_d1: TXD1,
    pub rx_d0: RXD0,
    pub rx_d1: RXD1,
}

impl<REFCLK, CRS, TXEN, TXD0, TXD1, RXD0, RXD1> EthPins<REFCLK, CRS, TXEN, TXD0, TXD1, RXD0, RXD1>
where
    REFCLK: RmiiRefClk + AlternateVeryHighSpeed,
    CRS: RmiiCrsDv + AlternateVeryHighSpeed,
    TXEN: RmiiTxEN + AlternateVeryHighSpeed,
    TXD0: RmiiTxD0 + AlternateVeryHighSpeed,
    TXD1: RmiiTxD1 + AlternateVeryHighSpeed,
    RXD0: RmiiRxD0 + AlternateVeryHighSpeed,
    RXD1: RmiiRxD1 + AlternateVeryHighSpeed,
{
    /// Pin setup.
    ///
    /// Set RMII pins to
    /// * Alternate function 11
    /// * High-speed
    ///
    /// This function consumes the pins so that you cannot use them
    /// anywhere else by accident.
    pub fn setup_pins(self) {
        self.ref_clk.into_af11_very_high_speed();
        self.crs.into_af11_very_high_speed();
        self.tx_en.into_af11_very_high_speed();
        self.tx_d0.into_af11_very_high_speed();
        self.tx_d1.into_af11_very_high_speed();
        self.rx_d0.into_af11_very_high_speed();
        self.rx_d1.into_af11_very_high_speed();
    }
}

#[allow(unused_macros)]
macro_rules! impl_pins {
    ( $($traity:ident: [$($pin:ty,)+],)+ ) => {
        $(
            $(
                unsafe impl $traity for $pin {}

                impl AlternateVeryHighSpeed for $pin {
                    fn into_af11_very_high_speed(self) {
                        self.into_alternate::<11>().set_speed(VeryHigh);
                    }
                }
            )+
        )+
    };
}

#[cfg(any(feature = "stm32f4xx-hal", feature = "stm32f7xx-hal"))]
impl_pins!(
    RmiiRefClk: [
        PA1<Input>,
    ],
    RmiiCrsDv: [
        PA7<Input>,
    ],
    RmiiTxEN: [
        PB11<Input>,
        PG11<Input>,
    ],
    RmiiTxD0: [
        PB12<Input>,
        PG13<Input>,
    ],
    RmiiTxD1: [
        PB13<Input>,
        PG14<Input>,
    ],
    RmiiRxD0: [
        PC4<Input>,
    ],
    RmiiRxD1: [
        PC5<Input>,
    ],
);

#[cfg(feature = "stm32f1xx-hal")]
mod stm32f1 {
    use super::*;
    use stm32f1xx_hal::gpio::{
        gpioa::*, gpiob::*, gpioc::*, gpiod::*, Alternate, Floating, IOPinSpeed, Input,
        OutputSpeed, PushPull,
    };

    // STM32F1xx's require access to the CRL/CRH registers to change pin mode. As a result, we
    // require that pins are already in the necessary mode before constructing `EthPins` as it
    // would be inconvenient to pass CRL and CRH through to the `AlternateVeryHighSpeed` callsite.

    macro_rules! impl_pins {
        ($($type:ident: [$(($PIN:ty, $is_input:literal)),+]),*) => {
            $(
                $(
                    unsafe impl $type for $PIN {}
                    impl AlternateVeryHighSpeed for $PIN {
                        fn into_af11_very_high_speed(self) {
                            // Within this critical section, modifying the `CRL` register can
                            // only be unsound if this critical section preempts other code
                            // that is modifying the same register
                            cortex_m::interrupt::free(|_| {
                                // SAFETY: this is sound as long as the API of the HAL and structure of the CRL
                                // struct does not change. In case the size of the `CRL` struct is changed, compilation
                                // will fail as `mem::transmute` can only convert between types of the same size.
                                //
                                // This guards us from unsound behaviour introduced by point releases of the f1 hal
                                let cr: &mut _ = &mut unsafe { core::mem::transmute(()) };
                                // The speed can only be changed on output pins
                                let mut pin = self.into_alternate_push_pull(cr);
                                pin.set_speed(cr, IOPinSpeed::Mhz50);

                                if $is_input {
                                    pin.into_floating_input(cr);
                                }
                            });
                        }
                    }
                )+
            )*
        };
    }

    impl_pins!(
        RmiiRefClk: [(PA1<Input<Floating>>, true)],
        RmiiCrsDv: [(PA7<Input<Floating>>, true), (PD8<Input<Floating>>, true)],
        RmiiTxEN: [(PB11<Alternate<PushPull>>, false)],
        RmiiTxD0: [(PB12<Alternate<PushPull>>, false)],
        RmiiTxD1: [(PB13<Alternate<PushPull>>, false)],
        RmiiRxD0: [(PC4<Input<Floating>>, true), (PD9<Input<Floating>>, true)],
        RmiiRxD1: [(PC5<Input<Floating>>, true), (PD10<Input<Floating>>, true)]
    );
}