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
#[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},
};
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;
let rcc = &*RCC::ptr();
let syscfg = &*SYSCFG::ptr();
bb::set(&rcc.apb2enr, SYSCFG_BIT);
if rcc.ahb1enr.read().ethmacen().bit_is_set() {
bb::clear(&rcc.ahb1enr, ETH_MAC_BIT);
}
bb::set(&syscfg.pmc, MII_RMII_BIT);
bb::set(&rcc.ahb1enr, ETH_MAC_BIT);
bb::set(&rcc.ahb1enr, ETH_TX_BIT);
bb::set(&rcc.ahb1enr, ETH_RX_BIT);
bb::set(&rcc.ahb1rstr, ETH_MAC_BIT);
bb::clear(&rcc.ahb1rstr, ETH_MAC_BIT);
}
#[cfg(feature = "stm32f7xx-hal")]
interrupt::free(|_| unsafe {
let rcc = &*RCC::ptr();
let syscfg = &*SYSCFG::ptr();
rcc.apb2enr.modify(|_, w| w.syscfgen().set_bit());
if rcc.ahb1enr.read().ethmacen().bit_is_set() {
rcc.ahb1enr.modify(|_, w| w.ethmacen().clear_bit());
}
syscfg.pmc.modify(|_, w| w.mii_rmii_sel().set_bit());
rcc.ahb1enr.modify(|_, w| {
w.ethmacen()
.set_bit()
.ethmactxen()
.set_bit()
.ethmacrxen()
.set_bit()
});
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();
rcc.apb2enr.modify(|_, w| w.afioen().set_bit());
if rcc.ahbenr.read().ethmacen().bit_is_set() {
rcc.ahbenr.modify(|_, w| w.ethmacen().clear_bit());
}
afio.mapr.modify(|_, w| w.mii_rmii_sel().set_bit());
rcc.ahbenr.modify(|_, w| {
w.ethmacen()
.set_bit()
.ethmactxen()
.set_bit()
.ethmacrxen()
.set_bit()
});
rcc.ahbrstr.modify(|_, w| w.ethmacrst().set_bit());
rcc.ahbrstr.modify(|_, w| w.ethmacrst().clear_bit());
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() {}
}
});
}
pub unsafe trait RmiiRefClk {}
pub unsafe trait RmiiCrsDv {}
pub unsafe trait RmiiTxEN {}
pub unsafe trait RmiiTxD0 {}
pub unsafe trait RmiiTxD1 {}
pub unsafe trait RmiiRxD0 {}
pub unsafe trait RmiiRxD1 {}
pub trait AlternateVeryHighSpeed {
fn into_af11_very_high_speed(self);
}
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,
{
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(all(
feature = "device-selected",
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,
};
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) {
cortex_m::interrupt::free(|_| {
let cr: &mut _ = &mut unsafe { core::mem::transmute(()) };
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)]
);
}