summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-03-29 16:23:52 +1100
committerDamien George <damien.p.george@gmail.com>2018-03-29 16:23:52 +1100
commitbc3a5f191714f28bef95d9f87c24f7367c90d54a (patch)
treecec24a1ce25ffb1012b40160a930864af8f43b24
parentb833f170c324bd7b9f0c1623d539fb301e7f09a6 (diff)
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.
-rw-r--r--ports/stm32/mphalport.c5
-rw-r--r--ports/stm32/mphalport.h3
2 files changed, 2 insertions, 6 deletions
diff --git a/ports/stm32/mphalport.c b/ports/stm32/mphalport.c
index 74906311e..0108d9026 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 c3d280b40..2843a79cd 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;