Commit | Line | Data |
---|---|---|
4cff3ded AW |
1 | /* |
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/>. | |
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" |
0e8b102e | 14 | #include "libs/Digipot.h" |
81b547a1 | 15 | #include "libs/Pauser.h" |
38d375e7 | 16 | #include "libs/StreamOutputPool.h" |
4c4a372a | 17 | #include <mri.h> |
4cff3ded AW |
18 | |
19 | #include "modules/communication/SerialConsole.h" | |
20 | #include "modules/communication/GcodeDispatch.h" | |
21 | #include "modules/robot/Planner.h" | |
22 | #include "modules/robot/Robot.h" | |
23 | #include "modules/robot/Stepper.h" | |
3a4fa0c1 | 24 | #include "modules/robot/Player.h" |
a39e1557 | 25 | #include <malloc.h> |
ded56b35 | 26 | |
4464301d AW |
27 | |
28 | ||
4cff3ded AW |
29 | // List of callback functions, ordered as their corresponding events |
30 | const ModuleCallback kernel_callback_functions[NUMBER_OF_DEFINED_EVENTS] = { | |
31 | &Module::on_main_loop, | |
32 | &Module::on_console_line_received, | |
33 | &Module::on_gcode_received, | |
34 | &Module::on_stepper_wake_up, | |
35 | &Module::on_gcode_execute, | |
36 | &Module::on_speed_change, | |
37 | &Module::on_block_begin, | |
da24d6ae | 38 | &Module::on_block_end, |
befcf5cc AW |
39 | &Module::on_config_reload, |
40 | &Module::on_play, | |
70c25627 L |
41 | &Module::on_pause, |
42 | &Module::on_idle | |
4cff3ded AW |
43 | }; |
44 | ||
8c309ca9 | 45 | #define baud_rate_setting_checksum 10922 |
3c132bd0 | 46 | #define uart0_checksum 16877 |
4cff3ded AW |
47 | |
48 | // The kernel is the central point in Smoothie : it stores modules, and handles event calls | |
49 | Kernel::Kernel(){ | |
ded56b35 | 50 | |
e6b5ae25 | 51 | // Value init for the arrays |
db453125 AW |
52 | for( uint8_t i=0; i<NUMBER_OF_DEFINED_EVENTS; i++ ){ |
53 | for( uint8_t index=0; index<32; index++ ){ | |
54 | this->hooks[i][index] = NULL; | |
55 | } | |
56 | } | |
e6b5ae25 | 57 | |
4cff3ded AW |
58 | // Config first, because we need the baud_rate setting before we start serial |
59 | this->config = new Config(); | |
a39e1557 | 60 | |
4cff3ded | 61 | // Serial second, because the other modules might want to say something |
38d375e7 | 62 | this->streams = new StreamOutputPool(); |
4cff3ded | 63 | |
b2b0b56d AW |
64 | // Configure UART depending on MRI config |
65 | NVIC_SetPriorityGrouping(0); | |
4c4a372a | 66 | /* |
b2b0b56d AW |
67 | if (strstr(MRI_UART, "MRI_UART_MBED_USB")){ |
68 | if (strstr(MRI_UART, "MRI_UART_SHARED")){ | |
69 | this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number()); | |
70 | }else{ | |
71 | this->serial = new SerialConsole(p13, p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number()); | |
72 | } | |
73 | }else{ | |
74 | this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number()); | |
75 | } | |
4c4a372a AW |
76 | */ |
77 | if( NVIC_GetPriority(UART0_IRQn) > 0 ){ | |
78 | this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number()); | |
79 | }else{ | |
80 | this->serial = new SerialConsole(p13, p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number()); | |
81 | } | |
82 | ||
b2b0b56d | 83 | |
4cff3ded AW |
84 | this->add_module( this->config ); |
85 | this->add_module( this->serial ); | |
38d375e7 | 86 | |
3c132bd0 AW |
87 | // HAL stuff |
88 | this->slow_ticker = new SlowTicker(); | |
3c132bd0 AW |
89 | this->step_ticker = new StepTicker(); |
90 | this->adc = new Adc(); | |
0e8b102e | 91 | this->digipot = new Digipot(); |
650ed0a8 AW |
92 | |
93 | // LPC17xx-specific | |
813727fb AW |
94 | NVIC_SetPriorityGrouping(0); |
95 | NVIC_SetPriority(TIMER0_IRQn, 2); | |
96 | NVIC_SetPriority(TIMER1_IRQn, 1); | |
97 | NVIC_SetPriority(TIMER2_IRQn, 3); | |
feb204be | 98 | |
b2b0b56d AW |
99 | // Set other priorities lower than the timers |
100 | NVIC_SetPriority(ADC_IRQn, 4); | |
101 | NVIC_SetPriority(USB_IRQn, 4); | |
102 | ||
103 | // If MRI is enabled | |
104 | if( MRI_ENABLE ){ | |
105 | if( NVIC_GetPriority(UART0_IRQn) > 0 ){ NVIC_SetPriority(UART0_IRQn, 4); } | |
106 | if( NVIC_GetPriority(UART1_IRQn) > 0 ){ NVIC_SetPriority(UART1_IRQn, 4); } | |
107 | if( NVIC_GetPriority(UART2_IRQn) > 0 ){ NVIC_SetPriority(UART2_IRQn, 4); } | |
108 | if( NVIC_GetPriority(UART3_IRQn) > 0 ){ NVIC_SetPriority(UART3_IRQn, 4); } | |
109 | }else{ | |
110 | NVIC_SetPriority(UART0_IRQn, 4); | |
111 | NVIC_SetPriority(UART1_IRQn, 4); | |
112 | NVIC_SetPriority(UART2_IRQn, 4); | |
113 | NVIC_SetPriority(UART3_IRQn, 4); | |
114 | } | |
115 | ||
feb204be AW |
116 | // Configure the step ticker |
117 | int base_stepping_frequency = this->config->value(base_stepping_frequency_checksum )->by_default(100000)->as_number(); | |
118 | double microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum )->by_default(5 )->as_number(); | |
119 | this->step_ticker->set_reset_delay( microseconds_per_step_pulse / 1000000L ); | |
120 | this->step_ticker->set_frequency( base_stepping_frequency ); | |
3c132bd0 | 121 | |
4cff3ded | 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() ); | |
127 | this->add_module( this->player = new Player() ); | |
128 | this->add_module( this->pauser = new Pauser() ); | |
feb204be AW |
129 | |
130 | ||
4cff3ded AW |
131 | } |
132 | ||
133 | void Kernel::add_module(Module* module){ | |
134 | module->kernel = this; | |
135 | module->on_module_loaded(); | |
b66fb830 | 136 | module->register_for_event(ON_CONFIG_RELOAD); |
4cff3ded AW |
137 | } |
138 | ||
139 | void Kernel::register_for_event(unsigned int id_event, Module* module){ | |
e6b5ae25 AW |
140 | uint8_t current_id = 0; |
141 | Module* current = this->hooks[id_event][0]; | |
142 | while(current != NULL ){ | |
143 | if( current == module ){ return; } | |
144 | current_id++; | |
145 | current = this->hooks[id_event][current_id]; | |
146 | } | |
147 | this->hooks[id_event][current_id] = module; | |
148 | this->hooks[id_event][current_id+1] = NULL; | |
4cff3ded AW |
149 | } |
150 | ||
151 | void Kernel::call_event(unsigned int id_event){ | |
e6b5ae25 AW |
152 | uint8_t current_id = 0; Module* current = this->hooks[id_event][0]; |
153 | while(current != NULL ){ // For each active stepper | |
154 | (current->*kernel_callback_functions[id_event])(this); | |
155 | current_id++; | |
156 | current = this->hooks[id_event][current_id]; | |
4cff3ded AW |
157 | } |
158 | } | |
159 | ||
160 | void Kernel::call_event(unsigned int id_event, void * argument){ | |
e6b5ae25 AW |
161 | uint8_t current_id = 0; Module* current = this->hooks[id_event][0]; |
162 | while(current != NULL ){ // For each active stepper | |
163 | (current->*kernel_callback_functions[id_event])(argument); | |
164 | current_id++; | |
165 | current = this->hooks[id_event][current_id]; | |
4cff3ded AW |
166 | } |
167 | } |