Merge branch 'edge' into onboot
[clinton/Smoothieware.git] / src / libs / StepTicker.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
9
10 using namespace std;
11 #include <vector>
12 #include "libs/nuts_bolts.h"
13 #include "libs/Module.h"
14 #include "libs/Kernel.h"
15 #include "StepTicker.h"
16 #include "system_LPC17xx.h" // mbed.h lib
17
18 #include <mri.h>
19
20 // StepTicker handles the base frequency ticking for the Stepper Motors / Actuators
21 // It has a list of those, and calls their tick() functions at regular intervals
22 // They then do Bresenham stuff themselves
23
24 StepTicker* global_step_ticker;
25
26 StepTicker::StepTicker(){
27 global_step_ticker = this;
28 LPC_TIM0->MR0 = 10000000; // Initial dummy value for Match Register
29 LPC_TIM0->MCR = 3; // Match on MR0, reset on MR0, match on MR1
30 LPC_TIM0->TCR = 1; // Enable interrupt
31
32 LPC_SC->PCONP |= (1 << 2); // Power Ticker ON
33 LPC_TIM1->MR0 = 1000000;
34 LPC_TIM1->MCR = 1;
35 LPC_TIM1->TCR = 1; // Enable interrupt
36
37 // Default start values
38 this->moves_finished = false;
39 this->reset_step_pins = false;
40 this->debug = 0;
41 this->has_axes = 0;
42 this->set_frequency(0.001);
43 this->set_reset_delay(100);
44 this->last_duration = 0;
45 for (int i = 0; i < 12; i++)
46 this->active_motors[i] = NULL;
47 this->active_motor_bm = 0;
48
49 NVIC_EnableIRQ(TIMER0_IRQn); // Enable interrupt handler
50 NVIC_EnableIRQ(TIMER1_IRQn); // Enable interrupt handler
51 }
52
53 // Set the base stepping frequency
54 void StepTicker::set_frequency( double frequency ){
55 this->frequency = frequency;
56 this->period = int(floor((SystemCoreClock/4)/frequency)); // SystemCoreClock/4 = Timer increments in a second
57 LPC_TIM0->MR0 = this->period;
58 if( LPC_TIM0->TC > LPC_TIM0->MR0 ){
59 LPC_TIM0->TCR = 3; // Reset
60 LPC_TIM0->TCR = 1; // Reset
61 }
62 }
63
64 // Set the reset delay
65 void StepTicker::set_reset_delay( double seconds ){
66 this->delay = int(floor(double(SystemCoreClock/4)*( seconds ))); // SystemCoreClock/4 = Timer increments in a second
67 LPC_TIM1->MR0 = this->delay;
68 }
69
70 // Add a stepper motor object to our list of steppers we must take care of
71 StepperMotor* StepTicker::add_stepper_motor(StepperMotor* stepper_motor){
72 this->stepper_motors.push_back(stepper_motor);
73 stepper_motor->step_ticker = this;
74 this->has_axes = true;
75 return stepper_motor;
76 }
77
78 // Call tick() on each active motor
79 inline void StepTicker::tick(){
80 _isr_context = true;
81
82 int i;
83 uint32_t bm;
84 for (i = 0, bm = 1; i < 12; i++, bm <<= 1)
85 {
86 if (this->active_motor_bm & bm)
87 {
88 this->active_motors[i]->tick();
89 }
90 }
91
92 _isr_context = false;
93 }
94
95 // Call signal_mode_finished() on each active motor that asked to be signaled. We do this instead of inside of tick() so that
96 // all tick()s are called before we do the move finishing
97 void StepTicker::signal_moves_finished(){
98 _isr_context = true;
99
100 int i;
101 uint32_t bm;
102 for (i = 0, bm = 1; i < 12; i++, bm <<= 1)
103 {
104 if (this->active_motor_bm & bm)
105 {
106 if (this->active_motors[i]->is_move_finished)
107 {
108 this->active_motors[i]->signal_move_finished();
109 if (this->active_motors[i]->moving == false)
110 {
111 if (i > 0)
112 {
113 i--;
114 bm >>= 1;
115 }
116 }
117 }
118 }
119 }
120 this->moves_finished = false;
121
122 _isr_context = false;
123 }
124
125 // Reset step pins on all active motors
126 inline void StepTicker::reset_tick(){
127 _isr_context = true;
128
129 int i;
130 uint32_t bm;
131 for (i = 0, bm = 1; i < 12; i++, bm <<= 1)
132 {
133 if (this->active_motor_bm & bm)
134 this->active_motors[i]->step_pin->set(0);
135 }
136
137 _isr_context = false;
138 }
139
140 extern "C" void TIMER1_IRQHandler (void){
141 LPC_TIM1->IR |= 1 << 0;
142 global_step_ticker->reset_tick();
143 }
144
145
146 //#pragma GCC push_options
147 //#pragma GCC optimize ("O0")
148
149
150 // The actual interrupt handler where we do all the work
151 extern "C" void TIMER0_IRQHandler (void){
152
153 LPC_GPIO1->FIODIR |= 1<<18;
154 LPC_GPIO1->FIOSET = 1<<18;
155
156 // uint32_t initial_tc = LPC_TIM0->TC;
157
158 LPC_TIM0->IR |= 1 << 0;
159
160 // If no axes enabled, just ignore for now
161 if( global_step_ticker->active_motor_bm == 0 ){
162 LPC_GPIO1->FIOCLR = 1<<18;
163 return;
164 }
165
166 // Do not get out of here before everything is nice and tidy
167 LPC_TIM0->MR0 = 2000000;
168
169 // Step pins
170 global_step_ticker->tick();
171
172 // We may have set a pin on in this tick, now we start the timer to set it off
173 if( global_step_ticker->reset_step_pins ){
174 LPC_TIM1->TCR = 3;
175 LPC_TIM1->TCR = 1;
176 global_step_ticker->reset_step_pins = false;
177 }
178
179 // If a move finished in this tick, we have to tell the actuator to act accordingly
180 if( global_step_ticker->moves_finished ){ global_step_ticker->signal_moves_finished(); }
181
182 // uint32_t after_signal = LPC_TIM0->TC;
183
184 // If we went over the duration an interrupt is supposed to last, we have a problem
185 // That can happen tipically when we change blocks, where more than usual computation is done
186 // This can be OK, if we take notice of it, which we do now
187 if( LPC_TIM0->TC > global_step_ticker->period ){ // TODO: remove the size condition
188
189 LPC_GPIO1->FIODIR |= 1<<19;
190 LPC_GPIO1->FIOSET = 1<<19;
191
192 uint32_t start_tc = LPC_TIM0->TC;
193
194 // How many ticks we want to skip ( this does not include the current tick, but we add the time we spent doing this computation last time )
195 uint32_t ticks_to_skip = ( ( LPC_TIM0->TC + global_step_ticker->last_duration ) / global_step_ticker->period );
196
197 // Next step is now to reduce this to how many steps we can *actually* skip
198 uint32_t ticks_we_actually_can_skip = ticks_to_skip;
199
200 int i;
201 uint32_t bm;
202 for (i = 0, bm = 1; i < 12; i++, bm <<= 1)
203 {
204 if (global_step_ticker->active_motor_bm & bm)
205 ticks_we_actually_can_skip =
206 min(ticks_we_actually_can_skip,
207 (uint32_t)((uint64_t)( (uint64_t)global_step_ticker->active_motors[i]->fx_ticks_per_step - (uint64_t)global_step_ticker->active_motors[i]->fx_counter ) >> 32)
208 );
209 }
210
211 // Adding to MR0 for this time is not enough, we must also increment the counters ourself artificially
212 for (i = 0, bm = 1; i < 12; i++, bm <<= 1)
213 {
214 if (global_step_ticker->active_motor_bm & bm)
215 global_step_ticker->active_motors[i]->fx_counter += (uint64_t)((uint64_t)(ticks_we_actually_can_skip)<<32);
216 }
217
218 // When must we have our next MR0 ? ( +1 is here to account that we are actually doing a legit MR0 match here too, not only overtime )
219 // LPC_TIM0->MR0 = ( ticks_we_actually_can_skip + 1 ) * global_step_ticker->period;
220 LPC_TIM0->MR0 = ( ticks_to_skip + 1 ) * global_step_ticker->period;
221
222 // This is so that we know how long this computation takes, and we can take it into account next time
223 int difference = (int)(LPC_TIM0->TC) - (int)(start_tc);
224 if( difference > 0 ){ global_step_ticker->last_duration = (uint32_t)difference; }
225
226 //if( global_step_ticker->last_duration > 2000 || LPC_TIM0->MR0 > 2000 || LPC_TIM0->TC > 2000 || initial_tc > 2000 ){ __debugbreak(); }
227
228 LPC_GPIO1->FIOCLR = 1<<19;
229
230 }else{
231 LPC_TIM0->MR0 = global_step_ticker->period;
232 }
233
234 LPC_GPIO1->FIOCLR = 1<<18;
235
236 while( LPC_TIM0->TC > LPC_TIM0->MR0 ){
237 LPC_TIM0->MR0 += global_step_ticker->period;
238 }
239
240 LPC_GPIO1->FIOCLR = 1<<18;
241 }
242
243
244 //#pragma GCC pop_options
245
246 // We make a list of steppers that want to be called so that we don't call them for nothing
247 void StepTicker::add_motor_to_active_list(StepperMotor* motor)
248 {
249 uint32_t bm;
250 int i;
251 for (i = 0, bm = 1; i < 12; i++, bm <<= 1)
252 {
253 if (this->active_motors[i] == motor)
254 {
255 this->active_motor_bm |= bm;
256 return;
257 }
258 if (this->active_motors[i] == NULL)
259 {
260 this->active_motors[i] = motor;
261 this->active_motor_bm |= bm;
262 return;
263 }
264 }
265 return;
266 }
267
268 // Remove a stepper from the list of active motors
269 void StepTicker::remove_motor_from_active_list(StepperMotor* motor)
270 {
271 uint32_t bm; int i;
272 for (i = 0, bm = 1; i < 12; i++, bm <<= 1)
273 {
274 if (this->active_motors[i] == motor)
275 {
276 this->active_motor_bm &= ~bm;
277 return;
278 }
279 }
280 }