rktk_drivers_nrf/softdevice/
mod.rs1use core::mem;
4
5use nrf_softdevice::{SocEvent, Softdevice, raw};
6use rktk::utils::Channel;
7
8#[cfg(feature = "softdevice-ble")]
9pub mod ble;
10pub mod flash;
11#[cfg(feature = "softdevice-ble")]
12pub mod split;
13pub mod vbus;
14
15#[allow(clippy::mut_from_ref)]
27pub fn init_softdevice(ble_gap_name: &'static str) -> &'static mut Softdevice {
28 let config = nrf_softdevice::Config {
29 clock: Some(raw::nrf_clock_lf_cfg_t {
30 source: raw::NRF_CLOCK_LF_SRC_RC as u8,
31 rc_ctiv: 16,
32 rc_temp_ctiv: 2,
33 accuracy: raw::NRF_CLOCK_LF_ACCURACY_500_PPM as u8,
34 }),
35 conn_gap: Some(raw::ble_gap_conn_cfg_t { conn_count: 6, event_length: 24 }),
36 conn_gatt: Some(raw::ble_gatt_conn_cfg_t { att_mtu: 256 }),
37 gatts_attr_tab_size: Some(raw::ble_gatts_cfg_attr_tab_size_t {
38 attr_tab_size: raw::BLE_GATTS_ATTR_TAB_SIZE_DEFAULT,
39 }),
40 gap_role_count: Some(raw::ble_gap_cfg_role_count_t {
41 adv_set_count: 1,
42 periph_role_count: 3,
43 central_role_count: 3,
44 central_sec_count: 0,
45 _bitfield_1: raw::ble_gap_cfg_role_count_t::new_bitfield_1(0),
46 }),
47 gap_device_name: Some(raw::ble_gap_cfg_device_name_t {
48 p_value: ble_gap_name.as_ptr() as _,
49 current_len: 9,
50 max_len: 9,
51 write_perm: unsafe { mem::zeroed() },
52 _bitfield_1: raw::ble_gap_cfg_device_name_t::new_bitfield_1(
53 raw::BLE_GATTS_VLOC_STACK as u8,
54 ),
55 }),
56 conn_l2cap: Some(raw::ble_l2cap_conn_cfg_t {
57 ch_count: 1,
58 rx_mps: 247,
59 tx_mps: 247,
60 rx_queue_size: 10,
61 tx_queue_size: 10,
62 }),
63 ..Default::default()
64 };
65
66 Softdevice::enable(&config)
67}
68
69pub fn start_softdevice(spawner: embassy_executor::Spawner, sd: &'static Softdevice) {
71 spawner.spawn(softdevice_task(sd).unwrap());
72}
73
74#[embassy_executor::task]
75async fn softdevice_task(sd: &'static Softdevice) -> ! {
76 unsafe {
77 nrf_softdevice::raw::sd_power_usbpwrrdy_enable(1);
78 nrf_softdevice::raw::sd_power_usbdetected_enable(1);
79 nrf_softdevice::raw::sd_power_usbremoved_enable(1);
80 nrf_softdevice::raw::sd_clock_hfclk_request();
81 }
82
83 sd.run_with_callback(|ev| {
84 let _ = SD_SOCEVENT_CHAN.try_send(ev);
85 })
86 .await
87}
88
89pub type SdEventChan = Channel<SocEvent, 8>;
90pub static SD_SOCEVENT_CHAN: SdEventChan = Channel::new();