refactor ON_HALT, add THEKERNEL->is_halted() for modules that just need to test it...
[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 "libs/nuts_bolts.h"
11 #include "libs/Module.h"
12 #include "libs/Kernel.h"
13 #include "SlowTicker.h"
14 #include "StepTicker.h"
15 #include "libs/Hook.h"
16 #include "modules/robot/Conveyor.h"
17 #include "Pauser.h"
18 #include "Gcode.h"
19
20 #include <mri.h>
21
22 // This module uses a Timer to periodically call hooks
23 // Modules register with a function ( callback ) and a frequency, and we then call that function at the given frequency.
24
25 SlowTicker* global_slow_ticker;
26
27 SlowTicker::SlowTicker(){
28 max_frequency = 0;
29 global_slow_ticker = this;
30
31
32 // ISP button FIXME: WHy is this here?
33 ispbtn.from_string("2.10")->as_input()->pull_up();
34
35 // TODO: What is this ??
36 flag_1s_flag = 0;
37 flag_1s_count = SystemCoreClock>>2;
38
39 // Configure the actual timer after setup to avoid race conditions
40 LPC_SC->PCONP |= (1 << 22); // Power Ticker ON
41 LPC_TIM2->MR0 = 10000; // Initial dummy value for Match Register
42 LPC_TIM2->MCR = 3; // Match on MR0, reset on MR0
43 LPC_TIM2->TCR = 1; // Enable interrupt
44 NVIC_EnableIRQ(TIMER2_IRQn); // Enable interrupt handler
45 }
46
47 void SlowTicker::on_module_loaded(){
48 register_for_event(ON_IDLE);
49 }
50
51 // Set the base frequency we use for all sub-frequencies
52 void SlowTicker::set_frequency( int frequency ){
53 this->interval = (SystemCoreClock >> 2) / frequency; // SystemCoreClock/4 = Timer increments in a second
54 LPC_TIM2->MR0 = this->interval;
55 LPC_TIM2->TCR = 3; // Reset
56 LPC_TIM2->TCR = 1; // Reset
57 flag_1s_count= SystemCoreClock>>2;
58 }
59
60 // The actual interrupt being called by the timer, this is where work is done
61 void SlowTicker::tick(){
62
63 // Call all hooks that need to be called ( bresenham )
64 for (Hook* hook : this->hooks){
65 hook->countdown -= this->interval;
66 if (hook->countdown < 0)
67 {
68 hook->countdown += hook->interval;
69 hook->call();
70 }
71 }
72
73 // deduct tick time from secound counter
74 flag_1s_count -= this->interval;
75 // if a whole second has elapsed,
76 if (flag_1s_count < 0)
77 {
78 // add a second to our counter
79 flag_1s_count += SystemCoreClock >> 2;
80 // and set a flag for idle event to pick up
81 flag_1s_flag++;
82 }
83
84 // Enter MRI mode if the ISP button is pressed
85 // TODO: This should have it's own module
86 if (ispbtn.get() == 0)
87 __debugbreak();
88
89 }
90
91 bool SlowTicker::flag_1s(){
92 // atomic flag check routine
93 // first disable interrupts
94 __disable_irq();
95 // then check for a flag
96 if (flag_1s_flag)
97 {
98 // if we have a flag, decrement the counter
99 flag_1s_flag--;
100 // re-enable interrupts
101 __enable_irq();
102 // and tell caller that we consumed a flag
103 return true;
104 }
105 // if no flag, re-enable interrupts and return false
106 __enable_irq();
107 return false;
108 }
109
110 #include "gpio.h"
111 extern GPIO leds[];
112 void SlowTicker::on_idle(void*)
113 {
114 static uint16_t ledcnt= 0;
115 if(THEKERNEL->is_using_leds()) {
116 // flash led 3 to show we are alive
117 leds[2]= (ledcnt++ & 0x1000) ? 1 : 0;
118 }
119
120 // if interrupt has set the 1 second flag
121 if (flag_1s())
122 // fire the on_second_tick event
123 THEKERNEL->call_event(ON_SECOND_TICK);
124 }
125
126 extern "C" void TIMER2_IRQHandler (void){
127 if((LPC_TIM2->IR >> 0) & 1){ // If interrupt register set for MR0
128 LPC_TIM2->IR |= 1 << 0; // Reset it
129 }
130 global_slow_ticker->tick();
131 }
132