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