Merge remote-tracking branch 'upstream/master' into edge
[clinton/Smoothieware.git] / src / main.cpp
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
8 #include "libs/Kernel.h"
9
10 #include "modules/tools/laser/Laser.h"
11 #include "modules/tools/extruder/ExtruderMaker.h"
12 #include "modules/tools/temperaturecontrol/TemperatureControlPool.h"
13 #include "modules/tools/endstops/Endstops.h"
14 #include "modules/tools/touchprobe/Touchprobe.h"
15 #include "modules/tools/zprobe/ZProbe.h"
16 #include "modules/tools/scaracal/SCARAcal.h"
17 #include "modules/tools/switch/SwitchPool.h"
18 #include "modules/tools/temperatureswitch/TemperatureSwitch.h"
19
20 #include "modules/robot/Conveyor.h"
21 #include "modules/utils/simpleshell/SimpleShell.h"
22 #include "modules/utils/configurator/Configurator.h"
23 #include "modules/utils/currentcontrol/CurrentControl.h"
24 #include "modules/utils/player/Player.h"
25 #include "modules/utils/pausebutton/PauseButton.h"
26 #include "modules/utils/PlayLed/PlayLed.h"
27 #include "modules/utils/panel/Panel.h"
28 #include "libs/Network/uip/Network.h"
29 #include "Config.h"
30 #include "checksumm.h"
31 #include "ConfigValue.h"
32
33 // #include "libs/ChaNFSSD/SDFileSystem.h"
34 #include "libs/nuts_bolts.h"
35 #include "libs/utils.h"
36
37 // Debug
38 #include "libs/SerialMessage.h"
39
40 #include "libs/USBDevice/USB.h"
41 #include "libs/USBDevice/USBMSD/USBMSD.h"
42 #include "libs/USBDevice/USBMSD/SDCard.h"
43 #include "libs/USBDevice/USBSerial/USBSerial.h"
44 #include "libs/USBDevice/DFU.h"
45 #include "libs/SDFAT.h"
46 #include "StreamOutputPool.h"
47 #include "ToolManager.h"
48
49 #include "libs/Watchdog.h"
50
51 #include "version.h"
52 #include "system_LPC17xx.h"
53
54 #include "mbed.h"
55
56 #define second_usb_serial_enable_checksum CHECKSUM("second_usb_serial_enable")
57 #define disable_msd_checksum CHECKSUM("msd_disable")
58 #define disable_leds_checksum CHECKSUM("leds_disable")
59
60 // Watchdog wd(5000000, WDT_MRI);
61
62 // USB Stuff
63 SDCard sd __attribute__ ((section ("AHBSRAM0"))) (P0_9, P0_8, P0_7, P0_6); // this selects SPI1 as the sdcard as it is on Smoothieboard
64 //SDCard sd(P0_18, P0_17, P0_15, P0_16); // this selects SPI0 as the sdcard
65
66 USB u __attribute__ ((section ("AHBSRAM0")));
67 USBSerial usbserial __attribute__ ((section ("AHBSRAM0"))) (&u);
68 USBMSD msc __attribute__ ((section ("AHBSRAM0"))) (&u, &sd);
69 //USBMSD *msc= NULL;
70 DFU dfu __attribute__ ((section ("AHBSRAM0"))) (&u);
71
72 SDFAT mounter __attribute__ ((section ("AHBSRAM0"))) ("sd", &sd);
73
74 GPIO leds[5] = {
75 GPIO(P1_18),
76 GPIO(P1_19),
77 GPIO(P1_20),
78 GPIO(P1_21),
79 GPIO(P4_28)
80 };
81
82 void init() {
83
84 // Default pins to low status
85 for (int i = 0; i < 5; i++){
86 leds[i].output();
87 leds[i]= 0;
88 }
89
90 Kernel* kernel = new Kernel();
91
92 kernel->streams->printf("Smoothie Running @%ldMHz\r\n", SystemCoreClock / 1000000);
93 Version version;
94 kernel->streams->printf(" Build version %s, Build date %s\r\n", version.get_build(), version.get_build_date());
95
96 //some boards don't have leds.. TOO BAD!
97 kernel->use_leds= !kernel->config->value( disable_leds_checksum )->by_default(false)->as_bool();
98
99 // attempt to be able to disable msd in config
100 // if(!kernel->config->value( disable_msd_checksum )->by_default(false)->as_bool()){
101 // msc= new USBMSD(&u, &sd);
102 // }else{
103 // msc= NULL;
104 // kernel->streams->printf("MSD is disabled\r\n");
105 // }
106
107 bool sdok= (sd.disk_initialize() == 0);
108
109 // Create and add main modules
110 kernel->add_module( new SimpleShell() );
111 kernel->add_module( new Configurator() );
112 kernel->add_module( new CurrentControl() );
113 kernel->add_module( new PauseButton() );
114 kernel->add_module( new PlayLed() );
115 kernel->add_module( new Endstops() );
116 kernel->add_module( new Player() );
117
118
119 // these modules can be completely disabled in the Makefile by adding to EXCLUDE_MODULES
120 #ifndef NO_TOOLS_SWITCH
121 SwitchPool *sp= new SwitchPool();
122 sp->load_tools();
123 delete sp;
124 #endif
125 #ifndef NO_TOOLS_EXTRUDER
126 // NOTE this must be done first before Temperature control so ToolManager can handle Tn before temperaturecontrol module does
127 ExtruderMaker *em= new ExtruderMaker();
128 em->load_tools();
129 delete em;
130 #endif
131 #ifndef NO_TOOLS_TEMPERATURECONTROL
132 // Note order is important here must be after extruder so Tn as a parameter will get executed first
133 TemperatureControlPool *tp= new TemperatureControlPool();
134 tp->load_tools();
135 delete tp;
136 #endif
137 #ifndef NO_TOOLS_LASER
138 kernel->add_module( new Laser() );
139 #endif
140 #ifndef NO_UTILS_PANEL
141 kernel->add_module( new Panel() );
142 #endif
143 #ifndef NO_TOOLS_TOUCHPROBE
144 kernel->add_module( new Touchprobe() );
145 #endif
146 #ifndef NO_TOOLS_ZPROBE
147 kernel->add_module( new ZProbe() );
148 #endif
149 #ifndef NO_TOOLS_SCARACAL
150 kernel->add_module( new SCARAcal() );
151 #endif
152 #ifndef NONETWORK
153 kernel->add_module( new Network() );
154 #endif
155 #ifndef NO_TOOLS_TEMPERATURESWITCH
156 kernel->add_module( new TemperatureSwitch() );
157 #endif
158
159 // Create and initialize USB stuff
160 u.init();
161 //if(sdok) { // only do this if there is an sd disk
162 // msc= new USBMSD(&u, &sd);
163 // kernel->add_module( msc );
164 //}
165
166 // if(msc != NULL){
167 // kernel->add_module( msc );
168 // }
169
170 kernel->add_module( &msc );
171 kernel->add_module( &usbserial );
172 if( kernel->config->value( second_usb_serial_enable_checksum )->by_default(false)->as_bool() ){
173 kernel->add_module( new USBSerial(&u) );
174 }
175 kernel->add_module( &dfu );
176 kernel->add_module( &u );
177
178 // clear up the config cache to save some memory
179 kernel->config->config_cache_clear();
180
181 if(kernel->use_leds) {
182 // set some leds to indicate status... led0 init doe, led1 mainloop running, led2 idle loop running, led3 sdcard ok
183 leds[0]= 1; // indicate we are done with init
184 leds[3]= sdok?1:0; // 4th led inidicates sdcard is available (TODO maye should indicate config was found)
185 }
186
187 if(sdok) {
188 // load config override file if present
189 // NOTE only Mxxx commands that set values should be put in this file. The file is generated by M500
190 FILE *fp= fopen(kernel->config_override_filename(), "r");
191 if(fp != NULL) {
192 char buf[132];
193 kernel->streams->printf("Loading config override file: %s...\n", kernel->config_override_filename());
194 while(fgets(buf, sizeof buf, fp) != NULL) {
195 kernel->streams->printf(" %s", buf);
196 if(buf[0] == ';') continue; // skip the comments
197 struct SerialMessage message= {&(StreamOutput::NullStream), buf};
198 kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
199 }
200 kernel->streams->printf("config override file executed\n");
201 fclose(fp);
202 }
203 }
204 }
205
206 int main()
207 {
208 init();
209
210 uint16_t cnt= 0;
211 // Main loop
212 while(1){
213 if(THEKERNEL->use_leds) {
214 // flash led 2 to show we are alive
215 leds[1]= (cnt++ & 0x1000) ? 1 : 0;
216 }
217 THEKERNEL->call_event(ON_MAIN_LOOP);
218 THEKERNEL->call_event(ON_IDLE);
219 }
220 }