Skip to content

Commit

Permalink
H2: Add BLE support (esp-rs#352)
Browse files Browse the repository at this point in the history
* H2: Add BLE support

* fix ci

* fix ci 2

* Add H2 into test scripts

* update to latest changes

* revert smoltcp change

* Update smoketest.bat

Co-authored-by: Scott Mabin <[email protected]>

---------

Co-authored-by: Scott Mabin <[email protected]>
  • Loading branch information
2 people authored and bjoernQ committed May 23, 2024
1 parent 27329f9 commit 054c5b0
Show file tree
Hide file tree
Showing 19 changed files with 627 additions and 16 deletions.
6 changes: 4 additions & 2 deletions esp-wifi/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Alias' for quickly building for different chips or running examples
# By default we enable
# By default we enable
# - `default` HAL features to set up basic chip specific settings
# - `embassy-time-timg0` as the examples assume we are using this time driver
# - `embassy-executor-thread` on Xtensa chips to take advantage of the Xtensa specific executor we have in esp-hal
Expand All @@ -10,13 +10,15 @@ esp32s3 = "run --features esp32s3 --target xtensa-esp32s3-none-elf --featur
esp32c2 = "run --features esp32c2 --target riscv32imc-unknown-none-elf --features esp32c2-hal/default,esp32c2-hal/embassy-time-timg0,esp32c2-hal/embassy-executor-thread"
esp32c3 = "run --features esp32c3 --target riscv32imc-unknown-none-elf --features esp32c3-hal/default,esp32c3-hal/embassy-time-timg0,esp32c3-hal/embassy-executor-thread"
esp32c6 = "run --features esp32c6 --target riscv32imac-unknown-none-elf --features esp32c6-hal/default,esp32c6-hal/embassy-time-timg0,esp32c6-hal/embassy-executor-thread"
esp32h2 = "run --features esp32h2 --target riscv32imac-unknown-none-elf --features esp32h2-hal/default,esp32h2-hal/embassy-time-timg0,esp32h2-hal/embassy-executor-thread"

besp32 = "build --features esp32 --target xtensa-esp32-none-elf --features esp32-hal/default,esp32-hal/embassy-time-timg0,esp32-hal/embassy-executor-thread"
besp32s2 = "build --features esp32s2 --target xtensa-esp32s2-none-elf --features esp32s2-hal/default,esp32s2-hal/embassy-time-timg0,esp32s2-hal/embassy-executor-thread"
besp32s3 = "build --features esp32s3 --target xtensa-esp32s3-none-elf --features esp32s3-hal/default,esp32s3-hal/embassy-time-timg0,esp32s3-hal/embassy-executor-thread"
besp32c2 = "build --features esp32c2 --target riscv32imc-unknown-none-elf --features esp32c2-hal/default,esp32c2-hal/embassy-time-timg0,esp32c2-hal/embassy-executor-thread"
besp32c3 = "build --features esp32c3 --target riscv32imc-unknown-none-elf --features esp32c3-hal/default,esp32c3-hal/embassy-time-timg0,esp32c3-hal/embassy-executor-thread"
besp32c6 = "build --features esp32c6 --target riscv32imac-unknown-none-elf --features esp32c6-hal/default,esp32c6-hal/embassy-time-timg0,esp32c6-hal/embassy-executor-thread"
besp32h2 = "build --features esp32h2 --target riscv32imac-unknown-none-elf --features esp32h2-hal/default,esp32h2-hal/embassy-time-timg0,esp32h2-hal/embassy-executor-thread"

[target.riscv32imc-unknown-none-elf]
runner = "espflash flash --monitor"
Expand Down Expand Up @@ -57,4 +59,4 @@ rustflags = [
]

[unstable]
build-std = [ "core" ]
build-std = [ "core" ]
6 changes: 6 additions & 0 deletions esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmt = { workspace = true, optional = true }
esp32c3-hal = { workspace = true, optional = true }
esp32c2-hal = { workspace = true, optional = true }
esp32c6-hal = { workspace = true, optional = true }
esp32h2-hal = { workspace = true, optional = true }
esp32-hal = { workspace = true, optional = true }
esp32s3-hal = { workspace = true, optional = true }
esp32s2-hal = { workspace = true, optional = true }
Expand Down Expand Up @@ -75,6 +76,7 @@ default = [ "log", "ipv4", "tcp", "udp", "icmp", "igmp", "dns", "dhcpv4" ]
esp32c2 = [ "esp32c2-hal", "esp-wifi-sys/esp32c2", "esp-println/esp32c2", "esp-backtrace/esp32c2" ]
esp32c3 = [ "esp32c3-hal", "esp-wifi-sys/esp32c3", "esp-println/esp32c3", "esp-backtrace/esp32c3" ]
esp32c6 = [ "esp32c6-hal", "esp-wifi-sys/esp32c6", "esp-println/esp32c6", "esp-backtrace/esp32c6" ]
esp32h2 = [ "esp32h2-hal", "esp-wifi-sys/esp32h2", "esp-println/esp32h2", "esp-backtrace/esp32h2" ]
esp32 = [ "esp32-hal", "esp-wifi-sys/esp32", "esp-println/esp32", "esp-backtrace/esp32" ]
esp32s2 = [ "esp32s2-hal", "esp-wifi-sys/esp32s2", "esp-println/esp32s2", "esp-backtrace/esp32s2" ]
esp32s3 = [ "esp32s3-hal", "esp-wifi-sys/esp32s3", "esp-println/esp32s3", "esp-backtrace/esp32s3" ]
Expand All @@ -87,12 +89,14 @@ async = [
"esp32c3-hal?/embassy",
"esp32c2-hal?/embassy",
"esp32c6-hal?/embassy",
"esp32h2-hal?/embassy",
"esp32-hal?/embassy",
"esp32s2-hal?/embassy",
"esp32s3-hal?/embassy",
"esp32c3-hal?/async",
"esp32c2-hal?/async",
"esp32c6-hal?/async",
"esp32h2-hal?/async",
"esp32-hal?/async",
"esp32s2-hal?/async",
"esp32s3-hal?/async",
Expand Down Expand Up @@ -127,6 +131,7 @@ defmt = [
"esp32c3-hal?/defmt",
"esp32c2-hal?/defmt",
"esp32c6-hal?/defmt",
"esp32h2-hal?/defmt",
"esp32-hal?/defmt",
"esp32s2-hal?/defmt",
"esp32s3-hal?/defmt",
Expand All @@ -136,6 +141,7 @@ log = [
"esp32c3-hal?/log",
"esp32c2-hal?/log",
"esp32c6-hal?/log",
"esp32h2-hal?/log",
"esp32-hal?/log",
"esp32s2-hal?/log",
"esp32s3-hal?/log",
Expand Down
2 changes: 1 addition & 1 deletion esp-wifi/automated-tests/test_ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use esp_backtrace as _;
use esp_println::println;
use esp_wifi::{ble::controller::BleConnector, initialize, EspWifiInitFor};
use examples_util::hal;
use hal::{clock::ClockControl, peripherals::*, prelude::*, Rng, IO};
use hal::{clock::ClockControl, peripherals::*, prelude::*, Rng};
#[path = "../../examples-util/util.rs"]
mod examples_util;

Expand Down
19 changes: 17 additions & 2 deletions esp-wifi/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
feature = "esp32c2",
feature = "esp32c3",
feature = "esp32c6",
feature = "esp32h2",
feature = "esp32s2",
feature = "esp32s3",
))]
Expand All @@ -17,12 +18,22 @@ fn main() -> Result<(), String> {
"#
);
}
#[cfg(all(feature = "wifi", feature = "esp32h2"))]
{
panic!(
r#"
WiFi is not supported on this target.
"#
);
}
#[cfg(all(feature = "coex", any(feature = "esp32s2")))]
{
panic!(
r#"
COEX is not yet supported on this target.
COEX is not yet supported on this target.
See https://github.com/esp-rs/esp-wifi/issues/92.
Expand All @@ -33,7 +44,7 @@ fn main() -> Result<(), String> {
Ok(level) => {
if level != "2" && level != "3" {
let message = format!(
"esp-wifi should be built with optimization level 2 or 3 - yours is {level}.
"esp-wifi should be built with optimization level 2 or 3 - yours is {level}.
See https://github.com/esp-rs/esp-wifi",
);
print_warning(message);
Expand All @@ -54,6 +65,9 @@ fn main() -> Result<(), String> {
#[cfg(feature = "esp32c6")]
println!("cargo:rustc-cfg=esp32c6");

#[cfg(feature = "esp32h2")]
println!("cargo:rustc-cfg=esp32h2");

#[cfg(feature = "esp32s2")]
println!("cargo:rustc-cfg=esp32s2");

Expand Down Expand Up @@ -124,6 +138,7 @@ fn try_read_xtensa_rustc_version(version: &str) -> Option<Version4> {
feature = "esp32c2",
feature = "esp32c3",
feature = "esp32c6",
feature = "esp32h2",
feature = "esp32s2",
feature = "esp32s3",
)))]
Expand Down
7 changes: 6 additions & 1 deletion esp-wifi/examples/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ fn main() -> ! {
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
#[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))]
let button = io.pins.gpio0.into_pull_down_input();
#[cfg(any(feature = "esp32c2", feature = "esp32c3", feature = "esp32c6"))]
#[cfg(any(
feature = "esp32c2",
feature = "esp32c3",
feature = "esp32c6",
feature = "esp32h2"
))]
let button = io.pins.gpio9.into_pull_down_input();

let mut debounce_cnt = 500;
Expand Down
7 changes: 6 additions & 1 deletion esp-wifi/examples/embassy_ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ async fn main(_spawner: Spawner) -> ! {
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
#[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))]
let button = io.pins.gpio0.into_pull_down_input();
#[cfg(any(feature = "esp32c2", feature = "esp32c3", feature = "esp32c6"))]
#[cfg(any(
feature = "esp32c2",
feature = "esp32c3",
feature = "esp32c6",
feature = "esp32h2"
))]
let button = io.pins.gpio9.into_pull_down_input();

