From bc3a5f191714f28bef95d9f87c24f7367c90d54a Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 29 Mar 2018 16:23:52 +1100 Subject: [PATCH] stm32/mphalport: Use MCU regs to detect if cycle counter is started. Instead of using a dedicated variable in RAM it's simpler to use the relevant bits in the DWT register. --- ports/stm32/mphalport.c | 5 +---- ports/stm32/mphalport.h | 3 +-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/ports/stm32/mphalport.c b/ports/stm32/mphalport.c index 74906311eb..0108d90261 100644 --- a/ports/stm32/mphalport.c +++ b/ports/stm32/mphalport.c @@ -7,8 +7,6 @@ #include "usb.h" #include "uart.h" -bool mp_hal_ticks_cpu_enabled = false; - // this table converts from HAL_StatusTypeDef to POSIX errno const byte mp_hal_status_to_errno_table[4] = { [HAL_OK] = 0, @@ -90,7 +88,7 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) { } void mp_hal_ticks_cpu_enable(void) { - if (!mp_hal_ticks_cpu_enabled) { + if (!(DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk)) { CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; #if defined(__CORTEX_M) && __CORTEX_M == 7 // on Cortex-M7 we must unlock the DWT before writing to its registers @@ -98,7 +96,6 @@ void mp_hal_ticks_cpu_enable(void) { #endif DWT->CYCCNT = 0; DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; - mp_hal_ticks_cpu_enabled = true; } } diff --git a/ports/stm32/mphalport.h b/ports/stm32/mphalport.h index c3d280b408..2843a79cd4 100644 --- a/ports/stm32/mphalport.h +++ b/ports/stm32/mphalport.h @@ -15,10 +15,9 @@ void mp_hal_set_interrupt_char(int c); // -1 to disable #define mp_hal_quiet_timing_exit(irq_state) restore_irq_pri(irq_state) #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); static inline mp_uint_t mp_hal_ticks_cpu(void) { - if (!mp_hal_ticks_cpu_enabled) { + if (!(DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk)) { mp_hal_ticks_cpu_enable(); } return DWT->CYCCNT;