Merge branch 'edge' into extruderfix
[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
17 #include <mri.h>
18
19 SlowTicker* global_slow_ticker;
20
21 SlowTicker::SlowTicker(){
22 max_frequency = 0;
23 global_slow_ticker = this;
24 LPC_SC->PCONP |= (1 << 22); // Power Ticker ON
25 LPC_TIM2->MR0 = 10000; // Initial dummy value for Match Register
26 LPC_TIM2->MCR = 3; // Match on MR0, reset on MR0
27 LPC_TIM2->TCR = 1; // Enable interrupt
28 NVIC_EnableIRQ(TIMER2_IRQn); // Enable interrupt handler
29
30 ispbtn.from_string("2.10")->as_input()->pull_up();
31
32 flag_1s_flag = 0;
33 flag_1s_count = SystemCoreClock;
34
35 g4_ticks = 0;
36 g4_pause = false;
37 }
38
39 void SlowTicker::on_module_loaded()
40 {
41 register_for_event(ON_IDLE);
42 register_for_event(ON_GCODE_RECEIVED);
43 register_for_event(ON_GCODE_EXECUTE);
44 }
45
46 void SlowTicker::set_frequency( int frequency ){
47 this->interval = (SystemCoreClock >> 2) / frequency; // SystemCoreClock/4 = Timer increments in a second
48 LPC_TIM2->MR0 = this->interval;
49 LPC_TIM2->TCR = 3; // Reset
50 LPC_TIM2->TCR = 1; // Reset
51 }
52
53 void SlowTicker::tick()
54 {
55 _isr_context = true;
56
57 LPC_GPIO1->FIODIR |= 1<<20;
58 LPC_GPIO1->FIOSET = 1<<20;
59
60 for (uint32_t i=0; i<this->hooks.size(); i++){
61 Hook* hook = this->hooks.at(i);
62 hook->countdown -= this->interval;
63 if (hook->countdown < 0)
64 {
65 hook->countdown += hook->interval;
66 hook->call();
67 }
68 }
69
70 flag_1s_count -= this->interval;
71 if (flag_1s_count < 0)
72 {
73 flag_1s_count += SystemCoreClock >> 2;
74 flag_1s_flag++;
75 }
76
77 if (g4_ticks > 0)
78 {
79 if (g4_ticks > interval)
80 g4_ticks -= interval;
81 else
82 g4_ticks = 0;
83 }
84
85 LPC_GPIO1->FIOCLR = 1<<20;
86
87 if (ispbtn.get() == 0)
88 __debugbreak();
89
90 _isr_context = false;
91 }
92
93 bool SlowTicker::flag_1s(){
94 __disable_irq();
95 if (flag_1s_flag)
96 {
97 flag_1s_flag--;
98 __enable_irq();
99 return true;
100 }
101 __enable_irq();
102 return false;
103 }
104
105 void SlowTicker::on_idle(void*)
106 {
107 if (flag_1s())
108 kernel->call_event(ON_SECOND_TICK);
109
110 // if G4 has finished, release our pause
111 if (g4_pause && (g4_ticks == 0))
112 {
113 g4_pause = false;
114 kernel->pauser->release();
115 }
116 }
117
118 void SlowTicker::on_gcode_received(void* argument){
119 Gcode* gcode = static_cast<Gcode*>(argument);
120 // Add the gcode to the queue ourselves if we need it
121 if( gcode->has_g && gcode->g == 4 ){
122 if( this->kernel->conveyor->queue.size() == 0 ){
123 this->kernel->call_event(ON_GCODE_EXECUTE, gcode );
124 }else{
125 Block* block = this->kernel->conveyor->queue.get_ref( this->kernel->conveyor->queue.size() - 1 );
126 block->append_gcode(gcode);
127 gcode->queued++;
128 }
129 }
130 }
131
132 void SlowTicker::on_gcode_execute(void* argument){
133 Gcode* gcode = static_cast<Gcode*>(argument);
134
135 if (gcode->has_g)
136 {
137 if (gcode->g == 4)
138 {
139 if (gcode->has_letter('P'))
140 {
141 // G4 Pnn should pause for nn milliseconds
142 // at 120MHz core clock, the longest possible delay is (2^32 / (120MHz / 4)) = 143 seconds
143 if (g4_pause)
144 {
145 g4_ticks += gcode->get_int('P') * ((SystemCoreClock >> 2) / 1000UL);
146 }
147 else
148 {
149 g4_ticks = gcode->get_int('P') * ((SystemCoreClock >> 2) / 1000UL);
150 g4_pause = true;
151 kernel->pauser->take();
152 }
153 }
154 }
155 }
156 }
157
158 extern "C" void TIMER2_IRQHandler (void){
159 if((LPC_TIM2->IR >> 0) & 1){ // If interrupt register set for MR0
160 LPC_TIM2->IR |= 1 << 0; // Reset it
161 }
162 global_slow_ticker->tick();
163 }
164