aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/nrf/Cargo.toml6
-rw-r--r--examples/nrf/src/bin/usb_hid.rs131
2 files changed, 135 insertions, 2 deletions
diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml
index aa30f3fa9..e944c171a 100644
--- a/examples/nrf/Cargo.toml
+++ b/examples/nrf/Cargo.toml
@@ -6,13 +6,14 @@ version = "0.1.0"
6 6
7[features] 7[features]
8default = ["nightly"] 8default = ["nightly"]
9nightly = ["embassy-nrf/nightly", "embassy-nrf/unstable-traits", "embassy-usb", "embassy-usb-serial"] 9nightly = ["embassy-nrf/nightly", "embassy-nrf/unstable-traits", "embassy-usb", "embassy-usb-serial", "embassy-usb-hid"]
10 10
11[dependencies] 11[dependencies]
12embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime"] } 12embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime"] }
13embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac"] } 13embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac"] }
14embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"], optional = true } 14embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"], optional = true }
15embassy-usb-serial = { version = "0.1.0", path = "../../embassy-usb-serial", features = ["defmt"], optional = true } 15embassy-usb-serial = { version = "0.1.0", path = "../../embassy-usb-serial", features = ["defmt"], optional = true }
16embassy-usb-hid = { version = "0.1.0", path = "../../embassy-usb-hid", features = ["defmt"], optional = true }
16 17
17defmt = "0.3" 18defmt = "0.3"
18defmt-rtt = "0.3" 19defmt-rtt = "0.3"
@@ -23,4 +24,5 @@ panic-probe = { version = "0.3", features = ["print-defmt"] }
23futures = { version = "0.3.17", default-features = false, features = ["async-await"] } 24futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
24rand = { version = "0.8.4", default-features = false } 25rand = { version = "0.8.4", default-features = false }
25embedded-storage = "0.3.0" 26embedded-storage = "0.3.0"
26 27usbd-hid = "0.5.2"
28serde = { version = "1.0.136", default-features = false }
diff --git a/examples/nrf/src/bin/usb_hid.rs b/examples/nrf/src/bin/usb_hid.rs
new file mode 100644
index 000000000..1fd056d00
--- /dev/null
+++ b/examples/nrf/src/bin/usb_hid.rs
@@ -0,0 +1,131 @@
1#![no_std]
2#![no_main]
3#![feature(generic_associated_types)]
4#![feature(type_alias_impl_trait)]
5
6#[path = "../example_common.rs"]
7mod example_common;
8
9use core::mem;
10use defmt::*;
11use embassy::executor::Spawner;
12use embassy::time::{Duration, Timer};
13use embassy_nrf::interrupt;
14use embassy_nrf::pac;
15use embassy_nrf::usb::Driver;
16use embassy_nrf::Peripherals;
17use embassy_usb::{Config, UsbDeviceBuilder};
18use embassy_usb_hid::{HidClass, ReportId, RequestHandler, State};
19use futures::future::join;
20use usbd_hid::descriptor::{MouseReport, SerializedDescriptor};
21
22#[embassy::main]
23async fn main(_spawner: Spawner, p: Peripherals) {
24 let clock: pac::CLOCK = unsafe { mem::transmute(()) };
25 let power: pac::POWER = unsafe { mem::transmute(()) };
26
27 info!("Enabling ext hfosc...");
28 clock.tasks_hfclkstart.write(|w| unsafe { w.bits(1) });
29 while clock.events_hfclkstarted.read().bits() != 1 {}
30
31 info!("Waiting for vbus...");
32 while !power.usbregstatus.read().vbusdetect().is_vbus_present() {}
33 info!("vbus OK");
34
35 // Create the driver, from the HAL.
36 let irq = interrupt::take!(USBD);
37 let driver = Driver::new(p.USBD, irq);
38
39 // Create embassy-usb Config
40 let mut config = Config::new(0xc0de, 0xcafe);
41 config.manufacturer = Some("Tactile Engineering");
42 config.product = Some("Testy");
43 config.serial_number = Some("12345678");
44 config.max_power = 100;
45
46 // Create embassy-usb DeviceBuilder using the driver and config.
47 // It needs some buffers for building the descriptors.
48 let mut device_descriptor = [0; 256];
49 let mut config_descriptor = [0; 256];
50 let mut bos_descriptor = [0; 256];
51 let mut control_buf = [0; 16];
52 let request_handler = MyRequestHandler {};
53
54 let mut state = State::<5, 0, 0>::new();
55
56 let mut builder = UsbDeviceBuilder::new(
57 driver,
58 config,
59 &mut device_descriptor,
60 &mut config_descriptor,
61 &mut bos_descriptor,
62 &mut control_buf,
63 );
64
65 // Create classes on the builder.
66 // let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
67 let mut hid = HidClass::new(
68 &mut builder,
69 &mut state,
70 MouseReport::desc(),
71 Some(&request_handler),
72 60,
73 );
74
75 // Build the builder.
76 let mut usb = builder.build();
77
78 // Run the USB device.
79 let usb_fut = usb.run();
80
81 // Do stuff with the class!
82 let hid_fut = async {
83 loop {
84 Timer::after(Duration::from_millis(500)).await;
85 hid.input()
86 .serialize(&MouseReport {
87 buttons: 0,
88 x: 0,
89 y: 4,
90 wheel: 0,
91 pan: 0,
92 })
93 .await
94 .unwrap();
95
96 Timer::after(Duration::from_millis(500)).await;
97 hid.input()
98 .serialize(&MouseReport {
99 buttons: 0,
100 x: 0,
101 y: -4,
102 wheel: 0,
103 pan: 0,
104 })
105 .await
106 .unwrap();
107 }
108 };
109
110 // Run everything concurrently.
111 // If we had made everything `'static` above instead, we could do this using separate tasks instead.
112 join(usb_fut, hid_fut).await;
113}
114
115struct MyRequestHandler {}
116
117impl RequestHandler for MyRequestHandler {
118 fn get_report(&self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
119 info!("Get report for {:?}", id);
120 None
121 }
122
123 fn set_idle(&self, id: Option<ReportId>, dur: Duration) {
124 info!("Set idle rate for {:?} to {:?}", id, dur);
125 }
126
127 fn get_idle(&self, id: Option<ReportId>) -> Option<Duration> {
128 info!("Get idle rate for {:?}", id);
129 None
130 }
131}