Commit ee50a6ef authored by Damien George's avatar Damien George

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: default avatarDamien George <damien@micropython.org>
parent badd3511
......@@ -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;
......
......@@ -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);
......
......@@ -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
......
......@@ -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;
}
......@@ -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"))
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment