Skip to content

Commit e5495b5

Browse files
authored
Merge pull request embassy-rs#3057 from dvdsk/docs-net-size
Document w5500 State and add w5500 example for stmf4
2 parents 4a4b8c9 + ab31a02 commit e5495b5

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

embassy-net-wiznet/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,22 @@ use embedded_hal_async::spi::SpiDevice;
1717
use crate::chip::Chip;
1818
use crate::device::WiznetDevice;
1919

20+
// If you change this update the docs of State
2021
const MTU: usize = 1514;
2122

2223
/// Type alias for the embassy-net driver.
2324
pub type Device<'d> = embassy_net_driver_channel::Device<'d, MTU>;
2425

2526
/// 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.
2636
pub struct State<const N_RX: usize, const N_TX: usize> {
2737
ch_state: ch::State<MTU, N_RX, N_TX>,
2838
}

examples/stm32f4/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ embassy-executor = { version = "0.5.0", path = "../../embassy-executor", feature
1212
embassy-time = { version = "0.3.1", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
1313
embassy-usb = { version = "0.2.0", path = "../../embassy-usb", features = ["defmt" ] }
1414
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"] }
1516
embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
1617

1718
defmt = "0.3"
@@ -20,6 +21,7 @@ defmt-rtt = "0.4"
2021
cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] }
2122
cortex-m-rt = "0.7.0"
2223
embedded-hal = "0.2.6"
24+
embedded-hal-bus = { version = "0.2", features = ["async"] }
2325
embedded-io = { version = "0.6.0" }
2426
embedded-io-async = { version = "0.6.1" }
2527
panic-probe = { version = "0.3", features = ["print-defmt"] }

examples/stm32f4/src/bin/eth_w5500.rs

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)