summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2020-08-01 23:50:23 +1000
committerDamien George <damien@micropython.org>2020-08-22 16:13:44 +1000
commitee50a6effebf315c38d4a129dc9f65ee722eb5b6 (patch)
tree16cfee569848d9d75fbb8eeacf39466f5b2b7319
parentbadd351150df70bea6644db98f48fbc3a1e5ffea (diff)
py/mphal.h: Introduce mp_hal_time_ns and implement on various ports.
This should return a 64-bit value being the number of nanoseconds since 1970/1/1. Signed-off-by: Damien George <damien@micropython.org>
-rw-r--r--ports/esp32/mphalport.c7
-rw-r--r--ports/esp8266/esp_mphal.c4
-rw-r--r--ports/stm32/rtc.c18
-rw-r--r--ports/unix/unix_mphal.c5
-rw-r--r--ports/zephyr/mphalport.h5
-rw-r--r--py/mphal.h6
6 files changed, 45 insertions, 0 deletions
diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c
index 99548ad62..5e526df66 100644
--- a/ports/esp32/mphalport.c
+++ b/ports/esp32/mphalport.c
@@ -28,6 +28,7 @@
#include <stdio.h>
#include <string.h>
+#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
@@ -195,6 +196,12 @@ void mp_hal_delay_us(uint32_t us) {
}
}
+uint64_t mp_hal_time_ns(void) {
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL;
+}
+
// Wake up the main task if it is sleeping
void mp_hal_wake_main_task_from_isr(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
diff --git a/ports/esp8266/esp_mphal.c b/ports/esp8266/esp_mphal.c
index c467ab72d..9e1b72e43 100644
--- a/ports/esp8266/esp_mphal.c
+++ b/ports/esp8266/esp_mphal.c
@@ -138,6 +138,10 @@ void MP_FASTCODE(mp_hal_delay_ms)(uint32_t delay) {
mp_hal_delay_us(delay * 1000);
}
+uint64_t mp_hal_time_ns(void) {
+ return pyb_rtc_get_us_since_2000() * 1000ULL;
+}
+
void ets_event_poll(void) {
ets_loop_iter();
mp_handle_pending(true);
diff --git a/ports/stm32/rtc.c b/ports/stm32/rtc.c
index de2635200..4f759c4bc 100644
--- a/ports/stm32/rtc.c
+++ b/ports/stm32/rtc.c
@@ -27,6 +27,7 @@
#include <stdio.h>
#include "py/runtime.h"
+#include "lib/timeutils/timeutils.h"
#include "extint.h"
#include "rtc.h"
#include "irq.h"
@@ -442,6 +443,23 @@ STATIC void RTC_CalendarConfig(void) {
}
}
+uint64_t mp_hal_time_ns(void) {
+ uint64_t ns = 0;
+ #if MICROPY_HW_ENABLE_RTC
+ // Get current according to the RTC.
+ rtc_init_finalise();
+ RTC_TimeTypeDef time;
+ RTC_DateTypeDef date;
+ HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN);
+ HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN);
+ ns = timeutils_nanoseconds_since_1970(
+ 2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds);
+ uint32_t usec = ((RTC_SYNCH_PREDIV - time.SubSeconds) * (1000000 / 64)) / ((RTC_SYNCH_PREDIV + 1) / 64);
+ ns += usec * 1000;
+ #endif
+ return ns;
+}
+
/******************************************************************************/
// MicroPython bindings
diff --git a/ports/unix/unix_mphal.c b/ports/unix/unix_mphal.c
index 42c22f7b5..f43067f64 100644
--- a/ports/unix/unix_mphal.c
+++ b/ports/unix/unix_mphal.c
@@ -214,3 +214,8 @@ mp_uint_t mp_hal_ticks_us(void) {
return tv.tv_sec * 1000000 + tv.tv_usec;
#endif
}
+
+uint64_t mp_hal_time_ns(void) {
+ time_t now = time(NULL);
+ return (uint64_t)now * 1000000000ULL;
+}
diff --git a/ports/zephyr/mphalport.h b/ports/zephyr/mphalport.h
index 8434a388b..9ef213ddb 100644
--- a/ports/zephyr/mphalport.h
+++ b/ports/zephyr/mphalport.h
@@ -24,6 +24,11 @@ static inline void mp_hal_delay_ms(mp_uint_t delay) {
k_msleep(delay);
}
+static inline uint64_t mp_hal_time_ns(void) {
+ // Not currently implemented.
+ return 0;
+}
+
#define mp_hal_delay_us_fast(us) (mp_hal_delay_us(us))
#define mp_hal_pin_od_low(p) (mp_raise_NotImplementedError("mp_hal_pin_od_low"))
#define mp_hal_pin_od_high(p) (mp_raise_NotImplementedError("mp_hal_pin_od_high"))
diff --git a/py/mphal.h b/py/mphal.h
index 66d80705a..6d11f6ddc 100644
--- a/py/mphal.h
+++ b/py/mphal.h
@@ -26,6 +26,7 @@
#ifndef MICROPY_INCLUDED_PY_MPHAL_H
#define MICROPY_INCLUDED_PY_MPHAL_H
+#include <stdint.h>
#include "py/mpconfig.h"
#ifdef MICROPY_MPHALPORT_H
@@ -74,6 +75,11 @@ mp_uint_t mp_hal_ticks_us(void);
mp_uint_t mp_hal_ticks_cpu(void);
#endif
+#ifndef mp_hal_time_ns
+// Nanoseconds since 1970/1/1.
+uint64_t mp_hal_time_ns(void);
+#endif
+
// If port HAL didn't define its own pin API, use generic
// "virtual pin" API from the core.
#ifndef mp_hal_pin_obj_t