cleanup before merging
[clinton/Smoothieware.git] / src / libs / SlowTicker.cpp
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
8 using namespace std;
9 #include <vector>
10 #include "mbed.h"
11 #include "libs/nuts_bolts.h"
12 #include "libs/Module.h"
13 #include "libs/Kernel.h"
14 #include "SlowTicker.h"
15 #include "libs/Hook.h"
16
17
18 SlowTicker* global_slow_ticker;
19
20 SlowTicker::SlowTicker(){
21 this->max_frequency = 1;
22 global_slow_ticker = this;
23 LPC_SC->PCONP |= (1 << 22); // Power Ticker ON
24 LPC_TIM2->MR0 = 10000; // Initial dummy value for Match Register
25 LPC_TIM2->MCR = 3; // Match on MR0, reset on MR0
26 LPC_TIM2->TCR = 1; // Enable interrupt
27 NVIC_EnableIRQ(TIMER2_IRQn); // Enable interrupt handler
28 }
29
30 void SlowTicker::set_frequency( int frequency ){
31 LPC_TIM2->MR0 = int(floor((SystemCoreClock/4)/frequency)); // SystemCoreClock/4 = Timer increments in a second
32 LPC_TIM2->TCR = 3; // Reset
33 LPC_TIM2->TCR = 1; // Reset
34 }
35
36 void SlowTicker::tick(){
37 if( this->max_frequency < 0.1 ){
38 this->kernel->serial->printf("empty\r\n");
39 }
40 for (int i=0; i<this->hooks.size(); i++){
41 Hook* hook = this->hooks.at(i);
42 hook->counter += ( hook->frequency / this->max_frequency );
43 if( hook->counter > 0 ){
44 hook->counter-=1;
45 hook->call();
46 }
47 }
48 }
49
50 extern "C" void TIMER2_IRQHandler (void){
51 if((LPC_TIM2->IR >> 0) & 1){ // If interrupt register set for MR0
52 LPC_TIM2->IR |= 1 << 0; // Reset it
53 global_slow_ticker->tick();
54 }
55 }
56