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/>.
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"
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.
22 SlowTicker
* global_slow_ticker
;
24 SlowTicker::SlowTicker(){
26 global_slow_ticker
= this;
28 // Configure the actual timer
29 LPC_SC
->PCONP
|= (1 << 22); // Power Ticker ON
30 LPC_TIM2
->MR0
= 10000; // Initial dummy value for Match Register
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
36 ispbtn
.from_string("2.10")->as_input()->pull_up();
38 // TODO: What is this ??
40 flag_1s_count
= SystemCoreClock
;
46 void SlowTicker::on_module_loaded(){
47 register_for_event(ON_IDLE
);
48 register_for_event(ON_GCODE_RECEIVED
);
49 register_for_event(ON_GCODE_EXECUTE
);
52 // Set the base frequency we use for all sub-frequencies
53 void SlowTicker::set_frequency( int frequency
){
54 this->interval
= (SystemCoreClock
>> 2) / frequency
; // SystemCoreClock/4 = Timer increments in a second
55 LPC_TIM2
->MR0
= this->interval
;
56 LPC_TIM2
->TCR
= 3; // Reset
57 LPC_TIM2
->TCR
= 1; // Reset
60 // The actual interrupt being called by the timer, this is where work is done
61 void SlowTicker::tick(){
63 // Call all hooks that need to be called ( bresenham )
64 for (uint32_t i
=0; i
<this->hooks
.size(); i
++){
65 Hook
* hook
= this->hooks
.at(i
);
66 hook
->countdown
-= this->interval
;
67 if (hook
->countdown
< 0)
69 hook
->countdown
+= hook
->interval
;
74 flag_1s_count
-= this->interval
;
75 if (flag_1s_count
< 0)
77 flag_1s_count
+= SystemCoreClock
>> 2;
83 if (g4_ticks
> interval
)
89 // Enter MRI mode if the ISP button is pressed
90 // TODO : This should have it's own module
91 if (ispbtn
.get() == 0)
96 bool SlowTicker::flag_1s(){
108 void SlowTicker::on_idle(void*)
111 kernel
->call_event(ON_SECOND_TICK
);
113 // if G4 has finished, release our pause
114 if (g4_pause
&& (g4_ticks
== 0))
117 kernel
->pauser
->release();
121 // When a G4-type gcode is received, add it to the queue so we can execute it in time
122 void SlowTicker::on_gcode_received(void* argument
){
123 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
124 // Add the gcode to the queue ourselves if we need it
125 if( gcode
->has_g
&& gcode
->g
== 4 ){
126 if( this->kernel
->conveyor
->queue
.size() == 0 ){
127 this->kernel
->call_event(ON_GCODE_EXECUTE
, gcode
);
129 Block
* block
= this->kernel
->conveyor
->queue
.get_ref( this->kernel
->conveyor
->queue
.size() - 1 );
130 block
->append_gcode(gcode
);
135 // When a G4-type gcode is executed, start the pause
136 void SlowTicker::on_gcode_execute(void* argument
){
137 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
141 bool updated
= false;
142 if (gcode
->has_letter('P')) {
144 g4_ticks
+= gcode
->get_int('P') * ((SystemCoreClock
>> 2) / 1000UL);
146 if (gcode
->has_letter('S')) {
148 g4_ticks
+= gcode
->get_int('S') * (SystemCoreClock
>> 2);
151 // G4 Smm Pnn should pause for mm seconds + nn milliseconds
152 // at 120MHz core clock, the longest possible delay is (2^32 / (120MHz / 4)) = 143 seconds
155 kernel
->pauser
->take();
162 extern "C" void TIMER2_IRQHandler (void){
163 if((LPC_TIM2
->IR
>> 0) & 1){ // If interrupt register set for MR0
164 LPC_TIM2
->IR
|= 1 << 0; // Reset it
166 global_slow_ticker
->tick();