diff options
| author | Matous Hybl <[email protected]> | 2021-10-28 14:22:02 +0200 |
|---|---|---|
| committer | Matous Hybl <[email protected]> | 2021-11-10 10:16:46 +0100 |
| commit | f0ba79059eea9a2c4f946d86e9e78136bdf99790 (patch) | |
| tree | df1169012ddf2002ff7da53c47c78c6466b0e0f3 /examples/stm32f7/src/bin/eth.rs | |
| parent | db889da0446833ff219e652bd68c397af858b999 (diff) | |
Add v1c ethernet driver for the STM32F7 family.
Diffstat (limited to 'examples/stm32f7/src/bin/eth.rs')
| -rw-r--r-- | examples/stm32f7/src/bin/eth.rs | 128 |
1 files changed, 128 insertions, 0 deletions
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 | } | ||
