aboutsummaryrefslogtreecommitdiff
path: root/laser-tag software/lptmr.c
diff options
context:
space:
mode:
Diffstat (limited to 'laser-tag software/lptmr.c')
-rw-r--r--laser-tag software/lptmr.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/laser-tag software/lptmr.c b/laser-tag software/lptmr.c
new file mode 100644
index 0000000..8fa7ad9
--- /dev/null
+++ b/laser-tag software/lptmr.c
@@ -0,0 +1,45 @@
+#include "lptmr.h"
+
+static const lptmr_user_config_t g_lptmrConfig = {
+ .timerMode = kLptmrTimerModeTimeCounter,
+ .freeRunningEnable = false,
+ .prescalerEnable = false,
+ .prescalerClockSource = kClockLptmrSrcLpoClk,
+ .isInterruptEnabled = true,
+};
+
+static lptmr_state_t lptmrState;
+static lptmr_callback_t user_cb;
+static uint32_t total_ms;
+
+static void lptmr_call_back(void)
+{
+ total_ms += 100;
+ user_cb();
+}
+
+uint32_t get_ms() {
+ uint32_t us = LPTMR_DRV_GetCurrentTimeUs(LPTMR0_IDX);
+ return total_ms + us / 1000;
+}
+
+void delay_ms(uint32_t delay)
+{
+ uint32_t until = get_ms() + delay;
+ while(get_ms() < until);
+}
+
+void lptmr_init(lptmr_callback_t cb)
+{
+ LPTMR_DRV_Init(LPTMR0_IDX, &lptmrState, &g_lptmrConfig);
+ LPTMR_DRV_SetTimerPeriodUs(LPTMR0_IDX, 100000);
+ user_cb = cb;
+ total_ms = 0;
+ LPTMR_DRV_InstallCallback(LPTMR0_IDX, lptmr_call_back);
+}
+
+void lptmr_start()
+{
+ LPTMR_DRV_Start(LPTMR0_IDX);
+}
+/* vim: set expandtab ts=4 sw=4: */