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