Merge pull request #375 from powertomato/uart_sel_for_release_fix
[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>
7af0714f 17#include "checksumm.h"
8d54c34c 18#include "ConfigValue.h"
61134a65
JM
19
20#include "libs/StepTicker.h"
21#include "libs/PublicData.h"
4cff3ded
AW
22#include "modules/communication/SerialConsole.h"
23#include "modules/communication/GcodeDispatch.h"
61134a65 24#include "modules/tools/toolsmanager/ToolsManager.h"
4cff3ded
AW
25#include "modules/robot/Planner.h"
26#include "modules/robot/Robot.h"
27#include "modules/robot/Stepper.h"
61134a65
JM
28#include <array>
29
30
31
3fceb8eb 32#include "modules/robot/Conveyor.h"
3857da41 33#include "modules/tools/endstops/Endstops.h"
a39e1557 34#include <malloc.h>
ded56b35 35
d4f93cf4
AG
36#define baud_rate_setting_checksum CHECKSUM("baud_rate")
37#define uart0_checksum CHECKSUM("uart0")
4cff3ded 38
879341be
JM
39Kernel* Kernel::instance;
40
7b49793d 41// The kernel is the central point in Smoothie : it stores modules, and handles event calls
4cff3ded 42Kernel::Kernel(){
879341be 43 instance= this; // setup the Singleton instance of the kernel
d4ee6ee2 44
577414f6 45 // serial first at fixed baud rate (MRI_BAUD) so config can report errors to serial
36463641 46 // Set to UART0, this will be changed to use the same UART as MRI if it's enabled
47 this->serial = new SerialConsole(USBTX, USBRX, 115200);
577414f6
JM
48
49 // Config next, but does not load cache yet
4cff3ded 50 this->config = new Config();
bcb96295
JM
51
52 // Pre-load the config cache, do after setting up serial so we can report errors to serial
577414f6
JM
53 this->config->config_cache_load();
54
55 // now config is loaded we can do normal setup for serial and the rest
56 delete this->serial;
a39e1557 57
38d375e7 58 this->streams = new StreamOutputPool();
d4ee6ee2 59
75f4581c
JM
60 this->current_path = "/";
61
b2b0b56d 62 // Configure UART depending on MRI config
bb3b7f09 63 // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands.
b2b0b56d 64 NVIC_SetPriorityGrouping(0);
ae367dbb 65 if( MRI_ENABLE ) {
66 switch( __mriPlatform_CommUartIndex() ) {
67 case 0:
68 this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number());
69 break;
70 case 1:
71 this->serial = new SerialConsole( p13, p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number());
72 break;
73 case 2:
74 this->serial = new SerialConsole( p28, p27, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number());
75 break;
76 case 3:
77 this->serial = new SerialConsole( p9, p10, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number());
78 break;
79 }
80 }
f5f88509 81
93694d6b 82 this->add_module( this->config );
4cff3ded 83 this->add_module( this->serial );
38d375e7 84
df27a6a3 85 // HAL stuff
4df07f88 86 add_module( this->slow_ticker = new SlowTicker());
3c132bd0
AW
87 this->step_ticker = new StepTicker();
88 this->adc = new Adc();
650ed0a8 89
d337942a 90 // TODO : These should go into platform-specific files
df27a6a3 91 // LPC17xx-specific
813727fb 92 NVIC_SetPriorityGrouping(0);
df27a6a3
MM
93 NVIC_SetPriority(TIMER0_IRQn, 2);
94 NVIC_SetPriority(TIMER1_IRQn, 1);
95 NVIC_SetPriority(TIMER2_IRQn, 3);
feb204be 96
b2b0b56d 97 // Set other priorities lower than the timers
df27a6a3
MM
98 NVIC_SetPriority(ADC_IRQn, 4);
99 NVIC_SetPriority(USB_IRQn, 4);
f5f88509 100
df27a6a3 101 // If MRI is enabled
b2b0b56d 102 if( MRI_ENABLE ){
df27a6a3
MM
103 if( NVIC_GetPriority(UART0_IRQn) > 0 ){ NVIC_SetPriority(UART0_IRQn, 4); }
104 if( NVIC_GetPriority(UART1_IRQn) > 0 ){ NVIC_SetPriority(UART1_IRQn, 4); }
105 if( NVIC_GetPriority(UART2_IRQn) > 0 ){ NVIC_SetPriority(UART2_IRQn, 4); }
106 if( NVIC_GetPriority(UART3_IRQn) > 0 ){ NVIC_SetPriority(UART3_IRQn, 4); }
b2b0b56d 107 }else{
df27a6a3
MM
108 NVIC_SetPriority(UART0_IRQn, 4);
109 NVIC_SetPriority(UART1_IRQn, 4);
110 NVIC_SetPriority(UART2_IRQn, 4);
111 NVIC_SetPriority(UART3_IRQn, 4);
b2b0b56d
AW
112 }
113
feb204be 114 // Configure the step ticker
af9072ee 115 int base_stepping_frequency = this->config->value(base_stepping_frequency_checksum )->by_default(100000)->as_number();
1ad23cd3 116 float microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum )->by_default(5 )->as_number();
f5f88509 117
d337942a 118 // Configure the step ticker ( TODO : shouldnt this go into stepticker's code ? )
feb204be
AW
119 this->step_ticker->set_reset_delay( microseconds_per_step_pulse / 1000000L );
120 this->step_ticker->set_frequency( base_stepping_frequency );
3c132bd0 121
df27a6a3 122 // Core modules
81b547a1
AW
123 this->add_module( this->gcode_dispatch = new GcodeDispatch() );
124 this->add_module( this->robot = new Robot() );
125 this->add_module( this->stepper = new Stepper() );
126 this->add_module( this->planner = new Planner() );
663d7943 127 this->add_module( this->conveyor = new Conveyor() );
81b547a1 128 this->add_module( this->pauser = new Pauser() );
b19aa09d 129 this->add_module( this->public_data = new PublicData() );
14ecdbd7 130 this->add_module( this->toolsmanager = new ToolsManager() );
3857da41 131
4cff3ded
AW
132}
133
93694d6b 134// Add a module to Kernel. We don't actually hold a list of modules, we just tell it where Kernel is
4cff3ded 135void Kernel::add_module(Module* module){
4cff3ded
AW
136 module->on_module_loaded();
137}
138
93694d6b 139// Adds a hook for a given module and event
af9072ee 140void Kernel::register_for_event(_EVENT_ENUM id_event, Module* module){
44912e95 141 this->hooks[id_event].push_back(module);
4cff3ded
AW
142}
143
93694d6b 144// Call a specific event without arguments
af9072ee 145void Kernel::call_event(_EVENT_ENUM id_event){
44912e95 146 for (Module* current : hooks[id_event]) {
e6b5ae25 147 (current->*kernel_callback_functions[id_event])(this);
4cff3ded
AW
148 }
149}
150
93694d6b 151// Call a specific event with an argument
af9072ee 152void Kernel::call_event(_EVENT_ENUM id_event, void * argument){
44912e95 153 for (Module* current : hooks[id_event]) {
e6b5ae25 154 (current->*kernel_callback_functions[id_event])(argument);
4cff3ded
AW
155 }
156}