// Async requires the GPIO interrupt to wake futures
Expand Down
4 changes: 2 additions & 2 deletions esp-wifi/src/ble/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
#[cfg(any(esp32, esp32c3, esp32s3))]
pub(crate) mod btdm;

#[cfg(any(esp32c2, esp32c6))]
#[cfg(any(esp32c2, esp32c6, esp32h2))]
pub(crate) mod npl;

use core::mem::MaybeUninit;

#[cfg(any(esp32, esp32c3, esp32s3))]
use self::btdm as ble;

#[cfg(any(esp32c2, esp32c6))]
#[cfg(any(esp32c2, esp32c6, esp32h2))]
use self::npl as ble;

pub(crate) use ble::ble_init;
Expand Down
1 change: 1 addition & 0 deletions esp-wifi/src/ble/npl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::timer::yield_task;

#[cfg_attr(esp32c2, path = "os_adapter_esp32c2.rs")]
#[cfg_attr(esp32c6, path = "os_adapter_esp32c6.rs")]
#[cfg_attr(esp32h2, path = "os_adapter_esp32h2.rs")]
pub(crate) mod ble_os_adapter_chip_specific;

const TIME_FOREVER: u32 = u32::MAX;
Expand Down
128 changes: 128 additions & 0 deletions esp-wifi/src/ble/os_adapter_esp32h2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
use crate::binary::include::esp_bt_controller_config_t;
use crate::common_adapter::RADIO_CLOCKS;
use crate::hal::system::RadioClockController;
use crate::hal::system::RadioPeripherals;

