aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32f4/src
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2024-09-11 22:06:26 +0200
committerDario Nieuwenhuis <[email protected]>2024-09-16 21:17:11 +0200
commitbe0d9775e3bcc3c1bd1448e357d7c6cd67b68991 (patch)
tree5e8c444b233a86ade113b096ab1e2934b7bbc7bd /examples/stm32f4/src
parent7648d42b7f23a2caad29ed6e16123b088ccdc8b5 (diff)
net: refactor to simplify lifetimes/generics.
Diffstat (limited to 'examples/stm32f4/src')
-rw-r--r--examples/stm32f4/src/bin/eth.rs13
-rw-r--r--examples/stm32f4/src/bin/eth_w5500.rs11
-rw-r--r--examples/stm32f4/src/bin/usb_ethernet.rs11
3 files changed, 16 insertions, 19 deletions
diff --git a/examples/stm32f4/src/bin/eth.rs b/examples/stm32f4/src/bin/eth.rs
index 9388c64bf..baed96449 100644
--- a/examples/stm32f4/src/bin/eth.rs
+++ b/examples/stm32f4/src/bin/eth.rs
@@ -4,7 +4,7 @@
4use defmt::*; 4use defmt::*;
5use embassy_executor::Spawner; 5use embassy_executor::Spawner;
6use embassy_net::tcp::TcpSocket; 6use embassy_net::tcp::TcpSocket;
7use embassy_net::{Ipv4Address, Stack, StackResources}; 7use embassy_net::{Ipv4Address, StackResources};
8use embassy_stm32::eth::generic_smi::GenericSMI; 8use embassy_stm32::eth::generic_smi::GenericSMI;
9use embassy_stm32::eth::{Ethernet, PacketQueue}; 9use embassy_stm32::eth::{Ethernet, PacketQueue};
10use embassy_stm32::peripherals::ETH; 10use embassy_stm32::peripherals::ETH;
@@ -24,8 +24,8 @@ bind_interrupts!(struct Irqs {
24type Device = Ethernet<'static, ETH, GenericSMI>; 24type Device = Ethernet<'static, ETH, GenericSMI>;
25 25
26#[embassy_executor::task] 26#[embassy_executor::task]
27async fn net_task(stack: &'static Stack<Device>) -> ! { 27async fn net_task(mut runner: embassy_net::Runner<'static, Device>) -> ! {
28 stack.run().await 28 runner.run().await
29} 29}
30 30
31#[embassy_executor::main] 31#[embassy_executor::main]
@@ -88,12 +88,11 @@ async fn main(spawner: Spawner) -> ! {
88 //}); 88 //});
89 89
90 // Init network stack 90 // Init network stack
91 static STACK: StaticCell<Stack<Device>> = StaticCell::new();
92 static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new(); 91 static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();
93 let stack = &*STACK.init(Stack::new(device, config, RESOURCES.init(StackResources::new()), seed)); 92 let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed);
94 93
95 // Launch network task 94 // Launch network task
96 unwrap!(spawner.spawn(net_task(stack))); 95 unwrap!(spawner.spawn(net_task(runner)));
97 96
98 // Ensure DHCP configuration is up before trying connect 97 // Ensure DHCP configuration is up before trying connect
99 stack.wait_config_up().await; 98 stack.wait_config_up().await;
@@ -105,7 +104,7 @@ async fn main(spawner: Spawner) -> ! {
105 let mut tx_buffer = [0; 4096]; 104 let mut tx_buffer = [0; 4096];
106 105
107 loop { 106 loop {
108 let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer); 107 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
109 108
110 socket.set_timeout(Some(embassy_time::Duration::from_secs(10))); 109 socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
111 110
diff --git a/examples/stm32f4/src/bin/eth_w5500.rs b/examples/stm32f4/src/bin/eth_w5500.rs
index 5c3c6c3ba..6e6bef08c 100644
--- a/examples/stm32f4/src/bin/eth_w5500.rs
+++ b/examples/stm32f4/src/bin/eth_w5500.rs
@@ -4,7 +4,7 @@
4use defmt::*; 4use defmt::*;
5use embassy_executor::Spawner; 5use embassy_executor::Spawner;
6use embassy_net::tcp::TcpSocket; 6use embassy_net::tcp::TcpSocket;
7use embassy_net::{Ipv4Address, Stack, StackResources}; 7use embassy_net::{Ipv4Address, StackResources};
8use embassy_net_wiznet::chip::W5500; 8use embassy_net_wiznet::chip::W5500;
9use embassy_net_wiznet::{Device, Runner, State}; 9use embassy_net_wiznet::{Device, Runner, State};
10use embassy_stm32::exti::ExtiInput; 10use embassy_stm32::exti::ExtiInput;
@@ -31,8 +31,8 @@ async fn ethernet_task(runner: Runner<'static, W5500, EthernetSPI, ExtiInput<'st
31} 31}
32 32
33#[embassy_executor::task] 33#[embassy_executor::task]
34async fn net_task(stack: &'static Stack<Device<'static>>) -> ! { 34async fn net_task(mut runner: embassy_net::Runner<'static, Device<'static>>) -> ! {
35 stack.run().await 35 runner.run().await
36} 36}
37 37
38#[embassy_executor::main] 38#[embassy_executor::main]
@@ -92,12 +92,11 @@ async fn main(spawner: Spawner) -> ! {
92 // gateway: Some(Ipv4Address::new(10, 42, 0, 1)), 92 // gateway: Some(Ipv4Address::new(10, 42, 0, 1)),
93 //}); 93 //});
94 94
95 static STACK: StaticCell<Stack<Device>> = StaticCell::new();
96 static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new(); 95 static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();
97 let stack = &*STACK.init(Stack::new(device, config, RESOURCES.init(StackResources::new()), seed)); 96 let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed);
98 97
99 // Launch network task 98 // Launch network task
100 unwrap!(spawner.spawn(net_task(stack))); 99 unwrap!(spawner.spawn(net_task(runner)));
101 100
102 // Ensure DHCP configuration is up before trying connect 101 // Ensure DHCP configuration is up before trying connect
103 stack.wait_config_up().await; 102 stack.wait_config_up().await;
diff --git a/examples/stm32f4/src/bin/usb_ethernet.rs b/examples/stm32f4/src/bin/usb_ethernet.rs
index 94e51c338..a9504ec04 100644
--- a/examples/stm32f4/src/bin/usb_ethernet.rs
+++ b/examples/stm32f4/src/bin/usb_ethernet.rs
@@ -4,7 +4,7 @@
4use defmt::*; 4use defmt::*;
5use embassy_executor::Spawner; 5use embassy_executor::Spawner;
6use embassy_net::tcp::TcpSocket; 6use embassy_net::tcp::TcpSocket;
7use embassy_net::{Stack, StackResources}; 7use embassy_net::StackResources;
8use embassy_stm32::rng::{self, Rng}; 8use embassy_stm32::rng::{self, Rng};
9use embassy_stm32::time::Hertz; 9use embassy_stm32::time::Hertz;
10use embassy_stm32::usb::Driver; 10use embassy_stm32::usb::Driver;
@@ -31,8 +31,8 @@ async fn usb_ncm_task(class: Runner<'static, UsbDriver, MTU>) -> ! {
31} 31}
32 32
33#[embassy_executor::task] 33#[embassy_executor::task]
34async fn net_task(stack: &'static Stack<Device<'static, MTU>>) -> ! { 34async fn net_task(mut runner: embassy_net::Runner<'static, Device<'static, MTU>>) -> ! {
35 stack.run().await 35 runner.run().await
36} 36}
37 37
38bind_interrupts!(struct Irqs { 38bind_interrupts!(struct Irqs {
@@ -144,11 +144,10 @@ async fn main(spawner: Spawner) {
144 let seed = u64::from_le_bytes(seed); 144 let seed = u64::from_le_bytes(seed);
145 145
146 // Init network stack 146 // Init network stack
147 static STACK: StaticCell<Stack<Device<'static, MTU>>> = StaticCell::new();
148 static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new(); 147 static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();
149 let stack = &*STACK.init(Stack::new(device, config, RESOURCES.init(StackResources::new()), seed)); 148 let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed);
150 149
151 unwrap!(spawner.spawn(net_task(stack))); 150 unwrap!(spawner.spawn(net_task(runner)));
152 151
153 // And now we can use it! 152 // And now we can use it!
154 153