Merge pull request #66 from arthurwolf/stepper
[clinton/Smoothieware.git] / src / libs / Kernel.cpp
CommitLineData
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"
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"
3a4fa0c1 23#include "modules/robot/Player.h"
a39e1557 24#include <malloc.h>
ded56b35 25
4464301d
AW
26
27
4cff3ded
AW
28// List of callback functions, ordered as their corresponding events
29const ModuleCallback kernel_callback_functions[NUMBER_OF_DEFINED_EVENTS] = {
30 &Module::on_main_loop,
31 &Module::on_console_line_received,
32 &Module::on_gcode_received,
33 &Module::on_stepper_wake_up,
34 &Module::on_gcode_execute,
35 &Module::on_speed_change,
36 &Module::on_block_begin,
da24d6ae 37 &Module::on_block_end,
befcf5cc
AW
38 &Module::on_config_reload,
39 &Module::on_play,
70c25627
L
40 &Module::on_pause,
41 &Module::on_idle
4cff3ded
AW
42};
43
8c309ca9 44#define baud_rate_setting_checksum 10922
3c132bd0 45#define uart0_checksum 16877
4cff3ded
AW
46
47// The kernel is the central point in Smoothie : it stores modules, and handles event calls
48Kernel::Kernel(){
ded56b35 49
e6b5ae25 50 // Value init for the arrays
db453125
AW
51 for( uint8_t i=0; i<NUMBER_OF_DEFINED_EVENTS; i++ ){
52 for( uint8_t index=0; index<32; index++ ){
53 this->hooks[i][index] = NULL;
54 }
55 }
e6b5ae25 56
4cff3ded
AW
57 // Config first, because we need the baud_rate setting before we start serial
58 this->config = new Config();
a39e1557 59
4cff3ded 60 // Serial second, because the other modules might want to say something
38d375e7 61 this->streams = new StreamOutputPool();
4cff3ded 62
6b080aff
AW
63 this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number());
64 //this->serial = new SerialConsole(p13, p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number());
4464301d 65
4cff3ded
AW
66 this->add_module( this->config );
67 this->add_module( this->serial );
38d375e7 68
3c132bd0
AW
69 // HAL stuff
70 this->slow_ticker = new SlowTicker();
3c132bd0
AW
71 this->step_ticker = new StepTicker();
72 this->adc = new Adc();
0e8b102e 73 this->digipot = new Digipot();
650ed0a8
AW
74
75 // LPC17xx-specific
813727fb
AW
76 NVIC_SetPriorityGrouping(0);
77 NVIC_SetPriority(TIMER0_IRQn, 2);
78 NVIC_SetPriority(TIMER1_IRQn, 1);
79 NVIC_SetPriority(TIMER2_IRQn, 3);
feb204be
AW
80
81 // Configure the step ticker
82 int base_stepping_frequency = this->config->value(base_stepping_frequency_checksum )->by_default(100000)->as_number();
83 double microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum )->by_default(5 )->as_number();
84 this->step_ticker->set_reset_delay( microseconds_per_step_pulse / 1000000L );
85 this->step_ticker->set_frequency( base_stepping_frequency );
3c132bd0 86
4cff3ded 87 // Core modules
81b547a1
AW
88 this->add_module( this->gcode_dispatch = new GcodeDispatch() );
89 this->add_module( this->robot = new Robot() );
90 this->add_module( this->stepper = new Stepper() );
91 this->add_module( this->planner = new Planner() );
92 this->add_module( this->player = new Player() );
93 this->add_module( this->pauser = new Pauser() );
feb204be
AW
94
95
4cff3ded
AW
96}
97
98void Kernel::add_module(Module* module){
99 module->kernel = this;
100 module->on_module_loaded();
b66fb830 101 module->register_for_event(ON_CONFIG_RELOAD);
4cff3ded
AW
102}
103
104void Kernel::register_for_event(unsigned int id_event, Module* module){
e6b5ae25
AW
105 uint8_t current_id = 0;
106 Module* current = this->hooks[id_event][0];
107 while(current != NULL ){
108 if( current == module ){ return; }
109 current_id++;
110 current = this->hooks[id_event][current_id];
111 }
112 this->hooks[id_event][current_id] = module;
113 this->hooks[id_event][current_id+1] = NULL;
4cff3ded
AW
114}
115
116void Kernel::call_event(unsigned int id_event){
e6b5ae25
AW
117 uint8_t current_id = 0; Module* current = this->hooks[id_event][0];
118 while(current != NULL ){ // For each active stepper
119 (current->*kernel_callback_functions[id_event])(this);
120 current_id++;
121 current = this->hooks[id_event][current_id];
4cff3ded
AW
122 }
123}
124
125void Kernel::call_event(unsigned int id_event, void * argument){
e6b5ae25
AW
126 uint8_t current_id = 0; Module* current = this->hooks[id_event][0];
127 while(current != NULL ){ // For each active stepper
128 (current->*kernel_callback_functions[id_event])(argument);
129 current_id++;
130 current = this->hooks[id_event][current_id];
4cff3ded
AW
131 }
132}