esp-radio 0.18.0

A WiFi, Bluetooth and ESP-NOW driver for use with Espressif chips and bare-metal Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use procmacros::BuilderLite;

use super::*;
use crate::{
    ble::InvalidConfigError,
    hal::{interrupt, peripherals::BT},
    interrupt_dispatch::Handler,
    sys::include::esp_bt_controller_config_t,
};

static ISR_INTERRUPT_4: Handler = Handler::new();
static ISR_INTERRUPT_7: Handler = Handler::new();

/// Transmission Power Level
#[derive(Default, Clone, Copy, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum TxPower {
    /// -24 dBm
    N24,
    /// -21 dBm
    N21,
    /// -18 dBm
    N18,
    /// -15 dBm
    N15,
    /// -12 dBm
    N12,
    /// -9 dBm
    N9,
    /// -6 dBm
    N6,
    /// -3 dBm
    N3,
    /// 0 dBm
    N0,
    /// 3 dBm
    P3,
    /// 6 dBm
    P6,
    /// 9 dBm
    #[default]
    P9,
    /// 12 dBm
    P12,
    /// 15 dBm
    P15,
    /// 18 dBm
    P18,
    /// 20 dBm
    P20,
}

#[allow(dead_code)]
impl TxPower {
    fn idx(self) -> esp_power_level_t {
        match self {
            Self::N24 => esp_power_level_t_ESP_PWR_LVL_N24,
            Self::N21 => esp_power_level_t_ESP_PWR_LVL_N21,
            Self::N18 => esp_power_level_t_ESP_PWR_LVL_N18,
            Self::N15 => esp_power_level_t_ESP_PWR_LVL_N15,
            Self::N12 => esp_power_level_t_ESP_PWR_LVL_N12,
            Self::N9 => esp_power_level_t_ESP_PWR_LVL_N9,
            Self::N6 => esp_power_level_t_ESP_PWR_LVL_N6,
            Self::N3 => esp_power_level_t_ESP_PWR_LVL_N3,
            Self::N0 => esp_power_level_t_ESP_PWR_LVL_N0,
            Self::P3 => esp_power_level_t_ESP_PWR_LVL_P3,
            Self::P6 => esp_power_level_t_ESP_PWR_LVL_P6,
            Self::P9 => esp_power_level_t_ESP_PWR_LVL_P9,
            Self::P12 => esp_power_level_t_ESP_PWR_LVL_P12,
            Self::P15 => esp_power_level_t_ESP_PWR_LVL_P15,
            Self::P18 => esp_power_level_t_ESP_PWR_LVL_P18,
            Self::P20 => esp_power_level_t_ESP_PWR_LVL_P20,
        }
    }

    fn dbm(self) -> i8 {
        match self {
            Self::N24 => -24,
            Self::N21 => -21,
            Self::N18 => -18,
            Self::N15 => -15,
            Self::N12 => -12,
            Self::N9 => -9,
            Self::N6 => -6,
            Self::N3 => -3,
            Self::N0 => 0,
            Self::P3 => 3,
            Self::P6 => 6,
            Self::P9 => 9,
            Self::P12 => 12,
            Self::P15 => 15,
            Self::P18 => 18,
            Self::P20 => 20,
        }
    }
}

