/* One tick of the 3.576 MHz timer corresponds to 1/3.576/1000 ms, or 0.000279642 ms. Scale the fraction by 65536 (16 bit shift), you get 37532.9217 */ u32 get_raw_ms(void) { u32 t_save; u32 temp; /* read the hardware 32 bit timer running at 3.576 MHz */ read_timer_atomic(t_save); t_save -= gd->prev_timer; /* get "delta time" since last call */ gd->prev_timer += t_save; /* assume we will use all of the counts */ /* * This first while loop is entered for any delta time > about 18.3 ms. The * while loop will execute twice 2.734% of the time, otherwise once. */ while (t_save > 65535) { temp = t_save >> 16; /* extract msb */ temp = ((temp * 37532) + ((temp * 60404) >> 16)) >> 11; /* temp = (temp * 37532) >> 11; */ gd->timer_in_ms += temp; t_save -= temp * 3576; } /* * This second while loop is entered for 94.837% of all possible delta times, * 0 through 0XFFFFFFFF. The second while loop will execute twice 0.037% of * the time, otherwise once. */ while (t_save >= 3576) { temp = (t_save * 37532) >> (16 + 11); if (temp == 0) temp = 1; /* we know that 1 works for sure */ gd->timer_in_ms += temp; t_save -= temp * 3576; } gd->prev_timer -= t_save; /* restore any counts we didn't use this time */ return gd->timer_in_ms; }