From 59b6dbd74e4e170a95515f5d33f0fc5502d9e154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Tue, 3 Dec 2019 15:56:22 +0100 Subject: [PATCH] [python] micropython_port_interruptible_msleep should always call micropython_port_interrupt_if_needed even if the delay is small --- python/port/helpers.cpp | 19 ++++++++++--------- python/port/helpers.h | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/python/port/helpers.cpp b/python/port/helpers.cpp index bb30687ab..438f7ca47 100644 --- a/python/port/helpers.cpp +++ b/python/port/helpers.cpp @@ -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; } diff --git a/python/port/helpers.h b/python/port/helpers.h index 761f96a7d..b9ab24cb7 100644 --- a/python/port/helpers.h +++ b/python/port/helpers.h @@ -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();