Commit | Line | Data |
---|---|---|
4cff3ded AW |
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/Kernel.h" | |
11 | #include "libs/Module.h" | |
12 | #include "libs/Config.h" | |
4cff3ded | 13 | #include "libs/nuts_bolts.h" |
ded56b35 | 14 | #include "libs/SlowTicker.h" |
3c132bd0 | 15 | #include "libs/Adc.h" |
0e8b102e | 16 | #include "libs/Digipot.h" |
81b547a1 | 17 | #include "libs/Pauser.h" |
38d375e7 | 18 | #include "libs/StreamOutputPool.h" |
4cff3ded AW |
19 | |
20 | #include "modules/communication/SerialConsole.h" | |
21 | #include "modules/communication/GcodeDispatch.h" | |
22 | #include "modules/robot/Planner.h" | |
23 | #include "modules/robot/Robot.h" | |
24 | #include "modules/robot/Stepper.h" | |
3a4fa0c1 | 25 | #include "modules/robot/Player.h" |
a39e1557 | 26 | #include <malloc.h> |
ded56b35 | 27 | |
4464301d AW |
28 | |
29 | ||
4cff3ded AW |
30 | // List of callback functions, ordered as their corresponding events |
31 | const ModuleCallback kernel_callback_functions[NUMBER_OF_DEFINED_EVENTS] = { | |
32 | &Module::on_main_loop, | |
33 | &Module::on_console_line_received, | |
34 | &Module::on_gcode_received, | |
35 | &Module::on_stepper_wake_up, | |
36 | &Module::on_gcode_execute, | |
37 | &Module::on_speed_change, | |
38 | &Module::on_block_begin, | |
da24d6ae | 39 | &Module::on_block_end, |
befcf5cc AW |
40 | &Module::on_config_reload, |
41 | &Module::on_play, | |
70c25627 L |
42 | &Module::on_pause, |
43 | &Module::on_idle | |
4cff3ded AW |
44 | }; |
45 | ||
8c309ca9 | 46 | #define baud_rate_setting_checksum 10922 |
3c132bd0 | 47 | #define uart0_checksum 16877 |
4cff3ded AW |
48 | |
49 | // The kernel is the central point in Smoothie : it stores modules, and handles event calls | |
50 | Kernel::Kernel(){ | |
ded56b35 | 51 | |
4cff3ded AW |
52 | // Config first, because we need the baud_rate setting before we start serial |
53 | this->config = new Config(); | |
a39e1557 | 54 | |
4cff3ded | 55 | // Serial second, because the other modules might want to say something |
38d375e7 | 56 | this->streams = new StreamOutputPool(); |
4cff3ded | 57 | |
4464301d AW |
58 | //this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number()); |
59 | this->serial = new SerialConsole(p13, p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number()); | |
60 | ||
4cff3ded AW |
61 | this->add_module( this->config ); |
62 | this->add_module( this->serial ); | |
38d375e7 | 63 | |
3c132bd0 AW |
64 | // HAL stuff |
65 | this->slow_ticker = new SlowTicker(); | |
3c132bd0 AW |
66 | this->step_ticker = new StepTicker(); |
67 | this->adc = new Adc(); | |
0e8b102e | 68 | this->digipot = new Digipot(); |
650ed0a8 AW |
69 | |
70 | // LPC17xx-specific | |
71 | NVIC_SetPriority(TIMER0_IRQn, 1); | |
72 | NVIC_SetPriority(TIMER2_IRQn, 2); | |
feb204be AW |
73 | |
74 | // Configure the step ticker | |
75 | int base_stepping_frequency = this->config->value(base_stepping_frequency_checksum )->by_default(100000)->as_number(); | |
76 | double microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum )->by_default(5 )->as_number(); | |
77 | this->step_ticker->set_reset_delay( microseconds_per_step_pulse / 1000000L ); | |
78 | this->step_ticker->set_frequency( base_stepping_frequency ); | |
3c132bd0 | 79 | |
4cff3ded | 80 | // Core modules |
81b547a1 AW |
81 | this->add_module( this->gcode_dispatch = new GcodeDispatch() ); |
82 | this->add_module( this->robot = new Robot() ); | |
83 | this->add_module( this->stepper = new Stepper() ); | |
84 | this->add_module( this->planner = new Planner() ); | |
85 | this->add_module( this->player = new Player() ); | |
86 | this->add_module( this->pauser = new Pauser() ); | |
feb204be AW |
87 | |
88 | ||
4cff3ded AW |
89 | } |
90 | ||
91 | void Kernel::add_module(Module* module){ | |
92 | module->kernel = this; | |
93 | module->on_module_loaded(); | |
b66fb830 | 94 | module->register_for_event(ON_CONFIG_RELOAD); |
4cff3ded AW |
95 | } |
96 | ||
97 | void Kernel::register_for_event(unsigned int id_event, Module* module){ | |
98 | this->hooks[id_event].push_back(module); | |
99 | } | |
100 | ||
101 | void Kernel::call_event(unsigned int id_event){ | |
102 | for(unsigned int i=0; i < this->hooks[id_event].size(); i++){ | |
103 | (this->hooks[id_event][i]->*kernel_callback_functions[id_event])(this); | |
104 | } | |
105 | } | |
106 | ||
107 | void Kernel::call_event(unsigned int id_event, void * argument){ | |
108 | for(unsigned int i=0; i < this->hooks[id_event].size(); i++){ | |
109 | (this->hooks[id_event][i]->*kernel_callback_functions[id_event])(argument); | |
110 | } | |
111 | } |