Skip to content

Commit

Permalink
Misc small changes (esp-rs#312)
Browse files Browse the repository at this point in the history
* Set levels to trace, unify some stuff, implement int disable

* Remove unnecessary paths

* Only check once if random is set, copy impl from esp-hal

* Avoid matching on constant

* Print current time in same line as other arming params

* Simplify tick <-> time conversions

* Work around espflash resolving timer addresses

* Raise compat_timer_setfn level to debug

* Resolve warnings

* Fix malloc pointer mutability

* Simplify import

* Resolve commented question
  • Loading branch information
bugadani authored and bjoernQ committed May 24, 2024
1 parent 730bc21 commit f87c8a8
Show file tree
Hide file tree
Showing 28 changed files with 284 additions and 311 deletions.
29 changes: 4 additions & 25 deletions esp-wifi/src/ble/btdm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@ use critical_section::Mutex;
use crate::ble::btdm::ble_os_adapter_chip_specific::G_OSI_FUNCS;
use crate::ble::HciOutCollector;
use crate::ble::HCI_OUT_COLLECTOR;
use crate::hal::macros::ram;
use crate::{
binary::include::*,
compat::{common::str_from_c, queue::SimpleQueue, work_queue::queue_work},
memory_fence::memory_fence,
timer::yield_task,
};

#[cfg(esp32)]
use esp32_hal as hal;
#[cfg(esp32c3)]
use esp32c3_hal as hal;
#[cfg(esp32s3)]
use esp32s3_hal as hal;

use hal::macros::ram;

#[cfg_attr(esp32c3, path = "os_adapter_esp32c3.rs")]
#[cfg_attr(esp32s3, path = "os_adapter_esp32s3.rs")]
#[cfg_attr(esp32, path = "os_adapter_esp32.rs")]
Expand Down Expand Up @@ -226,9 +218,8 @@ unsafe extern "C" fn queue_recv(queue: *const (), item: *const (), block_time_ms
block_time_ms
);

// is this ticks or millis?
let end_time = crate::timer::get_systimer_count()
+ (block_time_ms as u64 * (crate::timer::TICKS_PER_SECOND / 1000));
let end_time_ticks =
crate::timer::get_systimer_count() + crate::timer::millis_to_ticks(block_time_ms as u64);

// handle the BT_QUEUE
if queue == &BT_INTERNAL_QUEUE as *const _ as *const () {
Expand Down Expand Up @@ -257,7 +248,7 @@ unsafe extern "C" fn queue_recv(queue: *const (), item: *const (), block_time_ms
}

if block_time_ms != OSI_FUNCS_TIME_BLOCKING
&& crate::timer::get_systimer_count() > end_time
&& crate::timer::get_systimer_count() > end_time_ticks
{
trace!("queue_recv returns with timeout");
return -1;
Expand Down Expand Up @@ -338,18 +329,6 @@ 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 usize) as *const ()
}

unsafe extern "C" fn malloc_internal(size: u32) -> *const () {
crate::compat::malloc::malloc(size as usize) as *const ()
}

unsafe extern "C" fn free(ptr: *const ()) {
crate::compat::malloc::free(ptr as *const u8);
}

#[allow(unused)]
#[ram]
unsafe extern "C" fn srand(seed: u32) {
Expand Down
12 changes: 12 additions & 0 deletions esp-wifi/src/ble/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ pub(crate) use ble::send_hci;

pub mod controller;

pub unsafe extern "C" fn malloc(size: u32) -> *mut crate::binary::c_types::c_void {
crate::compat::malloc::malloc(size as usize).cast()
}

pub unsafe extern "C" fn malloc_internal(size: u32) -> *mut crate::binary::c_types::c_void {
crate::compat::malloc::malloc(size as usize).cast()
}

pub unsafe extern "C" fn free(ptr: *mut crate::binary::c_types::c_void) {
crate::compat::malloc::free(ptr.cast())
}

static mut HCI_OUT_COLLECTOR: MaybeUninit<HciOutCollector> = MaybeUninit::uninit();

#[derive(PartialEq, Debug)]
Expand Down
16 changes: 4 additions & 12 deletions esp-wifi/src/ble/npl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ pub struct ext_funcs_t {
>,
esp_intr_free:
Option<unsafe extern "C" fn(ret_handle: *mut *mut crate::binary::c_types::c_void) -> i32>,
malloc: Option<unsafe extern "C" fn(size: u32) -> *const u8>,
free: Option<unsafe extern "C" fn(*const crate::binary::c_types::c_void)>,
malloc: Option<unsafe extern "C" fn(size: u32) -> *mut crate::binary::c_types::c_void>,
free: Option<unsafe extern "C" fn(*mut crate::binary::c_types::c_void)>,
hal_uart_start_tx: Option<unsafe extern "C" fn(i32)>,
hal_uart_init_cbs: Option<
unsafe extern "C" fn(
Expand Down Expand Up @@ -330,8 +330,8 @@ 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(self::malloc),
free: Some(free),
malloc: Some(crate::ble::malloc),
free: Some(crate::ble::free),
hal_uart_start_tx: None,
hal_uart_init_cbs: None,
hal_uart_config: None,
Expand Down Expand Up @@ -417,14 +417,6 @@ unsafe extern "C" fn esp_intr_free(_ret_handle: *mut *mut crate::binary::c_types
todo!();
}

unsafe extern "C" fn malloc(size: u32) -> *const u8 {
crate::compat::malloc::malloc(size as usize)
}

unsafe extern "C" fn free(ptr: *const crate::binary::c_types::c_void) {
crate::compat::malloc::free(ptr as *const u8);
}

#[repr(C)]
pub struct npl_funcs_t {
p_ble_npl_os_started: Option<unsafe extern "C" fn() -> bool>,
Expand Down
76 changes: 32 additions & 44 deletions esp-wifi/src/ble/os_adapter_esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ pub(super) struct osi_funcs_s {
version: u32,
set_isr: Option<unsafe extern "C" fn(i32, unsafe extern "C" fn(), *const ()) -> i32>,
ints_on: Option<unsafe extern "C" fn(u32)>,
interrupt_disable: Option<unsafe extern "C" fn() -> ()>,
interrupt_restore: Option<unsafe extern "C" fn() -> ()>,
task_yield: Option<unsafe extern "C" fn() -> ()>,
task_yield_from_isr: Option<unsafe extern "C" fn() -> ()>,
interrupt_disable: Option<unsafe extern "C" fn()>,
interrupt_restore: Option<unsafe extern "C" fn()>,
task_yield: Option<unsafe extern "C" fn()>,
task_yield_from_isr: Option<unsafe extern "C" fn()>,
semphr_create: Option<unsafe extern "C" fn(u32, u32) -> *const ()>,
semphr_delete: Option<unsafe extern "C" fn(*const ()) -> ()>,
semphr_delete: Option<unsafe extern "C" fn(*const ())>,
semphr_take_from_isr: Option<unsafe extern "C" fn(*const (), *const ()) -> i32>,
semphr_give_from_isr: Option<unsafe extern "C" fn(*const (), *const ()) -> i32>,
semphr_take: Option<unsafe extern "C" fn(*const (), u32) -> i32>,
semphr_give: Option<unsafe extern "C" fn(*const ()) -> i32>,
mutex_create: Option<unsafe extern "C" fn() -> *const ()>,
mutex_delete: Option<unsafe extern "C" fn(*const ()) -> ()>,
mutex_delete: Option<unsafe extern "C" fn(*const ())>,
mutex_lock: Option<unsafe extern "C" fn(*const ()) -> i32>,
mutex_unlock: Option<unsafe extern "C" fn(*const ()) -> i32>,
queue_create: Option<unsafe extern "C" fn(u32, u32) -> *const ()>,
queue_delete: Option<unsafe extern "C" fn(*const ()) -> ()>,
queue_delete: Option<unsafe extern "C" fn(*const ())>,
queue_send: Option<unsafe extern "C" fn(*const (), *const (), u32) -> i32>,
queue_send_from_isr: Option<unsafe extern "C" fn(*const (), *const (), *const ()) -> i32>,
queue_recv: Option<unsafe extern "C" fn(*const (), *const (), u32) -> i32>,
Expand All @@ -61,42 +61,42 @@ pub(super) struct osi_funcs_s {
u32,
) -> i32,
>,
task_delete: Option<unsafe extern "C" fn(*const ()) -> ()>,
task_delete: Option<unsafe extern "C" fn(*const ())>,
is_in_isr: Option<unsafe extern "C" fn() -> i32>,
cause_sw_intr_to_core: Option<unsafe extern "C" fn(i32, i32) -> i32>,
malloc: Option<unsafe extern "C" fn(u32) -> *const ()>,
malloc_internal: Option<unsafe extern "C" fn(u32) -> *const ()>,
free: Option<unsafe extern "C" fn(*const ()) -> ()>,
malloc: Option<unsafe extern "C" fn(u32) -> *mut crate::binary::c_types::c_void>,
malloc_internal: Option<unsafe extern "C" fn(u32) -> *mut crate::binary::c_types::c_void>,
free: Option<unsafe extern "C" fn(*mut crate::binary::c_types::c_void)>,
read_efuse_mac: Option<unsafe extern "C" fn(*const ()) -> i32>,
srand: Option<unsafe extern "C" fn(u32) -> ()>,
srand: Option<unsafe extern "C" fn(u32)>,
rand: Option<unsafe extern "C" fn() -> i32>,
btdm_lpcycles_2_hus: Option<unsafe extern "C" fn(u32, u32) -> u32>,
btdm_hus_2_lpcycles: Option<unsafe extern "C" fn(u32) -> u32>,
btdm_sleep_check_duration: Option<unsafe extern "C" fn(i32) -> i32>,
btdm_sleep_enter_phase1: Option<unsafe extern "C" fn(i32) -> ()>,
btdm_sleep_enter_phase2: Option<unsafe extern "C" fn() -> ()>,
btdm_sleep_exit_phase1: Option<unsafe extern "C" fn() -> ()>,
btdm_sleep_exit_phase2: Option<unsafe extern "C" fn() -> ()>,
btdm_sleep_exit_phase3: Option<unsafe extern "C" fn() -> ()>,
btdm_sleep_enter_phase1: Option<unsafe extern "C" fn(i32)>,
btdm_sleep_enter_phase2: Option<unsafe extern "C" fn()>,
btdm_sleep_exit_phase1: Option<unsafe extern "C" fn()>,
btdm_sleep_exit_phase2: Option<unsafe extern "C" fn()>,
btdm_sleep_exit_phase3: Option<unsafe extern "C" fn()>,
coex_bt_wakeup_request: Option<unsafe extern "C" fn() -> bool>,
coex_bt_wakeup_request_end: Option<unsafe extern "C" fn() -> ()>,
coex_bt_wakeup_request_end: Option<unsafe extern "C" fn()>,
coex_bt_request: Option<unsafe extern "C" fn(u32, u32, u32) -> i32>,
coex_bt_release: Option<unsafe extern "C" fn(u32) -> i32>,
coex_register_bt_cb: Option<unsafe extern "C" fn(unsafe extern "C" fn()) -> i32>,
coex_bb_reset_lock: Option<unsafe extern "C" fn() -> u32>,
coex_bb_reset_unlock: Option<unsafe extern "C" fn(u32)>,
coex_schm_register_btdm_callback: Option<unsafe extern "C" fn(unsafe extern "C" fn()) -> i32>,
coex_schm_status_bit_clear: Option<unsafe extern "C" fn(i32, i32) -> ()>,
coex_schm_status_bit_set: Option<unsafe extern "C" fn(i32, i32) -> ()>,
coex_schm_status_bit_clear: Option<unsafe extern "C" fn(i32, i32)>,
coex_schm_status_bit_set: Option<unsafe extern "C" fn(i32, i32)>,
coex_schm_interval_get: Option<unsafe extern "C" fn() -> u32>,
coex_schm_curr_period_get: Option<unsafe extern "C" fn() -> u8>,
coex_schm_curr_phase_get: Option<unsafe extern "C" fn() -> *const ()>,
coex_wifi_channel_get: Option<unsafe extern "C" fn(*mut u8, *mut u8) -> i32>,
coex_register_wifi_channel_change_callback:
Option<unsafe extern "C" fn(unsafe extern "C" fn()) -> i32>,
set_isr13: Option<unsafe extern "C" fn(i32, unsafe extern "C" fn(), *const ()) -> i32>,
interrupt_l3_disable: Option<unsafe extern "C" fn() -> ()>,
interrupt_l3_restore: Option<unsafe extern "C" fn() -> ()>,
interrupt_l3_disable: Option<unsafe extern "C" fn()>,
interrupt_l3_restore: Option<unsafe extern "C" fn()>,
custom_queue_create:
Option<unsafe extern "C" fn(u32, u32) -> *mut crate::binary::c_types::c_void>,
coex_version_get: Option<
Expand Down Expand Up @@ -137,9 +137,9 @@ pub(super) static G_OSI_FUNCS: osi_funcs_s = osi_funcs_s {
task_delete: Some(task_delete),
is_in_isr: Some(is_in_isr),
cause_sw_intr_to_core: Some(cause_sw_intr_to_core),
malloc: Some(malloc),
malloc_internal: Some(malloc_internal),
free: Some(free),
malloc: Some(crate::ble::malloc),
malloc_internal: Some(crate::ble::malloc_internal),
free: Some(crate::ble::free),
read_efuse_mac: Some(read_efuse_mac),
srand: Some(crate::ble::btdm::srand),
rand: Some(crate::ble::btdm::rand),
Expand Down Expand Up @@ -540,7 +540,7 @@ pub(crate) unsafe extern "C" fn set_isr(n: i32, f: unsafe extern "C" fn(), arg:

pub(crate) unsafe extern "C" fn ints_on(mask: u32) {
trace!("chip_ints_on esp32 {:b}", mask);
hal::xtensa_lx::interrupt::enable_mask(mask);
crate::hal::xtensa_lx::interrupt::enable_mask(mask);
}

#[cfg(coex)]
Expand Down Expand Up @@ -571,15 +571,9 @@ fn async_wakeup_request(event: i32) -> bool {
let mut do_wakeup_request = false;

match event {
BTDM_ASYNC_WAKEUP_REQ_HCI => {
request_lock = true;
}
BTDM_ASYNC_WAKEUP_REQ_COEX => {
request_lock = false;
}
_ => {
return false;
}
e if e == BTDM_ASYNC_WAKEUP_REQ_HCI => request_lock = true,
e if e == BTDM_ASYNC_WAKEUP_REQ_COEX => request_lock = false,
_ => return false,
}

extern "C" {
Expand Down Expand Up @@ -614,15 +608,9 @@ fn async_wakeup_request_end(event: i32) {
let request_lock: bool;

match event {
BTDM_ASYNC_WAKEUP_REQ_HCI => {
request_lock = true;
}
BTDM_ASYNC_WAKEUP_REQ_COEX => {
request_lock = false;
}
_ => {
return;
}
e if e == BTDM_ASYNC_WAKEUP_REQ_HCI => request_lock = true,
e if e == BTDM_ASYNC_WAKEUP_REQ_COEX => request_lock = false,
_ => return,
}

extern "C" {
Expand Down
62 changes: 31 additions & 31 deletions esp-wifi/src/ble/os_adapter_esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ pub(crate) static mut BT_INTERRUPT_FUNCTION8: (
pub(super) struct osi_funcs_s {
magic: u32,
version: u32,
interrupt_set: Option<unsafe extern "C" fn(i32, i32, i32, i32) -> ()>,
interrupt_clear: Option<unsafe extern "C" fn(i32, i32) -> ()>,
interrupt_handler_set: Option<unsafe extern "C" fn(i32, extern "C" fn(), *const ()) -> ()>,
interrupt_disable: Option<unsafe extern "C" fn() -> ()>,
interrupt_enable: Option<unsafe extern "C" fn() -> ()>,
task_yield: Option<unsafe extern "C" fn() -> ()>,
task_yield_from_isr: Option<unsafe extern "C" fn() -> ()>,
interrupt_set: Option<unsafe extern "C" fn(i32, i32, i32, i32)>,
interrupt_clear: Option<unsafe extern "C" fn(i32, i32)>,
interrupt_handler_set: Option<unsafe extern "C" fn(i32, extern "C" fn(), *const ())>,
interrupt_disable: Option<unsafe extern "C" fn()>,
interrupt_enable: Option<unsafe extern "C" fn()>,
task_yield: Option<unsafe extern "C" fn()>,
task_yield_from_isr: Option<unsafe extern "C" fn()>,
semphr_create: Option<unsafe extern "C" fn(u32, u32) -> *const ()>,
semphr_delete: Option<unsafe extern "C" fn(*const ()) -> ()>,
semphr_delete: Option<unsafe extern "C" fn(*const ())>,
semphr_take_from_isr: Option<unsafe extern "C" fn(*const (), *const ()) -> i32>,
semphr_give_from_isr: Option<unsafe extern "C" fn(*const (), *const ()) -> i32>,
semphr_take: Option<unsafe extern "C" fn(*const (), u32) -> i32>,
semphr_give: Option<unsafe extern "C" fn(*const ()) -> i32>,
mutex_create: Option<unsafe extern "C" fn() -> *const ()>,
mutex_delete: Option<unsafe extern "C" fn(*const ()) -> ()>,
mutex_delete: Option<unsafe extern "C" fn(*const ())>,
mutex_lock: Option<unsafe extern "C" fn(*const ()) -> i32>,
mutex_unlock: Option<unsafe extern "C" fn(*const ()) -> i32>,
queue_create: Option<unsafe extern "C" fn(u32, u32) -> *const ()>,
queue_delete: Option<unsafe extern "C" fn(*const ()) -> ()>,
queue_delete: Option<unsafe extern "C" fn(*const ())>,
queue_send: Option<unsafe extern "C" fn(*const (), *const (), u32) -> i32>,
queue_send_from_isr: Option<unsafe extern "C" fn(*const (), *const (), *const ()) -> i32>,
queue_recv: Option<unsafe extern "C" fn(*const (), *const (), u32) -> i32>,
Expand All @@ -49,32 +49,32 @@ pub(super) struct osi_funcs_s {
u32,
) -> i32,
>,
task_delete: Option<unsafe extern "C" fn(*const ()) -> ()>,
task_delete: Option<unsafe extern "C" fn(*const ())>,
is_in_isr: Option<unsafe extern "C" fn() -> i32>,
cause_sw_intr_to_core: Option<unsafe extern "C" fn(i32, i32) -> i32>,
malloc: Option<unsafe extern "C" fn(u32) -> *const ()>,
malloc_internal: Option<unsafe extern "C" fn(u32) -> *const ()>,
free: Option<unsafe extern "C" fn(*const ()) -> ()>,
malloc: Option<unsafe extern "C" fn(u32) -> *mut crate::binary::c_types::c_void>,
malloc_internal: Option<unsafe extern "C" fn(u32) -> *mut crate::binary::c_types::c_void>,
free: Option<unsafe extern "C" fn(*mut crate::binary::c_types::c_void)>,
read_efuse_mac: Option<unsafe extern "C" fn(*const ()) -> i32>,
srand: Option<unsafe extern "C" fn(u32) -> ()>,
srand: Option<unsafe extern "C" fn(u32)>,
rand: Option<unsafe extern "C" fn() -> i32>,
btdm_lpcycles_2_hus: Option<unsafe extern "C" fn(u32, u32) -> u32>,
btdm_hus_2_lpcycles: Option<unsafe extern "C" fn(u32) -> u32>,
btdm_sleep_check_duration: Option<unsafe extern "C" fn(i32) -> i32>,
btdm_sleep_enter_phase1: Option<unsafe extern "C" fn(i32) -> ()>,
btdm_sleep_enter_phase2: Option<unsafe extern "C" fn() -> ()>,
btdm_sleep_exit_phase1: Option<unsafe extern "C" fn() -> ()>,
btdm_sleep_exit_phase2: Option<unsafe extern "C" fn() -> ()>,
btdm_sleep_exit_phase3: Option<unsafe extern "C" fn() -> ()>,
coex_wifi_sleep_set: Option<unsafe extern "C" fn(i32) -> ()>,
btdm_sleep_enter_phase1: Option<unsafe extern "C" fn(i32)>,
btdm_sleep_enter_phase2: Option<unsafe extern "C" fn()>,
btdm_sleep_exit_phase1: Option<unsafe extern "C" fn()>,
btdm_sleep_exit_phase2: Option<unsafe extern "C" fn()>,
btdm_sleep_exit_phase3: Option<unsafe extern "C" fn()>,
coex_wifi_sleep_set: Option<unsafe extern "C" fn(i32)>,
coex_core_ble_conn_dyn_prio_get: Option<unsafe extern "C" fn(*mut i32, *mut i32) -> i32>,
coex_schm_status_bit_set: Option<unsafe extern "C" fn(i32, i32) -> ()>,
coex_schm_status_bit_clear: Option<unsafe extern "C" fn(i32, i32) -> ()>,
interrupt_on: Option<unsafe extern "C" fn(i32) -> ()>,
interrupt_off: Option<unsafe extern "C" fn(i32) -> ()>,
esp_hw_power_down: Option<unsafe extern "C" fn() -> ()>,
esp_hw_power_up: Option<unsafe extern "C" fn() -> ()>,
ets_backup_dma_copy: Option<unsafe extern "C" fn(u32, u32, u32, i32) -> ()>,
coex_schm_status_bit_set: Option<unsafe extern "C" fn(i32, i32)>,
coex_schm_status_bit_clear: Option<unsafe extern "C" fn(i32, i32)>,
interrupt_on: Option<unsafe extern "C" fn(i32)>,
interrupt_off: Option<unsafe extern "C" fn(i32)>,
esp_hw_power_down: Option<unsafe extern "C" fn()>,
esp_hw_power_up: Option<unsafe extern "C" fn()>,
ets_backup_dma_copy: Option<unsafe extern "C" fn(u32, u32, u32, i32)>,
}

pub(super) static G_OSI_FUNCS: osi_funcs_s = osi_funcs_s {
Expand Down Expand Up @@ -107,9 +107,9 @@ pub(super) static G_OSI_FUNCS: osi_funcs_s = osi_funcs_s {
task_delete: Some(task_delete),
is_in_isr: Some(is_in_isr),
cause_sw_intr_to_core: Some(cause_sw_intr_to_core),
malloc: Some(malloc),
malloc_internal: Some(malloc_internal),
free: Some(free),
malloc: Some(crate::ble::malloc),
malloc_internal: Some(crate::ble::malloc_internal),
free: Some(crate::ble::free),
read_efuse_mac: Some(read_efuse_mac),
srand: Some(crate::ble::btdm::srand),
rand: Some(crate::ble::btdm::rand),
Expand Down
Loading

0 comments on commit f87c8a8

Please sign in to comment.