nanospin_ns_to_count()

Updated: May 06, 2022

Convert a time in nanoseconds into a number of iterations

Synopsis:

#include <time.h>

unsigned long nanospin_ns_to_count( 
                 unsigned long nsec );

Arguments:

nsec
The number of nanoseconds that you want to convert.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The nanospin_ns_to_count() function converts the number of nanoseconds specified in nsec into an iteration count suitable for nanospin_count().

Note: The nanospin*() functions are designed for use with hardware that requires short time delays between accesses. You should use them to delay only for times less than a few milliseconds. For longer delays, use the POSIX timer_*() functions.

Returns:

The number of iterations to busy-wait for, or -1 if an error occurred (errno is set).

Errors:

E2BIG
The delay specified by nsec is greater than 500 milliseconds.
ERANGE
The frequency of ClockCycles() is greater than 36 GHz and would lead to issues with 64-bit math for larger spin times.

Examples:

Busy-wait for at least one nanosecond:

#include <time.h>
#include <sys/syspage.h>

unsigned long time = 1;

…
/* Wake up the hardware, then wait for it to be ready. */

unsigned long count = nanospin_ns_to_count( time );
if ( count != -1UL ) {
  nanospin_count( count );
} else {
  /* Check errno and handle error here somehow */
}

/* Use the hardware. */
…

Classification:

QNX Neutrino

Safety:  
Cancellation point No
Interrupt handler Yes
Signal handler Yes
Thread Yes

Caveats:

You should use busy-waiting only when absolutely necessary for accessing hardware.