From ac66c8e9e49c4d4b356c1feb76815f97a7484269 Mon Sep 17 00:00:00 2001 From: Arjan van Vught Date: Tue, 10 Dec 2024 15:45:33 +0100 Subject: [PATCH] W.I.P. #287 --- lib-clib/.cproject | 8 +- lib-clib/Makefile.H3 | 3 +- .../src/h3/time_timer_arm_generic/time.cpp | 100 ++++++++++++++++++ lib-h3/src/irq_timer.cpp | 8 +- lib-ltc/src/arm/ltcreader.cpp | 10 +- 5 files changed, 115 insertions(+), 14 deletions(-) create mode 100755 lib-clib/src/h3/time_timer_arm_generic/time.cpp diff --git a/lib-clib/.cproject b/lib-clib/.cproject index 6e8271d32..1de2a2f66 100755 --- a/lib-clib/.cproject +++ b/lib-clib/.cproject @@ -170,14 +170,14 @@ - + - + - - + + diff --git a/lib-clib/Makefile.H3 b/lib-clib/Makefile.H3 index f3d8a420e..2ecf4bf3e 100755 --- a/lib-clib/Makefile.H3 +++ b/lib-clib/Makefile.H3 @@ -2,7 +2,8 @@ DEFINES =NDEBUG EXTRA_INCLUDES= -EXTRA_SRCDIR=src/h3/time_timer_avs +EXTRA_SRCDIR=src/h3/time_timer_arm_generic +#EXTRA_SRCDIR=src/h3/time_timer_avs include Rules.mk include ../firmware-template-h3/lib/Rules.mk diff --git a/lib-clib/src/h3/time_timer_arm_generic/time.cpp b/lib-clib/src/h3/time_timer_arm_generic/time.cpp new file mode 100755 index 000000000..49869a1d8 --- /dev/null +++ b/lib-clib/src/h3/time_timer_arm_generic/time.cpp @@ -0,0 +1,100 @@ +/** + * @file time.cpp + * + */ +/* Copyright (C) 2024 by Arjan van Vught mailto:info@gd32-dmx.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifdef __GNUC__ +# pragma GCC push_options +# pragma GCC optimize ("O2") +#endif + +#include +#include +#include +#include + +#include "h3.h" + +static uint32_t set_timer = 0; +static struct timeval s_tv; + +static inline uint64_t read_cntpct(void) { + uint32_t lo, hi; + asm volatile ("mrrc p15, 0, %0, %1, c14" : "=r" (lo), "=r" (hi)); + return ((uint64_t)hi << 32) | lo; +} + +extern "C" { +int gettimeofday(struct timeval *tv, __attribute__((unused)) struct timezone *tz) { + assert(tv != NULL); + + const uint64_t current_cntvct = read_cntpct(); + const uint64_t elapsed_ticks = current_cntvct - set_timer; + + // Convert ticks to microseconds + const uint64_t elapsed_usec = elapsed_ticks / 24;//(elapsed_ticks * 1000000ULL) / H3_F_24M; + const time_t elapsed_sec = elapsed_usec / 1000000ULL; + const suseconds_t elapsed_subsec = elapsed_usec % 1000000ULL; + + // Compute the current time + tv->tv_sec = s_tv.tv_sec + elapsed_sec; + tv->tv_usec = s_tv.tv_usec + elapsed_subsec; + + // Handle microsecond overflow + if (tv->tv_usec >= 1000000) { + tv->tv_sec++; + tv->tv_usec -= 1000000; + } + + return 0; +} + +int settimeofday(const struct timeval *tv, __attribute__((unused)) const struct timezone *tz) { + assert(tv != nullptr); + + // Capture the current virtual counter value as the reference + set_timer = read_cntpct(); + + // Set the new time + s_tv.tv_sec = tv->tv_sec; + s_tv.tv_usec = tv->tv_usec; + + return 0; +} + +/* + * time() returns the time as the number of seconds since the Epoch, + 1970-01-01 00:00:00 +0000 (UTC). + */ +time_t time(time_t *__timer) { + struct timeval tv; + gettimeofday(&tv, 0); + + if (__timer != nullptr) { + *__timer = tv.tv_sec; + } + + return tv.tv_sec; +} + +} diff --git a/lib-h3/src/irq_timer.cpp b/lib-h3/src/irq_timer.cpp index 78a41d50d..4dff672f0 100644 --- a/lib-h3/src/irq_timer.cpp +++ b/lib-h3/src/irq_timer.cpp @@ -52,8 +52,8 @@ /** * H3 Timers */ -static thunk_irq_timer_t h3_timer0_func = NULL; -static thunk_irq_timer_t h3_timer1_func = NULL; +static thunk_irq_timer_t h3_timer0_func; +static thunk_irq_timer_t h3_timer1_func; static void TIMER0_IRQHandler() { H3_TIMER->IRQ_STA = TIMER_IRQ_PEND_TMR0; /* Clear Timer 0 Pending bit */ @@ -78,8 +78,8 @@ static void TIMER1_IRQHandler() { /** * Generic ARM Timer */ -static volatile thunk_irq_timer_arm_t arm_physical_timer_func = nullptr; -static volatile thunk_irq_timer_arm_t arm_virtual_timer_func = nullptr; +static volatile thunk_irq_timer_arm_t arm_physical_timer_func; +static volatile thunk_irq_timer_arm_t arm_virtual_timer_func; static volatile uint32_t timer_value; diff --git a/lib-ltc/src/arm/ltcreader.cpp b/lib-ltc/src/arm/ltcreader.cpp index b7f89b454..0bae3c848 100755 --- a/lib-ltc/src/arm/ltcreader.cpp +++ b/lib-ltc/src/arm/ltcreader.cpp @@ -108,18 +108,18 @@ void EXTI10_15_IRQHandler() { __DMB(); #if defined (H3) - nFiqUsCurrent = H3_TIMER->AVS_CNT1; + nFiqUsCurrent = h3_hs_timer_lo_us(); H3_PIO_PA_INT->STA = static_cast(~0x0); - if (nFiqUsCurrent >= nFiqUsPrevious) { - nBitTime = nFiqUsCurrent - nFiqUsPrevious; + if (nFiqUsPrevious >= nFiqUsCurrent) { + nBitTime = nFiqUsPrevious - nFiqUsCurrent; + nBitTime = 42949672 - nBitTime; } else { - nBitTime = UINT32_MAX - (nFiqUsPrevious - nFiqUsCurrent); + nBitTime = nFiqUsCurrent - nFiqUsPrevious; } #elif defined (GD32) nFiqUsCurrent = TIMER_CNT(TIMER5); - if (nFiqUsCurrent >= nFiqUsPrevious) { nBitTime = nFiqUsCurrent - nFiqUsPrevious; } else {