putting Endstops back into main.cpp
[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/Pauser.h"
15 #include "libs/StreamOutputPool.h"
16 #include <mri.h>
17
18 #include "modules/communication/SerialConsole.h"
19 #include "modules/communication/GcodeDispatch.h"
20 #include "modules/robot/Planner.h"
21 #include "modules/robot/Robot.h"
22 #include "modules/robot/Stepper.h"
23 #include "modules/robot/Conveyor.h"
24 #include "modules/tools/endstops/Endstops.h"
25 #include <malloc.h>
26
27
28
29 #define baud_rate_setting_checksum CHECKSUM("baud_rate")
30 #define uart0_checksum CHECKSUM("uart0")
31
32 static int isDebugMonitorUsingUart0()
33 {
34 return NVIC_GetPriority(UART0_IRQn) == 0;
35 }
36
37 // The kernel is the central point in Smoothie : it stores modules, and handles event calls
38 Kernel::Kernel(){
39 // Config first, because we need the baud_rate setting before we start serial
40 this->config = new Config();
41
42 // Serial second, because the other modules might want to say something
43 this->streams = new StreamOutputPool();
44
45 // Configure UART depending on MRI config
46 NVIC_SetPriorityGrouping(0);
47 if( !isDebugMonitorUsingUart0() ){
48 this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number());
49 }else{
50 this->serial = new SerialConsole(p13, p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number());
51 }
52 this->add_module( this->config );
53
54 this->add_module( this->serial );
55
56 // HAL stuff
57 add_module( this->slow_ticker = new SlowTicker());
58 this->step_ticker = new StepTicker();
59 this->adc = new Adc();
60
61 // LPC17xx-specific
62 NVIC_SetPriorityGrouping(0);
63 NVIC_SetPriority(TIMER0_IRQn, 2);
64 NVIC_SetPriority(TIMER1_IRQn, 1);
65 NVIC_SetPriority(TIMER2_IRQn, 3);
66
67 // Set other priorities lower than the timers
68 NVIC_SetPriority(ADC_IRQn, 4);
69 NVIC_SetPriority(USB_IRQn, 4);
70
71 // If MRI is enabled
72 if( MRI_ENABLE ){
73 if( NVIC_GetPriority(UART0_IRQn) > 0 ){ NVIC_SetPriority(UART0_IRQn, 4); }
74 if( NVIC_GetPriority(UART1_IRQn) > 0 ){ NVIC_SetPriority(UART1_IRQn, 4); }
75 if( NVIC_GetPriority(UART2_IRQn) > 0 ){ NVIC_SetPriority(UART2_IRQn, 4); }
76 if( NVIC_GetPriority(UART3_IRQn) > 0 ){ NVIC_SetPriority(UART3_IRQn, 4); }
77 }else{
78 NVIC_SetPriority(UART0_IRQn, 4);
79 NVIC_SetPriority(UART1_IRQn, 4);
80 NVIC_SetPriority(UART2_IRQn, 4);
81 NVIC_SetPriority(UART3_IRQn, 4);
82 }
83
84 // Configure the step ticker
85 int base_stepping_frequency = this->config->value(base_stepping_frequency_checksum )->by_default(100000)->as_number();
86 double microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum )->by_default(5 )->as_number();
87
88 this->step_ticker->set_reset_delay( microseconds_per_step_pulse / 1000000L );
89
90 this->step_ticker->set_frequency( base_stepping_frequency );
91
92 // Core modules
93 this->add_module( this->gcode_dispatch = new GcodeDispatch() );
94 this->add_module( this->robot = new Robot() );
95 this->add_module( this->stepper = new Stepper() );
96 this->add_module( this->planner = new Planner() );
97 this->add_module( this->conveyor = new Conveyor() );
98 this->add_module( this->pauser = new Pauser() );
99
100 }
101
102 void Kernel::add_module(Module* module){
103 module->kernel = this;
104 module->on_module_loaded();
105 }
106
107 void Kernel::register_for_event(_EVENT_ENUM id_event, Module* module){
108 this->hooks[id_event].push_back(module);
109 }
110
111 void Kernel::call_event(_EVENT_ENUM id_event){
112 for (Module* current : hooks[id_event]) {
113 (current->*kernel_callback_functions[id_event])(this);
114 }
115 }
116
117 void Kernel::call_event(_EVENT_ENUM id_event, void * argument){
118 for (Module* current : hooks[id_event]) {
119 (current->*kernel_callback_functions[id_event])(argument);
120 }
121 }