1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void usleep(int us)
{
    const int cycles = (3 * us) >> 1;
    /* TIMER_FREQ * us / 1000000 = 
     * 24000000 / 16 * us / 1000000 =
     * 1.5 * us = 3 * us / 2
     */
    int timer_start = TIMER2_VALUE;
    while(1)
    {
        int timer_val = TIMER2_VALUE;
        if(timer_val > timer_start)   /* timer wrapped */
            timer_start += (TIMER_FREQ / HZ)
#ifdef HAVE_SCROLLWHEEL
            >> 1
#endif
            ;

        if(timer_start - timer_val >= cycles)
            return;
    }
}