Initial commit

This commit is contained in:
hodasemi 2023-04-12 11:40:56 +02:00
commit 510d910669
10 changed files with 1815 additions and 0 deletions

29
.cargo/config.toml Normal file
View file

@ -0,0 +1,29 @@
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
# Choose a default "cargo run" tool:
# - probe-run provides flashing and defmt via a hardware debugger
# - cargo embed offers flashing, rtt, defmt and a gdb server via a hardware debugger
# it is configured via the Embed.toml in the root of this project
# - elf2uf2-rs loads firmware over USB when the rp2040 is in boot mode
# runner = "probe-run --chip RP2040"
# runner = "cargo embed"
runner = "elf2uf2-rs -d"
rustflags = [
"-C", "linker=flip-link",
"-C", "link-arg=--nmagic",
"-C", "link-arg=-Tlink.x",
"-C", "link-arg=-Tdefmt.x",
# Code-size optimizations.
# trap unreachable can save a lot of space, but requires nightly compiler.
# uncomment the next line if you wish to enable it
# "-Z", "trap-unreachable=no",
"-C", "inline-threshold=5",
"-C", "no-vectorize-loops",
]
[build]
target = "thumbv6m-none-eabi"
[env]
DEFMT_LOG = "debug"

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

11
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,11 @@
{
"workbench.colorCustomizations": {
"activityBar.background": "#2C300F",
"titleBar.activeBackground": "#3D4314",
"titleBar.activeForeground": "#FAFBF3"
},
"rust-analyzer.check.allTargets": false,
"rust-analyzer.linkedProjects": [
"./Cargo.toml"
],
}

13
.vscode/tasks.json vendored Normal file
View file

@ -0,0 +1,13 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "cargo",
"command": "run",
"problemMatcher": [
"$rustc"
],
"label": "rust: cargo run"
}
]
}

1525
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

22
Cargo.toml Normal file
View file

@ -0,0 +1,22 @@
[package]
name = "embassy_pico_mouse"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
embassy-executor = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
embassy-rp = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "pio", "critical-section-impl"] }
embassy-usb = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = ["defmt"] }
embassy-time = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = ["defmt", "defmt-timestamp-uptime"] }
embassy-futures = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy" }
usbd-hid = "0.6.1"
defmt = "0.3"
defmt-rtt = "0.4"
cortex-m = { version = "0.7.6" }
cortex-m-rt = "0.7.0"
panic-probe = { version = "0.3", features = ["print-defmt"] }

15
memory.x Normal file
View file

@ -0,0 +1,15 @@
MEMORY {
BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100
FLASH : ORIGIN = 0x10000100, LENGTH = 2048K - 0x100
RAM : ORIGIN = 0x20000000, LENGTH = 256K
}
EXTERN(BOOT2_FIRMWARE)
SECTIONS {
/* ### Boot loader */
.boot2 ORIGIN(BOOT2) :
{
KEEP(*(.boot2));
} > BOOT2
} INSERT BEFORE .text;

4
rust-toolchain.toml Normal file
View file

@ -0,0 +1,4 @@
[toolchain]
channel = "nightly-2023-04-11"
components = [ "rust-src", "rustfmt", "llvm-tools-preview" ]
targets = ["thumbv6m-none-eabi"]

78
src/main.rs Normal file
View file

