Merge pull request #2601 from dhalbert/monotonic_ns-improvement

Monotonic ns improvement; time.sleep rounds to nearest msec
This commit is contained in:
Scott Shawcroft 2020-02-07 11:03:37 -08:00 committed by GitHub
commit 51004f8336
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 7 deletions

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-02-05 15:55-0800\n"
"POT-Creation-Date: 2020-02-07 10:02-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"

View File

@ -29,11 +29,20 @@
#include "shared-bindings/time/__init__.h"
#include "supervisor/shared/tick.h"
#include "tick.h"
inline uint64_t common_hal_time_monotonic() {
inline uint64_t common_hal_time_monotonic(void) {
return supervisor_ticks_ms64();
}
uint64_t common_hal_time_monotonic_ns(void) {
uint64_t ms;
uint32_t us_until_ms;
current_tick(&ms, &us_until_ms);
// us counts down.
return 1000 * (ms * 1000 + (1000 - us_until_ms));
}
void common_hal_time_delay_ms(uint32_t delay) {
mp_hal_delay_ms(delay);
}

View File

@ -24,6 +24,8 @@
* THE SOFTWARE.
*/
#include <sys/time.h>
#include "py/mphal.h"
#include "supervisor/shared/tick.h"
@ -32,6 +34,12 @@ uint64_t common_hal_time_monotonic(void) {
return supervisor_ticks_ms64();
}
uint64_t common_hal_time_monotonic_ns(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return 1000 * ((uint64_t) tv.tv_sec * 1000000 + (uint64_t) tv.tv_usec);
}
void common_hal_time_delay_ms(uint32_t delay) {
mp_hal_delay_ms(delay);
}

View File

@ -30,10 +30,20 @@
#include "supervisor/shared/tick.h"
inline uint64_t common_hal_time_monotonic() {
#include "tick.h"
inline uint64_t common_hal_time_monotonic(void) {
return supervisor_ticks_ms64();
}
uint64_t common_hal_time_monotonic_ns(void) {
uint64_t ms;
uint32_t us_until_ms;
current_tick(&ms, &us_until_ms);
// us counts down.
return 1000 * (ms * 1000 + (1000 - us_until_ms));
}
void common_hal_time_delay_ms(uint32_t delay) {
mp_hal_delay_ms(delay);
}

View File

@ -32,6 +32,14 @@ uint64_t common_hal_time_monotonic(void) {
return supervisor_ticks_ms64();
}
uint64_t common_hal_time_monotonic_ns(void) {
uint64_t ms;
uint32_t us_until_ms;
current_tick(&ms, &us_until_ms);
// us counts down.
return 1000 * (ms * 1000 + (1000 - us_until_ms));
}
void common_hal_time_delay_ms(uint32_t delay) {
mp_hal_delay_ms(delay);
}

View File

@ -32,6 +32,14 @@ uint64_t common_hal_time_monotonic(void) {
return supervisor_ticks_ms64();
}
uint64_t common_hal_time_monotonic_ns(void) {
uint64_t ms;
uint32_t us_until_ms;
current_tick(&ms, &us_until_ms);
// us counts down.
return 1000 * (ms * 1000 + (1000 - us_until_ms));
}
void common_hal_time_delay_ms(uint32_t delay) {
mp_hal_delay_ms(delay);
}

View File

@ -70,14 +70,16 @@ MP_DEFINE_CONST_FUN_OBJ_0(time_monotonic_obj, time_monotonic);
//|
STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
#if MICROPY_PY_BUILTINS_FLOAT
float seconds = mp_obj_get_float(seconds_o);
mp_float_t seconds = mp_obj_get_float(seconds_o);
mp_float_t msecs = 1000.0f * seconds + 0.5f;
#else
int seconds = mp_obj_get_int(seconds_o);
mp_int_t seconds = mp_obj_get_int(seconds_o);
mp_int_t msecs = 1000 * seconds;
#endif
if (seconds < 0) {
mp_raise_ValueError(translate("sleep length must be non-negative"));
}
common_hal_time_delay_ms(1000 * seconds);
common_hal_time_delay_ms(msecs);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
@ -216,7 +218,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
//| :rtype: int
//|
STATIC mp_obj_t time_monotonic_ns(void) {
uint64_t time64 = common_hal_time_monotonic() * 1000000llu;
uint64_t time64 = common_hal_time_monotonic_ns();
return mp_obj_new_int_from_ll((long long) time64);
}
MP_DEFINE_CONST_FUN_OBJ_0(time_monotonic_ns_obj, time_monotonic_ns);

View File

@ -36,6 +36,7 @@ extern mp_obj_t struct_time_from_tm(timeutils_struct_time_t *tm);
extern void struct_time_to_tm(mp_obj_t t, timeutils_struct_time_t *tm);
extern uint64_t common_hal_time_monotonic(void);
extern uint64_t common_hal_time_monotonic_ns(void);
extern void common_hal_time_delay_ms(uint32_t);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_TIME___INIT___H