diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-06-07 23:00:56 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-06-07 23:00:56 +0000 |
| commit | e5495b51b43f71cdc489e92f4deb0cbc0e27e6aa (patch) | |
| tree | 3514edf08ca79828d81c7db61f725b65c5334093 /examples/stm32f4 | |
| parent | 4a4b8c9b8de8eaf874372f6239dc1a220f172318 (diff) | |
| parent | ab31a02e17b812a965842c1b14dc44b96a57932a (diff) | |
Merge pull request #3057 from dvdsk/docs-net-size
Document w5500 State and add w5500 example for stmf4
Diffstat (limited to 'examples/stm32f4')
| -rw-r--r-- | examples/stm32f4/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/eth_w5500.rs | 138 |
2 files changed, 140 insertions, 0 deletions
diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index ecc1afe78..d909c7e68 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml | |||
| @@ -12,6 +12,7 @@ embassy-executor = { version = "0.5.0", path = "../../embassy-executor", feature | |||
| 12 | embassy-time = { version = "0.3.1", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 12 | embassy-time = { version = "0.3.1", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 13 | embassy-usb = { version = "0.2.0", path = "../../embassy-usb", features = ["defmt" ] } | 13 | embassy-usb = { version = "0.2.0", path = "../../embassy-usb", features = ["defmt" ] } |
| 14 | embassy-net = { version = "0.4.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", ] } | 14 | embassy-net = { version = "0.4.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", ] } |
| 15 | embassy-net-wiznet = { version = "0.1.0", path = "../../embassy-net-wiznet", features = ["defmt"] } | ||
| 15 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } | 16 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } |
| 16 | 17 | ||
| 17 | defmt = "0.3" | 18 | defmt = "0.3" |
| @@ -20,6 +21,7 @@ defmt-rtt = "0.4" | |||
| 20 | cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] } | 21 | cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] } |
| 21 | cortex-m-rt = "0.7.0" | 22 | cortex-m-rt = "0.7.0" |
| 22 | embedded-hal = "0.2.6" | 23 | embedded-hal = "0.2.6" |
| 24 | embedded-hal-bus = { version = "0.2", features = ["async"] } | ||
| 23 | embedded-io = { version = "0.6.0" } | 25 | embedded-io = { version = "0.6.0" } |
| 24 | embedded-io-async = { version = "0.6.1" } | 26 | embedded-io-async = { version = "0.6.1" } |
| 25 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 27 | panic-probe = { version = "0.3", features = ["print-defmt"] } |
diff --git a/examples/stm32f4/src/bin/eth_w5500.rs b/examples/stm32f4/src/bin/eth_w5500.rs new file mode 100644 index 000000000..c51111110 --- /dev/null +++ b/examples/stm32f4/src/bin/eth_w5500.rs | |||
| @@ -0,0 +1,138 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_net::tcp::TcpSocket; | ||
| 7 | use embassy_net::{Ipv4Address, Stack, StackResources}; | ||
| 8 | use embassy_net_wiznet::chip::W5500; | ||
| 9 | use embassy_net_wiznet::{Device, Runner, State}; | ||
| 10 | use embassy_stm32::exti::ExtiInput; | ||
| 11 | use embassy_stm32::gpio::{Level, Output, Pull, Speed}; | ||
| 12 | use embassy_stm32::mode::Async; | ||
| 13 | use embassy_stm32::rng::Rng; | ||
| 14 | use embassy_stm32::spi::Spi; | ||
| 15 | use embassy_stm32::time::Hertz; | ||
| 16 | use embassy_stm32::{bind_interrupts, peripherals, rng, spi, Config}; | ||
| 17 | use embassy_time::{Delay, Timer}; | ||
| 18 | use embedded_hal_bus::spi::ExclusiveDevice; | ||
| 19 | use embedded_io_async::Write; | ||
| 20 | use static_cell::StaticCell; | ||
| 21 | use {defmt_rtt as _, panic_probe as _}; | ||
| 22 | |||
| 23 | bind_interrupts!(struct Irqs { | ||
| 24 | HASH_RNG => rng::InterruptHandler<peripherals::RNG>; | ||
| 25 | }); | ||
| 26 | |||
| 27 | type EthernetSPI = ExclusiveDevice<Spi<'static, Async>, Output<'static>, Delay>; | ||
| 28 | #[embassy_executor::task] | ||
| 29 | async fn ethernet_task(runner: Runner<'static, W5500, EthernetSPI, ExtiInput<'static>, Output<'static>>) -> ! { | ||
| 30 | runner.run().await | ||
| 31 | } | ||
| 32 | |||
| 33 | #[embassy_executor::task] | ||
| 34 | async fn net_task(stack: &'static Stack<Device<'static>>) -> ! { | ||
| 35 | stack.run().await | ||
| 36 | } | ||
| 37 | |||
| 38 | #[embassy_executor::main] | ||
| 39 | async fn main(spawner: Spawner) -> ! { | ||
| 40 | let mut config = Config::default(); | ||
| 41 | { | ||
| 42 | use embassy_stm32::rcc::*; | ||
| 43 | config.rcc.hse = Some(Hse { | ||
| 44 | freq: Hertz(8_000_000), | ||
| 45 | mode: HseMode::Bypass, | ||
| 46 | }); | ||
| 47 | config.rcc.pll_src = PllSource::HSE; | ||
| 48 | config.rcc.pll = Some(Pll { | ||
| 49 | prediv: PllPreDiv::DIV4, | ||
| 50 | mul: PllMul::MUL180, | ||
| 51 | divp: Some(PllPDiv::DIV2), // 8mhz / 4 * 180 / 2 = 180Mhz. | ||
| 52 | divq: None, | ||
| 53 | divr: None, | ||
| 54 | }); | ||
| 55 | config.rcc.ahb_pre = AHBPrescaler::DIV1; | ||
| 56 | config.rcc.apb1_pre = APBPrescaler::DIV4; | ||
| 57 | config.rcc.apb2_pre = APBPrescaler::DIV2; | ||
| 58 | config.rcc.sys = Sysclk::PLL1_P; | ||
| 59 | } | ||
| 60 | let p = embassy_stm32::init(config); | ||
| 61 | |||
| 62 | info!("Hello World!"); | ||
| 63 | |||
| 64 | // Generate random seed | ||
| 65 | let mut rng = Rng::new(p.RNG, Irqs); | ||
| 66 | let mut seed = [0; 8]; | ||
| 67 | unwrap!(rng.async_fill_bytes(&mut seed).await); | ||
| 68 | let seed = u64::from_le_bytes(seed); | ||
| 69 | |||
| 70 | let mut spi_cfg = spi::Config::default(); | ||
| 71 | spi_cfg.frequency = Hertz(50_000_000); // up to 50m works | ||
| 72 | let (miso, mosi, clk) = (p.PA6, p.PA7, p.PA5); | ||
| 73 | let spi = Spi::new(p.SPI1, clk, mosi, miso, p.DMA2_CH3, p.DMA2_CH0, spi_cfg); | ||
| 74 | let cs = Output::new(p.PA4, Level::High, Speed::VeryHigh); | ||
| 75 | let spi = unwrap!(ExclusiveDevice::new(spi, cs, Delay)); | ||
| 76 | |||
| 77 | let w5500_int = ExtiInput::new(p.PB0, p.EXTI0, Pull::Up); | ||
| 78 | let w5500_reset = Output::new(p.PB1, Level::High, Speed::VeryHigh); | ||
| 79 | |||
| 80 | let mac_addr = [0x02, 234, 3, 4, 82, 231]; | ||
| 81 | static STATE: StaticCell<State<2, 2>> = StaticCell::new(); | ||
| 82 | let state = STATE.init(State::<2, 2>::new()); | ||
| 83 | let (device, runner) = embassy_net_wiznet::new(mac_addr, state, spi, w5500_int, w5500_reset).await; | ||
| 84 | unwrap!(spawner.spawn(ethernet_task(runner))); | ||
| 85 | |||
| 86 | let config = embassy_net::Config::dhcpv4(Default::default()); | ||
| 87 | //let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 { | ||
| 88 | // address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24), | ||
| 89 | // dns_servers: Vec::new(), | ||
| 90 | // gateway: Some(Ipv4Address::new(10, 42, 0, 1)), | ||
| 91 | //}); | ||
| 92 | |||
| 93 | static STACK: StaticCell<Stack<Device>> = StaticCell::new(); | ||
| 94 | static RESOURCES: StaticCell<StackResources<2>> = StaticCell::new(); | ||
| 95 | let stack = &*STACK.init(Stack::new( | ||
| 96 | device, | ||
| 97 | config, | ||
| 98 | RESOURCES.init(StackResources::<2>::new()), | ||
| 99 | seed, | ||
| 100 | )); | ||
| 101 | |||
| 102 | // Launch network task | ||
| 103 | unwrap!(spawner.spawn(net_task(stack))); | ||
| 104 | |||
| 105 | // Ensure DHCP configuration is up before trying connect | ||
| 106 | stack.wait_config_up().await; | ||
| 107 | |||
| 108 | info!("Network task initialized"); | ||
| 109 | |||
| 110 | // Then we can use it! | ||
| 111 | let mut rx_buffer = [0; 1024]; | ||
| 112 | let mut tx_buffer = [0; 1024]; | ||
| 113 | |||
| 114 | loop { | ||
| 115 | let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); | ||
| 116 | |||
| 117 | socket.set_timeout(Some(embassy_time::Duration::from_secs(10))); | ||
| 118 | |||
| 119 | let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000); | ||
| 120 | info!("connecting..."); | ||
| 121 | let r = socket.connect(remote_endpoint).await; | ||
| 122 | if let Err(e) = r { | ||
| 123 | info!("connect error: {:?}", e); | ||
| 124 | Timer::after_secs(1).await; | ||
| 125 | continue; | ||
| 126 | } | ||
| 127 | info!("connected!"); | ||
| 128 | let buf = [0; 1024]; | ||
| 129 | loop { | ||
| 130 | let r = socket.write_all(&buf).await; | ||
| 131 | if let Err(e) = r { | ||
| 132 | info!("write error: {:?}", e); | ||
| 133 | break; | ||
| 134 | } | ||
| 135 | Timer::after_secs(1).await; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
