aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-11-10 22:07:38 +0000
committerGitHub <[email protected]>2021-11-10 22:07:38 +0000
commit96e2f0dfc59d2e99b10cc2dd78ea0e4c16a1e15e (patch)
tree76355ee4bd3756613c267636783e3138ba4c196a /examples
parentfc75e0ec573267d7b97698ea140e1533e356e012 (diff)
parentf0ba79059eea9a2c4f946d86e9e78136bdf99790 (diff)
Merge #468
468: Add v1c ethernet driver for the STM32F7 family. r=Dirbaio a=matoushybl Co-authored-by: Matous Hybl <[email protected]>
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32f7/Cargo.toml18
-rw-r--r--examples/stm32f7/src/bin/eth.rs128
-rw-r--r--examples/stm32f7/src/example_common.rs12
3 files changed, 156 insertions, 2 deletions
diff --git a/examples/stm32f7/Cargo.toml b/examples/stm32f7/Cargo.toml
index cda2d7778..f49a23e31 100644
--- a/examples/stm32f7/Cargo.toml
+++ b/examples/stm32f7/Cargo.toml
@@ -19,8 +19,10 @@ defmt-error = []
19[dependencies] 19[dependencies]
20embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } 20embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] }
21embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } 21embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] }
22embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32f767zi", "unstable-pac", "time-driver-tim2"] } 22embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "net", "stm32f767zi", "unstable-pac", "time-driver-tim2"] }
23embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } 23embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" }
24embassy-net = { path = "../../embassy-net", default-features = false, features = ["defmt-debug", "defmt", "tcp", "medium-ethernet", "pool-16"] }
25embassy-macros = { path = "../../embassy-macros" }
24 26
25defmt = "0.2.3" 27defmt = "0.2.3"
26defmt-rtt = "0.2.0" 28defmt-rtt = "0.2.0"
@@ -33,3 +35,17 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa
33rtt-target = { version = "0.3.1", features = ["cortex-m"] } 35rtt-target = { version = "0.3.1", features = ["cortex-m"] }
34heapless = { version = "0.7.5", default-features = false } 36heapless = { version = "0.7.5", default-features = false }
35nb = "1.0.0" 37nb = "1.0.0"
38rand_core = "0.6.3"
39critical-section = "0.2.3"
40
41
42[dependencies.smoltcp]
43git = "https://github.com/smoltcp-rs/smoltcp"
44rev = "e4241510337e095b9d21136c5f58b2eaa1b78479"
45default-features = false
46features = [
47 "proto-ipv4",
48 "socket",
49 "async",
50 "defmt",
51]
diff --git a/examples/stm32f7/src/bin/eth.rs b/examples/stm32f7/src/bin/eth.rs
new file mode 100644
index 000000000..afb860862
--- /dev/null
+++ b/examples/stm32f7/src/bin/eth.rs
@@ -0,0 +1,128 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5#[path = "../example_common.rs"]
6mod example_common;
7use example_common::config;
8
9use cortex_m_rt::entry;
10use defmt::{info, unwrap};
11use defmt_rtt as _; // global logger
12use embassy::executor::{Executor, Spawner};
13use embassy::io::AsyncWriteExt;
14use embassy::time::{Duration, Timer};
15use embassy::util::Forever;
16use embassy_macros::interrupt_take;
17use embassy_net::{
18 Config as NetConfig, Ipv4Address, Ipv4Cidr, StackResources, StaticConfigurator, TcpSocket,
19};
20use embassy_stm32::eth::lan8742a::LAN8742A;
21use embassy_stm32::eth::{Ethernet, State};
22use embassy_stm32::rng::Rng;
23use embassy_stm32::{interrupt, peripherals};
24use heapless::Vec;
25use panic_probe as _;
26
27use peripherals::RNG;
28
29#[embassy::task]
30async fn main_task(
31 device: &'static mut Ethernet<'static, LAN8742A, 4, 4>,
32 config: &'static mut StaticConfigurator,
33 spawner: Spawner,
34) {
35 let net_resources = NET_RESOURCES.put(StackResources::new());
36
37 // Init network stack
38 embassy_net::init(device, config, net_resources);
39
40 // Launch network task
41 unwrap!(spawner.spawn(net_task()));
42
43 info!("Network task initialized");
44
45 // Then we can use it!
46 let mut rx_buffer = [0; 1024];
47 let mut tx_buffer = [0; 1024];
48 let mut socket = TcpSocket::new(&mut rx_buffer, &mut tx_buffer);
49
50 socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10)));
51
52 let remote_endpoint = (Ipv4Address::new(192, 168, 0, 10), 8000);
53 let r = socket.connect(remote_endpoint).await;
54 if let Err(e) = r {
55 info!("connect error: {:?}", e);
56 return;
57 }
58 info!("connected!");
59 loop {
60 let r = socket.write_all(b"Hello\n").await;
61 if let Err(e) = r {
62 info!("write error: {:?}", e);
63 return;
64 }
65 Timer::after(Duration::from_secs(1)).await;
66 }
67}
68
69#[embassy::task]
70async fn net_task() {
71 embassy_net::run().await
72}
73
74#[no_mangle]
75fn _embassy_rand(buf: &mut [u8]) {
76 use rand_core::RngCore;
77
78 critical_section::with(|_| unsafe {
79 unwrap!(RNG_INST.as_mut()).fill_bytes(buf);
80 });
81}
82
83static mut RNG_INST: Option<Rng<RNG>> = None;
84
85static EXECUTOR: Forever<Executor> = Forever::new();
86static STATE: Forever<State<'static, 4, 4>> = Forever::new();
87static ETH: Forever<Ethernet<'static, LAN8742A, 4, 4>> = Forever::new();
88static CONFIG: Forever<StaticConfigurator> = Forever::new();
89static NET_RESOURCES: Forever<StackResources<1, 2, 8>> = Forever::new();
90
91#[entry]
92fn main() -> ! {
93 info!("Hello World!");
94
95 info!("Setup RCC...");
96
97 let p = embassy_stm32::init(config());
98
99 let rng = Rng::new(p.RNG);
100 unsafe {
101 RNG_INST.replace(rng);
102 }
103
104 let eth_int = interrupt_take!(ETH);
105 let mac_addr = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF];
106 let state = STATE.put(State::new());
107
108 let eth = unsafe {
109 ETH.put(Ethernet::new(
110 state, p.ETH, eth_int, p.PA1, p.PA2, p.PC1, p.PA7, p.PC4, p.PC5, p.PG13, p.PB13,
111 p.PG11, LAN8742A, mac_addr, 0,
112 ))
113 };
114
115 let config = StaticConfigurator::new(NetConfig {
116 address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 0, 61), 24),
117 dns_servers: Vec::new(),
118 gateway: Some(Ipv4Address::new(192, 168, 0, 1)),
119 });
120
121 let config = CONFIG.put(config);
122
123 let executor = EXECUTOR.put(Executor::new());
124
125 executor.run(move |spawner| {
126 unwrap!(spawner.spawn(main_task(eth, config, spawner)));
127 })
128}
diff --git a/examples/stm32f7/src/example_common.rs b/examples/stm32f7/src/example_common.rs
index 54d633837..a786cb114 100644
--- a/examples/stm32f7/src/example_common.rs
+++ b/examples/stm32f7/src/example_common.rs
@@ -1,6 +1,9 @@
1#![macro_use] 1#![macro_use]
2 2
3use defmt_rtt as _; // global logger 3use defmt_rtt as _;
4use embassy_stm32::time::U32Ext;
5use embassy_stm32::Config;
6// global logger
4use panic_probe as _; 7use panic_probe as _;
5 8
6pub use defmt::*; 9pub use defmt::*;
@@ -15,3 +18,10 @@ defmt::timestamp! {"{=u64}", {
15 n as u64 18 n as u64
16 } 19 }
17} 20}
21
22#[allow(unused)]
23pub fn config() -> Config {
24 let mut config = Config::default();
25 config.rcc.sys_ck = Some(200.mhz().into());
26 config
27}