Fork of Smoothie to port to mbed non-LPC targets.
Fork of Smoothie by
libs/Kernel.cpp@2:1df0b61d3b5a, 2014-02-28 (annotated)
- Committer:
- Michael J. Spencer
- Date:
- Fri Feb 28 18:52:52 2014 -0800
- Revision:
- 2:1df0b61d3b5a
- Parent:
- 0:31e91bb0ef3c
- Child:
- 3:f151d08d335c
Update to latest Smoothie.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Michael J. Spencer |
2:1df0b61d3b5a | 1 | /* |
scachat | 0:31e91bb0ef3c | 2 | This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). |
scachat | 0:31e91bb0ef3c | 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. |
scachat | 0:31e91bb0ef3c | 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. |
Michael J. Spencer |
2:1df0b61d3b5a | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
scachat | 0:31e91bb0ef3c | 6 | */ |
scachat | 0:31e91bb0ef3c | 7 | |
scachat | 0:31e91bb0ef3c | 8 | #include "libs/Kernel.h" |
scachat | 0:31e91bb0ef3c | 9 | #include "libs/Module.h" |
scachat | 0:31e91bb0ef3c | 10 | #include "libs/Config.h" |
scachat | 0:31e91bb0ef3c | 11 | #include "libs/nuts_bolts.h" |
scachat | 0:31e91bb0ef3c | 12 | #include "libs/SlowTicker.h" |
scachat | 0:31e91bb0ef3c | 13 | #include "libs/Adc.h" |
scachat | 0:31e91bb0ef3c | 14 | #include "libs/Pauser.h" |
Michael J. Spencer |
2:1df0b61d3b5a | 15 | #include "libs/StreamOutputPool.h" |
Michael J. Spencer |
2:1df0b61d3b5a | 16 | #include <mri.h> |
scachat | 0:31e91bb0ef3c | 17 | |
scachat | 0:31e91bb0ef3c | 18 | #include "modules/communication/SerialConsole.h" |
scachat | 0:31e91bb0ef3c | 19 | #include "modules/communication/GcodeDispatch.h" |
scachat | 0:31e91bb0ef3c | 20 | #include "modules/robot/Planner.h" |
scachat | 0:31e91bb0ef3c | 21 | #include "modules/robot/Robot.h" |
scachat | 0:31e91bb0ef3c | 22 | #include "modules/robot/Stepper.h" |
Michael J. Spencer |
2:1df0b61d3b5a | 23 | #include "modules/robot/Conveyor.h" |
Michael J. Spencer |
2:1df0b61d3b5a | 24 | #include "modules/tools/endstops/Endstops.h" |
Michael J. Spencer |
2:1df0b61d3b5a | 25 | #include <malloc.h> |
scachat | 0:31e91bb0ef3c | 26 | |
Michael J. Spencer |
2:1df0b61d3b5a | 27 | #define baud_rate_setting_checksum CHECKSUM("baud_rate") |
Michael J. Spencer |
2:1df0b61d3b5a | 28 | #define uart0_checksum CHECKSUM("uart0") |
scachat | 0:31e91bb0ef3c | 29 | |
Michael J. Spencer |
2:1df0b61d3b5a | 30 | Kernel* Kernel::instance; |
scachat | 0:31e91bb0ef3c | 31 | |
Michael J. Spencer |
2:1df0b61d3b5a | 32 | // The kernel is the central point in Smoothie : it stores modules, and handles event calls |
Michael J. Spencer |
2:1df0b61d3b5a | 33 | Kernel::Kernel(){ |
Michael J. Spencer |
2:1df0b61d3b5a | 34 | instance= this; // setup the Singleton instance of the kernel |
scachat | 0:31e91bb0ef3c | 35 | |
Michael J. Spencer |
2:1df0b61d3b5a | 36 | // Config first, because we need the baud_rate setting before we start serial |
scachat | 0:31e91bb0ef3c | 37 | this->config = new Config(); |
scachat | 0:31e91bb0ef3c | 38 | |
scachat | 0:31e91bb0ef3c | 39 | // Serial second, because the other modules might want to say something |
Michael J. Spencer |
2:1df0b61d3b5a | 40 | this->streams = new StreamOutputPool(); |
Michael J. Spencer |
2:1df0b61d3b5a | 41 | |
Michael J. Spencer |
2:1df0b61d3b5a | 42 | this->current_path = "/"; |
Michael J. Spencer |
2:1df0b61d3b5a | 43 | |
Michael J. Spencer |
2:1df0b61d3b5a | 44 | // Configure UART depending on MRI config |
Michael J. Spencer |
2:1df0b61d3b5a | 45 | // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands. |
Michael J. Spencer |
2:1df0b61d3b5a | 46 | NVIC_SetPriorityGrouping(0); |
Michael J. Spencer |
2:1df0b61d3b5a | 47 | switch( __mriPlatform_CommUartIndex() ) { |
Michael J. Spencer |
2:1df0b61d3b5a | 48 | case 0: |
Michael J. Spencer |
2:1df0b61d3b5a | 49 | this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number()); |
Michael J. Spencer |
2:1df0b61d3b5a | 50 | break; |
Michael J. Spencer |
2:1df0b61d3b5a | 51 | case 1: |
Michael J. Spencer |
2:1df0b61d3b5a | 52 | this->serial = new SerialConsole( p13, p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number()); |
Michael J. Spencer |
2:1df0b61d3b5a | 53 | break; |
Michael J. Spencer |
2:1df0b61d3b5a | 54 | case 2: |
Michael J. Spencer |
2:1df0b61d3b5a | 55 | this->serial = new SerialConsole( p28, p27, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number()); |
Michael J. Spencer |
2:1df0b61d3b5a | 56 | break; |
Michael J. Spencer |
2:1df0b61d3b5a | 57 | case 3: |
Michael J. Spencer |
2:1df0b61d3b5a | 58 | this->serial = new SerialConsole( p9, p10, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number()); |
Michael J. Spencer |
2:1df0b61d3b5a | 59 | break; |
Michael J. Spencer |
2:1df0b61d3b5a | 60 | } |
scachat | 0:31e91bb0ef3c | 61 | |
scachat | 0:31e91bb0ef3c | 62 | this->add_module( this->config ); |
scachat | 0:31e91bb0ef3c | 63 | this->add_module( this->serial ); |
Michael J. Spencer |
2:1df0b61d3b5a | 64 | |
Michael J. Spencer |
2:1df0b61d3b5a | 65 | // HAL stuff |
Michael J. Spencer |
2:1df0b61d3b5a | 66 | add_module( this->slow_ticker = new SlowTicker()); |
scachat | 0:31e91bb0ef3c | 67 | this->step_ticker = new StepTicker(); |
scachat | 0:31e91bb0ef3c | 68 | this->adc = new Adc(); |
Michael J. Spencer |
2:1df0b61d3b5a | 69 | |
Michael J. Spencer |
2:1df0b61d3b5a | 70 | // TODO : These should go into platform-specific files |
Michael J. Spencer |
2:1df0b61d3b5a | 71 | // LPC17xx-specific |
Michael J. Spencer |
2:1df0b61d3b5a | 72 | NVIC_SetPriorityGrouping(0); |
Michael J. Spencer |
2:1df0b61d3b5a | 73 | NVIC_SetPriority(TIMER0_IRQn, 2); |
Michael J. Spencer |
2:1df0b61d3b5a | 74 | NVIC_SetPriority(TIMER1_IRQn, 1); |
Michael J. Spencer |
2:1df0b61d3b5a | 75 | NVIC_SetPriority(TIMER2_IRQn, 3); |
Michael J. Spencer |
2:1df0b61d3b5a | 76 | |
Michael J. Spencer |
2:1df0b61d3b5a | 77 | // Set other priorities lower than the timers |
Michael J. Spencer |
2:1df0b61d3b5a | 78 | NVIC_SetPriority(ADC_IRQn, 4); |
Michael J. Spencer |
2:1df0b61d3b5a | 79 | NVIC_SetPriority(USB_IRQn, 4); |
scachat | 0:31e91bb0ef3c | 80 | |
Michael J. Spencer |
2:1df0b61d3b5a | 81 | // If MRI is enabled |
Michael J. Spencer |
2:1df0b61d3b5a | 82 | if( MRI_ENABLE ){ |
Michael J. Spencer |
2:1df0b61d3b5a | 83 | if( NVIC_GetPriority(UART0_IRQn) > 0 ){ NVIC_SetPriority(UART0_IRQn, 4); } |
Michael J. Spencer |
2:1df0b61d3b5a | 84 | if( NVIC_GetPriority(UART1_IRQn) > 0 ){ NVIC_SetPriority(UART1_IRQn, 4); } |
Michael J. Spencer |
2:1df0b61d3b5a | 85 | if( NVIC_GetPriority(UART2_IRQn) > 0 ){ NVIC_SetPriority(UART2_IRQn, 4); } |
Michael J. Spencer |
2:1df0b61d3b5a | 86 | if( NVIC_GetPriority(UART3_IRQn) > 0 ){ NVIC_SetPriority(UART3_IRQn, 4); } |
Michael J. Spencer |
2:1df0b61d3b5a | 87 | }else{ |
Michael J. Spencer |
2:1df0b61d3b5a | 88 | NVIC_SetPriority(UART0_IRQn, 4); |
Michael J. Spencer |
2:1df0b61d3b5a | 89 | NVIC_SetPriority(UART1_IRQn, 4); |
Michael J. Spencer |
2:1df0b61d3b5a | 90 | NVIC_SetPriority(UART2_IRQn, 4); |
Michael J. Spencer |
2:1df0b61d3b5a | 91 | NVIC_SetPriority(UART3_IRQn, 4); |
Michael J. Spencer |
2:1df0b61d3b5a | 92 | } |
scachat | 0:31e91bb0ef3c | 93 | |
Michael J. Spencer |
2:1df0b61d3b5a | 94 | // Configure the step ticker |
Michael J. Spencer |
2:1df0b61d3b5a | 95 | int base_stepping_frequency = this->config->value(base_stepping_frequency_checksum )->by_default(100000)->as_number(); |
Michael J. Spencer |
2:1df0b61d3b5a | 96 | float microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum )->by_default(5 )->as_number(); |
Michael J. Spencer |
2:1df0b61d3b5a | 97 | |
Michael J. Spencer |
2:1df0b61d3b5a | 98 | // Configure the step ticker ( TODO : shouldnt this go into stepticker's code ? ) |
Michael J. Spencer |
2:1df0b61d3b5a | 99 | this->step_ticker->set_reset_delay( microseconds_per_step_pulse / 1000000L ); |
Michael J. Spencer |
2:1df0b61d3b5a | 100 | this->step_ticker->set_frequency( base_stepping_frequency ); |
Michael J. Spencer |
2:1df0b61d3b5a | 101 | |
Michael J. Spencer |
2:1df0b61d3b5a | 102 | // Core modules |
scachat | 0:31e91bb0ef3c | 103 | this->add_module( this->gcode_dispatch = new GcodeDispatch() ); |
scachat | 0:31e91bb0ef3c | 104 | this->add_module( this->robot = new Robot() ); |
scachat | 0:31e91bb0ef3c | 105 | this->add_module( this->stepper = new Stepper() ); |
scachat | 0:31e91bb0ef3c | 106 | this->add_module( this->planner = new Planner() ); |
Michael J. Spencer |
2:1df0b61d3b5a | 107 | this->add_module( this->conveyor = new Conveyor() ); |
scachat | 0:31e91bb0ef3c | 108 | this->add_module( this->pauser = new Pauser() ); |
Michael J. Spencer |
2:1df0b61d3b5a | 109 | this->add_module( this->public_data = new PublicData() ); |
Michael J. Spencer |
2:1df0b61d3b5a | 110 | this->add_module( this->toolsmanager = new ToolsManager() ); |
Michael J. Spencer |
2:1df0b61d3b5a | 111 | |
scachat | 0:31e91bb0ef3c | 112 | } |
scachat | 0:31e91bb0ef3c | 113 | |
Michael J. Spencer |
2:1df0b61d3b5a | 114 | // Add a module to Kernel. We don't actually hold a list of modules, we just tell it where Kernel is |
scachat | 0:31e91bb0ef3c | 115 | void Kernel::add_module(Module* module){ |
scachat | 0:31e91bb0ef3c | 116 | module->on_module_loaded(); |
scachat | 0:31e91bb0ef3c | 117 | } |
scachat | 0:31e91bb0ef3c | 118 | |
Michael J. Spencer |
2:1df0b61d3b5a | 119 | // Adds a hook for a given module and event |
Michael J. Spencer |
2:1df0b61d3b5a | 120 | void Kernel::register_for_event(_EVENT_ENUM id_event, Module* module){ |
scachat | 0:31e91bb0ef3c | 121 | this->hooks[id_event].push_back(module); |
scachat | 0:31e91bb0ef3c | 122 | } |
scachat | 0:31e91bb0ef3c | 123 | |
Michael J. Spencer |
2:1df0b61d3b5a | 124 | // Call a specific event without arguments |
Michael J. Spencer |
2:1df0b61d3b5a | 125 | void Kernel::call_event(_EVENT_ENUM id_event){ |
Michael J. Spencer |
2:1df0b61d3b5a | 126 | for (Module* current : hooks[id_event]) { |
Michael J. Spencer |
2:1df0b61d3b5a | 127 | (current->*kernel_callback_functions[id_event])(this); |
scachat | 0:31e91bb0ef3c | 128 | } |
scachat | 0:31e91bb0ef3c | 129 | } |
scachat | 0:31e91bb0ef3c | 130 | |
Michael J. Spencer |
2:1df0b61d3b5a | 131 | // Call a specific event with an argument |
Michael J. Spencer |
2:1df0b61d3b5a | 132 | void Kernel::call_event(_EVENT_ENUM id_event, void * argument){ |
Michael J. Spencer |
2:1df0b61d3b5a | 133 | for (Module* current : hooks[id_event]) { |
Michael J. Spencer |
2:1df0b61d3b5a | 134 | (current->*kernel_callback_functions[id_event])(argument); |
scachat | 0:31e91bb0ef3c | 135 | } |
scachat | 0:31e91bb0ef3c | 136 | } |