diff --git a/stmhal/gccollect.c b/stmhal/gccollect.c
index 8a7bbf27feb11300890a6011d3fdb7803f925eef..d7223dedc3f05fc6bf4272594b502c71c97980b0 100644
--- a/stmhal/gccollect.c
+++ b/stmhal/gccollect.c
@@ -39,7 +39,7 @@ mp_uint_t gc_helper_get_regs_and_sp(mp_uint_t *regs);
 void gc_collect(void) {
     // get current time, in case we want to time the GC
     #if 0
-    uint32_t start = sys_tick_get_microseconds();
+    uint32_t start = mp_hal_ticks_us();
     #endif
 
     // start the GC
@@ -66,7 +66,7 @@ void gc_collect(void) {
 
     #if 0
     // print GC info
-    uint32_t ticks = sys_tick_get_microseconds() - start;
+    uint32_t ticks = mp_hal_ticks_us() - start;
     gc_info_t info;
     gc_info(&info);
     printf("GC@%lu %lums\n", start, ticks);
diff --git a/stmhal/modpyb.c b/stmhal/modpyb.c
index 93ae5d40ba4a296191a9c232957de5c15d070e7e..c3c3c475a32dc3aa800c8160a71b22aaba588727 100644
--- a/stmhal/modpyb.c
+++ b/stmhal/modpyb.c
@@ -34,6 +34,7 @@
 #include "py/obj.h"
 #include "py/gc.h"
 #include "py/builtin.h"
+#include "py/mphal.h"
 #include "lib/utils/pyexec.h"
 #include "lib/oofatfs/ff.h"
 #include "lib/oofatfs/diskio.h"
@@ -112,7 +113,7 @@ STATIC mp_obj_t pyb_micros(void) {
     // We want to "cast" the 32 bit unsigned into a small-int.  This means
     // copying the MSB down 1 bit (extending the sign down), which is
     // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
-    return MP_OBJ_NEW_SMALL_INT(sys_tick_get_microseconds());
+    return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_us());
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros);
 
@@ -128,7 +129,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros);
 ///         # Perform some operation
 STATIC mp_obj_t pyb_elapsed_micros(mp_obj_t start) {
     uint32_t startMicros = mp_obj_get_int(start);
-    uint32_t currMicros = sys_tick_get_microseconds();
+    uint32_t currMicros = mp_hal_ticks_us();
     return MP_OBJ_NEW_SMALL_INT((currMicros - startMicros) & 0x3fffffff);
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros);
diff --git a/stmhal/mphalport.h b/stmhal/mphalport.h
index 7204d4bd32a6177d8091960c48ea1848e50939c2..7521024020c508a7be94eb1bdd8929fc242f2e18 100644
--- a/stmhal/mphalport.h
+++ b/stmhal/mphalport.h
@@ -27,13 +27,7 @@ void mp_hal_set_interrupt_char(int c); // -1 to disable
 
 // timing functions
 
-#include "stmhal/systick.h"
-
-#define mp_hal_delay_ms HAL_Delay
-#define mp_hal_delay_us(us) sys_tick_udelay(us)
-#define mp_hal_delay_us_fast(us) sys_tick_udelay(us)
-#define mp_hal_ticks_ms HAL_GetTick
-#define mp_hal_ticks_us() sys_tick_get_microseconds()
+#define mp_hal_delay_us_fast(us) mp_hal_delay_us(us)
 
 extern bool mp_hal_ticks_cpu_enabled;
 void mp_hal_ticks_cpu_enable(void);
diff --git a/stmhal/sdcard.c b/stmhal/sdcard.c
index c93b98b51e0caad6278fc00bd925fc62cad87eb5..73206f4316a2e1ea8a5099390e352393134addc0 100644
--- a/stmhal/sdcard.c
+++ b/stmhal/sdcard.c
@@ -28,9 +28,9 @@
 
 #include "py/nlr.h"
 #include "py/runtime.h"
+#include "py/mphal.h"
 #include "lib/oofatfs/ff.h"
 #include "extmod/vfs_fat.h"
-#include "mphalport.h"
 
 #include "sdcard.h"
 #include "pin.h"
