aboutsummaryrefslogtreecommitdiffstats
path: root/src/lpc176x/timer.c
blob: 318e8beaf00f1d3fe5cb99a8303156ffc27760d9 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// lpc176x timer interrupt scheduling
//
// Copyright (C) 2018  Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.

#include "LPC17xx.h" // LPC_TIM0
#include "board/irq.h" // irq_disable
#include "board/misc.h" // timer_read_time
#include "board/timer_irq.h" // timer_dispatch_many
#include "internal.h" // enable_peripheral_clock
#include "sched.h" // DECL_INIT

// Set the next irq time
static void
timer_set(uint32_t value)
{
    LPC_TIM0->MR0 = value;
    LPC_TIM0->IR = 0x01;
}

// Return the current time (in absolute clock ticks).
uint32_t
timer_read_time(void)
{
    return LPC_TIM0->TC;
}

// Activate timer dispatch as soon as possible
void
timer_kick(void)
{
    timer_set(timer_read_time() + 50);
}

void
timer_init(void)
{
    // Disable timer
    LPC_TIM0->TCR = 0x02;
    // Setup clock and prescaler (divide sys clock by 4)
    enable_peripheral_clock(PCLK_TIMER0);
    LPC_TIM0->PR = 3;
    // Enable interrupts
    NVIC_SetPriority(TIMER0_IRQn, 2);
    NVIC_EnableIRQ(TIMER0_IRQn);
    LPC_TIM0->MCR = 0x01;
    // Clear counter value
    LPC_TIM0->TC = 0;
    timer_kick();
    // Start timer
    LPC_TIM0->TCR = 0x01;
}
DECL_INIT(timer_init);

void __visible __aligned(16) // aligning helps stabilize perf benchmarks
TIMER0_IRQHandler(void)
{
    irq_disable();
    uint32_t next = timer_dispatch_many();
    timer_set(next);
    irq_enable();
}