aboutsummaryrefslogtreecommitdiff
path: root/laser-tag software/lptmr.c
blob: 8fa7ad9f278df009652df9c5be535a80685ab918 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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: */