diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-12-07 16:03:03 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-12-13 16:43:25 +0100 |
| commit | e9219405ca04e23b6543fb841fd97df54cf72f94 (patch) | |
| tree | 32d9129286949fdb887898adad1076ab352e95e9 /examples | |
| parent | aaaf5f23a8a3a0df1ad2186802e2afc061a74b72 (diff) | |
usb/cdc-ncm: add embassy-net Device implementation.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/nrf/src/bin/usb_ethernet.rs | 139 | ||||
| -rw-r--r-- | examples/rp/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/rp/src/bin/usb_ethernet.rs | 140 | ||||
| -rw-r--r-- | examples/stm32l5/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32l5/src/bin/usb_ethernet.rs | 140 |
6 files changed, 54 insertions, 371 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 | |||
| 16 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } | 16 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } |
| 17 | embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac"] } | 17 | embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac"] } |
| 18 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16"], optional = true } | 18 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16"], optional = true } |
| 19 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"], optional = true } | 19 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt", "embassy-net"], optional = true } |
| 20 | embedded-io = "0.4.0" | 20 | embedded-io = "0.4.0" |
| 21 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx126x", "time", "defmt"], optional = true } | 21 | embassy-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 | ||
| 5 | use core::mem; | 5 | use core::mem; |
| 6 | use core::sync::atomic::{AtomicBool, Ordering}; | ||
| 7 | use core::task::Waker; | ||
| 8 | 6 | ||
| 9 | use defmt::*; | 7 | use defmt::*; |
| 10 | use embassy_executor::Spawner; | 8 | use embassy_executor::Spawner; |
| 11 | use embassy_net::tcp::TcpSocket; | 9 | use embassy_net::tcp::TcpSocket; |
| 12 | use embassy_net::{PacketBox, PacketBoxExt, PacketBuf, Stack, StackResources}; | 10 | use embassy_net::{Stack, StackResources}; |
| 13 | use embassy_nrf::rng::Rng; | 11 | use embassy_nrf::rng::Rng; |
| 14 | use embassy_nrf::usb::{Driver, PowerUsb}; | 12 | use embassy_nrf::usb::{Driver, PowerUsb}; |
| 15 | use embassy_nrf::{interrupt, pac, peripherals}; | 13 | use embassy_nrf::{interrupt, pac, peripherals}; |
| 16 | use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; | 14 | use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState}; |
| 17 | use embassy_sync::channel::Channel; | 15 | use embassy_usb::class::cdc_ncm::{CdcNcmClass, State}; |
| 18 | use embassy_usb::class::cdc_ncm::{CdcNcmClass, Receiver, Sender, State}; | ||
| 19 | use embassy_usb::{Builder, Config, UsbDevice}; | 16 | use embassy_usb::{Builder, Config, UsbDevice}; |
| 20 | use embedded_io::asynch::Write; | 17 | use embedded_io::asynch::Write; |
| 21 | use static_cell::StaticCell; | 18 | use 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 | ||
| 32 | const MTU: usize = 1514; | ||
| 33 | |||
| 34 | #[embassy_executor::task] | 34 | #[embassy_executor::task] |
| 35 | async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! { | 35 | async 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] |
| 40 | async fn usb_ncm_rx_task(mut class: Receiver<'static, MyDriver>) { | 40 | async 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] | ||
| 69 | async 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] |
| 79 | async fn net_task(stack: &'static Stack<Device>) -> ! { | 45 | async 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 | |||
| 229 | static TX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new(); | ||
| 230 | static RX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new(); | ||
| 231 | static LINK_UP: AtomicBool = AtomicBool::new(false); | ||
| 232 | |||
| 233 | struct Device { | ||
| 234 | mac_addr: [u8; 6], | ||
| 235 | } | ||
| 236 | |||
| 237 | impl 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 | |||
| 10 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } | 10 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } |
| 11 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } | 11 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } |
| 12 | embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "pio", "critical-section-impl"] } | 12 | embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "pio", "critical-section-impl"] } |
| 13 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | 13 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt", "embassy-net"] } |
| 14 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } | 14 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } |
| 15 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } | 15 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } |
| 16 | embassy-usb-logger = { version = "0.1.0", path = "../../embassy-usb-logger" } | 16 | embassy-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 | ||
| 5 | use core::sync::atomic::{AtomicBool, Ordering}; | ||
| 6 | use core::task::Waker; | ||
| 7 | |||
| 8 | use defmt::*; | 5 | use defmt::*; |
| 9 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 10 | use embassy_net::tcp::TcpSocket; | 7 | use embassy_net::tcp::TcpSocket; |
| 11 | use embassy_net::{PacketBox, PacketBoxExt, PacketBuf, Stack, StackResources}; | 8 | use embassy_net::{Stack, StackResources}; |
| 12 | use embassy_rp::usb::Driver; | 9 | use embassy_rp::usb::Driver; |
| 13 | use embassy_rp::{interrupt, peripherals}; | 10 | use embassy_rp::{interrupt, peripherals}; |
| 14 | use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; | 11 | use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState}; |
| 15 | use embassy_sync::channel::Channel; | 12 | use embassy_usb::class::cdc_ncm::{CdcNcmClass, State}; |
| 16 | use embassy_usb::class::cdc_ncm::{CdcNcmClass, Receiver, Sender, State}; | ||
| 17 | use embassy_usb::{Builder, Config, UsbDevice}; | 13 | use embassy_usb::{Builder, Config, UsbDevice}; |
| 18 | use embedded_io::asynch::Write; | 14 | use embedded_io::asynch::Write; |
| 19 | use static_cell::StaticCell; | 15 | use 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 | ||
| 29 | const MTU: usize = 1514; | ||
| 30 | |||
| 32 | #[embassy_executor::task] | 31 | #[embassy_executor::task] |
| 33 | async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! { | 32 | async 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] |
| 38 | async fn usb_ncm_rx_task(mut class: Receiver<'static, MyDriver>) { | 37 | async 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] | ||
| 67 | async 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] |
| 77 | async fn net_task(stack: &'static Stack<Device>) -> ! { | 42 | async 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 | |||
| 218 | static TX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new(); | ||
| 219 | static RX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new(); | ||
| 220 | static LINK_UP: AtomicBool = AtomicBool::new(false); | ||
| 221 | |||
| 222 | struct Device { | ||
| 223 | mac_addr: [u8; 6], | ||
| 224 | } | ||
| 225 | |||
| 226 | impl 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/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 | |||
| 11 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } | 11 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } |
| 12 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 12 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 13 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l552ze", "time-driver-any", "exti", "unstable-traits", "memory-x"] } | 13 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l552ze", "time-driver-any", "exti", "unstable-traits", "memory-x"] } |
| 14 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | 14 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt", "embassy-net"] } |
| 15 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } | 15 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } |
| 16 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } | 16 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } |
| 17 | usbd-hid = "0.6.0" | 17 | usbd-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 | ||
| 5 | use core::sync::atomic::{AtomicBool, Ordering}; | ||
| 6 | use core::task::Waker; | ||
| 7 | |||
| 8 | use defmt::*; | 5 | use defmt::*; |
| 9 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 10 | use embassy_net::tcp::TcpSocket; | 7 | use embassy_net::tcp::TcpSocket; |
| 11 | use embassy_net::{PacketBox, PacketBoxExt, PacketBuf, Stack, StackResources}; | 8 | use embassy_net::{Stack, StackResources}; |
| 12 | use embassy_stm32::rcc::*; | 9 | use embassy_stm32::rcc::*; |
| 13 | use embassy_stm32::rng::Rng; | 10 | use embassy_stm32::rng::Rng; |
| 14 | use embassy_stm32::usb::Driver; | 11 | use embassy_stm32::usb::Driver; |
| 15 | use embassy_stm32::{interrupt, Config}; | 12 | use embassy_stm32::{interrupt, Config}; |
| 16 | use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; | 13 | use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState}; |
| 17 | use embassy_sync::channel::Channel; | 14 | use embassy_usb::class::cdc_ncm::{CdcNcmClass, State}; |
| 18 | use embassy_usb::class::cdc_ncm::{CdcNcmClass, Receiver, Sender, State}; | ||
| 19 | use embassy_usb::{Builder, UsbDevice}; | 15 | use embassy_usb::{Builder, UsbDevice}; |
| 20 | use embedded_io::asynch::Write; | 16 | use embedded_io::asynch::Write; |
| 21 | use rand_core::RngCore; | 17 | use 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 | ||
| 32 | const MTU: usize = 1514; | ||
| 33 | |||
| 35 | #[embassy_executor::task] | 34 | #[embassy_executor::task] |
| 36 | async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! { | 35 | async 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] |
| 41 | async fn usb_ncm_rx_task(mut class: Receiver<'static, MyDriver>) { | 40 | async 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] | ||
| 70 | async 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] |
| 80 | async fn net_task(stack: &'static Stack<Device>) -> ! { | 45 | async 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 | |||
| 225 | static TX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new(); | ||
| 226 | static RX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new(); | ||
| 227 | static LINK_UP: AtomicBool = AtomicBool::new(false); | ||
| 228 | |||
| 229 | struct Device { | ||
| 230 | mac_addr: [u8; 6], | ||
| 231 | } | ||
| 232 | |||
| 233 | impl 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 | } | ||
