Commit | Line | Data |
---|---|---|
df27a6a3 | 1 | /* |
4cff3ded AW |
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. | |
df27a6a3 | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
4cff3ded AW |
6 | */ |
7 | ||
4cff3ded AW |
8 | #include "libs/Kernel.h" |
9 | #include "libs/Module.h" | |
10 | #include "libs/Config.h" | |
4cff3ded | 11 | #include "libs/nuts_bolts.h" |
ded56b35 | 12 | #include "libs/SlowTicker.h" |
3c132bd0 | 13 | #include "libs/Adc.h" |
38d375e7 | 14 | #include "libs/StreamOutputPool.h" |
4c4a372a | 15 | #include <mri.h> |
7af0714f | 16 | #include "checksumm.h" |
8d54c34c | 17 | #include "ConfigValue.h" |
4cff3ded | 18 | |
61134a65 JM |
19 | #include "libs/StepTicker.h" |
20 | #include "libs/PublicData.h" | |
4cff3ded AW |
21 | #include "modules/communication/SerialConsole.h" |
22 | #include "modules/communication/GcodeDispatch.h" | |
23 | #include "modules/robot/Planner.h" | |
24 | #include "modules/robot/Robot.h" | |
25 | #include "modules/robot/Stepper.h" | |
3fceb8eb | 26 | #include "modules/robot/Conveyor.h" |
1629be83 | 27 | #include "modules/robot/Pauser.h" |
56ce2b5a | 28 | |
a39e1557 | 29 | #include <malloc.h> |
56ce2b5a | 30 | #include <array> |
ded56b35 | 31 | |
d4f93cf4 AG |
32 | #define baud_rate_setting_checksum CHECKSUM("baud_rate") |
33 | #define uart0_checksum CHECKSUM("uart0") | |
4cff3ded | 34 | |
38bf9a1c JM |
35 | #define base_stepping_frequency_checksum CHECKSUM("base_stepping_frequency") |
36 | #define microseconds_per_step_pulse_checksum CHECKSUM("microseconds_per_step_pulse") | |
a157d099 | 37 | #define acceleration_ticks_per_second_checksum CHECKSUM("acceleration_ticks_per_second") |
38bf9a1c | 38 | |
879341be JM |
39 | Kernel* Kernel::instance; |
40 | ||
7b49793d | 41 | // The kernel is the central point in Smoothie : it stores modules, and handles event calls |
4cff3ded | 42 | Kernel::Kernel(){ |
879341be | 43 | instance= this; // setup the Singleton instance of the kernel |
d4ee6ee2 | 44 | |
2ee2ad19 | 45 | // serial first at fixed baud rate (DEFAULT_SERIAL_BAUD_RATE) so config can report errors to serial |
36463641 | 46 | // Set to UART0, this will be changed to use the same UART as MRI if it's enabled |
2ee2ad19 | 47 | this->serial = new SerialConsole(USBTX, USBRX, DEFAULT_SERIAL_BAUD_RATE); |
577414f6 JM |
48 | |
49 | // Config next, but does not load cache yet | |
a157d099 | 50 | this->config = new Config(); |
a39e1557 | 51 | |
bcb96295 | 52 | // Pre-load the config cache, do after setting up serial so we can report errors to serial |
577414f6 JM |
53 | this->config->config_cache_load(); |
54 | ||
2ee2ad19 | 55 | // now config is loaded we can do normal setup for serial based on config |
577414f6 | 56 | delete this->serial; |
9326040b | 57 | this->serial= NULL; |
a39e1557 | 58 | |
a157d099 | 59 | this->streams = new StreamOutputPool(); |
d4ee6ee2 | 60 | |
75f4581c JM |
61 | this->current_path = "/"; |
62 | ||
b2b0b56d | 63 | // Configure UART depending on MRI config |
bb3b7f09 | 64 | // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands. |
b2b0b56d | 65 | NVIC_SetPriorityGrouping(0); |
f5f88509 | 66 | |
2ee2ad19 | 67 | #if MRI_ENABLE != 0 |
bb3b7f09 SK |
68 | switch( __mriPlatform_CommUartIndex() ) { |
69 | case 0: | |
2ee2ad19 | 70 | this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number()); |
bb3b7f09 SK |
71 | break; |
72 | case 1: | |
2ee2ad19 | 73 | this->serial = new SerialConsole( p13, p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number()); |
bb3b7f09 SK |
74 | break; |
75 | case 2: | |
2ee2ad19 | 76 | this->serial = new SerialConsole( p28, p27, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number()); |
bb3b7f09 SK |
77 | break; |
78 | case 3: | |
2ee2ad19 | 79 | this->serial = new SerialConsole( p9, p10, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number()); |
bb3b7f09 | 80 | break; |
4c4a372a | 81 | } |
2ee2ad19 JM |
82 | #endif |
83 | // default | |
9326040b | 84 | if(this->serial == NULL) { |
2ee2ad19 | 85 | this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number()); |
9326040b | 86 | } |
f5f88509 | 87 | |
93694d6b | 88 | this->add_module( this->config ); |
4cff3ded | 89 | this->add_module( this->serial ); |
38d375e7 | 90 | |
df27a6a3 | 91 | // HAL stuff |
a157d099 | 92 | add_module( this->slow_ticker = new SlowTicker()); |
3eadcfee | 93 | |
a157d099 JM |
94 | this->step_ticker = new StepTicker(); |
95 | this->adc = new Adc(); | |
650ed0a8 | 96 | |
d337942a | 97 | // TODO : These should go into platform-specific files |
df27a6a3 | 98 | // LPC17xx-specific |
813727fb | 99 | NVIC_SetPriorityGrouping(0); |
df27a6a3 MM |
100 | NVIC_SetPriority(TIMER0_IRQn, 2); |
101 | NVIC_SetPriority(TIMER1_IRQn, 1); | |
16220afe JM |
102 | NVIC_SetPriority(TIMER2_IRQn, 4); |
103 | NVIC_SetPriority(PendSV_IRQn, 3); | |
4dfd2dce | 104 | NVIC_SetPriority(RIT_IRQn, 3); // we make acceleration tick the same prio as pendsv so it can't be pre-empted by end of block |
feb204be | 105 | |
b2b0b56d | 106 | // Set other priorities lower than the timers |
16220afe JM |
107 | NVIC_SetPriority(ADC_IRQn, 5); |
108 | NVIC_SetPriority(USB_IRQn, 5); | |
f5f88509 | 109 | |
df27a6a3 | 110 | // If MRI is enabled |
b2b0b56d | 111 | if( MRI_ENABLE ){ |
16220afe JM |
112 | if( NVIC_GetPriority(UART0_IRQn) > 0 ){ NVIC_SetPriority(UART0_IRQn, 5); } |
113 | if( NVIC_GetPriority(UART1_IRQn) > 0 ){ NVIC_SetPriority(UART1_IRQn, 5); } | |
114 | if( NVIC_GetPriority(UART2_IRQn) > 0 ){ NVIC_SetPriority(UART2_IRQn, 5); } | |
115 | if( NVIC_GetPriority(UART3_IRQn) > 0 ){ NVIC_SetPriority(UART3_IRQn, 5); } | |
b2b0b56d | 116 | }else{ |
16220afe JM |
117 | NVIC_SetPriority(UART0_IRQn, 5); |
118 | NVIC_SetPriority(UART1_IRQn, 5); | |
119 | NVIC_SetPriority(UART2_IRQn, 5); | |
120 | NVIC_SetPriority(UART3_IRQn, 5); | |
b2b0b56d AW |
121 | } |
122 | ||
feb204be | 123 | // Configure the step ticker |
a157d099 JM |
124 | this->base_stepping_frequency = this->config->value(base_stepping_frequency_checksum)->by_default(100000)->as_number(); |
125 | float microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum)->by_default(5)->as_number(); | |
126 | this->acceleration_ticks_per_second = THEKERNEL->config->value(acceleration_ticks_per_second_checksum)->by_default(1000)->as_number(); | |
f5f88509 | 127 | |
d337942a | 128 | // Configure the step ticker ( TODO : shouldnt this go into stepticker's code ? ) |
aed1f6ca | 129 | this->step_ticker->set_reset_delay( microseconds_per_step_pulse ); |
dd0a7cfa | 130 | this->step_ticker->set_frequency( this->base_stepping_frequency ); |
a157d099 | 131 | this->step_ticker->set_acceleration_ticks_per_second(acceleration_ticks_per_second); // must be set after set_frequency |
3c132bd0 | 132 | |
df27a6a3 | 133 | // Core modules |
76217df5 | 134 | this->add_module( new GcodeDispatch() ); |
81b547a1 AW |
135 | this->add_module( this->robot = new Robot() ); |
136 | this->add_module( this->stepper = new Stepper() ); | |
663d7943 | 137 | this->add_module( this->conveyor = new Conveyor() ); |
81b547a1 | 138 | this->add_module( this->pauser = new Pauser() ); |
558e170c JM |
139 | |
140 | this->planner = new Planner(); | |
141 | ||
4cff3ded AW |
142 | } |
143 | ||
cb2e6bc6 | 144 | // Add a module to Kernel. We don't actually hold a list of modules we just call its on_module_loaded |
4cff3ded | 145 | void Kernel::add_module(Module* module){ |
4cff3ded AW |
146 | module->on_module_loaded(); |
147 | } | |
148 | ||
93694d6b | 149 | // Adds a hook for a given module and event |
96f67b65 JM |
150 | void Kernel::register_for_event(_EVENT_ENUM id_event, Module *mod){ |
151 | this->hooks[id_event].push_back(mod); | |
4cff3ded AW |
152 | } |
153 | ||
93ea6adb JM |
154 | // Call a specific event with an argument |
155 | void Kernel::call_event(_EVENT_ENUM id_event, void * argument){ | |
96f67b65 | 156 | for (auto m : hooks[id_event]) { |
93ea6adb | 157 | (m->*kernel_callback_functions[id_event])(argument); |
4cff3ded AW |
158 | } |
159 | } | |
160 | ||
93ea6adb JM |
161 | // These are used by tests to test for various things. basically mocks |
162 | bool Kernel::kernel_has_event(_EVENT_ENUM id_event, Module *mod) | |
163 | { | |
96f67b65 | 164 | for (auto m : hooks[id_event]) { |
93ea6adb | 165 | if(m == mod) return true; |
4cff3ded | 166 | } |
93ea6adb | 167 | return false; |
4cff3ded | 168 | } |
93ea6adb JM |
169 | |
170 | void Kernel::unregister_for_event(_EVENT_ENUM id_event, Module *mod) | |
171 | { | |
172 | for (auto i = hooks[id_event].begin(); i != hooks[id_event].end(); ++i) { | |
173 | if(*i == mod) { | |
174 | hooks[id_event].erase(i); | |
175 | return; | |
176 | } | |
177 | } | |
178 | } | |
179 |