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