Skip to content

Commit

Permalink
export raw mutex type from mcu module
Browse files Browse the repository at this point in the history
  • Loading branch information
Univa committed Mar 13, 2024
1 parent 67e03a8 commit cf0a84c
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 106 deletions.
11 changes: 5 additions & 6 deletions rumcake/src/backlight/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,20 @@ macro_rules! backlight_module {
use crate::keyboard::MATRIX_EVENTS;
use crate::{LEDEffect, State};
use embassy_futures::select;
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::channel::Channel;
use embassy_time::{Duration, Ticker};

pub mod animations;

use crate::hw::mcu::RawMutex;
use animations::{BacklightAnimator, BacklightCommand, BacklightConfig};

/// Channel for sending backlight commands.
///
/// Channel messages should be consumed by the [`backlight_task`], so user-level
/// level code should **not** attempt to receive messages from the channel, otherwise
/// commands may not be processed appropriately. You should only send to this channel.
pub static BACKLIGHT_COMMAND_CHANNEL: Channel<ThreadModeRawMutex, BacklightCommand, 2> =
pub static BACKLIGHT_COMMAND_CHANNEL: Channel<RawMutex, BacklightCommand, 2> =
Channel::new();

/// State that contains the current configuration for the backlight animator.
Expand All @@ -312,20 +312,19 @@ macro_rules! storage_module {
use defmt::{info, warn, Debug2Format};
use embassy_futures::select;
use embassy_futures::select::Either;
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::signal::Signal;
use embassy_time::Duration;
use embassy_time::Timer;

use crate::hw::mcu::RawMutex;
use crate::storage::{FlashStorage, StorageDevice};

use super::BacklightConfig;
use super::BACKLIGHT_CONFIG_STATE;

pub(super) static BACKLIGHT_CONFIG_STATE_LISTENER: Signal<ThreadModeRawMutex, ()> =
Signal::new();
pub(super) static BACKLIGHT_CONFIG_STATE_LISTENER: Signal<RawMutex, ()> = Signal::new();

pub(super) static BACKLIGHT_SAVE_SIGNAL: Signal<ThreadModeRawMutex, ()> = Signal::new();
pub(super) static BACKLIGHT_SAVE_SIGNAL: Signal<RawMutex, ()> = Signal::new();
};
}

Expand Down
9 changes: 4 additions & 5 deletions rumcake/src/bluetooth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#[cfg(any(all(feature = "nrf", feature = "bluetooth"), doc))]
pub mod nrf_ble;

use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::channel::Channel;
use embassy_sync::signal::Signal;

use crate::hw::mcu::RawMutex;
use crate::keyboard::{Keyboard, KeyboardLayout};
use crate::State;

