Skip to content

Instantly share code, notes, and snippets.

@barafael
Last active February 10, 2022 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barafael/5cd872d3736f9b64f934c4803c219ac1 to your computer and use it in GitHub Desktop.
Save barafael/5cd872d3736f9b64f934c4803c219ac1 to your computer and use it in GitHub Desktop.
--- rtic_v0.5/stm32f0_hid_mouse/src/main.rs 2022-02-10 14:15:04.270648800 +0100
+++ rtic_v1/stm32f0_hid_mouse/src/main.rs 2022-02-10 10:32:55.309537420 +0100
@@ -1,13 +1,16 @@
+/*
#![deny(unsafe_code)]
+*/
#![deny(warnings)]
#![no_std]
#![no_main]
+use cortex_m::interrupt::free as disable_interrupts;
use panic_halt as _;
use rtic::app;
-use rtic::{Exclusive, Mutex};
+use rtic::Exclusive;
+use rtic::Mutex;
use rtt_target::{rprintln, rtt_init_print};
-
use stm32f0xx_hal::{
gpio::gpioa::{PA0, PA1, PA15, PA2, PA3, PA4, PA5, PA6, PA7},
gpio::gpiob::{PB1, PB3, PB4},
@@ -16,30 +19,37 @@
prelude::*,
usb,
};
-
+use systick_monotonic::Systick;
use usb_device::{bus::UsbBusAllocator, prelude::*};
+use usbd_hid::{
+ descriptor::{MouseReport, SerializedDescriptor},
+ hid_class::HIDClass,
+};
-use usbd_hid::descriptor::generator_prelude::*;
-use usbd_hid::descriptor::MouseReport;
-use usbd_hid::hid_class::HIDClass;
+#[app(device = stm32f0xx_hal::pac, peripherals = true)]
+mod app {
+ use super::*;
-use cortex_m::interrupt::free as disable_interrupts;
+ #[monotonic(binds = SysTick)]
+ type Tonic = Systick<1000>;
-#[app(device = stm32f0xx_hal::pac, peripherals = true)]
-const APP: () = {
- struct Resources {
- usb_bus: &'static UsbBusAllocator<usb::UsbBusType>,
+ #[local]
+ struct Local {
+ usr_led: PB1<Output<PushPull>>,
usb_device: UsbDevice<'static, usb::UsbBusType>,
+ }
+
+ #[shared]
+ struct Shared {
usb_hid: HIDClass<'static, usb::UsbBusType>,
exti: pac::EXTI,
- usr_led: PB1<Output<PushPull>>,
- button3: PA15<Input<PullUp>>,
- button4: PB4<Input<PullUp>>,
- button5: PB3<Input<PullUp>>,
- tb_left: PA4<Input<PullUp>>,
- tb_up: PA5<Input<PullUp>>,
- tb_right: PA6<Input<PullUp>>,
- tb_down: PA7<Input<PullUp>>,
+ _button3: PA15<Input<PullUp>>,
+ _button4: PB4<Input<PullUp>>,
+ _button5: PB3<Input<PullUp>>,
+ _tb_left: PA4<Input<PullUp>>,
+ _tb_up: PA5<Input<PullUp>>,
+ _tb_right: PA6<Input<PullUp>>,
+ _tb_down: PA7<Input<PullUp>>,
bbled_red: PA0<Output<PushPull>>,
bbled_grn: PA1<Output<PushPull>>,
bbled_blu: PA2<Output<PushPull>>,
@@ -47,9 +57,7 @@
}
#[init]
- fn init(ctx: init::Context) -> init::LateResources {
- static mut USB_BUS: Option<UsbBusAllocator<usb::UsbBusType>> = None;
-
+ fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) {
// RTT handler
rtt_init_print!();
@@ -59,9 +67,6 @@
// This enables clock for SYSCFG and remaps USB pins to PA9 and PA10.
usb::remap_pins(&mut dp.RCC, &mut dp.SYSCFG);
- // Power on bbled dance
- //bbled_red.toggle().ok();
-
rprintln!("Initializing peripherals");
let mut rcc = dp
.RCC
@@ -73,9 +78,11 @@
.pclk(24.mhz())
.freeze(&mut dp.FLASH);
+ let mono = Systick::new(ctx.core.SYST, 36_000_000);
+
// Set up GPIO registers for USR LED and Buttons
let gpiob = dp.GPIOB.split(&mut rcc);
- let (usr_led, button4, button5) = disable_interrupts(|cs| {
+ let (usr_led, _button4, _button5) = disable_interrupts(|cs| {
(
gpiob.pb1.into_push_pull_output(cs),
gpiob.pb4.into_pull_up_input(cs),
@@ -90,11 +97,11 @@
bbled_grn,
bbled_blu,
bbled_wht,
- tb_left,
- tb_up,
- tb_right,
- tb_down,
- button3,
+ _tb_left,
+ _tb_up,
+ _tb_right,
+ _tb_down,
+ _button3,
usb_dm,
usb_dp,
) = disable_interrupts(|cs| {
@@ -113,6 +120,9 @@
)
});
+ // Power on bbled dance
+ //bbled_red.toggle().ok();
+
// Enable external interrupt for 3 aux buttons...
dp.SYSCFG.exticr1.write(|w| w.exti3().pb3());
// dp.SYSCFG.exticr2.write(|w| { w.exti4().pb4() }); // Disable spare button in favor of tb_left
@@ -149,46 +159,59 @@
pin_dp: usb_dp,
};
- *USB_BUS = Some(usb::UsbBus::new(usb));
-
rprintln!("Preparing HID mouse...");
- let usb_hid = HIDClass::new(USB_BUS.as_ref().unwrap(), MouseReport::desc(), 60);
+ static mut USB_BUS: Option<UsbBusAllocator<usb::UsbBusType>> = None;
+ unsafe {
+ USB_BUS = Some(usb::UsbBus::new(usb));
+ };
+ let usb_hid = HIDClass::new(
+ unsafe { USB_BUS.as_ref().unwrap() },
+ MouseReport::desc(),
+ 60,
+ );
rprintln!("Defining USB parameters...");
- let usb_device = UsbDeviceBuilder::new(USB_BUS.as_ref().unwrap(), UsbVidPid(0, 0x3821))
- .manufacturer("JoshFTW")
- .product("BBTrackball")
- .serial_number("RustFW")
- .device_class(0x00)
- .device_sub_class(0x00)
- .device_protocol(0x00)
- .build();
+ let usb_device =
+ UsbDeviceBuilder::new(unsafe { USB_BUS.as_ref().unwrap() }, UsbVidPid(0, 0x3821))
+ .manufacturer("JoshFTW")
+ .product("BBTrackball")
+ .serial_number("RustFW")
+ .device_class(0x00)
+ .device_sub_class(0x00)
+ .device_protocol(0x00)
+ .build();
rprintln!("Instantiating dp.EXTI...");
let exti = dp.EXTI;
- rprintln!("Defining late resources...");
- init::LateResources {
- usb_bus: USB_BUS.as_ref().unwrap(),
- usb_device,
+ rprintln!("Defining shared resources...");
+ let shared = Shared {
usb_hid,
exti,
- usr_led,
- button3,
- button4,
- button5,
- tb_left,
- tb_up,
- tb_right,
- tb_down,
+ _button3,
+ _button4,
+ _button5,
+ _tb_left,
+ _tb_up,
+ _tb_right,
+ _tb_down,
bbled_red,
bbled_grn,
bbled_blu,
bbled_wht,
- }
+ };
+
+ (
+ shared,
+ Local {
+ usr_led,
+ usb_device,
+ },
+ init::Monotonics(mono),
+ )
}
- #[idle(resources = [usb_device, usb_hid])]
+ #[idle]
fn idle(_: idle::Context) -> ! {
loop {
cortex_m::asm::nop();
@@ -196,74 +219,97 @@
}
}
- #[task(binds = EXTI2_3, resources = [exti])]
- fn exti2_3_interrupt(ctx: exti2_3_interrupt::Context) {
+ #[task(binds = EXTI2_3, shared = [exti])]
+ fn exti2_3_interrupt(mut ctx: exti2_3_interrupt::Context) {
rprintln!("Interrupts happening on EXTI2_3");
- match ctx.resources.exti.pr.read().bits() {
+ match ctx.shared.exti.lock(|exti| exti.pr.read().bits()) {
0x8 => {
rprintln!("PB3 triggered");
- ctx.resources.exti.pr.write(|w| w.pif3().set_bit()); // Clear interrupt
+ ctx.shared
+ .exti
+ .lock(|exti| exti.pr.write(|w| w.pif3().set_bit())); // Clear interrupt
}
_ => rprintln!("Some other bits were pushed around on EXTI2_3 ;)"),
}
}
- #[task(binds = EXTI4_15, resources = [exti, usb_device, usb_hid, usr_led, bbled_red, bbled_grn, bbled_wht, bbled_blu])]
- fn exti_4_15_interrupt(ctx: exti_4_15_interrupt::Context) {
+ #[task(binds = EXTI4_15, local = [usr_led], shared = [exti, usb_hid, bbled_red, bbled_grn, bbled_wht, bbled_blu])]
+ fn exti_4_15_interrupt(mut ctx: exti_4_15_interrupt::Context) {
rprintln!("Interrupts happening on EXTI for PA15...");
- let hid = ctx.resources.usb_hid;
- let usr_led = ctx.resources.usr_led;
-
- match ctx.resources.exti.pr.read().bits() {
+ match ctx.shared.exti.lock(|exti| exti.pr.read().bits()) {
0x8000 => {
rprintln!("PA15 triggered");
- ctx.resources.exti.pr.write(|w| w.pif15().set_bit()); // Clear interrupt
- send_mouse_report(Exclusive(hid), 0, 0, 1);
- usr_led.toggle().ok();
+ ctx.shared
+ .exti
+ .lock(|exti| exti.pr.write(|w| w.pif15().set_bit())); // Clear interrupt
+
+ ctx.shared
+ .usb_hid
+ .lock(|hid| super::send_mouse_report(Exclusive(hid), 0, 0, 1));
+ ctx.local.usr_led.toggle().ok();
}
0x10 => {
rprintln!("tb_left triggered!");
- ctx.resources.exti.pr.write(|w| w.pif4().set_bit());
- send_mouse_report(Exclusive(hid), 5, 0, 0);
- usr_led.toggle().ok();
+ ctx.shared
+ .exti
+ .lock(|exti| exti.pr.write(|w| w.pif4().set_bit()));
+ ctx.shared
+ .usb_hid
+ .lock(|hid| super::send_mouse_report(Exclusive(hid), 5, 0, 0));
+ ctx.local.usr_led.toggle().ok();
}
0x20 => {
rprintln!("tb_up triggered!");
- ctx.resources.exti.pr.write(|w| w.pif5().set_bit());
- send_mouse_report(Exclusive(hid), 0, 5, 0);
- usr_led.toggle().ok();
+ ctx.shared
+ .exti
+ .lock(|exti| exti.pr.write(|w| w.pif5().set_bit()));
+
+ ctx.shared
+ .usb_hid
+ .lock(|hid| super::send_mouse_report(Exclusive(hid), 0, 5, 0));
+ ctx.local.usr_led.toggle().ok();
}
0x40 => {
rprintln!("tb_right triggered!");
- ctx.resources.exti.pr.write(|w| w.pif6().set_bit());
- send_mouse_report(Exclusive(hid), -5, 0, 0);
- usr_led.toggle().ok();
+ ctx.shared
+ .exti
+ .lock(|exti| exti.pr.write(|w| w.pif6().set_bit()));
+
+ ctx.shared
+ .usb_hid
+ .lock(|hid| super::send_mouse_report(Exclusive(hid), -5, 0, 0));
+ ctx.local.usr_led.toggle().ok();
}
0x80 => {
rprintln!("tb_down triggered!");
- ctx.resources.exti.pr.write(|w| w.pif7().set_bit());
- send_mouse_report(Exclusive(hid), 0, -5, 0);
- usr_led.toggle().ok();
+ ctx.shared
+ .exti
+ .lock(|exti| exti.pr.write(|w| w.pif7().set_bit()));
+
+ ctx.shared
+ .usb_hid
+ .lock(|hid| super::send_mouse_report(Exclusive(hid), 0, -5, 0));
+ ctx.local.usr_led.toggle().ok();
}
_ => rprintln!("Some other bits were pushed around on EXTI4_15 ;)"),
}
}
- #[task(binds = USB, resources = [usb_device, usb_hid])]
- fn usb_handler(ctx: usb_handler::Context) {
+ #[task(binds = USB, local = [usb_device], shared = [usb_hid])]
+ fn usb_handler(mut ctx: usb_handler::Context) {
rprintln!("USB interrupt received.");
- let dev = ctx.resources.usb_device;
- let hid = ctx.resources.usb_hid;
-
- // USB dev poll only in the interrupt handler
- dev.poll(&mut [hid]);
+ let device = ctx.local.usb_device;
+ ctx.shared.usb_hid.lock(|hid| {
+ // USB dev poll only in the interrupt handler
+ device.poll(&mut [hid]);
+ });
}
-};
+}
fn send_mouse_report(
mut shared_hid: impl Mutex<T = HIDClass<'static, usb::UsbBusType>>,
@@ -271,7 +317,13 @@
y: i8,
buttons: u8,
) {
- let mr = MouseReport { x, y, buttons };
+ let mr = MouseReport {
+ x,
+ y,
+ buttons,
+ wheel: 0,
+ pan: 0,
+ };
shared_hid.lock(|hid| {
rprintln!("Sending mouse report...");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment