diff options
| -rw-r--r-- | embassy-net-wiznet/src/lib.rs | 10 | ||||
| -rw-r--r-- | examples/stm32f4/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/eth_w5500.rs | 138 |
3 files changed, 150 insertions, 0 deletions
diff --git a/embassy-net-wiznet/src/lib.rs b/embassy-net-wiznet/src/lib.rs index da70d22bd..90102196a 100644 --- a/embassy-net-wiznet/src/lib.rs +++ b/embassy-net-wiznet/src/lib.rs | |||
| @@ -17,12 +17,22 @@ use embedded_hal_async::spi::SpiDevice; | |||
| 17 | use crate::chip::Chip; | 17 | use crate::chip::Chip; |
| 18 | use crate::device::WiznetDevice; | 18 | use crate::device::WiznetDevice; |
| 19 | 19 | ||
| 20 | // If you change this update the docs of State | ||
| 20 | const MTU: usize = 1514; | 21 | const MTU: usize = 1514; |
| 21 | 22 | ||
| 22 | /// Type alias for the embassy-net driver. | 23 | /// Type alias for the embassy-net driver. |
| 23 | pub type Device<'d> = embassy_net_driver_channel::Device<'d, MTU>; | 24 | pub type Device<'d> = embassy_net_driver_channel::Device<'d, MTU>; |
| 24 | 25 | ||
| 25 | /// Internal state for the embassy-net integration. | 26 | /// Internal state for the embassy-net integration. |
| 27 | /// | ||
| 28 | /// The two generic arguments `N_RX` and `N_TX` set the size of the receive and | ||
| 29 | /// send packet queue. With a the ethernet MTU of _1514_ this takes up `N_RX + | ||
| 30 | /// NTX * 1514` bytes. While setting these both to 1 is the minimum this might | ||
| 31 | /// hurt performance as a packet can not be received while processing another. | ||
| 32 | /// | ||
| 33 | /// # Warning | ||
| 34 | /// On devices with a small amount of ram (think ~64k) watch out with the size | ||
| 35 | /// of there parameters. They will quickly use too much RAM. | ||
| 26 | pub struct State<const N_RX: usize, const N_TX: usize> { | 36 | pub struct State<const N_RX: usize, const N_TX: usize> { |
| 27 | ch_state: ch::State<MTU, N_RX, N_TX>, | 37 | ch_state: ch::State<MTU, N_RX, N_TX>, |
| 28 | } | 38 | } |
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 | } | ||
