rp-hal-boards/boards/pico/examples/pico_usb_twitchy_mouse.rs

180 lines
5.4 KiB
Rust
Raw Normal View History

//! # Pico USB 'Twitchy' Mouse Example
//!
//! Creates a USB HID Class Poiting device (i.e. a virtual mouse) on a Pico
//! board, with the USB driver running in the main thread.
//!
//! It generates movement reports which will twitch the cursor up and down by a
//! few pixels, several times a second.
//!
//! See the `Cargo.toml` file for Copyright and licence details.
//!
2021-09-10 23:39:01 +10:00
//! This is a port of
//! https://github.com/atsamd-rs/atsamd/blob/master/boards/itsybitsy_m0/examples/twitching_usb_mouse.rs
2021-09-10 23:39:01 +10:00
#![no_std]
#![no_main]
// The macro for our start-up function
2021-09-10 23:39:01 +10:00
use cortex_m_rt::entry;
// The macro for marking our interrupt functions
use pico::hal::pac::interrupt;
// Ensure we halt the program on panic (if we don't mention this crate it won't
// be linked)
2021-09-10 23:39:01 +10:00
use panic_halt as _;
// Pull in any important traits
use embedded_time::fixed_point::FixedPoint;
use pico::hal::prelude::*;
// A shorter alias for the Peripheral Access Crate, which provides low-level
// register access
use pico::hal::pac;
// A shorter alias for the Hardware Abstraction Layer, which provides
// higher-level drivers.
use pico::hal;
// USB Device support
use usb_device::{class_prelude::*, prelude::*};
// USB Human Interface Device (HID) Class support
2021-09-10 23:39:01 +10:00
use usbd_hid::descriptor::generator_prelude::*;
use usbd_hid::descriptor::MouseReport;
use usbd_hid::hid_class::HIDClass;
/// The USB Device Driver (shared with the interrupt).
static mut USB_DEVICE: Option<UsbDevice<hal::usb::UsbBus>> = None;
/// The USB Bus Driver (shared with the interrupt).
static mut USB_BUS: Option<UsbBusAllocator<hal::usb::UsbBus>> = None;
/// The USB Human Interface Device Driver (shared with the interrupt).
static mut USB_HID: Option<HIDClass<hal::usb::UsbBus>> = None;
/// Entry point to our bare-metal application.
///
/// The `#[entry]` macro ensures the Cortex-M start-up code calls this function
/// as soon as all global variables are initialised.
///
/// The function configures the RP2040 peripherals, then submits cursor movement
/// updates periodically.
2021-09-10 23:39:01 +10:00
#[entry]
fn main() -> ! {
// Grab our singleton objects
2021-09-10 23:39:01 +10:00
let mut pac = pac::Peripherals::take().unwrap();
// Set up the watchdog driver - needed by the clock setup code
let mut watchdog = hal::watchdog::Watchdog::new(pac.WATCHDOG);
// Configure the clocks
//
// The default is to generate a 125 MHz system clock
let clocks = hal::clocks::init_clocks_and_plls(
pico::XOSC_CRYSTAL_FREQ,
2021-09-10 23:39:01 +10:00
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut watchdog,
)
.ok()
.unwrap();
// Set up the USB driver
let usb_bus = UsbBusAllocator::new(hal::usb::UsbBus::new(
2021-09-10 23:39:01 +10:00
pac.USBCTRL_REGS,
pac.USBCTRL_DPRAM,
clocks.usb_clock,
true,
&mut pac.RESETS,
));
unsafe {
// Note (safety): This is safe as interrupts haven't been started yet
2021-09-10 23:39:01 +10:00
USB_BUS = Some(usb_bus);
}
2021-09-21 21:43:16 +10:00
// Grab a reference to the USB Bus allocator. We are promising to the
// compiler not to take mutable access to this global variable whilst this
// reference exists!
let bus_ref = unsafe { USB_BUS.as_ref().unwrap() };
// Set up the USB HID Class Device driver, providing Mouse Reports
let usb_hid = HIDClass::new(bus_ref, MouseReport::desc(), 60);
2021-09-10 23:39:01 +10:00
unsafe {
// Note (safety): This is safe as interrupts haven't been started yet.
2021-09-10 23:39:01 +10:00
USB_HID = Some(usb_hid);
}
// Create a USB device with a fake VID and PID
let usb_dev = UsbDeviceBuilder::new(bus_ref, UsbVidPid(0x16c0, 0x27da))
.manufacturer("Fake company")
.product("Twitchy Mousey")
.serial_number("TEST")
.device_class(0xEF) // misc
.build();
2021-09-10 23:39:01 +10:00
unsafe {
// Note (safety): This is safe as interrupts haven't been started yet
2021-09-10 23:39:01 +10:00
USB_DEVICE = Some(usb_dev);
}
unsafe {
// Enable the USB interrupt
2021-09-10 23:39:01 +10:00
pac::NVIC::unmask(hal::pac::Interrupt::USBCTRL_IRQ);
};
let core = pac::CorePeripherals::take().unwrap();
let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().integer());
// Move the cursor up and down every 200ms
2021-09-10 23:39:01 +10:00
loop {
delay.delay_ms(100);
let rep_up = MouseReport {
2021-09-10 23:39:01 +10:00
x: 0,
y: 4,
buttons: 0,
wheel: 0,
pan: 0,
};
push_mouse_movement(rep_up).ok().unwrap_or(0);
2021-09-10 23:39:01 +10:00
delay.delay_ms(100);
let rep_down = MouseReport {
2021-09-10 23:39:01 +10:00
x: 0,
y: -4,
buttons: 0,
wheel: 0,
pan: 0,
};
push_mouse_movement(rep_down).ok().unwrap_or(0);
2021-09-10 23:39:01 +10:00
}
}
/// Submit a new mouse movement report to the USB stack.
///
/// We do this with interrupts disabled, to avoid a race hazard with the USB IRQ.
2021-09-10 23:39:01 +10:00
fn push_mouse_movement(report: MouseReport) -> Result<usize, usb_device::UsbError> {
cortex_m::interrupt::free(|_| unsafe {
// Now interrupts are disabled, grab the global variable and, if
// available, send it a HID report
USB_HID.as_mut().map(|hid| hid.push_input(&report))
})
.unwrap()
2021-09-10 23:39:01 +10:00
}
/// This function is called whenever the USB Hardware generates an Interrupt
/// Request.
2021-09-10 23:39:01 +10:00
#[allow(non_snake_case)]
#[interrupt]
unsafe fn USBCTRL_IRQ() {
// Handle USB request
let usb_dev = USB_DEVICE.as_mut().unwrap();
let usb_hid = USB_HID.as_mut().unwrap();
usb_dev.poll(&mut [usb_hid]);
}
// End of file