2022-03-29 21:18:43 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use core::mem;
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2022-03-29 21:18:43 +02:00
|
|
|
use defmt::*;
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_executor::Spawner;
|
2022-09-22 16:48:35 +02:00
|
|
|
use embassy_futures::join::join;
|
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, usb};
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_time::{Duration, Timer};
|
2022-09-26 13:00:21 +02:00
|
|
|
use embassy_usb::class::hid::{HidWriter, ReportId, RequestHandler, State};
|
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};
|
2022-03-29 21:18:43 +02:00
|
|
|
use usbd_hid::descriptor::{MouseReport, SerializedDescriptor};
|
2022-06-12 22:15:44 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2022-04-06 02:18:39 +02:00
|
|
|
|
2023-03-05 22:36:53 +01:00
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
USBD => usb::InterruptHandler<peripherals::USBD>;
|
|
|
|
POWER_CLOCK => usb::vbus_detect::InterruptHandler;
|
|
|
|
});
|
|
|
|
|
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-03-29 21:18:43 +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-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 mouse example");
|
2022-03-29 21:18:43 +02:00
|
|
|
config.serial_number = Some("12345678");
|
|
|
|
config.max_power = 100;
|
2022-04-24 22:46:45 +02:00
|
|
|
config.max_packet_size_0 = 64;
|
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];
|
2023-03-05 22:36:53 +01:00
|
|
|
let mut msos_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-16 02:07:03 +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,
|
2023-03-05 22:36:53 +01:00
|
|
|
&mut msos_descriptor,
|
2022-03-29 21:18:43 +02:00
|
|
|
&mut control_buf,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Create classes on the builder.
|
2022-09-26 13:00:21 +02:00
|
|
|
let config = embassy_usb::class::hid::Config {
|
2022-04-16 02:07:03 +02:00
|
|
|
report_descriptor: MouseReport::desc(),
|
|
|
|
request_handler: Some(&request_handler),
|
|
|
|
poll_ms: 60,
|
|
|
|
max_packet_size: 8,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut writer = HidWriter::<_, 5>::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
|
|
|
|
|
|
|
// Run the USB device.
|
|
|
|
let usb_fut = usb.run();
|
|
|
|
|
|
|
|
// Do stuff with the class!
|
|
|
|
let hid_fut = async {
|
2022-04-06 02:18:39 +02:00
|
|
|
let mut y: i8 = 5;
|
2022-03-29 21:18:43 +02:00
|
|
|
loop {
|
|
|
|
Timer::after(Duration::from_millis(500)).await;
|
|
|
|
|
2022-04-06 02:18:39 +02:00
|
|
|
y = -y;
|
|
|
|
let report = MouseReport {
|
|
|
|
buttons: 0,
|
|
|
|
x: 0,
|
|
|
|
y,
|
|
|
|
wheel: 0,
|
|
|
|
pan: 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:18:39 +02:00
|
|
|
Ok(()) => {}
|
|
|
|
Err(e) => warn!("Failed to send report: {:?}", e),
|
|
|
|
}
|
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.
|
|
|
|
join(usb_fut, hid_fut).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-29 21:58:35 +02:00
|
|
|
fn set_idle_ms(&self, id: Option<ReportId>, dur: u32) {
|
2022-03-29 21:18:43 +02:00
|
|
|
info!("Set idle rate for {:?} to {:?}", id, dur);
|
|
|
|
}
|
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
fn get_idle_ms(&self, id: Option<ReportId>) -> Option<u32> {
|
2022-03-29 21:18:43 +02:00
|
|
|
info!("Get idle rate for {:?}", id);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|