Galileoのmillos()関数の値がおかしい。
調べてみたら、こんな実装。
ソースは Java/hardware/arduino/x86/cores/arduino/UtilTime.cpp
unsigned long millis( void )
{
return micros() / 1000;
}
これでは4294967(2^32/1000)、つまり約1時間11分でループしてしまう。
そこで以下のように書き換え。
unsigned long millis( void )
{
struct timespec t;
t.tv_sec = t.tv_nsec = 0;
clock_gettime(CLOCK_REALTIME, &t);
return (unsigned long)(t.tv_sec)*1000L + t.tv_nsec / 1000000L ;
}
これで32bitフルにカウントされた値が返るようになる。
ちなみにmicros()はこんな実装。それをちょこっと変更しただけ。
unsigned long micros( void )
{
struct timespec t;
t.tv_sec = t.tv_nsec = 0;
/* Considering the system does not suspend CLOCK_REALTIME is welcome.
However, if in the future our system suspend, we need to replace
CLOCK_REALTIME by CLOCK_BOOTTIME and apply the proper patches in
the kernel. */
clock_gettime(CLOCK_REALTIME, &t);
return (unsigned long)(t.tv_sec)*1000000L + t.tv_nsec / 1000L ;
}
スポンサーサイト