aboutsummaryrefslogtreecommitdiff
path: root/examples/nrf
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-12-07 16:03:03 +0100
committerDario Nieuwenhuis <[email protected]>2022-12-13 16:43:25 +0100
commite9219405ca04e23b6543fb841fd97df54cf72f94 (patch)
tree32d9129286949fdb887898adad1076ab352e95e9 /examples/nrf
parentaaaf5f23a8a3a0df1ad2186802e2afc061a74b72 (diff)
usb/cdc-ncm: add embassy-net Device implementation.
Diffstat (limited to 'examples/nrf')
-rw-r--r--examples/nrf/Cargo.toml2
-rw-r--r--examples/nrf/src/bin/usb_ethernet.rs139
2 files changed, 18 insertions, 123 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}