Improve mp_hal_delay_ms by implementing subtick resolution.
This commit is contained in:
parent
7b12bc8837
commit
fcf7df857e
2 changed files with 27 additions and 10 deletions
|
|
@ -85,6 +85,13 @@ void supervisor_tick(void) {
|
||||||
background_callback_add(&tick_callback, supervisor_background_tick, NULL);
|
background_callback_add(&tick_callback, supervisor_background_tick, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint64_t supervisor_get_raw_subticks(void) {
|
||||||
|
uint64_t ticks;
|
||||||
|
uint8_t subticks;
|
||||||
|
ticks = port_get_raw_ticks(&subticks);
|
||||||
|
return (ticks << 5) | subticks;
|
||||||
|
}
|
||||||
|
|
||||||
uint64_t supervisor_ticks_ms64() {
|
uint64_t supervisor_ticks_ms64() {
|
||||||
uint64_t result;
|
uint64_t result;
|
||||||
result = port_get_raw_ticks(NULL);
|
result = port_get_raw_ticks(NULL);
|
||||||
|
|
@ -97,26 +104,29 @@ uint32_t supervisor_ticks_ms32() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_hal_delay_ms(mp_uint_t delay_ms) {
|
void mp_hal_delay_ms(mp_uint_t delay_ms) {
|
||||||
uint64_t start_tick = port_get_raw_ticks(NULL);
|
uint64_t start_subtick = supervisor_get_raw_subticks();
|
||||||
// Adjust the delay to ticks vs ms.
|
// Convert delay from ms to subticks
|
||||||
uint64_t delay_ticks = (delay_ms * (uint64_t)1024) / 1000;
|
uint64_t delay_subticks = (delay_ms * (uint64_t)32768) / 1000;
|
||||||
uint64_t end_tick = start_tick + delay_ticks;
|
uint64_t end_subtick = start_subtick + delay_subticks;
|
||||||
int64_t remaining = delay_ticks;
|
int64_t remaining = delay_subticks;
|
||||||
|
|
||||||
// Loop until we've waited long enough or we've been CTRL-Ced by autoreload
|
// Loop until we've waited long enough or we've been CTRL-Ced by autoreload
|
||||||
// or the user.
|
// or the user.
|
||||||
while (remaining > 0 && !mp_hal_is_interrupted()) {
|
while (remaining > 0 && !mp_hal_is_interrupted()) {
|
||||||
RUN_BACKGROUND_TASKS;
|
RUN_BACKGROUND_TASKS;
|
||||||
remaining = end_tick - port_get_raw_ticks(NULL);
|
remaining = end_subtick - supervisor_get_raw_subticks();
|
||||||
// We break a bit early so we don't risk setting the alarm before the time when we call
|
// We break a bit early so we don't risk setting the alarm before the time when we call
|
||||||
// sleep.
|
// sleep.
|
||||||
if (remaining < 1) {
|
if (remaining < 1) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
port_interrupt_after_ticks(remaining);
|
uint32_t remaining_ticks = remaining / 32;
|
||||||
|
if (remaining_ticks > 0) {
|
||||||
|
port_interrupt_after_ticks(remaining_ticks);
|
||||||
// Idle until an interrupt happens.
|
// Idle until an interrupt happens.
|
||||||
port_idle_until_interrupt();
|
port_idle_until_interrupt();
|
||||||
remaining = end_tick - port_get_raw_ticks(NULL);
|
}
|
||||||
|
remaining = end_subtick - supervisor_get_raw_subticks();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,13 @@ extern uint32_t supervisor_ticks_ms32(void);
|
||||||
*/
|
*/
|
||||||
extern uint64_t supervisor_ticks_ms64(void);
|
extern uint64_t supervisor_ticks_ms64(void);
|
||||||
|
|
||||||
|
/** @brief Get 64-bit current time in subticks
|
||||||
|
*
|
||||||
|
* Time is returned as a 64-bit count of subticks where each subtick is 1/32768
|
||||||
|
* of a second.
|
||||||
|
*/
|
||||||
|
extern uint64_t supervisor_get_raw_subticks(void);
|
||||||
|
|
||||||
extern void supervisor_enable_tick(void);
|
extern void supervisor_enable_tick(void);
|
||||||
extern void supervisor_disable_tick(void);
|
extern void supervisor_disable_tick(void);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue