Skip to content

Commit 2b906e5

Browse files
bugadaniCopilot
andauthored
preempt - Abstract away blob ticks (#4063)
* Fix sleep * Remove unused timeout from mutex_lock * Abstract away blob ticks * Update esp-radio/src/time.rs Co-authored-by: Copilot <[email protected]> * Don't sleep for 10s --------- Co-authored-by: Copilot <[email protected]>
1 parent 044e042 commit 2b906e5

File tree

7 files changed

+81
-77
lines changed

7 files changed

+81
-77
lines changed

esp-radio/src/ble/npl.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use super::*;
1010
use crate::{
1111
binary::{c_types::*, include::*},
1212
compat::{self, OSI_FUNCS_TIME_BLOCKING, common::str_from_c, queue},
13+
time::{blob_ticks_to_micros, blob_ticks_to_millis, millis_to_blob_ticks},
1314
};
1415

1516
#[cfg_attr(esp32c2, path = "os_adapter_esp32c2.rs")]
@@ -633,36 +634,27 @@ unsafe extern "C" fn ble_npl_hw_set_isr(_no: i32, _mask: u32) {
633634
todo!()
634635
}
635636

636-
// NPL counts in milliseconds. Let's not lose range by using micros for our tick rate.
637-
const fn npl_time_to_ms(time: ble_npl_time_t) -> u32 {
638-
time
639-
}
640-
641-
const fn npl_ms_to_time(ms: u32) -> ble_npl_time_t {
642-
ms
643-
}
644-
645637
unsafe extern "C" fn ble_npl_time_delay(time: ble_npl_time_t) {
646-
let time = npl_time_to_ms(time);
647-
crate::preempt::usleep(time * 1000);
638+
let time = blob_ticks_to_micros(time);
639+
crate::preempt::usleep(time);
648640
}
649641

650642
unsafe extern "C" fn ble_npl_time_ticks_to_ms32(time: ble_npl_time_t) -> u32 {
651643
trace!("ble_npl_time_ticks_to_ms32 {}", time);
652-
npl_time_to_ms(time)
644+
blob_ticks_to_millis(time)
653645
}
654646

655647
unsafe extern "C" fn ble_npl_time_ms_to_ticks32(ms: u32) -> ble_npl_time_t {
656648
trace!("ble_npl_time_ms_to_ticks32 {}", ms);
657-
npl_ms_to_time(ms)
649+
millis_to_blob_ticks(ms)
658650
}
659651

660652
unsafe extern "C" fn ble_npl_time_ticks_to_ms(
661653
time: ble_npl_time_t,
662654
p_ms: *mut u32,
663655
) -> ble_npl_error_t {
664656
trace!("ble_npl_time_ticks_to_ms {}", time);
665-
unsafe { *p_ms = npl_time_to_ms(time) };
657+
unsafe { *p_ms = blob_ticks_to_millis(time) };
666658
0
667659
}
668660

@@ -671,7 +663,7 @@ unsafe extern "C" fn ble_npl_time_ms_to_ticks(
671663
p_time: *mut ble_npl_time_t,
672664
) -> ble_npl_error_t {
673665
trace!("ble_npl_time_ms_to_ticks {}", ms);
674-
unsafe { *p_time = npl_ms_to_time(ms) };
666+
unsafe { *p_time = millis_to_blob_ticks(ms) };
675667
0
676668
}
677669

@@ -724,7 +716,7 @@ unsafe extern "C" fn ble_npl_callout_stop(callout: *const ble_npl_callout) {
724716
unsafe {
725717
let co = (*callout).dummy as *mut Callout;
726718
// stop timer
727-
compat::timer_compat::compat_timer_disarm(addr_of_mut!((*co).timer_handle));
719+
compat::timer_compat::compat_timer_disarm(&raw mut (*co).timer_handle);
728720
}
729721
}
730722

@@ -737,7 +729,11 @@ unsafe extern "C" fn ble_npl_callout_reset(
737729
let co = unsafe { (*callout).dummy } as *mut Callout;
738730
unsafe {
739731
// start timer
740-
compat::timer_compat::compat_timer_arm(addr_of_mut!((*co).timer_handle), time, false);
732+
compat::timer_compat::compat_timer_arm(
733+
&raw mut (*co).timer_handle,
734+
blob_ticks_to_millis(time),
735+
false,
736+
);
741737
}
742738
0
743739
}
@@ -942,7 +938,12 @@ unsafe extern "C" fn ble_npl_eventq_get(
942938
let mut evt = core::ptr::null_mut::<ble_npl_event>();
943939
let wrapper = unwrap!(unsafe { queue.as_mut() }, "queue wrapper is null");
944940

945-
if queue::queue_receive(wrapper.dummy as _, (&raw mut evt).cast(), timeout) == 1 {
941+
if queue::queue_receive(
942+
wrapper.dummy as _,
943+
(&raw mut evt).cast(),
944+
blob_ticks_to_micros(timeout),
945+
) == 1
946+
{
946947
trace!("got {:x}", evt as usize);
947948
unsafe {
948949
let evt = (*evt).dummy as *mut Event;
@@ -1227,7 +1228,7 @@ pub(crate) fn ble_init() {
12271228

12281229
// this is to avoid (ASSERT r_ble_hci_ram_hs_cmd_tx:34 0 0)
12291230
// we wait a bit to make sure the ble task initialized everything
1230-
crate::compat::common::sleep(10);
1231+
crate::preempt::usleep(10_000);
12311232
}
12321233

12331234
// At some point the "High-speed ADC" entropy source became available.
@@ -1245,7 +1246,7 @@ pub(crate) fn ble_deinit() {
12451246
// Prevent ASSERT r_ble_ll_reset:1069 ... ...
12461247
// TODO: the cause of the issue is that the BLE controller can be dropped while the driver
12471248
// is in the process of handling a HCI command.
1248-
crate::compat::common::sleep(10);
1249+
crate::preempt::usleep(10_000);
12491250
// HCI deinit
12501251
npl::r_ble_hci_trans_cfg_hs(None, core::ptr::null(), None, core::ptr::null());
12511252

esp-radio/src/common_adapter/mod.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ use portable_atomic::{AtomicU32, Ordering};
1616
use crate::{
1717
binary::{
1818
c_types::{c_int, c_ulong, c_void},
19-
include::{esp_event_base_t, esp_timer_get_time},
19+
include::esp_event_base_t,
2020
},
2121
compat::{common::*, semaphore::*},
2222
hal::{self, clock::ModemClockController, ram},
23+
time::blob_ticks_to_micros,
2324
};
2425

2526
#[cfg_attr(esp32c3, path = "common_adapter_esp32c3.rs")]
@@ -121,7 +122,7 @@ pub unsafe extern "C" fn semphr_delete(semphr: *mut c_void) {
121122
#[ram]
122123
pub unsafe extern "C" fn semphr_take(semphr: *mut c_void, tick: u32) -> i32 {
123124
trace!(">>>> semphr_take {:?} block_time_tick {}", semphr, tick);
124-
sem_take(semphr, tick)
125+
sem_take(semphr, blob_ticks_to_micros(tick))
125126
}
126127

127128
#[ram]
@@ -198,9 +199,11 @@ pub unsafe extern "C" fn read_mac(mac: *mut u8, type_: u32) -> c_int {
198199
}
199200
}
200201

202+
const ESP_MAC_WIFI_SOFTAP: u32 = 1;
203+
const ESP_MAC_BT: u32 = 2;
204+
201205
unsafe {
202-
// ESP_MAC_WIFI_SOFTAP
203-
if type_ == 1 {
206+
if type_ == ESP_MAC_WIFI_SOFTAP {
204207
let tmp = mac.offset(0).read_volatile();
205208
for i in 0..64 {
206209
mac.offset(0).write_volatile(tmp | 0x02);
@@ -211,10 +214,7 @@ pub unsafe extern "C" fn read_mac(mac: *mut u8, type_: u32) -> c_int {
211214
break;
212215
}
213216
}
214-
}
215-
216-
// ESP_MAC_BT
217-
if type_ == 2 {
217+
} else if type_ == ESP_MAC_BT {
218218
let tmp = mac.offset(0).read_volatile();
219219
for i in 0..64 {
220220
mac.offset(0).write_volatile(tmp | 0x02);
@@ -228,6 +228,8 @@ pub unsafe extern "C" fn read_mac(mac: *mut u8, type_: u32) -> c_int {
228228

229229
mac.offset(5)
230230
.write_volatile(mac.offset(5).read_volatile() + 1);
231+
} else {
232+
return -1;
231233
}
232234
}
233235

@@ -273,20 +275,20 @@ pub unsafe extern "C" fn ets_timer_setfn(
273275
}
274276

275277
#[cfg(feature = "wifi")]
276-
pub unsafe extern "C" fn ets_timer_arm(timer: *mut c_void, tmout: u32, repeat: bool) {
277-
crate::compat::timer_compat::compat_timer_arm(timer.cast(), tmout, repeat);
278+
pub unsafe extern "C" fn ets_timer_arm(timer: *mut c_void, ms: u32, repeat: bool) {
279+
crate::compat::timer_compat::compat_timer_arm(timer.cast(), ms, repeat);
278280
}
279281

280282
#[cfg(feature = "wifi")]
281-
pub unsafe extern "C" fn ets_timer_arm_us(timer: *mut c_void, tmout: u32, repeat: bool) {
282-
crate::compat::timer_compat::compat_timer_arm_us(timer.cast(), tmout, repeat);
283+
pub unsafe extern "C" fn ets_timer_arm_us(timer: *mut c_void, us: u32, repeat: bool) {
284+
crate::compat::timer_compat::compat_timer_arm_us(timer.cast(), us, repeat);
283285
}
284286

285287
#[unsafe(no_mangle)]
286288
pub unsafe extern "C" fn __esp_radio_gettimeofday(tv: *mut timeval, _tz: *mut ()) -> i32 {
287289
if !tv.is_null() {
288290
unsafe {
289-
let microseconds = esp_timer_get_time();
291+
let microseconds = __esp_radio_esp_timer_get_time();
290292
(*tv).tv_sec = (microseconds / 1_000_000) as u64;
291293
(*tv).tv_usec = (microseconds % 1_000_000) as u32;
292294
}
@@ -312,9 +314,10 @@ pub unsafe extern "C" fn __esp_radio_esp_timer_get_time() -> i64 {
312314
// will not need to have a scheduler in their firmware.
313315
cfg_if::cfg_if! {
314316
if #[cfg(any(feature = "wifi", feature = "ble"))] {
315-
crate::time::ticks_to_micros(crate::preempt::now()) as i64
317+
crate::preempt::now() as i64
316318
} else {
317-
unreachable!()
319+
// In this case we don't have a scheduler, we can return esp-hal's timestamp.
320+
esp_hal::time::Instant::now().duration_since_epoch().as_micros() as i64
318321
}
319322
}
320323
}
@@ -492,7 +495,11 @@ pub unsafe extern "C" fn queue_send(
492495
item: *mut c_void,
493496
block_time_tick: u32,
494497
) -> i32 {
495-
crate::compat::queue::queue_send_to_back(queue.cast(), item.cast_const(), block_time_tick)
498+
crate::compat::queue::queue_send_to_back(
499+
queue.cast(),
500+
item.cast_const(),
501+
blob_ticks_to_micros(block_time_tick),
502+
)
496503
}
497504

498505
/// **************************************************************************
@@ -539,7 +546,11 @@ pub unsafe extern "C" fn queue_send_to_back(
539546
item: *mut c_void,
540547
block_time_tick: u32,
541548
) -> i32 {
542-
crate::compat::queue::queue_send_to_back(queue.cast(), item, block_time_tick)
549+
crate::compat::queue::queue_send_to_back(
550+
queue.cast(),
551+
item,
552+
blob_ticks_to_micros(block_time_tick),
553+
)
543554
}
544555

545556
/// **************************************************************************
@@ -562,7 +573,11 @@ pub unsafe extern "C" fn queue_send_to_front(
562573
item: *mut c_void,
563574
block_time_tick: u32,
564575
) -> i32 {
565-
crate::compat::queue::queue_send_to_front(queue.cast(), item, block_time_tick)
576+
crate::compat::queue::queue_send_to_front(
577+
queue.cast(),
578+
item,
579+
blob_ticks_to_micros(block_time_tick),
580+
)
566581
}
567582

568583
/// **************************************************************************
@@ -583,9 +598,9 @@ pub unsafe extern "C" fn queue_send_to_front(
583598
pub unsafe extern "C" fn queue_recv(
584599
queue: *mut c_void,
585600
item: *mut c_void,
586-
block_time_tick: u32,
601+
block_time_ms: u32,
587602
) -> i32 {
588-
crate::compat::queue::queue_receive(queue.cast(), item, block_time_tick)
603+
crate::compat::queue::queue_receive(queue.cast(), item, blob_ticks_to_micros(block_time_ms))
589604
}
590605

591606
pub unsafe extern "C" fn queue_recv_from_isr(

esp-radio/src/compat/common.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use esp_wifi_sys::{c_types::c_char, include::malloc};
1515
use super::{OSI_FUNCS_TIME_BLOCKING, malloc::free};
1616
use crate::{
1717
ESP_RADIO_LOCK,
18-
binary::c_types::{c_int, c_void},
18+
binary::c_types::{c_int, c_uint, c_void},
1919
memory_fence::memory_fence,
2020
preempt::{current_task, yield_task},
2121
};
@@ -44,14 +44,10 @@ pub(crate) fn thread_sem_get() -> *mut c_void {
4444
/// Implementation of sleep() from newlib in esp-idf.
4545
/// components/newlib/time.c
4646
#[unsafe(no_mangle)]
47-
pub(crate) unsafe extern "C" fn sleep(
48-
millis: crate::binary::c_types::c_uint,
49-
) -> crate::binary::c_types::c_uint {
47+
pub(crate) unsafe extern "C" fn sleep(seconds: c_uint) -> c_uint {
5048
trace!("sleep");
5149

52-
unsafe {
53-
usleep(millis * 1_000);
54-
}
50+
unsafe { usleep(seconds * 1_000_000) };
5551
0
5652
}
5753

esp-radio/src/compat/mutex.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use esp_wifi_sys::c_types::c_void;
22

3-
use crate::{
4-
compat::OSI_FUNCS_TIME_BLOCKING,
5-
preempt::mutex::{MutexHandle, MutexPtr},
6-
};
3+
use crate::preempt::mutex::{MutexHandle, MutexPtr};
74

85
pub(crate) fn mutex_create(recursive: bool) -> *mut c_void {
96
MutexHandle::new(recursive).leak().as_ptr().cast()
@@ -16,18 +13,12 @@ pub(crate) fn mutex_delete(mutex: *mut c_void) {
1613
core::mem::drop(handle);
1714
}
1815

19-
pub(crate) fn mutex_lock(mutex: *mut c_void, tick: u32) -> i32 {
16+
pub(crate) fn mutex_lock(mutex: *mut c_void) -> i32 {
2017
let ptr = unwrap!(MutexPtr::new(mutex.cast()), "mutex is null");
2118

2219
let handle = unsafe { MutexHandle::ref_from_ptr(&ptr) };
23-
// Assuming `tick` is in microseconds
24-
let timeout = if tick == OSI_FUNCS_TIME_BLOCKING {
25-
None
26-
} else {
27-
Some(tick)
28-
};
29-
30-
handle.lock(timeout) as i32
20+
21+
handle.lock(None) as i32
3122
}
3223

3324
pub(crate) fn mutex_unlock(mutex: *mut c_void) -> i32 {

esp-radio/src/compat/timer_compat.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ use crate::{
88
};
99

1010
pub(crate) fn compat_timer_arm(ets_timer: *mut ets_timer, tmout_ms: u32, repeat: bool) {
11-
compat_timer_arm_us(ets_timer, tmout_ms * 1000, repeat);
11+
compat_timer_arm_us(ets_timer, tmout_ms.saturating_mul(1000), repeat);
1212
}
1313

1414
pub(crate) fn compat_timer_arm_us(ets_timer: *mut ets_timer, us: u32, repeat: bool) {
1515
trace!(
16-
"timer_arm_us {:x} current: {} ticks: {} repeat: {}",
16+
"timer_arm_us {:x} current: {} micros: {} repeat: {}",
1717
ets_timer as usize,
1818
crate::preempt::now(),
19-
crate::time::micros_to_ticks(us as u64),
19+
us,
2020
repeat
2121
);
2222

esp-radio/src/time.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
//! Time conversions
2+
//!
3+
//! We're using 1ms per tick, to offer a decent-ish timeout range on u32.
4+
25
#![allow(unused)]
3-
pub const TICKS_PER_SECOND: u64 = 1_000_000;
46

5-
pub(crate) fn micros_to_ticks(us: u64) -> u64 {
6-
us * (TICKS_PER_SECOND / 1_000_000)
7+
pub(crate) const fn blob_ticks_to_micros(ticks: u32) -> u32 {
8+
ticks.saturating_mul(1_000)
79
}
810

9-
pub(crate) fn millis_to_ticks(ms: u64) -> u64 {
10-
ms * (TICKS_PER_SECOND / 1_000)
11+
pub(crate) const fn micros_to_blob_ticks(micros: u32) -> u32 {
12+
micros / 1_000
1113
}
1214

13-
pub(crate) fn ticks_to_micros(ticks: u64) -> u64 {
14-
ticks / (TICKS_PER_SECOND / 1_000_000)
15+
pub(crate) const fn blob_ticks_to_millis(ticks: u32) -> u32 {
16+
ticks
1517
}
1618

17-
pub(crate) fn ticks_to_millis(ticks: u64) -> u64 {
18-
ticks / (TICKS_PER_SECOND / 1_000)
19+
pub(crate) const fn millis_to_blob_ticks(millis: u32) -> u32 {
20+
millis
1921
}

0 commit comments

Comments
 (0)