-// attempt at high resolution microsecond timer
-// beware: timer counts in 21.3uS increments (1024/48Mhz)
-void SpinDelayUs(int us)
-{
- int ticks = (48*us) >> 10;
-
- // Borrow a PWM unit for my real-time clock
- AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
- // 48 MHz / 1024 gives 46.875 kHz
- AT91C_BASE_PWMC_CH0->PWMC_CMR = PWM_CH_MODE_PRESCALER(10);
- AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0;
- AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff;
-
- WORD start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
-
- for(;;) {
- WORD now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
- if (now == (WORD)(start + ticks))
- return;
-
- WDT_HIT();
- }
-}
-
-void SpinDelay(int ms)
-{
- // convert to uS and call microsecond delay function
- SpinDelayUs(ms*1000);
-}
-