Merge pull request #320 from gkbeer/edge
[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
879341be
JM
32Kernel* Kernel::instance;
33
7b49793d 34// The kernel is the central point in Smoothie : it stores modules, and handles event calls
4cff3ded 35Kernel::Kernel(){
879341be 36 instance= this; // setup the Singleton instance of the kernel
d4ee6ee2 37
df27a6a3 38 // Config first, because we need the baud_rate setting before we start serial
4cff3ded 39 this->config = new Config();
a39e1557 40
4cff3ded 41 // Serial second, because the other modules might want to say something
38d375e7 42 this->streams = new StreamOutputPool();
d4ee6ee2 43
b2b0b56d 44 // Configure UART depending on MRI config
bb3b7f09 45 // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands.
b2b0b56d 46 NVIC_SetPriorityGrouping(0);
bb3b7f09
SK
47 switch( __mriPlatform_CommUartIndex() ) {
48 case 0:
49 this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number());
50 break;
51 case 1:
52 this->serial = new SerialConsole( p13, p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number());
53 break;
54 case 2:
55 this->serial = new SerialConsole( p28, p27, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number());
56 break;
57 case 3:
58 this->serial = new SerialConsole( p9, p10, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number());
59 break;
4c4a372a 60 }
f5f88509 61
93694d6b 62 this->add_module( this->config );
4cff3ded 63 this->add_module( this->serial );
38d375e7 64
df27a6a3 65 // HAL stuff
4df07f88 66 add_module( this->slow_ticker = new SlowTicker());
3c132bd0
AW
67 this->step_ticker = new StepTicker();
68 this->adc = new Adc();
650ed0a8 69
d337942a 70 // TODO : These should go into platform-specific files
df27a6a3 71 // LPC17xx-specific
813727fb 72 NVIC_SetPriorityGrouping(0);
df27a6a3
MM
73 NVIC_SetPriority(TIMER0_IRQn, 2);
74 NVIC_SetPriority(TIMER1_IRQn, 1);
75 NVIC_SetPriority(TIMER2_IRQn, 3);
feb204be 76
b2b0b56d 77 // Set other priorities lower than the timers
df27a6a3
MM
78 NVIC_SetPriority(ADC_IRQn, 4);
79 NVIC_SetPriority(USB_IRQn, 4);
f5f88509 80
df27a6a3 81 // If MRI is enabled
b2b0b56d 82 if( MRI_ENABLE ){
df27a6a3
MM
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); }
b2b0b56d 87 }else{
df27a6a3
MM
88 NVIC_SetPriority(UART0_IRQn, 4);
89 NVIC_SetPriority(UART1_IRQn, 4);
90 NVIC_SetPriority(UART2_IRQn, 4);
91 NVIC_SetPriority(UART3_IRQn, 4);
b2b0b56d
AW
92 }
93
feb204be 94 // Configure the step ticker
af9072ee 95 int base_stepping_frequency = this->config->value(base_stepping_frequency_checksum )->by_default(100000)->as_number();
1ad23cd3 96 float microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum )->by_default(5 )->as_number();
f5f88509 97
d337942a 98 // Configure the step ticker ( TODO : shouldnt this go into stepticker's code ? )
feb204be
AW
99 this->step_ticker->set_reset_delay( microseconds_per_step_pulse / 1000000L );
100 this->step_ticker->set_frequency( base_stepping_frequency );
3c132bd0 101
df27a6a3 102 // Core modules
81b547a1
AW
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() );
663d7943 107 this->add_module( this->conveyor = new Conveyor() );
81b547a1 108 this->add_module( this->pauser = new Pauser() );
b19aa09d 109 this->add_module( this->public_data = new PublicData() );
14ecdbd7 110 this->add_module( this->toolsmanager = new ToolsManager() );
3857da41 111
4cff3ded
AW
112}
113
93694d6b 114// Add a module to Kernel. We don't actually hold a list of modules, we just tell it where Kernel is
4cff3ded 115void Kernel::add_module(Module* module){
4cff3ded
AW
116 module->on_module_loaded();
117}
118
93694d6b 119// Adds a hook for a given module and event
af9072ee 120void Kernel::register_for_event(_EVENT_ENUM id_event, Module* module){
44912e95 121 this->hooks[id_event].push_back(module);
4cff3ded
AW
122}
123
93694d6b 124// Call a specific event without arguments
af9072ee 125void Kernel::call_event(_EVENT_ENUM id_event){
44912e95 126 for (Module* current : hooks[id_event]) {
e6b5ae25 127 (current->*kernel_callback_functions[id_event])(this);
4cff3ded
AW
128 }
129}
130
93694d6b 131// Call a specific event with an argument
af9072ee 132void Kernel::call_event(_EVENT_ENUM id_event, void * argument){
44912e95 133 for (Module* current : hooks[id_event]) {
e6b5ae25 134 (current->*kernel_callback_functions[id_event])(argument);
4cff3ded
AW
135 }
136}