2021-06-15 21:52:43 +00:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
2021-08-04 17:39:02 +00:00
|
|
|
#[path = "../example_common.rs"]
|
|
|
|
mod example_common;
|
2022-02-10 20:38:03 +00:00
|
|
|
use embassy_stm32::peripherals::ETH;
|
2021-08-04 17:39:02 +00:00
|
|
|
use example_common::config;
|
2021-06-15 21:52:43 +00:00
|
|
|
|
|
|
|
use cortex_m_rt::entry;
|
|
|
|
use defmt::{info, unwrap};
|
|
|
|
use defmt_rtt as _; // global logger
|
|
|
|
use embassy::executor::{Executor, Spawner};
|
|
|
|
use embassy::io::AsyncWriteExt;
|
|
|
|
use embassy::time::{Duration, Timer};
|
|
|
|
use embassy::util::Forever;
|
2021-06-20 19:46:26 +00:00
|
|
|
use embassy_net::{
|
|
|
|
Config as NetConfig, Ipv4Address, Ipv4Cidr, StackResources, StaticConfigurator, TcpSocket,
|
|
|
|
};
|
2021-06-15 21:52:43 +00:00
|
|
|
use embassy_stm32::eth::lan8742a::LAN8742A;
|
2021-07-29 12:08:32 +00:00
|
|
|
use embassy_stm32::eth::{Ethernet, State};
|
2021-09-01 13:39:33 +00:00
|
|
|
use embassy_stm32::rng::Rng;
|
2021-08-04 17:39:02 +00:00
|
|
|
use embassy_stm32::{interrupt, peripherals};
|
2021-06-15 21:52:43 +00:00
|
|
|
use heapless::Vec;
|
|
|
|
use panic_probe as _;
|
2021-08-03 20:08:13 +00:00
|
|
|
use peripherals::RNG;
|
2021-06-15 21:52:43 +00:00
|
|
|
|
|
|
|
#[embassy::task]
|
|
|
|
async fn main_task(
|
2022-02-10 20:38:03 +00:00
|
|
|
device: &'static mut Ethernet<'static, ETH, LAN8742A, 4, 4>,
|
2021-06-15 21:52:43 +00:00
|
|
|
config: &'static mut StaticConfigurator,
|
|
|
|
spawner: Spawner,
|
|
|
|
) {
|
2021-06-20 19:46:26 +00:00
|
|
|
let net_resources = NET_RESOURCES.put(StackResources::new());
|
|
|
|
|
2021-06-15 21:52:43 +00:00
|
|
|
// Init network stack
|
2021-06-20 19:46:26 +00:00
|
|
|
embassy_net::init(device, config, net_resources);
|
2021-06-15 21:52:43 +00:00
|
|
|
|
|
|
|
// Launch network task
|
|
|
|
unwrap!(spawner.spawn(net_task()));
|
|
|
|
|
|
|
|
info!("Network task initialized");
|
|
|
|
|
|
|
|
// Then we can use it!
|
|
|
|
let mut rx_buffer = [0; 1024];
|
|
|
|
let mut tx_buffer = [0; 1024];
|
|
|
|
let mut socket = TcpSocket::new(&mut rx_buffer, &mut tx_buffer);
|
|
|
|
|
|
|
|
socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10)));
|
|
|
|
|
|
|
|
let remote_endpoint = (Ipv4Address::new(192, 168, 0, 10), 8000);
|
|
|
|
let r = socket.connect(remote_endpoint).await;
|
|
|
|
if let Err(e) = r {
|
|
|
|
info!("connect error: {:?}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
info!("connected!");
|
|
|
|
loop {
|
|
|
|
let r = socket.write_all(b"Hello\n").await;
|
|
|
|
if let Err(e) = r {
|
|
|
|
info!("write error: {:?}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Timer::after(Duration::from_secs(1)).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy::task]
|
|
|
|
async fn net_task() {
|
|
|
|
embassy_net::run().await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
fn _embassy_rand(buf: &mut [u8]) {
|
|
|
|
use rand_core::RngCore;
|
|
|
|
|
|
|
|
critical_section::with(|_| unsafe {
|
|
|
|
unwrap!(RNG_INST.as_mut()).fill_bytes(buf);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-01 13:39:33 +00:00
|
|
|
static mut RNG_INST: Option<Rng<RNG>> = None;
|
2021-06-15 21:52:43 +00:00
|
|
|
|
|
|
|
static EXECUTOR: Forever<Executor> = Forever::new();
|
2022-02-10 20:38:03 +00:00
|
|
|
static STATE: Forever<State<'static, ETH, 4, 4>> = Forever::new();
|
|
|
|
static ETH: Forever<Ethernet<'static, ETH, LAN8742A, 4, 4>> = Forever::new();
|
2021-06-15 21:52:43 +00:00
|
|
|
static CONFIG: Forever<StaticConfigurator> = Forever::new();
|
2021-06-20 19:46:26 +00:00
|
|
|
static NET_RESOURCES: Forever<StackResources<1, 2, 8>> = Forever::new();
|
2021-06-15 21:52:43 +00:00
|
|
|
|
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
|
|
|
info!("Hello World!");
|
|
|
|
|
|
|
|
info!("Setup RCC...");
|
|
|
|
|
2021-08-04 17:39:02 +00:00
|
|
|
let p = embassy_stm32::init(config());
|
2021-06-15 21:52:43 +00:00
|
|
|
|
2021-09-01 13:39:33 +00:00
|
|
|
let rng = Rng::new(p.RNG);
|
2021-06-15 21:52:43 +00:00
|
|
|
unsafe {
|
|
|
|
RNG_INST.replace(rng);
|
|
|
|
}
|
|
|
|
|
2021-11-24 02:17:54 +00:00
|
|
|
let eth_int = interrupt::take!(ETH);
|
2021-06-15 21:52:43 +00:00
|
|
|
let mac_addr = [0x10; 6];
|
2021-07-29 12:08:32 +00:00
|
|
|
let state = STATE.put(State::new());
|
2021-08-02 10:40:01 +00:00
|
|
|
let eth = unsafe {
|
|
|
|
ETH.put(Ethernet::new(
|
2021-11-03 16:01:48 +00:00
|
|
|
state, p.ETH, eth_int, p.PA1, p.PA2, p.PC1, p.PA7, p.PC4, p.PC5, p.PG13, p.PB13,
|
|
|
|
p.PG11, LAN8742A, mac_addr, 0,
|
2021-08-02 10:40:01 +00:00
|
|
|
))
|
|
|
|
};
|
2021-06-15 21:52:43 +00:00
|
|
|
|
|
|
|
let config = StaticConfigurator::new(NetConfig {
|
|
|
|
address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 0, 61), 24),
|
|
|
|
dns_servers: Vec::new(),
|
|
|
|
gateway: Some(Ipv4Address::new(192, 168, 0, 1)),
|
|
|
|
});
|
|
|
|
|
|
|
|
let config = CONFIG.put(config);
|
|
|
|
|
|
|
|
let executor = EXECUTOR.put(Executor::new());
|
|
|
|
|
|
|
|
executor.run(move |spawner| {
|
2021-07-29 12:08:32 +00:00
|
|
|
unwrap!(spawner.spawn(main_task(eth, config, spawner)));
|
2021-06-15 21:52:43 +00:00
|
|
|
})
|
|
|
|
}
|