Fork of Smoothie to port to mbed non-LPC targets.

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002       This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
00003       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.
00004       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.
00005       You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
00006 */
00007 
00008 #include "libs/Kernel.h"
00009 #include "modules/tools/laser/Laser.h"
00010 #include "modules/tools/extruder/ExtruderMaker.h"
00011 #include "modules/tools/temperaturecontrol/TemperatureControlPool.h"
00012 #include "modules/tools/endstops/Endstops.h"
00013 #include "modules/tools/touchprobe/Touchprobe.h"
00014 #include "modules/tools/switch/SwitchPool.h"
00015 #include "modules/robot/Conveyor.h"
00016 #include "modules/utils/simpleshell/SimpleShell.h"
00017 #include "modules/utils/configurator/Configurator.h"
00018 #include "modules/utils/currentcontrol/CurrentControl.h"
00019 #include "modules/utils/player/Player.h"
00020 #include "modules/utils/pausebutton/PauseButton.h"
00021 #include "modules/utils/PlayLed/PlayLed.h"
00022 #include "modules/utils/panel/Panel.h"
00023 #include "libs/Network/uip/Network.h"
00024 
00025 // #include "libs/ChaNFSSD/SDFileSystem.h"
00026 #include "libs/Config.h"
00027 #include "libs/nuts_bolts.h"
00028 #include "libs/utils.h"
00029 
00030 // Debug
00031 #include "libs/SerialMessage.h"
00032 
00033 #include "libs/USBDevice/USB.h"
00034 #include "libs/USBDevice/USBMSD/USBMSD.h"
00035 #include "libs/USBDevice/USBMSD/SDCard.h"
00036 #include "libs/USBDevice/USBSerial/USBSerial.h"
00037 #include "libs/USBDevice/DFU.h"
00038 #include "libs/SDFAT.h"
00039 
00040 #include "libs/Watchdog.h"
00041 
00042 #include "version.h"
00043 
00044 #define second_usb_serial_enable_checksum  CHECKSUM("second_usb_serial_enable")
00045 #define disable_msd_checksum  CHECKSUM("msd_disable")
00046 #define disable_leds_checksum  CHECKSUM("leds_disable")
00047 
00048 // Watchdog wd(5000000, WDT_MRI);
00049 
00050 // USB Stuff
00051 SDCard sd(P0_9, P0_8, P0_7, P0_6);      // this selects SPI1 as the sdcard as it is on Smoothieboard
00052 //SDCard sd(P0_18, P0_17, P0_15, P0_16);  // this selects SPI0 as the sdcard
00053 
00054 USB u;
00055 USBSerial usbserial(&u);
00056 USBMSD msc(&u, &sd);
00057 //USBMSD *msc= NULL;
00058 DFU dfu(&u);
00059 
00060 SDFAT mounter("sd", &sd);
00061 
00062 GPIO leds[5] = {
00063     GPIO(P1_18),
00064     GPIO(P1_19),
00065     GPIO(P1_20),
00066     GPIO(P1_21),
00067     GPIO(P4_28)
00068 };
00069 
00070 int main() {
00071 
00072     // Default pins to low status
00073     for (int i = 0; i < 5; i++){
00074         leds[i].output();
00075         leds[i]= 0;
00076     }
00077 
00078     Kernel* kernel = new Kernel();
00079 
00080     kernel->streams->printf("Smoothie ( grbl port ) version 0.7.2 with new accel @%ldMHz\r\n", SystemCoreClock / 1000000);
00081     Version version;
00082     kernel->streams->printf("  Build version %s, Build date %s\r\n", version.get_build(), version.get_build_date());
00083 
00084     //some boards don't have leds.. TOO BAD!
00085     kernel->use_leds= !kernel->config->value( disable_leds_checksum )->by_default(false)->as_bool();
00086 
00087     // attempt to be able to disable msd in config
00088     // if(!kernel->config->value( disable_msd_checksum )->by_default(false)->as_bool()){
00089     //     msc= new USBMSD(&u, &sd);
00090     // }else{
00091     //     msc= NULL;
00092     //     kernel->streams->printf("MSD is disabled\r\n");
00093     // }
00094 
00095     bool sdok= (sd.disk_initialize() == 0);
00096 
00097     // Create and add main modules
00098     kernel->add_module( new SimpleShell() );
00099     kernel->add_module( new Configurator() );
00100     kernel->add_module( new CurrentControl() );
00101     kernel->add_module( new SwitchPool() );
00102     kernel->add_module( new PauseButton() );
00103     kernel->add_module( new PlayLed() );
00104     kernel->add_module( new Endstops() );
00105     kernel->add_module( new Player() );
00106 
00107     // these modules can be completely disabled in the Makefile by adding to EXCLUDE_MODULES
00108     #ifndef NO_TOOLS_TEMPERATURECONTROL
00109     kernel->add_module( new TemperatureControlPool() );
00110     #endif
00111     #ifndef NO_TOOLS_EXTRUDER
00112     kernel->add_module( new ExtruderMaker() );
00113     #endif
00114     #ifndef NO_TOOLS_LASER
00115     kernel->add_module( new Laser() );
00116     #endif
00117     #ifndef NO_UTILS_PANEL
00118     kernel->add_module( new Panel() );
00119     #endif
00120     #ifndef NO_TOOLS_TOUCHPROBE
00121     kernel->add_module( new Touchprobe() );
00122     #endif
00123     #ifndef NONETWORK
00124     kernel->add_module( new Network() );
00125     #endif
00126 
00127     // Create and initialize USB stuff
00128     u.init();
00129     //if(sdok) { // only do this if there is an sd disk
00130     //    msc= new USBMSD(&u, &sd);
00131     //    kernel->add_module( msc );
00132     //}
00133 
00134     // if(msc != NULL){
00135     //     kernel->add_module( msc );
00136     // }
00137 
00138     kernel->add_module( &msc );
00139     kernel->add_module( &usbserial );
00140     if( kernel->config->value( second_usb_serial_enable_checksum )->by_default(false)->as_bool() ){
00141         kernel->add_module( new USBSerial(&u) );
00142     }
00143     kernel->add_module( &dfu );
00144     kernel->add_module( &u );
00145 
00146     // clear up the config cache to save some memory
00147     kernel->config->config_cache_clear();
00148 
00149     if(kernel->use_leds) {
00150         // set some leds to indicate status... led0 init doe, led1 mainloop running, led2 idle loop running, led3 sdcard ok
00151         leds[0]= 1; // indicate we are done with init
00152         leds[3]= sdok?1:0; // 4th led inidicates sdcard is available (TODO maye should indicate config was found)
00153     }
00154 
00155     if(sdok) {
00156         // load config override file if present
00157         // NOTE only Mxxx commands that set values should be put in this file. The file is generated by M500
00158         FILE *fp= fopen(kernel->config_override_filename(), "r");
00159         if(fp != NULL) {
00160             char buf[132];
00161             kernel->streams->printf("Loading config override file: %s...\n", kernel->config_override_filename());
00162             while(fgets(buf, sizeof buf, fp) != NULL) {
00163                 kernel->streams->printf("  %s", buf);
00164                 if(buf[0] == ';') continue; // skip the comments
00165                 struct SerialMessage message= {&(StreamOutput::NullStream), buf};
00166                 kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
00167             }
00168             kernel->streams->printf("config override file executed\n");
00169             fclose(fp);
00170         }
00171     }
00172 
00173     uint16_t cnt= 0;
00174     // Main loop
00175     while(1){
00176         if(kernel->use_leds) {
00177             // flash led 2 to show we are alive
00178             leds[1]= (cnt++ & 0x1000) ? 1 : 0;
00179         }
00180         kernel->call_event(ON_MAIN_LOOP);
00181         kernel->call_event(ON_IDLE);
00182     }
00183 }