refactor ON_HALT, add THEKERNEL->is_halted() for modules that just need to test it...
[clinton/Smoothieware.git] / src / libs / Kernel.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 #include "libs/Kernel.h"
9 #include "libs/Module.h"
10 #include "libs/Config.h"
11 #include "libs/nuts_bolts.h"
12 #include "libs/SlowTicker.h"
13 #include "libs/Adc.h"
14 #include "libs/StreamOutputPool.h"
15 #include <mri.h>
16 #include "checksumm.h"
17 #include "ConfigValue.h"
18
19 #include "libs/StepTicker.h"
20 #include "libs/PublicData.h"
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"
26 #include "modules/robot/Conveyor.h"
27 #include "modules/robot/Pauser.h"
28
29 #include <malloc.h>
30 #include <array>
31
32 #define baud_rate_setting_checksum CHECKSUM("baud_rate")
33 #define uart0_checksum CHECKSUM("uart0")
34
35 #define base_stepping_frequency_checksum CHECKSUM("base_stepping_frequency")
36 #define microseconds_per_step_pulse_checksum CHECKSUM("microseconds_per_step_pulse")
37 #define acceleration_ticks_per_second_checksum CHECKSUM("acceleration_ticks_per_second")
38 #define disable_leds_checksum CHECKSUM("leds_disable")
39
40 Kernel* Kernel::instance;
41
42 // The kernel is the central point in Smoothie : it stores modules, and handles event calls
43 Kernel::Kernel(){
44 halted= false;
45
46 instance= this; // setup the Singleton instance of the kernel
47
48 // serial first at fixed baud rate (DEFAULT_SERIAL_BAUD_RATE) so config can report errors to serial
49 // Set to UART0, this will be changed to use the same UART as MRI if it's enabled
50 this->serial = new SerialConsole(USBTX, USBRX, DEFAULT_SERIAL_BAUD_RATE);
51
52 // Config next, but does not load cache yet
53 this->config = new Config();
54
55 // Pre-load the config cache, do after setting up serial so we can report errors to serial
56 this->config->config_cache_load();
57
58 // now config is loaded we can do normal setup for serial based on config
59 delete this->serial;
60 this->serial= NULL;
61
62 this->streams = new StreamOutputPool();
63
64 this->current_path = "/";
65
66 // Configure UART depending on MRI config
67 // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands.
68 NVIC_SetPriorityGrouping(0);
69
70 #if MRI_ENABLE != 0
71 switch( __mriPlatform_CommUartIndex() ) {
72 case 0:
73 this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
74 break;
75 case 1:
76 this->serial = new SerialConsole( p13, p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
77 break;
78 case 2:
79 this->serial = new SerialConsole( p28, p27, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
80 break;
81 case 3:
82 this->serial = new SerialConsole( p9, p10, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
83 break;
84 }
85 #endif
86 // default
87 if(this->serial == NULL) {
88 this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
89 }
90
91 //some boards don't have leds.. TOO BAD!
92 this->use_leds= !this->config->value( disable_leds_checksum )->by_default(false)->as_bool();
93
94 this->add_module( this->config );
95 this->add_module( this->serial );
96
97 // HAL stuff
98 add_module( this->slow_ticker = new SlowTicker());
99
100 this->step_ticker = new StepTicker();
101 this->adc = new Adc();
102
103 // TODO : These should go into platform-specific files
104 // LPC17xx-specific
105 NVIC_SetPriorityGrouping(0);
106 NVIC_SetPriority(TIMER0_IRQn, 2);
107 NVIC_SetPriority(TIMER1_IRQn, 1);
108 NVIC_SetPriority(TIMER2_IRQn, 4);
109 NVIC_SetPriority(PendSV_IRQn, 3);
110 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
111
112 // Set other priorities lower than the timers
113 NVIC_SetPriority(ADC_IRQn, 5);
114 NVIC_SetPriority(USB_IRQn, 5);
115
116 // If MRI is enabled
117 if( MRI_ENABLE ){
118 if( NVIC_GetPriority(UART0_IRQn) > 0 ){ NVIC_SetPriority(UART0_IRQn, 5); }
119 if( NVIC_GetPriority(UART1_IRQn) > 0 ){ NVIC_SetPriority(UART1_IRQn, 5); }
120 if( NVIC_GetPriority(UART2_IRQn) > 0 ){ NVIC_SetPriority(UART2_IRQn, 5); }
121 if( NVIC_GetPriority(UART3_IRQn) > 0 ){ NVIC_SetPriority(UART3_IRQn, 5); }
122 }else{
123 NVIC_SetPriority(UART0_IRQn, 5);
124 NVIC_SetPriority(UART1_IRQn, 5);
125 NVIC_SetPriority(UART2_IRQn, 5);
126 NVIC_SetPriority(UART3_IRQn, 5);
127 }
128
129 // Configure the step ticker
130 this->base_stepping_frequency = this->config->value(base_stepping_frequency_checksum)->by_default(100000)->as_number();
131 float microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum)->by_default(5)->as_number();
132 this->acceleration_ticks_per_second = THEKERNEL->config->value(acceleration_ticks_per_second_checksum)->by_default(1000)->as_number();
133
134 // Configure the step ticker ( TODO : shouldnt this go into stepticker's code ? )
135 this->step_ticker->set_reset_delay( microseconds_per_step_pulse );
136 this->step_ticker->set_frequency( this->base_stepping_frequency );
137 this->step_ticker->set_acceleration_ticks_per_second(acceleration_ticks_per_second); // must be set after set_frequency
138
139 // Core modules
140 this->add_module( new GcodeDispatch() );
141 this->add_module( this->robot = new Robot() );
142 this->add_module( this->stepper = new Stepper() );
143 this->add_module( this->conveyor = new Conveyor() );
144 this->add_module( this->pauser = new Pauser() );
145
146 this->planner = new Planner();
147
148 }
149
150 // Add a module to Kernel. We don't actually hold a list of modules we just call its on_module_loaded
151 void Kernel::add_module(Module* module){
152 module->on_module_loaded();
153 }
154
155 // Adds a hook for a given module and event
156 void Kernel::register_for_event(_EVENT_ENUM id_event, Module *mod){
157 this->hooks[id_event].push_back(mod);
158 }
159
160 // Call a specific event with an argument
161 void Kernel::call_event(_EVENT_ENUM id_event, void * argument){
162 if(id_event == ON_HALT) {
163 this->halted= (argument == nullptr);
164 }
165 for (auto m : hooks[id_event]) {
166 (m->*kernel_callback_functions[id_event])(argument);
167 }
168 }
169
170 // These are used by tests to test for various things. basically mocks
171 bool Kernel::kernel_has_event(_EVENT_ENUM id_event, Module *mod)
172 {
173 for (auto m : hooks[id_event]) {
174 if(m == mod) return true;
175 }
176 return false;
177 }
178
179 void Kernel::unregister_for_event(_EVENT_ENUM id_event, Module *mod)
180 {
181 for (auto i = hooks[id_event].begin(); i != hooks[id_event].end(); ++i) {
182 if(*i == mod) {
183 hooks[id_event].erase(i);
184 return;
185 }
186 }
187 }
188