/// Bluetooth controller configuration.
#[derive(BuilderLite, Clone, Copy, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Config {
    /// The priority of the RTOS task.
    task_priority: u8,

    /// The stack size of the RTOS task.
    task_stack_size: u16,

    /// The maximum number of simultaneous connections.
    ///
    /// Range: 1 - 2
    max_connections: u16,

    /// Enable QA test mode.
    qa_test_mode: bool,

    /// Enable BQB test mode.
    bqb_test: bool,

    /// Size of the resolvable private address list.
    ll_resolv_list_size: u16,

    /// Maximum number of periodic advertiser list.
    ///
    /// Range: 1 - 5
    ll_sync_list_cnt: u8,

    /// Maximum number of periodic advertising syncs.
    ///
    /// Range: 0 - 3
    ll_sync_cnt: u8,

    /// Count of duplicated lists for scan response packets
    ///
    /// Range: 1 - 100
    ll_rsp_dup_list_count: u16,

    /// Count of duplicated lists for advertising packets
    ///
    /// Range: 1 - 100
    ll_adv_dup_list_count: u16,

    /// Default TX power.
    default_tx_power: TxPower,

    /// High Priority HCI Event Buffer count
    hci_high_buffer_count: u16,

    /// Low Priority HCI Event Buffer count
    hci_low_buffer_count: u16,

    /// Size of the whitelist.
    whitelist_size: u8,

    /// Buffer size of ACL (Asynchronous Connection-Less) data
    acl_buf_size: u16,

    /// Buffer count of ACL data
    acl_buf_count: u16,

    /// Buffer size for HCI event data
    hci_evt_buf_size: u16,

    /// Maximum number of extended advertising instances.
    multi_adv_instances: u16,

    /// Maximum size of extended advertising data
    ///
    /// Range: 0 - 1650
    ext_adv_max_size: u16,

    /// Disable scan backoff
    dis_scan_backoff: bool,

    /// The value of upperlimitmax during scan backoff procedure
    ///
    /// The value of upperlimitmax needs to be a power of 2.
    ///
    /// Range: 1 - 256
    scan_backoff_max: u16,

    /// Enables verification of the Access Address within the `CONNECT_IND` PDU.
    ///
    /// Enabling this option will add stricter verification of the Access Address in the
    /// `CONNECT_IND` PDU. This improves security by ensuring that only connection requests with
    /// valid Access Addresses are accepted. If disabled, only basic checks are applied,
    /// improving compatibility.
    verify_access_address: bool,

    /// Enable BLE Clear Channel Assessment (CCA).
    cca: bool,

    /// Absolute value of hardware-triggered CCA threshold.
    ///
    /// The CCA threshold is always negative.
    ///
    /// If the channel assessment result exceeds the CCA threshold (e.g. -75 dBm), indicating
    /// the channel is busy, the hardware will not transmit packets on that channel.
    ///
    /// Range: 20 dBm - 100 dBm
    cca_threshold: u8,

    /// Disconnect when Instant Passed (0x28) occurs during ACL connection update.
    disconnect_llcp_conn_update: bool,

    /// Disconnect when Instant Passed (0x28) occurs during ACL channel map update.
    disconnect_llcp_chan_map_update: bool,

    /// Disconnect when Instant Passed (0x28) occurs during ACL PHY update.
    disconnect_llcp_phy_update: bool,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            task_priority: crate::preempt::max_task_priority()
                .saturating_sub(2)
                .min(255) as u8,
            task_stack_size: CONFIG_BT_LE_CONTROLLER_TASK_STACK_SIZE as _,
            max_connections: CONFIG_BT_LE_MAX_CONNECTIONS as _,
            qa_test_mode: false,
            bqb_test: false,
            ll_resolv_list_size: CONFIG_BT_LE_LL_RESOLV_LIST_SIZE as _,
            ll_sync_list_cnt: CONFIG_BT_LE_MAX_PERIODIC_ADVERTISER_LIST as _,
            ll_sync_cnt: CONFIG_BT_LE_MAX_PERIODIC_SYNCS as _,
            ll_rsp_dup_list_count: CONFIG_BT_LE_LL_DUP_SCAN_LIST_COUNT as _,
            ll_adv_dup_list_count: CONFIG_BT_LE_LL_DUP_SCAN_LIST_COUNT as _,
            default_tx_power: TxPower::default(),
            hci_high_buffer_count: CONFIG_BT_LE_HCI_EVT_HI_BUF_COUNT as _,
            hci_low_buffer_count: CONFIG_BT_LE_HCI_EVT_LO_BUF_COUNT as _,
            whitelist_size: CONFIG_BT_LE_WHITELIST_SIZE as _,
            acl_buf_size: CONFIG_BT_LE_ACL_BUF_SIZE as _,
            acl_buf_count: CONFIG_BT_LE_ACL_BUF_COUNT as _,
            hci_evt_buf_size: CONFIG_BT_LE_HCI_EVT_BUF_SIZE as _,
            multi_adv_instances: CONFIG_BT_LE_MAX_EXT_ADV_INSTANCES as _,
            ext_adv_max_size: CONFIG_BT_LE_EXT_ADV_MAX_SIZE as _,
            dis_scan_backoff: false,
            scan_backoff_max: CONFIG_BT_CTRL_SCAN_BACKOFF_UPPERLIMITMAX as _,
            verify_access_address: false,
            disconnect_llcp_conn_update: false,
            disconnect_llcp_chan_map_update: false,
            disconnect_llcp_phy_update: false,
            cca_threshold: 65,
            cca: false,
        }
    }
}

