Commit | Line | Data |
---|---|---|
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" |
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 | 29 | // List of callback functions, ordered as their corresponding events |
df27a6a3 MM |
30 | const ModuleCallback kernel_callback_functions[NUMBER_OF_DEFINED_EVENTS] = { |
31 | &Module::on_main_loop, | |
4cff3ded | 32 | &Module::on_console_line_received, |
df27a6a3 | 33 | &Module::on_gcode_received, |
4cff3ded AW |
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 | 47 | |
7b49793d | 48 | // The kernel is the central point in Smoothie : it stores modules, and handles event calls |
4cff3ded | 49 | Kernel::Kernel(){ |
f5f88509 MM |
50 | extern SerialConsole uart; |
51 | ||
52 | uart.printf("Kernel: "); | |
ded56b35 | 53 | |
e6b5ae25 | 54 | // Value init for the arrays |
df27a6a3 | 55 | for( uint8_t i=0; i<NUMBER_OF_DEFINED_EVENTS; i++ ){ |
db453125 | 56 | for( uint8_t index=0; index<32; index++ ){ |
df27a6a3 MM |
57 | this->hooks[i][index] = NULL; |
58 | } | |
db453125 | 59 | } |
e6b5ae25 | 60 | |
f5f88509 MM |
61 | uart.printf("\t[new Config]\n"); |
62 | ||
df27a6a3 | 63 | // Config first, because we need the baud_rate setting before we start serial |
4cff3ded | 64 | this->config = new Config(); |
a39e1557 | 65 | |
f5f88509 MM |
66 | uart.printf("\t[new StreamOutputPool]\n"); |
67 | ||
4cff3ded | 68 | // Serial second, because the other modules might want to say something |
38d375e7 | 69 | this->streams = new StreamOutputPool(); |
4cff3ded | 70 | |
b2b0b56d AW |
71 | // Configure UART depending on MRI config |
72 | NVIC_SetPriorityGrouping(0); | |
4c4a372a | 73 | /* |
b2b0b56d AW |
74 | if (strstr(MRI_UART, "MRI_UART_MBED_USB")){ |
75 | if (strstr(MRI_UART, "MRI_UART_SHARED")){ | |
76 | this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number()); | |
77 | }else{ | |
78 | this->serial = new SerialConsole(p13, p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number()); | |
79 | } | |
80 | }else{ | |
81 | this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number()); | |
82 | } | |
4c4a372a | 83 | */ |
f5f88509 MM |
84 | // if( NVIC_GetPriority(UART0_IRQn) > 0 ){ |
85 | // this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(2000000)->as_number()); | |
86 | // }else{ | |
87 | // this->serial = new SerialConsole(p13, p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(2000000)->as_number()); | |
88 | // } | |
89 | // this->serial = &uart; | |
4c4a372a | 90 | |
f5f88509 | 91 | uart.printf("\tADD config\n"); |
b2b0b56d | 92 | |
4cff3ded | 93 | this->add_module( this->config ); |
f5f88509 MM |
94 | |
95 | __debugbreak(); | |
96 | ||
97 | uart.printf("\tADD serial\n"); | |
4cff3ded | 98 | this->add_module( this->serial ); |
38d375e7 | 99 | |
df27a6a3 | 100 | // HAL stuff |
f5f88509 | 101 | uart.printf("\tADD SlowTicker\n"); |
3c132bd0 | 102 | this->slow_ticker = new SlowTicker(); |
f5f88509 | 103 | uart.printf("\tADD StepTicker\n"); |
3c132bd0 | 104 | this->step_ticker = new StepTicker(); |
f5f88509 | 105 | uart.printf("\tADD Adc\n"); |
3c132bd0 | 106 | this->adc = new Adc(); |
f5f88509 | 107 | uart.printf("\tADD Digipot\n"); |
0e8b102e | 108 | this->digipot = new Digipot(); |
650ed0a8 | 109 | |
df27a6a3 | 110 | // LPC17xx-specific |
813727fb | 111 | NVIC_SetPriorityGrouping(0); |
df27a6a3 MM |
112 | NVIC_SetPriority(TIMER0_IRQn, 2); |
113 | NVIC_SetPriority(TIMER1_IRQn, 1); | |
114 | NVIC_SetPriority(TIMER2_IRQn, 3); | |
feb204be | 115 | |
b2b0b56d | 116 | // Set other priorities lower than the timers |
df27a6a3 MM |
117 | NVIC_SetPriority(ADC_IRQn, 4); |
118 | NVIC_SetPriority(USB_IRQn, 4); | |
f5f88509 | 119 | |
df27a6a3 | 120 | // If MRI is enabled |
b2b0b56d | 121 | if( MRI_ENABLE ){ |
df27a6a3 MM |
122 | if( NVIC_GetPriority(UART0_IRQn) > 0 ){ NVIC_SetPriority(UART0_IRQn, 4); } |
123 | if( NVIC_GetPriority(UART1_IRQn) > 0 ){ NVIC_SetPriority(UART1_IRQn, 4); } | |
124 | if( NVIC_GetPriority(UART2_IRQn) > 0 ){ NVIC_SetPriority(UART2_IRQn, 4); } | |
125 | if( NVIC_GetPriority(UART3_IRQn) > 0 ){ NVIC_SetPriority(UART3_IRQn, 4); } | |
b2b0b56d | 126 | }else{ |
df27a6a3 MM |
127 | NVIC_SetPriority(UART0_IRQn, 4); |
128 | NVIC_SetPriority(UART1_IRQn, 4); | |
129 | NVIC_SetPriority(UART2_IRQn, 4); | |
130 | NVIC_SetPriority(UART3_IRQn, 4); | |
b2b0b56d AW |
131 | } |
132 | ||
f5f88509 | 133 | // uart.printf("Configure:\n\tStepTicker:\n"); |
feb204be AW |
134 | // Configure the step ticker |
135 | int base_stepping_frequency = this->config->value(base_stepping_frequency_checksum )->by_default(100000)->as_number(); | |
136 | double microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum )->by_default(5 )->as_number(); | |
f5f88509 MM |
137 | |
138 | // uart.printf("\t\tset_reset_delay: %u\n", microseconds_per_step_pulse / 1000000L); | |
139 | // this->step_ticker->set_reset_delay( microseconds_per_step_pulse / 1000000L ); | |
140 | ||
141 | // uart.printf("\t\tset_frequency: %u\n", base_stepping_frequency); | |
142 | // this->step_ticker->set_frequency( base_stepping_frequency ); | |
3c132bd0 | 143 | |
df27a6a3 | 144 | // Core modules |
f5f88509 | 145 | uart.printf("\tADD GcodeDispatch\n"); |
81b547a1 | 146 | this->add_module( this->gcode_dispatch = new GcodeDispatch() ); |
f5f88509 MM |
147 | // uart.printf("\tADD Robot\n"); |
148 | // this->add_module( this->robot = new Robot() ); | |
149 | uart.printf("\tADD Stepper\n"); | |
81b547a1 | 150 | this->add_module( this->stepper = new Stepper() ); |
f5f88509 | 151 | uart.printf("\tADD Planner\n"); |
81b547a1 | 152 | this->add_module( this->planner = new Planner() ); |
f5f88509 | 153 | uart.printf("\tADD Player\n"); |
81b547a1 | 154 | this->add_module( this->player = new Player() ); |
f5f88509 | 155 | uart.printf("\tADD Pauser\n"); |
81b547a1 | 156 | this->add_module( this->pauser = new Pauser() ); |
feb204be | 157 | |
f5f88509 | 158 | uart.printf("Kernel Complete!\n"); |
4cff3ded AW |
159 | } |
160 | ||
161 | void Kernel::add_module(Module* module){ | |
162 | module->kernel = this; | |
163 | module->on_module_loaded(); | |
b66fb830 | 164 | module->register_for_event(ON_CONFIG_RELOAD); |
4cff3ded AW |
165 | } |
166 | ||
167 | void Kernel::register_for_event(unsigned int id_event, Module* module){ | |
df27a6a3 | 168 | uint8_t current_id = 0; |
e6b5ae25 AW |
169 | Module* current = this->hooks[id_event][0]; |
170 | while(current != NULL ){ | |
171 | if( current == module ){ return; } | |
172 | current_id++; | |
173 | current = this->hooks[id_event][current_id]; | |
174 | } | |
175 | this->hooks[id_event][current_id] = module; | |
176 | this->hooks[id_event][current_id+1] = NULL; | |
4cff3ded AW |
177 | } |
178 | ||
179 | void Kernel::call_event(unsigned int id_event){ | |
e6b5ae25 AW |
180 | uint8_t current_id = 0; Module* current = this->hooks[id_event][0]; |
181 | while(current != NULL ){ // For each active stepper | |
182 | (current->*kernel_callback_functions[id_event])(this); | |
183 | current_id++; | |
184 | current = this->hooks[id_event][current_id]; | |
4cff3ded AW |
185 | } |
186 | } | |
187 | ||
188 | void Kernel::call_event(unsigned int id_event, void * argument){ | |
e6b5ae25 AW |
189 | uint8_t current_id = 0; Module* current = this->hooks[id_event][0]; |
190 | while(current != NULL ){ // For each active stepper | |
191 | (current->*kernel_callback_functions[id_event])(argument); | |
192 | current_id++; | |
193 | current = this->hooks[id_event][current_id]; | |
4cff3ded AW |
194 | } |
195 | } |