title: Delay by 1 microsecond in C MIPS Environment
body: <p>I'm trying to port u8glib (graphics library) to MIPS processor, OpenWrt router.
  Here's an <a href="https://code.google.com/p/u8glib/source/browse/sys/arm/lpc1114/common/u8g_arm.c" rel="nofollow">example</a> in arm environment.</p>
  
  <p>As such, one of the routines I must implement is:</p>
  
  <pre><code>delay_micro_seconds(uint32_t us)
  </code></pre>
  
  <p>Since this is a high resolution unit of time, how can I do this reliably in my environment? I've tried the following, but I'm not even sure how to validate it:</p>
  
  <pre><code>nanosleep((struct timespec[]){{0, 1000}}, NULL);
  </code></pre>
  
  <p><strong>How can I validate this approach? If its a  bad approach, how could I reliably delay by 1 microsecond in C?</strong></p>
  
  <p>EDIT: I've tried this, but I'm getting strange output, I expect the difference between the two print s to be 1000*10 iterations = 10,000 , but it is actually closer to 670,000 nanoseconds:</p>
  
  <pre><code>   int main(int argc, char **argv)
     {
      long res, resb;
      struct timespec ts, tsb;
      int i;
  
      res = clock_gettime(CLOCK_REALTIME, &amp;ts);
  
      for(i=0;i&lt;10;i++){
              nanosleep((struct timespec[]){{0,1000}}, NULL);
      }
  
      resb = clock_gettime(CLOCK_REALTIME, &amp;tsb);
  
      if (0 == res) printf("%ld %ld\n", ts.tv_sec, ts.tv_nsec);
      else perror("clock_gettime");
      if (0 == resb) printf("%ld %ld\n", tsb.tv_sec, tsb.tv_nsec);
      else perror("clock_gettime"); //getting 670k delta instead of 10k for tv_nsec
      return 0;
      } 
  </code></pre>