@ -0,0 +1,78 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
#![feature(never_type)]
mod mouse_hid;
use core::future::Future;
use embassy_executor::Spawner;
use embassy_futures::join::join;
use embassy_rp::usb::Driver;
use embassy_rp::{interrupt, peripherals::USB};
use embassy_usb::class::hid::{HidWriter, ReportId, RequestHandler, State};
use embassy_usb::control::OutResponse;
use embassy_usb::{Builder, Config};
use embassy_time::{Duration, Timer};
use usbd_hid::descriptor::{MouseReport, SerializedDescriptor};
use crate::mouse_hid::{MouseConfig, MouseHID};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default());
let mut mouse_hid = MouseHID::new(
p.USB,
MouseConfig {
vendor_id: 0x046d,
product_id: 0x101b,
manufacturer: "Logitech",
product: "Marathon Mouse/Performance Plus M705",
serial_number: "B14D65DA",
},
)
.await;
// let usb_fut = mouse_hid.run();
const PIXEL: i8 = 2;
const WAIT_TIME: Duration = Duration::from_secs(1);
// Do stuff with the class!
let hid_fut = async {
loop {
mouse_move(&mut mouse_hid, WAIT_TIME, PIXEL).await;
mouse_move(&mut mouse_hid, WAIT_TIME, -PIXEL).await;
// mouse_move(&mut mouse_hid, Duration::from_millis(50), -PIXEL).await;
}
};
// // 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;
hid_fut.await;
}
async fn mouse_move(mouse_hid: &mut MouseHID<'_>, duration: Duration, v: i8) {
Timer::after(duration).await;
let report = MouseReport {
buttons: 0,
x: 0,
y: v,
wheel: 0,
pan: 0,
};
mouse_hid.mouse_report(report).await;
}

117
src/mouse_hid.rs Normal file
View file

@ -0,0 +1,117 @@
use core::future::Future;
use embassy_rp::usb::Driver;
use embassy_rp::{interrupt, peripherals::USB};
use embassy_usb::class::hid::{HidWriter, ReportId, RequestHandler, State};
use embassy_usb::control::OutResponse;
use embassy_usb::{Builder, Config, UsbDevice};
use usbd_hid::descriptor::{MouseReport, SerializedDescriptor};
pub struct MouseConfig<'a> {
pub vendor_id: u16,
pub product_id: u16,
pub manufacturer: &'a str,
pub product: &'a str,
pub serial_number: &'a str,
}
pub struct MouseHID<'d> {
writer: HidWriter<'d, Driver<'d, USB>, 5>,
usb: UsbDevice<'d, Driver<'d, USB>>,
state: State<'d>,
device_descriptor: [u8; 256],
config_descriptor: [u8; 256],
bos_descriptor: [u8; 256],
control_buf: [u8; 64],
}
impl<'d> MouseHID<'d> {
pub async fn new(usb: USB, mouse_config: MouseConfig<'_>) -> MouseHID<'d> {
// Create the driver, from the HAL.
let irq = interrupt::take!(USBCTRL_IRQ);
let driver = Driver::new(usb, irq);
let mut config = Config::new(mouse_config.vendor_id, mouse_config.product_id);
config.manufacturer = Some(mouse_config.manufacturer);
config.product = Some(mouse_config.product);
config.serial_number = Some(mouse_config.serial_number);
config.device_class = 0;
config.device_protocol = 2;
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.
let config = embassy_usb::class::hid::Config {
report_descriptor: MouseReport::desc(),
request_handler: Some(&request_handler),
poll_ms: 60,
max_packet_size: 8,
};
let writer = HidWriter::<_, 5>::new(&mut builder, &mut state, config);
// Build the builder.
let usb = builder.build();
MouseHID {
writer,
usb,
state,
device_descriptor,
config_descriptor,
bos_descriptor,
control_buf,
}
}
pub async fn run(&'d mut self) -> impl Future<Output = !> + 'd {
self.usb.run()
}
pub async fn mouse_report(&mut self, report: MouseReport) {
match self.writer.write_serialize(&report).await {
Ok(()) => {}
Err(_e) => (),
}
}
}
struct MyRequestHandler {}
impl RequestHandler for MyRequestHandler {
fn get_report(&self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
None
}
fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse {
OutResponse::Accepted
}
fn set_idle_ms(&self, id: Option<ReportId>, dur: u32) {}
fn get_idle_ms(&self, id: Option<ReportId>) -> Option<u32> {
None
}
}