diff --git a/esp-wifi/src/ble/btdm.rs b/esp-wifi/src/ble/btdm.rs index f91900068a1..4433a310b55 100644 --- a/esp-wifi/src/ble/btdm.rs +++ b/esp-wifi/src/ble/btdm.rs @@ -339,11 +339,11 @@ unsafe extern "C" fn cause_sw_intr_to_core(_core: i32, _intr_no: i32) -> i32 { } unsafe extern "C" fn malloc(size: u32) -> *const () { - crate::compat::malloc::malloc(size) as *const () + crate::compat::malloc::malloc(size as usize) as *const () } unsafe extern "C" fn malloc_internal(size: u32) -> *const () { - crate::compat::malloc::malloc(size) as *const () + crate::compat::malloc::malloc(size as usize) as *const () } unsafe extern "C" fn free(ptr: *const ()) { diff --git a/esp-wifi/src/ble/mod.rs b/esp-wifi/src/ble/mod.rs index a9aa74d4430..ad83ec0b765 100644 --- a/esp-wifi/src/ble/mod.rs +++ b/esp-wifi/src/ble/mod.rs @@ -1,7 +1,7 @@ #[cfg(any(esp32, esp32c3, esp32s3))] pub(crate) mod btdm; -#[cfg(any(esp32c2))] +#[cfg(any(esp32c2, esp32c6))] pub(crate) mod npl; use core::mem::MaybeUninit; @@ -9,7 +9,7 @@ use core::mem::MaybeUninit; #[cfg(any(esp32, esp32c3, esp32s3))] use self::btdm as ble; -#[cfg(any(esp32c2))] +#[cfg(any(esp32c2, esp32c6))] use self::npl as ble; pub(crate) use ble::ble_init; diff --git a/esp-wifi/src/ble/npl.rs b/esp-wifi/src/ble/npl.rs index 3c10e42a513..01f5b2e58ea 100644 --- a/esp-wifi/src/ble/npl.rs +++ b/esp-wifi/src/ble/npl.rs @@ -11,15 +11,16 @@ use crate::compat::queue::SimpleQueue; use crate::timer::yield_task; #[cfg_attr(esp32c2, path = "os_adapter_esp32c2.rs")] +#[cfg_attr(esp32c6, path = "os_adapter_esp32c6.rs")] pub(crate) mod ble_os_adapter_chip_specific; const TIME_FOREVER: u32 = u32::MAX; const OS_MSYS_1_BLOCK_COUNT: i32 = 24; -const SYSINIT_MSYS_1_MEMPOOL_SIZE: u32 = 768; +const SYSINIT_MSYS_1_MEMPOOL_SIZE: usize = 768; const SYSINIT_MSYS_1_MEMBLOCK_SIZE: i32 = 128; const OS_MSYS_2_BLOCK_COUNT: i32 = 24; -const SYSINIT_MSYS_2_MEMPOOL_SIZE: u32 = 1920; +const SYSINIT_MSYS_2_MEMPOOL_SIZE: usize = 1920; const SYSINIT_MSYS_2_MEMBLOCK_SIZE: i32 = 320; const BLE_HCI_TRANS_BUF_CMD: i32 = 3; @@ -217,6 +218,7 @@ pub(crate) static mut OS_MSYS_INIT_2_MEMPOOL: OsMempool = OsMempool::zeroed(); extern "C" { static ble_hci_trans_funcs_ptr: &'static BleHciTransFuncsT; + #[cfg(esp32c2)] static mut r_ble_stub_funcs_ptr: *mut u32; pub(crate) fn ble_controller_init(cfg: *const esp_bt_controller_config_t) -> i32; @@ -319,6 +321,7 @@ pub struct ext_funcs_t { ecc_gen_key_pair: Option i32>, ecc_gen_dh_key: Option i32>, esp_reset_rpa_moudle: Option, + #[cfg(esp32c2)] esp_bt_track_pll_cap: Option, magic: u32, } @@ -327,7 +330,7 @@ static G_OSI_FUNCS: ext_funcs_t = ext_funcs_t { ext_version: 0x20221122, esp_intr_alloc: Some(self::ble_os_adapter_chip_specific::esp_intr_alloc), esp_intr_free: Some(esp_intr_free), - malloc: Some(malloc), + malloc: Some(self::malloc), free: Some(free), hal_uart_start_tx: None, hal_uart_init_cbs: None, @@ -342,6 +345,7 @@ static G_OSI_FUNCS: ext_funcs_t = ext_funcs_t { ecc_gen_key_pair: Some(ecc_gen_key_pair), ecc_gen_dh_key: Some(ecc_gen_dh_key), esp_reset_rpa_moudle: Some(self::ble_os_adapter_chip_specific::esp_reset_rpa_moudle), + #[cfg(esp32c2)] esp_bt_track_pll_cap: None, magic: 0xA5A5A5A5, }; @@ -420,7 +424,7 @@ unsafe extern "C" fn esp_intr_free(_ret_handle: *mut *mut crate::binary::c_types } unsafe extern "C" fn malloc(size: u32) -> *const u8 { - crate::compat::malloc::malloc(size) + crate::compat::malloc::malloc(size as usize) } unsafe extern "C" fn free(ptr: *const crate::binary::c_types::c_void) { @@ -1112,7 +1116,11 @@ pub(crate) fn ble_init() { // "patch" r_ble_ll_random - it needs syscall_table_ptr // probably long term we should rather initialize syscall_table_ptr - *(r_ble_stub_funcs_ptr.offset(0x7dc / 4)) = ble_ll_random_override as *const u32 as u32; + #[cfg(esp32c2)] + { + *(r_ble_stub_funcs_ptr.offset(0x7dc / 4)) = + self::ble_os_adapter_chip_specific::ble_ll_random_override as *const u32 as u32; + } // this is a workaround for an unclear problem // (ASSERT r_ble_hci_ram_hs_cmd_tx:34 0 0) @@ -1122,24 +1130,15 @@ pub(crate) fn ble_init() { } } -unsafe extern "C" fn ble_ll_random_override() -> u32 { - // this is not very random but good enough for now - it's not used for crypto - unsafe { - static mut VALUE: u32 = 0; - VALUE = VALUE.wrapping_add(3); - VALUE - } -} - fn os_msys_buf_alloc() -> bool { unsafe { OS_MSYS_INIT_1_DATA = crate::compat::malloc::calloc( 1, - core::mem::size_of::() as u32 * SYSINIT_MSYS_1_MEMPOOL_SIZE, + core::mem::size_of::() * SYSINIT_MSYS_1_MEMPOOL_SIZE as usize, ) as *mut u32; OS_MSYS_INIT_2_DATA = crate::compat::malloc::calloc( 1, - core::mem::size_of::() as u32 * SYSINIT_MSYS_2_MEMPOOL_SIZE, + core::mem::size_of::() * SYSINIT_MSYS_2_MEMPOOL_SIZE, ) as *mut u32; !(OS_MSYS_INIT_1_DATA.is_null() || OS_MSYS_INIT_2_DATA.is_null()) diff --git a/esp-wifi/src/ble/os_adapter_esp32c2.rs b/esp-wifi/src/ble/os_adapter_esp32c2.rs index e0097d695ca..80b59287043 100644 --- a/esp-wifi/src/ble/os_adapter_esp32c2.rs +++ b/esp-wifi/src/ble/os_adapter_esp32c2.rs @@ -110,3 +110,12 @@ pub(super) unsafe extern "C" fn esp_reset_rpa_moudle() { unwrap!(RADIO_CLOCKS.as_mut()).reset_rpa(); } } + +pub(super) unsafe extern "C" fn ble_ll_random_override() -> u32 { + // this is not very random but good enough for now - it's not used for crypto + unsafe { + static mut VALUE: u32 = 0; + VALUE = VALUE.wrapping_add(3); + VALUE + } +} diff --git a/esp-wifi/src/ble/os_adapter_esp32c6.rs b/esp-wifi/src/ble/os_adapter_esp32c6.rs new file mode 100644 index 00000000000..2c38b6c70f3 --- /dev/null +++ b/esp-wifi/src/ble/os_adapter_esp32c6.rs @@ -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_4: ( + *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_7: ( + *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: 0, + cca_drop_mode: 0, //??? + cca_low_tx_pwr: 0, //??? + main_xtal_freq: 40, + ignore_wl_for_direct_adv: 0, + config_magic: 0x5A5AA5A5, + + cpu_freq_mhz: 160, + enable_pcl: 0, // CONFIG_BT_LE_POWER_CONTROL_ENABLED + 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 { + 4 => ISR_INTERRUPT_4 = (handler, arg), + 7 => ISR_INTERRUPT_7 = (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 + } +} diff --git a/esp-wifi/src/compat/malloc.rs b/esp-wifi/src/compat/malloc.rs index aa55816137a..27dcce165e4 100644 --- a/esp-wifi/src/compat/malloc.rs +++ b/esp-wifi/src/compat/malloc.rs @@ -2,7 +2,7 @@ use core::alloc::Layout; use crate::HEAP; -pub unsafe extern "C" fn malloc(size: u32) -> *const u8 { +pub unsafe extern "C" fn malloc(size: usize) -> *const u8 { trace!("alloc {}", size); let total_size = size as usize + 4; @@ -44,10 +44,10 @@ pub unsafe extern "C" fn free(ptr: *const u8) { } #[no_mangle] -pub unsafe extern "C" fn calloc(number: u32, size: u32) -> *const u8 { +pub unsafe extern "C" fn calloc(number: u32, size: usize) -> *const u8 { trace!("calloc {} {}", number, size); - let total_size = number * size; + let total_size = number as usize * size; let ptr = malloc(total_size) as *mut u8; if !ptr.is_null() { diff --git a/esp-wifi/src/esp_now/mod.rs b/esp-wifi/src/esp_now/mod.rs index ccdfbe768fb..d6b0dc1a94a 100644 --- a/esp-wifi/src/esp_now/mod.rs +++ b/esp-wifi/src/esp_now/mod.rs @@ -428,7 +428,7 @@ impl<'d> EspNowSender<'d> { data: &[u8], ) -> Result, EspNowError> { ESP_NOW_SEND_CB_INVOKED.store(false, Ordering::Release); - check_error!({ esp_now_send(dst_addr.as_ptr(), data.as_ptr(), data.len() as u32) })?; + check_error!({ esp_now_send(dst_addr.as_ptr(), data.as_ptr(), data.len()) })?; Ok(SendWaiter(PhantomData)) } } @@ -851,11 +851,7 @@ mod asynch { ESP_NOW_TX_WAKER.register(cx.waker()); ESP_NOW_SEND_CB_INVOKED.store(false, Ordering::Release); if let Err(e) = check_error!({ - esp_now_send( - self.addr.as_ptr(), - self.data.as_ptr(), - self.data.len() as u32, - ) + esp_now_send(self.addr.as_ptr(), self.data.as_ptr(), self.data.len()) }) { return Poll::Ready(Err(e)); } diff --git a/esp-wifi/src/timer_esp32c6.rs b/esp-wifi/src/timer_esp32c6.rs index d9b5a156131..81496f44f27 100644 --- a/esp-wifi/src/timer_esp32c6.rs +++ b/esp-wifi/src/timer_esp32c6.rs @@ -56,15 +56,11 @@ pub fn setup_timer_isr(systimer: Alarm) { #[cfg(feature = "ble")] { unwrap!(interrupt::enable( - Interrupt::RWBT, + Interrupt::LP_TIMER, interrupt::Priority::Priority1 )); unwrap!(interrupt::enable( - Interrupt::RWBLE, - interrupt::Priority::Priority1 - )); - unwrap!(interrupt::enable( - Interrupt::BT_BB, + Interrupt::BT_MAC, interrupt::Priority::Priority1 )); } @@ -117,61 +113,45 @@ fn WIFI_PWR() { #[cfg(feature = "ble")] #[interrupt] -fn RWBT() { +fn LP_TIMER() { unsafe { - let intr = &*peripherals::INTERRUPT_CORE0::ptr(); - intr.cpu_int_clear.write(|w| w.bits(1 << 1)); + trace!("LP_TIMER interrupt"); - let (fnc, arg) = crate::ble::btdm::ble_os_adapter_chip_specific::BT_INTERRUPT_FUNCTION5; + let (fnc, arg) = crate::ble::npl::ble_os_adapter_chip_specific::ISR_INTERRUPT_7; - trace!("interrupt RWBT {:?} {:?}", fnc, arg); + trace!("interrupt LP_TIMER {:?} {:?}", fnc, arg); if !fnc.is_null() { - let fnc: fn(*mut binary::c_types::c_void) = core::mem::transmute(fnc); - fnc(arg); - } - - trace!("interrupt 5 done"); - }; -} + trace!("interrupt LP_TIMER call"); -#[cfg(feature = "ble")] -#[interrupt] -fn RWBLE() { - unsafe { - let intr = &*peripherals::INTERRUPT_CORE0::ptr(); - intr.cpu_int_clear.write(|w| w.bits(1 << 1)); - - let (fnc, arg) = crate::ble::btdm::ble_os_adapter_chip_specific::BT_INTERRUPT_FUNCTION5; - - trace!("interrupt RWBLE {:?} {:?}", fnc, arg); - - if !fnc.is_null() { let fnc: fn(*mut binary::c_types::c_void) = core::mem::transmute(fnc); fnc(arg); + trace!("LP_TIMER done"); } - trace!("interrupt 5 done"); + trace!("interrupt LP_TIMER done"); }; } #[cfg(feature = "ble")] #[interrupt] -fn BT_BB(_trap_frame: &mut TrapFrame) { +fn BT_MAC() { unsafe { - let intr = &*peripherals::INTERRUPT_CORE0::ptr(); - intr.cpu_int_clear.write(|w| w.bits(1 << 1)); + trace!("BT_MAC interrupt"); - let (fnc, arg) = crate::ble::btdm::ble_os_adapter_chip_specific::BT_INTERRUPT_FUNCTION8; + let (fnc, arg) = crate::ble::npl::ble_os_adapter_chip_specific::ISR_INTERRUPT_4; - trace!("interrupt BT_BB {:?} {:?}", fnc, arg); + trace!("interrupt BT_MAC {:?} {:?}", fnc, arg); if !fnc.is_null() { + trace!("interrupt BT_MAC call"); + let fnc: fn(*mut binary::c_types::c_void) = core::mem::transmute(fnc); fnc(arg); + trace!("BT_MAC done"); } - trace!("interrupt 8 done"); + trace!("interrupt BT_MAC done"); }; } diff --git a/esp-wifi/src/wifi/os_adapter.rs b/esp-wifi/src/wifi/os_adapter.rs index 6a4d5518411..6a2b6c23947 100644 --- a/esp-wifi/src/wifi/os_adapter.rs +++ b/esp-wifi/src/wifi/os_adapter.rs @@ -861,10 +861,8 @@ pub unsafe extern "C" fn task_get_max_priority() -> i32 { * ****************************************************************************/ #[no_mangle] -pub unsafe extern "C" fn malloc( - size: crate::binary::c_types::c_uint, -) -> *mut crate::binary::c_types::c_void { - crate::compat::malloc::malloc(size as u32) as *mut crate::binary::c_types::c_void +pub unsafe extern "C" fn malloc(size: usize) -> *mut crate::binary::c_types::c_void { + crate::compat::malloc::malloc(size) as *mut crate::binary::c_types::c_void } /**************************************************************************** @@ -906,7 +904,7 @@ pub unsafe extern "C" fn event_post( event_base: *const crate::binary::c_types::c_char, event_id: i32, event_data: *mut crate::binary::c_types::c_void, - event_data_size: size_t, + event_data_size: usize, ticks_to_wait: u32, ) -> i32 { trace!( @@ -1395,7 +1393,7 @@ pub unsafe extern "C" fn nvs_set_blob( _handle: u32, _key: *const crate::binary::c_types::c_char, _value: *const crate::binary::c_types::c_void, - _length: size_t, + _length: usize, ) -> crate::binary::c_types::c_int { todo!("nvs_set_blob") } @@ -1420,7 +1418,7 @@ pub unsafe extern "C" fn nvs_get_blob( _handle: u32, _key: *const crate::binary::c_types::c_char, _out_value: *mut crate::binary::c_types::c_void, - _length: *mut size_t, + _length: *mut usize, ) -> crate::binary::c_types::c_int { todo!("nvs_get_blob") } @@ -1460,7 +1458,7 @@ pub unsafe extern "C" fn nvs_erase_key( * 0 if success or -1 if fail * ****************************************************************************/ -pub unsafe extern "C" fn get_random(buf: *mut u8, len: size_t) -> crate::binary::c_types::c_int { +pub unsafe extern "C" fn get_random(buf: *mut u8, len: usize) -> crate::binary::c_types::c_int { for i in 0..len as usize { buf.add(i) .write_volatile((crate::wifi::rand() & 0xff) as u8) @@ -1588,8 +1586,8 @@ pub unsafe extern "C" fn log_timestamp() -> u32 { * Memory pointer * ****************************************************************************/ -pub unsafe extern "C" fn malloc_internal(size: size_t) -> *mut crate::binary::c_types::c_void { - crate::compat::malloc::malloc(size as u32) as *mut crate::binary::c_types::c_void +pub unsafe extern "C" fn malloc_internal(size: usize) -> *mut crate::binary::c_types::c_void { + crate::compat::malloc::malloc(size) as *mut crate::binary::c_types::c_void } /**************************************************************************** @@ -1608,7 +1606,7 @@ pub unsafe extern "C" fn malloc_internal(size: size_t) -> *mut crate::binary::c_ ****************************************************************************/ pub unsafe extern "C" fn realloc_internal( _ptr: *mut crate::binary::c_types::c_void, - _size: size_t, + _size: usize, ) -> *mut crate::binary::c_types::c_void { todo!("realloc_internal") } @@ -1628,10 +1626,10 @@ pub unsafe extern "C" fn realloc_internal( * ****************************************************************************/ pub unsafe extern "C" fn calloc_internal( - n: size_t, - size: size_t, + n: usize, + size: usize, ) -> *mut crate::binary::c_types::c_void { - calloc(n as u32, size as u32) as *mut crate::binary::c_types::c_void + calloc(n as u32, size) as *mut crate::binary::c_types::c_void } /**************************************************************************** @@ -1647,8 +1645,8 @@ pub unsafe extern "C" fn calloc_internal( * New memory pointer * ****************************************************************************/ -pub unsafe extern "C" fn zalloc_internal(size: size_t) -> *mut crate::binary::c_types::c_void { - calloc(size as u32, 1u32) as *mut crate::binary::c_types::c_void +pub unsafe extern "C" fn zalloc_internal(size: usize) -> *mut crate::binary::c_types::c_void { + calloc(size as u32, 1usize) as *mut crate::binary::c_types::c_void } /**************************************************************************** @@ -1664,8 +1662,8 @@ pub unsafe extern "C" fn zalloc_internal(size: size_t) -> *mut crate::binary::c_ * Memory pointer * ****************************************************************************/ -pub unsafe extern "C" fn wifi_malloc(size: size_t) -> *mut crate::binary::c_types::c_void { - malloc(size as u32) +pub unsafe extern "C" fn wifi_malloc(size: usize) -> *mut crate::binary::c_types::c_void { + malloc(size) } /**************************************************************************** @@ -1684,7 +1682,7 @@ pub unsafe extern "C" fn wifi_malloc(size: size_t) -> *mut crate::binary::c_type ****************************************************************************/ pub unsafe extern "C" fn wifi_realloc( _ptr: *mut crate::binary::c_types::c_void, - _size: size_t, + _size: usize, ) -> *mut crate::binary::c_types::c_void { todo!("wifi_realloc") } @@ -1703,12 +1701,9 @@ pub unsafe extern "C" fn wifi_realloc( * New memory pointer * ****************************************************************************/ -pub unsafe extern "C" fn wifi_calloc( - n: size_t, - size: size_t, -) -> *mut crate::binary::c_types::c_void { +pub unsafe extern "C" fn wifi_calloc(n: usize, size: usize) -> *mut crate::binary::c_types::c_void { trace!("wifi_calloc {} {}", n, size); - calloc(n as u32, size as u32) as *mut crate::binary::c_types::c_void + calloc(n as u32, size) as *mut crate::binary::c_types::c_void } /**************************************************************************** @@ -1724,7 +1719,7 @@ pub unsafe extern "C" fn wifi_calloc( * New memory pointer * ****************************************************************************/ -pub unsafe extern "C" fn wifi_zalloc(size: size_t) -> *mut crate::binary::c_types::c_void { +pub unsafe extern "C" fn wifi_zalloc(size: usize) -> *mut crate::binary::c_types::c_void { wifi_calloc(size, 1) }