RKTK API Docs RKTK Home Repo

rktk_drivers_nrf/softdevice/
mod.rs

1//! Drivers that needs softdevice instance.
2
3use core::mem;
4
5use nrf_softdevice::{raw, Softdevice};
6
7#[cfg(feature = "softdevice-ble")]
8pub mod ble;
9pub mod flash;
10#[cfg(feature = "softdevice-ble")]
11pub mod split;
12
13/// Initialize the softdevice and return the instance.
14///
15/// # Usage
16/// This function enables softdevice, but doesn't start. To start softdevice, call `start_softdevice`.
17///
18/// ```
19/// let sd = init_softdevice(...);  // <-- Get mutable reference to softdevice
20/// start_server(sd).await;         // <-- Use softdevice for function which requires mutable reference to softdevice.
21/// start_softdevice(sd).await;     // <-- Starts softdevice. This function borrows softdevice forever, so from this point, you can only use immutable reference to softdevice.
22/// get_flash(sd).await;            // <-- get_flash does not require mutable reference to softdevice, so you can use this after starting softdevice;
23/// ```
24#[allow(clippy::mut_from_ref)]
25pub fn init_softdevice(ble_gap_name: &'static str) -> &'static mut Softdevice {
26    let config = nrf_softdevice::Config {
27        clock: Some(raw::nrf_clock_lf_cfg_t {
28            source: raw::NRF_CLOCK_LF_SRC_RC as u8,
29            rc_ctiv: 16,
30            rc_temp_ctiv: 2,
31            accuracy: raw::NRF_CLOCK_LF_ACCURACY_500_PPM as u8,
32        }),
33        conn_gap: Some(raw::ble_gap_conn_cfg_t {
34            conn_count: 6,
35            event_length: 24,
36        }),
37        conn_gatt: Some(raw::ble_gatt_conn_cfg_t { att_mtu: 256 }),
38        gatts_attr_tab_size: Some(raw::ble_gatts_cfg_attr_tab_size_t {
39            attr_tab_size: raw::BLE_GATTS_ATTR_TAB_SIZE_DEFAULT,
40        }),
41        gap_role_count: Some(raw::ble_gap_cfg_role_count_t {
42            adv_set_count: 1,
43            periph_role_count: 3,
44            central_role_count: 3,
45            central_sec_count: 0,
46            _bitfield_1: raw::ble_gap_cfg_role_count_t::new_bitfield_1(0),
47        }),
48        gap_device_name: Some(raw::ble_gap_cfg_device_name_t {
49            p_value: ble_gap_name.as_ptr() as _,
50            current_len: 9,
51            max_len: 9,
52            write_perm: unsafe { mem::zeroed() },
53            _bitfield_1: raw::ble_gap_cfg_device_name_t::new_bitfield_1(
54                raw::BLE_GATTS_VLOC_STACK as u8,
55            ),
56        }),
57        conn_l2cap: Some(raw::ble_l2cap_conn_cfg_t {
58            ch_count: 1,
59            rx_mps: 247,
60            tx_mps: 247,
61            rx_queue_size: 10,
62            tx_queue_size: 10,
63        }),
64        ..Default::default()
65    };
66
67    Softdevice::enable(&config)
68}
69
70/// Starts softdevice task
71pub async fn start_softdevice(sd: &'static Softdevice) {
72    let spawner = embassy_executor::Spawner::for_current_executor().await;
73    spawner.spawn(softdevice_task(sd)).unwrap();
74}
75
76#[embassy_executor::task]
77async fn softdevice_task(sd: &'static Softdevice) -> ! {
78    sd.run().await
79}