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