Expand Down Expand Up @@ -54,11 +54,10 @@ pub enum BluetoothCommand {
/// nRF5x-based keyboards), so user-level code should **not** attempt to receive messages from the
/// channel, otherwise commands may not be processed appropriately. You should only send to this
/// channel.
pub static BLUETOOTH_COMMAND_CHANNEL: Channel<ThreadModeRawMutex, BluetoothCommand, 2> =
Channel::new();
pub static BLUETOOTH_COMMAND_CHANNEL: Channel<RawMutex, BluetoothCommand, 2> = Channel::new();

pub(crate) static BLUETOOTH_CONNECTED_STATE: State<bool> =
State::new(false, &[&crate::hw::BLUETOOTH_CONNECTED_STATE_LISTENER]);

pub(crate) static CURRENT_OUTPUT_STATE_LISTENER: Signal<ThreadModeRawMutex, ()> = Signal::new();
pub(crate) static BATTERY_LEVEL_LISTENER: Signal<ThreadModeRawMutex, ()> = Signal::new();
pub(crate) static CURRENT_OUTPUT_STATE_LISTENER: Signal<RawMutex, ()> = Signal::new();
pub(crate) static BATTERY_LEVEL_LISTENER: Signal<RawMutex, ()> = Signal::new();
6 changes: 3 additions & 3 deletions rumcake/src/display/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
//! [`drivers::DisplayDriver`]).

use embassy_futures::select::{select, select_array, Either};
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::signal::Signal;
use embassy_time::{Duration, Ticker, Timer};

pub mod drivers;

use self::drivers::DisplayDriver;
use crate::hw::mcu::RawMutex;

pub(crate) static OUTPUT_MODE_STATE_LISTENER: Signal<ThreadModeRawMutex, ()> = Signal::new();
pub(crate) static BATTERY_LEVEL_LISTENER: Signal<ThreadModeRawMutex, ()> = Signal::new();
pub(crate) static OUTPUT_MODE_STATE_LISTENER: Signal<RawMutex, ()> = Signal::new();
pub(crate) static BATTERY_LEVEL_LISTENER: Signal<RawMutex, ()> = Signal::new();

/// A trait that keyboards must implement to use a display.
pub trait DisplayDevice {
Expand Down
25 changes: 9 additions & 16 deletions rumcake/src/drivers/nrf_ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
pub mod central {
use defmt::{assert, debug, error, info, warn, Debug2Format};
use embassy_futures::select::{select, select_slice, Either};
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::channel::Channel;
use embassy_sync::mutex::Mutex;
use embassy_sync::pubsub::{PubSubChannel, Publisher};
Expand All @@ -25,28 +24,24 @@ pub mod central {
use nrf_softdevice::ble::{central, Address, AddressType};
use nrf_softdevice::{RawError, Softdevice};

use crate::hw::mcu::RawMutex;
use crate::split::drivers::{CentralDeviceDriver, CentralDeviceError};
use crate::split::{
MessageToCentral, MessageToPeripheral, MESSAGE_TO_CENTRAL_BUFFER_SIZE,
MESSAGE_TO_PERIPHERAL_BUFFER_SIZE,
};

pub struct NRFBLECentralDriver<'a> {
publisher: Publisher<'a, ThreadModeRawMutex, MessageToPeripheral, 4, 4, 1>,
publisher: Publisher<'a, RawMutex, MessageToPeripheral, 4, 4, 1>,
}

pub static BLE_MESSAGES_FROM_PERIPHERALS: Channel<ThreadModeRawMutex, MessageToCentral, 4> =
pub static BLE_MESSAGES_FROM_PERIPHERALS: Channel<RawMutex, MessageToCentral, 4> =
Channel::new();

pub static BLE_MESSAGES_TO_PERIPHERALS: PubSubChannel<
ThreadModeRawMutex,
MessageToPeripheral,
4,
4,
1,
> = PubSubChannel::new();
pub static BLE_MESSAGES_TO_PERIPHERALS: PubSubChannel<RawMutex, MessageToPeripheral, 4, 4, 1> =
PubSubChannel::new();

pub static BLUETOOTH_CONNECTION_MUTEX: Mutex<ThreadModeRawMutex, ()> = Mutex::new(());
pub static BLUETOOTH_CONNECTION_MUTEX: Mutex<RawMutex, ()> = Mutex::new(());

/// Create an instance of the nRF bluetooth central device driver.
pub fn setup_driver() -> NRFBLECentralDriver<'static> {
Expand Down Expand Up @@ -229,14 +224,13 @@ pub mod central {
pub mod peripheral {
use defmt::{debug, error, info, warn, Debug2Format};
use embassy_futures::select::{select, Either};
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::channel::Channel;
use nrf_softdevice::ble::gatt_server::{run, set_sys_attrs};
use nrf_softdevice::ble::peripheral::{advertise_connectable, ConnectableAdvertisement};
use nrf_softdevice::ble::{Address, AddressType};
use nrf_softdevice::Softdevice;

use crate::hw::mcu::BLUETOOTH_ADVERTISING_MUTEX;
use crate::hw::mcu::{RawMutex, BLUETOOTH_ADVERTISING_MUTEX};
use crate::split::drivers::{PeripheralDeviceDriver, PeripheralDeviceError};
use crate::split::{
MessageToCentral, MessageToPeripheral, MESSAGE_TO_CENTRAL_BUFFER_SIZE,
Expand All @@ -245,10 +239,9 @@ pub mod peripheral {

pub struct NRFBLEPeripheralDriver {}

pub static BLE_MESSAGES_TO_CENTRAL: Channel<ThreadModeRawMutex, MessageToCentral, 4> =
Channel::new();
pub static BLE_MESSAGES_TO_CENTRAL: Channel<RawMutex, MessageToCentral, 4> = Channel::new();

pub static BLE_MESSAGES_FROM_CENTRAL: Channel<ThreadModeRawMutex, MessageToPeripheral, 4> =
pub static BLE_MESSAGES_FROM_CENTRAL: Channel<RawMutex, MessageToPeripheral, 4> =
Channel::new();

/// Create an instance of the nRF bluetooth central device driver.
Expand Down
4 changes: 3 additions & 1 deletion rumcake/src/hw/mcu/nrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub use nrf_softdevice;
#[cfg(feature = "nrf52840")]
pub const SYSCLK: u32 = 48_000_000;

pub type RawMutex = ThreadModeRawMutex;

pub fn jump_to_bootloader() {
// TODO
}
Expand Down Expand Up @@ -196,7 +198,7 @@ pub async fn adc_task() {
/// A mutex that is locked when the softdevice is advertising. This is mainly to prevent
/// [`nrf_softdevice::ble::peripheral::ADV_PORTAL`] from being opened by more than one task at the
/// same time.
pub static BLUETOOTH_ADVERTISING_MUTEX: Mutex<ThreadModeRawMutex, ()> = Mutex::new(());
pub static BLUETOOTH_ADVERTISING_MUTEX: Mutex<RawMutex, ()> = Mutex::new(());

#[cfg(feature = "nrf-ble")]
/// A basic trait that all nRF5x-based devices that use bluetooth features must implement.
Expand Down
3 changes: 3 additions & 0 deletions rumcake/src/hw/mcu/stm32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use embassy_stm32::flash::{Blocking, Flash as HALFlash};
use embassy_stm32::peripherals::{FLASH, PA11, PA12, USB};
use embassy_stm32::rcc::{APBPrescaler, Hse, Pll, PllMul, PllPreDiv, PllSource, Sysclk, HSI_FREQ};
use embassy_stm32::usb::Driver;
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use static_cell::StaticCell;

pub use rumcake_macros::{input_pin, output_pin, setup_buffered_uart, setup_i2c};
Expand All @@ -21,6 +22,8 @@ pub const SYSCLK: u32 = 48_000_000;
#[cfg(feature = "stm32f303cb")]
pub const SYSCLK: u32 = 72_000_000;

pub type RawMutex = ThreadModeRawMutex;

/// A function that allows you to jump to the bootloader, usually for re-flashing the firmware.
pub fn jump_to_bootloader() {
#[cfg(feature = "stm32f072cb")]
Expand Down
10 changes: 5 additions & 5 deletions rumcake/src/hw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ pub mod mcu;

use crate::State;
use embassy_futures::select;
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::signal::Signal;

use mcu::RawMutex;

/// State that contains the current battery level. `rumcake` may or may not use this
/// static internally, depending on what MCU is being used. The contents of this state
/// is usually set by a task in the [`mcu`] module. For example, on nRF5x-based MCUs,
Expand Down Expand Up @@ -75,10 +76,9 @@ pub static CURRENT_OUTPUT_STATE: State<Option<HIDOutput>> = State::new(
],
);

pub(crate) static OUTPUT_MODE_STATE_LISTENER: Signal<ThreadModeRawMutex, ()> = Signal::new();
pub(crate) static USB_RUNNING_STATE_LISTENER: Signal<ThreadModeRawMutex, ()> = Signal::new();
pub(crate) static BLUETOOTH_CONNECTED_STATE_LISTENER: Signal<ThreadModeRawMutex, ()> =
Signal::new();
pub(crate) static OUTPUT_MODE_STATE_LISTENER: Signal<RawMutex, ()> = Signal::new();
pub(crate) static USB_RUNNING_STATE_LISTENER: Signal<RawMutex, ()> = Signal::new();
pub(crate) static BLUETOOTH_CONNECTED_STATE_LISTENER: Signal<RawMutex, ()> = Signal::new();

#[rumcake_macros::task]
pub async fn output_switcher() {
Expand Down
25 changes: 10 additions & 15 deletions rumcake/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

use core::convert::Infallible;
use defmt::{debug, info, warn, Debug2Format};
use embassy_sync::channel::Channel;
use embassy_sync::mutex::{Mutex, MutexGuard};
use embassy_sync::pubsub::{PubSubBehavior, PubSubChannel};
use embassy_sync::{blocking_mutex::raw::ThreadModeRawMutex, channel::Channel};
use embassy_time::{Duration, Ticker, Timer};
use embedded_hal::digital::v2::{InputPin, OutputPin};
use heapless::Vec;
Expand All @@ -22,6 +22,7 @@ use usbd_human_interface_device::{
#[cfg(feature = "media-keycodes")]
pub use usbd_human_interface_device::page::Consumer;

use crate::hw::mcu::RawMutex;
use crate::hw::CURRENT_OUTPUT_STATE;

pub use rumcake_macros::{build_layout, build_matrix, remap_matrix};
Expand Down Expand Up @@ -87,7 +88,7 @@ pub trait KeyboardLayout {
/// A mutex-guaraded [`keyberon::layout::Layout`]. This also stores the original layout, so that it
/// can be reset to it's initial state if modifications are made to it.
pub struct Layout<const C: usize, const R: usize, const L: usize> {
layout: once_cell::sync::OnceCell<Mutex<ThreadModeRawMutex, KeyberonLayout<C, R, L, Keycode>>>,
layout: once_cell::sync::OnceCell<Mutex<RawMutex, KeyberonLayout<C, R, L, Keycode>>>,
}

impl<const C: usize, const R: usize, const L: usize> Layout<C, R, L> {
Expand All @@ -102,7 +103,7 @@ impl<const C: usize, const R: usize, const L: usize> Layout<C, R, L> {
.get_or_init(|| Mutex::new(KeyberonLayout::new(layers)));
}

pub async fn lock(&self) -> MutexGuard<ThreadModeRawMutex, KeyberonLayout<C, R, L, Keycode>> {
pub async fn lock(&self) -> MutexGuard<RawMutex, KeyberonLayout<C, R, L, Keycode>> {
self.layout.get().unwrap().lock().await
}
}
Expand Down Expand Up @@ -206,7 +207,7 @@ pub enum Keycode {
///
/// The coordinates received will be remapped according to the implementation of
/// [`KeyboardMatrix::remap_to_layout`].
pub(crate) static POLLED_EVENTS_CHANNEL: Channel<ThreadModeRawMutex, Event, 1> = Channel::new();
pub(crate) static POLLED_EVENTS_CHANNEL: Channel<RawMutex, Event, 1> = Channel::new();

#[rumcake_macros::task]
pub async fn matrix_poll<K: KeyboardMatrix>(
Expand Down Expand Up @@ -258,29 +259,23 @@ pub async fn matrix_poll<K: KeyboardMatrix>(
/// There can be a maximum of 4 subscribers, and the number of subscribers actually used
/// depend on what features you have enabled. With underglow and backlight enabled, 2 subscriber
/// slots will be used.
pub static MATRIX_EVENTS: PubSubChannel<ThreadModeRawMutex, Event, 4, 4, 1> = PubSubChannel::new();
pub static MATRIX_EVENTS: PubSubChannel<RawMutex, Event, 4, 4, 1> = PubSubChannel::new();

/// Channel for sending NKRO HID keyboard reports.
///
/// Channel messages should be consumed by the bluetooth task or USB task, so user-level code
/// should **not** attempt to receive messages from the channel, otherwise commands may not be
/// processed appropriately. You should only send to this channel.
pub static KEYBOARD_REPORT_HID_SEND_CHANNEL: Channel<
ThreadModeRawMutex,
NKROBootKeyboardReport,
1,
> = Channel::new();
pub static KEYBOARD_REPORT_HID_SEND_CHANNEL: Channel<RawMutex, NKROBootKeyboardReport, 1> =
Channel::new();

/// Channel for sending consumer HID reports.
///
/// Channel messages should be consumed by the bluetooth task or USB task, so user-level code
/// should **not** attempt to receive messages from the channel, otherwise commands may not be
/// processed appropriately. You should only send to this channel.
pub static CONSUMER_REPORT_HID_SEND_CHANNEL: Channel<
ThreadModeRawMutex,
MultipleConsumerReport,
1,
> = Channel::new();
pub static CONSUMER_REPORT_HID_SEND_CHANNEL: Channel<RawMutex, MultipleConsumerReport, 1> =
Channel::new();

#[rumcake_macros::task]
pub async fn layout_collect<K: KeyboardLayout + 'static>(_k: K)
Expand Down
15 changes: 8 additions & 7 deletions rumcake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
#![warn(missing_docs)]
#![doc = include_str!("../../README.md")]

use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::mutex::{Mutex, MutexGuard};
use embassy_sync::signal::Signal;

use crate::hw::mcu::RawMutex;

pub(crate) trait StaticArray {
const LEN: usize;
}
Expand All @@ -30,13 +31,13 @@ trait Cycle {
/// Data structure that allows you to notify listeners about any changes to the data being managed.
/// This can be useful when you want a task to react to changes to certain data.
pub struct State<'a, T: Clone + PartialEq> {
data: Mutex<ThreadModeRawMutex, T>,
listeners: &'a [&'a Signal<ThreadModeRawMutex, ()>],
data: Mutex<RawMutex, T>,
listeners: &'a [&'a Signal<RawMutex, ()>],
}

impl<'a, T: Clone + PartialEq> State<'a, T> {
/// Create some new state, with the specified listeners.
pub const fn new(data: T, listeners: &'a [&'a Signal<ThreadModeRawMutex, ()>]) -> State<'a, T> {
pub const fn new(data: T, listeners: &'a [&'a Signal<RawMutex, ()>]) -> State<'a, T> {
Self {
data: Mutex::new(data),
listeners,
Expand Down Expand Up @@ -69,7 +70,7 @@ impl<'a, T: Clone + PartialEq> State<'a, T> {

async fn update_inner<R>(
&self,
updater: impl FnOnce(&mut MutexGuard<'_, ThreadModeRawMutex, T>) -> R,
updater: impl FnOnce(&mut MutexGuard<'_, RawMutex, T>) -> R,
) -> (bool, R) {
let mut data = self.data.lock().await;
let old = data.clone();
Expand All @@ -80,7 +81,7 @@ impl<'a, T: Clone + PartialEq> State<'a, T> {
/// Update state using a function, and notify listeners
pub async fn update<R>(
&self,
updater: impl FnOnce(&mut MutexGuard<'_, ThreadModeRawMutex, T>) -> R,
updater: impl FnOnce(&mut MutexGuard<'_, RawMutex, T>) -> R,
) -> R {
let (changed, update_result) = self.update_inner(updater).await;

Expand All @@ -94,7 +95,7 @@ impl<'a, T: Clone + PartialEq> State<'a, T> {
/// Update state using a function without notifying listeners
pub async fn quiet_update<R>(
&self,
updater: impl FnOnce(&mut MutexGuard<'_, ThreadModeRawMutex, T>) -> R,
updater: impl FnOnce(&mut MutexGuard<'_, RawMutex, T>) -> R,
) -> R {
let (_changed, update_result) = self.update_inner(updater).await;
update_result
Expand Down
5 changes: 2 additions & 3 deletions rumcake/src/split/central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

use defmt::{error, Debug2Format};
use embassy_futures::select::{select, Either};
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::channel::Channel;

use crate::hw::mcu::RawMutex;
use crate::keyboard::POLLED_EVENTS_CHANNEL;
use crate::split::MessageToCentral;

Expand All @@ -23,8 +23,7 @@ use super::MessageToPeripheral;
/// Channel messages should be consumed by the central task, so user-level code should
/// **not** attempt to receive messages from the channel, otherwise commands may not be processed
/// appropriately. You should only send to this channel.
pub static MESSAGE_TO_PERIPHERALS: Channel<ThreadModeRawMutex, MessageToPeripheral, 4> =
Channel::new();
pub static MESSAGE_TO_PERIPHERALS: Channel<RawMutex, MessageToPeripheral, 4> = Channel::new();

#[rumcake_macros::task]
pub async fn central_task(mut driver: impl CentralDeviceDriver) {
Expand Down
Loading

0 comments on commit cf0a84c

Please sign in to comment.