aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-stm32/build.rs18
-rw-r--r--examples/stm32h7rs/Cargo.toml2
-rw-r--r--examples/stm32h7rs/src/bin/eth.rs116
3 files changed, 135 insertions, 1 deletions
diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs
index eb0437bc2..8965b312c 100644
--- a/embassy-stm32/build.rs
+++ b/embassy-stm32/build.rs
@@ -911,6 +911,24 @@ fn main() {
911 (("eth", "TXD2"), quote!(crate::eth::TXD2Pin)), 911 (("eth", "TXD2"), quote!(crate::eth::TXD2Pin)),
912 (("eth", "TXD3"), quote!(crate::eth::TXD3Pin)), 912 (("eth", "TXD3"), quote!(crate::eth::TXD3Pin)),
913 (("eth", "TX_EN"), quote!(crate::eth::TXEnPin)), 913 (("eth", "TX_EN"), quote!(crate::eth::TXEnPin)),
914 (("eth", "MII_REF_CLK"), quote!(crate::eth::RefClkPin)),
915 (("eth", "MII_RX_CLK"), quote!(crate::eth::RXClkPin)),
916 (("eth", "MII_TX_CLK"), quote!(crate::eth::TXClkPin)),
917 (("eth", "MII_MDIO"), quote!(crate::eth::MDIOPin)),
918 (("eth", "MII_MDC"), quote!(crate::eth::MDCPin)),
919 (("eth", "MII_CRS_DV"), quote!(crate::eth::CRSPin)),
920 (("eth", "MII_RX_DV"), quote!(crate::eth::RXDVPin)),
921 (("eth", "MII_RXD0"), quote!(crate::eth::RXD0Pin)),
922 (("eth", "MII_RXD1"), quote!(crate::eth::RXD1Pin)),
923 (("eth", "MII_RXD2"), quote!(crate::eth::RXD2Pin)),
924 (("eth", "MII_RXD3"), quote!(crate::eth::RXD3Pin)),
925 (("eth", "MII_TXD0"), quote!(crate::eth::TXD0Pin)),
926 (("eth", "MII_TXD1"), quote!(crate::eth::TXD1Pin)),
927 (("eth", "MII_TXD2"), quote!(crate::eth::TXD2Pin)),
928 (("eth", "MII_TXD3"), quote!(crate::eth::TXD3Pin)),
929 (("eth", "MII_TX_EN"), quote!(crate::eth::TXEnPin)),
930 (("eth", "RMII_REF_CLK"), quote!(crate::eth::RefClkPin)),
931 (("eth", "RMII_CRS_DV"), quote!(crate::eth::CRSPin)),
914 (("fmc", "A0"), quote!(crate::fmc::A0Pin)), 932 (("fmc", "A0"), quote!(crate::fmc::A0Pin)),
915 (("fmc", "A1"), quote!(crate::fmc::A1Pin)), 933 (("fmc", "A1"), quote!(crate::fmc::A1Pin)),
916 (("fmc", "A2"), quote!(crate::fmc::A2Pin)), 934 (("fmc", "A2"), quote!(crate::fmc::A2Pin)),
diff --git a/examples/stm32h7rs/Cargo.toml b/examples/stm32h7rs/Cargo.toml
index 6564fffbf..a47dbe21e 100644
--- a/examples/stm32h7rs/Cargo.toml
+++ b/examples/stm32h7rs/Cargo.toml
@@ -10,7 +10,7 @@ embassy-stm32 = { version = "0.2.0", path = "../../embassy-stm32", features = ["
10embassy-sync = { version = "0.6.2", path = "../../embassy-sync", features = ["defmt"] } 10embassy-sync = { version = "0.6.2", path = "../../embassy-sync", features = ["defmt"] }
11embassy-executor = { version = "0.7.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt"] } 11embassy-executor = { version = "0.7.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt"] }
12embassy-time = { version = "0.4.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } 12embassy-time = { version = "0.4.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
13embassy-net = { version = "0.7.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "proto-ipv6", "dns"] } 13embassy-net = { version = "0.7.0", path = "../../embassy-net", features = ["defmt", "udp", "medium-ethernet", "medium-ip", "proto-ipv4"] }
14embassy-usb = { version = "0.4.0", path = "../../embassy-usb", features = ["defmt"] } 14embassy-usb = { version = "0.4.0", path = "../../embassy-usb", features = ["defmt"] }
15embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } 15embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
16 16
diff --git a/examples/stm32h7rs/src/bin/eth.rs b/examples/stm32h7rs/src/bin/eth.rs
new file mode 100644
index 000000000..1bb748faa
--- /dev/null
+++ b/examples/stm32h7rs/src/bin/eth.rs
@@ -0,0 +1,116 @@
1#![no_std]
2#![no_main]
3
4use defmt::*;
5use embassy_executor::Spawner;
6use embassy_net::udp::{PacketMetadata, UdpSocket};
7use embassy_net::{Ipv4Cidr, Ipv4Address, StackResources};
8use embassy_stm32::eth::{Ethernet, GenericPhy, PacketQueue};
9use embassy_stm32::peripherals::ETH;
10use embassy_stm32::rng::Rng;
11use embassy_stm32::{bind_interrupts, eth, peripherals, rng, Config};
12use embassy_time::Timer;
13use heapless::Vec;
14use rand_core::RngCore;
15use static_cell::StaticCell;
16use {defmt_rtt as _, panic_probe as _};
17
18bind_interrupts!(struct Irqs {
19 ETH => eth::InterruptHandler;
20 RNG => rng::InterruptHandler<peripherals::RNG>;
21});
22
23type Device = Ethernet<'static, ETH, GenericPhy>;
24
25#[embassy_executor::task]
26async fn net_task(mut runner: embassy_net::Runner<'static, Device>) -> ! {
27 runner.run().await
28}
29
30#[embassy_executor::main]
31async fn main(spawner: Spawner) -> ! {
32 let mut config = Config::default();
33 {
34 use embassy_stm32::rcc::*;
35 config.rcc.hsi = Some(HSIPrescaler::DIV1);
36 config.rcc.csi = true;
37 config.rcc.hsi48 = Some(Default::default()); // needed for RNG
38 config.rcc.pll1 = Some(Pll {
39 source: PllSource::HSI,
40 prediv: PllPreDiv::DIV4,
41 mul: PllMul::MUL50,
42 divp: Some(PllDiv::DIV2),
43 divq: None,
44 divr: None,
45 });
46 config.rcc.sys = Sysclk::PLL1_P; // 400 Mhz
47 config.rcc.ahb_pre = AHBPrescaler::DIV2; // 200 Mhz
48 config.rcc.apb1_pre = APBPrescaler::DIV2; // 100 Mhz
49 config.rcc.apb2_pre = APBPrescaler::DIV2; // 100 Mhz
50 config.rcc.apb4_pre = APBPrescaler::DIV2; // 100 Mhz
51 config.rcc.apb5_pre = APBPrescaler::DIV2; // 100 Mhz
52 config.rcc.voltage_scale = VoltageScale::HIGH;
53 }
54 let p = embassy_stm32::init(config);
55 info!("Hello World!");
56
57 // Generate random seed.
58 let mut rng = Rng::new(p.RNG, Irqs);
59 let mut seed = [0; 8];
60 rng.fill_bytes(&mut seed);
61 let seed = u64::from_le_bytes(seed);
62
63 let mac_addr = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF];
64
65 static PACKETS: StaticCell<PacketQueue<4, 4>> = StaticCell::new();
66 let device = Ethernet::new(
67 PACKETS.init(PacketQueue::<4, 4>::new()),
68 p.ETH,
69 Irqs,
70 p.PB6,
71 p.PA2,
72 p.PG6,
73 p.PA7,
74 p.PG4,
75 p.PG5,
76 p.PG13,
77 p.PG12,
78 p.PG11,
79 GenericPhy::new(0),
80 mac_addr,
81 );
82
83 // Have to use UDP w/ static config to fit in internal flash
84 // let config = embassy_net::Config::dhcpv4(Default::default());
85 let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
86 address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
87 dns_servers: Vec::new(),
88 gateway: Some(Ipv4Address::new(10, 42, 0, 1)),
89 });
90
91 // Init network stack
92 static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();
93 let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed);
94
95 // Launch network task
96 unwrap!(spawner.spawn(net_task(runner)));
97
98 // Ensure DHCP configuration is up before trying connect
99 stack.wait_config_up().await;
100
101 info!("Network task initialized");
102
103 // Then we can use it!
104 let mut rx_meta = [PacketMetadata::EMPTY; 16];
105 let mut rx_buffer = [0; 1024];
106 let mut tx_meta = [PacketMetadata::EMPTY; 16];
107 let mut tx_buffer = [0; 1024];
108
109 let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000);
110 let socket = UdpSocket::new(stack, &mut rx_meta, &mut rx_buffer, &mut tx_meta, &mut tx_buffer);
111 loop {
112 // You need to start a server on the host machine, for example: `nc -lu 8000`
113 socket.send_to(b"Hello, world", remote_endpoint).await.expect("Buffer sent");
114 Timer::after_secs(1).await;
115 }
116}