Browse Source

Move setup() and loop() to the end

Scott Lahteine 8 years ago
parent
commit
f9a192c7e3
1 changed files with 214 additions and 214 deletions
  1. 214
    214
      Marlin/Marlin_main.cpp

+ 214
- 214
Marlin/Marlin_main.cpp View File

850
      */
850
      */
851
     STOW_Z_SERVO();
851
     STOW_Z_SERVO();
852
   #endif
852
   #endif
853
-
854
-  #if HAS_BED_PROBE
855
-    endstops.enable_z_probe(false);
856
-  #endif
857
 }
853
 }
858
 
854
 
859
 /**
855
 /**
879
 
875
 
880
 #endif
876
 #endif
881
 
877
 
882
-/**
883
- * Marlin entry-point: Set up before the program loop
884
- *  - Set up the kill pin, filament runout, power hold
885
- *  - Start the serial port
886
- *  - Print startup messages and diagnostics
887
- *  - Get EEPROM or default settings
888
- *  - Initialize managers for:
889
- *    • temperature
890
- *    • planner
891
- *    • watchdog
892
- *    • stepper
893
- *    • photo pin
894
- *    • servos
895
- *    • LCD controller
896
- *    • Digipot I2C
897
- *    • Z probe sled
898
- *    • status LEDs
899
- */
900
-void setup() {
901
-
902
-  #ifdef DISABLE_JTAG
903
-    // Disable JTAG on AT90USB chips to free up pins for IO
904
-    MCUCR = 0x80;
905
-    MCUCR = 0x80;
906
-  #endif
907
-
908
-  #if ENABLED(FILAMENT_RUNOUT_SENSOR)
909
-    setup_filrunoutpin();
910
-  #endif
911
-
912
-  setup_killpin();
913
-
914
-  setup_powerhold();
915
-
916
-  #if HAS_STEPPER_RESET
917
-    disableStepperDrivers();
918
-  #endif
919
-
920
-  MYSERIAL.begin(BAUDRATE);
921
-  SERIAL_PROTOCOLLNPGM("start");
922
-  SERIAL_ECHO_START;
923
-
924
-  // Check startup - does nothing if bootloader sets MCUSR to 0
925
-  byte mcu = MCUSR;
926
-  if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
927
-  if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
928
-  if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
929
-  if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
930
-  if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
931
-  MCUSR = 0;
932
-
933
-  SERIAL_ECHOPGM(MSG_MARLIN);
934
-  SERIAL_ECHOLNPGM(" " SHORT_BUILD_VERSION);
935
-
936
-  #ifdef STRING_DISTRIBUTION_DATE
937
-    #ifdef STRING_CONFIG_H_AUTHOR
938
-      SERIAL_ECHO_START;
939
-      SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
940
-      SERIAL_ECHOPGM(STRING_DISTRIBUTION_DATE);
941
-      SERIAL_ECHOPGM(MSG_AUTHOR);
942
-      SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
943
-      SERIAL_ECHOPGM("Compiled: ");
944
-      SERIAL_ECHOLNPGM(__DATE__);
945
-    #endif // STRING_CONFIG_H_AUTHOR
946
-  #endif // STRING_DISTRIBUTION_DATE
947
-
948
-  SERIAL_ECHO_START;
949
-  SERIAL_ECHOPGM(MSG_FREE_MEMORY);
950
-  SERIAL_ECHO(freeMemory());
951
-  SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
952
-  SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
953
-
954
-  // Send "ok" after commands by default
955
-  for (int8_t i = 0; i < BUFSIZE; i++) send_ok[i] = true;
956
-
957
-  // Load data from EEPROM if available (or use defaults)
958
-  // This also updates variables in the planner, elsewhere
959
-  Config_RetrieveSettings();
960
-
961
-  // Initialize current position based on home_offset
962
-  memcpy(current_position, home_offset, sizeof(home_offset));
963
-
964
-  // Vital to init stepper/planner equivalent for current_position
965
-  SYNC_PLAN_POSITION_KINEMATIC();
966
-
967
-  thermalManager.init();    // Initialize temperature loop
968
-
969
-  #if ENABLED(USE_WATCHDOG)
970
-    watchdog_init();
971
-  #endif
972
-
973
-  stepper.init();    // Initialize stepper, this enables interrupts!
974
-  setup_photpin();
975
-  servo_init();
976
-
977
-  #if HAS_CONTROLLERFAN
978
-    SET_OUTPUT(CONTROLLERFAN_PIN); //Set pin used for driver cooling fan
979
-  #endif
980
-
981
-  #if HAS_STEPPER_RESET
982
-    enableStepperDrivers();
983
-  #endif
984
-
985
-  #if ENABLED(DIGIPOT_I2C)
986
-    digipot_i2c_init();
987
-  #endif
988
-
989
-  #if ENABLED(DAC_STEPPER_CURRENT)
990
-    dac_init();
991
-  #endif
992
-
993
-  #if ENABLED(Z_PROBE_SLED) && PIN_EXISTS(SLED)
994
-    pinMode(SLED_PIN, OUTPUT);
995
-    digitalWrite(SLED_PIN, LOW); // turn it off
996
-  #endif // Z_PROBE_SLED
997
-
998
-  setup_homepin();
999
-
1000
-  #ifdef STAT_LED_RED
1001
-    pinMode(STAT_LED_RED, OUTPUT);
1002
-    digitalWrite(STAT_LED_RED, LOW); // turn it off
1003
-  #endif
1004
-
1005
-  #ifdef STAT_LED_BLUE
1006
-    pinMode(STAT_LED_BLUE, OUTPUT);
1007
-    digitalWrite(STAT_LED_BLUE, LOW); // turn it off
1008
-  #endif
1009
-
1010
-  lcd_init();
1011
-  #if ENABLED(SHOW_BOOTSCREEN)
1012
-    #if ENABLED(DOGLCD)
1013
-      safe_delay(BOOTSCREEN_TIMEOUT);
1014
-    #elif ENABLED(ULTRA_LCD)
1015
-      bootscreen();
1016
-      lcd_init();
1017
-    #endif
1018
-  #endif
1019
-
1020
-  #if ENABLED(MIXING_EXTRUDER) && MIXING_VIRTUAL_TOOLS > 1
1021
-    // Initialize mixing to 100% color 1
1022
-    for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
1023
-      mixing_factor[i] = (i == 0) ? 1 : 0;
1024
-    for (uint8_t t = 0; t < MIXING_VIRTUAL_TOOLS; t++)
1025
-      for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
1026
-        mixing_virtual_tool_mix[t][i] = mixing_factor[i];
1027
-  #endif
1028
-
1029
-  #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
1030
-    i2c.onReceive(i2c_on_receive);
1031
-    i2c.onRequest(i2c_on_request);
1032
-  #endif
1033
-}
1034
-
1035
-/**
1036
- * The main Marlin program loop
1037
- *
1038
- *  - Save or log commands to SD
1039
- *  - Process available commands (if not saving)
1040
- *  - Call heater manager
1041
- *  - Call inactivity manager
1042
- *  - Call endstop manager
1043
- *  - Call LCD update
1044
- */
1045
-void loop() {
1046
-  if (commands_in_queue < BUFSIZE) get_available_commands();
1047
-
1048
-  #if ENABLED(SDSUPPORT)
1049
-    card.checkautostart(false);
1050
-  #endif
1051
-
1052
-  if (commands_in_queue) {
1053
-
1054
-    #if ENABLED(SDSUPPORT)
1055
-
1056
-      if (card.saving) {
1057
-        char* command = command_queue[cmd_queue_index_r];
1058
-        if (strstr_P(command, PSTR("M29"))) {
1059
-          // M29 closes the file
1060
-          card.closefile();
1061
-          SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
1062
-          ok_to_send();
1063
-        }
1064
-        else {
1065
-          // Write the string from the read buffer to SD
1066
-          card.write_command(command);
1067
-          if (card.logging)
1068
-            process_next_command(); // The card is saving because it's logging
1069
-          else
1070
-            ok_to_send();
1071
-        }
1072
-      }
1073
-      else
1074
-        process_next_command();
1075
-
1076
-    #else
1077
-
1078
-      process_next_command();
1079
-
1080
-    #endif // SDSUPPORT
1081
-
1082
-    // The queue may be reset by a command handler or by code invoked by idle() within a handler
1083
-    if (commands_in_queue) {
1084
-      --commands_in_queue;
1085
-      cmd_queue_index_r = (cmd_queue_index_r + 1) % BUFSIZE;
1086
-    }
1087
-  }
1088
-  endstops.report_state();
1089
-  idle();
1090
-}
1091
-
1092
 void gcode_line_error(const char* err, bool doFlush = true) {
878
 void gcode_line_error(const char* err, bool doFlush = true) {
1093
   SERIAL_ERROR_START;
879
   SERIAL_ERROR_START;
1094
   serialprintPGM(err);
880
   serialprintPGM(err);
8889
   for (uint8_t i = 0; i < COUNT(filament_size); i++)
8675
   for (uint8_t i = 0; i < COUNT(filament_size); i++)
8890
     volumetric_multiplier[i] = calculate_volumetric_multiplier(filament_size[i]);
8676
     volumetric_multiplier[i] = calculate_volumetric_multiplier(filament_size[i]);
8891
 }
8677
 }
