Allow TABS in config
[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 "libs/Hook.h"
15 #include "modules/robot/Conveyor.h"
16 #include "Pauser.h"
17 #include "Gcode.h"
18
19 #include <mri.h>
20
21 // This module uses a Timer to periodically call hooks
22 // Modules register with a function ( callback ) and a frequency, and we then call that function at the given frequency.
23
24 SlowTicker* global_slow_ticker;
25
26 SlowTicker::SlowTicker(){
27 max_frequency = 0;
28 global_slow_ticker = this;
29
30 // Configure the actual timer
31 LPC_SC->PCONP |= (1 << 22); // Power Ticker ON
32 LPC_TIM2->MR0 = 10000; // Initial dummy value for Match Register
33 LPC_TIM2->MCR = 3; // Match on MR0, reset on MR0
34 LPC_TIM2->TCR = 1; // Enable interrupt
35 NVIC_EnableIRQ(TIMER2_IRQn); // Enable interrupt handler
36
37 // ISP button
38 ispbtn.from_string("2.10")->as_input()->pull_up();
39
40 // TODO: What is this ??
41 flag_1s_flag = 0;
42 flag_1s_count = SystemCoreClock>>2;
43
44 g4_ticks = 0;
45 g4_pause = false;
46 }
47
48 void SlowTicker::on_module_loaded(){
49 register_for_event(ON_IDLE);
50 register_for_event(ON_GCODE_RECEIVED);
51 register_for_event(ON_GCODE_EXECUTE);
52 }
53
54 // Set the base frequency we use for all sub-frequencies
55 void SlowTicker::set_frequency( int frequency ){
56 this->interval = (SystemCoreClock >> 2) / frequency; // SystemCoreClock/4 = Timer increments in a second
57 LPC_TIM2->MR0 = this->interval;
58 LPC_TIM2->TCR = 3; // Reset
59 LPC_TIM2->TCR = 1; // Reset
60 flag_1s_count= SystemCoreClock>>2;
61 }
62
63 // The actual interrupt being called by the timer, this is where work is done
64 void SlowTicker::tick(){
65
66 // Call all hooks that need to be called ( bresenham )
67 for (uint32_t i=0; i<this->hooks.size(); i++){
68 Hook* hook = this->hooks.at(i);
69 hook->countdown -= this->interval;
70 if (hook->countdown < 0)
71 {
72 hook->countdown += hook->interval;
73 hook->call();
74 }
75 }
76
77 // deduct tick time from secound counter
78 flag_1s_count -= this->interval;
79 // if a whole second has elapsed,
80 if (flag_1s_count < 0)
81 {
82 // add a second to our counter
83 flag_1s_count += SystemCoreClock >> 2;
84 // and set a flag for idle event to pick up
85 flag_1s_flag++;
86 }
87
88 // if we're counting down a pause
89 if (g4_ticks > 0)
90 {
91 // deduct tick time from timeout
92 if (g4_ticks > interval)
93 g4_ticks -= interval;
94 else
95 g4_ticks = 0;
96 }
97
98 // Enter MRI mode if the ISP button is pressed
99 // TODO: This should have it's own module
100 if (ispbtn.get() == 0)
101 __debugbreak();
102
103 }
104
105 bool SlowTicker::flag_1s(){
106 // atomic flag check routine
107 // first disable interrupts
108 __disable_irq();
109 // then check for a flag
110 if (flag_1s_flag)
111 {
112 // if we have a flag, decrement the counter
113 flag_1s_flag--;
114 // re-enable interrupts
115 __enable_irq();
116 // and tell caller that we consumed a flag
117 return true;
118 }
119 // if no flag, re-enable interrupts and return false
120 __enable_irq();
121 return false;
122 }
123
124 #include "gpio.h"
125 extern GPIO leds[];
126 void SlowTicker::on_idle(void*)
127 {
128 static uint16_t ledcnt= 0;
129 if(THEKERNEL->use_leds) {
130 // flash led 3 to show we are alive
131 leds[2]= (ledcnt++ & 0x1000) ? 1 : 0;
132 }
133
134 // if interrupt has set the 1 second flag
135 if (flag_1s())
136 // fire the on_second_tick event
137 THEKERNEL->call_event(ON_SECOND_TICK);
138
139 // if G4 has finished, release our pause
140 if (g4_pause && (g4_ticks == 0))
141 {
142 g4_pause = false;
143 THEKERNEL->pauser->release();
144 }
145 }
146
147 // When a G4-type gcode is received, add it to the queue so we can execute it in time
148 void SlowTicker::on_gcode_received(void* argument){
149 Gcode* gcode = static_cast<Gcode*>(argument);
150 // Add the gcode to the queue ourselves if we need it
151 if( gcode->has_g && gcode->g == 4 ){
152 THEKERNEL->conveyor->append_gcode(gcode);
153 // ensure that no subsequent gcodes get executed along with our G4
154 THEKERNEL->conveyor->queue_head_block();
155 }
156 }
157
158 // When a G4-type gcode is executed, start the pause
159 void SlowTicker::on_gcode_execute(void* argument){
160 Gcode* gcode = static_cast<Gcode*>(argument);
161
162 if (gcode->has_g){
163 if (gcode->g == 4){
164 gcode->mark_as_taken();
165 bool updated = false;
166 if (gcode->has_letter('P')) {
167 updated = true;
168 g4_ticks += gcode->get_int('P') * ((SystemCoreClock >> 2) / 1000UL);
169 }
170 if (gcode->has_letter('S')) {
171 updated = true;
172 g4_ticks += gcode->get_int('S') * (SystemCoreClock >> 2);
173 }
174 if (updated){
175 // G4 Smm Pnn should pause for mm seconds + nn milliseconds
176 // at 120MHz core clock, the longest possible delay is (2^32 / (120MHz / 4)) = 143 seconds
177 if (!g4_pause){
178 g4_pause = true;
179 THEKERNEL->pauser->take();
180 }
181 }
182 }
183 }
184 }
185
186 extern "C" void TIMER2_IRQHandler (void){
187 if((LPC_TIM2->IR >> 0) & 1){ // If interrupt register set for MR0
188 LPC_TIM2->IR |= 1 << 0; // Reset it
189 }
190 global_slow_ticker->tick();
191 }
192