You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
858 B
36 lines
858 B
#pragma GCC optimize ("O2") |
|
|
|
#include <stm32f4xx.h> |
|
#include <hal.h> |
|
/* |
|
* Provides a micro-second granular delay using the CPU cycle counter. |
|
*/ |
|
|
|
/* cycles per microsecond */ |
|
uint32_t us_ticks; |
|
|
|
void stopwatch_init(void) |
|
{ |
|
us_ticks = SystemCoreClock / 1000000; |
|
|
|
/* turn on access to the DWT registers */ |
|
DEMCR |= DEMCR_TRCENA; |
|
/* enable the CPU cycle counter */ |
|
DWT_CTRL |= CYCCNTENA; |
|
|
|
stopwatch_reset(); |
|
} |
|
|
|
|
|
void stopwatch_delay_us(uint32_t us){ |
|
// we don't call stopwatch_reset() because any delay() in interrupt will reset main counter. It should be free running |
|
uint32_t ts = stopwatch_getticks(); // start time in ticks |
|
uint32_t dly = us * us_ticks; // delay in ticks |
|
while(1) { |
|
uint32_t now = stopwatch_getticks(); // current time in ticks |
|
uint32_t dt = now - ts; |
|
|
|
if (dt >= dly) |
|
break; |
|
} |
|
}
|
|
|