The mbed ports used wrong time computations for the (#1642)

'jerry_port_get_current_time' function. Microseconds was used instead of
milliseconds.

A quick fix has been done in the Yotta-based mbed port, and an extended one
- with correction - works in the mbed OS 5 port now.

JerryScript-DCO-1.0-Signed-off-by: Gabor Loki loki@inf.u-szeged.hu
This commit is contained in:
Gabor Loki 2017-03-17 08:35:14 +01:00 committed by László Langó
parent 23ac60915f
commit dd7e20c85d
2 changed files with 30 additions and 4 deletions

View File

@ -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 */

View File

@ -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 */