putting Endstops back into main.cpp
[clinton/Smoothieware.git] / src / libs / Kernel.cpp
CommitLineData
df27a6a3 1/*
4cff3ded
AW
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.
df27a6a3 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
4cff3ded
AW
6*/
7
4cff3ded
AW
8#include "libs/Kernel.h"
9#include "libs/Module.h"
10#include "libs/Config.h"
4cff3ded 11#include "libs/nuts_bolts.h"
ded56b35 12#include "libs/SlowTicker.h"
3c132bd0 13#include "libs/Adc.h"
81b547a1 14#include "libs/Pauser.h"
38d375e7 15#include "libs/StreamOutputPool.h"
4c4a372a 16#include <mri.h>
4cff3ded
AW
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"
3fceb8eb 23#include "modules/robot/Conveyor.h"
3857da41 24#include "modules/tools/endstops/Endstops.h"
a39e1557 25#include <malloc.h>
ded56b35 26
4464301d
AW
27
28
d4f93cf4
AG
29#define baud_rate_setting_checksum CHECKSUM("baud_rate")
30#define uart0_checksum CHECKSUM("uart0")
4cff3ded 31
e99870e2
AG
32static int isDebugMonitorUsingUart0()
33{
34 return NVIC_GetPriority(UART0_IRQn) == 0;
35}
4cff3ded 36
7b49793d 37// The kernel is the central point in Smoothie : it stores modules, and handles event calls
4cff3ded 38Kernel::Kernel(){
df27a6a3 39 // Config first, because we need the baud_rate setting before we start serial
4cff3ded 40 this->config = new Config();
a39e1557 41
4cff3ded 42 // Serial second, because the other modules might want to say something
38d375e7 43 this->streams = new StreamOutputPool();
4cff3ded 44
b2b0b56d
AW
45 // Configure UART depending on MRI config
46 NVIC_SetPriorityGrouping(0);
e99870e2 47 if( !isDebugMonitorUsingUart0() ){
4c4a372a
AW
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 }
4cff3ded 52 this->add_module( this->config );
f5f88509 53
4cff3ded 54 this->add_module( this->serial );
38d375e7 55
df27a6a3 56 // HAL stuff
4df07f88 57 add_module( this->slow_ticker = new SlowTicker());
3c132bd0
AW
58 this->step_ticker = new StepTicker();
59 this->adc = new Adc();
650ed0a8 60
df27a6a3 61 // LPC17xx-specific
813727fb 62 NVIC_SetPriorityGrouping(0);
df27a6a3
MM
63 NVIC_SetPriority(TIMER0_IRQn, 2);
64 NVIC_SetPriority(TIMER1_IRQn, 1);
65 NVIC_SetPriority(TIMER2_IRQn, 3);
feb204be 66
b2b0b56d 67 // Set other priorities lower than the timers
df27a6a3
MM
68 NVIC_SetPriority(ADC_IRQn, 4);
69 NVIC_SetPriority(USB_IRQn, 4);
f5f88509 70
df27a6a3 71 // If MRI is enabled
b2b0b56d 72 if( MRI_ENABLE ){
df27a6a3
MM
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); }
b2b0b56d 77 }else{
df27a6a3
MM
78 NVIC_SetPriority(UART0_IRQn, 4);
79 NVIC_SetPriority(UART1_IRQn, 4);
80 NVIC_SetPriority(UART2_IRQn, 4);
81 NVIC_SetPriority(UART3_IRQn, 4);
b2b0b56d
AW
82 }
83
feb204be 84 // Configure the step ticker
af9072ee 85 int base_stepping_frequency = this->config->value(base_stepping_frequency_checksum )->by_default(100000)->as_number();
feb204be 86 double microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum )->by_default(5 )->as_number();
f5f88509 87
feb204be 88 this->step_ticker->set_reset_delay( microseconds_per_step_pulse / 1000000L );
f5f88509 89
feb204be 90 this->step_ticker->set_frequency( base_stepping_frequency );
3c132bd0 91
df27a6a3 92 // Core modules
81b547a1
AW
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() );
663d7943 97 this->add_module( this->conveyor = new Conveyor() );
81b547a1 98 this->add_module( this->pauser = new Pauser() );
3857da41 99
4cff3ded
AW
100}
101
102void Kernel::add_module(Module* module){
103 module->kernel = this;
104 module->on_module_loaded();
105}
106
af9072ee 107void Kernel::register_for_event(_EVENT_ENUM id_event, Module* module){
44912e95 108 this->hooks[id_event].push_back(module);
4cff3ded
AW
109}
110
af9072ee 111void Kernel::call_event(_EVENT_ENUM id_event){
44912e95 112 for (Module* current : hooks[id_event]) {
e6b5ae25 113 (current->*kernel_callback_functions[id_event])(this);
4cff3ded
AW
114 }
115}
116
af9072ee 117void Kernel::call_event(_EVENT_ENUM id_event, void * argument){
44912e95 118 for (Module* current : hooks[id_event]) {
e6b5ae25 119 (current->*kernel_callback_functions[id_event])(argument);
4cff3ded
AW
120 }
121}