From f0d952d33ac7b2fb491fc356ce1d399f4460d5e4 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 1 Nov 2019 22:07:51 -0500 Subject: [PATCH] Cortex M3-M7 can use DWT->CYCCNT (#15762) Suggested by Alex Kenis at https://youtu.be/ZfyPcdhQnAA?t=502 --- Marlin/src/HAL/HAL_STM32/HAL.cpp | 2 +- Marlin/src/HAL/shared/Delay.h | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Marlin/src/HAL/HAL_STM32/HAL.cpp b/Marlin/src/HAL/HAL_STM32/HAL.cpp index edc161a5d3..addef3a85d 100644 --- a/Marlin/src/HAL/HAL_STM32/HAL.cpp +++ b/Marlin/src/HAL/HAL_STM32/HAL.cpp @@ -49,7 +49,7 @@ uint16_t HAL_adc_result; // ------------------------ // Needed for DELAY_NS() / DELAY_US() on CORTEX-M7 -#if (defined(__arm__) || defined(__thumb__)) && __CORTEX_M == 7 +#if (defined(__arm__) || defined(__thumb__)) && WITHIN(__CORTEX_M, 3, 7) // HAL pre-initialization task // Force the preinit function to run between the premain() and main() function // of the STM32 arduino core diff --git a/Marlin/src/HAL/shared/Delay.h b/Marlin/src/HAL/shared/Delay.h index b3f8f246fb..4433134291 100644 --- a/Marlin/src/HAL/shared/Delay.h +++ b/Marlin/src/HAL/shared/Delay.h @@ -34,16 +34,17 @@ #if defined(__arm__) || defined(__thumb__) - #if __CORTEX_M == 7 + #if WITHIN(__CORTEX_M, 3, 7) - // Cortex-M7 can use the cycle counter of the DWT unit + // Cortex-M3 through M7 can use the cycle counter of the DWT unit // http://www.anthonyvh.com/2017/05/18/cortex_m-cycle_counter/ FORCE_INLINE static void enableCycleCounter() { CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; - // Unlock DWT. - DWT->LAR = 0xC5ACCE55; + #if __CORTEX_M == 7 + DWT->LAR = 0xC5ACCE55; // Unlock DWT on the M7 + #endif DWT->CYCCNT = 0; DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;