Commit | Line | Data |
---|---|---|
ded56b35 AW |
1 | using namespace std; |
2 | #include <vector> | |
3 | #include "mbed.h" | |
4 | #include "libs/nuts_bolts.h" | |
5 | #include "libs/Module.h" | |
6 | #include "libs/Kernel.h" | |
7 | #include "SlowTicker.h" | |
8 | ||
9 | ||
10 | SlowTicker* global_slow_ticker; | |
11 | ||
12 | SlowTicker::SlowTicker(){ | |
13 | global_slow_ticker = this; | |
7dd8133c AW |
14 | LPC_SC->PCONP |= (1 << 22); // Power Ticker ON |
15 | LPC_TIM2->MR0 = 1000000; // Initial dummy value for Match Register | |
16 | LPC_TIM2->MCR = 3; // Match on MR0, reset on MR0 | |
17 | LPC_TIM2->TCR = 1; // Enable interrupt | |
18 | NVIC_EnableIRQ(TIMER2_IRQn); // Enable interrupt handler | |
ded56b35 AW |
19 | } |
20 | ||
21 | void SlowTicker::set_frequency( int frequency ){ | |
7dd8133c AW |
22 | LPC_TIM2->MR0 = int(floor((SystemCoreClock/4)/frequency)); // SystemCoreClock/4 = Timer increments in a second |
23 | LPC_TIM2->TCR = 3; // Reset | |
24 | LPC_TIM2->TCR = 1; // Reset | |
ded56b35 AW |
25 | } |
26 | ||
27 | void SlowTicker::tick(){ | |
28 | for (int i=0; i<this->hooks.size(); i++){ | |
29 | this->hooks.at(i)->call(); | |
30 | } | |
31 | } | |
32 | ||
33 | extern "C" void TIMER2_IRQHandler (void){ | |
7dd8133c AW |
34 | if((LPC_TIM2->IR >> 0) & 1){ // If interrupt register set for MR0 |
35 | LPC_TIM2->IR |= 1 << 0; // Reset it | |
ded56b35 AW |
36 | global_slow_ticker->tick(); |
37 | } | |
38 | } | |
39 |