Skip to content

Commit bc3a5f1

Browse files
committed
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.
1 parent b833f17 commit bc3a5f1

2 files changed

Lines changed: 2 additions & 6 deletions

File tree

ports/stm32/mphalport.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#include "usb.h"
88
#include "uart.h"
99

10-
bool mp_hal_ticks_cpu_enabled = false;
11-
1210
// this table converts from HAL_StatusTypeDef to POSIX errno
1311
const byte mp_hal_status_to_errno_table[4] = {
1412
[HAL_OK] = 0,
@@ -90,15 +88,14 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
9088
}
9189

9290
void mp_hal_ticks_cpu_enable(void) {
93-
if (!mp_hal_ticks_cpu_enabled) {
91+
if (!(DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk)) {
9492
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
9593
#if defined(__CORTEX_M) && __CORTEX_M == 7
9694
// on Cortex-M7 we must unlock the DWT before writing to its registers
9795
DWT->LAR = 0xc5acce55;
9896
#endif
9997
DWT->CYCCNT = 0;
10098
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
101-
mp_hal_ticks_cpu_enabled = true;
10299
}
103100
}
104101

ports/stm32/mphalport.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ void mp_hal_set_interrupt_char(int c); // -1 to disable
1515
#define mp_hal_quiet_timing_exit(irq_state) restore_irq_pri(irq_state)
1616
#define mp_hal_delay_us_fast(us) mp_hal_delay_us(us)
1717

18-
extern bool mp_hal_ticks_cpu_enabled;
1918
void mp_hal_ticks_cpu_enable(void);
2019
static inline mp_uint_t mp_hal_ticks_cpu(void) {
21-
if (!mp_hal_ticks_cpu_enabled) {
20+
if (!(DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk)) {
2221
mp_hal_ticks_cpu_enable();
2322
}
2423
return DWT->CYCCNT;

0 commit comments

Comments
 (0)