diff options
| author | Dario Nieuwenhuis <[email protected]> | 2025-03-20 13:34:29 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-03-20 13:34:29 +0000 |
| commit | fecb7a2b2b6f1953a2fe57557cb83d063ab5eea4 (patch) | |
| tree | 95803202e661fc1884a6738c40854844f0948c5e | |
| parent | 31352b2436ceeee65c76d5cc85124cc3dfa40c34 (diff) | |
| parent | 83b70d5847e919421696b40ac6ed7021eb7a4678 (diff) | |
Merge pull request #3976 from friedman-ionq/stm32h7rs-enet
define stm32h7rs ethernet pin traits
| -rw-r--r-- | examples/stm32h7rs/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32h7rs/src/bin/eth.rs | 119 |
2 files changed, 120 insertions, 1 deletions
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 = [" | |||
| 10 | embassy-sync = { version = "0.6.2", path = "../../embassy-sync", features = ["defmt"] } | 10 | embassy-sync = { version = "0.6.2", path = "../../embassy-sync", features = ["defmt"] } |
| 11 | embassy-executor = { version = "0.7.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt"] } | 11 | embassy-executor = { version = "0.7.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt"] } |
| 12 | embassy-time = { version = "0.4.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 12 | embassy-time = { version = "0.4.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 13 | embassy-net = { version = "0.7.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "proto-ipv6", "dns"] } | 13 | embassy-net = { version = "0.7.0", path = "../../embassy-net", features = ["defmt", "udp", "medium-ethernet", "medium-ip", "proto-ipv4"] } |
| 14 | embassy-usb = { version = "0.4.0", path = "../../embassy-usb", features = ["defmt"] } | 14 | embassy-usb = { version = "0.4.0", path = "../../embassy-usb", features = ["defmt"] } |
| 15 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } | 15 | embassy-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..f2bd9575e --- /dev/null +++ b/examples/stm32h7rs/src/bin/eth.rs | |||
| @@ -0,0 +1,119 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_net::udp::{PacketMetadata, UdpSocket}; | ||
| 7 | use embassy_net::{Ipv4Address, Ipv4Cidr, StackResources}; | ||
| 8 | use embassy_stm32::eth::{Ethernet, GenericPhy, PacketQueue}; | ||
| 9 | use embassy_stm32::peripherals::ETH; | ||
| 10 | use embassy_stm32::rng::Rng; | ||
| 11 | use embassy_stm32::{bind_interrupts, eth, peripherals, rng, Config}; | ||
| 12 | use embassy_time::Timer; | ||
| 13 | use heapless::Vec; | ||
| 14 | use rand_core::RngCore; | ||
| 15 | use static_cell::StaticCell; | ||
| 16 | use {defmt_rtt as _, panic_probe as _}; | ||
| 17 | |||
| 18 | bind_interrupts!(struct Irqs { | ||
| 19 | ETH => eth::InterruptHandler; | ||
| 20 | RNG => rng::InterruptHandler<peripherals::RNG>; | ||
| 21 | }); | ||
| 22 | |||
| 23 | type Device = Ethernet<'static, ETH, GenericPhy>; | ||
| 24 | |||
| 25 | #[embassy_executor::task] | ||
| 26 | async fn net_task(mut runner: embassy_net::Runner<'static, Device>) -> ! { | ||
| 27 | runner.run().await | ||
| 28 | } | ||
| 29 | |||
| 30 | #[embassy_executor::main] | ||
| 31 | async 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 | ||
| 114 | .send_to(b"Hello, world", remote_endpoint) | ||
| 115 | .await | ||
| 116 | .expect("Buffer sent"); | ||
| 117 | Timer::after_secs(1).await; | ||
| 118 | } | ||
| 119 | } | ||
