2021-06-15 21:52:43 +00:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
2021-07-03 12:05:12 +00:00
|
|
|
#![allow(incomplete_features)]
|
2021-06-15 21:52:43 +00:00
|
|
|
#![feature(trait_alias)]
|
|
|
|
#![feature(min_type_alias_impl_trait)]
|
|
|
|
#![feature(impl_trait_in_bindings)]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use core::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
|
|
|
|
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;
|
|
|
|
use embassy_macros::interrupt_take;
|
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::clock::{Alarm, Clock};
|
|
|
|
use embassy_stm32::eth::lan8742a::LAN8742A;
|
2021-07-29 12:08:32 +00:00
|
|
|
use embassy_stm32::eth::{Ethernet, State};
|
2021-08-04 15:32:39 +00:00
|
|
|
use embassy_stm32::rcc::{self, Rcc};
|
2021-06-15 21:52:43 +00:00
|
|
|
use embassy_stm32::rng::Random;
|
2021-08-04 15:32:39 +00:00
|
|
|
use embassy_stm32::time::U32Ext;
|
2021-06-15 21:52:43 +00:00
|
|
|
use embassy_stm32::{interrupt, peripherals, Config};
|
|
|
|
use heapless::Vec;
|
|
|
|
use panic_probe as _;
|
|
|
|
use peripherals::{RNG, TIM2};
|
|
|
|
|
|
|
|
defmt::timestamp! {"{=u64}", {
|
|
|
|
static COUNT: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
// NOTE(no-CAS) `timestamps` runs with interrupts disabled
|
|
|
|
let n = COUNT.load(Ordering::Relaxed);
|
|
|
|
COUNT.store(n + 1, Ordering::Relaxed);
|
|
|
|
n as u64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy::task]
|
|
|
|
async fn main_task(
|
2021-07-29 12:08:32 +00:00
|
|
|
device: &'static mut Ethernet<'static, 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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static mut RNG_INST: Option<Random<RNG>> = None;
|
|
|
|
|
|
|
|
static EXECUTOR: Forever<Executor> = Forever::new();
|
|
|
|
static TIMER_RTC: Forever<Clock<TIM2>> = Forever::new();
|
|
|
|
static ALARM: Forever<Alarm<TIM2>> = Forever::new();
|
2021-07-29 12:08:32 +00:00
|
|
|
static STATE: Forever<State<'static, 4, 4>> = Forever::new();
|
2021-06-15 21:52:43 +00:00
|
|
|
static ETH: Forever<Ethernet<'static, LAN8742A, 4, 4>> = Forever::new();
|
|
|
|
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 15:32:39 +00:00
|
|
|
let mut p = embassy_stm32::init(config());
|
2021-06-15 21:52:43 +00:00
|
|
|
|
|
|
|
// Constrain and Freeze clock
|
|
|
|
|
2021-08-04 15:32:39 +00:00
|
|
|
let mut rcc = Rcc::new(&mut p.RCC, rcc::Config::default());
|
2021-06-15 21:52:43 +00:00
|
|
|
rcc.enable_debug_wfe(&mut p.DBGMCU, true);
|
|
|
|
|
|
|
|
let rtc_int = interrupt_take!(TIM2);
|
|
|
|
let rtc = TIMER_RTC.put(Clock::new(p.TIM2, rtc_int));
|
|
|
|
rtc.start();
|
|
|
|
let alarm = ALARM.put(rtc.alarm1());
|
|
|
|
|
|
|
|
unsafe { embassy::time::set_clock(rtc) };
|
|
|
|
|
|
|
|
let rng = Random::new(p.RNG);
|
|
|
|
unsafe {
|
|
|
|
RNG_INST.replace(rng);
|
|
|
|
}
|
|
|
|
|
|
|
|
let eth_int = interrupt_take!(ETH);
|
|
|
|
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(
|
|
|
|
state, p.ETH, eth_int, p.PA1, p.PA2, p.PC1, p.PA7, p.PC4, p.PC5, p.PB12, p.PB13,
|
|
|
|
p.PB11, LAN8742A, mac_addr, 1,
|
|
|
|
))
|
|
|
|
};
|
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.set_alarm(alarm);
|
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
2021-08-04 15:32:39 +00:00
|
|
|
|
|
|
|
fn config() -> Config {
|
|
|
|
let mut config = Config::default();
|
|
|
|
config.rcc = rcc_config();
|
|
|
|
config
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rcc_config() -> rcc::Config {
|
|
|
|
let mut config = rcc::Config::default();
|
|
|
|
config.sys_ck = Some(400.mhz().into());
|
2021-08-04 15:35:18 +00:00
|
|
|
config.pll1.q_ck = Some(100.mhz().into());
|
2021-08-04 15:32:39 +00:00
|
|
|
config
|
2021-08-04 15:35:18 +00:00
|
|
|
}
|