diff options
| author | Ulf Lilleengen <[email protected]> | 2022-08-09 14:44:18 +0200 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2022-08-09 14:44:18 +0200 |
| commit | 2e76b13a4c5fcf46b3a9fc58ceef91e9317c12b7 (patch) | |
| tree | 716c6ed979d6bfa5537a4ffb449a8faf0ba51b1b /examples | |
| parent | 80c1551153b06a14e5dc475f6fbd945db06b8117 (diff) | |
Add example using embedded-nal-async traits
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32h7/src/bin/eth_client.rs | 125 |
1 files changed, 125 insertions, 0 deletions
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 | } | ||
