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/>.
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"
14 #include "libs/Pauser.h"
15 #include "libs/StreamOutputPool.h"
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"
27 #define baud_rate_setting_checksum CHECKSUM("baud_rate")
28 #define uart0_checksum CHECKSUM("uart0")
30 Kernel
* Kernel::instance
;
32 // The kernel is the central point in Smoothie : it stores modules, and handles event calls
34 instance
= this; // setup the Singleton instance of the kernel
36 // Config first, because we need the baud_rate setting before we start serial
37 this->config
= new Config();
39 // Serial second, because the other modules might want to say something
40 this->streams
= new StreamOutputPool();
42 this->current_path
= "/";
44 // Configure UART depending on MRI config
45 // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands.
46 NVIC_SetPriorityGrouping(0);
47 switch( __mriPlatform_CommUartIndex() ) {
49 this->serial
= new SerialConsole(USBTX
, USBRX
, this->config
->value(uart0_checksum
,baud_rate_setting_checksum
)->by_default(9600)->as_number());
52 this->serial
= new SerialConsole( p13
, p14
, this->config
->value(uart0_checksum
,baud_rate_setting_checksum
)->by_default(9600)->as_number());
55 this->serial
= new SerialConsole( p28
, p27
, this->config
->value(uart0_checksum
,baud_rate_setting_checksum
)->by_default(9600)->as_number());
58 this->serial
= new SerialConsole( p9
, p10
, this->config
->value(uart0_checksum
,baud_rate_setting_checksum
)->by_default(9600)->as_number());
62 this->add_module( this->config
);
63 this->add_module( this->serial
);
66 add_module( this->slow_ticker
= new SlowTicker());
67 this->step_ticker
= new StepTicker();
68 this->adc
= new Adc();
70 // TODO : These should go into platform-specific files
72 NVIC_SetPriorityGrouping(0);
73 NVIC_SetPriority(TIMER0_IRQn
, 2);
74 NVIC_SetPriority(TIMER1_IRQn
, 1);
75 NVIC_SetPriority(TIMER2_IRQn
, 3);
77 // Set other priorities lower than the timers
78 NVIC_SetPriority(ADC_IRQn
, 4);
79 NVIC_SetPriority(USB_IRQn
, 4);
83 if( NVIC_GetPriority(UART0_IRQn
) > 0 ){ NVIC_SetPriority(UART0_IRQn
, 4); }
84 if( NVIC_GetPriority(UART1_IRQn
) > 0 ){ NVIC_SetPriority(UART1_IRQn
, 4); }
85 if( NVIC_GetPriority(UART2_IRQn
) > 0 ){ NVIC_SetPriority(UART2_IRQn
, 4); }
86 if( NVIC_GetPriority(UART3_IRQn
) > 0 ){ NVIC_SetPriority(UART3_IRQn
, 4); }
88 NVIC_SetPriority(UART0_IRQn
, 4);
89 NVIC_SetPriority(UART1_IRQn
, 4);
90 NVIC_SetPriority(UART2_IRQn
, 4);
91 NVIC_SetPriority(UART3_IRQn
, 4);
94 // Configure the step ticker
95 int base_stepping_frequency
= this->config
->value(base_stepping_frequency_checksum
)->by_default(100000)->as_number();
96 float microseconds_per_step_pulse
= this->config
->value(microseconds_per_step_pulse_checksum
)->by_default(5 )->as_number();
98 // Configure the step ticker ( TODO : shouldnt this go into stepticker's code ? )
99 this->step_ticker
->set_reset_delay( microseconds_per_step_pulse
/ 1000000L );
100 this->step_ticker
->set_frequency( base_stepping_frequency
);
103 this->add_module( this->gcode_dispatch
= new GcodeDispatch() );
104 this->add_module( this->robot
= new Robot() );
105 this->add_module( this->stepper
= new Stepper() );
106 this->add_module( this->planner
= new Planner() );
107 this->add_module( this->conveyor
= new Conveyor() );
108 this->add_module( this->pauser
= new Pauser() );
109 this->add_module( this->public_data
= new PublicData() );
110 this->add_module( this->toolsmanager
= new ToolsManager() );
114 // Add a module to Kernel. We don't actually hold a list of modules, we just tell it where Kernel is
115 void Kernel::add_module(Module
* module
){
116 module
->on_module_loaded();
119 // Adds a hook for a given module and event
120 void Kernel::register_for_event(_EVENT_ENUM id_event
, Module
* module
){
121 this->hooks
[id_event
].push_back(module
);
124 // Call a specific event without arguments
125 void Kernel::call_event(_EVENT_ENUM id_event
){
126 for (Module
* current
: hooks
[id_event
]) {
127 (current
->*kernel_callback_functions
[id_event
])(this);
131 // Call a specific event with an argument
132 void Kernel::call_event(_EVENT_ENUM id_event
, void * argument
){
133 for (Module
* current
: hooks
[id_event
]) {
134 (current
->*kernel_callback_functions
[id_event
])(argument
);