summaryrefslogtreecommitdiff
path: root/ports/risc-v64/gnu/example_build/erbium/hwtimer.c
diff options
context:
space:
mode:
authorFrédéric Desbiens <[email protected]>2026-10-02 10:42:44 -0400
committerGitHub <[email protected]>2026-10-02 10:42:44 -0400
commit93387b0a60382a29579bbad5045c68328e3751df (patch)
tree42be790d602cb76f55090e5a7c418df7ea2b3428 /ports/risc-v64/gnu/example_build/erbium/hwtimer.c
parent44d7c95c582d415c4ad84527180b29c93c3bf664 (diff)
parent28281cd5647d4671236ca08d19bd18ed15b2265a (diff)
Merge pull request #783 from eclipse-threadx/devv6.5.2.202603_rel
Merged changes ahead of the v6.5.2.202603 release
Diffstat (limited to 'ports/risc-v64/gnu/example_build/erbium/hwtimer.c')
-rw-r--r--ports/risc-v64/gnu/example_build/erbium/hwtimer.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/ports/risc-v64/gnu/example_build/erbium/hwtimer.c b/ports/risc-v64/gnu/example_build/erbium/hwtimer.c
new file mode 100644
index 00000000..511bea59
--- /dev/null
+++ b/ports/risc-v64/gnu/example_build/erbium/hwtimer.c
@@ -0,0 +1,42 @@
+/***************************************************************************
+ * Copyright (c) 2026 Eclipse ThreadX contributors
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the MIT License which is available at
+ * https://opensource.org/licenses/MIT.
+ *
+ * AI Disclosure: This file was largely AI-generated by Claude Code (Opus 5.5).
+ * The AI-generated portions may be considered public domain (CC0-1.0)
+ * and not subject to the project's licence. The human contributor has
+ * reviewed and verified that the code is correct.
+ *
+ * SPDX-License-Identifier: MIT and CC0-1.0
+ **************************************************************************/
+
+#include "tx_api.h"
+#include "hwtimer.h"
+
+/* Naturally aligned 64-bit MMIO accesses are single loads and stores on
+ RV64; volatile keeps the compiler from caching or reordering them. */
+#define MTIME (*(volatile uint64_t *)ERBIUM_MTIME)
+#define MTIMECMP (*(volatile uint64_t *)ERBIUM_MTIMECMP)
+
+int hwtimer_init(void)
+{
+ MTIMECMP = MTIME + TICKNUM_PER_TIMER;
+ return 0;
+}
+
+int hwtimer_handler(void)
+{
+ /* Advance from the previous compare value, so trap latency does not
+ accumulate as tick drift. */
+ uint64_t next = MTIMECMP + TICKNUM_PER_TIMER;
+ uint64_t now = MTIME;
+
+ if (next <= now)
+ next = now + TICKNUM_PER_TIMER;
+
+ MTIMECMP = next;
+ return 0;
+}