summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--stmhal/gccollect.c4
-rw-r--r--stmhal/modpyb.c5
-rw-r--r--stmhal/mphalport.h8
-rw-r--r--stmhal/sdcard.c2
-rw-r--r--stmhal/stm32_it.c2
-rw-r--r--stmhal/systick.c42
-rw-r--r--stmhal/systick.h2
7 files changed, 39 insertions, 26 deletions
diff --git a/stmhal/gccollect.c b/stmhal/gccollect.c
index 8a7bbf27fe..d7223dedc3 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 93ae5d40ba..c3c3c475a3 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 7204d4bd32..7521024020 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 c93b98b51e..73206f4316 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 d8fcc59ce5..913f1c9315 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 ade05d74d7..eb11de9b74 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 5a4dfb3ed2..1e7f623355 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);