raspberrypi: Make port_idle_until_interrupt work

This needs thorough testing before it's merged, as we tried
and reverted this once before (#5341 and #5356).

I think that besides checking for tinyusb having "something to do",
the fact that `port_interrupt_after_ticks` and `port_disable_tick`
weren't implemented that was causing a secondary problem.

I've tested this on a pico w over reboot-cycles and ctrl-c-cycles,
with and without drive automounting, with and without serial repl open,
and on a power-only connection.

I didn't notice the problem reported in #5356 after merely implementing
port_idle_until_interrupt; but I did notice that sleeps in general would
take over-long until "something" (like writing to the USB drive) happened;
I think "something" was probably calling port_enable_tick(). When this
problem was happening, sleeps would take a lot longer; for instance,
`sleep(.001)` would take about 1/20s and `sleep(.1)` would take about 1/7s.
This commit is contained in:
Jeff Epler 2022-11-12 09:16:23 -06:00
parent f76351d178
commit 78fc43baab
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
2 changed files with 21 additions and 7 deletions

View File

@ -127,6 +127,7 @@ INC += \
-I../shared/timeutils \ -I../shared/timeutils \
-Iboards/$(BOARD) \ -Iboards/$(BOARD) \
-Iboards/ \ -Iboards/ \
-isystem ./../../lib/cmsis/inc \
-isystem sdk/ \ -isystem sdk/ \
-isystem sdk/src/common/pico_base/include/ \ -isystem sdk/src/common/pico_base/include/ \
-isystem sdk/src/common/pico_binary_info/include/ \ -isystem sdk/src/common/pico_binary_info/include/ \

View File

@ -72,6 +72,9 @@
#include "supervisor/serial.h" #include "supervisor/serial.h"
#include "tusb.h"
#include <cmsis_compiler.h>
extern volatile bool mp_msc_enabled; extern volatile bool mp_msc_enabled;
STATIC void _tick_callback(uint alarm_num); STATIC void _tick_callback(uint alarm_num);
@ -241,38 +244,48 @@ uint32_t port_get_saved_word(void) {
return __scratch_x_start__; return __scratch_x_start__;
} }
static volatile bool ticks_enabled;
uint64_t port_get_raw_ticks(uint8_t *subticks) { uint64_t port_get_raw_ticks(uint8_t *subticks) {
uint64_t microseconds = time_us_64(); uint64_t microseconds = time_us_64();
return 1024 * (microseconds / 1000000) + (microseconds % 1000000) / 977; return 1024 * (microseconds / 1000000) + (microseconds % 1000000) / 977;
} }
STATIC void _tick_callback(uint alarm_num) { STATIC void _tick_callback(uint alarm_num) {
supervisor_tick(); if (ticks_enabled) {
hardware_alarm_set_target(0, delayed_by_us(get_absolute_time(), 977)); supervisor_tick();
hardware_alarm_set_target(0, delayed_by_us(get_absolute_time(), 977));
}
} }
// Enable 1/1024 second tick. // Enable 1/1024 second tick.
void port_enable_tick(void) { void port_enable_tick(void) {
ticks_enabled = true;
hardware_alarm_set_target(0, delayed_by_us(get_absolute_time(), 977)); hardware_alarm_set_target(0, delayed_by_us(get_absolute_time(), 977));
} }
// Disable 1/1024 second tick. // Disable 1/1024 second tick.
void port_disable_tick(void) { void port_disable_tick(void) {
// hardware_alarm_cancel(0); // One additional _tick_callback may occur, but it will just return
// whenever !ticks_enabled. Cancel is not called just in case
// it could nuke a timeout set by port_interrupt_after_ticks.
ticks_enabled = false;
} }
// This is called by sleep, we ignore it when our ticks are enabled because // This is called by sleep, we ignore it when our ticks are enabled because
// they'll wake us up earlier. If we don't, we'll mess up ticks by overwriting // they'll wake us up earlier. If we don't, we'll mess up ticks by overwriting
// the next RTC wake up time. // the next RTC wake up time.
void port_interrupt_after_ticks(uint32_t ticks) { void port_interrupt_after_ticks(uint32_t ticks) {
if (!ticks_enabled) {
hardware_alarm_set_target(0, delayed_by_us(get_absolute_time(), ticks * 977));
}
} }
void port_idle_until_interrupt(void) { void port_idle_until_interrupt(void) {
common_hal_mcu_disable_interrupts(); common_hal_mcu_disable_interrupts();
if (!background_callback_pending()) { if (!background_callback_pending() && !tud_task_event_ready()) {
// TODO: Does not work when board is power-cycled. __DSB();
// asm volatile ("dsb 0xF" ::: "memory"); __WFI();
// __wfi();
} }
common_hal_mcu_enable_interrupts(); common_hal_mcu_enable_interrupts();
} }