diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-08-05 19:10:45 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-08-15 14:09:36 +0200 |
| commit | 4af1cf88d29f29df52f8c9e7928409e60a91ffbc (patch) | |
| tree | 378592151c055bce98da86f3b18aacdef4798564 | |
| parent | 2c1402843ae8d4f07db3670bd7153e794a1553ae (diff) | |
net-enc28j60: add example.
| -rw-r--r-- | examples/nrf52840/Cargo.toml | 4 | ||||
| -rw-r--r-- | examples/nrf52840/src/bin/ethernet_enc28j60.rs | 124 |
2 files changed, 128 insertions, 0 deletions
diff --git a/examples/nrf52840/Cargo.toml b/examples/nrf52840/Cargo.toml index 15fe22d3a..8c6f6bccf 100644 --- a/examples/nrf52840/Cargo.toml +++ b/examples/nrf52840/Cargo.toml | |||
| @@ -12,6 +12,7 @@ nightly = [ | |||
| 12 | "embassy-nrf/nightly", | 12 | "embassy-nrf/nightly", |
| 13 | "embassy-net/nightly", | 13 | "embassy-net/nightly", |
| 14 | "embassy-net-esp-hosted", | 14 | "embassy-net-esp-hosted", |
| 15 | "embassy-net-enc28j60", | ||
| 15 | "embassy-nrf/unstable-traits", | 16 | "embassy-nrf/unstable-traits", |
| 16 | "embassy-time/nightly", | 17 | "embassy-time/nightly", |
| 17 | "embassy-time/unstable-traits", | 18 | "embassy-time/unstable-traits", |
| @@ -40,6 +41,7 @@ lora-phy = { version = "1", optional = true } | |||
| 40 | lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"], optional = true } | 41 | lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"], optional = true } |
| 41 | lorawan = { version = "0.7.3", default-features = false, features = ["default-crypto"], optional = true } | 42 | lorawan = { version = "0.7.3", default-features = false, features = ["default-crypto"], optional = true } |
| 42 | embassy-net-esp-hosted = { version = "0.1.0", path = "../../embassy-net-esp-hosted", features = ["defmt"], optional = true } | 43 | embassy-net-esp-hosted = { version = "0.1.0", path = "../../embassy-net-esp-hosted", features = ["defmt"], optional = true } |
| 44 | embassy-net-enc28j60 = { version = "0.1.0", path = "../../embassy-net-enc28j60", features = ["defmt"], optional = true } | ||
| 43 | 45 | ||
| 44 | defmt = "0.3" | 46 | defmt = "0.3" |
| 45 | defmt-rtt = "0.4" | 47 | defmt-rtt = "0.4" |
| @@ -54,7 +56,9 @@ rand = { version = "0.8.4", default-features = false } | |||
| 54 | embedded-storage = "0.3.0" | 56 | embedded-storage = "0.3.0" |
| 55 | usbd-hid = "0.6.0" | 57 | usbd-hid = "0.6.0" |
| 56 | serde = { version = "1.0.136", default-features = false } | 58 | serde = { version = "1.0.136", default-features = false } |
| 59 | embedded-hal = { version = "1.0.0-alpha.11" } | ||
| 57 | embedded-hal-async = { version = "0.2.0-alpha.2", optional = true } | 60 | embedded-hal-async = { version = "0.2.0-alpha.2", optional = true } |
| 61 | embedded-hal-bus = { version = "0.1.0-alpha.3" } | ||
| 58 | num-integer = { version = "0.1.45", default-features = false } | 62 | num-integer = { version = "0.1.45", default-features = false } |
| 59 | microfft = "0.5.0" | 63 | microfft = "0.5.0" |
| 60 | 64 | ||
diff --git a/examples/nrf52840/src/bin/ethernet_enc28j60.rs b/examples/nrf52840/src/bin/ethernet_enc28j60.rs new file mode 100644 index 000000000..d1b796fab --- /dev/null +++ b/examples/nrf52840/src/bin/ethernet_enc28j60.rs | |||
| @@ -0,0 +1,124 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_net::tcp::TcpSocket; | ||
| 8 | use embassy_net::{Stack, StackResources}; | ||
| 9 | use embassy_net_enc28j60::Enc28j60; | ||
| 10 | use embassy_nrf::gpio::{Level, Output, OutputDrive}; | ||
| 11 | use embassy_nrf::rng::Rng; | ||
| 12 | use embassy_nrf::spim::Spim; | ||
| 13 | use embassy_nrf::{bind_interrupts, peripherals, spim}; | ||
| 14 | use embassy_time::Delay; | ||
| 15 | use embedded_hal_bus::spi::ExclusiveDevice; | ||
| 16 | use embedded_io_async::Write; | ||
| 17 | use static_cell::make_static; | ||
| 18 | use {defmt_rtt as _, panic_probe as _}; | ||
| 19 | |||
| 20 | bind_interrupts!(struct Irqs { | ||
| 21 | SPIM3 => spim::InterruptHandler<peripherals::SPI3>; | ||
| 22 | RNG => embassy_nrf::rng::InterruptHandler<peripherals::RNG>; | ||
| 23 | }); | ||
| 24 | |||
| 25 | #[embassy_executor::task] | ||
| 26 | async fn net_task( | ||
| 27 | stack: &'static Stack< | ||
| 28 | Enc28j60< | ||
| 29 | ExclusiveDevice<Spim<'static, peripherals::SPI3>, Output<'static, peripherals::P0_15>, Delay>, | ||
| 30 | Output<'static, peripherals::P0_13>, | ||
| 31 | >, | ||
| 32 | >, | ||
| 33 | ) -> ! { | ||
| 34 | stack.run().await | ||
| 35 | } | ||
| 36 | |||
| 37 | #[embassy_executor::main] | ||
| 38 | async fn main(spawner: Spawner) { | ||
| 39 | let p = embassy_nrf::init(Default::default()); | ||
| 40 | info!("running!"); | ||
| 41 | |||
| 42 | let eth_sck = p.P0_20; | ||
| 43 | let eth_mosi = p.P0_22; | ||
| 44 | let eth_miso = p.P0_24; | ||
| 45 | let eth_cs = p.P0_15; | ||
| 46 | let eth_rst = p.P0_13; | ||
| 47 | let _eth_irq = p.P0_12; | ||
| 48 | |||
| 49 | let mut config = spim::Config::default(); | ||
| 50 | config.frequency = spim::Frequency::M16; | ||
| 51 | let spi = spim::Spim::new(p.SPI3, Irqs, eth_sck, eth_miso, eth_mosi, config); | ||
| 52 | let cs = Output::new(eth_cs, Level::High, OutputDrive::Standard); | ||
| 53 | let spi = ExclusiveDevice::new(spi, cs, Delay); | ||
| 54 | |||
| 55 | let rst = Output::new(eth_rst, Level::High, OutputDrive::Standard); | ||
| 56 | let mac_addr = [2, 3, 4, 5, 6, 7]; | ||
| 57 | let device = Enc28j60::new(spi, Some(rst), mac_addr); | ||
| 58 | |||
| 59 | let config = embassy_net::Config::dhcpv4(Default::default()); | ||
| 60 | // let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 { | ||
| 61 | // address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24), | ||
| 62 | // dns_servers: Vec::new(), | ||
| 63 | // gateway: Some(Ipv4Address::new(10, 42, 0, 1)), | ||
| 64 | // }); | ||
| 65 | |||
| 66 | // Generate random seed | ||
| 67 | let mut rng = Rng::new(p.RNG, Irqs); | ||
| 68 | let mut seed = [0; 8]; | ||
| 69 | rng.blocking_fill_bytes(&mut seed); | ||
| 70 | let seed = u64::from_le_bytes(seed); | ||
| 71 | |||
| 72 | // Init network stack | ||
| 73 | let stack = &*make_static!(Stack::new( | ||
| 74 | device, | ||
| 75 | config, | ||
| 76 | make_static!(StackResources::<2>::new()), | ||
| 77 | seed | ||
| 78 | )); | ||
| 79 | |||
| 80 | unwrap!(spawner.spawn(net_task(stack))); | ||
| 81 | |||
| 82 | // And now we can use it! | ||
| 83 | |||
| 84 | let mut rx_buffer = [0; 4096]; | ||
| 85 | let mut tx_buffer = [0; 4096]; | ||
| 86 | let mut buf = [0; 4096]; | ||
| 87 | |||
| 88 | loop { | ||
| 89 | let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); | ||
| 90 | socket.set_timeout(Some(embassy_time::Duration::from_secs(10))); | ||
| 91 | |||
| 92 | info!("Listening on TCP:1234..."); | ||
| 93 | if let Err(e) = socket.accept(1234).await { | ||
| 94 | warn!("accept error: {:?}", e); | ||
| 95 | continue; | ||
| 96 | } | ||
| 97 | |||
| 98 | info!("Received connection from {:?}", socket.remote_endpoint()); | ||
| 99 | |||
| 100 | loop { | ||
| 101 | let n = match socket.read(&mut buf).await { | ||
| 102 | Ok(0) => { | ||
| 103 | warn!("read EOF"); | ||
| 104 | break; | ||
| 105 | } | ||
| 106 | Ok(n) => n, | ||
| 107 | Err(e) => { | ||
| 108 | warn!("read error: {:?}", e); | ||
| 109 | break; | ||
| 110 | } | ||
| 111 | }; | ||
| 112 | |||
| 113 | info!("rxd {:02x}", &buf[..n]); | ||
| 114 | |||
| 115 | match socket.write_all(&buf[..n]).await { | ||
| 116 | Ok(()) => {} | ||
| 117 | Err(e) => { | ||
| 118 | warn!("write error: {:?}", e); | ||
| 119 | break; | ||
| 120 | } | ||
| 121 | }; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