impl Config {
    pub(crate) fn validate(&self) -> Result<(), InvalidConfigError> {
        crate::ble::validate_range!(
            self,
            task_priority,
            0,
            crate::preempt::max_task_priority().min(255) as u8
        );
        crate::ble::validate_range!(self, max_connections, 1, 2);
        crate::ble::validate_range!(self, ll_sync_cnt, 0, 3);
        crate::ble::validate_range!(self, ll_sync_list_cnt, 1, 5);
        crate::ble::validate_range!(self, ll_rsp_dup_list_count, 1, 100);
        crate::ble::validate_range!(self, ll_adv_dup_list_count, 1, 100);
        crate::ble::validate_range!(self, whitelist_size, 1, 31);
        crate::ble::validate_range!(self, multi_adv_instances, 0, 4);
        crate::ble::validate_range!(self, ext_adv_max_size, 0, 1650);
        crate::ble::validate_range!(self, scan_backoff_max, 1, 256);
        crate::ble::validate_range!(self, cca_threshold, 20, 100);
        Ok(())
    }
}

pub(crate) fn create_ble_config(config: &Config) -> esp_bt_controller_config_t {
    let main_xtal_freq = esp_hal::clock::xtal_clock().as_mhz() as u8;

    let rtc_freq = if main_xtal_freq == 26 { 40000 } else { 32000 };

    // keep them aligned with BT_CONTROLLER_INIT_CONFIG_DEFAULT in ESP-IDF
    // ideally _some_ of these values should be configurable
    esp_bt_controller_config_t {
        config_version: CONFIG_VERSION,
        ble_ll_resolv_list_size: config.ll_resolv_list_size,
        ble_hci_evt_hi_buf_count: config.hci_high_buffer_count,
        ble_hci_evt_lo_buf_count: config.hci_low_buffer_count,
        ble_ll_sync_list_cnt: config.ll_sync_list_cnt,
        ble_ll_sync_cnt: config.ll_sync_cnt,
        ble_ll_rsp_dup_list_count: config.ll_rsp_dup_list_count,
        ble_ll_adv_dup_list_count: config.ll_adv_dup_list_count,
        ble_ll_tx_pwr_dbm: config.default_tx_power.dbm() as u8,
        rtc_freq,
        ble_ll_sca: CONFIG_BT_LE_LL_SCA as _,
        ble_ll_scan_phy_number: if CONFIG_BT_LE_LL_CFG_FEAT_LE_CODED_PHY != 0 {
            2
        } else {
            1
        },
        ble_ll_conn_def_auth_pyld_tmo: 3000,
        ble_ll_jitter_usecs: 16,
        ble_ll_sched_max_adv_pdu_usecs: 376,
        ble_ll_sched_direct_adv_max_usecs: 502,
        ble_ll_sched_adv_max_usecs: 852,
        ble_scan_rsp_data_max_len: 31,
        ble_ll_cfg_num_hci_cmd_pkts: 1,
        ble_ll_ctrl_proc_timeout_ms: 40000,
        nimble_max_connections: config.max_connections,
        ble_whitelist_size: config.whitelist_size,
        ble_acl_buf_size: config.acl_buf_size,
        ble_acl_buf_count: config.acl_buf_count,
        ble_hci_evt_buf_size: config.hci_evt_buf_size,
        ble_multi_adv_instances: config.multi_adv_instances,
        ble_ext_adv_max_size: config.ext_adv_max_size,
        controller_task_stack_size: config.task_stack_size,
        controller_task_prio: config.task_priority,
        controller_run_cpu: 0,
        enable_qa_test: config.qa_test_mode as u8,
        enable_bqb_test: config.bqb_test as u8,
        enable_uart_hci: 0,
        ble_hci_uart_port: 0,
        ble_hci_uart_baud: 0,
        ble_hci_uart_data_bits: 0,
        ble_hci_uart_stop_bits: 0,
        ble_hci_uart_flow_ctrl: 0,
        ble_hci_uart_uart_parity: 0,
        enable_tx_cca: config.cca as u8,
        cca_rssi_thresh: (256 - config.cca_threshold as u32) as u8,
        sleep_en: 0, // CONFIG_BT_LE_SLEEP_ENABLE unset
        coex_phy_coded_tx_rx_time_limit: CONFIG_BT_LE_COEX_PHY_CODED_TX_RX_TLIM_EFF as _,
        dis_scan_backoff: config.dis_scan_backoff as u8,
        ble_scan_classify_filter_enable: 0,
        cca_drop_mode: 0,  //???
        cca_low_tx_pwr: 0, //???
        main_xtal_freq,
        version_num: crate::hal::efuse::minor_chip_version(),
        ignore_wl_for_direct_adv: 0,
        csa2_select: CONFIG_BT_LE_50_FEATURE_SUPPORT as _,
        ble_aa_check: config.verify_access_address as u8,
        ble_llcp_disc_flag: config.disconnect_llcp_conn_update as u8
            | ((config.disconnect_llcp_chan_map_update as u8) << 1)
            | ((config.disconnect_llcp_phy_update as u8) << 2),
        scan_backoff_upperlimitmax: config.scan_backoff_max,
        vhci_enabled: CONFIG_BT_LE_HCI_INTERFACE_USE_RAM as _,
        config_magic: CONFIG_MAGIC,
    }
}

