/* * Simple microseconds delay routine, utilizing ARM's DWT * (Data Watchpoint and Trace Unit) and HAL library. * Intended to use with gcc compiler, but I hope it can be used * with any other C compiler across the Universe (provided that * ARM and CMSIS already invented) :) * Max K * * * This file is part of DWT_Delay package. * DWT_Delay is free software: you can redistribute it and/or modify it * under the terms of the MIT License */ // #include "stm32f1xx_hal.h" // change to whatever MCU or Cortex-M core you use #include "dwt_delay.h" #include "at32f403a_407.h" // CMSIS header /** * Initialization routine. * You might need to enable access to DWT registers on Cortex-M7 * DWT->LAR = 0xC5ACCE55 */ void DWT_Init(void) { if (!(CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk)) { CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; DWT->CYCCNT = 0; DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; } } #if DWT_DELAY_NEWBIE /** * If you are a newbie and see magic in DWT_Delay, consider this more * illustrative function, where you explicitly determine a counter * value when delay should stop while keeping things in bounds of uint32. * * @param uint32_t us Number of microseconds to delay for */ void DWT_Delay(uint32_t us) { uint32_t startTick = DWT->CYCCNT, targetTick = DWT->CYCCNT + us * (SystemCoreClock/1000000); // Must check if target tick is out of bounds and overflowed if (targetTick > startTick) { // Not overflowed while (DWT->CYCCNT < targetTick); } else { // Overflowed while (DWT->CYCCNT > startTick || DWT->CYCCNT < targetTick); } } #else /** * Delay routine itself. * Time is in microseconds (1/1000000th of a second), not to be * confused with millisecond (1/1000th). * * No need to check an overflow. Let it just tick :) * * @param uint32_t us Number of microseconds to delay for */ void DWT_Delay(uint32_t us) { uint32_t startTick = DWT->CYCCNT, delayTicks = us * (SystemCoreClock/1000000); while (DWT->CYCCNT - startTick < delayTicks); } #endif