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"
29 #define baud_rate_setting_checksum CHECKSUM("baud_rate")
30 #define uart0_checksum CHECKSUM("uart0")
32 static int isDebugMonitorUsingUart0()
34 return NVIC_GetPriority(UART0_IRQn
) == 0;
37 // The kernel is the central point in Smoothie : it stores modules, and handles event calls
39 // Config first, because we need the baud_rate setting before we start serial
40 this->config
= new Config();
42 // Serial second, because the other modules might want to say something
43 this->streams
= new StreamOutputPool();
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());
50 this->serial
= new SerialConsole(p13
, p14
, this->config
->value(uart0_checksum
,baud_rate_setting_checksum
)->by_default(9600)->as_number());
52 this->add_module( this->config
);
54 this->add_module( this->serial
);
57 add_module( this->slow_ticker
= new SlowTicker());
58 this->step_ticker
= new StepTicker();
59 this->adc
= new Adc();
62 NVIC_SetPriorityGrouping(0);
63 NVIC_SetPriority(TIMER0_IRQn
, 2);
64 NVIC_SetPriority(TIMER1_IRQn
, 1);
65 NVIC_SetPriority(TIMER2_IRQn
, 3);
67 // Set other priorities lower than the timers
68 NVIC_SetPriority(ADC_IRQn
, 4);
69 NVIC_SetPriority(USB_IRQn
, 4);
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); }
78 NVIC_SetPriority(UART0_IRQn
, 4);
79 NVIC_SetPriority(UART1_IRQn
, 4);
80 NVIC_SetPriority(UART2_IRQn
, 4);
81 NVIC_SetPriority(UART3_IRQn
, 4);
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();
88 this->step_ticker
->set_reset_delay( microseconds_per_step_pulse
/ 1000000L );
90 this->step_ticker
->set_frequency( base_stepping_frequency
);
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() );
102 void Kernel::add_module(Module
* module
){
103 module
->kernel
= this;
104 module
->on_module_loaded();
107 void Kernel::register_for_event(_EVENT_ENUM id_event
, Module
* module
){
108 this->hooks
[id_event
].push_back(module
);
111 void Kernel::call_event(_EVENT_ENUM id_event
){
112 for (Module
* current
: hooks
[id_event
]) {
113 (current
->*kernel_callback_functions
[id_event
])(this);
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
);