Skip to content

Commit 4ceefcd

Browse files
JurajSadelMabezDev
authored andcommitted
H2: Add BLE support (esp-rs#352)
* 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]>
1 parent b14e8a6 commit 4ceefcd

19 files changed

+627
-16
lines changed

esp-wifi/.cargo/config.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Alias' for quickly building for different chips or running examples
2-
# By default we enable
2+
# By default we enable
33
# - `default` HAL features to set up basic chip specific settings
44
# - `embassy-time-timg0` as the examples assume we are using this time driver
55
# - `embassy-executor-thread` on Xtensa chips to take advantage of the Xtensa specific executor we have in esp-hal
@@ -10,13 +10,15 @@ esp32s3 = "run --features esp32s3 --target xtensa-esp32s3-none-elf --featur
1010
esp32c2 = "run --features esp32c2 --target riscv32imc-unknown-none-elf --features esp32c2-hal/default,esp32c2-hal/embassy-time-timg0,esp32c2-hal/embassy-executor-thread"
1111
esp32c3 = "run --features esp32c3 --target riscv32imc-unknown-none-elf --features esp32c3-hal/default,esp32c3-hal/embassy-time-timg0,esp32c3-hal/embassy-executor-thread"
1212
esp32c6 = "run --features esp32c6 --target riscv32imac-unknown-none-elf --features esp32c6-hal/default,esp32c6-hal/embassy-time-timg0,esp32c6-hal/embassy-executor-thread"
13+
esp32h2 = "run --features esp32h2 --target riscv32imac-unknown-none-elf --features esp32h2-hal/default,esp32h2-hal/embassy-time-timg0,esp32h2-hal/embassy-executor-thread"
1314

1415
besp32 = "build --features esp32 --target xtensa-esp32-none-elf --features esp32-hal/default,esp32-hal/embassy-time-timg0,esp32-hal/embassy-executor-thread"
1516
besp32s2 = "build --features esp32s2 --target xtensa-esp32s2-none-elf --features esp32s2-hal/default,esp32s2-hal/embassy-time-timg0,esp32s2-hal/embassy-executor-thread"
1617
besp32s3 = "build --features esp32s3 --target xtensa-esp32s3-none-elf --features esp32s3-hal/default,esp32s3-hal/embassy-time-timg0,esp32s3-hal/embassy-executor-thread"
1718
besp32c2 = "build --features esp32c2 --target riscv32imc-unknown-none-elf --features esp32c2-hal/default,esp32c2-hal/embassy-time-timg0,esp32c2-hal/embassy-executor-thread"
1819
besp32c3 = "build --features esp32c3 --target riscv32imc-unknown-none-elf --features esp32c3-hal/default,esp32c3-hal/embassy-time-timg0,esp32c3-hal/embassy-executor-thread"
1920
besp32c6 = "build --features esp32c6 --target riscv32imac-unknown-none-elf --features esp32c6-hal/default,esp32c6-hal/embassy-time-timg0,esp32c6-hal/embassy-executor-thread"
21+
besp32h2 = "build --features esp32h2 --target riscv32imac-unknown-none-elf --features esp32h2-hal/default,esp32h2-hal/embassy-time-timg0,esp32h2-hal/embassy-executor-thread"
2022

2123
[target.riscv32imc-unknown-none-elf]
2224
runner = "espflash flash --monitor"
@@ -57,4 +59,4 @@ rustflags = [
5759
]
5860

5961
[unstable]
60-
build-std = [ "core" ]
62+
build-std = [ "core" ]

