From a5aea995a802fea8fc1b3e4b5fe47bd6d1fca2a4 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 23 May 2022 03:50:43 +0200 Subject: WIP embassy-net v2 --- examples/stm32f7/src/bin/eth.rs | 173 +++++++++++++++++++--------------------- 1 file changed, 83 insertions(+), 90 deletions(-) (limited to 'examples/stm32f7/src') diff --git a/examples/stm32f7/src/bin/eth.rs b/examples/stm32f7/src/bin/eth.rs index dca9338b2..af012f826 100644 --- a/examples/stm32f7/src/bin/eth.rs +++ b/examples/stm32f7/src/bin/eth.rs @@ -2,130 +2,123 @@ #![no_main] #![feature(type_alias_impl_trait)] -use cortex_m_rt::entry; use defmt::*; -use embassy::executor::{Executor, Spawner}; +use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy::util::Forever; use embassy_net::tcp::TcpSocket; -use embassy_net::{Config as NetConfig, Ipv4Address, Ipv4Cidr, StackResources, StaticConfigurator}; +use embassy_net::{Ipv4Address, Stack, StackResources}; use embassy_stm32::eth::generic_smi::GenericSMI; use embassy_stm32::eth::{Ethernet, State}; -use embassy_stm32::interrupt; use embassy_stm32::peripherals::ETH; -use embassy_stm32::peripherals::RNG; use embassy_stm32::rng::Rng; use embassy_stm32::time::U32Ext; use embassy_stm32::Config; +use embassy_stm32::{interrupt, Peripherals}; use embedded_io::asynch::Write; -use heapless::Vec; use defmt_rtt as _; // global logger use panic_probe as _; - -#[embassy::task] -async fn main_task( - device: &'static mut Ethernet<'static, ETH, GenericSMI, 4, 4>, - config: &'static mut StaticConfigurator, - spawner: Spawner, -) { - let net_resources = NET_RESOURCES.put(StackResources::new()); - - // Init network stack - embassy_net::init(device, config, net_resources); - - // Launch network task - unwrap!(spawner.spawn(net_task())); - - info!("Network task initialized"); - - // Then we can use it! - let mut rx_buffer = [0; 1024]; - let mut tx_buffer = [0; 1024]; - let mut socket = TcpSocket::new(&mut rx_buffer, &mut tx_buffer); - - socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); - - let remote_endpoint = (Ipv4Address::new(192, 168, 0, 10), 8000); - let r = socket.connect(remote_endpoint).await; - if let Err(e) = r { - info!("connect error: {:?}", e); - return; - } - info!("connected!"); - loop { - let r = socket.write_all(b"Hello\n").await; - if let Err(e) = r { - info!("write error: {:?}", e); - return; - } - Timer::after(Duration::from_secs(1)).await; - } -} - -#[embassy::task] -async fn net_task() { - embassy_net::run().await +use rand_core::RngCore; + +macro_rules! forever { + ($val:expr) => {{ + type T = impl Sized; + static FOREVER: Forever = Forever::new(); + FOREVER.put_with(move || $val) + }}; } -#[no_mangle] -fn _embassy_rand(buf: &mut [u8]) { - use rand_core::RngCore; +type Device = Ethernet<'static, ETH, GenericSMI, 4, 4>; - critical_section::with(|_| unsafe { - unwrap!(RNG_INST.as_mut()).fill_bytes(buf); - }); +#[embassy::task] +async fn net_task(stack: &'static Stack) -> ! { + stack.run().await } -static mut RNG_INST: Option> = None; - -static EXECUTOR: Forever = Forever::new(); -static STATE: Forever> = Forever::new(); -static ETH: Forever> = Forever::new(); -static CONFIG: Forever = Forever::new(); -static NET_RESOURCES: Forever> = Forever::new(); - fn config() -> Config { let mut config = Config::default(); config.rcc.sys_ck = Some(200.mhz().into()); config } -#[entry] -fn main() -> ! { +#[embassy::main(config = "config()")] +async fn main(spawner: Spawner, p: Peripherals) -> ! { info!("Hello World!"); - info!("Setup RCC..."); - - let p = embassy_stm32::init(config()); - - let rng = Rng::new(p.RNG); - unsafe { - RNG_INST.replace(rng); - } + // Generate random seed. + let mut rng = Rng::new(p.RNG); + let mut seed = [0; 8]; + rng.fill_bytes(&mut seed); + let seed = u64::from_le_bytes(seed); let eth_int = interrupt::take!(ETH); let mac_addr = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF]; - let state = STATE.put(State::new()); - let eth = unsafe { - ETH.put(Ethernet::new( - state, p.ETH, eth_int, p.PA1, p.PA2, p.PC1, p.PA7, p.PC4, p.PC5, p.PG13, p.PB13, - p.PG11, GenericSMI, mac_addr, 0, - )) + let device = unsafe { + Ethernet::new( + forever!(State::new()), + p.ETH, + eth_int, + p.PA1, + p.PA2, + p.PC1, + p.PA7, + p.PC4, + p.PC5, + p.PG13, + p.PB13, + p.PG11, + GenericSMI, + mac_addr, + 0, + ) }; - let config = StaticConfigurator::new(NetConfig { - address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 0, 61), 24), - dns_servers: Vec::new(), - gateway: Some(Ipv4Address::new(192, 168, 0, 1)), - }); + let config = embassy_net::ConfigStrategy::Dhcp; + //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { + // address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24), + // dns_servers: Vec::new(), + // gateway: Some(Ipv4Address::new(10, 42, 0, 1)), + //}); - let config = CONFIG.put(config); + // Init network stack + let stack = &*forever!(Stack::new( + device, + config, + forever!(StackResources::<1, 2, 8>::new()), + seed + )); - let executor = EXECUTOR.put(Executor::new()); + // Launch network task + unwrap!(spawner.spawn(net_task(&stack))); - executor.run(move |spawner| { - unwrap!(spawner.spawn(main_task(eth, config, spawner))); - }) + info!("Network task initialized"); + + // Then we can use it! + let mut rx_buffer = [0; 1024]; + let mut tx_buffer = [0; 1024]; + + loop { + let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer); + + socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); + + let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000); + info!("connecting..."); + let r = socket.connect(remote_endpoint).await; + if let Err(e) = r { + info!("connect error: {:?}", e); + continue; + } + info!("connected!"); + loop { + let r = socket.write_all(b"Hello\n").await; + if let Err(e) = r { + info!("write error: {:?}", e); + return; + } + Timer::after(Duration::from_secs(1)).await; + } + } } -- cgit