diff options
| author | kalkyl <[email protected]> | 2023-05-09 01:51:08 +0200 |
|---|---|---|
| committer | kalkyl <[email protected]> | 2023-05-09 01:51:08 +0200 |
| commit | 72b0379125b87bcd274bdb81127dd5f0ab29d661 (patch) | |
| tree | b620d67bc9ea930051668d63190dd6de62485b0a /examples/src/bin/multisocket.rs | |
:rainbow:
Diffstat (limited to 'examples/src/bin/multisocket.rs')
| -rw-r--r-- | examples/src/bin/multisocket.rs | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/examples/src/bin/multisocket.rs b/examples/src/bin/multisocket.rs new file mode 100644 index 000000000..49bcbdbb0 --- /dev/null +++ b/examples/src/bin/multisocket.rs | |||
| @@ -0,0 +1,148 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_futures::yield_now; | ||
| 8 | use embassy_net::{Stack, StackResources}; | ||
| 9 | use embassy_net_w5500::*; | ||
| 10 | use embassy_rp::clocks::RoscRng; | ||
| 11 | use embassy_rp::gpio::{Input, Level, Output, Pull}; | ||
| 12 | use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0}; | ||
| 13 | use embassy_rp::spi::{Async, Config as SpiConfig, Spi}; | ||
| 14 | use embedded_hal_async::spi::ExclusiveDevice; | ||
| 15 | use embedded_io::asynch::Write; | ||
| 16 | use rand::RngCore; | ||
| 17 | use static_cell::StaticCell; | ||
| 18 | use {defmt_rtt as _, panic_probe as _}; | ||
| 19 | |||
| 20 | macro_rules! singleton { | ||
| 21 | ($val:expr) => {{ | ||
| 22 | type T = impl Sized; | ||
| 23 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); | ||
| 24 | let (x,) = STATIC_CELL.init(($val,)); | ||
| 25 | x | ||
| 26 | }}; | ||
| 27 | } | ||
| 28 | |||
| 29 | #[embassy_executor::task] | ||
| 30 | async fn ethernet_task( | ||
| 31 | runner: Runner< | ||
| 32 | 'static, | ||
| 33 | ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static, PIN_17>>, | ||
| 34 | Input<'static, PIN_21>, | ||
| 35 | Output<'static, PIN_20>, | ||
| 36 | >, | ||
| 37 | ) -> ! { | ||
| 38 | runner.run().await | ||
| 39 | } | ||
| 40 | |||
| 41 | #[embassy_executor::task] | ||
| 42 | async fn net_task(stack: &'static Stack<Device<'static>>) -> ! { | ||
| 43 | stack.run().await | ||
| 44 | } | ||
| 45 | |||
| 46 | #[embassy_executor::main] | ||
| 47 | async fn main(spawner: Spawner) { | ||
| 48 | let p = embassy_rp::init(Default::default()); | ||
| 49 | let mut rng = RoscRng; | ||
| 50 | |||
| 51 | let mut spi_cfg = SpiConfig::default(); | ||
| 52 | spi_cfg.frequency = 50_000_000; | ||
| 53 | let (miso, mosi, clk) = (p.PIN_16, p.PIN_19, p.PIN_18); | ||
| 54 | let spi = Spi::new(p.SPI0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg); | ||
| 55 | let cs = Output::new(p.PIN_17, Level::High); | ||
| 56 | let w5500_int = Input::new(p.PIN_21, Pull::Up); | ||
| 57 | let w5500_reset = Output::new(p.PIN_20, Level::High); | ||
| 58 | |||
| 59 | let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; | ||
| 60 | let state = singleton!(State::<8, 8>::new()); | ||
| 61 | let (device, runner) = embassy_net_w5500::new( | ||
| 62 | mac_addr, | ||
| 63 | state, | ||
| 64 | ExclusiveDevice::new(spi, cs), | ||
| 65 | w5500_int, | ||
| 66 | w5500_reset, | ||
| 67 | ) | ||
| 68 | .await; | ||
| 69 | unwrap!(spawner.spawn(ethernet_task(runner))); | ||
| 70 | |||
| 71 | // Generate random seed | ||
| 72 | let seed = rng.next_u64(); | ||
| 73 | |||
| 74 | // Init network stack | ||
| 75 | let stack = &*singleton!(Stack::new( | ||
| 76 | device, | ||
| 77 | embassy_net::Config::Dhcp(Default::default()), | ||
| 78 | singleton!(StackResources::<3>::new()), | ||
| 79 | seed | ||
| 80 | )); | ||
| 81 | |||
| 82 | // Launch network task | ||
| 83 | unwrap!(spawner.spawn(net_task(&stack))); | ||
| 84 | |||
| 85 | info!("Waiting for DHCP..."); | ||
| 86 | let cfg = wait_for_config(stack).await; | ||
| 87 | let local_addr = cfg.address.address(); | ||
| 88 | info!("IP address: {:?}", local_addr); | ||
| 89 | |||
| 90 | // Create two sockets listening to the same port, to handle simultaneous connections | ||
| 91 | unwrap!(spawner.spawn(listen_task(&stack, 0, 1234))); | ||
| 92 | unwrap!(spawner.spawn(listen_task(&stack, 1, 1234))); | ||
| 93 | } | ||
| 94 | |||
| 95 | #[embassy_executor::task(pool_size = 2)] | ||
| 96 | async fn listen_task(stack: &'static Stack<Device<'static>>, id: u8, port: u16) { | ||
| 97 | let mut rx_buffer = [0; 4096]; | ||
| 98 | let mut tx_buffer = [0; 4096]; | ||
| 99 | let mut buf = [0; 4096]; | ||
| 100 | loop { | ||
| 101 | let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); | ||
| 102 | socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); | ||
| 103 | |||
| 104 | info!("SOCKET {}: Listening on TCP:{}...", id, port); | ||
| 105 | if let Err(e) = socket.accept(port).await { | ||
| 106 | warn!("accept error: {:?}", e); | ||
| 107 | continue; | ||
| 108 | } | ||
| 109 | info!( | ||
| 110 | "SOCKET {}: Received connection from {:?}", | ||
| 111 | id, | ||
| 112 | socket.remote_endpoint() | ||
| 113 | ); | ||
| 114 | |||
| 115 | loop { | ||
| 116 | let n = match socket.read(&mut buf).await { | ||
| 117 | Ok(0) => { | ||
| 118 | warn!("read EOF"); | ||
| 119 | break; | ||
| 120 | } | ||
| 121 | Ok(n) => n, | ||
| 122 | Err(e) => { | ||
| 123 | warn!("SOCKET {}: {:?}", id, e); | ||
| 124 | break; | ||
| 125 | } | ||
| 126 | }; | ||
| 127 | info!( | ||
| 128 | "SOCKET {}: rxd {}", | ||
| 129 | id, | ||
| 130 | core::str::from_utf8(&buf[..n]).unwrap() | ||
| 131 | ); | ||
| 132 | |||
| 133 | if let Err(e) = socket.write_all(&buf[..n]).await { | ||
| 134 | warn!("write error: {:?}", e); | ||
| 135 | break; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | async fn wait_for_config(stack: &'static Stack<Device<'static>>) -> embassy_net::StaticConfig { | ||
| 142 | loop { | ||
| 143 | if let Some(config) = stack.config() { | ||
| 144 | return config.clone(); | ||
| 145 | } | ||
| 146 | yield_now().await; | ||
| 147 | } | ||
| 148 | } | ||
