diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-06-09 03:36:48 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-06-22 21:12:10 +0200 |
| commit | 6c123596b7d48ee66ea93e8b1515e91231e9bced (patch) | |
| tree | 7f657d78d2fac5a24982d74b909789d60a2dfee7 /examples | |
| parent | 1f2be2dac5eeed739d2866b9b63ca06fdd84c276 (diff) | |
wip: esp-hosted net driver.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf52840/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/nrf52840/src/bin/wifi_esp_hosted.rs | 143 |
2 files changed, 145 insertions, 0 deletions
diff --git a/examples/nrf52840/Cargo.toml b/examples/nrf52840/Cargo.toml index 6627b7861..7c9d48bad 100644 --- a/examples/nrf52840/Cargo.toml +++ b/examples/nrf52840/Cargo.toml | |||
| @@ -22,6 +22,7 @@ embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["ti | |||
| 22 | lora-phy = { version = "1", optional = true } | 22 | lora-phy = { version = "1", optional = true } |
| 23 | lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"], optional = true } | 23 | lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"], optional = true } |
| 24 | lorawan = { version = "0.7.3", default-features = false, features = ["default-crypto"], optional = true } | 24 | lorawan = { version = "0.7.3", default-features = false, features = ["default-crypto"], optional = true } |
| 25 | embassy-net-esp-hosted = { version = "0.1.0", path = "../../embassy-net-esp-hosted", features = ["defmt"] } | ||
| 25 | 26 | ||
| 26 | defmt = "0.3" | 27 | defmt = "0.3" |
| 27 | defmt-rtt = "0.4" | 28 | defmt-rtt = "0.4" |
| @@ -35,3 +36,4 @@ rand = { version = "0.8.4", default-features = false } | |||
| 35 | embedded-storage = "0.3.0" | 36 | embedded-storage = "0.3.0" |
| 36 | usbd-hid = "0.6.0" | 37 | usbd-hid = "0.6.0" |
| 37 | serde = { version = "1.0.136", default-features = false } | 38 | serde = { version = "1.0.136", default-features = false } |
| 39 | embedded-hal-async = "0.2.0-alpha.1" | ||
diff --git a/examples/nrf52840/src/bin/wifi_esp_hosted.rs b/examples/nrf52840/src/bin/wifi_esp_hosted.rs new file mode 100644 index 000000000..401dbd33c --- /dev/null +++ b/examples/nrf52840/src/bin/wifi_esp_hosted.rs | |||
| @@ -0,0 +1,143 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::{info, unwrap, warn}; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_net::tcp::TcpSocket; | ||
| 8 | use embassy_net::{Stack, StackResources}; | ||
| 9 | use embassy_nrf::gpio::{AnyPin, Input, Level, Output, OutputDrive, Pin, Pull}; | ||
| 10 | use embassy_nrf::rng::Rng; | ||
| 11 | use embassy_nrf::spim::{self, Spim}; | ||
| 12 | use embassy_nrf::{bind_interrupts, peripherals}; | ||
| 13 | use embassy_time::{Duration, Timer}; | ||
| 14 | use embedded_hal_async::spi::ExclusiveDevice; | ||
| 15 | use embedded_io::asynch::Write; | ||
| 16 | use static_cell::make_static; | ||
| 17 | use {defmt_rtt as _, embassy_net_esp_hosted as hosted, panic_probe as _}; | ||
| 18 | |||
| 19 | bind_interrupts!(struct Irqs { | ||
| 20 | SPIM3 => spim::InterruptHandler<peripherals::SPI3>; | ||
| 21 | RNG => embassy_nrf::rng::InterruptHandler<peripherals::RNG>; | ||
| 22 | }); | ||
| 23 | |||
| 24 | #[embassy_executor::task] | ||
| 25 | async fn wifi_task( | ||
| 26 | runner: hosted::Runner< | ||
| 27 | 'static, | ||
| 28 | ExclusiveDevice<Spim<'static, peripherals::SPI3>, Output<'static, peripherals::P0_31>>, | ||
| 29 | Input<'static, AnyPin>, | ||
| 30 | Output<'static, peripherals::P1_03>, | ||
| 31 | >, | ||
| 32 | ) -> ! { | ||
| 33 | runner.run().await | ||
| 34 | } | ||
| 35 | |||
| 36 | #[embassy_executor::task] | ||
| 37 | async fn net_task(stack: &'static Stack<hosted::NetDriver<'static>>) -> ! { | ||
| 38 | stack.run().await | ||
| 39 | } | ||
| 40 | |||
| 41 | #[embassy_executor::main] | ||
| 42 | async fn main(spawner: Spawner) { | ||
| 43 | info!("Hello World!"); | ||
| 44 | |||
| 45 | let p = embassy_nrf::init(Default::default()); | ||
| 46 | |||
| 47 | let miso = p.P0_28; | ||
| 48 | let sck = p.P0_29; | ||
| 49 | let mosi = p.P0_30; | ||
| 50 | let cs = Output::new(p.P0_31, Level::High, OutputDrive::HighDrive); | ||
| 51 | let handshake = Input::new(p.P1_01.degrade(), Pull::Up); | ||
| 52 | let ready = Input::new(p.P1_02.degrade(), Pull::None); | ||
| 53 | let reset = Output::new(p.P1_03, Level::Low, OutputDrive::Standard); | ||
| 54 | |||
| 55 | let mut config = spim::Config::default(); | ||
| 56 | config.frequency = spim::Frequency::M1; | ||
| 57 | config.mode = spim::MODE_2; // !!! | ||
| 58 | let spi = spim::Spim::new(p.SPI3, Irqs, sck, miso, mosi, config); | ||
| 59 | let spi = ExclusiveDevice::new(spi, cs); | ||
| 60 | |||
| 61 | let (device, mut control, runner) = embassy_net_esp_hosted::new( | ||
| 62 | make_static!(embassy_net_esp_hosted::State::new()), | ||
| 63 | spi, | ||
| 64 | handshake, | ||
| 65 | ready, | ||
| 66 | reset, | ||
| 67 | ) | ||
| 68 | .await; | ||
| 69 | |||
| 70 | unwrap!(spawner.spawn(wifi_task(runner))); | ||
| 71 | |||
| 72 | // TODO: wait for ESP_INIT event instead of hardcoding delay. | ||
| 73 | Timer::after(Duration::from_secs(3)).await; | ||
| 74 | |||
| 75 | control.init().await; | ||
| 76 | control.join(env!("WIFI_NETWORK"), env!("WIFI_PASSWORD")).await; | ||
| 77 | |||
| 78 | let config = embassy_net::Config::dhcpv4(Default::default()); | ||
| 79 | // let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 { | ||
| 80 | // address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24), | ||
| 81 | // dns_servers: Vec::new(), | ||
| 82 | // gateway: Some(Ipv4Address::new(10, 42, 0, 1)), | ||
| 83 | // }); | ||
| 84 | |||
| 85 | // Generate random seed | ||
| 86 | let mut rng = Rng::new(p.RNG, Irqs); | ||
| 87 | let mut seed = [0; 8]; | ||
| 88 | rng.blocking_fill_bytes(&mut seed); | ||
| 89 | let seed = u64::from_le_bytes(seed); | ||
| 90 | |||
| 91 | // Init network stack | ||
| 92 | let stack = &*make_static!(Stack::new( | ||
| 93 | device, | ||
| 94 | config, | ||
| 95 | make_static!(StackResources::<2>::new()), | ||
| 96 | seed | ||
| 97 | )); | ||
| 98 | |||
| 99 | unwrap!(spawner.spawn(net_task(stack))); | ||
| 100 | |||
| 101 | // And now we can use it! | ||
| 102 | |||
| 103 | let mut rx_buffer = [0; 4096]; | ||
| 104 | let mut tx_buffer = [0; 4096]; | ||
| 105 | let mut buf = [0; 4096]; | ||
| 106 | |||
| 107 | loop { | ||
| 108 | let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); | ||
| 109 | socket.set_timeout(Some(embassy_time::Duration::from_secs(10))); | ||
| 110 | |||
| 111 | info!("Listening on TCP:1234..."); | ||
| 112 | if let Err(e) = socket.accept(1234).await { | ||
| 113 | warn!("accept error: {:?}", e); | ||
| 114 | continue; | ||
| 115 | } | ||
| 116 | |||
| 117 | info!("Received connection from {:?}", socket.remote_endpoint()); | ||
| 118 | |||
| 119 | loop { | ||
| 120 | let n = match socket.read(&mut buf).await { | ||
| 121 | Ok(0) => { | ||
| 122 | warn!("read EOF"); | ||
| 123 | break; | ||
| 124 | } | ||
| 125 | Ok(n) => n, | ||
| 126 | Err(e) => { | ||
| 127 | warn!("read error: {:?}", e); | ||
| 128 | break; | ||
| 129 | } | ||
| 130 | }; | ||
| 131 | |||
| 132 | info!("rxd {:02x}", &buf[..n]); | ||
| 133 | |||
| 134 | match socket.write_all(&buf[..n]).await { | ||
| 135 | Ok(()) => {} | ||
| 136 | Err(e) => { | ||
| 137 | warn!("write error: {:?}", e); | ||
| 138 | break; | ||
| 139 | } | ||
| 140 | }; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
