[python] micropython_port_interruptible_msleep should always call micropython_port_interrupt_if_needed even if the delay is small

This commit is contained in:
Émilie Feral
2019-12-03 15:56:22 +01:00
committed by LeaNumworks
parent b36f9416b4
commit 59b6dbd74e
2 changed files with 11 additions and 10 deletions

View File

@@ -22,21 +22,22 @@ bool micropython_port_vm_hook_loop() {
return micropython_port_interrupt_if_needed();
}
bool micropython_port_interruptible_msleep(uint32_t delay) {
bool micropython_port_interruptible_msleep(int32_t delay) {
assert(delay >= 0);
/* We don't use millis because the systick drifts when changing the HCLK
* frequency. */
constexpr uint32_t interruptionCheckDelay = 100;
const uint32_t numberOfInterruptionChecks = delay / interruptionCheckDelay;
uint32_t remainingInterruptionChecks = numberOfInterruptionChecks;
while (remainingInterruptionChecks > 0) {
constexpr int32_t interruptionCheckDelay = 100;
const int32_t numberOfInterruptionChecks = delay / interruptionCheckDelay;
int32_t remainingDelay = delay - numberOfInterruptionChecks * interruptionCheckDelay;
int32_t currentRemainingInterruptionChecks = numberOfInterruptionChecks;
do {
// We assume the time taken by the interruption check is insignificant
if (micropython_port_interrupt_if_needed()) {
return true;
}
remainingInterruptionChecks--;
Ion::Timing::msleep(interruptionCheckDelay);
}
Ion::Timing::msleep(delay - numberOfInterruptionChecks * interruptionCheckDelay);
Ion::Timing::msleep(currentRemainingInterruptionChecks == numberOfInterruptionChecks ? remainingDelay : interruptionCheckDelay);
currentRemainingInterruptionChecks--;
} while (currentRemainingInterruptionChecks >= 0);
return false;
}

View File

@@ -10,7 +10,7 @@ extern "C" {
// These methods return true if they have been interrupted
bool micropython_port_vm_hook_loop();
bool micropython_port_interruptible_msleep(uint32_t delay);
bool micropython_port_interruptible_msleep(int32_t delay);
bool micropython_port_interrupt_if_needed();
int micropython_port_random();