Skip to content

Commit b5a8015

Browse files
datdenkiknietJohannes Draaijer
authored andcommitted
De-specify all examples so they will run on any MCU, assuming that the default pins
are used and the PHY doesn't require a specific pin value to enable it.
1 parent 2315080 commit b5a8015

File tree

9 files changed

+386
-628
lines changed

9 files changed

+386
-628
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ jobs:
180180
- stm32f107
181181
- stm32f407
182182
- stm32f765
183-
- rtic-echo-example-altpin,stm32f765
183+
- nucleo-pins,stm32f765
184184
steps:
185185
- name: Checkout
186186
uses: actions/checkout@v3

Cargo.toml

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ stm32f779 = ["stm32f7xx-hal/stm32f779", "device-selected", "fence"]
6363
smoltcp-phy = ["smoltcp"]
6464

6565
# Example features
66+
nucleo-pins = [ ]
6667
rtic-echo-example = [ "ieee802_3_miim", "defmt", "smoltcp-phy", "smoltcp/defmt", "smoltcp/medium-ethernet", "smoltcp/socket-tcp" ]
67-
rtic-echo-example-altpin = [ ]
6868

6969
[dev-dependencies]
7070
cortex-m = "0.7"
@@ -79,29 +79,22 @@ systick-monotonic = "1.0"
7979

8080
[[example]]
8181
name = "pktgen"
82-
required-features = ["stm32f429"]
82+
required-features = [ ]
8383

8484
[[example]]
8585
name = "ip"
8686
required-features = [
87-
"stm32f429", "smoltcp-phy", "log", "smoltcp/socket-tcp", "smoltcp/socket-icmp",
88-
"smoltcp/log", "smoltcp/verbose"
89-
]
90-
91-
[[example]]
92-
name = "ip-f107"
93-
required-features = [
94-
"stm32f107", "smoltcp-phy", "log", "smoltcp/socket-tcp", "smoltcp/socket-icmp",
87+
"smoltcp-phy", "log", "smoltcp/socket-tcp", "smoltcp/socket-icmp",
9588
"smoltcp/log", "smoltcp/verbose"
9689
]
9790

9891
[[example]]
9992
name = "arp"
100-
required-features = ["stm32f407"]
93+
required-features = [ ]
10194

10295
[[example]]
10396
name = "arp-smoltcp"
104-
required-features = ["stm32f407", "smoltcp-phy", "smoltcp/socket-icmp"]
97+
required-features = ["smoltcp-phy", "smoltcp/socket-icmp"]
10598

10699
[[example]]
107100
name = "rtic-echo"

examples/arp-smoltcp.rs

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// cargo build --example arp-smoltcp --features=stm32f407,smoltcp-phy,smoltcp/socket-tcp,smoltcp/socket-icmp
2-
// This example uses the STM32F407 and the KSZ8051R as PHY. If necessary the pins,
3-
// the PHY register addresses and masks have to be adapted, as well as the IPs.
4-
// With Wireshark, you can see the ARP packets, which should look like this:
5-
// No. Time Source Destination Protocol Length Info
6-
// 1 0.000000000 Cetia_ad:be:ef Broadcast ARP 60 Who has 10.0.0.2? Tell 10.0.0.10
1+
//! cargo build --example arp-smoltcp --features=<MCU>,smoltcp-phy,smoltcp/socket-tcp,smoltcp/socket-icmp
2+
//! This example should work on any MCU with an 802.3 compatible, on-by-default PHY connected to the default
3+
//! ethernet pins of that MCU.
4+
//! With Wireshark, you can see the ARP packets, which should look like this:
5+
//! No. Time Source Destination Protocol Length Info
6+
//! 1 0.000000000 Cetia_ad:be:ef Broadcast ARP 60 Who has 10.0.0.2? Tell 10.0.0.10
77
88
#![no_std]
99
#![no_main]
@@ -17,18 +17,17 @@ use cortex_m::asm;
1717
use cortex_m::interrupt::Mutex;
1818
use cortex_m_rt::{entry, exception};
1919
use cortex_m_semihosting::hprintln;
20-
use fugit::RateExtU32;
2120
use smoltcp::wire::{
2221
ArpOperation, ArpPacket, ArpRepr, EthernetAddress, EthernetFrame, EthernetProtocol,
2322
EthernetRepr, Ipv4Address,
2423
};
2524
use stm32_eth::{
26-
hal::gpio::{GpioExt, Speed},
27-
hal::rcc::RccExt,
2825
mac::{phy::BarePhy, Phy},
2926
stm32::{interrupt, CorePeripherals, Peripherals, SYST},
3027
};
31-
use stm32_eth::{EthPins, RingEntry, TxError};
28+
use stm32_eth::{RingEntry, TxError};
29+
30+
pub mod common;
3231

3332
const PHY_ADDR: u8 = 0;
3433

