diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-11-10 22:07:38 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-11-10 22:07:38 +0000 |
| commit | 96e2f0dfc59d2e99b10cc2dd78ea0e4c16a1e15e (patch) | |
| tree | 76355ee4bd3756613c267636783e3138ba4c196a /examples | |
| parent | fc75e0ec573267d7b97698ea140e1533e356e012 (diff) | |
| parent | f0ba79059eea9a2c4f946d86e9e78136bdf99790 (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.toml | 18 | ||||
| -rw-r--r-- | examples/stm32f7/src/bin/eth.rs | 128 | ||||
| -rw-r--r-- | examples/stm32f7/src/example_common.rs | 12 |
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] |
| 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", "stm32f767zi", "unstable-pac", "time-driver-tim2"] } | 22 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "net", "stm32f767zi", "unstable-pac", "time-driver-tim2"] } |
| 23 | embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } | 23 | embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } |
| 24 | embassy-net = { path = "../../embassy-net", default-features = false, features = ["defmt-debug", "defmt", "tcp", "medium-ethernet", "pool-16"] } | ||
| 25 | embassy-macros = { path = "../../embassy-macros" } | ||
| 24 | 26 | ||
| 25 | defmt = "0.2.3" | 27 | defmt = "0.2.3" |
| 26 | defmt-rtt = "0.2.0" | 28 | defmt-rtt = "0.2.0" |
| @@ -33,3 +35,17 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa | |||
| 33 | rtt-target = { version = "0.3.1", features = ["cortex-m"] } | 35 | rtt-target = { version = "0.3.1", features = ["cortex-m"] } |
| 34 | heapless = { version = "0.7.5", default-features = false } | 36 | heapless = { version = "0.7.5", default-features = false } |
| 35 | nb = "1.0.0" | 37 | nb = "1.0.0" |
| 38 | rand_core = "0.6.3" | ||
| 39 | critical-section = "0.2.3" | ||
| 40 | |||
| 41 | |||
| 42 | [dependencies.smoltcp] | ||
| 43 | git = "https://github.com/smoltcp-rs/smoltcp" | ||
| 44 | rev = "e4241510337e095b9d21136c5f58b2eaa1b78479" | ||
| 45 | default-features = false | ||
| 46 | features = [ | ||
| 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"] | ||
| 6 | mod example_common; | ||
| 7 | use example_common::config; | ||
| 8 | |||
| 9 | use cortex_m_rt::entry; | ||
| 10 | use defmt::{info, unwrap}; | ||
| 11 | use defmt_rtt as _; // global logger | ||
| 12 | use embassy::executor::{Executor, Spawner}; | ||
| 13 | use embassy::io::AsyncWriteExt; | ||
| 14 | use embassy::time::{Duration, Timer}; | ||
| 15 | use embassy::util::Forever; | ||
| 16 | use embassy_macros::interrupt_take; | ||
| 17 | use embassy_net::{ | ||
| 18 | Config as NetConfig, Ipv4Address, Ipv4Cidr, StackResources, StaticConfigurator, TcpSocket, | ||
| 19 | }; | ||
| 20 | use embassy_stm32::eth::lan8742a::LAN8742A; | ||
| 21 | use embassy_stm32::eth::{Ethernet, State}; | ||
| 22 | use embassy_stm32::rng::Rng; | ||
| 23 | use embassy_stm32::{interrupt, peripherals}; | ||
| 24 | use heapless::Vec; | ||
| 25 | use panic_probe as _; | ||
| 26 | |||
| 27 | use peripherals::RNG; | ||
| 28 | |||
| 29 | #[embassy::task] | ||
| 30 | async 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] | ||
| 70 | async fn net_task() { | ||
| 71 | embassy_net::run().await | ||
| 72 | } | ||
| 73 | |||
| 74 | #[no_mangle] | ||
| 75 | fn _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 | |||
| 83 | static mut RNG_INST: Option<Rng<RNG>> = None; | ||
| 84 | |||
| 85 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 86 | static STATE: Forever<State<'static, 4, 4>> = Forever::new(); | ||
| 87 | static ETH: Forever<Ethernet<'static, LAN8742A, 4, 4>> = Forever::new(); | ||
| 88 | static CONFIG: Forever<StaticConfigurator> = Forever::new(); | ||
| 89 | static NET_RESOURCES: Forever<StackResources<1, 2, 8>> = Forever::new(); | ||
| 90 | |||
| 91 | #[entry] | ||
| 92 | fn 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 | ||
| 3 | use defmt_rtt as _; // global logger | 3 | use defmt_rtt as _; |
| 4 | use embassy_stm32::time::U32Ext; | ||
| 5 | use embassy_stm32::Config; | ||
| 6 | // global logger | ||
| 4 | use panic_probe as _; | 7 | use panic_probe as _; |
| 5 | 8 | ||
| 6 | pub use defmt::*; | 9 | pub 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)] | ||
| 23 | pub fn config() -> Config { | ||
| 24 | let mut config = Config::default(); | ||
| 25 | config.rcc.sys_ck = Some(200.mhz().into()); | ||
| 26 | config | ||
| 27 | } | ||
