2022-04-23 05:24:38 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use core::mem;
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2022-04-23 05:24:38 +02:00
|
|
|
use defmt::*;
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_executor::Spawner;
|
2022-05-04 20:48:37 +02:00
|
|
|
use embassy_net::tcp::TcpSocket;
|
2022-12-07 16:03:03 +01:00
|
|
|
use embassy_net::{Stack, StackResources};
|
2022-05-23 03:50:43 +02:00
|
|
|
use embassy_nrf::rng::Rng;
|
2023-03-05 22:36:53 +01:00
|
|
|
use embassy_nrf::usb::vbus_detect::HardwareVbusDetect;
|
|
|
|
use embassy_nrf::usb::Driver;
|
|
|
|
use embassy_nrf::{bind_interrupts, pac, peripherals, rng, usb};
|
2022-12-07 16:03:03 +01:00
|
|
|
use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState};
|
|
|
|
use embassy_usb::class::cdc_ncm::{CdcNcmClass, State};
|
2022-04-23 05:24:38 +02:00
|
|
|
use embassy_usb::{Builder, Config, UsbDevice};
|
2022-08-30 19:25:36 +02:00
|
|
|
use embedded_io::asynch::Write;
|
2023-06-01 01:32:11 +02:00
|
|
|
use static_cell::make_static;
|
2022-06-12 22:15:44 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2022-04-23 05:24:38 +02:00
|
|
|
|
2023-03-05 21:50:15 +01:00
|
|
|
bind_interrupts!(struct Irqs {
|
2023-03-05 22:36:53 +01:00
|
|
|
USBD => usb::InterruptHandler<peripherals::USBD>;
|
|
|
|
POWER_CLOCK => usb::vbus_detect::InterruptHandler;
|
2023-03-05 21:50:15 +01:00
|
|
|
RNG => rng::InterruptHandler<peripherals::RNG>;
|
|
|
|
});
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
type MyDriver = Driver<'static, peripherals::USBD, HardwareVbusDetect>;
|
2022-04-23 05:24:38 +02:00
|
|
|
|
2022-12-07 16:03:03 +01:00
|
|
|
const MTU: usize = 1514;
|
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::task]
|
2022-04-23 05:24:38 +02:00
|
|
|
async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! {
|
|
|
|
device.run().await
|
|
|
|
}
|
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::task]
|
2022-12-07 16:03:03 +01:00
|
|
|
async fn usb_ncm_task(class: Runner<'static, MyDriver, MTU>) -> ! {
|
|
|
|
class.run().await
|
2022-04-23 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::task]
|
2022-12-07 16:03:03 +01:00
|
|
|
async fn net_task(stack: &'static Stack<Device<'static, MTU>>) -> ! {
|
2022-05-23 03:50:43 +02:00
|
|
|
stack.run().await
|
2022-04-23 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::main]
|
2022-08-17 18:49:55 +02:00
|
|
|
async fn main(spawner: Spawner) {
|
|
|
|
let p = embassy_nrf::init(Default::default());
|
2022-04-23 05:24:38 +02:00
|
|
|
let clock: pac::CLOCK = unsafe { mem::transmute(()) };
|
|
|
|
|
|
|
|
info!("Enabling ext hfosc...");
|
|
|
|
clock.tasks_hfclkstart.write(|w| unsafe { w.bits(1) });
|
|
|
|
while clock.events_hfclkstarted.read().bits() != 1 {}
|
|
|
|
|
|
|
|
// Create the driver, from the HAL.
|
2023-03-05 22:36:53 +01:00
|
|
|
let driver = Driver::new(p.USBD, Irqs, HardwareVbusDetect::new(Irqs));
|
2022-04-23 05:24:38 +02:00
|
|
|
|
|
|
|
// Create embassy-usb Config
|
|
|
|
let mut config = 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])[..],
|
|
|
|
&mut make_static!([0; 128])[..],
|
2022-04-23 05:24:38 +02: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);
|
2022-04-23 05:24:38 +02: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);
|
2022-12-07 16:03:03 +01:00
|
|
|
unwrap!(spawner.spawn(usb_ncm_task(runner)));
|
2022-04-23 05:24:38 +02:00
|
|
|
|
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 {
|
2022-05-23 03:50:43 +02:00
|
|
|
// address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
|
|
|
|
// dns_servers: Vec::new(),
|
|
|
|
// gateway: Some(Ipv4Address::new(10, 42, 0, 1)),
|
2023-06-07 12:04:15 +02:00
|
|
|
// });
|
2022-05-23 03:50:43 +02:00
|
|
|
|
|
|
|
// Generate random seed
|
2023-03-05 21:50:15 +01:00
|
|
|
let mut rng = Rng::new(p.RNG, Irqs);
|
2022-05-23 03:50:43 +02:00
|
|
|
let mut seed = [0; 8];
|
|
|
|
rng.blocking_fill_bytes(&mut seed);
|
|
|
|
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
|
|
|
|
));
|
2022-05-23 03:50:43 +02:00
|
|
|
|
|
|
|
unwrap!(spawner.spawn(net_task(stack)));
|
2022-04-23 05:24:38 +02:00
|
|
|
|
|
|
|
// And now we can use it!
|
|
|
|
|
|
|
|
let mut rx_buffer = [0; 4096];
|
|
|
|
let mut tx_buffer = [0; 4096];
|
|
|
|
let mut buf = [0; 4096];
|
|
|
|
|
|
|
|
loop {
|
2022-05-23 03:50:43 +02:00
|
|
|
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)));
|
2022-04-23 05:24:38 +02: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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|