embassy/examples/stm32l5/src/bin/usb_ethernet.rs

165 lines
4.8 KiB
Rust
Raw Normal View History

2022-05-30 00:36:30 +02:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
2022-05-30 00:36:30 +02:00
use embassy_net::tcp::TcpSocket;
use embassy_net::{Stack, StackResources};
2022-05-30 00:36:30 +02:00
use embassy_stm32::rcc::*;
use embassy_stm32::rng::Rng;
use embassy_stm32::usb::Driver;
use embassy_stm32::{interrupt, Config};
use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState};
use embassy_usb::class::cdc_ncm::{CdcNcmClass, State};
2022-05-30 00:36:30 +02:00
use embassy_usb::{Builder, UsbDevice};
use embedded_io::asynch::Write;
2022-05-30 00:36:30 +02:00
use rand_core::RngCore;
2022-08-22 15:51:44 +02:00
use static_cell::StaticCell;
2022-06-12 22:15:44 +02:00
use {defmt_rtt as _, panic_probe as _};
2022-05-30 00:36:30 +02:00
type MyDriver = Driver<'static, embassy_stm32::peripherals::USB>;
2022-08-22 15:51:44 +02:00
macro_rules! singleton {
2022-05-30 00:36:30 +02:00
($val:expr) => {{
type T = impl Sized;
2022-08-22 15:51:44 +02:00
static STATIC_CELL: StaticCell<T> = StaticCell::new();
let (x,) = STATIC_CELL.init(($val,));
x
2022-05-30 00:36:30 +02:00
}};
}
const MTU: usize = 1514;
#[embassy_executor::task]
2022-05-30 00:36:30 +02:00
async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! {
device.run().await
}
#[embassy_executor::task]
async fn usb_ncm_task(class: Runner<'static, MyDriver, MTU>) -> ! {
class.run().await
2022-05-30 00:36:30 +02:00
}
#[embassy_executor::task]
async fn net_task(stack: &'static Stack<Device<'static, MTU>>) -> ! {
2022-05-30 00:36:30 +02:00
stack.run().await
}
#[embassy_executor::main]
async fn main(spawner: Spawner) {
2022-05-30 00:36:30 +02:00
let mut config = Config::default();
2022-06-12 22:15:44 +02:00
config.rcc.mux = ClockSrc::PLL(PLLSource::HSI16, PLLClkDiv::Div2, PLLSrcDiv::Div1, PLLMul::Mul10, None);
2022-05-30 00:36:30 +02:00
config.rcc.hsi48 = true;
let p = embassy_stm32::init(config);
2022-05-30 00:36:30 +02:00
// Create the driver, from the HAL.
let irq = interrupt::take!(USB_FS);
let driver = Driver::new(p.USB, irq, p.PA12, p.PA11);
// 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,
&mut singleton!([0; 256])[..],
&mut singleton!([0; 256])[..],
&mut singleton!([0; 256])[..],
&mut singleton!([0; 128])[..],
2022-05-30 00:36:30 +02:00
None,
);
// 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.
let class = CdcNcmClass::new(&mut builder, singleton!(State::new()), host_mac_addr, 64);
2022-05-30 00:36:30 +02:00
// Build the builder.
let usb = builder.build();
unwrap!(spawner.spawn(usb_task(usb)));
let (runner, device) = class.into_embassy_net_device::<MTU, 4, 4>(singleton!(NetState::new()), our_mac_addr);
unwrap!(spawner.spawn(usb_ncm_task(runner)));
2022-05-30 00:36:30 +02:00
let config = embassy_net::ConfigStrategy::Dhcp;
//let config = embassy_net::ConfigStrategy::Static(embassy_net::Config {
// 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
let mut rng = Rng::new(p.RNG);
let seed = rng.next_u64();
// Init network stack
2022-08-22 15:51:44 +02:00
let stack = &*singleton!(Stack::new(
2022-05-30 00:36:30 +02:00
device,
config,
2022-08-22 15:51:44 +02:00
singleton!(StackResources::<1, 2, 8>::new()),
2022-05-30 00:36:30 +02:00
seed
));
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);
socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10)));
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;
}
};
}
}
}