2022-05-30 00:36:30 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
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;
|
2022-05-30 00:36:30 +02:00
|
|
|
use embassy_stm32::rcc::*;
|
|
|
|
use embassy_stm32::usb::Driver;
|
2022-08-30 19:25:36 +02:00
|
|
|
use embassy_stm32::{interrupt, Config};
|
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-05-30 00:36:30 +02:00
|
|
|
use embassy_usb::control::OutResponse;
|
|
|
|
use embassy_usb::Builder;
|
|
|
|
use usbd_hid::descriptor::{MouseReport, SerializedDescriptor};
|
2022-06-12 22:15:44 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2022-05-30 00:36:30 +02:00
|
|
|
|
2022-08-17 22:25:58 +02:00
|
|
|
#[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;
|
2022-08-17 22:25:58 +02:00
|
|
|
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("HID mouse example");
|
|
|
|
config.serial_number = Some("12345678");
|
|
|
|
config.max_power = 100;
|
|
|
|
config.max_packet_size_0 = 64;
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
let mut control_buf = [0; 64];
|
|
|
|
let request_handler = MyRequestHandler {};
|
|
|
|
|
|
|
|
let mut state = State::new();
|
|
|
|
|
|
|
|
let mut builder = Builder::new(
|
|
|
|
driver,
|
|
|
|
config,
|
|
|
|
&mut device_descriptor,
|
|
|
|
&mut config_descriptor,
|
|
|
|
&mut bos_descriptor,
|
|
|
|
&mut control_buf,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Create classes on the builder.
|
2022-09-26 13:00:21 +02:00
|
|
|
let config = embassy_usb::class::hid::Config {
|
2022-05-30 00:36:30 +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);
|
|
|
|
|
|
|
|
// Build the builder.
|
|
|
|
let mut usb = builder.build();
|
|
|
|
|
|
|
|
// Run the USB device.
|
|
|
|
let usb_fut = usb.run();
|
|
|
|
|
|
|
|
// Do stuff with the class!
|
|
|
|
let hid_fut = async {
|
|
|
|
let mut y: i8 = 5;
|
|
|
|
loop {
|
|
|
|
Timer::after(Duration::from_millis(500)).await;
|
|
|
|
|
|
|
|
y = -y;
|
|
|
|
let report = MouseReport {
|
|
|
|
buttons: 0,
|
|
|
|
x: 0,
|
|
|
|
y,
|
|
|
|
wheel: 0,
|
|
|
|
pan: 0,
|
|
|
|
};
|
|
|
|
match writer.write_serialize(&report).await {
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(e) => warn!("Failed to send report: {:?}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse {
|
|
|
|
info!("Set report for {:?}: {=[u8]}", id, data);
|
|
|
|
OutResponse::Accepted
|
|
|
|
}
|
|
|
|
|
2022-08-30 19:25:36 +02:00
|
|
|
fn set_idle_ms(&self, id: Option<ReportId>, dur: u32) {
|
2022-05-30 00:36:30 +02:00
|
|
|
info!("Set idle rate for {:?} to {:?}", id, dur);
|
|
|
|
}
|
|
|
|
|
2022-08-30 19:25:36 +02:00
|
|
|
fn get_idle_ms(&self, id: Option<ReportId>) -> Option<u32> {
|
2022-05-30 00:36:30 +02:00
|
|
|
info!("Get idle rate for {:?}", id);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|