fix deadlock/race condition
[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/spindle/Spindle.h"
12 #include "modules/tools/extruder/ExtruderMaker.h"
13 #include "modules/tools/temperaturecontrol/TemperatureControlPool.h"
14 #include "modules/tools/endstops/Endstops.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 #define dfu_enable_checksum CHECKSUM("dfu_enable")
60
61 // Watchdog wd(5000000, WDT_MRI);
62
63 // USB Stuff
64 SDCard sd __attribute__ ((section ("AHBSRAM0"))) (P0_9, P0_8, P0_7, P0_6); // this selects SPI1 as the sdcard as it is on Smoothieboard
65 //SDCard sd(P0_18, P0_17, P0_15, P0_16); // this selects SPI0 as the sdcard
66 //SDCard sd(P0_18, P0_17, P0_15, P2_8); // this selects SPI0 as the sdcard witrh a different sd select
67
68 USB u __attribute__ ((section ("AHBSRAM0")));
69 USBSerial usbserial __attribute__ ((section ("AHBSRAM0"))) (&u);
70 #ifndef DISABLEMSD
71 USBMSD msc __attribute__ ((section ("AHBSRAM0"))) (&u, &sd);
72 #else
73 USBMSD *msc= NULL;
74 #endif
75
76 SDFAT mounter __attribute__ ((section ("AHBSRAM0"))) ("sd", &sd);
77
78 GPIO leds[5] = {
79 GPIO(P1_18),
80 GPIO(P1_19),
81 GPIO(P1_20),
82 GPIO(P1_21),
83 GPIO(P4_28)
84 };
85
86 // debug pins, only used if defined in src/makefile
87 #ifdef STEPTICKER_DEBUG_PIN
88 GPIO stepticker_debug_pin(STEPTICKER_DEBUG_PIN);
89 #endif
90
91 void init() {
92
93 // Default pins to low status
94 for (int i = 0; i < 5; i++){
95 leds[i].output();
96 leds[i]= 0;
97 }
98
99 #ifdef STEPTICKER_DEBUG_PIN
100 stepticker_debug_pin.output();
101 stepticker_debug_pin= 0;
102 #endif
103
104 Kernel* kernel = new Kernel();
105
106 kernel->streams->printf("Smoothie Running @%ldMHz\r\n", SystemCoreClock / 1000000);
107 Version version;
108 kernel->streams->printf(" Build version %s, Build date %s\r\n", version.get_build(), version.get_build_date());
109
110 //some boards don't have leds.. TOO BAD!
111 kernel->use_leds= !kernel->config->value( disable_leds_checksum )->by_default(false)->as_bool();
112
113 bool sdok= (sd.disk_initialize() == 0);
114 if(!sdok) kernel->streams->printf("SDCard is disabled\r\n");
115
116 #ifdef DISABLEMSD
117 // attempt to be able to disable msd in config
118 if(sdok && !kernel->config->value( disable_msd_checksum )->by_default(false)->as_bool()){
119 // HACK to zero the memory USBMSD uses as it and its objects seem to not initialize properly in the ctor
120 size_t n= sizeof(USBMSD);
121 void *v = AHB0.alloc(n);
122 memset(v, 0, n); // clear the allocated memory
123 msc= new(v) USBMSD(&u, &sd); // allocate object using zeroed memory
124 }else{
125 msc= NULL;
126 kernel->streams->printf("MSD is disabled\r\n");
127 }
128 #endif
129
130
131 // Create and add main modules
132 kernel->add_module( new SimpleShell() );
133 kernel->add_module( new Configurator() );
134 kernel->add_module( new CurrentControl() );
135 kernel->add_module( new PauseButton() );
136 kernel->add_module( new PlayLed() );
137 kernel->add_module( new Endstops() );
138 kernel->add_module( new Player() );
139
140
141 // these modules can be completely disabled in the Makefile by adding to EXCLUDE_MODULES
142 #ifndef NO_TOOLS_SWITCH
143 SwitchPool *sp= new SwitchPool();
144 sp->load_tools();
145 delete sp;
146 #endif
147 #ifndef NO_TOOLS_EXTRUDER
148 // NOTE this must be done first before Temperature control so ToolManager can handle Tn before temperaturecontrol module does
149 ExtruderMaker *em= new ExtruderMaker();
150 em->load_tools();
151 delete em;
152 #endif
153 #ifndef NO_TOOLS_TEMPERATURECONTROL
154 // Note order is important here must be after extruder so Tn as a parameter will get executed first
155 TemperatureControlPool *tp= new TemperatureControlPool();
156 tp->load_tools();
157 delete tp;
158 #endif
159 #ifndef NO_TOOLS_LASER
160 kernel->add_module( new Laser() );
161 #endif
162 #ifndef NO_TOOLS_SPINDLE
163 kernel->add_module( new Spindle() );
164 #endif
165 #ifndef NO_UTILS_PANEL
166 kernel->add_module( new Panel() );
167 #endif
168 #ifndef NO_TOOLS_TOUCHPROBE
169 kernel->add_module( new Touchprobe() );
170 #endif
171 #ifndef NO_TOOLS_ZPROBE
172 kernel->add_module( new ZProbe() );
173 #endif
174 #ifndef NO_TOOLS_SCARACAL
175 kernel->add_module( new SCARAcal() );
176 #endif
177 #ifndef NONETWORK
178 kernel->add_module( new Network() );
179 #endif
180 #ifndef NO_TOOLS_TEMPERATURESWITCH
181 // Must be loaded after TemperatureControlPool
182 kernel->add_module( new TemperatureSwitch() );
183 #endif
184
185 // Create and initialize USB stuff
186 u.init();
187
188 #ifdef DISABLEMSD
189 if(sdok && msc != NULL){
190 kernel->add_module( msc );
191 }
192 #else
193 kernel->add_module( &msc );
194 #endif
195
196 kernel->add_module( &usbserial );
197 if( kernel->config->value( second_usb_serial_enable_checksum )->by_default(false)->as_bool() ){
198 kernel->add_module( new(AHB0) USBSerial(&u) );
199 }
200
201 if( kernel->config->value( dfu_enable_checksum )->by_default(false)->as_bool() ){
202 kernel->add_module( new(AHB0) DFU(&u));
203 }
204 kernel->add_module( &u );
205
206 // clear up the config cache to save some memory
207 kernel->config->config_cache_clear();
208
209 if(kernel->use_leds) {
210 // set some leds to indicate status... led0 init doe, led1 mainloop running, led2 idle loop running, led3 sdcard ok
211 leds[0]= 1; // indicate we are done with init
212 leds[3]= sdok?1:0; // 4th led inidicates sdcard is available (TODO maye should indicate config was found)
213 }
214
215 if(sdok) {
216 // load config override file if present
217 // NOTE only Mxxx commands that set values should be put in this file. The file is generated by M500
218 FILE *fp= fopen(kernel->config_override_filename(), "r");
219 if(fp != NULL) {
220 char buf[132];
221 kernel->streams->printf("Loading config override file: %s...\n", kernel->config_override_filename());
222 while(fgets(buf, sizeof buf, fp) != NULL) {
223 kernel->streams->printf(" %s", buf);
224 if(buf[0] == ';') continue; // skip the comments
225 struct SerialMessage message= {&(StreamOutput::NullStream), buf};
226 kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
227 }
228 kernel->streams->printf("config override file executed\n");
229 fclose(fp);
230 }
231 }
232 }
233
234 int main()
235 {
236 init();
237
238 uint16_t cnt= 0;
239 // Main loop
240 while(1){
241 if(THEKERNEL->use_leds) {
242 // flash led 2 to show we are alive
243 leds[1]= (cnt++ & 0x1000) ? 1 : 0;
244 }
245 THEKERNEL->call_event(ON_MAIN_LOOP);
246 THEKERNEL->call_event(ON_IDLE);
247 }
248 }