From 4d702b9b02f0c503547cecc1c6ea075cb2a2e596 Mon Sep 17 00:00:00 2001 From: Marc Poulhiès Date: Fri, 22 Jul 2022 21:01:10 +0200 Subject: Update all crate, in particular rtic Keyboard works, but customevents for leds are not working (yet). --- firmware/rust/Cargo.lock | 99 ++++++---------------------- firmware/rust/Cargo.toml | 2 +- firmware/rust/src/main.rs | 165 +++++++++++++++++++++++++++------------------- 3 files changed, 118 insertions(+), 148 deletions(-) diff --git a/firmware/rust/Cargo.lock b/firmware/rust/Cargo.lock index eb2f956..4764d91 100644 --- a/firmware/rust/Cargo.lock +++ b/firmware/rust/Cargo.lock @@ -17,18 +17,6 @@ version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0ffd3d69bd89910509a5d31d1f1353f38ccffdd116dd0099bbd6627f7bd8ad8" -[[package]] -name = "as-slice" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45403b49e3954a4b8428a0ac21a4b7afadccf92bfd96273f1a58cd4812496ae0" -dependencies = [ - "generic-array 0.12.4", - "generic-array 0.13.3", - "generic-array 0.14.5", - "stable_deref_trait", -] - [[package]] name = "atomic-polyfill" version = "0.1.8" @@ -146,23 +134,26 @@ dependencies = [ [[package]] name = "cortex-m-rtic" -version = "0.5.9" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb77bb72c171d0aa52a2954d8dc7e3f0800ab54b8df7d826f138abbea9eb2794" +checksum = "c6b82f1c39acd6c3a35c2013b6110c20f5bc534522791fabadeed49ccada2dce" dependencies = [ + "bare-metal 1.0.0", "cortex-m", "cortex-m-rtic-macros", - "heapless 0.6.1", + "heapless", "rtic-core", + "rtic-monotonic", "version_check", ] [[package]] name = "cortex-m-rtic-macros" -version = "0.5.3" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc874eda99515b15e67f03562726a530388f454431096d30131051b52b840559" +checksum = "9e8e9645ef54bec1cf70ac33e9bf9566e6507ab5b41ae6baf3735662194e8607" dependencies = [ + "proc-macro-error", "proc-macro2", "quote", "rtic-syntax", @@ -197,43 +188,6 @@ dependencies = [ "void", ] -[[package]] -name = "generic-array" -version = "0.12.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffdf9f34f1447443d37393cc6c2b8313aebddcd96906caf34e54c68d8e57d7bd" -dependencies = [ - "typenum", -] - -[[package]] -name = "generic-array" -version = "0.13.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f797e67af32588215eaaab8327027ee8e71b9dd0b2b26996aedf20c030fce309" -dependencies = [ - "typenum", -] - -[[package]] -name = "generic-array" -version = "0.14.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803" -dependencies = [ - "typenum", - "version_check", -] - -[[package]] -name = "hash32" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4041af86e63ac4298ce40e5cca669066e75b6f1aa3390fe2561ffa5e1d9f4cc" -dependencies = [ - "byteorder", -] - [[package]] name = "hash32" version = "0.2.1" @@ -249,18 +203,6 @@ version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "607c8a29735385251a339424dd462993c0fed8fa09d378f259377df08c126022" -[[package]] -name = "heapless" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "634bd4d29cbf24424d0a4bfcbf80c6960129dc24424752a7d1d1390607023422" -dependencies = [ - "as-slice", - "generic-array 0.14.5", - "hash32 0.1.1", - "stable_deref_trait", -] - [[package]] name = "heapless" version = "0.7.15" @@ -268,7 +210,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f6733da246dc2af610133c8be0667170fd68e8ca5630936b520300eee8846f9" dependencies = [ "atomic-polyfill", - "hash32 0.2.1", + "hash32", "rustc_version 0.4.0", "spin", "stable_deref_trait", @@ -292,7 +234,7 @@ dependencies = [ "arraydeque", "either", "embedded-hal", - "heapless 0.7.15", + "heapless", "keyberon-macros", "usb-device", ] @@ -458,18 +400,25 @@ dependencies = [ [[package]] name = "rtic-core" -version = "0.3.1" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9369355b04d06a3780ec0f51ea2d225624db777acbc60abd8ca4832da5c1a42" + +[[package]] +name = "rtic-monotonic" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bd58a6949de8ff797a346a28d9f13f7b8f54fa61bb5e3cb0985a4efb497a5ef" +checksum = "fb8b0b822d1a366470b9cea83a1d4e788392db763539dc4ba022bcc787fece82" [[package]] name = "rtic-syntax" -version = "0.4.0" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8152fcaa845720d61e6cc570548b89144c2c307f18a480bbd97e55e9f6eeff04" +checksum = "3ad3ae243dd8d0a1b064615f664d4fa7e63929939074c564cbe5efdc4c503065" dependencies = [ "indexmap", "proc-macro2", + "quote", "syn", ] @@ -601,12 +550,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "typenum" -version = "1.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" - [[package]] name = "unicode-ident" version = "1.0.1" diff --git a/firmware/rust/Cargo.toml b/firmware/rust/Cargo.toml index 54f44cc..870f048 100644 --- a/firmware/rust/Cargo.toml +++ b/firmware/rust/Cargo.toml @@ -11,7 +11,7 @@ cortex-m = "0.7" cortex-m-rt = { version = "0.7", features = ["device"] } panic-halt = "0.2" keyberon = { git = "https://github.com/TeXiToi/keyberon" } -cortex-m-rtic = { version = "0.5", default-features = false, features = ["cortex-m-7"] } +cortex-m-rtic = "1.0.0" embedded-hal = "0.2" usb-device = "0.2" nb = "1.0" diff --git a/firmware/rust/src/main.rs b/firmware/rust/src/main.rs index 523ece9..20d232a 100644 --- a/firmware/rust/src/main.rs +++ b/firmware/rust/src/main.rs @@ -5,17 +5,18 @@ use panic_halt as _; use core::convert::Infallible; -use keyberon::action::{k, l, m, Action::*, HoldTapConfig}; +use keyberon::action::{k, l, Action::*, HoldTapConfig}; use keyberon::key_code::KeyCode::*; - +use keyberon::layout::{Event, Layout}; type Action = keyberon::action::Action; -use keyberon::layout::Layout; + use keyberon::matrix::Matrix; use rtic::app; use stm32f0xx_hal as hal; use usb_device::bus::UsbBusAllocator; use usb_device::class::UsbClass as _; +use usb_device::device::UsbDeviceState; extern crate smart_leds; extern crate ws2812_spi; @@ -37,7 +38,6 @@ use hal::{ use keyberon::debounce::Debouncer; use keyberon::key_code::KbHidReport; -use keyberon::key_code::KeyCode; type Spi = hal::spi::Spi< stm32::SPI1, @@ -367,22 +367,33 @@ impl Backlight { } } -#[app(device = crate::hal::pac, peripherals = true)] -const APP: () = { - struct Resources { + +#[app(device = crate::hal::pac, peripherals = true, dispatchers = [CEC_CAN])] +mod app { + use super::*; + + #[shared] + struct Shared { usb_dev: UsbDevice, usb_class: UsbClass, - matrix: Matrix>, Pin>, 12, 5>, - debouncer: Debouncer<[[bool; 12]; 5]>, + + #[lock_free] layout: Layout<12, 5, 2, CustomActions>, - timer: timers::Timer, + #[lock_free] backlight: Backlight, } - #[init] - fn init(mut c: init::Context) -> init::LateResources { - static mut USB_BUS: Option> = None; + #[local] + struct Local { + matrix: Matrix>, Pin>, 12, 5>, + debouncer: Debouncer<[[bool; 12]; 5]>, + timer: timers::Timer, + } + + #[init(local = [bus: Option> = None])] + fn init(mut c: init::Context) -> (Shared, Local, init::Monotonics) { + let mut rcc = c .device .RCC @@ -401,8 +412,9 @@ const APP: () = { pin_dm: gpioa.pa11, pin_dp: gpioa.pa12, }; - *USB_BUS = Some(usb::UsbBusType::new(usb)); - let usb_bus = USB_BUS.as_ref().unwrap(); + + *c.local.bus = Some(usb::UsbBusType::new(usb)); + let usb_bus = c.local.bus.as_ref().unwrap(); // Handling of ws2812 leds @@ -485,51 +497,51 @@ const APP: () = { ], )}); - init::LateResources { - usb_dev, - usb_class, - timer, - debouncer: Debouncer::new([[false; 12]; 5], [[false; 12]; 5], 5), - matrix: matrix.get(), - layout: Layout::new(&LAYERS), - - backlight: Backlight { - mode: BacklightMode::Off, - brightness: 8, + ( + Shared { + usb_dev, + usb_class, + layout: Layout::new(&LAYERS), + backlight: Backlight { + mode: BacklightMode::Off, + brightness: 8, + }, }, - } + + Local { + timer, + debouncer: Debouncer::new([[false; 12]; 5], [[false; 12]; 5], 5), + matrix: matrix.get(), + }, + init::Monotonics(), + ) } - #[task(binds = USB, priority = 4, resources = [usb_dev, usb_class])] + #[task(binds = USB, priority = 3, shared = [usb_dev, usb_class])] fn usb_rx(c: usb_rx::Context) { - if c.resources.usb_dev.poll(&mut [c.resources.usb_class]) { - c.resources.usb_class.poll(); - } + (c.shared.usb_dev, c.shared.usb_class).lock(|usb_dev, usb_class| { + if usb_dev.poll(&mut [usb_class]) { + usb_class.poll(); + } + }); } - #[task( - binds = TIM3, - priority = 2, - resources = [matrix, debouncer, timer, layout, usb_class, backlight], - )] - fn tick(c: tick::Context) { - c.resources.timer.wait().ok(); - - for event in c - .resources - .debouncer - .events(c.resources.matrix.get().unwrap()) - { - c.resources.layout.event(event); + #[task(priority = 2, shared = [usb_dev, usb_class, layout, backlight])] + fn tick_keyberon(mut c: tick_keyberon::Context) { + let tick = c.shared.layout.tick(); + if c.shared.usb_dev.lock(|d| d.state()) != UsbDeviceState::Configured { + return; } - let mut usb_class = c.resources.usb_class; - let backlight = c.resources.backlight; + // match tick { + // CustomEvent::Release(()) => unsafe { cortex_m::asm::bootload(0x1FFFC800 as _) }, + // _ => (), + // } - match c.resources.layout.tick() { + match tick { keyberon::layout::CustomEvent::Release(CustomActions::LightUp) => { - let bl_val = &mut backlight.brightness; + let bl_val = &mut c.shared.backlight.brightness; *bl_val = if *bl_val == 100 { 100 } else { *bl_val + 1 }; - usb_class.lock(|k| { + c.shared.usb_class.lock(|k| { let leds = k.device_mut().leds_mut(); if leds.ws .write(brightness(leds.leds.iter().cloned(), *bl_val)).is_err() { @@ -538,9 +550,9 @@ const APP: () = { }); } keyberon::layout::CustomEvent::Release(CustomActions::LightDown) => { - let bl_val = &mut backlight.brightness; + let bl_val = &mut c.shared.backlight.brightness; *bl_val = if *bl_val == 0 { 0 } else { *bl_val - 1 }; - usb_class.lock(|k| { + c.shared.usb_class.lock(|k| { let leds = k.device_mut().leds_mut(); if leds.ws .write(brightness(leds.leds.iter().cloned(), *bl_val)).is_err() { @@ -549,37 +561,52 @@ const APP: () = { }); } keyberon::layout::CustomEvent::Release(CustomActions::ColorCycle) => { - backlight.next_color(); + c.shared.backlight.next_color(); } keyberon::layout::CustomEvent::Release(CustomActions::ModeCycle) => { - backlight.next_mode(); + c.shared.backlight.next_mode(); } keyberon::layout::CustomEvent::Release(CustomActions::FreqUp) => { - backlight.change_freq(true); + c.shared.backlight.change_freq(true); } keyberon::layout::CustomEvent::Release(CustomActions::FreqDown) => { - backlight.change_freq(false); + c.shared.backlight.change_freq(false); } _ => (), } - usb_class.lock(|k| { - backlight.refresh_leds(k.device_mut().leds_mut()); - }); - c.resources.layout.tick(); - send_report(c.resources.layout.keycodes(), &mut usb_class); + let report: KbHidReport = c.shared.layout.keycodes().collect(); + if !c + .shared + .usb_class + .lock(|k| k.device_mut().set_keyboard_report(report.clone())) + { + return; + } + while let Ok(0) = c.shared.usb_class.lock(|k| k.write(report.as_bytes())) {} } - extern "C" { - fn CEC_CAN(); + #[task(priority = 2, capacity = 8, shared = [layout])] + fn handle_event(c: handle_event::Context, event: Event) { + c.shared.layout.event(event) } -}; -fn send_report(iter: impl Iterator, usb_class: &mut resources::usb_class<'_>) { - use rtic::Mutex; - let report: KbHidReport = iter.collect(); - if usb_class.lock(|k| k.device_mut().set_keyboard_report(report.clone())) { - while let Ok(0) = usb_class.lock(|k| k.write(report.as_bytes())) {} + #[task( + binds = TIM3, + priority = 1, + local = [matrix, debouncer, timer], + )] + fn tick(c: tick::Context) { + c.local.timer.wait().ok(); + + for event in c + .local + .debouncer + .events(c.local.matrix.get().get()) + { + handle_event::spawn(event).unwrap(); + } + tick_keyberon::spawn().unwrap(); } } -- cgit v1.2.3