aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-12-13 17:03:51 +0100
committerGitHub <[email protected]>2022-12-13 17:03:51 +0100
commit5b65b0e84377338b615ba3fc16d0a96d2db5c206 (patch)
tree627cc0abdfdf95f504728fceaaf9816575d05e84 /examples
parent47747d3b73f392e53ead8ff49cd09fd017df3215 (diff)
parent790e4e1594d455d06c7303a628172244e78af0da (diff)
Merge pull request #1096 from embassy-rs/net-refactor2
net: remove packet pool
Diffstat (limited to 'examples')
-rw-r--r--examples/nrf/Cargo.toml2
-rw-r--r--examples/nrf/src/bin/usb_ethernet.rs139
-rw-r--r--examples/rp/Cargo.toml2
-rw-r--r--examples/rp/src/bin/usb_ethernet.rs140
-rw-r--r--examples/std/src/tuntap.rs113
-rw-r--r--examples/stm32f7/Cargo.toml2
-rw-r--r--examples/stm32f7/src/bin/eth.rs50
-rw-r--r--examples/stm32h7/Cargo.toml2
-rw-r--r--examples/stm32h7/src/bin/eth.rs43
-rw-r--r--examples/stm32h7/src/bin/eth_client.rs43
-rw-r--r--examples/stm32l5/Cargo.toml2
-rw-r--r--examples/stm32l5/src/bin/usb_ethernet.rs140
12 files changed, 181 insertions, 497 deletions
diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml
index 8b95ac3a6..a980a505c 100644
--- a/examples/nrf/Cargo.toml
+++ b/examples/nrf/Cargo.toml
@@ -16,7 +16,7 @@ embassy-executor = { version = "0.1.0", path = "../../embassy-executor", feature
16embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } 16embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] }
17embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac"] } 17embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac"] }
18embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16"], optional = true } 18embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16"], optional = true }
19embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"], optional = true } 19embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt", "embassy-net"], optional = true }
20embedded-io = "0.4.0" 20embedded-io = "0.4.0"
21embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx126x", "time", "defmt"], optional = true } 21embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx126x", "time", "defmt"], optional = true }
22 22
diff --git a/examples/nrf/src/bin/usb_ethernet.rs b/examples/nrf/src/bin/usb_ethernet.rs
index de93a2b45..e5f704524 100644
--- a/examples/nrf/src/bin/usb_ethernet.rs
+++ b/examples/nrf/src/bin/usb_ethernet.rs
@@ -3,19 +3,16 @@
3#![feature(type_alias_impl_trait)] 3#![feature(type_alias_impl_trait)]
4 4
5use core::mem; 5use core::mem;
6use core::sync::atomic::{AtomicBool, Ordering};
7use core::task::Waker;
8 6
9use defmt::*; 7use defmt::*;
10use embassy_executor::Spawner; 8use embassy_executor::Spawner;
11use embassy_net::tcp::TcpSocket; 9use embassy_net::tcp::TcpSocket;
12use embassy_net::{PacketBox, PacketBoxExt, PacketBuf, Stack, StackResources}; 10use embassy_net::{Stack, StackResources};
13use embassy_nrf::rng::Rng; 11use embassy_nrf::rng::Rng;
14use embassy_nrf::usb::{Driver, PowerUsb}; 12use embassy_nrf::usb::{Driver, PowerUsb};
15use embassy_nrf::{interrupt, pac, peripherals}; 13use embassy_nrf::{interrupt, pac, peripherals};
16use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; 14use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState};
17use embassy_sync::channel::Channel; 15use embassy_usb::class::cdc_ncm::{CdcNcmClass, State};
18use embassy_usb::class::cdc_ncm::{CdcNcmClass, Receiver, Sender, State};
19use embassy_usb::{Builder, Config, UsbDevice}; 16use embassy_usb::{Builder, Config, UsbDevice};
20use embedded_io::asynch::Write; 17use embedded_io::asynch::Write;
21use static_cell::StaticCell; 18use static_cell::StaticCell;
@@ -27,56 +24,25 @@ macro_rules! singleton {
27 ($val:expr) => {{ 24 ($val:expr) => {{
28 type T = impl Sized; 25 type T = impl Sized;
29 static STATIC_CELL: StaticCell<T> = StaticCell::new(); 26 static STATIC_CELL: StaticCell<T> = StaticCell::new();
30 STATIC_CELL.init_with(move || $val) 27 let (x,) = STATIC_CELL.init(($val,));
28 x
31 }}; 29 }};
32} 30}
33 31
32const MTU: usize = 1514;
33
34#[embassy_executor::task] 34#[embassy_executor::task]
35async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! { 35async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! {
36 device.run().await 36 device.run().await
37} 37}
38 38
39#[embassy_executor::task] 39#[embassy_executor::task]
40async fn usb_ncm_rx_task(mut class: Receiver<'static, MyDriver>) { 40async fn usb_ncm_task(class: Runner<'static, MyDriver, MTU>) -> ! {
41 loop { 41 class.run().await
42 warn!("WAITING for connection");
43 LINK_UP.store(false, Ordering::Relaxed);
44
45 class.wait_connection().await.unwrap();
46
47 warn!("Connected");
48 LINK_UP.store(true, Ordering::Relaxed);
49
50 loop {
51 let mut p = unwrap!(PacketBox::new(embassy_net::Packet::new()));
52 let n = match class.read_packet(&mut p[..]).await {
53 Ok(n) => n,
54 Err(e) => {
55 warn!("error reading packet: {:?}", e);
56 break;
57 }
58 };
59
60 let buf = p.slice(0..n);
61 if RX_CHANNEL.try_send(buf).is_err() {
62 warn!("Failed pushing rx'd packet to channel.");
63 }
64 }
65 }
66}
67
68#[embassy_executor::task]
69async fn usb_ncm_tx_task(mut class: Sender<'static, MyDriver>) {
70 loop {
71 let pkt = TX_CHANNEL.recv().await;
72 if let Err(e) = class.write_packet(&pkt[..]).await {
73 warn!("Failed to TX packet: {:?}", e);
74 }
75 }
76} 42}
77 43
78#[embassy_executor::task] 44#[embassy_executor::task]
79async fn net_task(stack: &'static Stack<Device>) -> ! { 45async fn net_task(stack: &'static Stack<Device<'static, MTU>>) -> ! {
80 stack.run().await 46 stack.run().await
81} 47}
82 48
@@ -108,55 +74,32 @@ async fn main(spawner: Spawner) {
108 config.device_sub_class = 0x02; 74 config.device_sub_class = 0x02;
109 config.device_protocol = 0x01; 75 config.device_protocol = 0x01;
110 76
111 struct Resources {
112 device_descriptor: [u8; 256],
113 config_descriptor: [u8; 256],
114 bos_descriptor: [u8; 256],
115 control_buf: [u8; 128],
116 serial_state: State<'static>,
117 }
118 let res: &mut Resources = singleton!(Resources {
119 device_descriptor: [0; 256],
120 config_descriptor: [0; 256],
121 bos_descriptor: [0; 256],
122 control_buf: [0; 128],
123 serial_state: State::new(),
124 });
125
126 // Create embassy-usb DeviceBuilder using the driver and config. 77 // Create embassy-usb DeviceBuilder using the driver and config.
127 let mut builder = Builder::new( 78 let mut builder = Builder::new(
128 driver, 79 driver,
129 config, 80 config,
130 &mut res.device_descriptor, 81 &mut singleton!([0; 256])[..],
131 &mut res.config_descriptor, 82 &mut singleton!([0; 256])[..],
132 &mut res.bos_descriptor, 83 &mut singleton!([0; 256])[..],
133 &mut res.control_buf, 84 &mut singleton!([0; 128])[..],
134 None, 85 None,
135 ); 86 );
136 87
137 // WARNINGS for Android ethernet tethering:
138 // - On Pixel 4a, it refused to work on Android 11, worked on Android 12.
139 // - if the host's MAC address has the "locally-administered" bit set (bit 1 of first byte),
140 // it doesn't work! The "Ethernet tethering" option in settings doesn't get enabled.
141 // This is due to regex spaghetti: https://android.googlesource.com/platform/frameworks/base/+/refs/tags/android-mainline-12.0.0_r84/core/res/res/values/config.xml#417
142 // and this nonsense in the linux kernel: https://github.com/torvalds/linux/blob/c00c5e1d157bec0ef0b0b59aa5482eb8dc7e8e49/drivers/net/usb/usbnet.c#L1751-L1757
143
144 // Our MAC addr. 88 // Our MAC addr.
145 let our_mac_addr = [0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC]; 89 let our_mac_addr = [0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC];
146 // Host's MAC addr. This is the MAC the host "thinks" its USB-to-ethernet adapter has. 90 // Host's MAC addr. This is the MAC the host "thinks" its USB-to-ethernet adapter has.
147 let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88]; 91 let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88];
148 92
149 // Create classes on the builder. 93 // Create classes on the builder.
150 let class = CdcNcmClass::new(&mut builder, &mut res.serial_state, host_mac_addr, 64); 94 let class = CdcNcmClass::new(&mut builder, singleton!(State::new()), host_mac_addr, 64);
151 95
152 // Build the builder. 96 // Build the builder.
153 let usb = builder.build(); 97 let usb = builder.build();
154 98
155 unwrap!(spawner.spawn(usb_task(usb))); 99 unwrap!(spawner.spawn(usb_task(usb)));
156 100
157 let (tx, rx) = class.split(); 101 let (runner, device) = class.into_embassy_net_device::<MTU, 4, 4>(singleton!(NetState::new()), our_mac_addr);
158 unwrap!(spawner.spawn(usb_ncm_rx_task(rx))); 102 unwrap!(spawner.spawn(usb_ncm_task(runner)));
159 unwrap!(spawner.spawn(usb_ncm_tx_task(tx)));
160 103
161 let config = embassy_net::ConfigStrategy::Dhcp; 104 let config = embassy_net::ConfigStrategy::Dhcp;
162 //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { 105 //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config {
@@ -172,7 +115,6 @@ async fn main(spawner: Spawner) {
172 let seed = u64::from_le_bytes(seed); 115 let seed = u64::from_le_bytes(seed);
173 116
174 // Init network stack 117 // Init network stack
175 let device = Device { mac_addr: our_mac_addr };
176 let stack = &*singleton!(Stack::new( 118 let stack = &*singleton!(Stack::new(
177 device, 119 device,
178 config, 120 config,
@@ -225,50 +167,3 @@ async fn main(spawner: Spawner) {
225 } 167 }
226 } 168 }
227} 169}
228
229static TX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new();
230static RX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new();
231static LINK_UP: AtomicBool = AtomicBool::new(false);
232
233struct Device {
234 mac_addr: [u8; 6],
235}
236
237impl embassy_net::Device for Device {
238 fn register_waker(&mut self, waker: &Waker) {
239 // loopy loopy wakey wakey
240 waker.wake_by_ref()
241 }
242
243 fn link_state(&mut self) -> embassy_net::LinkState {
244 match LINK_UP.load(Ordering::Relaxed) {
245 true => embassy_net::LinkState::Up,
246 false => embassy_net::LinkState::Down,
247 }
248 }
249
250 fn capabilities(&self) -> embassy_net::DeviceCapabilities {
251 let mut caps = embassy_net::DeviceCapabilities::default();
252 caps.max_transmission_unit = 1514; // 1500 IP + 14 ethernet header
253 caps.medium = embassy_net::Medium::Ethernet;
254 caps
255 }
256
257 fn is_transmit_ready(&mut self) -> bool {
258 true
259 }
260
261 fn transmit(&mut self, pkt: PacketBuf) {
262 if TX_CHANNEL.try_send(pkt).is_err() {
263 warn!("TX failed")
264 }
265 }
266
267 fn receive<'a>(&mut self) -> Option<PacketBuf> {
268 RX_CHANNEL.try_recv().ok()
269 }
270
271 fn ethernet_address(&self) -> [u8; 6] {
272 self.mac_addr
273 }
274}
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml
index dfbc26426..31a08cfb6 100644
--- a/examples/rp/Cargo.toml
+++ b/examples/rp/Cargo.toml
@@ -10,7 +10,7 @@ embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["de
10embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } 10embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] }
11embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } 11embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] }
12embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "pio", "critical-section-impl"] } 12embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "pio", "critical-section-impl"] }
13embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } 13embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt", "embassy-net"] }
14embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } 14embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] }
15embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } 15embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
16embassy-usb-logger = { version = "0.1.0", path = "../../embassy-usb-logger" } 16embassy-usb-logger = { version = "0.1.0", path = "../../embassy-usb-logger" }
diff --git a/examples/rp/src/bin/usb_ethernet.rs b/examples/rp/src/bin/usb_ethernet.rs
index 1057fe7fd..d0aec874a 100644
--- a/examples/rp/src/bin/usb_ethernet.rs
+++ b/examples/rp/src/bin/usb_ethernet.rs
@@ -2,18 +2,14 @@
2#![no_main] 2#![no_main]
3#![feature(type_alias_impl_trait)] 3#![feature(type_alias_impl_trait)]
4 4
5use core::sync::atomic::{AtomicBool, Ordering};
6use core::task::Waker;
7
8use defmt::*; 5use defmt::*;
9use embassy_executor::Spawner; 6use embassy_executor::Spawner;
10use embassy_net::tcp::TcpSocket; 7use embassy_net::tcp::TcpSocket;
11use embassy_net::{PacketBox, PacketBoxExt, PacketBuf, Stack, StackResources}; 8use embassy_net::{Stack, StackResources};
12use embassy_rp::usb::Driver; 9use embassy_rp::usb::Driver;
13use embassy_rp::{interrupt, peripherals}; 10use embassy_rp::{interrupt, peripherals};
14use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; 11use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState};
15use embassy_sync::channel::Channel; 12use embassy_usb::class::cdc_ncm::{CdcNcmClass, State};
16use embassy_usb::class::cdc_ncm::{CdcNcmClass, Receiver, Sender, State};
17use embassy_usb::{Builder, Config, UsbDevice}; 13use embassy_usb::{Builder, Config, UsbDevice};
18use embedded_io::asynch::Write; 14use embedded_io::asynch::Write;
19use static_cell::StaticCell; 15use static_cell::StaticCell;
@@ -25,56 +21,25 @@ macro_rules! singleton {
25 ($val:expr) => {{ 21 ($val:expr) => {{
26 type T = impl Sized; 22 type T = impl Sized;
27 static STATIC_CELL: StaticCell<T> = StaticCell::new(); 23 static STATIC_CELL: StaticCell<T> = StaticCell::new();
28 STATIC_CELL.init_with(move || $val) 24 let (x,) = STATIC_CELL.init(($val,));
25 x
29 }}; 26 }};
30} 27}
31 28
29const MTU: usize = 1514;
30
32#[embassy_executor::task] 31#[embassy_executor::task]
33async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! { 32async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! {
34 device.run().await 33 device.run().await
35} 34}
36 35
37#[embassy_executor::task] 36#[embassy_executor::task]
38async fn usb_ncm_rx_task(mut class: Receiver<'static, MyDriver>) { 37async fn usb_ncm_task(class: Runner<'static, MyDriver, MTU>) -> ! {
39 loop { 38 class.run().await
40 warn!("WAITING for connection");
41 LINK_UP.store(false, Ordering::Relaxed);
42
43 class.wait_connection().await.unwrap();
44
45 warn!("Connected");
46 LINK_UP.store(true, Ordering::Relaxed);
47
48 loop {
49 let mut p = unwrap!(PacketBox::new(embassy_net::Packet::new()));
50 let n = match class.read_packet(&mut p[..]).await {
51 Ok(n) => n,
52 Err(e) => {
53 warn!("error reading packet: {:?}", e);
54 break;
55 }
56 };
57
58 let buf = p.slice(0..n);
59 if RX_CHANNEL.try_send(buf).is_err() {
60 warn!("Failed pushing rx'd packet to channel.");
61 }
62 }
63 }
64}
65
66#[embassy_executor::task]
67async fn usb_ncm_tx_task(mut class: Sender<'static, MyDriver>) {
68 loop {
69 let pkt = TX_CHANNEL.recv().await;
70 if let Err(e) = class.write_packet(&pkt[..]).await {
71 warn!("Failed to TX packet: {:?}", e);
72 }
73 }
74} 39}
75 40
76#[embassy_executor::task] 41#[embassy_executor::task]
77async fn net_task(stack: &'static Stack<Device>) -> ! { 42async fn net_task(stack: &'static Stack<Device<'static, MTU>>) -> ! {
78 stack.run().await 43 stack.run().await
79} 44}
80 45
@@ -100,55 +65,32 @@ async fn main(spawner: Spawner) {
100 config.device_sub_class = 0x02; 65 config.device_sub_class = 0x02;
101 config.device_protocol = 0x01; 66 config.device_protocol = 0x01;
102 67
103 struct Resources {
104 device_descriptor: [u8; 256],
105 config_descriptor: [u8; 256],
106 bos_descriptor: [u8; 256],
107 control_buf: [u8; 128],
108 serial_state: State<'static>,
109 }
110 let res: &mut Resources = singleton!(Resources {
111 device_descriptor: [0; 256],
112 config_descriptor: [0; 256],
113 bos_descriptor: [0; 256],
114 control_buf: [0; 128],
115 serial_state: State::new(),
116 });
117
118 // Create embassy-usb DeviceBuilder using the driver and config. 68 // Create embassy-usb DeviceBuilder using the driver and config.
119 let mut builder = Builder::new( 69 let mut builder = Builder::new(
120 driver, 70 driver,
121 config, 71 config,
122 &mut res.device_descriptor, 72 &mut singleton!([0; 256])[..],
123 &mut res.config_descriptor, 73 &mut singleton!([0; 256])[..],
124 &mut res.bos_descriptor, 74 &mut singleton!([0; 256])[..],
125 &mut res.control_buf, 75 &mut singleton!([0; 128])[..],
126 None, 76 None,
127 ); 77 );
128 78
129 // WARNINGS for Android ethernet tethering:
130 // - On Pixel 4a, it refused to work on Android 11, worked on Android 12.
131 // - if the host's MAC address has the "locally-administered" bit set (bit 1 of first byte),
132 // it doesn't work! The "Ethernet tethering" option in settings doesn't get enabled.
133 // This is due to regex spaghetti: https://android.googlesource.com/platform/frameworks/base/+/refs/tags/android-mainline-12.0.0_r84/core/res/res/values/config.xml#417
134 // and this nonsense in the linux kernel: https://github.com/torvalds/linux/blob/c00c5e1d157bec0ef0b0b59aa5482eb8dc7e8e49/drivers/net/usb/usbnet.c#L1751-L1757
135
136 // Our MAC addr. 79 // Our MAC addr.
137 let our_mac_addr = [0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC]; 80 let our_mac_addr = [0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC];
138 // Host's MAC addr. This is the MAC the host "thinks" its USB-to-ethernet adapter has. 81 // Host's MAC addr. This is the MAC the host "thinks" its USB-to-ethernet adapter has.
139 let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88]; 82 let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88];
140 83
141 // Create classes on the builder. 84 // Create classes on the builder.
142 let class = CdcNcmClass::new(&mut builder, &mut res.serial_state, host_mac_addr, 64); 85 let class = CdcNcmClass::new(&mut builder, singleton!(State::new()), host_mac_addr, 64);
143 86
144 // Build the builder. 87 // Build the builder.
145 let usb = builder.build(); 88 let usb = builder.build();
146 89
147 unwrap!(spawner.spawn(usb_task(usb))); 90 unwrap!(spawner.spawn(usb_task(usb)));
148 91
149 let (tx, rx) = class.split(); 92 let (runner, device) = class.into_embassy_net_device::<MTU, 4, 4>(singleton!(NetState::new()), our_mac_addr);
150 unwrap!(spawner.spawn(usb_ncm_rx_task(rx))); 93 unwrap!(spawner.spawn(usb_ncm_task(runner)));
151 unwrap!(spawner.spawn(usb_ncm_tx_task(tx)));
152 94
153 let config = embassy_net::ConfigStrategy::Dhcp; 95 let config = embassy_net::ConfigStrategy::Dhcp;
154 //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { 96 //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config {
@@ -161,7 +103,6 @@ async fn main(spawner: Spawner) {
161 let seed = 1234; // guaranteed random, chosen by a fair dice roll 103 let seed = 1234; // guaranteed random, chosen by a fair dice roll
162 104
163 // Init network stack 105 // Init network stack
164 let device = Device { mac_addr: our_mac_addr };
165 let stack = &*singleton!(Stack::new( 106 let stack = &*singleton!(Stack::new(
166 device, 107 device,
167 config, 108 config,
@@ -214,50 +155,3 @@ async fn main(spawner: Spawner) {
214 } 155 }
215 } 156 }
216} 157}
217
218static TX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new();
219static RX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new();
220static LINK_UP: AtomicBool = AtomicBool::new(false);
221
222struct Device {
223 mac_addr: [u8; 6],
224}
225
226impl embassy_net::Device for Device {
227 fn register_waker(&mut self, waker: &Waker) {
228 // loopy loopy wakey wakey
229 waker.wake_by_ref()
230 }
231
232 fn link_state(&mut self) -> embassy_net::LinkState {
233 match LINK_UP.load(Ordering::Relaxed) {
234 true => embassy_net::LinkState::Up,
235 false => embassy_net::LinkState::Down,
236 }
237 }
238
239 fn capabilities(&self) -> embassy_net::DeviceCapabilities {
240 let mut caps = embassy_net::DeviceCapabilities::default();
241 caps.max_transmission_unit = 1514; // 1500 IP + 14 ethernet header
242 caps.medium = embassy_net::Medium::Ethernet;
243 caps
244 }
245
246 fn is_transmit_ready(&mut self) -> bool {
247 true
248 }
249
250 fn transmit(&mut self, pkt: PacketBuf) {
251 if TX_CHANNEL.try_send(pkt).is_err() {
252 warn!("TX failed")
253 }
254 }
255
256 fn receive<'a>(&mut self) -> Option<PacketBuf> {
257 RX_CHANNEL.try_recv().ok()
258 }
259
260 fn ethernet_address(&self) -> [u8; 6] {
261 self.mac_addr
262 }
263}
diff --git a/examples/std/src/tuntap.rs b/examples/std/src/tuntap.rs
index a0cace7f7..bb3e194cc 100644
--- a/examples/std/src/tuntap.rs
+++ b/examples/std/src/tuntap.rs
@@ -1,8 +1,10 @@
1use std::io; 1use std::io;
2use std::io::{Read, Write}; 2use std::io::{Read, Write};
3use std::os::unix::io::{AsRawFd, RawFd}; 3use std::os::unix::io::{AsRawFd, RawFd};
4use std::task::Context;
4 5
5use async_io::Async; 6use async_io::Async;
7use embassy_net::device::{self, Device, DeviceCapabilities, LinkState};
6use log::*; 8use log::*;
7 9
8pub const SIOCGIFMTU: libc::c_ulong = 0x8921; 10pub const SIOCGIFMTU: libc::c_ulong = 0x8921;
@@ -125,54 +127,35 @@ impl io::Write for TunTap {
125 127
126pub struct TunTapDevice { 128pub struct TunTapDevice {
127 device: Async<TunTap>, 129 device: Async<TunTap>,
128 waker: Option<Waker>,
129} 130}
130 131
131impl TunTapDevice { 132impl TunTapDevice {
132 pub fn new(name: &str) -> io::Result<TunTapDevice> { 133 pub fn new(name: &str) -> io::Result<TunTapDevice> {
133 Ok(Self { 134 Ok(Self {
134 device: Async::new(TunTap::new(name)?)?, 135 device: Async::new(TunTap::new(name)?)?,
135 waker: None,
136 }) 136 })
137 } 137 }
138} 138}
139 139
140use core::task::Waker;
141use std::task::Context;
142
143use embassy_net::{Device, DeviceCapabilities, LinkState, Packet, PacketBox, PacketBoxExt, PacketBuf};
144
145impl Device for TunTapDevice { 140impl Device for TunTapDevice {
146 fn is_transmit_ready(&mut self) -> bool { 141 type RxToken<'a> = RxToken where Self: 'a;
147 true 142 type TxToken<'a> = TxToken<'a> where Self: 'a;
148 }
149 143
150 fn transmit(&mut self, pkt: PacketBuf) { 144 fn receive(&mut self, cx: &mut Context) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
151 // todo handle WouldBlock 145 let mut buf = vec![0; self.device.get_ref().mtu];
152 match self.device.get_mut().write(&pkt) {
153 Ok(_) => {}
154 Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
155 info!("transmit WouldBlock");
156 }
157 Err(e) => panic!("transmit error: {:?}", e),
158 }
159 }
160
161 fn receive(&mut self) -> Option<PacketBuf> {
162 let mut pkt = PacketBox::new(Packet::new()).unwrap();
163 loop { 146 loop {
164 match self.device.get_mut().read(&mut pkt[..]) { 147 match self.device.get_mut().read(&mut buf) {
165 Ok(n) => { 148 Ok(n) => {
166 return Some(pkt.slice(0..n)); 149 buf.truncate(n);
150 return Some((
151 RxToken { buffer: buf },
152 TxToken {
153 device: &mut self.device,
154 },
155 ));
167 } 156 }
168 Err(e) if e.kind() == io::ErrorKind::WouldBlock => { 157 Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
169 let ready = if let Some(w) = self.waker.as_ref() { 158 if !self.device.poll_readable(cx).is_ready() {
170 let mut cx = Context::from_waker(w);
171 self.device.poll_readable(&mut cx).is_ready()
172 } else {
173 false
174 };
175 if !ready {
176 return None; 159 return None;
177 } 160 }
178 } 161 }
@@ -181,28 +164,10 @@ impl Device for TunTapDevice {
181 } 164 }
182 } 165 }
183 166
184 fn register_waker(&mut self, w: &Waker) { 167 fn transmit(&mut self, _cx: &mut Context) -> Option<Self::TxToken<'_>> {
185 match self.waker { 168 Some(TxToken {
186 // Optimization: If both the old and new Wakers wake the same task, we can simply 169 device: &mut self.device,
187 // keep the old waker, skipping the clone. (In most executor implementations, 170 })
188 // cloning a waker is somewhat expensive, comparable to cloning an Arc).
189 Some(ref w2) if (w2.will_wake(w)) => {}
190 _ => {
191 // clone the new waker and store it
192 if let Some(old_waker) = core::mem::replace(&mut self.waker, Some(w.clone())) {
193 // We had a waker registered for another task. Wake it, so the other task can
194 // reregister itself if it's still interested.
195 //
196 // If two tasks are waiting on the same thing concurrently, this will cause them
197 // to wake each other in a loop fighting over this WakerRegistration. This wastes
198 // CPU but things will still work.
199 //
200 // If the user wants to have two tasks waiting on the same thing they should use
201 // a more appropriate primitive that can store multiple wakers.
202 old_waker.wake()
203 }
204 }
205 }
206 } 171 }
207 172
208 fn capabilities(&self) -> DeviceCapabilities { 173 fn capabilities(&self) -> DeviceCapabilities {
@@ -211,7 +176,7 @@ impl Device for TunTapDevice {
211 caps 176 caps
212 } 177 }
213 178
214 fn link_state(&mut self) -> LinkState { 179 fn link_state(&mut self, _cx: &mut Context) -> LinkState {
215 LinkState::Up 180 LinkState::Up
216 } 181 }
217 182
@@ -219,3 +184,41 @@ impl Device for TunTapDevice {
219 [0x02, 0x03, 0x04, 0x05, 0x06, 0x07] 184 [0x02, 0x03, 0x04, 0x05, 0x06, 0x07]
220 } 185 }
221} 186}
187
188#[doc(hidden)]
189pub struct RxToken {
190 buffer: Vec<u8>,
191}
192
193impl device::RxToken for RxToken {
194 fn consume<R, F>(mut self, f: F) -> R
195 where
196 F: FnOnce(&mut [u8]) -> R,
197 {
198 f(&mut self.buffer)
199 }
200}
201
202#[doc(hidden)]
203pub struct TxToken<'a> {
204 device: &'a mut Async<TunTap>,
205}
206
207impl<'a> device::TxToken for TxToken<'a> {
208 fn consume<R, F>(self, len: usize, f: F) -> R
209 where
210 F: FnOnce(&mut [u8]) -> R,
211 {
212 let mut buffer = vec![0; len];
213 let result = f(&mut buffer);
214
215 // todo handle WouldBlock with async
216 match self.device.get_mut().write(&buffer) {
217 Ok(_) => {}
218 Err(e) if e.kind() == io::ErrorKind::WouldBlock => info!("transmit WouldBlock"),
219 Err(e) => panic!("transmit error: {:?}", e),
220 }
221
222 result
223 }
224}
diff --git a/examples/stm32f7/Cargo.toml b/examples/stm32f7/Cargo.toml
index f4d674cdc..afdf87000 100644
--- a/examples/stm32f7/Cargo.toml
+++ b/examples/stm32f7/Cargo.toml
@@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
8embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["defmt"] } 8embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["defmt"] }
9embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } 9embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] }
10embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } 10embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
11embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "net", "stm32f767zi", "unstable-pac", "time-driver-any", "exti"] } 11embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "embassy-net", "stm32f767zi", "unstable-pac", "time-driver-any", "exti"] }
12embassy-net = { path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } 12embassy-net = { path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] }
13embedded-io = { version = "0.4.0", features = ["async"] } 13embedded-io = { version = "0.4.0", features = ["async"] }
14 14
diff --git a/examples/stm32f7/src/bin/eth.rs b/examples/stm32f7/src/bin/eth.rs
index 5202edf62..224cc202b 100644
--- a/examples/stm32f7/src/bin/eth.rs
+++ b/examples/stm32f7/src/bin/eth.rs
@@ -7,7 +7,7 @@ use embassy_executor::Spawner;
7use embassy_net::tcp::TcpSocket; 7use embassy_net::tcp::TcpSocket;
8use embassy_net::{Ipv4Address, Stack, StackResources}; 8use embassy_net::{Ipv4Address, Stack, StackResources};
9use embassy_stm32::eth::generic_smi::GenericSMI; 9use embassy_stm32::eth::generic_smi::GenericSMI;
10use embassy_stm32::eth::{Ethernet, State}; 10use embassy_stm32::eth::{Ethernet, PacketQueue};
11use embassy_stm32::peripherals::ETH; 11use embassy_stm32::peripherals::ETH;
12use embassy_stm32::rng::Rng; 12use embassy_stm32::rng::Rng;
13use embassy_stm32::time::mhz; 13use embassy_stm32::time::mhz;
@@ -22,11 +22,12 @@ macro_rules! singleton {
22 ($val:expr) => {{ 22 ($val:expr) => {{
23 type T = impl Sized; 23 type T = impl Sized;
24 static STATIC_CELL: StaticCell<T> = StaticCell::new(); 24 static STATIC_CELL: StaticCell<T> = StaticCell::new();
25 STATIC_CELL.init_with(move || $val) 25 let (x,) = STATIC_CELL.init(($val,));
26 x
26 }}; 27 }};
27} 28}
28 29
29type Device = Ethernet<'static, ETH, GenericSMI, 4, 4>; 30type Device = Ethernet<'static, ETH, GenericSMI>;
30 31
31#[embassy_executor::task] 32#[embassy_executor::task]
32async fn net_task(stack: &'static Stack<Device>) -> ! { 33async fn net_task(stack: &'static Stack<Device>) -> ! {
@@ -50,25 +51,23 @@ async fn main(spawner: Spawner) -> ! {
50 let eth_int = interrupt::take!(ETH); 51 let eth_int = interrupt::take!(ETH);
51 let mac_addr = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF]; 52 let mac_addr = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF];
52 53
53 let device = unsafe { 54 let device = Ethernet::new(
54 Ethernet::new( 55 singleton!(PacketQueue::<16, 16>::new()),
55 singleton!(State::new()), 56 p.ETH,
56 p.ETH, 57 eth_int,
57 eth_int, 58 p.PA1,
58 p.PA1, 59 p.PA2,
59 p.PA2, 60 p.PC1,
60 p.PC1, 61 p.PA7,
61 p.PA7, 62 p.PC4,
62 p.PC4, 63 p.PC5,
63 p.PC5, 64 p.PG13,
64 p.PG13, 65 p.PB13,
65 p.PB13, 66 p.PG11,
66 p.PG11, 67 GenericSMI,
67 GenericSMI, 68 mac_addr,
68 mac_addr, 69 0,
69 0, 70 );
70 )
71 };
72 71
73 let config = embassy_net::ConfigStrategy::Dhcp; 72 let config = embassy_net::ConfigStrategy::Dhcp;
74 //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { 73 //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config {
@@ -91,8 +90,8 @@ async fn main(spawner: Spawner) -> ! {
91 info!("Network task initialized"); 90 info!("Network task initialized");
92 91
93 // Then we can use it! 92 // Then we can use it!
94 let mut rx_buffer = [0; 1024]; 93 let mut rx_buffer = [0; 4096];
95 let mut tx_buffer = [0; 1024]; 94 let mut tx_buffer = [0; 4096];
96 95
97 loop { 96 loop {
98 let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer); 97 let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer);
@@ -107,8 +106,9 @@ async fn main(spawner: Spawner) -> ! {
107 continue; 106 continue;
108 } 107 }
109 info!("connected!"); 108 info!("connected!");
109 let buf = [0; 1024];
110 loop { 110 loop {
111 let r = socket.write_all(b"Hello\n").await; 111 let r = socket.write_all(&buf).await;
112 if let Err(e) = r { 112 if let Err(e) = r {
113 info!("write error: {:?}", e); 113 info!("write error: {:?}", e);
114 return; 114 return;
diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml
index 11ce35053..5acf0035d 100644
--- a/examples/stm32h7/Cargo.toml
+++ b/examples/stm32h7/Cargo.toml
@@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
8embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["defmt"] } 8embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["defmt"] }
9embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } 9embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] }
10embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] } 10embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] }
11embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h743bi", "net", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] } 11embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h743bi", "embassy-net", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] }
12embassy-net = { path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16", "unstable-traits"] } 12embassy-net = { path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16", "unstable-traits"] }
13embedded-io = { version = "0.4.0", features = ["async"] } 13embedded-io = { version = "0.4.0", features = ["async"] }
14 14
diff --git a/examples/stm32h7/src/bin/eth.rs b/examples/stm32h7/src/bin/eth.rs
index 4ccc0b5ef..551325ca4 100644
--- a/examples/stm32h7/src/bin/eth.rs
+++ b/examples/stm32h7/src/bin/eth.rs
@@ -7,7 +7,7 @@ use embassy_executor::Spawner;
7use embassy_net::tcp::TcpSocket; 7use embassy_net::tcp::TcpSocket;
8use embassy_net::{Ipv4Address, Stack, StackResources}; 8use embassy_net::{Ipv4Address, Stack, StackResources};
9use embassy_stm32::eth::generic_smi::GenericSMI; 9use embassy_stm32::eth::generic_smi::GenericSMI;
10use embassy_stm32::eth::{Ethernet, State}; 10use embassy_stm32::eth::{Ethernet, PacketQueue};
11use embassy_stm32::peripherals::ETH; 11use embassy_stm32::peripherals::ETH;
12use embassy_stm32::rng::Rng; 12use embassy_stm32::rng::Rng;
13use embassy_stm32::time::mhz; 13use embassy_stm32::time::mhz;
@@ -22,11 +22,12 @@ macro_rules! singleton {
22 ($val:expr) => {{ 22 ($val:expr) => {{
23 type T = impl Sized; 23 type T = impl Sized;
24 static STATIC_CELL: StaticCell<T> = StaticCell::new(); 24 static STATIC_CELL: StaticCell<T> = StaticCell::new();
25 STATIC_CELL.init_with(move || $val) 25 let (x,) = STATIC_CELL.init(($val,));
26 x
26 }}; 27 }};
27} 28}
28 29
29type Device = Ethernet<'static, ETH, GenericSMI, 4, 4>; 30type Device = Ethernet<'static, ETH, GenericSMI>;
30 31
31#[embassy_executor::task] 32#[embassy_executor::task]
32async fn net_task(stack: &'static Stack<Device>) -> ! { 33async fn net_task(stack: &'static Stack<Device>) -> ! {
@@ -51,25 +52,23 @@ async fn main(spawner: Spawner) -> ! {
51 let eth_int = interrupt::take!(ETH); 52 let eth_int = interrupt::take!(ETH);
52 let mac_addr = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF]; 53 let mac_addr = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF];
53 54
54 let device = unsafe { 55 let device = Ethernet::new(
55 Ethernet::new( 56 singleton!(PacketQueue::<16, 16>::new()),
56 singleton!(State::new()), 57 p.ETH,
57 p.ETH, 58 eth_int,
58 eth_int, 59 p.PA1,
59 p.PA1, 60 p.PA2,
60 p.PA2, 61 p.PC1,
61 p.PC1, 62 p.PA7,
62 p.PA7, 63 p.PC4,
63 p.PC4, 64 p.PC5,
64 p.PC5, 65 p.PG13,
65 p.PG13, 66 p.PB13,
66 p.PB13, 67 p.PG11,
67 p.PG11, 68 GenericSMI,
68 GenericSMI, 69 mac_addr,
69 mac_addr, 70 0,
70 0, 71 );
71 )
72 };
73 72
74 let config = embassy_net::ConfigStrategy::Dhcp; 73 let config = embassy_net::ConfigStrategy::Dhcp;
75 //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { 74 //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config {
diff --git a/examples/stm32h7/src/bin/eth_client.rs b/examples/stm32h7/src/bin/eth_client.rs
index 64fd84141..61a08ae10 100644
--- a/examples/stm32h7/src/bin/eth_client.rs
+++ b/examples/stm32h7/src/bin/eth_client.rs
@@ -7,7 +7,7 @@ use embassy_executor::Spawner;
7use embassy_net::tcp::client::{TcpClient, TcpClientState}; 7use embassy_net::tcp::client::{TcpClient, TcpClientState};
8use embassy_net::{Stack, StackResources}; 8use embassy_net::{Stack, StackResources};
9use embassy_stm32::eth::generic_smi::GenericSMI; 9use embassy_stm32::eth::generic_smi::GenericSMI;
10use embassy_stm32::eth::{Ethernet, State}; 10use embassy_stm32::eth::{Ethernet, PacketQueue};
11use embassy_stm32::peripherals::ETH; 11use embassy_stm32::peripherals::ETH;
12use embassy_stm32::rng::Rng; 12use embassy_stm32::rng::Rng;
13use embassy_stm32::time::mhz; 13use embassy_stm32::time::mhz;
@@ -23,11 +23,12 @@ macro_rules! singleton {
23 ($val:expr) => {{ 23 ($val:expr) => {{
24 type T = impl Sized; 24 type T = impl Sized;
25 static STATIC_CELL: StaticCell<T> = StaticCell::new(); 25 static STATIC_CELL: StaticCell<T> = StaticCell::new();
26 STATIC_CELL.init_with(move || $val) 26 let (x,) = STATIC_CELL.init(($val,));
27 x
27 }}; 28 }};
28} 29}
29 30
30type Device = Ethernet<'static, ETH, GenericSMI, 4, 4>; 31type Device = Ethernet<'static, ETH, GenericSMI>;
31 32
32#[embassy_executor::task] 33#[embassy_executor::task]
33async fn net_task(stack: &'static Stack<Device>) -> ! { 34async fn net_task(stack: &'static Stack<Device>) -> ! {
@@ -52,25 +53,23 @@ async fn main(spawner: Spawner) -> ! {
52 let eth_int = interrupt::take!(ETH); 53 let eth_int = interrupt::take!(ETH);
53 let mac_addr = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF]; 54 let mac_addr = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF];
54 55
55 let device = unsafe { 56 let device = Ethernet::new(
56 Ethernet::new( 57 singleton!(PacketQueue::<16, 16>::new()),
57 singleton!(State::new()), 58 p.ETH,
58 p.ETH, 59 eth_int,
59 eth_int, 60 p.PA1,
60 p.PA1, 61 p.PA2,
61 p.PA2, 62 p.PC1,
62 p.PC1, 63 p.PA7,
63 p.PA7, 64 p.PC4,
64 p.PC4, 65 p.PC5,
65 p.PC5, 66 p.PG13,
66 p.PG13, 67 p.PB13,
67 p.PB13, 68 p.PG11,
68 p.PG11, 69 GenericSMI,
69 GenericSMI, 70 mac_addr,
70 mac_addr, 71 0,
71 0, 72 );
72 )
73 };
74 73
75 let config = embassy_net::ConfigStrategy::Dhcp; 74 let config = embassy_net::ConfigStrategy::Dhcp;
76 //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { 75 //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config {
diff --git a/examples/stm32l5/Cargo.toml b/examples/stm32l5/Cargo.toml
index 73ad50787..bb8515312 100644
--- a/examples/stm32l5/Cargo.toml
+++ b/examples/stm32l5/Cargo.toml
@@ -11,7 +11,7 @@ embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["de
11embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } 11embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] }
12embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } 12embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
13embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l552ze", "time-driver-any", "exti", "unstable-traits", "memory-x"] } 13embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l552ze", "time-driver-any", "exti", "unstable-traits", "memory-x"] }
14embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } 14embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt", "embassy-net"] }
15embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } 15embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] }
16embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } 16embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
17usbd-hid = "0.6.0" 17usbd-hid = "0.6.0"
diff --git a/examples/stm32l5/src/bin/usb_ethernet.rs b/examples/stm32l5/src/bin/usb_ethernet.rs
index 4f36d3f5a..b49329ea4 100644
--- a/examples/stm32l5/src/bin/usb_ethernet.rs
+++ b/examples/stm32l5/src/bin/usb_ethernet.rs
@@ -2,20 +2,16 @@
2#![no_main] 2#![no_main]
3#![feature(type_alias_impl_trait)] 3#![feature(type_alias_impl_trait)]
4 4
5use core::sync::atomic::{AtomicBool, Ordering};
6use core::task::Waker;
7
8use defmt::*; 5use defmt::*;
9use embassy_executor::Spawner; 6use embassy_executor::Spawner;
10use embassy_net::tcp::TcpSocket; 7use embassy_net::tcp::TcpSocket;
11use embassy_net::{PacketBox, PacketBoxExt, PacketBuf, Stack, StackResources}; 8use embassy_net::{Stack, StackResources};
12use embassy_stm32::rcc::*; 9use embassy_stm32::rcc::*;
13use embassy_stm32::rng::Rng; 10use embassy_stm32::rng::Rng;
14use embassy_stm32::usb::Driver; 11use embassy_stm32::usb::Driver;
15use embassy_stm32::{interrupt, Config}; 12use embassy_stm32::{interrupt, Config};
16use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; 13use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState};
17use embassy_sync::channel::Channel; 14use embassy_usb::class::cdc_ncm::{CdcNcmClass, State};
18use embassy_usb::class::cdc_ncm::{CdcNcmClass, Receiver, Sender, State};
19use embassy_usb::{Builder, UsbDevice}; 15use embassy_usb::{Builder, UsbDevice};
20use embedded_io::asynch::Write; 16use embedded_io::asynch::Write;
21use rand_core::RngCore; 17use rand_core::RngCore;
@@ -28,56 +24,25 @@ macro_rules! singleton {
28 ($val:expr) => {{ 24 ($val:expr) => {{
29 type T = impl Sized; 25 type T = impl Sized;
30 static STATIC_CELL: StaticCell<T> = StaticCell::new(); 26 static STATIC_CELL: StaticCell<T> = StaticCell::new();
31 STATIC_CELL.init_with(move || $val) 27 let (x,) = STATIC_CELL.init(($val,));
28 x
32 }}; 29 }};
33} 30}
34 31
32const MTU: usize = 1514;
33
35#[embassy_executor::task] 34#[embassy_executor::task]
36async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! { 35async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! {
37 device.run().await 36 device.run().await
38} 37}
39 38
40#[embassy_executor::task] 39#[embassy_executor::task]
41async fn usb_ncm_rx_task(mut class: Receiver<'static, MyDriver>) { 40async fn usb_ncm_task(class: Runner<'static, MyDriver, MTU>) -> ! {
42 loop { 41 class.run().await
43 warn!("WAITING for connection");
44 LINK_UP.store(false, Ordering::Relaxed);
45
46 class.wait_connection().await.unwrap();
47
48 warn!("Connected");
49 LINK_UP.store(true, Ordering::Relaxed);
50
51 loop {
52 let mut p = unwrap!(PacketBox::new(embassy_net::Packet::new()));
53 let n = match class.read_packet(&mut p[..]).await {
54 Ok(n) => n,
55 Err(e) => {
56 warn!("error reading packet: {:?}", e);
57 break;
58 }
59 };
60
61 let buf = p.slice(0..n);
62 if RX_CHANNEL.try_send(buf).is_err() {
63 warn!("Failed pushing rx'd packet to channel.");
64 }
65 }
66 }
67}
68
69#[embassy_executor::task]
70async fn usb_ncm_tx_task(mut class: Sender<'static, MyDriver>) {
71 loop {
72 let pkt = TX_CHANNEL.recv().await;
73 if let Err(e) = class.write_packet(&pkt[..]).await {
74 warn!("Failed to TX packet: {:?}", e);
75 }
76 }
77} 42}
78 43
79#[embassy_executor::task] 44#[embassy_executor::task]
80async fn net_task(stack: &'static Stack<Device>) -> ! { 45async fn net_task(stack: &'static Stack<Device<'static, MTU>>) -> ! {
81 stack.run().await 46 stack.run().await
82} 47}
83 48
@@ -106,55 +71,32 @@ async fn main(spawner: Spawner) {
106 config.device_sub_class = 0x02; 71 config.device_sub_class = 0x02;
107 config.device_protocol = 0x01; 72 config.device_protocol = 0x01;
108 73
109 struct Resources {
110 device_descriptor: [u8; 256],
111 config_descriptor: [u8; 256],
112 bos_descriptor: [u8; 256],
113 control_buf: [u8; 128],
114 serial_state: State<'static>,
115 }
116 let res: &mut Resources = singleton!(Resources {
117 device_descriptor: [0; 256],
118 config_descriptor: [0; 256],
119 bos_descriptor: [0; 256],
120 control_buf: [0; 128],
121 serial_state: State::new(),
122 });
123
124 // Create embassy-usb DeviceBuilder using the driver and config. 74 // Create embassy-usb DeviceBuilder using the driver and config.
125 let mut builder = Builder::new( 75 let mut builder = Builder::new(
126 driver, 76 driver,
127 config, 77 config,
128 &mut res.device_descriptor, 78 &mut singleton!([0; 256])[..],
129 &mut res.config_descriptor, 79 &mut singleton!([0; 256])[..],
130 &mut res.bos_descriptor, 80 &mut singleton!([0; 256])[..],
131 &mut res.control_buf, 81 &mut singleton!([0; 128])[..],
132 None, 82 None,
133 ); 83 );
134 84
135 // WARNINGS for Android ethernet tethering:
136 // - On Pixel 4a, it refused to work on Android 11, worked on Android 12.
137 // - if the host's MAC address has the "locally-administered" bit set (bit 1 of first byte),
138 // it doesn't work! The "Ethernet tethering" option in settings doesn't get enabled.
139 // This is due to regex spaghetti: https://android.googlesource.com/platform/frameworks/base/+/refs/tags/android-mainline-12.0.0_r84/core/res/res/values/config.xml#417
140 // and this nonsense in the linux kernel: https://github.com/torvalds/linux/blob/c00c5e1d157bec0ef0b0b59aa5482eb8dc7e8e49/drivers/net/usb/usbnet.c#L1751-L1757
141
142 // Our MAC addr. 85 // Our MAC addr.
143 let our_mac_addr = [0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC]; 86 let our_mac_addr = [0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC];
144 // Host's MAC addr. This is the MAC the host "thinks" its USB-to-ethernet adapter has. 87 // Host's MAC addr. This is the MAC the host "thinks" its USB-to-ethernet adapter has.
145 let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88]; 88 let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88];
146 89
147 // Create classes on the builder. 90 // Create classes on the builder.
148 let class = CdcNcmClass::new(&mut builder, &mut res.serial_state, host_mac_addr, 64); 91 let class = CdcNcmClass::new(&mut builder, singleton!(State::new()), host_mac_addr, 64);
149 92
150 // Build the builder. 93 // Build the builder.
151 let usb = builder.build(); 94 let usb = builder.build();
152 95
153 unwrap!(spawner.spawn(usb_task(usb))); 96 unwrap!(spawner.spawn(usb_task(usb)));
154 97
155 let (tx, rx) = class.split(); 98 let (runner, device) = class.into_embassy_net_device::<MTU, 4, 4>(singleton!(NetState::new()), our_mac_addr);
156 unwrap!(spawner.spawn(usb_ncm_rx_task(rx))); 99 unwrap!(spawner.spawn(usb_ncm_task(runner)));
157 unwrap!(spawner.spawn(usb_ncm_tx_task(tx)));
158 100
159 let config = embassy_net::ConfigStrategy::Dhcp; 101 let config = embassy_net::ConfigStrategy::Dhcp;
160 //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { 102 //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config {
@@ -168,7 +110,6 @@ async fn main(spawner: Spawner) {
168 let seed = rng.next_u64(); 110 let seed = rng.next_u64();
169 111
170 // Init network stack 112 // Init network stack
171 let device = Device { mac_addr: our_mac_addr };
172 let stack = &*singleton!(Stack::new( 113 let stack = &*singleton!(Stack::new(
173 device, 114 device,
174 config, 115 config,
@@ -221,50 +162,3 @@ async fn main(spawner: Spawner) {
221 } 162 }
222 } 163 }
223} 164}
224
225static TX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new();
226static RX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new();
227static LINK_UP: AtomicBool = AtomicBool::new(false);
228
229struct Device {
230 mac_addr: [u8; 6],
231}
232
233impl embassy_net::Device for Device {
234 fn register_waker(&mut self, waker: &Waker) {
235 // loopy loopy wakey wakey
236 waker.wake_by_ref()
237 }
238
239 fn link_state(&mut self) -> embassy_net::LinkState {
240 match LINK_UP.load(Ordering::Relaxed) {
241 true => embassy_net::LinkState::Up,
242 false => embassy_net::LinkState::Down,
243 }
244 }
245
246 fn capabilities(&self) -> embassy_net::DeviceCapabilities {
247 let mut caps = embassy_net::DeviceCapabilities::default();
248 caps.max_transmission_unit = 1514; // 1500 IP + 14 ethernet header
249 caps.medium = embassy_net::Medium::Ethernet;
250 caps
251 }
252
253 fn is_transmit_ready(&mut self) -> bool {
254 true
255 }
256
257 fn transmit(&mut self, pkt: PacketBuf) {
258 if TX_CHANNEL.try_send(pkt).is_err() {
259 warn!("TX failed")
260 }
261 }
262
263 fn receive<'a>(&mut self) -> Option<PacketBuf> {
264 RX_CHANNEL.try_recv().ok()
265 }
266
267 fn ethernet_address(&self) -> [u8; 6] {
268 self.mac_addr
269 }
270}