diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-05-23 03:50:43 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-05-25 19:56:22 +0200 |
| commit | a5aea995a802fea8fc1b3e4b5fe47bd6d1fca2a4 (patch) | |
| tree | 0fcb4c01914347eff5b3be44b284aa9432e28678 /examples/nrf/src | |
| parent | 36a1f203648dcb402727ea3eb5d30cf1f6993795 (diff) | |
WIP embassy-net v2
Diffstat (limited to 'examples/nrf/src')
| -rw-r--r-- | examples/nrf/src/bin/usb_ethernet.rs | 75 |
1 files changed, 40 insertions, 35 deletions
diff --git a/examples/nrf/src/bin/usb_ethernet.rs b/examples/nrf/src/bin/usb_ethernet.rs index 843487c03..49f2fb89a 100644 --- a/examples/nrf/src/bin/usb_ethernet.rs +++ b/examples/nrf/src/bin/usb_ethernet.rs | |||
| @@ -12,8 +12,9 @@ use embassy::channel::Channel; | |||
| 12 | use embassy::executor::Spawner; | 12 | use embassy::executor::Spawner; |
| 13 | use embassy::util::Forever; | 13 | use embassy::util::Forever; |
| 14 | use embassy_net::tcp::TcpSocket; | 14 | use embassy_net::tcp::TcpSocket; |
| 15 | use embassy_net::{PacketBox, PacketBoxExt, PacketBuf}; | 15 | use embassy_net::{PacketBox, PacketBoxExt, PacketBuf, Stack, StackResources}; |
| 16 | use embassy_nrf::pac; | 16 | use embassy_nrf::pac; |
| 17 | use embassy_nrf::rng::Rng; | ||
| 17 | use embassy_nrf::usb::Driver; | 18 | use embassy_nrf::usb::Driver; |
| 18 | use embassy_nrf::Peripherals; | 19 | use embassy_nrf::Peripherals; |
| 19 | use embassy_nrf::{interrupt, peripherals}; | 20 | use embassy_nrf::{interrupt, peripherals}; |
| @@ -27,6 +28,14 @@ use panic_probe as _; | |||
| 27 | 28 | ||
| 28 | type MyDriver = Driver<'static, peripherals::USBD>; | 29 | type MyDriver = Driver<'static, peripherals::USBD>; |
| 29 | 30 | ||
| 31 | macro_rules! forever { | ||
| 32 | ($val:expr) => {{ | ||
| 33 | type T = impl Sized; | ||
| 34 | static FOREVER: Forever<T> = Forever::new(); | ||
| 35 | FOREVER.put_with(move || $val) | ||
| 36 | }}; | ||
| 37 | } | ||
| 38 | |||
| 30 | #[embassy::task] | 39 | #[embassy::task] |
| 31 | async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! { | 40 | async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! { |
| 32 | device.run().await | 41 | device.run().await |
| @@ -72,8 +81,8 @@ async fn usb_ncm_tx_task(mut class: Sender<'static, MyDriver>) { | |||
| 72 | } | 81 | } |
| 73 | 82 | ||
| 74 | #[embassy::task] | 83 | #[embassy::task] |
| 75 | async fn net_task() -> ! { | 84 | async fn net_task(stack: &'static Stack<Device>) -> ! { |
| 76 | embassy_net::run().await | 85 | stack.run().await |
| 77 | } | 86 | } |
| 78 | 87 | ||
| 79 | #[embassy::main] | 88 | #[embassy::main] |
| @@ -114,8 +123,7 @@ async fn main(spawner: Spawner, p: Peripherals) { | |||
| 114 | control_buf: [u8; 128], | 123 | control_buf: [u8; 128], |
| 115 | serial_state: State<'static>, | 124 | serial_state: State<'static>, |
| 116 | } | 125 | } |
| 117 | static RESOURCES: Forever<Resources> = Forever::new(); | 126 | let res: &mut Resources = forever!(Resources { |
| 118 | let res = RESOURCES.put(Resources { | ||
| 119 | device_descriptor: [0; 256], | 127 | device_descriptor: [0; 256], |
| 120 | config_descriptor: [0; 256], | 128 | config_descriptor: [0; 256], |
| 121 | bos_descriptor: [0; 256], | 129 | bos_descriptor: [0; 256], |
| @@ -158,28 +166,31 @@ async fn main(spawner: Spawner, p: Peripherals) { | |||
| 158 | unwrap!(spawner.spawn(usb_ncm_rx_task(rx))); | 166 | unwrap!(spawner.spawn(usb_ncm_rx_task(rx))); |
| 159 | unwrap!(spawner.spawn(usb_ncm_tx_task(tx))); | 167 | unwrap!(spawner.spawn(usb_ncm_tx_task(tx))); |
| 160 | 168 | ||
| 161 | // Init embassy-net | 169 | let config = embassy_net::ConfigStrategy::Dhcp; |
| 162 | struct NetResources { | 170 | //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { |
| 163 | resources: embassy_net::StackResources<1, 2, 8>, | 171 | // address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24), |
| 164 | configurator: embassy_net::DhcpConfigurator, | 172 | // dns_servers: Vec::new(), |
| 165 | //configurator: StaticConfigurator, | 173 | // gateway: Some(Ipv4Address::new(10, 42, 0, 1)), |
| 166 | device: Device, | 174 | //}); |
| 167 | } | 175 | |
| 168 | static NET_RESOURCES: Forever<NetResources> = Forever::new(); | 176 | // Generate random seed |
| 169 | let res = NET_RESOURCES.put(NetResources { | 177 | let mut rng = Rng::new(p.RNG, interrupt::take!(RNG)); |
| 170 | resources: embassy_net::StackResources::new(), | 178 | let mut seed = [0; 8]; |
| 171 | configurator: embassy_net::DhcpConfigurator::new(), | 179 | rng.blocking_fill_bytes(&mut seed); |
| 172 | //configurator: embassy_net::StaticConfigurator::new(embassy_net::Config { | 180 | let seed = u64::from_le_bytes(seed); |
| 173 | // address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 1), 24), | 181 | |
| 174 | // dns_servers: Default::default(), | 182 | // Init network stack |
| 175 | // gateway: None, | 183 | let device = Device { |
| 176 | //}), | 184 | mac_addr: our_mac_addr, |
| 177 | device: Device { | 185 | }; |
| 178 | mac_addr: our_mac_addr, | 186 | let stack = &*forever!(Stack::new( |
| 179 | }, | 187 | device, |
| 180 | }); | 188 | config, |
| 181 | embassy_net::init(&mut res.device, &mut res.configurator, &mut res.resources); | 189 | forever!(StackResources::<1, 2, 8>::new()), |
| 182 | unwrap!(spawner.spawn(net_task())); | 190 | seed |
| 191 | )); | ||
| 192 | |||
| 193 | unwrap!(spawner.spawn(net_task(stack))); | ||
| 183 | 194 | ||
| 184 | // And now we can use it! | 195 | // And now we can use it! |
| 185 | 196 | ||
| @@ -188,7 +199,7 @@ async fn main(spawner: Spawner, p: Peripherals) { | |||
| 188 | let mut buf = [0; 4096]; | 199 | let mut buf = [0; 4096]; |
| 189 | 200 | ||
| 190 | loop { | 201 | loop { |
| 191 | let mut socket = TcpSocket::new(&mut rx_buffer, &mut tx_buffer); | 202 | let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); |
| 192 | socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); | 203 | socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); |
| 193 | 204 | ||
| 194 | info!("Listening on TCP:1234..."); | 205 | info!("Listening on TCP:1234..."); |
| @@ -246,7 +257,7 @@ impl embassy_net::Device for Device { | |||
| 246 | } | 257 | } |
| 247 | } | 258 | } |
| 248 | 259 | ||
| 249 | fn capabilities(&mut self) -> embassy_net::DeviceCapabilities { | 260 | fn capabilities(&self) -> embassy_net::DeviceCapabilities { |
| 250 | let mut caps = embassy_net::DeviceCapabilities::default(); | 261 | let mut caps = embassy_net::DeviceCapabilities::default(); |
| 251 | caps.max_transmission_unit = 1514; // 1500 IP + 14 ethernet header | 262 | caps.max_transmission_unit = 1514; // 1500 IP + 14 ethernet header |
| 252 | caps.medium = embassy_net::Medium::Ethernet; | 263 | caps.medium = embassy_net::Medium::Ethernet; |
| @@ -271,9 +282,3 @@ impl embassy_net::Device for Device { | |||
| 271 | self.mac_addr | 282 | self.mac_addr |
| 272 | } | 283 | } |
| 273 | } | 284 | } |
| 274 | |||
| 275 | #[no_mangle] | ||
| 276 | fn _embassy_rand(buf: &mut [u8]) { | ||
| 277 | // TODO | ||
| 278 | buf.fill(0x42) | ||
| 279 | } | ||
