Luigi Santivetti | 69972f9 | 2019-11-12 22:55:40 +0000 | [diff] [blame^] | 1 | /* |
| 2 | main.c - An embedded CNC Controller with rs274/ngc (g-code) support |
| 3 | Part of Grbl |
| 4 | |
| 5 | Copyright (c) 2011-2016 Sungeun K. Jeon for Gnea Research LLC |
| 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud |
| 7 | |
| 8 | Grbl is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | Grbl is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Grbl. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | #include "grbl.h" |
| 23 | |
| 24 | |
| 25 | // Declare system global variable structure |
| 26 | system_t sys; |
| 27 | int32_t sys_position[N_AXIS]; // Real-time machine (aka home) position vector in steps. |
| 28 | int32_t sys_probe_position[N_AXIS]; // Last probe position in machine coordinates and steps. |
| 29 | volatile uint8_t sys_probe_state; // Probing state value. Used to coordinate the probing cycle with stepper ISR. |
| 30 | volatile uint8_t sys_rt_exec_state; // Global realtime executor bitflag variable for state management. See EXEC bitmasks. |
| 31 | volatile uint8_t sys_rt_exec_alarm; // Global realtime executor bitflag variable for setting various alarms. |
| 32 | volatile uint8_t sys_rt_exec_motion_override; // Global realtime executor bitflag variable for motion-based overrides. |
| 33 | volatile uint8_t sys_rt_exec_accessory_override; // Global realtime executor bitflag variable for spindle/coolant overrides. |
| 34 | #ifdef DEBUG |
| 35 | volatile uint8_t sys_rt_exec_debug; |
| 36 | #endif |
| 37 | |
| 38 | |
| 39 | int main(void) |
| 40 | { |
| 41 | // Initialize system upon power-up. |
| 42 | serial_init(); // Setup serial baud rate and interrupts |
| 43 | settings_init(); // Load Grbl settings from EEPROM |
| 44 | stepper_init(); // Configure stepper pins and interrupt timers |
| 45 | system_init(); // Configure pinout pins and pin-change interrupt |
| 46 | |
| 47 | memset(sys_position,0,sizeof(sys_position)); // Clear machine position. |
| 48 | sei(); // Enable interrupts |
| 49 | |
| 50 | // Initialize system state. |
| 51 | #ifdef FORCE_INITIALIZATION_ALARM |
| 52 | // Force Grbl into an ALARM state upon a power-cycle or hard reset. |
| 53 | sys.state = STATE_ALARM; |
| 54 | #else |
| 55 | sys.state = STATE_IDLE; |
| 56 | #endif |
| 57 | |
| 58 | // Check for power-up and set system alarm if homing is enabled to force homing cycle |
| 59 | // by setting Grbl's alarm state. Alarm locks out all g-code commands, including the |
| 60 | // startup scripts, but allows access to settings and internal commands. Only a homing |
| 61 | // cycle '$H' or kill alarm locks '$X' will disable the alarm. |
| 62 | // NOTE: The startup script will run after successful completion of the homing cycle, but |
| 63 | // not after disabling the alarm locks. Prevents motion startup blocks from crashing into |
| 64 | // things uncontrollably. Very bad. |
| 65 | #ifdef HOMING_INIT_LOCK |
| 66 | if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_ALARM; } |
| 67 | #endif |
| 68 | |
| 69 | // Grbl initialization loop upon power-up or a system abort. For the latter, all processes |
| 70 | // will return to this loop to be cleanly re-initialized. |
| 71 | for(;;) { |
| 72 | |
| 73 | // Reset system variables. |
| 74 | uint8_t prior_state = sys.state; |
| 75 | memset(&sys, 0, sizeof(system_t)); // Clear system struct variable. |
| 76 | sys.state = prior_state; |
| 77 | sys.f_override = DEFAULT_FEED_OVERRIDE; // Set to 100% |
| 78 | sys.r_override = DEFAULT_RAPID_OVERRIDE; // Set to 100% |
| 79 | sys.spindle_speed_ovr = DEFAULT_SPINDLE_SPEED_OVERRIDE; // Set to 100% |
| 80 | memset(sys_probe_position,0,sizeof(sys_probe_position)); // Clear probe position. |
| 81 | sys_probe_state = 0; |
| 82 | sys_rt_exec_state = 0; |
| 83 | sys_rt_exec_alarm = 0; |
| 84 | sys_rt_exec_motion_override = 0; |
| 85 | sys_rt_exec_accessory_override = 0; |
| 86 | |
| 87 | // Reset Grbl primary systems. |
| 88 | serial_reset_read_buffer(); // Clear serial read buffer |
| 89 | gc_init(); // Set g-code parser to default state |
| 90 | spindle_init(); |
| 91 | coolant_init(); |
| 92 | limits_init(); |
| 93 | probe_init(); |
| 94 | plan_reset(); // Clear block buffer and planner variables |
| 95 | st_reset(); // Clear stepper subsystem variables. |
| 96 | |
| 97 | // Sync cleared gcode and planner positions to current system position. |
| 98 | plan_sync_position(); |
| 99 | gc_sync_position(); |
| 100 | |
| 101 | // Print welcome message. Indicates an initialization has occured at power-up or with a reset. |
| 102 | report_init_message(); |
| 103 | |
| 104 | // Start Grbl main loop. Processes program inputs and executes them. |
| 105 | protocol_main_loop(); |
| 106 | |
| 107 | } |
| 108 | return 0; /* Never reached */ |
| 109 | } |