2022-03-29 21:18:43 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(generic_associated_types)]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use core::mem;
|
2022-04-10 21:41:51 +02:00
|
|
|
use core::sync::atomic::{AtomicBool, Ordering};
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2022-03-29 21:18:43 +02:00
|
|
|
use defmt::*;
|
2022-06-12 07:16:56 +02:00
|
|
|
use embassy::channel::signal::Signal;
|
2022-03-29 21:18:43 +02:00
|
|
|
use embassy::executor::Spawner;
|
2022-04-06 02:24:55 +02:00
|
|
|
use embassy::time::Duration;
|
2022-07-09 08:40:10 +02:00
|
|
|
use embassy::util::{select, Either};
|
2022-04-06 02:24:55 +02:00
|
|
|
use embassy_nrf::gpio::{Input, Pin, Pull};
|
2022-07-09 08:40:10 +02:00
|
|
|
use embassy_nrf::usb::{Driver, PowerUsb};
|
2022-06-12 22:15:44 +02:00
|
|
|
use embassy_nrf::{interrupt, pac, Peripherals};
|
2022-03-31 17:25:01 +02:00
|
|
|
use embassy_usb::control::OutResponse;
|
2022-04-16 02:17:24 +02:00
|
|
|
use embassy_usb::{Builder, Config, DeviceStateHandler};
|
usb-hid: Simplify API.
- Renamed structs to HidReaderWriter, HidReader, HidWriter.
- Removed unused const generics on `State`.
- Simplified generics on `HidReaderWriter`.
The class type previously was `HidClass<D, Driver<'d, USBD>, ReportReader<'d, Driver<'d, USBD>, OUT_N>, IN_N>`
It's now `HidClass<D, Driver<'d, USBD>, IN_N, OUT_N>`. Note that the driver type `Driver<'d, USBD>` is no longer repeated.
- Constructors are now: `HidWriter::new()` for IN-only, `HidReaderWriter::new()` for IN+OUT. No complicated bounds.
- HidReaderWriter has all the methods from HidReader, HidWriter.
2022-04-16 01:59:40 +02:00
|
|
|
use embassy_usb_hid::{HidReaderWriter, ReportId, RequestHandler, State};
|
2022-03-29 21:18:43 +02:00
|
|
|
use futures::future::join;
|
2022-04-06 02:24:55 +02:00
|
|
|
use usbd_hid::descriptor::{KeyboardReport, SerializedDescriptor};
|
2022-06-12 22:15:44 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2022-04-06 02:18:39 +02:00
|
|
|
|
2022-04-10 21:41:51 +02:00
|
|
|
static SUSPENDED: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
2022-03-29 21:18:43 +02:00
|
|
|
#[embassy::main]
|
|
|
|
async fn main(_spawner: Spawner, p: Peripherals) {
|
|
|
|
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.
|
|
|
|
let irq = interrupt::take!(USBD);
|
2022-07-09 08:40:10 +02:00
|
|
|
let power_irq = interrupt::take!(POWER_CLOCK);
|
|
|
|
let driver = Driver::new(p.USBD, irq, PowerUsb::new(power_irq));
|
2022-03-29 21:18:43 +02:00
|
|
|
|
|
|
|
// Create embassy-usb Config
|
|
|
|
let mut config = Config::new(0xc0de, 0xcafe);
|
2022-04-24 22:46:45 +02:00
|
|
|
config.manufacturer = Some("Embassy");
|
|
|
|
config.product = Some("HID keyboard example");
|
2022-03-29 21:18:43 +02:00
|
|
|
config.serial_number = Some("12345678");
|
|
|
|
config.max_power = 100;
|
2022-04-06 02:24:55 +02:00
|
|
|
config.max_packet_size_0 = 64;
|
2022-04-10 21:41:51 +02:00
|
|
|
config.supports_remote_wakeup = true;
|
2022-03-29 21:18:43 +02:00
|
|
|
|
|
|
|
// Create embassy-usb DeviceBuilder using the driver and config.
|
|
|
|
// It needs some buffers for building the descriptors.
|
|
|
|
let mut device_descriptor = [0; 256];
|
|
|
|
let mut config_descriptor = [0; 256];
|
|
|
|
let mut bos_descriptor = [0; 256];
|
2022-05-09 02:07:48 +02:00
|
|
|
let mut control_buf = [0; 64];
|
2022-03-29 21:18:43 +02:00
|
|
|
let request_handler = MyRequestHandler {};
|
2022-04-10 21:41:51 +02:00
|
|
|
let device_state_handler = MyDeviceStateHandler::new();
|
2022-03-29 21:18:43 +02:00
|
|
|
|
usb-hid: Simplify API.
- Renamed structs to HidReaderWriter, HidReader, HidWriter.
- Removed unused const generics on `State`.
- Simplified generics on `HidReaderWriter`.
The class type previously was `HidClass<D, Driver<'d, USBD>, ReportReader<'d, Driver<'d, USBD>, OUT_N>, IN_N>`
It's now `HidClass<D, Driver<'d, USBD>, IN_N, OUT_N>`. Note that the driver type `Driver<'d, USBD>` is no longer repeated.
- Constructors are now: `HidWriter::new()` for IN-only, `HidReaderWriter::new()` for IN+OUT. No complicated bounds.
- HidReaderWriter has all the methods from HidReader, HidWriter.
2022-04-16 01:59:40 +02:00
|
|
|
let mut state = State::new();
|
2022-03-29 21:18:43 +02:00
|
|
|
|
2022-04-16 02:17:24 +02:00
|
|
|
let mut builder = Builder::new(
|
2022-03-29 21:18:43 +02:00
|
|
|
driver,
|
|
|
|
config,
|
|
|
|
&mut device_descriptor,
|
|
|
|
&mut config_descriptor,
|
|
|
|
&mut bos_descriptor,
|
|
|
|
&mut control_buf,
|
2022-04-10 21:41:51 +02:00
|
|
|
Some(&device_state_handler),
|
2022-03-29 21:18:43 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Create classes on the builder.
|
2022-04-16 02:07:03 +02:00
|
|
|
let config = embassy_usb_hid::Config {
|
|
|
|
report_descriptor: KeyboardReport::desc(),
|
|
|
|
request_handler: Some(&request_handler),
|
|
|
|
poll_ms: 60,
|
|
|
|
max_packet_size: 64,
|
|
|
|
};
|
|
|
|
let hid = HidReaderWriter::<_, 1, 8>::new(&mut builder, &mut state, config);
|
2022-03-29 21:18:43 +02:00
|
|
|
|
|
|
|
// Build the builder.
|
2022-04-10 21:41:51 +02:00
|
|
|
let mut usb = builder.build();
|
2022-03-29 21:18:43 +02:00
|
|
|
|
2022-04-13 22:04:31 +02:00
|
|
|
let remote_wakeup = Signal::new();
|
|
|
|
|
2022-03-29 21:18:43 +02:00
|
|
|
// Run the USB device.
|
2022-04-13 19:09:08 +02:00
|
|
|
let usb_fut = async {
|
|
|
|
loop {
|
2022-07-09 08:40:10 +02:00
|
|
|
usb.run_until_suspend().await;
|
|
|
|
match select(usb.wait_resume(), remote_wakeup.wait()).await {
|
|
|
|
Either::First(_) => (),
|
|
|
|
Either::Second(_) => unwrap!(usb.remote_wakeup().await),
|
2022-04-13 19:09:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-03-29 21:18:43 +02:00
|
|
|
|
2022-04-06 02:24:55 +02:00
|
|
|
let mut button = Input::new(p.P0_11.degrade(), Pull::Up);
|
|
|
|
|
usb-hid: Simplify API.
- Renamed structs to HidReaderWriter, HidReader, HidWriter.
- Removed unused const generics on `State`.
- Simplified generics on `HidReaderWriter`.
The class type previously was `HidClass<D, Driver<'d, USBD>, ReportReader<'d, Driver<'d, USBD>, OUT_N>, IN_N>`
It's now `HidClass<D, Driver<'d, USBD>, IN_N, OUT_N>`. Note that the driver type `Driver<'d, USBD>` is no longer repeated.
- Constructors are now: `HidWriter::new()` for IN-only, `HidReaderWriter::new()` for IN+OUT. No complicated bounds.
- HidReaderWriter has all the methods from HidReader, HidWriter.
2022-04-16 01:59:40 +02:00
|
|
|
let (reader, mut writer) = hid.split();
|
2022-04-06 02:24:55 +02:00
|
|
|
|
2022-03-29 21:18:43 +02:00
|
|
|
// Do stuff with the class!
|
2022-04-06 02:24:55 +02:00
|
|
|
let in_fut = async {
|
2022-03-29 21:18:43 +02:00
|
|
|
loop {
|
2022-04-06 02:24:55 +02:00
|
|
|
button.wait_for_low().await;
|
|
|
|
info!("PRESSED");
|
2022-04-10 21:41:51 +02:00
|
|
|
|
|
|
|
if SUSPENDED.load(Ordering::Acquire) {
|
|
|
|
info!("Triggering remote wakeup");
|
2022-04-13 22:04:31 +02:00
|
|
|
remote_wakeup.signal(());
|
2022-04-11 18:00:05 +02:00
|
|
|
} else {
|
|
|
|
let report = KeyboardReport {
|
|
|
|
keycodes: [4, 0, 0, 0, 0, 0],
|
|
|
|
leds: 0,
|
|
|
|
modifier: 0,
|
|
|
|
reserved: 0,
|
|
|
|
};
|
usb-hid: Simplify API.
- Renamed structs to HidReaderWriter, HidReader, HidWriter.
- Removed unused const generics on `State`.
- Simplified generics on `HidReaderWriter`.
The class type previously was `HidClass<D, Driver<'d, USBD>, ReportReader<'d, Driver<'d, USBD>, OUT_N>, IN_N>`
It's now `HidClass<D, Driver<'d, USBD>, IN_N, OUT_N>`. Note that the driver type `Driver<'d, USBD>` is no longer repeated.
- Constructors are now: `HidWriter::new()` for IN-only, `HidReaderWriter::new()` for IN+OUT. No complicated bounds.
- HidReaderWriter has all the methods from HidReader, HidWriter.
2022-04-16 01:59:40 +02:00
|
|
|
match writer.write_serialize(&report).await {
|
2022-04-11 18:00:05 +02:00
|
|
|
Ok(()) => {}
|
|
|
|
Err(e) => warn!("Failed to send report: {:?}", e),
|
|
|
|
};
|
2022-04-10 21:41:51 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 02:24:55 +02:00
|
|
|
button.wait_for_high().await;
|
|
|
|
info!("RELEASED");
|
|
|
|
let report = KeyboardReport {
|
|
|
|
keycodes: [0, 0, 0, 0, 0, 0],
|
|
|
|
leds: 0,
|
|
|
|
modifier: 0,
|
|
|
|
reserved: 0,
|
|
|
|
};
|
usb-hid: Simplify API.
- Renamed structs to HidReaderWriter, HidReader, HidWriter.
- Removed unused const generics on `State`.
- Simplified generics on `HidReaderWriter`.
The class type previously was `HidClass<D, Driver<'d, USBD>, ReportReader<'d, Driver<'d, USBD>, OUT_N>, IN_N>`
It's now `HidClass<D, Driver<'d, USBD>, IN_N, OUT_N>`. Note that the driver type `Driver<'d, USBD>` is no longer repeated.
- Constructors are now: `HidWriter::new()` for IN-only, `HidReaderWriter::new()` for IN+OUT. No complicated bounds.
- HidReaderWriter has all the methods from HidReader, HidWriter.
2022-04-16 01:59:40 +02:00
|
|
|
match writer.write_serialize(&report).await {
|
2022-04-06 02:24:55 +02:00
|
|
|
Ok(()) => {}
|
|
|
|
Err(e) => warn!("Failed to send report: {:?}", e),
|
|
|
|
};
|
2022-03-29 21:18:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-06 02:24:55 +02:00
|
|
|
let out_fut = async {
|
usb-hid: Simplify API.
- Renamed structs to HidReaderWriter, HidReader, HidWriter.
- Removed unused const generics on `State`.
- Simplified generics on `HidReaderWriter`.
The class type previously was `HidClass<D, Driver<'d, USBD>, ReportReader<'d, Driver<'d, USBD>, OUT_N>, IN_N>`
It's now `HidClass<D, Driver<'d, USBD>, IN_N, OUT_N>`. Note that the driver type `Driver<'d, USBD>` is no longer repeated.
- Constructors are now: `HidWriter::new()` for IN-only, `HidReaderWriter::new()` for IN+OUT. No complicated bounds.
- HidReaderWriter has all the methods from HidReader, HidWriter.
2022-04-16 01:59:40 +02:00
|
|
|
reader.run(false, &request_handler).await;
|
2022-04-06 02:24:55 +02:00
|
|
|
};
|
2022-04-10 21:41:51 +02:00
|
|
|
|
2022-03-29 21:18:43 +02:00
|
|
|
// Run everything concurrently.
|
|
|
|
// If we had made everything `'static` above instead, we could do this using separate tasks instead.
|
2022-04-06 02:24:55 +02:00
|
|
|
join(usb_fut, join(in_fut, out_fut)).await;
|
2022-03-29 21:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct MyRequestHandler {}
|
|
|
|
|
|
|
|
impl RequestHandler for MyRequestHandler {
|
|
|
|
fn get_report(&self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
|
|
|
|
info!("Get report for {:?}", id);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-03-31 17:25:01 +02:00
|
|
|
fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse {
|
|
|
|
info!("Set report for {:?}: {=[u8]}", id, data);
|
|
|
|
OutResponse::Accepted
|
|
|
|
}
|
|
|
|
|
2022-03-29 21:18:43 +02:00
|
|
|
fn set_idle(&self, id: Option<ReportId>, dur: Duration) {
|
|
|
|
info!("Set idle rate for {:?} to {:?}", id, dur);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_idle(&self, id: Option<ReportId>) -> Option<Duration> {
|
|
|
|
info!("Get idle rate for {:?}", id);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2022-04-10 21:41:51 +02:00
|
|
|
|
|
|
|
struct MyDeviceStateHandler {
|
|
|
|
configured: AtomicBool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MyDeviceStateHandler {
|
|
|
|
fn new() -> Self {
|
|
|
|
MyDeviceStateHandler {
|
|
|
|
configured: AtomicBool::new(false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DeviceStateHandler for MyDeviceStateHandler {
|
2022-04-13 19:09:08 +02:00
|
|
|
fn enabled(&self, enabled: bool) {
|
|
|
|
self.configured.store(false, Ordering::Relaxed);
|
|
|
|
SUSPENDED.store(false, Ordering::Release);
|
|
|
|
if enabled {
|
|
|
|
info!("Device enabled");
|
|
|
|
} else {
|
|
|
|
info!("Device disabled");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-10 21:41:51 +02:00
|
|
|
fn reset(&self) {
|
|
|
|
self.configured.store(false, Ordering::Relaxed);
|
|
|
|
info!("Bus reset, the Vbus current limit is 100mA");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn addressed(&self, addr: u8) {
|
|
|
|
self.configured.store(false, Ordering::Relaxed);
|
|
|
|
info!("USB address set to: {}", addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn configured(&self, configured: bool) {
|
|
|
|
self.configured.store(configured, Ordering::Relaxed);
|
|
|
|
if configured {
|
2022-06-12 22:15:44 +02:00
|
|
|
info!("Device configured, it may now draw up to the configured current limit from Vbus.")
|
2022-04-10 21:41:51 +02:00
|
|
|
} else {
|
|
|
|
info!("Device is no longer configured, the Vbus current limit is 100mA.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn suspended(&self, suspended: bool) {
|
|
|
|
if suspended {
|
|
|
|
info!("Device suspended, the Vbus current limit is 500µA (or 2.5mA for high-power devices with remote wakeup enabled).");
|
|
|
|
SUSPENDED.store(true, Ordering::Release);
|
|
|
|
} else {
|
|
|
|
SUSPENDED.store(false, Ordering::Release);
|
|
|
|
if self.configured.load(Ordering::Relaxed) {
|
2022-06-12 22:15:44 +02:00
|
|
|
info!("Device resumed, it may now draw up to the configured current limit from Vbus");
|
2022-04-10 21:41:51 +02:00
|
|
|
} else {
|
|
|
|
info!("Device resumed, the Vbus current limit is 100mA");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|