pub(crate) static mut ISR_INTERRUPT_15: (
*mut crate::binary::c_types::c_void,
*mut crate::binary::c_types::c_void,
) = (core::ptr::null_mut(), core::ptr::null_mut());

pub(crate) static mut ISR_INTERRUPT_3: (
*mut crate::binary::c_types::c_void,
*mut crate::binary::c_types::c_void,
) = (core::ptr::null_mut(), core::ptr::null_mut());

pub(crate) static BLE_CONFIG: esp_bt_controller_config_t = esp_bt_controller_config_t {
config_version: 0x20230113,
ble_ll_resolv_list_size: 4,
ble_hci_evt_hi_buf_count: 30,
ble_hci_evt_lo_buf_count: 8,
ble_ll_sync_list_cnt: 5,
ble_ll_sync_cnt: 20,
ble_ll_rsp_dup_list_count: 20,
ble_ll_adv_dup_list_count: 20,
ble_ll_tx_pwr_dbm: 9,
rtc_freq: 32000,
ble_ll_sca: 60,
ble_ll_scan_phy_number: 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: 2,
ble_whitelist_size: 12,
ble_acl_buf_size: 255,
ble_acl_buf_count: 24,
ble_hci_evt_buf_size: 70,
ble_multi_adv_instances: 1,
ble_ext_adv_max_size: 31,
controller_task_stack_size: 4096,
controller_task_prio: 253, // ???
controller_run_cpu: 0,
enable_qa_test: 0,
enable_bqb_test: 0,
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: 0,
cca_rssi_thresh: (256 - 50) as u8,
sleep_en: 0,
coex_phy_coded_tx_rx_time_limit: 0,
dis_scan_backoff: 0,
ble_scan_classify_filter_enable: 1,
cca_drop_mode: 0, //???
cca_low_tx_pwr: 0, //???
main_xtal_freq: 32,
ignore_wl_for_direct_adv: 0,
config_magic: 0x5A5AA5A5,

cpu_freq_mhz: 96,
enable_pcl: 0,
// version_num: 0,
};

pub(crate) fn bt_periph_module_enable() {
unsafe {
unwrap!(RADIO_CLOCKS.as_mut()).enable(RadioPeripherals::Bt);
}
}

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

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

match source {
3 => ISR_INTERRUPT_3 = (handler, arg),
15 => ISR_INTERRUPT_15 = (handler, arg),
_ => panic!("Unexpected interrupt source {}", source),
}

0
}

pub(super) fn ble_rtc_clk_init() {
unsafe {
unwrap!(RADIO_CLOCKS.as_mut()).ble_rtc_clk_init();
}
}

pub(super) unsafe extern "C" fn esp_reset_rpa_moudle() {
trace!("esp_reset_rpa_moudle");
unsafe {
unwrap!(RADIO_CLOCKS.as_mut()).reset_rpa();
}
}

#[allow(improper_ctypes_definitions)]
#[no_mangle]
unsafe extern "C" fn jrand48(
_xsubi: [crate::binary::c_types::c_ushort; 3],
) -> crate::binary::c_types::c_long {
// this is not very random but good enough for now - it's apparently not used for crypto
unsafe {
static mut VALUE: u32 = 0;
VALUE = VALUE.wrapping_add(3);
VALUE as i32
}
}
Loading

0 comments on commit 054c5b0

Please sign in to comment.