fix when slow ticker gets started
[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 "StepTicker.h"
15 #include "libs/Hook.h"
16 #include "modules/robot/Conveyor.h"
17 #include "Gcode.h"
18
19 #include <mri.h>
20
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
24 SlowTicker* global_slow_ticker;
25
26 SlowTicker::SlowTicker(){
27 global_slow_ticker = this;
28
29 // ISP button FIXME: WHy is this here?
30 ispbtn.from_string("2.10")->as_input()->pull_up();
31
32 LPC_SC->PCONP |= (1 << 22); // Power Ticker ON
33 LPC_TIM2->MCR = 3; // Match on MR0, reset on MR0
34 // do not enable interrupt until setup is complete
35 LPC_TIM2->TCR = 0; // Disable interrupt
36
37 max_frequency = 5; // initial max frequency is set to 5Hz
38 set_frequency(max_frequency);
39 flag_1s_flag = 0;
40 }
41
42 void SlowTicker::start()
43 {
44 LPC_TIM2->TCR = 1; // Enable interrupt
45 NVIC_EnableIRQ(TIMER2_IRQn); // Enable interrupt handler
46 }
47
48 void SlowTicker::on_module_loaded(){
49 register_for_event(ON_IDLE);
50 }
51
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
58 flag_1s_count= SystemCoreClock>>2;
59 }
60
61 // The actual interrupt being called by the timer, this is where work is done
62 void SlowTicker::tick(){
63
64 // Call all hooks that need to be called ( bresenham )
65 for (Hook* hook : this->hooks){
66 hook->countdown -= this->interval;
67 if (hook->countdown < 0)
68 {
69 hook->countdown += hook->interval;
70 hook->call();
71 }
72 }
73
74 // deduct tick time from secound counter
75 flag_1s_count -= this->interval;
76 // if a whole second has elapsed,
77 if (flag_1s_count < 0)
78 {
79 // add a second to our counter
80 flag_1s_count += SystemCoreClock >> 2;
81 // and set a flag for idle event to pick up
82 flag_1s_flag++;
83 }
84
85 // Enter MRI mode if the ISP button is pressed
86 // TODO: This should have it's own module
87 if (ispbtn.get() == 0)
88 __debugbreak();
89
90 }
91
92 bool SlowTicker::flag_1s(){
93 // atomic flag check routine
94 // first disable interrupts
95 __disable_irq();
96 // then check for a flag
97 if (flag_1s_flag)
98 {
99 // if we have a flag, decrement the counter
100 flag_1s_flag--;
101 // re-enable interrupts
102 __enable_irq();
103 // and tell caller that we consumed a flag
104 return true;
105 }
106 // if no flag, re-enable interrupts and return false
107 __enable_irq();
108 return false;
109 }
110
111 #include "gpio.h"
112 extern GPIO leds[];
113 void SlowTicker::on_idle(void*)
114 {
115 static uint16_t ledcnt= 0;
116 if(THEKERNEL->is_using_leds()) {
117 // flash led 3 to show we are alive
118 leds[2]= (ledcnt++ & 0x1000) ? 1 : 0;
119 }
120
121 // if interrupt has set the 1 second flag
122 if (flag_1s())
123 // fire the on_second_tick event
124 THEKERNEL->call_event(ON_SECOND_TICK);
125 }
126
127 extern "C" void TIMER2_IRQHandler (void){
128 if((LPC_TIM2->IR >> 0) & 1){ // If interrupt register set for MR0
129 LPC_TIM2->IR |= 1 << 0; // Reset it
130 }
131 global_slow_ticker->tick();
132 }
133