@@ -40,42 +39,19 @@ fn main() -> ! {
4039
let p = Peripherals::take().unwrap();
4140
let mut cp = CorePeripherals::take().unwrap();
4241

43-
let rcc = p.RCC.constrain();
44-
// HCLK must be at least 25MHz to use the ethernet peripheral
45-
let clocks = rcc.cfgr.sysclk(32.MHz()).hclk(32.MHz()).freeze();
42+
let (clocks, gpio, ethernet) = common::setup_clocks(p);
4643

4744
setup_systick(&mut cp.SYST);
4845

4946
hprintln!("Enabling ethernet...").unwrap();
50-
let gpioa = p.GPIOA.split();
51-
let gpiob = p.GPIOB.split();
52-
let gpioc = p.GPIOC.split();
53-
let gpiog = p.GPIOG.split();
54-
55-
let eth_pins = EthPins {
56-
ref_clk: gpioa.pa1,
57-
crs: gpioa.pa7,
58-
tx_en: gpiob.pb11,
59-
tx_d0: gpiog.pg13,
60-
tx_d1: gpiog.pg14,
61-
rx_d0: gpioc.pc4,
62-
rx_d1: gpioc.pc5,
63-
};
64-
65-
let mut mdio = gpioa.pa2.into_alternate();
66-
mdio.set_speed(Speed::VeryHigh);
67-
let mut mdc = gpioc.pc1.into_alternate();
68-
mdc.set_speed(Speed::VeryHigh);
69-
70-
// ETH_PHY_RESET(RST#) PB2 Chip Reset (active-low)
71-
let _eth_reset = gpiob.pb2.into_push_pull_output().set_high();
47+
let (eth_pins, mdio, mdc) = common::setup_pins(gpio);
7248

7349
let mut rx_ring: [RingEntry<_>; 16] = Default::default();
7450
let mut tx_ring: [RingEntry<_>; 8] = Default::default();
7551
let (mut eth_dma, eth_mac) = stm32_eth::new(
76-
p.ETHERNET_MAC,
77-
p.ETHERNET_MMC,
78-
p.ETHERNET_DMA,
52+
ethernet.mac,
53+
ethernet.mmc,
54+
ethernet.dma,
7955
&mut rx_ring[..],
8056
&mut tx_ring[..],
8157
clocks,

examples/arp.rs

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// cargo build --example arp --features=stm32f407
2-
// This example uses the STM32F407 and the KSZ8051R as PHY. If necessary the pins,
3-
// the PHY register addresses and masks have to be adapted, as well as the IPs.
4-
// With Wireshark, you can see the ARP packets, which should look like this:
5-
// No. Time Source Destination Protocol Length Info
6-
// 1 0.000000000 Cetia_ad:be:ef Broadcast ARP 60 Who has 10.0.0.2? Tell 10.0.0.10
1+
//! cargo build --example arp --features=<MCU>
2+
//! This example should work on any MCU with an 802.3 compatible, on-by-default PHY connected to the
3+
//! default ethernet pins of that MCU.
4+
//! With Wireshark, you can see the ARP packets, which should look like this:
5+
//! No. Time Source Destination Protocol Length Info
6+
//! 1 0.000000000 Cetia_ad:be:ef Broadcast ARP 60 Who has 10.0.0.2? Tell 10.0.0.10
77
88
#![no_std]
99
#![no_main]
@@ -16,17 +16,16 @@ use cortex_m_rt::{entry, exception};
1616

1717
use cortex_m::asm;
1818
use cortex_m::interrupt::Mutex;
19-
use fugit::RateExtU32;
2019
use stm32_eth::{
21-
hal::gpio::{GpioExt, Speed},
22-
hal::rcc::RccExt,
2320
mac::{phy::BarePhy, Phy},
2421
stm32::{interrupt, CorePeripherals, Peripherals, SYST},
2522
};
2623

2724
use cortex_m_semihosting::hprintln;
2825

29-
use stm32_eth::{EthPins, RingEntry, TxError};
26+
pub mod common;
27+
28+
use stm32_eth::{RingEntry, TxError};
3029

3130
const PHY_ADDR: u8 = 0;
3231

@@ -38,42 +37,20 @@ fn main() -> ! {
3837
let p = Peripherals::take().unwrap();
3938
let mut cp = CorePeripherals::take().unwrap();
4039

41-
let rcc = p.RCC.constrain();
42-
// HCLK must be at least 25MHz to use the ethernet peripheral
43-
let clocks = rcc.cfgr.sysclk(32.MHz()).hclk(32.MHz()).freeze();
40+
let (clocks, gpio, ethernet) = common::setup_clocks(p);
4441

4542
setup_systick(&mut cp.SYST);
4643

4744
hprintln!("Enabling ethernet...").unwrap();
48-
let gpioa = p.GPIOA.split();
49-
let gpiob = p.GPIOB.split();
50-
let gpioc = p.GPIOC.split();
51-
let gpiog = p.GPIOG.split();
52-
53-
let eth_pins = EthPins {
54-
ref_clk: gpioa.pa1,
55-
crs: gpioa.pa7,
56-
tx_en: gpiob.pb11,
57-
tx_d0: gpiog.pg13,
58-
tx_d1: gpiog.pg14,
59-
rx_d0: gpioc.pc4,
60-
rx_d1: gpioc.pc5,
61-
};
62-
63-
let mut mdio = gpioa.pa2.into_alternate();
64-
mdio.set_speed(Speed::VeryHigh);
65-
let mut mdc = gpioc.pc1.into_alternate();
66-
mdc.set_speed(Speed::VeryHigh);
67-
68-
// ETH_PHY_RESET(RST#) PB2 Chip Reset (active-low)
69-
let _eth_reset = gpiob.pb2.into_push_pull_output().set_high();
45+
46+
let (eth_pins, mdio, mdc) = common::setup_pins(gpio);
7047

7148
let mut rx_ring: [RingEntry<_>; 16] = Default::default();
7249
let mut tx_ring: [RingEntry<_>; 8] = Default::default();
7350
let (mut eth_dma, eth_mac) = stm32_eth::new(
74-
p.ETHERNET_MAC,
75-
p.ETHERNET_MMC,
76-
p.ETHERNET_DMA,
51+
ethernet.mac,
52+
ethernet.mmc,
53+
ethernet.dma,
7754
&mut rx_ring[..],
7855
&mut tx_ring[..],
7956
clocks,

0 commit comments

Comments
 (0)