diff --git a/targets/mbed/source/port/jerry_port.c b/targets/mbed/source/port/jerry_port.c index f37830c3d..18c163a05 100644 --- a/targets/mbed/source/port/jerry_port.c +++ b/targets/mbed/source/port/jerry_port.c @@ -76,10 +76,12 @@ jerry_port_get_time_zone (jerry_time_zone_t *tz_p) /**< timezone pointer */ /** * Implementation of jerry_port_get_current_time. * - * @return current timer's counter value in microseconds + * @return current timer's counter value in milliseconds */ double jerry_port_get_current_time (void) { - return (double) us_ticker_read (); + /* Note: if the target has its own RTC, this value should be extended by the + * RTC's one. */ + return (double) us_ticker_read () / 1000; } /* jerry_port_get_current_time */ diff --git a/targets/mbedos5/source/jerry_port_mbed.c b/targets/mbedos5/source/jerry_port_mbed.c index 58289af9e..62b779424 100644 --- a/targets/mbedos5/source/jerry_port_mbed.c +++ b/targets/mbedos5/source/jerry_port_mbed.c @@ -90,10 +90,34 @@ jerry_port_get_time_zone (jerry_time_zone_t *tz_p) /**< timezone pointer */ /** * Implementation of jerry_port_get_current_time. * - * @return current timer's counter value in microseconds + * @return current timer's counter value in milliseconds */ double jerry_port_get_current_time (void) { - return (double) us_ticker_read (); + static uint64_t last_tick = 0; + static time_t last_time = 0; + static uint32_t skew = 0; + + uint64_t curr_tick = us_ticker_read (); /* The value is in microseconds. */ + time_t curr_time = time(NULL); /* The value is in seconds. */ + double result = curr_time * 1000; + + /* The us_ticker_read () has an overflow for each UINT_MAX microseconds + * (~71 mins). For each overflow event the ticker-based clock is about 33 + * milliseconds fast. Without a timer thread the milliseconds part of the + * time can be corrected if the difference of two get_current_time calls + * are within the mentioned 71 mins. Above that interval we can assume + * that the milliseconds part of the time is negligibe. + */ + if (curr_time - last_time > (time_t)(((uint32_t)-1) / 1000000)) { + skew = 0; + } else if (last_tick > curr_tick) { + skew = (skew + 33) % 1000; + } + result += (curr_tick / 1000 - skew) % 1000; + + last_tick = curr_tick; + last_time = curr_time; + return result; } /* jerry_port_get_current_time */