diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-06-16 16:55:39 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-06-16 16:55:39 +0200 |
| commit | 0d1ae0a01e18e19cc8f33fd184785c19f18e1277 (patch) | |
| tree | 79e3055dc8b04c2e9b4db45d0561e97a1eacc0e2 /examples | |
| parent | 6386c34079913732466046194f79a683a4aefce4 (diff) | |
| parent | 098ce6e7404f10aa6c7dd91260ca6864a92c6a59 (diff) | |
Merge pull request #247 from thalesfragoso/eth-v2
Eth v2
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32h7/.cargo/config | 10 | ||||
| -rw-r--r-- | examples/stm32h7/Cargo.toml | 17 | ||||
| -rw-r--r-- | examples/stm32h7/build.rs | 21 | ||||
| -rw-r--r-- | examples/stm32h7/memory.x | 2 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/eth.rs | 167 |
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] | ||
| 2 | runner = 'probe-run --chip STM32H743ZITx' | ||
| 3 | rustflags = [ | ||
| 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] | ||
| 10 | target = "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] |
| 20 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } | 20 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } |
| 21 | embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } | 21 | embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } |
| 22 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32h743zi"] } | 22 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32h743zi", "net"] } |
| 23 | embassy-extras = {version = "0.1.0", path = "../../embassy-extras" } | 23 | embassy-extras = {version = "0.1.0", path = "../../embassy-extras" } |
| 24 | embassy-net = { path = "../../embassy-net", default-features = false, features = ["defmt-debug", "defmt", "tcp", "medium-ethernet", "pool-16"] } | ||
| 25 | stm32-metapac = { path = "../../stm32-metapac", features = ["stm32h743zi"] } | ||
| 26 | embassy-macros = { path = "../../embassy-macros" } | ||
| 24 | stm32h7 = { version = "0.13", features = ["stm32h743"]} | 27 | stm32h7 = { version = "0.13", features = ["stm32h743"]} |
| 25 | stm32h7xx-hal = { version = "0.9.0", features = ["stm32h743"] } | 28 | stm32h7xx-hal = { version = "0.9.0", features = ["stm32h743"] } |
| 26 | 29 | ||
| @@ -34,6 +37,18 @@ panic-probe = { version = "0.2.0", features= ["print-defmt"] } | |||
| 34 | futures = { version = "0.3.8", default-features = false, features = ["async-await"] } | 37 | futures = { version = "0.3.8", default-features = false, features = ["async-await"] } |
| 35 | rtt-target = { version = "0.3", features = ["cortex-m"] } | 38 | rtt-target = { version = "0.3", features = ["cortex-m"] } |
| 36 | heapless = { version = "0.7.1", default-features = false } | 39 | heapless = { version = "0.7.1", default-features = false } |
| 40 | rand_core = { version = "0.6.2" } | ||
| 41 | critical-section = "0.2.1" | ||
| 37 | 42 | ||
| 38 | micromath = "2.0.0" | 43 | micromath = "2.0.0" |
| 39 | 44 | ||
| 45 | [dependencies.smoltcp] | ||
| 46 | git = "https://github.com/smoltcp-rs/smoltcp" | ||
| 47 | rev = "e4241510337e095b9d21136c5f58b2eaa1b78479" | ||
| 48 | default-features = false | ||
| 49 | features = [ | ||
| 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 @@ | |||
| 1 | use std::env; | ||
| 2 | use std::fs::File; | ||
| 3 | use std::io::Write; | ||
| 4 | use std::path::PathBuf; | ||
| 5 | |||
| 6 | fn 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 @@ | |||
| 1 | MEMORY | 1 | MEMORY |
| 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 | |||
| 8 | use core::pin::Pin; | ||
| 9 | use core::sync::atomic::{AtomicUsize, Ordering}; | ||
| 10 | |||
| 11 | use cortex_m_rt::entry; | ||
| 12 | use defmt::{info, unwrap}; | ||
| 13 | use defmt_rtt as _; // global logger | ||
| 14 | use embassy::executor::{Executor, Spawner}; | ||
| 15 | use embassy::io::AsyncWriteExt; | ||
| 16 | use embassy::time::{Duration, Timer}; | ||
| 17 | use embassy::util::Forever; | ||
| 18 | use embassy_macros::interrupt_take; | ||
| 19 | use embassy_net::{Config as NetConfig, Ipv4Address, Ipv4Cidr, StaticConfigurator, TcpSocket}; | ||
| 20 | use embassy_stm32::clock::{Alarm, Clock}; | ||
| 21 | use embassy_stm32::eth::lan8742a::LAN8742A; | ||
| 22 | use embassy_stm32::eth::Ethernet; | ||
| 23 | use embassy_stm32::rcc::{Config as RccConfig, Rcc}; | ||
| 24 | use embassy_stm32::rng::Random; | ||
| 25 | use embassy_stm32::time::Hertz; | ||
| 26 | use embassy_stm32::{interrupt, peripherals, Config}; | ||
| 27 | use heapless::Vec; | ||
| 28 | use panic_probe as _; | ||
| 29 | use peripherals::{RNG, TIM2}; | ||
| 30 | |||
| 31 | defmt::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] | ||
| 41 | async 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] | ||
| 79 | async fn net_task() { | ||
| 80 | embassy_net::run().await | ||
| 81 | } | ||
| 82 | |||
| 83 | #[no_mangle] | ||
| 84 | fn _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 | |||
| 92 | static mut RNG_INST: Option<Random<RNG>> = None; | ||
| 93 | |||
| 94 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 95 | static TIMER_RTC: Forever<Clock<TIM2>> = Forever::new(); | ||
| 96 | static ALARM: Forever<Alarm<TIM2>> = Forever::new(); | ||
| 97 | static ETH: Forever<Ethernet<'static, LAN8742A, 4, 4>> = Forever::new(); | ||
| 98 | static DEVICE: Forever<Pin<&'static mut Ethernet<'static, LAN8742A, 4, 4>>> = Forever::new(); | ||
| 99 | static CONFIG: Forever<StaticConfigurator> = Forever::new(); | ||
| 100 | |||
| 101 | #[entry] | ||
| 102 | fn 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 | } | ||