esp-wifi/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ defmt = { workspace = true, optional = true }
2626
esp32c3-hal = { workspace = true, optional = true }
2727
esp32c2-hal = { workspace = true, optional = true }
2828
esp32c6-hal = { workspace = true, optional = true }
29+
esp32h2-hal = { workspace = true, optional = true }
2930
esp32-hal = { workspace = true, optional = true }
3031
esp32s3-hal = { workspace = true, optional = true }
3132
esp32s2-hal = { workspace = true, optional = true }
@@ -75,6 +76,7 @@ default = [ "log", "ipv4", "tcp", "udp", "icmp", "igmp", "dns", "dhcpv4" ]
7576
esp32c2 = [ "esp32c2-hal", "esp-wifi-sys/esp32c2", "esp-println/esp32c2", "esp-backtrace/esp32c2" ]
7677
esp32c3 = [ "esp32c3-hal", "esp-wifi-sys/esp32c3", "esp-println/esp32c3", "esp-backtrace/esp32c3" ]
7778
esp32c6 = [ "esp32c6-hal", "esp-wifi-sys/esp32c6", "esp-println/esp32c6", "esp-backtrace/esp32c6" ]
79+
esp32h2 = [ "esp32h2-hal", "esp-wifi-sys/esp32h2", "esp-println/esp32h2", "esp-backtrace/esp32h2" ]
7880
esp32 = [ "esp32-hal", "esp-wifi-sys/esp32", "esp-println/esp32", "esp-backtrace/esp32" ]
7981
esp32s2 = [ "esp32s2-hal", "esp-wifi-sys/esp32s2", "esp-println/esp32s2", "esp-backtrace/esp32s2" ]
8082
esp32s3 = [ "esp32s3-hal", "esp-wifi-sys/esp32s3", "esp-println/esp32s3", "esp-backtrace/esp32s3" ]
@@ -87,12 +89,14 @@ async = [
8789
"esp32c3-hal?/embassy",
8890
"esp32c2-hal?/embassy",
8991
"esp32c6-hal?/embassy",
92+
"esp32h2-hal?/embassy",
9093
"esp32-hal?/embassy",
9194
"esp32s2-hal?/embassy",
9295
"esp32s3-hal?/embassy",
9396
"esp32c3-hal?/async",
9497
"esp32c2-hal?/async",
9598
"esp32c6-hal?/async",
99+
"esp32h2-hal?/async",
96100
"esp32-hal?/async",
97101
"esp32s2-hal?/async",
98102
"esp32s3-hal?/async",
@@ -127,6 +131,7 @@ defmt = [
127131
"esp32c3-hal?/defmt",
128132
"esp32c2-hal?/defmt",
129133
"esp32c6-hal?/defmt",
134+
"esp32h2-hal?/defmt",
130135
"esp32-hal?/defmt",
131136
"esp32s2-hal?/defmt",
132137
"esp32s3-hal?/defmt",
@@ -136,6 +141,7 @@ log = [
136141
"esp32c3-hal?/log",
137142
"esp32c2-hal?/log",
138143
"esp32c6-hal?/log",
144+
"esp32h2-hal?/log",
139145
"esp32-hal?/log",
140146
"esp32s2-hal?/log",
141147
"esp32s3-hal?/log",

esp-wifi/automated-tests/test_ble.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use esp_backtrace as _;
1414
use esp_println::println;
1515
use esp_wifi::{ble::controller::BleConnector, initialize, EspWifiInitFor};
1616
use examples_util::hal;
17-
use hal::{clock::ClockControl, peripherals::*, prelude::*, Rng, IO};
17+
use hal::{clock::ClockControl, peripherals::*, prelude::*, Rng};
1818
#[path = "../../examples-util/util.rs"]
1919
mod examples_util;
2020

esp-wifi/build.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
feature = "esp32c2",
44
feature = "esp32c3",
55
feature = "esp32c6",
6+
feature = "esp32h2",
67
feature = "esp32s2",
78
feature = "esp32s3",
89
))]
@@ -17,12 +18,22 @@ fn main() -> Result<(), String> {
1718
"#
1819
);
1920
}
21+
#[cfg(all(feature = "wifi", feature = "esp32h2"))]
22+
{
23+
panic!(
24+
r#"
25+
26+
WiFi is not supported on this target.
27+
28+
"#
29+
);
30+
}
2031
#[cfg(all(feature = "coex", any(feature = "esp32s2")))]
2132
{
2233
panic!(
2334
r#"
2435
25-
COEX is not yet supported on this target.
36+
COEX is not yet supported on this target.
2637
2738
See https://github.com/esp-rs/esp-wifi/issues/92.
2839
@@ -33,7 +44,7 @@ fn main() -> Result<(), String> {
3344
Ok(level) => {
3445
if level != "2" && level != "3" {
3546
let message = format!(
36-
"esp-wifi should be built with optimization level 2 or 3 - yours is {level}.
47+
"esp-wifi should be built with optimization level 2 or 3 - yours is {level}.
3748
See https://github.com/esp-rs/esp-wifi",
3849
);
3950
print_warning(message);
@@ -54,6 +65,9 @@ fn main() -> Result<(), String> {
5465
#[cfg(feature = "esp32c6")]
5566
println!("cargo:rustc-cfg=esp32c6");
5667

68+
#[cfg(feature = "esp32h2")]
69+
println!("cargo:rustc-cfg=esp32h2");
70+
5771
#[cfg(feature = "esp32s2")]
5872
println!("cargo:rustc-cfg=esp32s2");
5973

@@ -124,6 +138,7 @@ fn try_read_xtensa_rustc_version(version: &str) -> Option<Version4> {
124138
feature = "esp32c2",
125139
feature = "esp32c3",
126140
feature = "esp32c6",
141+
feature = "esp32h2",
127142
feature = "esp32s2",
128143
feature = "esp32s3",
129144
)))]

esp-wifi/examples/ble.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ fn main() -> ! {
4242
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
4343
#[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))]
4444
let button = io.pins.gpio0.into_pull_down_input();
45-
#[cfg(any(feature = "esp32c2", feature = "esp32c3", feature = "esp32c6"))]
45+
#[cfg(any(
46+
feature = "esp32c2",
47+
feature = "esp32c3",
48+
feature = "esp32c6",
49+
feature = "esp32h2"
50+
))]
4651
let button = io.pins.gpio9.into_pull_down_input();
4752

4853
let mut debounce_cnt = 500;

esp-wifi/examples/embassy_ble.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ async fn main(_spawner: Spawner) -> ! {
5050
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
5151
#[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))]
5252
let button = io.pins.gpio0.into_pull_down_input();
53-
#[cfg(any(feature = "esp32c2", feature = "esp32c3", feature = "esp32c6"))]
53+
#[cfg(any(
54+
feature = "esp32c2",
55+
feature = "esp32c3",
56+
feature = "esp32c6",
57+
feature = "esp32h2"
58+
))]
5459
let button = io.pins.gpio9.into_pull_down_input();
5560

