diff --git a/rumcake-macros/src/hw/stm32.rs b/rumcake-macros/src/hw/stm32.rs index 48a0b31..a4225a4 100644 --- a/rumcake-macros/src/hw/stm32.rs +++ b/rumcake-macros/src/hw/stm32.rs @@ -238,9 +238,10 @@ fn setup_adc_inner(adc_definition: &STM32AdcSamplerDefinition) -> (TokenStream, ) }, quote! { - ::rumcake::hw::platform::Channel::new( - unsafe { ::rumcake::hw::platform::embassy_stm32::peripherals::#pin::steal() } - ) + unsafe { + use ::rumcake::hw::platform::embassy_stm32::adc::AdcChannel; + ::rumcake::hw::platform::embassy_stm32::peripherals::#pin::steal().degrade_adc() + } }, ) } @@ -251,9 +252,10 @@ fn setup_adc_inner(adc_definition: &STM32AdcSamplerDefinition) -> (TokenStream, ::rumcake::hw::platform::AnalogPinType::Direct }, quote! { - ::rumcake::hw::platform::Channel::new( - unsafe { ::rumcake::hw::platform::embassy_stm32::peripherals::#pin::steal() } - ) + unsafe { + use ::rumcake::hw::platform::embassy_stm32::adc::AdcChannel; + ::rumcake::hw::platform::embassy_stm32::peripherals::#pin::steal().degrade_adc() + } }, ) }, diff --git a/rumcake-macros/src/keyboard.rs b/rumcake-macros/src/keyboard.rs index de3e497..ace9df7 100644 --- a/rumcake-macros/src/keyboard.rs +++ b/rumcake-macros/src/keyboard.rs @@ -376,15 +376,14 @@ pub(crate) fn keyboard_main( if keyboard.usb && (keyboard.via.is_some() || keyboard.vial.is_some()) { initialization.extend(quote! { - static VIA_COMMAND_HANDLER: ::rumcake::usb::ViaCommandHandler<#kb_name> = ::rumcake::usb::ViaCommandHandler::new(); // Via HID setup let (via_reader, via_writer) = - ::rumcake::usb::setup_usb_via_hid_reader_writer(&VIA_COMMAND_HANDLER, &mut builder).split(); + ::rumcake::usb::setup_usb_via_hid_reader_writer(&mut builder).split(); }); spawning.extend(quote! { // HID raw report (for VIA) reading and writing spawner - .spawn(::rumcake::usb_hid_via_read_task!(&VIA_COMMAND_HANDLER, via_reader)) + .spawn(::rumcake::usb_hid_via_read_task!(#kb_name, via_reader)) .unwrap(); }); spawning.extend(quote! { diff --git a/rumcake/Cargo.toml b/rumcake/Cargo.toml index 788cfae..21f7e2a 100644 --- a/rumcake/Cargo.toml +++ b/rumcake/Cargo.toml @@ -41,14 +41,14 @@ embedded-io-async = "0.6.0" embedded-graphics = { version = "0.8.1", features = ["defmt"] } embedded-layout = "0.3.2" embedded-text = "0.6.2" -embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "b8be126", features = ["defmt"] } -embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "b8be126" } -embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "b8be126", features = ["defmt", "integrated-timers", "executor-thread"] } -embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "b8be126", features = ["defmt", "defmt-timestamp-uptime"] } -embassy-usb = { git = "https://github.com/embassy-rs/embassy", rev = "b8be126", features = ["defmt"] } -embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "b8be126", features = ["defmt", "unstable-pac"], optional = true } -embassy-stm32 = { git = "https://github.com/embassy-rs/embassy", rev = "b8be126", features = ["defmt", "unstable-pac", "exti"], optional = true } -embassy-nrf = { git = "https://github.com/embassy-rs/embassy", rev = "b8be126", features = ["defmt", "nfc-pins-as-gpio", "time-driver-rtc1"], optional = true } +embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "01c1cb2", features = ["defmt"] } +embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "01c1cb2" } +embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "01c1cb2", features = ["defmt", "integrated-timers", "executor-thread"] } +embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "01c1cb2", features = ["defmt", "defmt-timestamp-uptime"] } +embassy-usb = { git = "https://github.com/embassy-rs/embassy", rev = "01c1cb2", features = ["defmt"] } +embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "01c1cb2", features = ["defmt", "unstable-pac"], optional = true } +embassy-stm32 = { git = "https://github.com/embassy-rs/embassy", rev = "01c1cb2", features = ["defmt", "unstable-pac", "exti"], optional = true } +embassy-nrf = { git = "https://github.com/embassy-rs/embassy", rev = "01c1cb2", features = ["defmt", "nfc-pins-as-gpio", "time-driver-rtc1"], optional = true } nrf-softdevice = { git = "https://github.com/embassy-rs/nrf-softdevice", rev = "487f98e", optional = true } tickv = { git = "https://github.com/tock/tock", rev = "18cf287" } keyberon = { path = "../keyberon" } diff --git a/rumcake/src/hw/mcu/nrf.rs b/rumcake/src/hw/mcu/nrf.rs index 0ad6604..828e5e2 100644 --- a/rumcake/src/hw/mcu/nrf.rs +++ b/rumcake/src/hw/mcu/nrf.rs @@ -102,8 +102,6 @@ pub fn setup_usb_driver() -> embassy_usb:: embassy_nrf::usb::vbus_detect::HardwareVbusDetect::new(Irqs), ); - static DEVICE_DESCRIPTOR: StaticCell<[u8; 256]> = StaticCell::new(); - let device_descriptor = DEVICE_DESCRIPTOR.init([0; 256]); static CONFIG_DESCRIPTOR: StaticCell<[u8; 256]> = StaticCell::new(); let config_descriptor = CONFIG_DESCRIPTOR.init([0; 256]); static BOS_DESCRIPTOR: StaticCell<[u8; 256]> = StaticCell::new(); @@ -116,7 +114,6 @@ pub fn setup_usb_driver() -> embassy_usb:: embassy_usb::Builder::new( usb_driver, config, - device_descriptor, config_descriptor, bos_descriptor, msos_descriptor, diff --git a/rumcake/src/hw/mcu/rp.rs b/rumcake/src/hw/mcu/rp.rs index ef87fff..23d235e 100644 --- a/rumcake/src/hw/mcu/rp.rs +++ b/rumcake/src/hw/mcu/rp.rs @@ -75,9 +75,6 @@ pub fn setup_usb_driver( let usb_driver = Driver::new(USB::steal(), Irqs); - static DEVICE_DESCRIPTOR: static_cell::StaticCell<[u8; 256]> = - static_cell::StaticCell::new(); - let device_descriptor = DEVICE_DESCRIPTOR.init([0; 256]); static CONFIG_DESCRIPTOR: static_cell::StaticCell<[u8; 256]> = static_cell::StaticCell::new(); let config_descriptor = CONFIG_DESCRIPTOR.init([0; 256]); @@ -91,7 +88,6 @@ pub fn setup_usb_driver( embassy_usb::Builder::new( usb_driver, config, - device_descriptor, config_descriptor, bos_descriptor, msos_descriptor, diff --git a/rumcake/src/hw/mcu/stm32.rs b/rumcake/src/hw/mcu/stm32.rs index 2b48a38..7341672 100644 --- a/rumcake/src/hw/mcu/stm32.rs +++ b/rumcake/src/hw/mcu/stm32.rs @@ -5,13 +5,10 @@ //! can remain hardware-agnostic. use core::cell::RefCell; -use core::future::poll_fn; -use core::marker::PhantomData; use core::ops::DerefMut; -use core::task::Poll; use embassy_futures::block_on; -use embassy_stm32::adc::{Adc, AdcPin, Instance, InterruptHandler, SampleTime}; +use embassy_stm32::adc::{Adc, AnyAdcChannel, Instance, InterruptHandler, SampleTime}; use embassy_stm32::flash::{Blocking, Flash as HALFlash}; use embassy_stm32::gpio::Output; use embassy_stm32::interrupt::typelevel::Binding; @@ -139,9 +136,6 @@ pub fn setup_usb_driver( let usb_driver = Driver::new(USB::steal(), Irqs, PA12::steal(), PA11::steal()); - static DEVICE_DESCRIPTOR: static_cell::StaticCell<[u8; 256]> = - static_cell::StaticCell::new(); - let device_descriptor = DEVICE_DESCRIPTOR.init([0; 256]); static CONFIG_DESCRIPTOR: static_cell::StaticCell<[u8; 256]> = static_cell::StaticCell::new(); let config_descriptor = CONFIG_DESCRIPTOR.init([0; 256]); @@ -155,7 +149,6 @@ pub fn setup_usb_driver( embassy_usb::Builder::new( usb_driver, config, - device_descriptor, config_descriptor, bos_descriptor, msos_descriptor, @@ -164,23 +157,6 @@ pub fn setup_usb_driver( } } -pub struct Channel { - channel: u8, - _phantom: PhantomData, -} - -impl Channel { - pub fn new(mut pin: impl AdcPin) -> Self { - #[cfg(feature = "stm32f072cb")] - pin.set_as_analog(); - - Self { - channel: pin.channel(), - _phantom: PhantomData, - } - } -} - /// Different types of analog pins. pub enum AnalogPinType<'a, const MP: usize> { /// A pin that is connected to an analog multiplexer. Must contain a [`Multiplexer`] @@ -200,9 +176,9 @@ pub struct AdcSampler<'a, ADC: Instance, const MP: usize, const C: usize> { } struct RawAdcSampler<'a, ADC: Instance, const MP: usize, const C: usize> { - _adc: Adc<'a, ADC>, + adc: Adc<'a, ADC>, idx_to_pin_type: [AnalogPinType<'a, MP>; C], - analog_pins: [Channel; C], + analog_pins: [AnyAdcChannel; C], } impl<'a, ADC: Instance, const MP: usize, const C: usize> AdcSampler<'a, ADC, MP, C> { @@ -211,97 +187,20 @@ impl<'a, ADC: Instance, const MP: usize, const C: usize> AdcSampler<'a, ADC, MP, adc: impl Peripheral

+ 'a, irq: impl Binding> + 'a, idx_to_pin_type: [AnalogPinType<'a, MP>; C], - analog_pins: [Channel; C], + analog_pins: [AnyAdcChannel; C], ) -> Self { - let _adc = Adc::new(adc, irq, &mut embassy_time::Delay); - - #[cfg(feature = "stm32f303cb")] - for Channel { channel: ch, .. } in analog_pins.iter() { - let sample_time = SampleTime::CYCLES1_5; - if *ch <= 9 { - ADC::regs() - .smpr1() - .modify(|reg| reg.set_smp(*ch as _, sample_time)); - } else { - ADC::regs() - .smpr2() - .modify(|reg| reg.set_smp((ch - 10) as _, sample_time)); - } - } - - #[cfg(feature = "stm32f072cb")] - { - let sample_time = SampleTime::CYCLES1_5; - ADC::regs().smpr().modify(|reg| reg.set_smp(sample_time)) - } + let adc = Adc::new(adc, irq); + adc.set_sample_time(SampleTime::CYCLES1_5); Self { adc_sampler: BlockingMutex::new(RefCell::new(RawAdcSampler { - _adc, + adc, idx_to_pin_type, analog_pins, })), } } - async fn read(&self, ch: &mut Channel) -> AdcSampleType { - // This is just Adc::read and Adc::convert inlined. - // Sample time setup is done in `Self::new()`. - - #[cfg(feature = "stm32f303cb")] - { - ADC::regs().sqr1().write(|w| w.set_sq(0, ch.channel)); - - ADC::regs().isr().write(|_| {}); - - ADC::regs().ier().modify(|w| w.set_eocie(true)); - ADC::regs().cr().modify(|w| w.set_adstart(true)); - - poll_fn(|cx| { - ADC::state().waker.register(cx.waker()); - - if ADC::regs().isr().read().eoc() { - Poll::Ready(()) - } else { - Poll::Pending - } - }) - .await; - - ADC::regs().isr().write(|_| {}); - - ADC::regs().dr().read().rdata() - } - - #[cfg(feature = "stm32f072cb")] - { - ADC::regs() - .chselr() - .write(|reg| reg.set_chselx(ch.channel as usize, true)); - - ADC::regs().isr().modify(|reg| { - reg.set_eoc(true); - reg.set_eosmp(true); - }); - - ADC::regs().ier().modify(|w| w.set_eocie(true)); - ADC::regs().cr().modify(|reg| reg.set_adstart(true)); - - poll_fn(|cx| { - ADC::state().waker.register(cx.waker()); - - if ADC::regs().isr().read().eoc() { - Poll::Ready(()) - } else { - Poll::Pending - } - }) - .await; - - ADC::regs().dr().read().data() - } - } - /// Obtain a sample from the ADC. The `ch` argument corresponds to the index of the analog pin /// you want to sample (which you provided in the [`Self::new`] method). If the pin is /// multiplexed, the `sub_ch` argument is used to determine which multiplexer channel to sample @@ -310,17 +209,17 @@ impl<'a, ADC: Instance, const MP: usize, const C: usize> AdcSampler<'a, ADC, MP, self.adc_sampler.lock(|adc_sampler| { let mut adc_sampler = adc_sampler.borrow_mut(); let RawAdcSampler { + adc, idx_to_pin_type, analog_pins, - .. } = adc_sampler.deref_mut(); idx_to_pin_type.get_mut(ch).map(|channel| match channel { AnalogPinType::Multiplexed(ref mut multiplexer) => { multiplexer.select_channel(sub_ch as u8).unwrap(); - block_on(self.read(&mut analog_pins[ch])) + block_on(adc.read(&mut analog_pins[ch])) } - AnalogPinType::Direct => block_on(self.read(&mut analog_pins[ch])), + AnalogPinType::Direct => block_on(adc.read(&mut analog_pins[ch])), }) }) } diff --git a/rumcake/src/keyboard.rs b/rumcake/src/keyboard.rs index b1e97e3..d039304 100644 --- a/rumcake/src/keyboard.rs +++ b/rumcake/src/keyboard.rs @@ -540,6 +540,7 @@ where let mut ticker = Ticker::every(Duration::from_millis(1)); let matrix_channel = K::get_matrix_events_channel(); + let matrix_event_publisher = MATRIX_EVENTS.immediate_publisher(); #[cfg(feature = "media-keycodes")] let consumer_report_channel = K::get_consumer_report_send_channel(); @@ -560,7 +561,7 @@ where if let Some(event) = event { layout.event(event); - MATRIX_EVENTS.publish_immediate(event); // Just immediately publish since we don't want to hold up any key events to be converted into keycodes. + matrix_event_publisher.publish_immediate(event); // Just immediately publish since we don't want to hold up any key events to be converted into keycodes. }; let tick = layout.tick(); diff --git a/rumcake/src/split/peripheral.rs b/rumcake/src/split/peripheral.rs index 21e18ec..15ee9cd 100644 --- a/rumcake/src/split/peripheral.rs +++ b/rumcake/src/split/peripheral.rs @@ -115,6 +115,7 @@ impl From> for PeripheralDeviceError { #[rumcake_macros::task] pub async fn peripheral_task(_k: K, mut driver: impl PeripheralDeviceDriver) { let channel = K::get_matrix_events_channel(); + let matrix_event_publisher = MATRIX_EVENTS.immediate_publisher(); loop { match select( @@ -160,7 +161,7 @@ pub async fn peripheral_task(_k: K, mut driver: impl Periph } }, Either::Second(event) => { - MATRIX_EVENTS.publish_immediate(event); + matrix_event_publisher.publish_immediate(event); if let Err(err) = driver.send_message_to_central(event.into()).await { error!( diff --git a/rumcake/src/usb.rs b/rumcake/src/usb.rs index 6726cd6..ab7242d 100644 --- a/rumcake/src/usb.rs +++ b/rumcake/src/usb.rs @@ -171,32 +171,22 @@ pub async fn usb_hid_consumer_write_task( } #[cfg(feature = "via")] -pub struct ViaCommandHandler { +struct ViaCommandHandler { _phantom: PhantomData, } -#[cfg(feature = "via")] -impl ViaCommandHandler { - pub const fn new() -> Self { - Self { - _phantom: PhantomData, - } - } -} - #[cfg(feature = "via")] /// Configure the HID report reader and writer for Via/Vial packets. /// /// The reader should be passed to [`usb_hid_via_read_task`], and the writer should be passed to /// [`usb_hid_via_write_task`]. -pub fn setup_usb_via_hid_reader_writer( - command_handler: &'static ViaCommandHandler, +pub fn setup_usb_via_hid_reader_writer( builder: &mut Builder<'static, impl Driver<'static>>, ) -> HidReaderWriter<'static, impl Driver<'static>, 32, 32> { static VIA_STATE: StaticCell = StaticCell::new(); let via_state = VIA_STATE.init(UsbState::new()); let via_hid_config = Config { - request_handler: Some(command_handler), + request_handler: None, report_descriptor: crate::via::VIA_REPORT_DESCRIPTOR, poll_ms: 1, max_packet_size: 32, @@ -206,11 +196,11 @@ pub fn setup_usb_via_hid_reader_writer( #[cfg(feature = "via")] impl RequestHandler for ViaCommandHandler { - fn get_report(&self, _id: ReportId, _buf: &mut [u8]) -> Option { + fn get_report(&mut self, _id: ReportId, _buf: &mut [u8]) -> Option { None } - fn set_report(&self, _id: ReportId, buf: &[u8]) -> OutResponse { + fn set_report(&mut self, _id: ReportId, buf: &[u8]) -> OutResponse { let mut data: [u8; 32] = [0; 32]; data.copy_from_slice(buf); @@ -226,20 +216,26 @@ impl RequestHandler for ViaCommandHandler { OutResponse::Accepted } - fn get_idle_ms(&self, _id: Option) -> Option { + fn get_idle_ms(&mut self, _id: Option) -> Option { None } - fn set_idle_ms(&self, _id: Option, _duration_ms: u32) {} + fn set_idle_ms(&mut self, _id: Option, _duration_ms: u32) {} } #[cfg(feature = "via")] #[rumcake_macros::task] pub async fn usb_hid_via_read_task( - command_handler: &ViaCommandHandler, + _kb: T, hid: HidReader<'static, impl Driver<'static>, 32>, ) { - hid.run(false, command_handler).await; + hid.run( + false, + &mut ViaCommandHandler { + _phantom: PhantomData as PhantomData, + }, + ) + .await; } #[cfg(feature = "via")]