diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-08-10 08:01:59 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-08-10 08:01:59 +0000 |
| commit | de22cb906567b1262f91398c82b6ed90803852fc (patch) | |
| tree | ba3aa090ca446595fd041416d193299f9ff1eeb2 /examples | |
| parent | b7b4c84067e0e85fa641a540458438297176f2e4 (diff) | |
| parent | 87401c49b71eb22a4f1a9ce4b318ebd20ea38000 (diff) | |
Merge #895
895: Implement embedded-nal-async traits for embassy-net r=lulf a=lulf
Co-authored-by: Ulf Lilleengen <[email protected]>
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32h7/Cargo.toml | 3 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/eth_client.rs | 125 |
2 files changed, 127 insertions, 1 deletions
diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml index 8b1999b30..896046759 100644 --- a/examples/stm32h7/Cargo.toml +++ b/examples/stm32h7/Cargo.toml | |||
| @@ -7,7 +7,7 @@ version = "0.1.0" | |||
| 7 | embassy-util = { version = "0.1.0", path = "../../embassy-util", features = ["defmt"] } | 7 | embassy-util = { version = "0.1.0", path = "../../embassy-util", features = ["defmt"] } |
| 8 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "time-tick-32768hz"] } | 8 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "time-tick-32768hz"] } |
| 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h743bi", "net", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] } | 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h743bi", "net", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] } |
| 10 | embassy-net = { path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } | 10 | embassy-net = { path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16", "unstable-traits"] } |
| 11 | embedded-io = { version = "0.3.0", features = ["async"] } | 11 | embedded-io = { version = "0.3.0", features = ["async"] } |
| 12 | 12 | ||
| 13 | defmt = "0.3" | 13 | defmt = "0.3" |
| @@ -18,6 +18,7 @@ cortex-m-rt = "0.7.0" | |||
| 18 | embedded-hal = "0.2.6" | 18 | embedded-hal = "0.2.6" |
| 19 | embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" } | 19 | embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" } |
| 20 | embedded-hal-async = { version = "0.1.0-alpha.1" } | 20 | embedded-hal-async = { version = "0.1.0-alpha.1" } |
| 21 | embedded-nal-async = "0.2.0" | ||
| 21 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 22 | panic-probe = { version = "0.3", features = ["print-defmt"] } |
| 22 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 23 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 23 | heapless = { version = "0.7.5", default-features = false } | 24 | heapless = { version = "0.7.5", default-features = false } |
diff --git a/examples/stm32h7/src/bin/eth_client.rs b/examples/stm32h7/src/bin/eth_client.rs new file mode 100644 index 000000000..a66c6f196 --- /dev/null +++ b/examples/stm32h7/src/bin/eth_client.rs | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::executor::Spawner; | ||
| 7 | use embassy_executor::time::{Duration, Timer}; | ||
| 8 | use embassy_net::tcp::client::{TcpClient, TcpClientState}; | ||
| 9 | use embassy_net::{Stack, StackResources}; | ||
| 10 | use embassy_stm32::eth::generic_smi::GenericSMI; | ||
| 11 | use embassy_stm32::eth::{Ethernet, State}; | ||
| 12 | use embassy_stm32::peripherals::ETH; | ||
| 13 | use embassy_stm32::rng::Rng; | ||
| 14 | use embassy_stm32::time::mhz; | ||
| 15 | use embassy_stm32::{interrupt, Config, Peripherals}; | ||
| 16 | use embassy_util::Forever; | ||
| 17 | use embedded_io::asynch::Write; | ||
| 18 | use embedded_nal_async::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpConnect}; | ||
| 19 | use rand_core::RngCore; | ||
| 20 | use {defmt_rtt as _, panic_probe as _}; | ||
| 21 | |||
| 22 | macro_rules! forever { | ||
| 23 | ($val:expr) => {{ | ||
| 24 | type T = impl Sized; | ||
| 25 | static FOREVER: Forever<T> = Forever::new(); | ||
| 26 | FOREVER.put_with(move || $val) | ||
| 27 | }}; | ||
| 28 | } | ||
| 29 | |||
| 30 | type Device = Ethernet<'static, ETH, GenericSMI, 4, 4>; | ||
| 31 | |||
| 32 | #[embassy_executor::task] | ||
| 33 | async fn net_task(stack: &'static Stack<Device>) -> ! { | ||
| 34 | stack.run().await | ||
| 35 | } | ||
| 36 | |||
| 37 | pub fn config() -> Config { | ||
| 38 | let mut config = Config::default(); | ||
| 39 | config.rcc.sys_ck = Some(mhz(400)); | ||
| 40 | config.rcc.hclk = Some(mhz(200)); | ||
| 41 | config.rcc.pll1.q_ck = Some(mhz(100)); | ||
| 42 | config | ||
| 43 | } | ||
| 44 | |||
| 45 | #[embassy_executor::main(config = "config()")] | ||
| 46 | async fn main(spawner: Spawner, p: Peripherals) -> ! { | ||
| 47 | info!("Hello World!"); | ||
| 48 | |||
| 49 | // Generate random seed. | ||
| 50 | let mut rng = Rng::new(p.RNG); | ||
| 51 | let mut seed = [0; 8]; | ||
| 52 | rng.fill_bytes(&mut seed); | ||
| 53 | let seed = u64::from_le_bytes(seed); | ||
| 54 | |||
| 55 | let eth_int = interrupt::take!(ETH); | ||
| 56 | let mac_addr = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF]; | ||
| 57 | |||
| 58 | let device = unsafe { | ||
| 59 | Ethernet::new( | ||
| 60 | forever!(State::new()), | ||
| 61 | p.ETH, | ||
| 62 | eth_int, | ||
| 63 | p.PA1, | ||
| 64 | p.PA2, | ||
| 65 | p.PC1, | ||
| 66 | p.PA7, | ||
| 67 | p.PC4, | ||
| 68 | p.PC5, | ||
| 69 | p.PG13, | ||
| 70 | p.PB13, | ||
| 71 | p.PG11, | ||
| 72 | GenericSMI, | ||
| 73 | mac_addr, | ||
| 74 | 0, | ||
| 75 | ) | ||
| 76 | }; | ||
| 77 | |||
| 78 | let config = embassy_net::ConfigStrategy::Dhcp; | ||
| 79 | //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { | ||
| 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 | // Init network stack | ||
| 86 | let stack = &*forever!(Stack::new( | ||
| 87 | device, | ||
| 88 | config, | ||
| 89 | forever!(StackResources::<1, 2, 8>::new()), | ||
| 90 | seed | ||
| 91 | )); | ||
| 92 | |||
| 93 | // Launch network task | ||
| 94 | unwrap!(spawner.spawn(net_task(&stack))); | ||
| 95 | |||
| 96 | info!("Network task initialized"); | ||
| 97 | |||
| 98 | // To ensure DHCP configuration before trying connect | ||
| 99 | Timer::after(Duration::from_secs(20)).await; | ||
| 100 | |||
| 101 | static STATE: TcpClientState<1, 1024, 1024> = TcpClientState::new(); | ||
| 102 | let client = TcpClient::new(&stack, &STATE); | ||
| 103 | |||
| 104 | loop { | ||
| 105 | let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(10, 42, 0, 1), 8000)); | ||
| 106 | |||
| 107 | info!("connecting..."); | ||
| 108 | let r = client.connect(addr).await; | ||
| 109 | if let Err(e) = r { | ||
| 110 | info!("connect error: {:?}", e); | ||
| 111 | Timer::after(Duration::from_secs(1)).await; | ||
| 112 | continue; | ||
| 113 | } | ||
| 114 | let mut connection = r.unwrap(); | ||
| 115 | info!("connected!"); | ||
| 116 | loop { | ||
| 117 | let r = connection.write_all(b"Hello\n").await; | ||
| 118 | if let Err(e) = r { | ||
| 119 | info!("write error: {:?}", e); | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | Timer::after(Duration::from_secs(1)).await; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } | ||
