I am trying to port some software/library from arduino to linux/raspberry pi. One of the things I can't do is a system tick that is unaffected by any change of system clock (manual change or day light timing or anything).
I've found code here but it doesn't compile on my Debian "undefined reference".
unsigned long millis(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts );
return ( ts.tv_sec * 1000 + ts.tv_nsec / 1000000L );
}
unsigned long micros(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts );
return ( ts.tv_sec * 1000000L + ts.tv_nsec / 1000L );
}
I did man page on my system and got a page that describes how to use this function but I don't seem to be able to compile the above code without error. The time.h is riddled with #ifdef and here is the relevant part:
# ifdef __USE_POSIX199309
/* Pause execution for a number of nanoseconds.
This function is a cancellation point and therefore not marked with
__THROW. */
extern int nanosleep (__const struct timespec *__requested_time,
struct timespec *__remaining);
/* Get resolution of clock CLOCK_ID. */
extern int clock_getres (clockid_t __clock_id, struct timespec *__res) __THROW;
/* Get current value of clock CLOCK_ID and store it in TP. */
extern int clock_gettime (clockid_t __clock_id, struct timespec *__tp) __THROW;
/* Set clock CLOCK_ID to value TP. */
extern int clock_settime (clockid_t __clock_id, __const struct timespec *__tp)
__THROW;
# ifdef __USE_XOPEN2K
/* High-resolution sleep with the specified clock.
This function is a cancellation point and therefore not marked with
__THROW. */
extern int clock_nanosleep (clockid_t __clock_id, int __flags,
__const struct timespec *__req,
struct timespec *__rem);
/* Return clock ID for CPU-time clock. */
extern int clock_getcpuclockid (pid_t __pid, clockid_t *__clock_id) __THROW;
# endif
/* Create new per-process timer using CLOCK_ID. */
extern int timer_create (clockid_t __clock_id,
struct sigevent *__restrict __evp,
timer_t *__restrict __timerid) __THROW;
/* Delete timer TIMERID. */
extern int timer_delete (timer_t __timerid) __THROW;
/* Set timer TIMERID to VALUE, returning old value in OVLAUE. */
extern int timer_settime (timer_t __timerid, int __flags,
__const struct itimerspec *__restrict __value,
struct itimerspec *__restrict __ovalue) __THROW;
/* Get current value of timer TIMERID and store it in VLAUE. */
extern int timer_gettime (timer_t __timerid, struct itimerspec *__value)
__THROW;
/* Get expiration overrun for timer TIMERID. */
extern int timer_getoverrun (timer_t __timerid) __THROW;
# endif
So if I don't define __USE_POSIX199309 I don't even get these functions. So I added the define in my compiler option -D__USE_POSIX199309. This portion is displayed in regular color coding by eclipse after I added the option. Still same error. Any insight?