add debug pins to makefile, ignored if not set
[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 number_of_motors_checksum CHECKSUM("number_of_motors")
38
39 Kernel* Kernel::instance;
40
41 // The kernel is the central point in Smoothie : it stores modules, and handles event calls
42 Kernel::Kernel(){
43 instance= this; // setup the Singleton instance of the kernel
44
45 // serial first at fixed baud rate (DEFAULT_SERIAL_BAUD_RATE) so config can report errors to serial
46 // Set to UART0, this will be changed to use the same UART as MRI if it's enabled
47 this->serial = new SerialConsole(USBTX, USBRX, DEFAULT_SERIAL_BAUD_RATE);
48
49 // Config next, but does not load cache yet
50 this->config = new Config();
51
52 // Pre-load the config cache, do after setting up serial so we can report errors to serial
53 this->config->config_cache_load();
54
55 // now config is loaded we can do normal setup for serial based on config
56 delete this->serial;
57 this->serial= NULL;
58
59 this->streams = new StreamOutputPool();
60
61 this->current_path = "/";
62
63 // Configure UART depending on MRI config
64 // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands.
65 NVIC_SetPriorityGrouping(0);
66
67 #if MRI_ENABLE != 0
68 switch( __mriPlatform_CommUartIndex() ) {
69 case 0:
70 this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
71 break;
72 case 1:
73 this->serial = new SerialConsole( p13, p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
74 break;
75 case 2:
76 this->serial = new SerialConsole( p28, p27, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
77 break;
78 case 3:
79 this->serial = new SerialConsole( p9, p10, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
80 break;
81 }
82 #endif
83 // default
84 if(this->serial == NULL) {
85 this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
86 }
87
88 this->add_module( this->config );
89 this->add_module( this->serial );
90
91 // HAL stuff
92 add_module( this->slow_ticker = new SlowTicker());
93
94 int nmotors= this->config->value(number_of_motors_checksum)->by_default(5)->as_number(); // default X Y Z E1 E2
95 this->step_ticker = new StepTicker(nmotors);
96 this->adc = new Adc();
97
98 // TODO : These should go into platform-specific files
99 // LPC17xx-specific
100 NVIC_SetPriorityGrouping(0);
101 NVIC_SetPriority(TIMER0_IRQn, 2);
102 NVIC_SetPriority(TIMER1_IRQn, 1);
103 NVIC_SetPriority(TIMER2_IRQn, 3);
104
105 // Set other priorities lower than the timers
106 NVIC_SetPriority(ADC_IRQn, 4);
107 NVIC_SetPriority(USB_IRQn, 4);
108
109 // If MRI is enabled
110 if( MRI_ENABLE ){
111 if( NVIC_GetPriority(UART0_IRQn) > 0 ){ NVIC_SetPriority(UART0_IRQn, 4); }
112 if( NVIC_GetPriority(UART1_IRQn) > 0 ){ NVIC_SetPriority(UART1_IRQn, 4); }
113 if( NVIC_GetPriority(UART2_IRQn) > 0 ){ NVIC_SetPriority(UART2_IRQn, 4); }
114 if( NVIC_GetPriority(UART3_IRQn) > 0 ){ NVIC_SetPriority(UART3_IRQn, 4); }
115 }else{
116 NVIC_SetPriority(UART0_IRQn, 4);
117 NVIC_SetPriority(UART1_IRQn, 4);
118 NVIC_SetPriority(UART2_IRQn, 4);
119 NVIC_SetPriority(UART3_IRQn, 4);
120 }
121
122 // Configure the step ticker
123 this->base_stepping_frequency = this->config->value(base_stepping_frequency_checksum )->by_default(100000)->as_number();
124 float microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum )->by_default(5 )->as_number();
125
126 // Configure the step ticker ( TODO : shouldnt this go into stepticker's code ? )
127 this->step_ticker->set_reset_delay( microseconds_per_step_pulse / 1000000.0F );
128 this->step_ticker->set_frequency( this->base_stepping_frequency );
129
130 // Core modules
131 this->add_module( new GcodeDispatch() );
132 this->add_module( this->robot = new Robot() );
133 this->add_module( this->stepper = new Stepper() );
134 this->add_module( this->conveyor = new Conveyor() );
135 this->add_module( this->pauser = new Pauser() );
136
137 this->planner = new Planner();
138
139 }
140
141 // Add a module to Kernel. We don't actually hold a list of modules, we just tell it where Kernel is
142 void Kernel::add_module(Module* module){
143 module->on_module_loaded();
144 }
145
146 // Adds a hook for a given module and event
147 void Kernel::register_for_event(_EVENT_ENUM id_event, Module *mod){
148 this->hooks[id_event].push_back(mod);
149 }
150
151 // Call a specific event without arguments
152 void Kernel::call_event(_EVENT_ENUM id_event){
153 for (auto m : hooks[id_event]) {
154 (m->*kernel_callback_functions[id_event])(this);
155 }
156 }
157
158 // Call a specific event with an argument
159 void Kernel::call_event(_EVENT_ENUM id_event, void * argument){
160 for (auto m : hooks[id_event]) {
161 (m->*kernel_callback_functions[id_event])(argument);
162 }
163 }