The timer will clock at whatever frequency you set.
Use the Prescaler to get down to 1 MHz (1us tick units), say 42-1 if the APB clocking at 42 MHz
Set the TIM to maximal, ie Period = 0xFFFFFFFF or 0xFFFF
It will count through all states 0x00000000 thru 0xFFFFFFFF, and back to 0
void delay_us(uint32_t delay)
{
uint32_t start = TIM2->CNT;
while((TIM2->CNT - start) < delay) { };
}
With a faster clock you can resolve more finely/precisely.
Consider also entry/exit time, vs the edge of the timer you're catching
//******************************************************************************
// Cortex M3 cycle counters in the STM32's trace unit
// From http://forums.arm.com/index.php?showtopic=13949
volatile unsigned int *DWT_CYCCNT = (volatile unsigned int *)0xE0001004; //address of the register
volatile unsigned int *DWT_CONTROL = (volatile unsigned int *)0xE0001000; //address of the register
volatile unsigned int *SCB_DEMCR = (volatile unsigned int *)0xE000EDFC; //address of the register
//******************************************************************************
void CycleCounter_Configuration(void)
{
*SCB_DEMCR |= 0x01000000;
*DWT_CYCCNT = 0; // reset the counter
*DWT_CONTROL |= 1 ; // enable the counter
}
//******************************************************************************
void SleepUSec(unsigned int Delay)
{
unsigned int Current, Start;
Delay = Delay * (SystemCoreClock / 1000000);
Start = *DWT_CYCCNT;
do
{
Current = *DWT_CYCCNT;
}
while((Current - Start) < Delay);
}
//******************************************************************************