blob: 4e5b66e302cbb093e10713273c11f84716901b40 [file] [log] [blame]
Luigi Santivetti69972f92019-11-12 22:55:40 +00001/*
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
26system_t sys;
27int32_t sys_position[N_AXIS]; // Real-time machine (aka home) position vector in steps.
28int32_t sys_probe_position[N_AXIS]; // Last probe position in machine coordinates and steps.
29volatile uint8_t sys_probe_state; // Probing state value. Used to coordinate the probing cycle with stepper ISR.
30volatile uint8_t sys_rt_exec_state; // Global realtime executor bitflag variable for state management. See EXEC bitmasks.
31volatile uint8_t sys_rt_exec_alarm; // Global realtime executor bitflag variable for setting various alarms.
32volatile uint8_t sys_rt_exec_motion_override; // Global realtime executor bitflag variable for motion-based overrides.
33volatile 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
39int 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
Luigi Santivetti42a1b332019-11-17 22:31:46 +000045 #ifdef USE_ADC
46 adc_data adc;
47 adc_init(&adc); // Setup ADC module with defaults
48 #endif
Luigi Santivetti69972f92019-11-12 22:55:40 +000049 system_init(); // Configure pinout pins and pin-change interrupt
50
51 memset(sys_position,0,sizeof(sys_position)); // Clear machine position.
52 sei(); // Enable interrupts
53
54 // Initialize system state.
55 #ifdef FORCE_INITIALIZATION_ALARM
56 // Force Grbl into an ALARM state upon a power-cycle or hard reset.
57 sys.state = STATE_ALARM;
58 #else
59 sys.state = STATE_IDLE;
60 #endif
61
62 // Check for power-up and set system alarm if homing is enabled to force homing cycle
63 // by setting Grbl's alarm state. Alarm locks out all g-code commands, including the
64 // startup scripts, but allows access to settings and internal commands. Only a homing
65 // cycle '$H' or kill alarm locks '$X' will disable the alarm.
66 // NOTE: The startup script will run after successful completion of the homing cycle, but
67 // not after disabling the alarm locks. Prevents motion startup blocks from crashing into
68 // things uncontrollably. Very bad.
69 #ifdef HOMING_INIT_LOCK
70 if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_ALARM; }
71 #endif
72
73 // Grbl initialization loop upon power-up or a system abort. For the latter, all processes
74 // will return to this loop to be cleanly re-initialized.
75 for(;;) {
76
77 // Reset system variables.
78 uint8_t prior_state = sys.state;
79 memset(&sys, 0, sizeof(system_t)); // Clear system struct variable.
80 sys.state = prior_state;
81 sys.f_override = DEFAULT_FEED_OVERRIDE; // Set to 100%
82 sys.r_override = DEFAULT_RAPID_OVERRIDE; // Set to 100%
83 sys.spindle_speed_ovr = DEFAULT_SPINDLE_SPEED_OVERRIDE; // Set to 100%
84 memset(sys_probe_position,0,sizeof(sys_probe_position)); // Clear probe position.
85 sys_probe_state = 0;
86 sys_rt_exec_state = 0;
87 sys_rt_exec_alarm = 0;
88 sys_rt_exec_motion_override = 0;
89 sys_rt_exec_accessory_override = 0;
90
91 // Reset Grbl primary systems.
92 serial_reset_read_buffer(); // Clear serial read buffer
93 gc_init(); // Set g-code parser to default state
94 spindle_init();
95 coolant_init();
96 limits_init();
97 probe_init();
98 plan_reset(); // Clear block buffer and planner variables
99 st_reset(); // Clear stepper subsystem variables.
100
101 // Sync cleared gcode and planner positions to current system position.
102 plan_sync_position();
103 gc_sync_position();
104
105 // Print welcome message. Indicates an initialization has occured at power-up or with a reset.
106 report_init_message();
107
108 // Start Grbl main loop. Processes program inputs and executes them.
109 protocol_main_loop();
110
111 }
112 return 0; /* Never reached */
113}