diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-05-23 03:50:43 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-05-25 19:56:22 +0200 |
| commit | a5aea995a802fea8fc1b3e4b5fe47bd6d1fca2a4 (patch) | |
| tree | 0fcb4c01914347eff5b3be44b284aa9432e28678 /examples/stm32f7 | |
| parent | 36a1f203648dcb402727ea3eb5d30cf1f6993795 (diff) | |
WIP embassy-net v2
Diffstat (limited to 'examples/stm32f7')
| -rw-r--r-- | examples/stm32f7/Cargo.toml | 7 | ||||
| -rw-r--r-- | examples/stm32f7/src/bin/eth.rs | 173 |
2 files changed, 84 insertions, 96 deletions
diff --git a/examples/stm32f7/Cargo.toml b/examples/stm32f7/Cargo.toml index e68d1d58c..cd7394a5c 100644 --- a/examples/stm32f7/Cargo.toml +++ b/examples/stm32f7/Cargo.toml | |||
| @@ -8,7 +8,7 @@ resolver = "2" | |||
| 8 | [dependencies] | 8 | [dependencies] |
| 9 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime"] } | 9 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime"] } |
| 10 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "net", "stm32f767zi", "unstable-pac", "time-driver-any", "exti"] } | 10 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "net", "stm32f767zi", "unstable-pac", "time-driver-any", "exti"] } |
| 11 | embassy-net = { path = "../../embassy-net", features = ["defmt", "tcp", "medium-ethernet", "pool-16"] } | 11 | embassy-net = { path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } |
| 12 | embedded-io = { version = "0.3.0", features = ["async"] } | 12 | embedded-io = { version = "0.3.0", features = ["async"] } |
| 13 | 13 | ||
| 14 | defmt = "0.3" | 14 | defmt = "0.3" |
| @@ -24,8 +24,3 @@ nb = "1.0.0" | |||
| 24 | rand_core = "0.6.3" | 24 | rand_core = "0.6.3" |
| 25 | critical-section = "0.2.3" | 25 | critical-section = "0.2.3" |
| 26 | embedded-storage = "0.3.0" | 26 | embedded-storage = "0.3.0" |
| 27 | |||
| 28 | [dependencies.smoltcp] | ||
| 29 | version = "0.8.0" | ||
| 30 | default-features = false | ||
| 31 | features = ["defmt"] | ||
diff --git a/examples/stm32f7/src/bin/eth.rs b/examples/stm32f7/src/bin/eth.rs index dca9338b2..af012f826 100644 --- a/examples/stm32f7/src/bin/eth.rs +++ b/examples/stm32f7/src/bin/eth.rs | |||
| @@ -2,130 +2,123 @@ | |||
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 3 | #![feature(type_alias_impl_trait)] |
| 4 | 4 | ||
| 5 | use cortex_m_rt::entry; | ||
| 6 | use defmt::*; | 5 | use defmt::*; |
| 7 | use embassy::executor::{Executor, Spawner}; | 6 | use embassy::executor::Spawner; |
| 8 | use embassy::time::{Duration, Timer}; | 7 | use embassy::time::{Duration, Timer}; |
| 9 | use embassy::util::Forever; | 8 | use embassy::util::Forever; |
| 10 | use embassy_net::tcp::TcpSocket; | 9 | use embassy_net::tcp::TcpSocket; |
| 11 | use embassy_net::{Config as NetConfig, Ipv4Address, Ipv4Cidr, StackResources, StaticConfigurator}; | 10 | use embassy_net::{Ipv4Address, Stack, StackResources}; |
| 12 | use embassy_stm32::eth::generic_smi::GenericSMI; | 11 | use embassy_stm32::eth::generic_smi::GenericSMI; |
| 13 | use embassy_stm32::eth::{Ethernet, State}; | 12 | use embassy_stm32::eth::{Ethernet, State}; |
| 14 | use embassy_stm32::interrupt; | ||
| 15 | use embassy_stm32::peripherals::ETH; | 13 | use embassy_stm32::peripherals::ETH; |
| 16 | use embassy_stm32::peripherals::RNG; | ||
| 17 | use embassy_stm32::rng::Rng; | 14 | use embassy_stm32::rng::Rng; |
| 18 | use embassy_stm32::time::U32Ext; | 15 | use embassy_stm32::time::U32Ext; |
| 19 | use embassy_stm32::Config; | 16 | use embassy_stm32::Config; |
| 17 | use embassy_stm32::{interrupt, Peripherals}; | ||
| 20 | use embedded_io::asynch::Write; | 18 | use embedded_io::asynch::Write; |
| 21 | use heapless::Vec; | ||
| 22 | 19 | ||
| 23 | use defmt_rtt as _; // global logger | 20 | use defmt_rtt as _; // global logger |
| 24 | use panic_probe as _; | 21 | use panic_probe as _; |
| 25 | 22 | use rand_core::RngCore; | |
| 26 | #[embassy::task] | 23 | |
| 27 | async fn main_task( | 24 | macro_rules! forever { |
| 28 | device: &'static mut Ethernet<'static, ETH, GenericSMI, 4, 4>, | 25 | ($val:expr) => {{ |
| 29 | config: &'static mut StaticConfigurator, | 26 | type T = impl Sized; |
| 30 | spawner: Spawner, | 27 | static FOREVER: Forever<T> = Forever::new(); |
| 31 | ) { | 28 | FOREVER.put_with(move || $val) |
| 32 | let net_resources = NET_RESOURCES.put(StackResources::new()); | 29 | }}; |
| 33 | |||
| 34 | // Init network stack | ||
| 35 | embassy_net::init(device, config, net_resources); | ||
| 36 | |||
| 37 | // Launch network task | ||
| 38 | unwrap!(spawner.spawn(net_task())); | ||
| 39 | |||
| 40 | info!("Network task initialized"); | ||
| 41 | |||
| 42 | // Then we can use it! | ||
| 43 | let mut rx_buffer = [0; 1024]; | ||
| 44 | let mut tx_buffer = [0; 1024]; | ||
| 45 | let mut socket = TcpSocket::new(&mut rx_buffer, &mut tx_buffer); | ||
| 46 | |||
| 47 | socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); | ||
| 48 | |||
| 49 | let remote_endpoint = (Ipv4Address::new(192, 168, 0, 10), 8000); | ||
| 50 | let r = socket.connect(remote_endpoint).await; | ||
| 51 | if let Err(e) = r { | ||
| 52 | info!("connect error: {:?}", e); | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | info!("connected!"); | ||
| 56 | loop { | ||
| 57 | let r = socket.write_all(b"Hello\n").await; | ||
| 58 | if let Err(e) = r { | ||
| 59 | info!("write error: {:?}", e); | ||
| 60 | return; | ||
| 61 | } | ||
| 62 | Timer::after(Duration::from_secs(1)).await; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | #[embassy::task] | ||
| 67 | async fn net_task() { | ||
| 68 | embassy_net::run().await | ||
| 69 | } | 30 | } |
| 70 | 31 | ||
| 71 | #[no_mangle] | 32 | type Device = Ethernet<'static, ETH, GenericSMI, 4, 4>; |
| 72 | fn _embassy_rand(buf: &mut [u8]) { | ||
| 73 | use rand_core::RngCore; | ||
| 74 | 33 | ||
| 75 | critical_section::with(|_| unsafe { | 34 | #[embassy::task] |
| 76 | unwrap!(RNG_INST.as_mut()).fill_bytes(buf); | 35 | async fn net_task(stack: &'static Stack<Device>) -> ! { |
| 77 | }); | 36 | stack.run().await |
| 78 | } | 37 | } |
| 79 | 38 | ||
| 80 | static mut RNG_INST: Option<Rng<RNG>> = None; | ||
| 81 | |||
| 82 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 83 | static STATE: Forever<State<'static, ETH, 4, 4>> = Forever::new(); | ||
| 84 | static ETH: Forever<Ethernet<'static, ETH, GenericSMI, 4, 4>> = Forever::new(); | ||
| 85 | static CONFIG: Forever<StaticConfigurator> = Forever::new(); | ||
| 86 | static NET_RESOURCES: Forever<StackResources<1, 2, 8>> = Forever::new(); | ||
| 87 | |||
| 88 | fn config() -> Config { | 39 | fn config() -> Config { |
| 89 | let mut config = Config::default(); | 40 | let mut config = Config::default(); |
| 90 | config.rcc.sys_ck = Some(200.mhz().into()); | 41 | config.rcc.sys_ck = Some(200.mhz().into()); |
| 91 | config | 42 | config |
| 92 | } | 43 | } |
| 93 | 44 | ||
| 94 | #[entry] | 45 | #[embassy::main(config = "config()")] |
| 95 | fn main() -> ! { | 46 | async fn main(spawner: Spawner, p: Peripherals) -> ! { |
| 96 | info!("Hello World!"); | 47 | info!("Hello World!"); |
| 97 | 48 | ||
| 98 | info!("Setup RCC..."); | 49 | // Generate random seed. |
| 99 | 50 | let mut rng = Rng::new(p.RNG); | |
| 100 | let p = embassy_stm32::init(config()); | 51 | let mut seed = [0; 8]; |
| 101 | 52 | rng.fill_bytes(&mut seed); | |
| 102 | let rng = Rng::new(p.RNG); | 53 | let seed = u64::from_le_bytes(seed); |
| 103 | unsafe { | ||
| 104 | RNG_INST.replace(rng); | ||
| 105 | } | ||
| 106 | 54 | ||
| 107 | let eth_int = interrupt::take!(ETH); | 55 | let eth_int = interrupt::take!(ETH); |
| 108 | let mac_addr = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF]; | 56 | let mac_addr = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF]; |
| 109 | let state = STATE.put(State::new()); | ||
| 110 | 57 | ||
| 111 | let eth = unsafe { | 58 | let device = unsafe { |
| 112 | ETH.put(Ethernet::new( | 59 | Ethernet::new( |
| 113 | state, p.ETH, eth_int, p.PA1, p.PA2, p.PC1, p.PA7, p.PC4, p.PC5, p.PG13, p.PB13, | 60 | forever!(State::new()), |
| 114 | p.PG11, GenericSMI, mac_addr, 0, | 61 | p.ETH, |
| 115 | )) | 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 | ) | ||
| 116 | }; | 76 | }; |
| 117 | 77 | ||
| 118 | let config = StaticConfigurator::new(NetConfig { | 78 | let config = embassy_net::ConfigStrategy::Dhcp; |
| 119 | address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 0, 61), 24), | 79 | //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { |
| 120 | dns_servers: Vec::new(), | 80 | // address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24), |
| 121 | gateway: Some(Ipv4Address::new(192, 168, 0, 1)), | 81 | // dns_servers: Vec::new(), |
| 122 | }); | 82 | // gateway: Some(Ipv4Address::new(10, 42, 0, 1)), |
| 83 | //}); | ||
| 123 | 84 | ||
| 124 | let config = CONFIG.put(config); | 85 | // Init network stack |
| 86 | let stack = &*forever!(Stack::new( | ||
| 87 | device, | ||
| 88 | config, | ||
| 89 | forever!(StackResources::<1, 2, 8>::new()), | ||
| 90 | seed | ||
| 91 | )); | ||
| 125 | 92 | ||
| 126 | let executor = EXECUTOR.put(Executor::new()); | 93 | // Launch network task |
| 94 | unwrap!(spawner.spawn(net_task(&stack))); | ||
| 127 | 95 | ||
| 128 | executor.run(move |spawner| { | 96 | info!("Network task initialized"); |
| 129 | unwrap!(spawner.spawn(main_task(eth, config, spawner))); | 97 | |
| 130 | }) | 98 | // Then we can use it! |
| 99 | let mut rx_buffer = [0; 1024]; | ||
| 100 | let mut tx_buffer = [0; 1024]; | ||
| 101 | |||
| 102 | loop { | ||
| 103 | let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer); | ||
| 104 | |||
| 105 | socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); | ||
| 106 | |||
| 107 | let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000); | ||
| 108 | info!("connecting..."); | ||
| 109 | let r = socket.connect(remote_endpoint).await; | ||
| 110 | if let Err(e) = r { | ||
| 111 | info!("connect error: {:?}", e); | ||
| 112 | continue; | ||
| 113 | } | ||
| 114 | info!("connected!"); | ||
| 115 | loop { | ||
| 116 | let r = socket.write_all(b"Hello\n").await; | ||
| 117 | if let Err(e) = r { | ||
| 118 | info!("write error: {:?}", e); | ||
| 119 | return; | ||
| 120 | } | ||
| 121 | Timer::after(Duration::from_secs(1)).await; | ||
| 122 | } | ||
| 123 | } | ||
| 131 | } | 124 | } |