8678
+
8679
+/**
8680
+ * Marlin entry-point: Set up before the program loop
8681
+ *  - Set up the kill pin, filament runout, power hold
8682
+ *  - Start the serial port
8683
+ *  - Print startup messages and diagnostics
8684
+ *  - Get EEPROM or default settings
8685
+ *  - Initialize managers for:
8686
+ *    • temperature
8687
+ *    • planner
8688
+ *    • watchdog
8689
+ *    • stepper
8690
+ *    • photo pin
8691
+ *    • servos
8692
+ *    • LCD controller
8693
+ *    • Digipot I2C
8694
+ *    • Z probe sled
8695
+ *    • status LEDs
8696
+ */
8697
+void setup() {
8698
+
8699
+  #ifdef DISABLE_JTAG
8700
+    // Disable JTAG on AT90USB chips to free up pins for IO
8701
+    MCUCR = 0x80;
8702
+    MCUCR = 0x80;
8703
+  #endif
8704
+
8705
+  #if ENABLED(FILAMENT_RUNOUT_SENSOR)
8706
+    setup_filrunoutpin();
8707
+  #endif
8708
+
8709
+  setup_killpin();
8710
+
8711
+  setup_powerhold();
8712
+
8713
+  #if HAS_STEPPER_RESET
8714
+    disableStepperDrivers();
8715
+  #endif
8716
+
8717
+  MYSERIAL.begin(BAUDRATE);
8718
+  SERIAL_PROTOCOLLNPGM("start");
8719
+  SERIAL_ECHO_START;
8720
+
8721
+  // Check startup - does nothing if bootloader sets MCUSR to 0
8722
+  byte mcu = MCUSR;
8723
+  if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
8724
+  if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
8725
+  if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
8726
+  if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
8727
+  if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
8728
+  MCUSR = 0;
8729
+
8730
+  SERIAL_ECHOPGM(MSG_MARLIN);
8731
+  SERIAL_ECHOLNPGM(" " SHORT_BUILD_VERSION);
8732
+
8733
+  #ifdef STRING_DISTRIBUTION_DATE
8734
+    #ifdef STRING_CONFIG_H_AUTHOR
8735
+      SERIAL_ECHO_START;
8736
+      SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
8737
+      SERIAL_ECHOPGM(STRING_DISTRIBUTION_DATE);
8738
+      SERIAL_ECHOPGM(MSG_AUTHOR);
8739
+      SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
8740
+      SERIAL_ECHOPGM("Compiled: ");
8741
+      SERIAL_ECHOLNPGM(__DATE__);
8742
+    #endif // STRING_CONFIG_H_AUTHOR
8743
+  #endif // STRING_DISTRIBUTION_DATE
8744
+
8745
+  SERIAL_ECHO_START;
8746
+  SERIAL_ECHOPGM(MSG_FREE_MEMORY);
8747
+  SERIAL_ECHO(freeMemory());
8748
+  SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
8749
+  SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
8750
+
8751
+  // Send "ok" after commands by default
8752
+  for (int8_t i = 0; i < BUFSIZE; i++) send_ok[i] = true;
8753
+
8754
+  // Load data from EEPROM if available (or use defaults)
8755
+  // This also updates variables in the planner, elsewhere
8756
+  Config_RetrieveSettings();
8757
+
8758
+  // Initialize current position based on home_offset
8759
+  memcpy(current_position, home_offset, sizeof(home_offset));
8760
+
8761
+  // Vital to init stepper/planner equivalent for current_position
8762
+  SYNC_PLAN_POSITION_KINEMATIC();
8763
+
8764
+  thermalManager.init();    // Initialize temperature loop
8765
+
8766
+  #if ENABLED(USE_WATCHDOG)
8767
+    watchdog_init();
8768
+  #endif
8769
+
8770
+  stepper.init();    // Initialize stepper, this enables interrupts!
8771
+  setup_photpin();
8772
+  servo_init();
8773
+
8774
+  #if HAS_BED_PROBE
8775
+    endstops.enable_z_probe(false);
8776
+  #endif
8777
+
8778
+  #if HAS_CONTROLLERFAN
8779
+    SET_OUTPUT(CONTROLLERFAN_PIN); //Set pin used for driver cooling fan
8780
+  #endif
8781
+
8782
+  #if HAS_STEPPER_RESET
8783
+    enableStepperDrivers();
8784
+  #endif
8785
+
8786
+  #if ENABLED(DIGIPOT_I2C)
8787
+    digipot_i2c_init();
8788
+  #endif
8789
+
8790
+  #if ENABLED(DAC_STEPPER_CURRENT)
8791
+    dac_init();
8792
+  #endif
8793
+
8794
+  #if ENABLED(Z_PROBE_SLED) && PIN_EXISTS(SLED)
8795
+    pinMode(SLED_PIN, OUTPUT);
8796
+    digitalWrite(SLED_PIN, LOW); // turn it off
8797
+  #endif // Z_PROBE_SLED
8798
+
8799
+  setup_homepin();
8800
+
8801
+  #ifdef STAT_LED_RED
8802
+    pinMode(STAT_LED_RED, OUTPUT);
8803
+    digitalWrite(STAT_LED_RED, LOW); // turn it off
8804
+  #endif
8805
+
8806
+  #ifdef STAT_LED_BLUE
8807
+    pinMode(STAT_LED_BLUE, OUTPUT);
8808
+    digitalWrite(STAT_LED_BLUE, LOW); // turn it off
8809
+  #endif
8810
+
8811
+  lcd_init();
8812
+  #if ENABLED(SHOW_BOOTSCREEN)
8813
+    #if ENABLED(DOGLCD)
8814
+      safe_delay(BOOTSCREEN_TIMEOUT);
8815
+    #elif ENABLED(ULTRA_LCD)
8816
+      bootscreen();
8817
+      lcd_init();
8818
+    #endif
8819
+  #endif
8820
+
8821
+  #if ENABLED(MIXING_EXTRUDER) && MIXING_VIRTUAL_TOOLS > 1
8822
+    // Initialize mixing to 100% color 1
8823
+    for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
8824
+      mixing_factor[i] = (i == 0) ? 1 : 0;
8825
+    for (uint8_t t = 0; t < MIXING_VIRTUAL_TOOLS; t++)
8826
+      for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
8827
+        mixing_virtual_tool_mix[t][i] = mixing_factor[i];
8828
+  #endif
8829
+
8830
+  #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
8831
+    i2c.onReceive(i2c_on_receive);
8832
+    i2c.onRequest(i2c_on_request);
8833
+  #endif
8834
+}
8835
+
8836
+/**
8837
+ * The main Marlin program loop
8838
+ *
8839
+ *  - Save or log commands to SD
8840
+ *  - Process available commands (if not saving)
8841
+ *  - Call heater manager
8842
+ *  - Call inactivity manager
8843
+ *  - Call endstop manager
8844
+ *  - Call LCD update
8845
+ */
8846
+void loop() {
8847
+  if (commands_in_queue < BUFSIZE) get_available_commands();
8848
+
8849
+  #if ENABLED(SDSUPPORT)
8850
+    card.checkautostart(false);
8851
+  #endif
8852
+
8853
+  if (commands_in_queue) {
8854
+
8855
+    #if ENABLED(SDSUPPORT)
8856
+
8857
+      if (card.saving) {
8858
+        char* command = command_queue[cmd_queue_index_r];
8859
+        if (strstr_P(command, PSTR("M29"))) {
8860
+          // M29 closes the file
8861
+          card.closefile();
8862
+          SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
8863
+          ok_to_send();
8864
+        }
8865
+        else {
8866
+          // Write the string from the read buffer to SD
8867
+          card.write_command(command);
8868
+          if (card.logging)
8869
+            process_next_command(); // The card is saving because it's logging
8870
+          else
8871
+            ok_to_send();
8872
+        }
8873
+      }
8874
+      else
8875
+        process_next_command();
8876
+
8877
+    #else
8878
+
8879
+      process_next_command();
8880
+
8881
+    #endif // SDSUPPORT
8882
+
8883
+    // The queue may be reset by a command handler or by code invoked by idle() within a handler
8884
+    if (commands_in_queue) {
8885
+      --commands_in_queue;
8886
+      cmd_queue_index_r = (cmd_queue_index_r + 1) % BUFSIZE;
8887
+    }
8888
+  }
8889
+  endstops.report_state();
8890
+  idle();
8891
+}

Loading…
Cancel
Save