Skip to content

Commit

Permalink
Merge pull request #46 from HaoboGu/feat/more_nrf
Browse files Browse the repository at this point in the history
Support more nRF chips
  • Loading branch information
HaoboGu authored Jun 11, 2024
2 parents 5de5724 + c4524db commit 210fc70
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 40 deletions.
2 changes: 2 additions & 0 deletions examples/use_config/nrf52840_ble/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/use_config/nrf52840_ble/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ license = "MIT OR Apache-2.0"
rmk = { version = "0.1.21", path = "../../../rmk", features = [
"nrf52840_ble",
"col2row",
"async_matrix",
] }
cortex-m = "0.7.7"
cortex-m-rt = "0.7.3"
Expand Down
4 changes: 4 additions & 0 deletions rmk-macro/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Support more nRF chips: nRF52833, nRF52810, nRF52811

## [0.1.5] - 2024-06-08

### Added
Expand Down
25 changes: 14 additions & 11 deletions rmk-macro/src/bind_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ pub(crate) fn bind_interrupt_default(
usb_info: &UsbInfo,
toml_config: &KeyboardTomlConfig,
) -> TokenStream2 {
if !chip.has_usb() {
return quote! {};
}
let interrupt_name = format_ident!("{}", usb_info.interrupt_name);
let peripheral_name = format_ident!("{}", usb_info.peripheral_name);
match chip.series {
crate::ChipSeries::Stm32 => {
if !chip.has_usb() {
return quote! {};
}
let usb_mod_name = if usb_info.peripheral_name.contains("OTG") {
format_ident!("{}", "usb_otg")
} else {
Expand All @@ -78,17 +78,20 @@ pub(crate) fn bind_interrupt_default(
} else {
None
};
if chip.has_usb() {
let interrupt_binding = if chip.has_usb() {
quote! {
use ::embassy_nrf::bind_interrupts;
bind_interrupts!(struct Irqs {
#interrupt_name => ::embassy_nrf::usb::InterruptHandler<::embassy_nrf::peripherals::#peripheral_name>;
#saadc_interrupt
POWER_CLOCK => ::embassy_nrf::usb::vbus_detect::InterruptHandler;
});
#interrupt_name => ::embassy_nrf::usb::InterruptHandler<::embassy_nrf::peripherals::#peripheral_name>;
#saadc_interrupt
POWER_CLOCK => ::embassy_nrf::usb::vbus_detect::InterruptHandler;
}
} else {
quote! {}
quote! { #saadc_interrupt }
};
quote! {
use ::embassy_nrf::bind_interrupts;
bind_interrupts!(struct Irqs {
#interrupt_binding
});
}
}
crate::ChipSeries::Rp2040 => {
Expand Down
4 changes: 4 additions & 0 deletions rmk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Support more nRF chips: nRF52833, nRF52810, nRF52811

## [0.1.21] - 2024-06-08

### Added
Expand Down
31 changes: 16 additions & 15 deletions rmk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,16 @@ rapid_debouncer = []
#!
#! ⚠️ Due to the limitation of docs.rs, functions gated by BLE features won't show in docs.rs. You have to head to [`examples`](https://github.com/HaoboGu/rmk/tree/main/examples) folder of RMK repo for their usages.
## Enable feature if you want to use nRF52840 with BLE.
nrf52840_ble = [
"_nrf_ble",
"dep:embassy-nrf",
"dep:once_cell",
"nrf-softdevice/nrf52840",
"nrf-softdevice/s140",
]
nrf52840_ble = ["_nrf_ble", "nrf-softdevice/nrf52840", "nrf-softdevice/s140"]
## Enable feature if you want to use nRF52833 with BLE.
nrf52833_ble = ["_nrf_ble", "nrf-softdevice/nrf52833", "nrf-softdevice/s140"]
## Enable feature if you want to use nRF52832 with BLE.
nrf52832_ble = [
"_nrf_ble",
"dep:embassy-nrf",
"dep:once_cell",
"nrf-softdevice/nrf52832",
"nrf-softdevice/s132",
]
nrf52832_ble = ["_nrf_ble", "nrf-softdevice/nrf52832", "nrf-softdevice/s132"]
## Enable feature if you want to use nRF52811 with BLE.
nrf52811_ble = ["_nrf_ble", "nrf-softdevice/nrf52811", "nrf-softdevice/s140"]
## Enable feature if you want to use nRF52810 with BLE.
nrf52810_ble = ["_nrf_ble", "nrf-softdevice/nrf52810", "nrf-softdevice/s132"]

## Enable feature if you want to use ESP32C3 with BLE.
esp32c3_ble = ["_esp_ble"]
## Enable feature if you want to use ESP32S3 with BLE.
Expand All @@ -108,5 +103,11 @@ _esp_ble = [
"dep:esp32-nimble",
"dep:esp-idf-svc",
]
_nrf_ble = ["_ble", "rmk-config/_nrf_ble", "dep:nrf-softdevice"]
_nrf_ble = [
"_ble",
"rmk-config/_nrf_ble",
"dep:nrf-softdevice",
"dep:embassy-nrf",
"dep:once_cell",
]
_ble = []
5 changes: 3 additions & 2 deletions rmk/src/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ pub mod nrf;
use defmt::error;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Receiver};
use embassy_time::Timer;
#[cfg(feature = "nrf52840_ble")]
#[cfg(any(feature = "nrf52840_ble", feature = "nrf52833_ble"))]
pub use nrf::SOFTWARE_VBUS;

use crate::{
hid::HidWriterWrapper, keyboard::{write_other_report_to_host, KeyboardReportMessage},
hid::HidWriterWrapper,
keyboard::{write_other_report_to_host, KeyboardReportMessage},
usb::descriptor::CompositeReportType,
};

Expand Down
36 changes: 25 additions & 11 deletions rmk/src/ble/nrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod hid_service;
pub(crate) mod server;
pub(crate) mod spec;

// TODO: Conditional imports should be compatible with more nRF chip models
use self::server::BleServer;
use crate::{
ble::{
Expand Down Expand Up @@ -42,7 +41,7 @@ use nrf_softdevice::{
use rmk_config::BleBatteryConfig;
use sequential_storage::{cache::NoCache, map::fetch_item};
use static_cell::StaticCell;
#[cfg(not(feature = "nrf52832_ble"))]
#[cfg(any(feature = "nrf52840_ble", feature = "nrf52833_ble"))]
use {
crate::{
run_usb_keyboard,
Expand All @@ -59,11 +58,11 @@ use {
/// Maximum number of bonded devices
pub const BONDED_DEVICE_NUM: usize = 8;

#[cfg(feature = "nrf52840_ble")]
#[cfg(any(feature = "nrf52840_ble", feature = "nrf52833_ble"))]
/// Software Vbus detect when using BLE + USB
pub static SOFTWARE_VBUS: OnceCell<SoftwareVbusDetect> = OnceCell::new();

#[cfg(feature = "nrf52840_ble")]
#[cfg(any(feature = "nrf52840_ble", feature = "nrf52833_ble"))]
/// Background task of nrf_softdevice
#[embassy_executor::task]
pub(crate) async fn softdevice_task(sd: &'static nrf_softdevice::Softdevice) -> ! {
Expand Down Expand Up @@ -99,10 +98,20 @@ pub(crate) async fn softdevice_task(sd: &'static nrf_softdevice::Softdevice) ->
.await
}

// nRF52832 doesn't have USB, so the softdevice_task is different
#[cfg(feature = "nrf52832_ble")]
// Some nRF BLE chips doesn't have USB, so the softdevice_task is different
#[cfg(any(
feature = "nrf52832_ble",
feature = "nrf52811_ble",
feature = "nrf52810_ble"
))]
#[embassy_executor::task]
pub(crate) async fn softdevice_task(sd: &'static nrf_softdevice::Softdevice) -> ! {
// Enable dcdc-mode, reduce power consumption
unsafe {
nrf_softdevice::raw::sd_power_dcdc_mode_set(
nrf_softdevice::raw::NRF_POWER_DCDC_MODES_NRF_POWER_DCDC_ENABLE as u8,
);
};
sd.run().await
}

Expand Down Expand Up @@ -162,7 +171,7 @@ pub(crate) fn nrf_ble_config(keyboard_name: &str) -> Config {
/// * `spwaner` - embassy task spwaner, used to spawn nrf_softdevice background task
/// * `saadc` - nRF's [saadc](https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52832.ps.v1.1%2Fsaadc.html) instance for battery level detection, if you don't need it, pass `None`
pub async fn initialize_nrf_ble_keyboard_with_config_and_run<
#[cfg(not(feature = "nrf52832_ble"))] D: Driver<'static>,
#[cfg(any(feature = "nrf52840_ble", feature = "nrf52833_ble"))] D: Driver<'static>,
#[cfg(feature = "async_matrix")] In: Wait + InputPin,
#[cfg(not(feature = "async_matrix"))] In: InputPin,
Out: OutputPin,
Expand All @@ -175,7 +184,7 @@ pub async fn initialize_nrf_ble_keyboard_with_config_and_run<
#[cfg(not(feature = "col2row"))] input_pins: [In; COL],
#[cfg(feature = "col2row")] output_pins: [Out; COL],
#[cfg(not(feature = "col2row"))] output_pins: [Out; ROW],
#[cfg(not(feature = "nrf52832_ble"))] usb_driver: Option<D>,
#[cfg(any(feature = "nrf52840_ble", feature = "nrf52833_ble"))] usb_driver: Option<D>,
mut keyboard_config: RmkConfig<'static, Out>,
spawner: Spawner,
) -> ! {
Expand Down Expand Up @@ -223,7 +232,7 @@ pub async fn initialize_nrf_ble_keyboard_with_config_and_run<

// Keyboard services
let mut keyboard = Keyboard::new(input_pins, output_pins, &keymap);
#[cfg(not(feature = "nrf52832_ble"))]
#[cfg(any(feature = "nrf52840_ble", feature = "nrf52833_ble"))]
let (mut usb_device, mut vial_service, mut light_service) = (
usb_driver.map(|u| KeyboardUsbDevice::new(u, keyboard_config.usb_config)),
VialService::new(&keymap, keyboard_config.vial_config),
Expand Down Expand Up @@ -253,7 +262,7 @@ pub async fn initialize_nrf_ble_keyboard_with_config_and_run<

// If there is a USB device, things become a little bit complex because we need to enable switching between USB and BLE.
// Remember that USB ALWAYS has higher priority than BLE.
#[cfg(not(feature = "nrf52832_ble"))]
#[cfg(any(feature = "nrf52840_ble", feature = "nrf52833_ble"))]
if let Some(ref mut usb_device) = usb_device {
// Check and run via USB first
if USB_DEVICE_ENABLED.load(Ordering::SeqCst) {
Expand Down Expand Up @@ -283,6 +292,7 @@ pub async fn initialize_nrf_ble_keyboard_with_config_and_run<
bonder.load_sys_attrs(&conn);
let usb_configured = wait_for_usb_configured();
let usb_fut = usb_device.device.run();
// TODO: enable light service(and vial service) in ble mode
match select(
run_ble_keyboard(
&conn,
Expand Down Expand Up @@ -333,7 +343,11 @@ pub async fn initialize_nrf_ble_keyboard_with_config_and_run<
}
}

#[cfg(feature = "nrf52832_ble")]
#[cfg(any(
feature = "nrf52832_ble",
feature = "nrf52811_ble",
feature = "nrf52810_ble"
))]
match adv_fut.await {
Ok(conn) => {
bonder.load_sys_attrs(&conn);
Expand Down
1 change: 0 additions & 1 deletion rmk/src/usb/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ impl CompositeReport {
data: &mut [u8],
report_type: CompositeReportType,
) -> Result<usize, ssmarshal::Error> {
// TODO: Optimize it
// Use usbd-hid's report to do serialization, but not so efficient.
match report_type {
CompositeReportType::None => Ok(0),
Expand Down

0 comments on commit 210fc70

Please sign in to comment.