diff --git a/stmhal/stm32_it.c b/stmhal/stm32_it.c
index d8fcc59ce5009b02fe9e982e2249b5693030ef7d..913f1c9315eabde08712b67d2fa0f7da2a8a3360 100644
--- a/stmhal/stm32_it.c
+++ b/stmhal/stm32_it.c
@@ -298,7 +298,7 @@ void SysTick_Handler(void) {
     uwTick += 1;
 
     // Read the systick control regster. This has the side effect of clearing
-    // the COUNTFLAG bit, which makes the logic in sys_tick_get_microseconds
+    // the COUNTFLAG bit, which makes the logic in mp_hal_ticks_us
     // work properly.
     SysTick->CTRL;
 
diff --git a/stmhal/systick.c b/stmhal/systick.c
index ade05d74d752225b96e59b122ad11699f9933382..eb11de9b74dd558f3cd31a7d9a85c37964a593e8 100644
--- a/stmhal/systick.c
+++ b/stmhal/systick.c
@@ -24,20 +24,36 @@
  * THE SOFTWARE.
  */
 
-#include STM32_HAL_H
-
-#include "py/obj.h"
+#include "py/mphal.h"
 #include "irq.h"
 #include "systick.h"
 #include "pybthread.h"
 
-// We provide our own version of HAL_Delay that calls __WFI while waiting, in
-// order to reduce power consumption.
-// Note: Upon entering this function we may or may not have the GIL.
+extern __IO uint32_t uwTick;
+
+// We provide our own version of HAL_Delay that calls __WFI while waiting,
+// and works when interrupts are disabled.  This function is intended to be
+// used only by the ST HAL functions.
 void HAL_Delay(uint32_t Delay) {
     if (query_irq() == IRQ_STATE_ENABLED) {
         // IRQs enabled, so can use systick counter to do the delay
-        extern __IO uint32_t uwTick;
+        uint32_t start = uwTick;
+        // Wraparound of tick is taken care of by 2's complement arithmetic.
+        while (uwTick - start < Delay) {
+            // Enter sleep mode, waiting for (at least) the SysTick interrupt.
+            __WFI();
+        }
+    } else {
+        // IRQs disabled, use mp_hal_delay_ms routine.
+        mp_hal_delay_ms(Delay);
+    }
+}
+
+// Core delay function that does an efficient sleep and may switch thread context.
+// Note: Upon entering this function we may or may not have the GIL.
+void mp_hal_delay_ms(mp_uint_t Delay) {
+    if (query_irq() == IRQ_STATE_ENABLED) {
+        // IRQs enabled, so can use systick counter to do the delay
         uint32_t start = uwTick;
         // Wraparound of tick is taken care of by 2's complement arithmetic.
         while (uwTick - start < Delay) {
@@ -64,11 +80,11 @@ void HAL_Delay(uint32_t Delay) {
 }
 
 // delay for given number of microseconds
-void sys_tick_udelay(uint32_t usec) {
+void mp_hal_delay_us(mp_uint_t usec) {
     if (query_irq() == IRQ_STATE_ENABLED) {
         // IRQs enabled, so can use systick counter to do the delay
-        uint32_t start = sys_tick_get_microseconds();
-        while (sys_tick_get_microseconds() - start < usec) {
+        uint32_t start = mp_hal_ticks_us();
+        while (mp_hal_ticks_us() - start < usec) {
         }
     } else {
         // IRQs disabled, so need to use a busy loop for the delay
@@ -92,11 +108,15 @@ void sys_tick_wait_at_least(uint32_t start_tick, uint32_t delay_ms) {
     }
 }
 
+mp_uint_t mp_hal_ticks_ms(void) {
+    return uwTick;
+}
+
 // The SysTick timer counts down at 168 MHz, so we can use that knowledge
 // to grab a microsecond counter.
 //
 // We assume that HAL_GetTickis returns milliseconds.
-uint32_t sys_tick_get_microseconds(void) {
+mp_uint_t mp_hal_ticks_us(void) {
     mp_uint_t irq_state = disable_irq();
     uint32_t counter = SysTick->VAL;
     uint32_t milliseconds = HAL_GetTick();
diff --git a/stmhal/systick.h b/stmhal/systick.h
index 5a4dfb3ed26d85a8e645ccb270f6c7f0bb58b65f..1e7f623355e7d5e077e4e6f3111d26a8206e8d0d 100644
--- a/stmhal/systick.h
+++ b/stmhal/systick.h
@@ -24,7 +24,5 @@
  * THE SOFTWARE.
  */
 
-void sys_tick_udelay(uint32_t usec);
 void sys_tick_wait_at_least(uint32_t stc, uint32_t delay_ms);
 bool sys_tick_has_passed(uint32_t stc, uint32_t delay_ms);
-uint32_t sys_tick_get_microseconds(void);