static void
apollo2_os_tick_handler(void)
{
    uint32_t cur;
    int os_ticks;
    int delta;
    os_sr_t sr;

    OS_ENTER_CRITICAL(sr);

    /* Calculate elapsed ticks and advance OS time. */
    cur = am_hal_stimer_counter_get();
    delta = cur - apollo2_os_tick_prev;
    os_ticks = delta / apollo2_os_tick_dur;
    os_time_advance(os_ticks);

    /* Clear timer interrupt. */
    am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREA);

    /* Update the time associated with the most recent tick. */
    apollo2_os_tick_prev += os_ticks * apollo2_os_tick_dur;

    /* Schedule timer to interrupt at the next tick. */
    apollo2_os_tick_set_timer(1);

    OS_EXIT_CRITICAL(sr);
}
static void
nrf52_timer_handler(void)
{
    int delta;
    int ticks;
    os_sr_t sr;
    uint32_t counter;

    os_trace_enter_isr();
    OS_ENTER_CRITICAL(sr);

    /* Calculate elapsed ticks and advance OS time. */

    counter = nrf52_os_tick_counter();
    delta = sub24(counter, g_hal_os_tick.lastocmp);
    ticks = delta / g_hal_os_tick.ticks_per_ostick;
    os_time_advance(ticks);

    /* Clear timer interrupt */
    OS_TICK_TIMER->EVENTS_COMPARE[OS_TICK_CMPREG] = 0;

    /* Update the time associated with the most recent tick */
    g_hal_os_tick.lastocmp = (g_hal_os_tick.lastocmp +
        (ticks * g_hal_os_tick.ticks_per_ostick)) & 0xffffff;

    /* Update the output compare to interrupt at the next tick */
    nrf52_os_tick_set_ocmp(g_hal_os_tick.lastocmp + g_hal_os_tick.ticks_per_ostick);

    OS_EXIT_CRITICAL(sr);
    os_trace_exit_isr();
}
void
timer_interrupt_handler(void)
{
    uint64_t time = get_timer_value();
    int delta = (int)(time - last_tick_time);
    last_tick_time = time;

    int ticks = delta / ticks_per_ostick;
    set_mtimecmp(time + ticks_per_ostick);

    os_time_advance(ticks);
}
/*
 * LPTMR irq handler
 *
 * This IRQ handles OS time. Currently, this MCU does not have the tickless
 * OS implemented. It is also possible to miss OS ticks if interrupts are
 * disabled for too long (longer than one tick).
 */
static void
mkw41z_os_tick_handler(void)
{
    uint32_t csr;
    uint32_t sr;

    OS_ENTER_CRITICAL(sr);

    /* Must make sure flag is set when we get interrupt */
    csr = LPTMR0->CSR;
    if (csr & LPTMR_CSR_TCF_MASK) {
        /* Advance os time */
        os_time_advance(1);

        /* Clear mask */
        LPTMR0->CSR = csr;
    }

    OS_EXIT_CRITICAL(sr);
}
void
timer_handler(void)
{
    os_time_advance(1);
}