5661
// Async requires the GPIO interrupt to wake futures

esp-wifi/src/ble/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
#[cfg(any(esp32, esp32c3, esp32s3))]
44
pub(crate) mod btdm;
55

6-
#[cfg(any(esp32c2, esp32c6))]
6+
#[cfg(any(esp32c2, esp32c6, esp32h2))]
77
pub(crate) mod npl;
88

99
use core::mem::MaybeUninit;
1010

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

14-
#[cfg(any(esp32c2, esp32c6))]
14+
#[cfg(any(esp32c2, esp32c6, esp32h2))]
1515
use self::npl as ble;
1616

1717
pub(crate) use ble::ble_init;

esp-wifi/src/ble/npl.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::timer::yield_task;
1616

1717
#[cfg_attr(esp32c2, path = "os_adapter_esp32c2.rs")]
1818
#[cfg_attr(esp32c6, path = "os_adapter_esp32c6.rs")]
19+
#[cfg_attr(esp32h2, path = "os_adapter_esp32h2.rs")]
1920
pub(crate) mod ble_os_adapter_chip_specific;
2021

2122
const TIME_FOREVER: u32 = u32::MAX;
+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
use crate::binary::include::esp_bt_controller_config_t;
2+
use crate::common_adapter::RADIO_CLOCKS;
3+
use crate::hal::system::RadioClockController;
4+
use crate::hal::system::RadioPeripherals;
5+
6+
pub(crate) static mut ISR_INTERRUPT_15: (
7+
*mut crate::binary::c_types::c_void,
8+
*mut crate::binary::c_types::c_void,
9+
) = (core::ptr::null_mut(), core::ptr::null_mut());
10+
11+
pub(crate) static mut ISR_INTERRUPT_3: (
12+
*mut crate::binary::c_types::c_void,
13+
*mut crate::binary::c_types::c_void,
14+
) = (core::ptr::null_mut(), core::ptr::null_mut());
15+
16+
pub(crate) static BLE_CONFIG: esp_bt_controller_config_t = esp_bt_controller_config_t {
17+
config_version: 0x20230113,
18+
ble_ll_resolv_list_size: 4,
19+
ble_hci_evt_hi_buf_count: 30,
20+
ble_hci_evt_lo_buf_count: 8,
21+
ble_ll_sync_list_cnt: 5,
22+
ble_ll_sync_cnt: 20,
23+
ble_ll_rsp_dup_list_count: 20,
24+
ble_ll_adv_dup_list_count: 20,
25+
ble_ll_tx_pwr_dbm: 9,
26+
rtc_freq: 32000,
27+
ble_ll_sca: 60,
28+
ble_ll_scan_phy_number: 1,
29+
ble_ll_conn_def_auth_pyld_tmo: 3000,
30+
ble_ll_jitter_usecs: 16,
31+
ble_ll_sched_max_adv_pdu_usecs: 376,
32+
ble_ll_sched_direct_adv_max_usecs: 502,
33+
ble_ll_sched_adv_max_usecs: 852,
34+
ble_scan_rsp_data_max_len: 31,
35+
ble_ll_cfg_num_hci_cmd_pkts: 1,
36+
ble_ll_ctrl_proc_timeout_ms: 40000,
37+
nimble_max_connections: 2,
38+
ble_whitelist_size: 12,
39+
ble_acl_buf_size: 255,
40+
ble_acl_buf_count: 24,
41+
ble_hci_evt_buf_size: 70,
42+
ble_multi_adv_instances: 1,
43+
ble_ext_adv_max_size: 31,
44+
controller_task_stack_size: 4096,
45+
controller_task_prio: 253, // ???
46+
controller_run_cpu: 0,
47+
enable_qa_test: 0,
48+
enable_bqb_test: 0,
49+
enable_uart_hci: 0,
50+
ble_hci_uart_port: 0,
51+
ble_hci_uart_baud: 0,
52+
ble_hci_uart_data_bits: 0,
53+
ble_hci_uart_stop_bits: 0,
54+
ble_hci_uart_flow_ctrl: 0,
55+
ble_hci_uart_uart_parity: 0,
56+
enable_tx_cca: 0,
57+
cca_rssi_thresh: (256 - 50) as u8,
58+
sleep_en: 0,
59+
coex_phy_coded_tx_rx_time_limit: 0,
60+
dis_scan_backoff: 0,
61+
ble_scan_classify_filter_enable: 1,
62+
cca_drop_mode: 0, //???
63+
cca_low_tx_pwr: 0, //???
64+
main_xtal_freq: 32,
65+
ignore_wl_for_direct_adv: 0,
66+
config_magic: 0x5A5AA5A5,
67+
68+
cpu_freq_mhz: 96,
69+
enable_pcl: 0,
70+
// version_num: 0,
71+
};
72+
73+
pub(crate) fn bt_periph_module_enable() {
74+
unsafe {
75+
unwrap!(RADIO_CLOCKS.as_mut()).enable(RadioPeripherals::Bt);
76+
}
77+
}
78+
79+
pub(crate) fn disable_sleep_mode() {
80+
// nothing
81+
}
82+
83+
pub(super) unsafe extern "C" fn esp_intr_alloc(
84+
source: u32,
85+
flags: u32,
86+
handler: *mut crate::binary::c_types::c_void,
87+
arg: *mut crate::binary::c_types::c_void,
88+
ret_handle: *mut *mut crate::binary::c_types::c_void,
89+
) -> i32 {
90+
debug!(
91+
"esp_intr_alloc {} {} {:?} {:?} {:?}",
92+
source, flags, handler, arg, ret_handle
93+
);
94+
95+
match source {
96+
3 => ISR_INTERRUPT_3 = (handler, arg),
97+
15 => ISR_INTERRUPT_15 = (handler, arg),
98+
_ => panic!("Unexpected interrupt source {}", source),
99+
}
100+
101+
0
102+
}
103+
104+
pub(super) fn ble_rtc_clk_init() {
105+
unsafe {
106+
unwrap!(RADIO_CLOCKS.as_mut()).ble_rtc_clk_init();
107+
}
108+
}
109+
110+
pub(super) unsafe extern "C" fn esp_reset_rpa_moudle() {
111+
trace!("esp_reset_rpa_moudle");
112+
unsafe {
113+
unwrap!(RADIO_CLOCKS.as_mut()).reset_rpa();
114+
}
115+
}
116+
117+
#[allow(improper_ctypes_definitions)]
118+
#[no_mangle]
119+
unsafe extern "C" fn jrand48(
120+
_xsubi: [crate::binary::c_types::c_ushort; 3],
121+
) -> crate::binary::c_types::c_long {
122+
// this is not very random but good enough for now - it's apparently not used for crypto
123+
unsafe {
124+
static mut VALUE: u32 = 0;
125+
VALUE = VALUE.wrapping_add(3);
126+
VALUE as i32
127+
}
128+
}

0 commit comments

Comments
 (0)