2023-01-18 01:37:02 +01:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use defmt::*;
|
|
|
|
use embassy_executor::Spawner;
|
|
|
|
use embassy_net::tcp::TcpSocket;
|
|
|
|
use embassy_net::{Stack, StackResources};
|
2023-07-31 01:41:12 +02:00
|
|
|
use embassy_stm32::rng::{self, Rng};
|
2023-10-18 04:31:53 +02:00
|
|
|
use embassy_stm32::time::Hertz;
|
2023-01-18 01:37:02 +01:00
|
|
|
use embassy_stm32::usb_otg::Driver;
|
2023-05-25 00:29:56 +02:00
|
|
|
use embassy_stm32::{bind_interrupts, peripherals, usb_otg, Config};
|
2023-01-18 01:37:02 +01:00
|
|
|
use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState};
|
|
|
|
use embassy_usb::class::cdc_ncm::{CdcNcmClass, State};
|
|
|
|
use embassy_usb::{Builder, UsbDevice};
|
2023-08-07 13:43:09 +02:00
|
|
|
use embedded_io_async::Write;
|
2023-06-01 01:32:11 +02:00
|
|
|
use static_cell::make_static;
|
2023-01-18 01:37:02 +01:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
|
|
|
|
type UsbDriver = Driver<'static, embassy_stm32::peripherals::USB_OTG_FS>;
|
|
|
|
|
|
|
|
const MTU: usize = 1514;
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
|
|
|
async fn usb_task(mut device: UsbDevice<'static, UsbDriver>) -> ! {
|
|
|
|
device.run().await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
|
|
|
async fn usb_ncm_task(class: Runner<'static, UsbDriver, MTU>) -> ! {
|
|
|
|
class.run().await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
|
|
|
async fn net_task(stack: &'static Stack<Device<'static, MTU>>) -> ! {
|
|
|
|
stack.run().await
|
|
|
|
}
|
|
|
|
|
2023-05-25 00:29:56 +02:00
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
OTG_FS => usb_otg::InterruptHandler<peripherals::USB_OTG_FS>;
|
2023-07-31 01:41:12 +02:00
|
|
|
HASH_RNG => rng::InterruptHandler<peripherals::RNG>;
|
2023-05-25 00:29:56 +02:00
|
|
|
});
|
|
|
|
|
2023-01-18 01:37:02 +01:00
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(spawner: Spawner) {
|
|
|
|
info!("Hello World!");
|
|
|
|
|
|
|
|
let mut config = Config::default();
|
2023-10-18 04:31:53 +02:00
|
|
|
{
|
|
|
|
use embassy_stm32::rcc::*;
|
|
|
|
config.rcc.hse = Some(Hse {
|
|
|
|
freq: Hertz(8_000_000),
|
|
|
|
mode: HseMode::Bypass,
|
|
|
|
});
|
|
|
|
config.rcc.pll_src = PllSource::HSE;
|
|
|
|
config.rcc.pll = Some(Pll {
|
|
|
|
prediv: PllPreDiv::DIV4,
|
|
|
|
mul: PllMul::MUL168,
|
|
|
|
divp: Some(Pllp::DIV2), // 8mhz / 4 * 168 / 2 = 168Mhz.
|
|
|
|
divq: Some(Pllq::DIV7), // 8mhz / 4 * 168 / 7 = 48Mhz.
|
|
|
|
divr: None,
|
|
|
|
});
|
|
|
|
config.rcc.ahb_pre = AHBPrescaler::DIV1;
|
|
|
|
config.rcc.apb1_pre = APBPrescaler::DIV4;
|
|
|
|
config.rcc.apb2_pre = APBPrescaler::DIV2;
|
|
|
|
config.rcc.sys = Sysclk::PLL1_P;
|
|
|
|
}
|
2023-01-18 01:37:02 +01:00
|
|
|
let p = embassy_stm32::init(config);
|
|
|
|
|
|
|
|
// Create the driver, from the HAL.
|
2023-06-01 01:32:11 +02:00
|
|
|
let ep_out_buffer = &mut make_static!([0; 256])[..];
|
2023-06-27 08:42:51 +02:00
|
|
|
let mut config = embassy_stm32::usb_otg::Config::default();
|
|
|
|
config.vbus_detection = true;
|
|
|
|
let driver = Driver::new_fs(p.USB_OTG_FS, Irqs, p.PA12, p.PA11, ep_out_buffer, config);
|
2023-01-18 01:37:02 +01:00
|
|
|
|
|
|
|
// Create embassy-usb Config
|
|
|
|
let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
|
|
|
|
config.manufacturer = Some("Embassy");
|
|
|
|
config.product = Some("USB-Ethernet example");
|
|
|
|
config.serial_number = Some("12345678");
|
|
|
|
config.max_power = 100;
|
|
|
|
config.max_packet_size_0 = 64;
|
|
|
|
|
|
|
|
// Required for Windows support.
|
|
|
|
config.composite_with_iads = true;
|
|
|
|
config.device_class = 0xEF;
|
|
|
|
config.device_sub_class = 0x02;
|
|
|
|
config.device_protocol = 0x01;
|
|
|
|
|
|
|
|
// Create embassy-usb DeviceBuilder using the driver and config.
|
|
|
|
let mut builder = Builder::new(
|
|
|
|
driver,
|
|
|
|
config,
|
2023-06-01 01:32:11 +02:00
|
|
|
&mut make_static!([0; 256])[..],
|
|
|
|
&mut make_static!([0; 256])[..],
|
|
|
|
&mut make_static!([0; 256])[..],
|
|
|
|
&mut make_static!([0; 128])[..],
|
2023-01-18 01:37:02 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Our MAC addr.
|
|
|
|
let our_mac_addr = [0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC];
|
|
|
|
// Host's MAC addr. This is the MAC the host "thinks" its USB-to-ethernet adapter has.
|
|
|
|
let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88];
|
|
|
|
|
|
|
|
// Create classes on the builder.
|
2023-06-01 01:32:11 +02:00
|
|
|
let class = CdcNcmClass::new(&mut builder, make_static!(State::new()), host_mac_addr, 64);
|
2023-01-18 01:37:02 +01:00
|
|
|
|
|
|
|
// Build the builder.
|
|
|
|
let usb = builder.build();
|
|
|
|
|
|
|
|
unwrap!(spawner.spawn(usb_task(usb)));
|
|
|
|
|
2023-06-01 01:32:11 +02:00
|
|
|
let (runner, device) = class.into_embassy_net_device::<MTU, 4, 4>(make_static!(NetState::new()), our_mac_addr);
|
2023-01-18 01:37:02 +01:00
|
|
|
unwrap!(spawner.spawn(usb_ncm_task(runner)));
|
|
|
|
|
2023-06-07 12:04:15 +02:00
|
|
|
let config = embassy_net::Config::dhcpv4(Default::default());
|
|
|
|
//let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
|
2023-01-18 01:37:02 +01:00
|
|
|
// address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
|
|
|
|
// dns_servers: Vec::new(),
|
|
|
|
// gateway: Some(Ipv4Address::new(10, 42, 0, 1)),
|
|
|
|
//});
|
|
|
|
|
|
|
|
// Generate random seed
|
2023-07-31 01:41:12 +02:00
|
|
|
let mut rng = Rng::new(p.RNG, Irqs);
|
2023-01-18 01:37:02 +01:00
|
|
|
let mut seed = [0; 8];
|
|
|
|
unwrap!(rng.async_fill_bytes(&mut seed).await);
|
|
|
|
let seed = u64::from_le_bytes(seed);
|
|
|
|
|
|
|
|
// Init network stack
|
2023-06-01 01:32:11 +02:00
|
|
|
let stack = &*make_static!(Stack::new(
|
|
|
|
device,
|
|
|
|
config,
|
|
|
|
make_static!(StackResources::<2>::new()),
|
|
|
|
seed
|
|
|
|
));
|
2023-01-18 01:37:02 +01:00
|
|
|
|
|
|
|
unwrap!(spawner.spawn(net_task(stack)));
|
|
|
|
|
|
|
|
// And now we can use it!
|
|
|
|
|
|
|
|
let mut rx_buffer = [0; 4096];
|
|
|
|
let mut tx_buffer = [0; 4096];
|
|
|
|
let mut buf = [0; 4096];
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
2023-05-15 00:38:58 +02:00
|
|
|
socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
|
2023-01-18 01:37:02 +01:00
|
|
|
|
|
|
|
info!("Listening on TCP:1234...");
|
|
|
|
if let Err(e) = socket.accept(1234).await {
|
|
|
|
warn!("accept error: {:?}", e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("Received connection from {:?}", socket.remote_endpoint());
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let n = match socket.read(&mut buf).await {
|
|
|
|
Ok(0) => {
|
|
|
|
warn!("read EOF");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(e) => {
|
|
|
|
warn!("read error: {:?}", e);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
info!("rxd {:02x}", &buf[..n]);
|
|
|
|
|
|
|
|
match socket.write_all(&buf[..n]).await {
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(e) => {
|
|
|
|
warn!("write error: {:?}", e);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|