| 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 | // 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. |
| 21 | |
| 22 | SlowTicker* global_slow_ticker; |
| 23 | |
| 24 | SlowTicker::SlowTicker(){ |
| 25 | max_frequency = 0; |
| 26 | global_slow_ticker = this; |
| 27 | |
| 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 |
| 34 | |
| 35 | // ISP button |
| 36 | ispbtn.from_string("2.10")->as_input()->pull_up(); |
| 37 | |
| 38 | // TODO: What is this ?? |
| 39 | flag_1s_flag = 0; |
| 40 | flag_1s_count = SystemCoreClock>>2; |
| 41 | |
| 42 | g4_ticks = 0; |
| 43 | g4_pause = false; |
| 44 | } |
| 45 | |
| 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); |
| 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 (uint32_t i=0; i<this->hooks.size(); i++){ |
| 66 | Hook* hook = this->hooks.at(i); |
| 67 | hook->countdown -= this->interval; |
| 68 | if (hook->countdown < 0) |
| 69 | { |
| 70 | hook->countdown += hook->interval; |
| 71 | hook->call(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // deduct tick time from secound counter |
| 76 | flag_1s_count -= this->interval; |
| 77 | // if a whole second has elapsed, |
| 78 | if (flag_1s_count < 0) |
| 79 | { |
| 80 | // add a second to our counter |
| 81 | flag_1s_count += SystemCoreClock >> 2; |
| 82 | // and set a flag for idle event to pick up |
| 83 | flag_1s_flag++; |
| 84 | } |
| 85 | |
| 86 | // if we're counting down a pause |
| 87 | if (g4_ticks > 0) |
| 88 | { |
| 89 | // deduct tick time from timeout |
| 90 | if (g4_ticks > interval) |
| 91 | g4_ticks -= interval; |
| 92 | else |
| 93 | g4_ticks = 0; |
| 94 | } |
| 95 | |
| 96 | // Enter MRI mode if the ISP button is pressed |
| 97 | // TODO: This should have it's own module |
| 98 | if (ispbtn.get() == 0) |
| 99 | __debugbreak(); |
| 100 | |
| 101 | } |
| 102 | |
| 103 | bool SlowTicker::flag_1s(){ |
| 104 | // atomic flag check routine |
| 105 | // first disable interrupts |
| 106 | __disable_irq(); |
| 107 | // then check for a flag |
| 108 | if (flag_1s_flag) |
| 109 | { |
| 110 | // if we have a flag, decrement the counter |
| 111 | flag_1s_flag--; |
| 112 | // re-enable interrupts |
| 113 | __enable_irq(); |
| 114 | // and tell caller that we consumed a flag |
| 115 | return true; |
| 116 | } |
| 117 | // if no flag, re-enable interrupts and return false |
| 118 | __enable_irq(); |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | #include "gpio.h" |
| 123 | extern GPIO leds[]; |
| 124 | void SlowTicker::on_idle(void*) |
| 125 | { |
| 126 | static uint16_t ledcnt= 0; |
| 127 | if(kernel->use_leds) { |
| 128 | // flash led 3 to show we are alive |
| 129 | leds[2]= (ledcnt++ & 0x1000) ? 1 : 0; |
| 130 | } |
| 131 | |
| 132 | // if interrupt has set the 1 second flag |
| 133 | if (flag_1s()) |
| 134 | // fire the on_second_tick event |
| 135 | kernel->call_event(ON_SECOND_TICK); |
| 136 | |
| 137 | // if G4 has finished, release our pause |
| 138 | if (g4_pause && (g4_ticks == 0)) |
| 139 | { |
| 140 | g4_pause = false; |
| 141 | kernel->pauser->release(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // When a G4-type gcode is received, add it to the queue so we can execute it in time |
| 146 | void SlowTicker::on_gcode_received(void* argument){ |
| 147 | Gcode* gcode = static_cast<Gcode*>(argument); |
| 148 | // Add the gcode to the queue ourselves if we need it |
| 149 | if( gcode->has_g && gcode->g == 4 ){ |
| 150 | if( THEKERNEL->conveyor->queue.size() == 0 ){ |
| 151 | THEKERNEL->call_event(ON_GCODE_EXECUTE, gcode ); |
| 152 | }else{ |
| 153 | Block* block = THEKERNEL->conveyor->queue.get_ref( THEKERNEL->conveyor->queue.size() - 1 ); |
| 154 | block->append_gcode(gcode); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // When a G4-type gcode is executed, start the pause |
| 160 | void SlowTicker::on_gcode_execute(void* argument){ |
| 161 | Gcode* gcode = static_cast<Gcode*>(argument); |
| 162 | |
| 163 | if (gcode->has_g){ |
| 164 | if (gcode->g == 4){ |
| 165 | gcode->mark_as_taken(); |
| 166 | bool updated = false; |
| 167 | if (gcode->has_letter('P')) { |
| 168 | updated = true; |
| 169 | g4_ticks += gcode->get_int('P') * ((SystemCoreClock >> 2) / 1000UL); |
| 170 | } |
| 171 | if (gcode->has_letter('S')) { |
| 172 | updated = true; |
| 173 | g4_ticks += gcode->get_int('S') * (SystemCoreClock >> 2); |
| 174 | } |
| 175 | if (updated){ |
| 176 | // G4 Smm Pnn should pause for mm seconds + nn milliseconds |
| 177 | // at 120MHz core clock, the longest possible delay is (2^32 / (120MHz / 4)) = 143 seconds |
| 178 | if (!g4_pause){ |
| 179 | g4_pause = true; |
| 180 | kernel->pauser->take(); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | extern "C" void TIMER2_IRQHandler (void){ |
| 188 | if((LPC_TIM2->IR >> 0) & 1){ // If interrupt register set for MR0 |
| 189 | LPC_TIM2->IR |= 1 << 0; // Reset it |
| 190 | } |
| 191 | global_slow_ticker->tick(); |
| 192 | } |
| 193 | |