Fork of Smoothie to port to mbed non-LPC targets.
Fork of Smoothie by
libs/Kernel.cpp@0:31e91bb0ef3c, 2012-07-31 (annotated)
- Committer:
- scachat
- Date:
- Tue Jul 31 21:11:18 2012 +0000
- Revision:
- 0:31e91bb0ef3c
- Child:
- 2:1df0b61d3b5a
smoothie port to mbed online compiler
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scachat | 0:31e91bb0ef3c | 1 | /* |
scachat | 0:31e91bb0ef3c | 2 | This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). |
scachat | 0:31e91bb0ef3c | 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. |
scachat | 0:31e91bb0ef3c | 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. |
scachat | 0:31e91bb0ef3c | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
scachat | 0:31e91bb0ef3c | 6 | */ |
scachat | 0:31e91bb0ef3c | 7 | |
scachat | 0:31e91bb0ef3c | 8 | using namespace std; |
scachat | 0:31e91bb0ef3c | 9 | #include <vector> |
scachat | 0:31e91bb0ef3c | 10 | #include "libs/Kernel.h" |
scachat | 0:31e91bb0ef3c | 11 | #include "libs/Module.h" |
scachat | 0:31e91bb0ef3c | 12 | #include "libs/Config.h" |
scachat | 0:31e91bb0ef3c | 13 | #include "libs/nuts_bolts.h" |
scachat | 0:31e91bb0ef3c | 14 | #include "libs/SlowTicker.h" |
scachat | 0:31e91bb0ef3c | 15 | #include "libs/Adc.h" |
scachat | 0:31e91bb0ef3c | 16 | #include "libs/Digipot.h" |
scachat | 0:31e91bb0ef3c | 17 | #include "libs/Pauser.h" |
scachat | 0:31e91bb0ef3c | 18 | |
scachat | 0:31e91bb0ef3c | 19 | #include "modules/communication/SerialConsole.h" |
scachat | 0:31e91bb0ef3c | 20 | #include "modules/communication/GcodeDispatch.h" |
scachat | 0:31e91bb0ef3c | 21 | #include "modules/robot/Planner.h" |
scachat | 0:31e91bb0ef3c | 22 | #include "modules/robot/Robot.h" |
scachat | 0:31e91bb0ef3c | 23 | #include "modules/robot/Stepper.h" |
scachat | 0:31e91bb0ef3c | 24 | #include "modules/robot/Player.h" |
scachat | 0:31e91bb0ef3c | 25 | |
scachat | 0:31e91bb0ef3c | 26 | |
scachat | 0:31e91bb0ef3c | 27 | // List of callback functions, ordered as their corresponding events |
scachat | 0:31e91bb0ef3c | 28 | const ModuleCallback kernel_callback_functions[NUMBER_OF_DEFINED_EVENTS] = { |
scachat | 0:31e91bb0ef3c | 29 | &Module::on_main_loop, |
scachat | 0:31e91bb0ef3c | 30 | &Module::on_console_line_received, |
scachat | 0:31e91bb0ef3c | 31 | &Module::on_gcode_received, |
scachat | 0:31e91bb0ef3c | 32 | &Module::on_stepper_wake_up, |
scachat | 0:31e91bb0ef3c | 33 | &Module::on_gcode_execute, |
scachat | 0:31e91bb0ef3c | 34 | &Module::on_speed_change, |
scachat | 0:31e91bb0ef3c | 35 | &Module::on_block_begin, |
scachat | 0:31e91bb0ef3c | 36 | &Module::on_block_end, |
scachat | 0:31e91bb0ef3c | 37 | &Module::on_config_reload, |
scachat | 0:31e91bb0ef3c | 38 | &Module::on_play, |
scachat | 0:31e91bb0ef3c | 39 | &Module::on_pause, |
scachat | 0:31e91bb0ef3c | 40 | &Module::on_idle |
scachat | 0:31e91bb0ef3c | 41 | }; |
scachat | 0:31e91bb0ef3c | 42 | |
scachat | 0:31e91bb0ef3c | 43 | #define baud_rate_setting_checksum 10922 |
scachat | 0:31e91bb0ef3c | 44 | #define uart0_checksum 16877 |
scachat | 0:31e91bb0ef3c | 45 | |
scachat | 0:31e91bb0ef3c | 46 | // The kernel is the central point in Smoothie : it stores modules, and handles event calls |
scachat | 0:31e91bb0ef3c | 47 | Kernel::Kernel(){ |
scachat | 0:31e91bb0ef3c | 48 | |
scachat | 0:31e91bb0ef3c | 49 | // Config first, because we need the baud_rate setting before we start serial |
scachat | 0:31e91bb0ef3c | 50 | this->config = new Config(); |
scachat | 0:31e91bb0ef3c | 51 | |
scachat | 0:31e91bb0ef3c | 52 | // Serial second, because the other modules might want to say something |
scachat | 0:31e91bb0ef3c | 53 | this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number()); |
scachat | 0:31e91bb0ef3c | 54 | |
scachat | 0:31e91bb0ef3c | 55 | this->add_module( this->config ); |
scachat | 0:31e91bb0ef3c | 56 | this->add_module( this->serial ); |
scachat | 0:31e91bb0ef3c | 57 | |
scachat | 0:31e91bb0ef3c | 58 | // HAL stuff |
scachat | 0:31e91bb0ef3c | 59 | this->slow_ticker = new SlowTicker(); |
scachat | 0:31e91bb0ef3c | 60 | this->step_ticker = new StepTicker(); |
scachat | 0:31e91bb0ef3c | 61 | this->adc = new Adc(); |
scachat | 0:31e91bb0ef3c | 62 | this->digipot = new Digipot(); |
scachat | 0:31e91bb0ef3c | 63 | |
scachat | 0:31e91bb0ef3c | 64 | // LPC17xx-specific |
scachat | 0:31e91bb0ef3c | 65 | NVIC_SetPriority(TIMER0_IRQn, 1); |
scachat | 0:31e91bb0ef3c | 66 | NVIC_SetPriority(TIMER2_IRQn, 2); |
scachat | 0:31e91bb0ef3c | 67 | |
scachat | 0:31e91bb0ef3c | 68 | // Core modules |
scachat | 0:31e91bb0ef3c | 69 | this->add_module( this->gcode_dispatch = new GcodeDispatch() ); |
scachat | 0:31e91bb0ef3c | 70 | this->add_module( this->robot = new Robot() ); |
scachat | 0:31e91bb0ef3c | 71 | this->add_module( this->stepper = new Stepper() ); |
scachat | 0:31e91bb0ef3c | 72 | this->add_module( this->planner = new Planner() ); |
scachat | 0:31e91bb0ef3c | 73 | this->add_module( this->player = new Player() ); |
scachat | 0:31e91bb0ef3c | 74 | this->add_module( this->pauser = new Pauser() ); |
scachat | 0:31e91bb0ef3c | 75 | } |
scachat | 0:31e91bb0ef3c | 76 | |
scachat | 0:31e91bb0ef3c | 77 | void Kernel::add_module(Module* module){ |
scachat | 0:31e91bb0ef3c | 78 | module->kernel = this; |
scachat | 0:31e91bb0ef3c | 79 | module->on_module_loaded(); |
scachat | 0:31e91bb0ef3c | 80 | module->register_for_event(ON_CONFIG_RELOAD); |
scachat | 0:31e91bb0ef3c | 81 | } |
scachat | 0:31e91bb0ef3c | 82 | |
scachat | 0:31e91bb0ef3c | 83 | void Kernel::register_for_event(unsigned int id_event, Module* module){ |
scachat | 0:31e91bb0ef3c | 84 | this->hooks[id_event].push_back(module); |
scachat | 0:31e91bb0ef3c | 85 | } |
scachat | 0:31e91bb0ef3c | 86 | |
scachat | 0:31e91bb0ef3c | 87 | void Kernel::call_event(unsigned int id_event){ |
scachat | 0:31e91bb0ef3c | 88 | for(unsigned int i=0; i < this->hooks[id_event].size(); i++){ |
scachat | 0:31e91bb0ef3c | 89 | (this->hooks[id_event][i]->*kernel_callback_functions[id_event])(this); |
scachat | 0:31e91bb0ef3c | 90 | } |
scachat | 0:31e91bb0ef3c | 91 | } |
scachat | 0:31e91bb0ef3c | 92 | |
scachat | 0:31e91bb0ef3c | 93 | void Kernel::call_event(unsigned int id_event, void * argument){ |
scachat | 0:31e91bb0ef3c | 94 | for(unsigned int i=0; i < this->hooks[id_event].size(); i++){ |
scachat | 0:31e91bb0ef3c | 95 | (this->hooks[id_event][i]->*kernel_callback_functions[id_event])(argument); |
scachat | 0:31e91bb0ef3c | 96 | } |
scachat | 0:31e91bb0ef3c | 97 | } |