summaryrefslogtreecommitdiff
path: root/libhw/rp2040_hwtimer.c
diff options
context:
space:
mode:
Diffstat (limited to 'libhw/rp2040_hwtimer.c')
-rw-r--r--libhw/rp2040_hwtimer.c46
1 files changed, 13 insertions, 33 deletions
diff --git a/libhw/rp2040_hwtimer.c b/libhw/rp2040_hwtimer.c
index c8c281e..ada6246 100644
--- a/libhw/rp2040_hwtimer.c
+++ b/libhw/rp2040_hwtimer.c
@@ -1,6 +1,6 @@
/* libhw/rp2040_hwtimer.c - <libhw/generic/alarmclock.h> implementation for the RP2040's hardware timer
*
- * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
@@ -9,10 +9,10 @@
#include <libcr/coroutine.h>
#include <libmisc/assert.h>
-#include <libmisc/vcall.h>
#define IMPLEMENTATION_FOR_LIBHW_GENERIC_ALARMCLOCK_H YES
#include <libhw/generic/alarmclock.h>
+
#include <libhw/rp2040_hwtimer.h>
/******************************************************************************/
@@ -23,48 +23,32 @@ void add_alarm_at(void) {};
/* Types **********************************************************************/
struct rp2040_hwtimer {
- implements_alarmclock;
enum rp2040_hwalarm_instance alarm_num;
bool initialized;
struct alarmclock_trigger *queue;
};
+LO_IMPLEMENTATION_H(alarmclock, struct rp2040_hwtimer, rp2040_hwtimer);
+LO_IMPLEMENTATION_C(alarmclock, struct rp2040_hwtimer, rp2040_hwtimer, static);
/* Globals ********************************************************************/
-static uint64_t rp2040_hwtimer_get_time_ns(implements_alarmclock *self);
-static bool rp2040_hwtimer_add_trigger(implements_alarmclock *self,
- struct alarmclock_trigger *trigger,
- uint64_t fire_at_ns,
- void (*cb)(void *),
- void *cb_arg);
-static void rp2040_hwtimer_del_trigger(implements_alarmclock *self,
- struct alarmclock_trigger *trigger);
-
-static struct alarmclock_vtable rp2040_hwtimer_vtable = {
- .get_time_ns = rp2040_hwtimer_get_time_ns,
- .add_trigger = rp2040_hwtimer_add_trigger,
- .del_trigger = rp2040_hwtimer_del_trigger,
-};
-
static struct rp2040_hwtimer hwtimers[] = {
- { .vtable = &rp2040_hwtimer_vtable, .alarm_num = 0 },
- { .vtable = &rp2040_hwtimer_vtable, .alarm_num = 1 },
- { .vtable = &rp2040_hwtimer_vtable, .alarm_num = 2 },
- { .vtable = &rp2040_hwtimer_vtable, .alarm_num = 3 },
+ { .alarm_num = 0 },
+ { .alarm_num = 1 },
+ { .alarm_num = 2 },
+ { .alarm_num = 3 },
};
static_assert(sizeof(hwtimers)/sizeof(hwtimers[0]) == _RP2040_HWALARM_NUM);
-implements_alarmclock *bootclock = &hwtimers[0];
-
/* Main implementation ********************************************************/
-implements_alarmclock *rp2040_hwtimer(enum rp2040_hwalarm_instance alarm_num) {
+lo_interface alarmclock rp2040_hwtimer(enum rp2040_hwalarm_instance alarm_num) {
assert(alarm_num < _RP2040_HWALARM_NUM);
- return &hwtimers[alarm_num];
+ return lo_box_rp2040_hwtimer_as_alarmclock(&hwtimers[alarm_num]);
}
-static uint64_t rp2040_hwtimer_get_time_ns(implements_alarmclock *) {
+static uint64_t rp2040_hwtimer_get_time_ns(struct rp2040_hwtimer *) {
return timer_time_us_64(timer_hw) * (NS_PER_S/US_PER_S);
}
@@ -93,13 +77,11 @@ static void rp2040_hwtimer_intrhandler(void) {
timer_hw->alarm[alarm_num] = (uint32_t)NS_TO_US_ROUNDUP(alarmclock->queue->fire_at_ns);
}
-static bool rp2040_hwtimer_add_trigger(implements_alarmclock *_alarmclock,
+static bool rp2040_hwtimer_add_trigger(struct rp2040_hwtimer *alarmclock,
struct alarmclock_trigger *trigger,
uint64_t fire_at_ns,
void (*cb)(void *),
void *cb_arg) {
- struct rp2040_hwtimer *alarmclock =
- VCALL_SELF(struct rp2040_hwtimer, implements_alarmclock, _alarmclock);
assert(alarmclock);
assert(trigger);
assert(fire_at_ns);
@@ -149,10 +131,8 @@ static bool rp2040_hwtimer_add_trigger(implements_alarmclock *_alarmclock,
return false;
}
-static void rp2040_hwtimer_del_trigger(implements_alarmclock *_alarmclock,
+static void rp2040_hwtimer_del_trigger(struct rp2040_hwtimer *alarmclock,
struct alarmclock_trigger *trigger) {
- struct rp2040_hwtimer *alarmclock =
- VCALL_SELF(struct rp2040_hwtimer, implements_alarmclock, _alarmclock);
assert(alarmclock);
assert(trigger);