Commit | Line | Data |
---|---|---|
cd011f58 AW |
1 | /* |
2 | This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). | |
3 | Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | |
4 | Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | |
5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. | |
6 | */ | |
7 | ||
ded56b35 AW |
8 | using namespace std; |
9 | #include <vector> | |
ded56b35 AW |
10 | #include "libs/nuts_bolts.h" |
11 | #include "libs/Module.h" | |
12 | #include "libs/Kernel.h" | |
13 | #include "SlowTicker.h" | |
d9ebc974 | 14 | #include "libs/Hook.h" |
ded56b35 AW |
15 | |
16 | ||
17 | SlowTicker* global_slow_ticker; | |
18 | ||
19 | SlowTicker::SlowTicker(){ | |
d9ebc974 | 20 | this->max_frequency = 1; |
ded56b35 | 21 | global_slow_ticker = this; |
7dd8133c | 22 | LPC_SC->PCONP |= (1 << 22); // Power Ticker ON |
d9ebc974 | 23 | LPC_TIM2->MR0 = 10000; // Initial dummy value for Match Register |
7dd8133c AW |
24 | LPC_TIM2->MCR = 3; // Match on MR0, reset on MR0 |
25 | LPC_TIM2->TCR = 1; // Enable interrupt | |
26 | NVIC_EnableIRQ(TIMER2_IRQn); // Enable interrupt handler | |
ded56b35 AW |
27 | } |
28 | ||
29 | void SlowTicker::set_frequency( int frequency ){ | |
7dd8133c AW |
30 | LPC_TIM2->MR0 = int(floor((SystemCoreClock/4)/frequency)); // SystemCoreClock/4 = Timer increments in a second |
31 | LPC_TIM2->TCR = 3; // Reset | |
32 | LPC_TIM2->TCR = 1; // Reset | |
ded56b35 AW |
33 | } |
34 | ||
35 | void SlowTicker::tick(){ | |
36 | for (int i=0; i<this->hooks.size(); i++){ | |
d9ebc974 AW |
37 | Hook* hook = this->hooks.at(i); |
38 | hook->counter += ( hook->frequency / this->max_frequency ); | |
d9ebc974 AW |
39 | if( hook->counter > 0 ){ |
40 | hook->counter-=1; | |
d9ebc974 AW |
41 | hook->call(); |
42 | } | |
ded56b35 AW |
43 | } |
44 | } | |
45 | ||
46 | extern "C" void TIMER2_IRQHandler (void){ | |
7dd8133c AW |
47 | if((LPC_TIM2->IR >> 0) & 1){ // If interrupt register set for MR0 |
48 | LPC_TIM2->IR |= 1 << 0; // Reset it | |
ded56b35 AW |
49 | global_slow_ticker->tick(); |
50 | } | |
51 | } | |
52 |