PageRenderTime 26ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main.cpp

https://github.com/Smoothieware/Smoothieware
C++ | 275 lines | 205 code | 38 blank | 32 comment | 19 complexity | 18c90157a6ffe64d27171fd0b5aa317e MD5 | raw file
  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. #include "libs/Kernel.h"
  8. #include "modules/tools/laser/Laser.h"
  9. #include "modules/tools/spindle/SpindleMaker.h"
  10. #include "modules/tools/extruder/ExtruderMaker.h"
  11. #include "modules/tools/temperaturecontrol/TemperatureControlPool.h"
  12. #include "modules/tools/endstops/Endstops.h"
  13. #include "modules/tools/zprobe/ZProbe.h"
  14. #include "modules/tools/scaracal/SCARAcal.h"
  15. #include "RotaryDeltaCalibration.h"
  16. #include "modules/tools/switch/SwitchPool.h"
  17. #include "modules/tools/temperatureswitch/TemperatureSwitch.h"
  18. #include "modules/tools/drillingcycles/Drillingcycles.h"
  19. #include "FilamentDetector.h"
  20. #include "MotorDriverControl.h"
  21. #include "modules/robot/Conveyor.h"
  22. #include "modules/utils/simpleshell/SimpleShell.h"
  23. #include "modules/utils/configurator/Configurator.h"
  24. #include "modules/utils/currentcontrol/CurrentControl.h"
  25. #include "modules/utils/player/Player.h"
  26. #include "modules/utils/killbutton/KillButton.h"
  27. #include "modules/utils/PlayLed/PlayLed.h"
  28. #include "modules/utils/panel/Panel.h"
  29. #include "libs/Network/uip/Network.h"
  30. #include "Config.h"
  31. #include "checksumm.h"
  32. #include "ConfigValue.h"
  33. #include "StepTicker.h"
  34. #include "SlowTicker.h"
  35. #include "Robot.h"
  36. // #include "libs/ChaNFSSD/SDFileSystem.h"
  37. #include "libs/nuts_bolts.h"
  38. #include "libs/utils.h"
  39. // Debug
  40. #include "libs/SerialMessage.h"
  41. #include "libs/USBDevice/USB.h"
  42. #include "libs/USBDevice/USBMSD/USBMSD.h"
  43. #include "libs/USBDevice/USBMSD/SDCard.h"
  44. #include "libs/USBDevice/USBSerial/USBSerial.h"
  45. #include "libs/USBDevice/DFU.h"
  46. #include "libs/SDFAT.h"
  47. #include "StreamOutputPool.h"
  48. #include "ToolManager.h"
  49. #include "libs/Watchdog.h"
  50. #include "version.h"
  51. #include "system_LPC17xx.h"
  52. #include "platform_memory.h"
  53. #include "mbed.h"
  54. #define second_usb_serial_enable_checksum CHECKSUM("second_usb_serial_enable")
  55. #define disable_msd_checksum CHECKSUM("msd_disable")
  56. #define dfu_enable_checksum CHECKSUM("dfu_enable")
  57. #define watchdog_timeout_checksum CHECKSUM("watchdog_timeout")
  58. // USB Stuff
  59. SDCard sd __attribute__ ((section ("AHBSRAM0"))) (P0_9, P0_8, P0_7, P0_6); // this selects SPI1 as the sdcard as it is on Smoothieboard
  60. //SDCard sd(P0_18, P0_17, P0_15, P0_16); // this selects SPI0 as the sdcard
  61. //SDCard sd(P0_18, P0_17, P0_15, P2_8); // this selects SPI0 as the sdcard witrh a different sd select
  62. USB u __attribute__ ((section ("AHBSRAM0")));
  63. USBSerial usbserial __attribute__ ((section ("AHBSRAM0"))) (&u);
  64. #ifndef DISABLEMSD
  65. USBMSD msc __attribute__ ((section ("AHBSRAM0"))) (&u, &sd);
  66. #else
  67. USBMSD *msc= NULL;
  68. #endif
  69. SDFAT mounter __attribute__ ((section ("AHBSRAM0"))) ("sd", &sd);
  70. GPIO leds[5] = {
  71. GPIO(P1_18),
  72. GPIO(P1_19),
  73. GPIO(P1_20),
  74. GPIO(P1_21),
  75. GPIO(P4_28)
  76. };
  77. void init() {
  78. // Default pins to low status
  79. for (int i = 0; i < 5; i++){
  80. leds[i].output();
  81. leds[i]= 0;
  82. }
  83. Kernel* kernel = new Kernel();
  84. kernel->streams->printf("Smoothie Running @%ldMHz\r\n", SystemCoreClock / 1000000);
  85. SimpleShell::version_command("", kernel->streams);
  86. bool sdok= (sd.disk_initialize() == 0);
  87. if(!sdok) kernel->streams->printf("SDCard failed to initialize\r\n");
  88. #ifdef NONETWORK
  89. kernel->streams->printf("NETWORK is disabled\r\n");
  90. #endif
  91. #ifdef DISABLEMSD
  92. // attempt to be able to disable msd in config
  93. if(sdok && !kernel->config->value( disable_msd_checksum )->by_default(true)->as_bool()){
  94. // HACK to zero the memory USBMSD uses as it and its objects seem to not initialize properly in the ctor
  95. size_t n= sizeof(USBMSD);
  96. void *v = AHB0.alloc(n);
  97. memset(v, 0, n); // clear the allocated memory
  98. msc= new(v) USBMSD(&u, &sd); // allocate object using zeroed memory
  99. }else{
  100. msc= NULL;
  101. kernel->streams->printf("MSD is disabled\r\n");
  102. }
  103. #endif
  104. // Create and add main modules
  105. kernel->add_module( new(AHB0) Player() );
  106. kernel->add_module( new(AHB0) CurrentControl() );
  107. kernel->add_module( new(AHB0) KillButton() );
  108. kernel->add_module( new(AHB0) PlayLed() );
  109. // these modules can be completely disabled in the Makefile by adding to EXCLUDE_MODULES
  110. #ifndef NO_TOOLS_SWITCH
  111. SwitchPool *sp= new SwitchPool();
  112. sp->load_tools();
  113. delete sp;
  114. #endif
  115. #ifndef NO_TOOLS_EXTRUDER
  116. // NOTE this must be done first before Temperature control so ToolManager can handle Tn before temperaturecontrol module does
  117. ExtruderMaker *em= new ExtruderMaker();
  118. em->load_tools();
  119. delete em;
  120. #endif
  121. #ifndef NO_TOOLS_TEMPERATURECONTROL
  122. // Note order is important here must be after extruder so Tn as a parameter will get executed first
  123. TemperatureControlPool *tp= new TemperatureControlPool();
  124. tp->load_tools();
  125. delete tp;
  126. #endif
  127. #ifndef NO_TOOLS_ENDSTOPS
  128. kernel->add_module( new(AHB0) Endstops() );
  129. #endif
  130. #ifndef NO_TOOLS_LASER
  131. kernel->add_module( new Laser() );
  132. #endif
  133. #ifndef NO_TOOLS_SPINDLE
  134. SpindleMaker *sm= new SpindleMaker();
  135. sm->load_spindle();
  136. delete sm;
  137. //kernel->add_module( new(AHB0) Spindle() );
  138. #endif
  139. #ifndef NO_UTILS_PANEL
  140. kernel->add_module( new(AHB0) Panel() );
  141. #endif
  142. #ifndef NO_TOOLS_ZPROBE
  143. kernel->add_module( new(AHB0) ZProbe() );
  144. #endif
  145. #ifndef NO_TOOLS_SCARACAL
  146. kernel->add_module( new(AHB0) SCARAcal() );
  147. #endif
  148. #ifndef NO_TOOLS_ROTARYDELTACALIBRATION
  149. kernel->add_module( new(AHB0) RotaryDeltaCalibration() );
  150. #endif
  151. #ifndef NONETWORK
  152. kernel->add_module( new Network() );
  153. #endif
  154. #ifndef NO_TOOLS_TEMPERATURESWITCH
  155. // Must be loaded after TemperatureControl
  156. kernel->add_module( new(AHB0) TemperatureSwitch() );
  157. #endif
  158. #ifndef NO_TOOLS_DRILLINGCYCLES
  159. kernel->add_module( new(AHB0) Drillingcycles() );
  160. #endif
  161. #ifndef NO_TOOLS_FILAMENTDETECTOR
  162. kernel->add_module( new(AHB0) FilamentDetector() );
  163. #endif
  164. #ifndef NO_UTILS_MOTORDRIVERCONTROL
  165. kernel->add_module( new MotorDriverControl(0) );
  166. #endif
  167. // Create and initialize USB stuff
  168. u.init();
  169. #ifdef DISABLEMSD
  170. if(sdok && msc != NULL){
  171. kernel->add_module( msc );
  172. }
  173. #else
  174. kernel->add_module( &msc );
  175. #endif
  176. kernel->add_module( &usbserial );
  177. if( kernel->config->value( second_usb_serial_enable_checksum )->by_default(false)->as_bool() ){
  178. kernel->add_module( new(AHB0) USBSerial(&u) );
  179. }
  180. if( kernel->config->value( dfu_enable_checksum )->by_default(false)->as_bool() ){
  181. kernel->add_module( new(AHB0) DFU(&u));
  182. }
  183. // 10 second watchdog timeout (or config as seconds)
  184. float t= kernel->config->value( watchdog_timeout_checksum )->by_default(10.0F)->as_number();
  185. if(t > 0.1F) {
  186. // NOTE setting WDT_RESET with the current bootloader would leave it in DFU mode which would be suboptimal
  187. kernel->add_module( new Watchdog(t*1000000, WDT_MRI)); // WDT_RESET));
  188. kernel->streams->printf("Watchdog enabled for %f seconds\n", t);
  189. }else{
  190. kernel->streams->printf("WARNING Watchdog is disabled\n");
  191. }
  192. kernel->add_module( &u );
  193. // memory before cache is cleared
  194. //SimpleShell::print_mem(kernel->streams);
  195. // clear up the config cache to save some memory
  196. kernel->config->config_cache_clear();
  197. if(kernel->is_using_leds()) {
  198. // set some leds to indicate status... led0 init done, led1 mainloop running, led2 idle loop running, led3 sdcard ok
  199. leds[0]= 1; // indicate we are done with init
  200. leds[3]= sdok?1:0; // 4th led indicates sdcard is available (TODO maye should indicate config was found)
  201. }
  202. if(sdok) {
  203. // load config override file if present
  204. // NOTE only Mxxx commands that set values should be put in this file. The file is generated by M500
  205. FILE *fp= fopen(kernel->config_override_filename(), "r");
  206. if(fp != NULL) {
  207. char buf[132];
  208. kernel->streams->printf("Loading config override file: %s...\n", kernel->config_override_filename());
  209. while(fgets(buf, sizeof buf, fp) != NULL) {
  210. kernel->streams->printf(" %s", buf);
  211. if(buf[0] == ';') continue; // skip the comments
  212. struct SerialMessage message= {&(StreamOutput::NullStream), buf};
  213. kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
  214. }
  215. kernel->streams->printf("config override file executed\n");
  216. fclose(fp);
  217. }
  218. }
  219. // start the timers and interrupts
  220. THEKERNEL->conveyor->start(THEROBOT->get_number_registered_motors());
  221. THEKERNEL->step_ticker->start();
  222. THEKERNEL->slow_ticker->start();
  223. }
  224. int main()
  225. {
  226. init();
  227. uint16_t cnt= 0;
  228. // Main loop
  229. while(1){
  230. if(THEKERNEL->is_using_leds()) {
  231. // flash led 2 to show we are alive
  232. leds[1]= (cnt++ & 0x1000) ? 1 : 0;
  233. }
  234. THEKERNEL->call_event(ON_MAIN_LOOP);
  235. THEKERNEL->call_event(ON_IDLE);
  236. }
  237. }