Skip to content

Commit

Permalink
feat(ble): add default nrf ble config
Browse files Browse the repository at this point in the history
Signed-off-by: HaoboGu <[email protected]>
  • Loading branch information
HaoboGu committed Mar 7, 2024
1 parent da18c4c commit 5607e1b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
37 changes: 2 additions & 35 deletions boards/nrf52840_ble/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mod keymap;
mod vial;

use crate::keymap::{COL, NUM_LAYER, ROW};
use core::mem;
use defmt::*;
use defmt_rtt as _;
use embassy_executor::Spawner;
Expand All @@ -19,8 +18,8 @@ use embassy_nrf::{
};
use panic_probe as _;
use rmk::{
ble::nrf_ble_config,
config::{KeyboardUsbConfig, RmkConfig, VialConfig},
nrf_softdevice::{self, raw},
};

use vial::{VIAL_KEYBOARD_DEF, VIAL_KEYBOARD_ID};
Expand All @@ -31,39 +30,7 @@ const EEPROM_SIZE: usize = 128;
async fn main(spawner: Spawner) {
info!("Hello NRF BLE!");
let keyboard_name = "RMK Keyboard";
let ble_config = nrf_softdevice::Config {
clock: Some(raw::nrf_clock_lf_cfg_t {
source: raw::NRF_CLOCK_LF_SRC_RC as u8,
rc_ctiv: 16,
rc_temp_ctiv: 2,
accuracy: raw::NRF_CLOCK_LF_ACCURACY_500_PPM as u8,
}),
conn_gap: Some(raw::ble_gap_conn_cfg_t {
conn_count: 6,
event_length: 24,
}),
conn_gatt: Some(raw::ble_gatt_conn_cfg_t { att_mtu: 256 }),
gatts_attr_tab_size: Some(raw::ble_gatts_cfg_attr_tab_size_t {
attr_tab_size: raw::BLE_GATTS_ATTR_TAB_SIZE_DEFAULT,
}),
gap_role_count: Some(raw::ble_gap_cfg_role_count_t {
adv_set_count: 1,
periph_role_count: 3,
central_role_count: 3,
central_sec_count: 0,
_bitfield_1: raw::ble_gap_cfg_role_count_t::new_bitfield_1(0),
}),
gap_device_name: Some(raw::ble_gap_cfg_device_name_t {
p_value: keyboard_name.as_ptr() as _,
current_len: keyboard_name.len() as u16,
max_len: keyboard_name.len() as u16,
write_perm: unsafe { mem::zeroed() },
_bitfield_1: raw::ble_gap_cfg_device_name_t::new_bitfield_1(
raw::BLE_GATTS_VLOC_STACK as u8,
),
}),
..Default::default()
};
let ble_config = nrf_ble_config(keyboard_name);

let mut nrf_config = embassy_nrf::config::Config::default();
nrf_config.gpiote_interrupt_priority = Priority::P2;
Expand Down
41 changes: 39 additions & 2 deletions rmk/src/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,54 @@ use crate::{
ble::bonder::{BondInfo, FLASH_CHANNEL},
keyboard::Keyboard,
};
use core::{convert::Infallible, ops::Range};
use core::{convert::Infallible, mem, ops::Range};
use defmt::info;
use embassy_time::Timer;
use embedded_hal::digital::{InputPin, OutputPin};
use embedded_storage::nor_flash::NorFlash;
use nrf_softdevice::Flash;
use nrf_softdevice::{raw, Config, Flash};
use sequential_storage::{
cache::NoCache,
map::{remove_item, store_item},
};

/// Create default nrf ble config
pub fn nrf_ble_config(keyboard_name: &str) -> Config {
Config {
clock: Some(raw::nrf_clock_lf_cfg_t {
source: raw::NRF_CLOCK_LF_SRC_RC as u8,
rc_ctiv: 16,
rc_temp_ctiv: 2,
accuracy: raw::NRF_CLOCK_LF_ACCURACY_500_PPM as u8,
}),
conn_gap: Some(raw::ble_gap_conn_cfg_t {
conn_count: 6,
event_length: 24,
}),
conn_gatt: Some(raw::ble_gatt_conn_cfg_t { att_mtu: 256 }),
gatts_attr_tab_size: Some(raw::ble_gatts_cfg_attr_tab_size_t {
attr_tab_size: raw::BLE_GATTS_ATTR_TAB_SIZE_DEFAULT,
}),
gap_role_count: Some(raw::ble_gap_cfg_role_count_t {
adv_set_count: 1,
periph_role_count: 3,
central_role_count: 3,
central_sec_count: 0,
_bitfield_1: raw::ble_gap_cfg_role_count_t::new_bitfield_1(0),
}),
gap_device_name: Some(raw::ble_gap_cfg_device_name_t {
p_value: keyboard_name.as_ptr() as _,
current_len: keyboard_name.len() as u16,
max_len: keyboard_name.len() as u16,
write_perm: unsafe { mem::zeroed() },
_bitfield_1: raw::ble_gap_cfg_device_name_t::new_bitfield_1(
raw::BLE_GATTS_VLOC_STACK as u8,
),
}),
..Default::default()
}
}

/// Background task of nrf_softdevice
#[embassy_executor::task]
pub(crate) async fn softdevice_task(sd: &'static nrf_softdevice::Softdevice) -> ! {
Expand Down

0 comments on commit 5607e1b

Please sign in to comment.