Skip to content

Commit

Permalink
update embassy
Browse files Browse the repository at this point in the history
  • Loading branch information
Univa committed May 25, 2024
1 parent e406fbf commit 9a49269
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 156 deletions.
14 changes: 8 additions & 6 deletions rumcake-macros/src/hw/stm32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
},
)
}
Expand All @@ -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()
}
},
)
},
Expand Down
5 changes: 2 additions & 3 deletions rumcake-macros/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down
16 changes: 8 additions & 8 deletions rumcake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
3 changes: 0 additions & 3 deletions rumcake/src/hw/mcu/nrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ pub fn setup_usb_driver<K: crate::usb::USBKeyboard + 'static>() -> 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();
Expand All @@ -116,7 +114,6 @@ pub fn setup_usb_driver<K: crate::usb::USBKeyboard + 'static>() -> embassy_usb::
embassy_usb::Builder::new(
usb_driver,
config,
device_descriptor,
config_descriptor,
bos_descriptor,
msos_descriptor,
Expand Down
4 changes: 0 additions & 4 deletions rumcake/src/hw/mcu/rp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ pub fn setup_usb_driver<K: crate::usb::USBKeyboard>(

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]);
Expand All @@ -91,7 +88,6 @@ pub fn setup_usb_driver<K: crate::usb::USBKeyboard>(
embassy_usb::Builder::new(
usb_driver,
config,
device_descriptor,
config_descriptor,
bos_descriptor,
msos_descriptor,
Expand Down
121 changes: 10 additions & 111 deletions rumcake/src/hw/mcu/stm32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -139,9 +136,6 @@ pub fn setup_usb_driver<K: crate::usb::USBKeyboard>(

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]);
Expand All @@ -155,7 +149,6 @@ pub fn setup_usb_driver<K: crate::usb::USBKeyboard>(
embassy_usb::Builder::new(
usb_driver,
config,
device_descriptor,
config_descriptor,
bos_descriptor,
msos_descriptor,
Expand All @@ -164,23 +157,6 @@ pub fn setup_usb_driver<K: crate::usb::USBKeyboard>(
}
}

pub struct Channel<A: Instance> {
channel: u8,
_phantom: PhantomData<A>,
}

impl<A: Instance> Channel<A> {
pub fn new(mut pin: impl AdcPin<A>) -> 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`]
Expand All @@ -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<ADC>; C],
analog_pins: [AnyAdcChannel<ADC>; C],
}

impl<'a, ADC: Instance, const MP: usize, const C: usize> AdcSampler<'a, ADC, MP, C> {
Expand All @@ -211,97 +187,20 @@ impl<'a, ADC: Instance, const MP: usize, const C: usize> AdcSampler<'a, ADC, MP,
adc: impl Peripheral<P = ADC> + 'a,
irq: impl Binding<ADC::Interrupt, InterruptHandler<ADC>> + 'a,
idx_to_pin_type: [AnalogPinType<'a, MP>; C],
analog_pins: [Channel<ADC>; C],
analog_pins: [AnyAdcChannel<ADC>; 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<ADC>) -> 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
Expand All @@ -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])),
})
})
}
Expand Down
3 changes: 2 additions & 1 deletion rumcake/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion rumcake/src/split/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl<E> From<ReadExactError<E>> for PeripheralDeviceError<E> {
#[rumcake_macros::task]
pub async fn peripheral_task<K: PeripheralDevice>(_k: K, mut driver: impl PeripheralDeviceDriver) {
let channel = K::get_matrix_events_channel();
let matrix_event_publisher = MATRIX_EVENTS.immediate_publisher();

loop {
match select(
Expand Down Expand Up @@ -160,7 +161,7 @@ pub async fn peripheral_task<K: PeripheralDevice>(_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!(
Expand Down
Loading

0 comments on commit 9a49269

Please sign in to comment.