pub(crate) fn bt_periph_module_enable() {
    // nothing
}

pub(crate) fn disable_sleep_mode() {
    // nothing
}

pub(super) unsafe extern "C" fn esp_intr_alloc(
    source: u32,
    flags: u32,
    handler: *mut c_void,
    arg: *mut c_void,
    ret_handle: *mut *mut c_void,
) -> i32 {
    trace!(
        "esp_intr_alloc {} {} {:?} {:?} {:?}",
        source, flags, handler, arg, ret_handle
    );

    match source {
        4 => unsafe {
            ISR_INTERRUPT_4.set(handler, arg);
            BT::steal().enable_mac_interrupt(interrupt::Priority::Priority1);
        },
        7 => unsafe {
            ISR_INTERRUPT_7.set(handler, arg);
            BT::steal().enable_lp_timer_interrupt(interrupt::Priority::Priority1);
        },
        _ => panic!("Unexpected interrupt source {}", source),
    }

    0
}

pub(super) fn ble_rtc_clk_init() {
    crate::radio_clocks::clocks_ll::ble_rtc_clk_init();
}

pub(super) unsafe extern "C" fn esp_reset_rpa_moudle() {
    trace!("esp_reset_rpa_moudle");
    crate::radio_clocks::clocks_ll::reset_rpa();
}

// Provide the symbol for < eco4 to make the linker happy
#[unsafe(no_mangle)]
unsafe fn g_ble_lll_rfmgmt_env_p() -> *mut c_void {
    // prevent "undefined symbol: g_ble_lll_rfmgmt_env_p" for ESP32-C2 < eco4
    unreachable!()
}

pub(crate) fn shutdown_ble_isr() {
    unsafe {
        BT::steal().disable_lp_timer_interrupt_on_all_cores();
        BT::steal().disable_mac_interrupt_on_all_cores();
    }
}

#[unsafe(no_mangle)]
#[crate::hal::ram]
extern "C" fn LP_TIMER() {
    ISR_INTERRUPT_7.dispatch();
}

#[unsafe(no_mangle)]
#[crate::hal::ram]
extern "C" fn BT_MAC() {
    ISR_INTERRUPT_4.dispatch();
}