Want more features on Pastebin? Sign Up, it's FREE!
Guest

loop based udelay without multiplication

By: a guest on Jun 18th, 2010  |  syntax: C  |  size: 0.71 KB  |  views: 149  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. static inline void udelay(unsigned usecs)
  2. {
  3.     unsigned cycles_per_usec;
  4.     unsigned loops_per_usec;
  5.  
  6.     if (cpu_frequency == CPUFREQ_MAX) {
  7.         cycles_per_usec = (CPUFREQ_MAX + 999999) / 1000000;
  8.     } else {
  9.         cycles_per_usec = (CPUFREQ_NORMAL + 999999) / 1000000;
  10.     }
  11.  
  12.     /*
  13.      * One iteration of the inner loop takes 4 cycles,
  14.      * sustract 1 to account for the outer loop.
  15.      */
  16.     loops_per_usec = (cycles_per_usec+3)/4 - 1;
  17.  
  18.     asm volatile(
  19.         "2: mov  r0, %1      \n"
  20.         "1: subs r0, r0, #1  \n"
  21.         "   bne  1b          \n"
  22.         "   subs %0, %0, #1  \n"
  23.         "   bne  2b          \n"
  24.         : : "r"(usecs), "r"(loops_per_usec) : "r0"
  25.     );
  26. }
clone this paste RAW Paste Data