aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorThales Fragoso <[email protected]>2021-06-15 18:52:43 -0300
committerDario Nieuwenhuis <[email protected]>2021-06-16 16:48:35 +0200
commit098ce6e7404f10aa6c7dd91260ca6864a92c6a59 (patch)
tree79e3055dc8b04c2e9b4db45d0561e97a1eacc0e2 /examples
parent77546825a15abb8e2fd6956adc4a5f125a0cb8cf (diff)
stm32h7: Add ethernet example
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32h7/.cargo/config10
-rw-r--r--examples/stm32h7/Cargo.toml17
-rw-r--r--examples/stm32h7/build.rs21
-rw-r--r--examples/stm32h7/memory.x2
-rw-r--r--examples/stm32h7/src/bin/eth.rs167
5 files changed, 215 insertions, 2 deletions
diff --git a/examples/stm32h7/.cargo/config b/examples/stm32h7/.cargo/config
new file mode 100644
index 000000000..6b6a9f506
--- /dev/null
+++ b/examples/stm32h7/.cargo/config
@@ -0,0 +1,10 @@
1[target.thumbv7em-none-eabihf]
2runner = 'probe-run --chip STM32H743ZITx'
3rustflags = [
4 # LLD (shipped with the Rust toolchain) is used as the default linker
5 "-C", "link-arg=-Tlink.x",
6 "-C", "link-arg=-Tdefmt.x",
7]
8
9[build]
10target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml
index d7288e4c7..ebaa4e5db 100644
--- a/examples/stm32h7/Cargo.toml
+++ b/examples/stm32h7/Cargo.toml
@@ -19,8 +19,11 @@ 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", "stm32h743zi"] } 22embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32h743zi", "net"] }
23embassy-extras = {version = "0.1.0", path = "../../embassy-extras" } 23embassy-extras = {version = "0.1.0", path = "../../embassy-extras" }
24embassy-net = { path = "../../embassy-net", default-features = false, features = ["defmt-debug", "defmt", "tcp", "medium-ethernet", "pool-16"] }
25stm32-metapac = { path = "../../stm32-metapac", features = ["stm32h743zi"] }
26embassy-macros = { path = "../../embassy-macros" }
24stm32h7 = { version = "0.13", features = ["stm32h743"]} 27stm32h7 = { version = "0.13", features = ["stm32h743"]}
25stm32h7xx-hal = { version = "0.9.0", features = ["stm32h743"] } 28stm32h7xx-hal = { version = "0.9.0", features = ["stm32h743"] }
26 29
@@ -34,6 +37,18 @@ panic-probe = { version = "0.2.0", features= ["print-defmt"] }
34futures = { version = "0.3.8", default-features = false, features = ["async-await"] } 37futures = { version = "0.3.8", default-features = false, features = ["async-await"] }
35rtt-target = { version = "0.3", features = ["cortex-m"] } 38rtt-target = { version = "0.3", features = ["cortex-m"] }
36heapless = { version = "0.7.1", default-features = false } 39heapless = { version = "0.7.1", default-features = false }
40rand_core = { version = "0.6.2" }
41critical-section = "0.2.1"
37 42
38micromath = "2.0.0" 43micromath = "2.0.0"
39 44
45[dependencies.smoltcp]
46git = "https://github.com/smoltcp-rs/smoltcp"
47rev = "e4241510337e095b9d21136c5f58b2eaa1b78479"
48default-features = false
49features = [
50 "proto-ipv4",
51 "socket",
52 "async",
53 "defmt",
54]
diff --git a/examples/stm32h7/build.rs b/examples/stm32h7/build.rs
new file mode 100644
index 000000000..555cdf687
--- /dev/null
+++ b/examples/stm32h7/build.rs
@@ -0,0 +1,21 @@
1use std::env;
2use std::fs::File;
3use std::io::Write;
4use std::path::PathBuf;
5
6fn main() {
7 // Put `memory.x` in our output directory and ensure it's
8 // on the linker search path.
9 let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
10 File::create(out.join("memory.x"))
11 .unwrap()
12 .write_all(include_bytes!("memory.x"))
13 .unwrap();
14 println!("cargo:rustc-link-search={}", out.display());
15
16 // By default, Cargo will re-run a build script whenever
17 // any file in the project changes. By specifying `memory.x`
18 // here, we ensure the build script is only re-run when
19 // `memory.x` is changed.
20 println!("cargo:rerun-if-changed=memory.x");
21}
diff --git a/examples/stm32h7/memory.x b/examples/stm32h7/memory.x
index 48f58e36b..ef9485d12 100644
--- a/examples/stm32h7/memory.x
+++ b/examples/stm32h7/memory.x
@@ -1,5 +1,5 @@
1MEMORY 1MEMORY
2{ 2{
3 FLASH : ORIGIN = 0x08000000, LENGTH = 2048K 3 FLASH : ORIGIN = 0x08000000, LENGTH = 2048K
4 RAM : ORIGIN = 0x20000000, LENGTH = 128K 4 RAM : ORIGIN = 0x24000000, LENGTH = 384K
5} 5}
diff --git a/examples/stm32h7/src/bin/eth.rs b/examples/stm32h7/src/bin/eth.rs
new file mode 100644
index 000000000..57997da0a
--- /dev/null
+++ b/examples/stm32h7/src/bin/eth.rs
@@ -0,0 +1,167 @@
1#![no_std]
2#![no_main]
3#![feature(trait_alias)]
4#![feature(min_type_alias_impl_trait)]
5#![feature(impl_trait_in_bindings)]
6#![feature(type_alias_impl_trait)]
7
8use core::pin::Pin;
9use core::sync::atomic::{AtomicUsize, Ordering};
10
11use cortex_m_rt::entry;
12use defmt::{info, unwrap};
13use defmt_rtt as _; // global logger
14use embassy::executor::{Executor, Spawner};
15use embassy::io::AsyncWriteExt;
16use embassy::time::{Duration, Timer};
17use embassy::util::Forever;
18use embassy_macros::interrupt_take;
19use embassy_net::{Config as NetConfig, Ipv4Address, Ipv4Cidr, StaticConfigurator, TcpSocket};
20use embassy_stm32::clock::{Alarm, Clock};
21use embassy_stm32::eth::lan8742a::LAN8742A;
22use embassy_stm32::eth::Ethernet;
23use embassy_stm32::rcc::{Config as RccConfig, Rcc};
24use embassy_stm32::rng::Random;
25use embassy_stm32::time::Hertz;
26use embassy_stm32::{interrupt, peripherals, Config};
27use heapless::Vec;
28use panic_probe as _;
29use peripherals::{RNG, TIM2};
30
31defmt::timestamp! {"{=u64}", {
32 static COUNT: AtomicUsize = AtomicUsize::new(0);
33 // NOTE(no-CAS) `timestamps` runs with interrupts disabled
34 let n = COUNT.load(Ordering::Relaxed);
35 COUNT.store(n + 1, Ordering::Relaxed);
36 n as u64
37 }
38}
39
40#[embassy::task]
41async fn main_task(
42 device: &'static mut Pin<&'static mut Ethernet<'static, LAN8742A, 4, 4>>,
43 config: &'static mut StaticConfigurator,
44 spawner: Spawner,
45) {
46 // Init network stack
47 embassy_net::init(device, config);
48
49 // Launch network task
50 unwrap!(spawner.spawn(net_task()));
51
52 info!("Network task initialized");
53
54 // Then we can use it!
55 let mut rx_buffer = [0; 1024];
56 let mut tx_buffer = [0; 1024];
57 let mut socket = TcpSocket::new(&mut rx_buffer, &mut tx_buffer);
58
59 socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10)));
60
61 let remote_endpoint = (Ipv4Address::new(192, 168, 0, 10), 8000);
62 let r = socket.connect(remote_endpoint).await;
63 if let Err(e) = r {
64 info!("connect error: {:?}", e);
65 return;
66 }
67 info!("connected!");
68 loop {
69 let r = socket.write_all(b"Hello\n").await;
70 if let Err(e) = r {
71 info!("write error: {:?}", e);
72 return;
73 }
74 Timer::after(Duration::from_secs(1)).await;
75 }
76}
77
78#[embassy::task]
79async fn net_task() {
80 embassy_net::run().await
81}
82
83#[no_mangle]
84fn _embassy_rand(buf: &mut [u8]) {
85 use rand_core::RngCore;
86
87 critical_section::with(|_| unsafe {
88 unwrap!(RNG_INST.as_mut()).fill_bytes(buf);
89 });
90}
91
92static mut RNG_INST: Option<Random<RNG>> = None;
93
94static EXECUTOR: Forever<Executor> = Forever::new();
95static TIMER_RTC: Forever<Clock<TIM2>> = Forever::new();
96static ALARM: Forever<Alarm<TIM2>> = Forever::new();
97static ETH: Forever<Ethernet<'static, LAN8742A, 4, 4>> = Forever::new();
98static DEVICE: Forever<Pin<&'static mut Ethernet<'static, LAN8742A, 4, 4>>> = Forever::new();
99static CONFIG: Forever<StaticConfigurator> = Forever::new();
100
101#[entry]
102fn main() -> ! {
103 use stm32_metapac::RCC;
104
105 info!("Hello World!");
106
107 info!("Setup RCC...");
108 let mut rcc_config = RccConfig::default();
109 rcc_config.sys_ck = Some(Hertz(400_000_000));
110 rcc_config.pll1.q_ck = Some(Hertz(100_000_000));
111 let config = Config::default().rcc(rcc_config);
112
113 let mut p = embassy_stm32::init(config);
114
115 // Constrain and Freeze clock
116
117 let mut rcc = Rcc::new(&mut p.RCC, RccConfig::default());
118 rcc.enable_debug_wfe(&mut p.DBGMCU, true);
119
120 unsafe {
121 RCC.ahb4enr().modify(|w| {
122 w.set_gpioaen(true);
123 w.set_gpioben(true);
124 w.set_gpiocen(true);
125 w.set_gpioden(true);
126 w.set_gpioien(true);
127 });
128 }
129
130 let rtc_int = interrupt_take!(TIM2);
131 let rtc = TIMER_RTC.put(Clock::new(p.TIM2, rtc_int));
132 rtc.start();
133 let alarm = ALARM.put(rtc.alarm1());
134
135 unsafe { embassy::time::set_clock(rtc) };
136
137 let rng = Random::new(p.RNG);
138 unsafe {
139 RNG_INST.replace(rng);
140 }
141
142 let eth_int = interrupt_take!(ETH);
143 let mac_addr = [0x10; 6];
144 let eth = ETH.put(Ethernet::new(
145 p.ETH, eth_int, p.PA1, p.PA2, p.PC1, p.PA7, p.PC4, p.PC5, p.PB12, p.PB13, p.PB11, LAN8742A,
146 mac_addr, 1,
147 ));
148
149 // NOTE(unsafe) This thing is a &'static
150 let net_device = DEVICE.put(unsafe { Pin::new_unchecked(eth) });
151 net_device.as_mut().init();
152
153 let config = StaticConfigurator::new(NetConfig {
154 address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 0, 61), 24),
155 dns_servers: Vec::new(),
156 gateway: Some(Ipv4Address::new(192, 168, 0, 1)),
157 });
158
159 let config = CONFIG.put(config);
160
161 let executor = EXECUTOR.put(Executor::new());
162 executor.set_alarm(alarm);
163
164 executor.run(move |spawner| {
165 unwrap!(spawner.spawn(main_task(net_device, config, spawner)));
166 })
167}