Skip to content

Commit

Permalink
W.I.P. #287
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvught committed Dec 10, 2024
1 parent b609a7b commit ac66c8e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 14 deletions.
8 changes: 4 additions & 4 deletions lib-clib/.cproject
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@
</folderInfo>
<sourceEntries>
<entry excluding="src|src/rdm" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
<entry excluding="c++|gd32|gd32/time_ptp|gd32/time_systick|gd32/time_timer|h3|h3/time_hs_timer|libc|nostdlib" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src"/>
<entry excluding="h3|gd32|gd32/time_ptp|libc|gd32/time_timer|gd32/time_systick|h3/time_hs_timer|c++|nostdlib" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src/c++"/>
<entry excluding="time_ptp|time_systick|time_timer" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src/gd32"/>
<entry excluding="time_timer|time_ptp|time_systick" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src/gd32"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src/gd32/time_ptp"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src/gd32/time_systick"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src/gd32/time_timer"/>
<entry excluding="time_timer_avs|time_hs_timer" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src/h3"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src/h3/time_hs_timer"/>
<entry excluding="time_timer_arm_generic|time_timer_avs" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src/h3"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src/h3/time_timer_arm_generic"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src/h3/time_timer_avs"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src/libc"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src/nostdlib"/>
Expand Down
3 changes: 2 additions & 1 deletion lib-clib/Makefile.H3
Original file line number Diff line number Diff line change
Expand Up @@ -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
100 changes: 100 additions & 0 deletions lib-clib/src/h3/time_timer_arm_generic/time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* @file time.cpp
*
*/
/* Copyright (C) 2024 by Arjan van Vught mailto:[email protected]
*
* 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 <cstdint>
#include <time.h>
#include <sys/time.h>
#include <cassert>

#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;
}

}
8 changes: 4 additions & 4 deletions lib-h3/src/irq_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;

Expand Down
10 changes: 5 additions & 5 deletions lib-ltc/src/arm/ltcreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(~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 {
Expand Down

0 comments on commit ac66c8e

Please sign in to comment.