gnea\grbl-Mega  1.0f
Source Code Documentation ( Internal Workings )
main.c
Go to the documentation of this file.
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 
26 
27 
28 int main(void)
29 {
30 // Initialize system upon power-up.
31  serial_init();
32  settings_init();
33  stepper_init();
34  system_init();
35 
36  memset(sys_position,0,sizeof(sys_position));
37  sei();
38 
39 // Initialize system state.
40  #ifdef FORCE_INITIALIZATION_ALARM
41 // Force Grbl into an ALARM state upon a power-cycle or hard reset.
42  sys.state = STATE_ALARM;
43  #else
44  sys.state = STATE_IDLE;
45  #endif
46 
47 // Check for power-up and set system alarm if homing is enabled to force homing cycle
48 // by setting Grbl's alarm state. Alarm locks out all g-code commands, including the
49 // startup scripts, but allows access to settings and internal commands. Only a homing
50 // cycle '$H' or kill alarm locks '$X' will disable the alarm.
51 //
53 // not after disabling the alarm locks. Prevents motion startup blocks from crashing into
54 // things uncontrollably. Very bad.
55  #ifdef HOMING_INIT_LOCK
57  #endif
58 
59 // Grbl initialization loop upon power-up or a system abort. For the latter, all processes
60 // will return to this loop to be cleanly re-initialized.
61  for(;;) {
62 
63 // Reset system variables.
64  uint8_t prior_state = sys.state;
65  memset(&sys, 0, sizeof(system_t));
66  sys.state = prior_state;
71  sys_probe_state = 0;
76 
77 // Reset Grbl primary systems.
79  gc_init();
80  spindle_init();
81  coolant_init();
82  limits_init();
83  probe_init();
84  sleep_init();
85  plan_reset();
86  st_reset();
87 
88 // Sync cleared gcode and planner positions to current system position.
91 
92 // Print welcome message. Indicates an initialization has occured at power-up or with a reset.
94 
95 // Start Grbl main loop. Processes program inputs and executes them.
97 
98  }
99  return 0;
100 }
volatile uint8_t sys_probe_state
Probing state value. Used to coordinate the probing cycle with stepper ISR.
Definition: system.h:133
void protocol_main_loop()
Starts Grbl main loop. It handles all incoming characters from the serial port and executes...
Definition: protocol.c:37
void report_init_message()
Welcome message.
Definition: report.c:166
#define DEFAULT_FEED_OVERRIDE
Definition: config.h:210
void plan_sync_position()
Reset the planner position vectors. Called by the system abort/initialization routine.
Definition: planner.c:471
void limits_init()
Initialize the limits module.
Definition: limits.c:32
#define DEFAULT_RAPID_OVERRIDE
100%. Don't change this value.
Definition: config.h:216
void spindle_init()
Initializes spindle pins and hardware PWM, if enabled.
uint8_t spindle_speed_ovr
Spindle speed value in percent.
Definition: system.h:121
void gc_sync_position()
Sets g-code parser position in mm. Input in steps. Called by the system abort and hard...
Definition: gcode.c:53
Define global system variables.
Definition: system.h:111
void stepper_init()
Initialize and start the stepper motor subsystem.
Definition: stepper.c:500
void serial_reset_read_buffer()
Reset and empty data in read buffer. Used by e-stop and reset.
Definition: serial.c:195
volatile uint8_t sys_rt_exec_alarm
Global realtime executor bitflag variable for setting various alarms.
Definition: system.h:135
uint8_t state
Tracks the current system state of Grbl.
Definition: system.h:112
#define DEFAULT_SPINDLE_SPEED_OVERRIDE
100%. Don't change this value.
Definition: config.h:221
void plan_reset()
Initialize and reset the motion plan subsystem.
Definition: planner.c:198
uint8_t f_override
Feed rate override value in percent.
Definition: system.h:119
#define BITFLAG_HOMING_ENABLE
Definition: settings.h:39
volatile uint8_t sys_rt_exec_motion_override
Global realtime executor bitflag variable for motion-based overrides.
Definition: system.h:136
void coolant_init()
Initializes coolant control pins.
void gc_init()
Initialize the parser.
Definition: gcode.c:41
uint8_t flags
Contains default boolean settings.
Definition: settings.h:92
void probe_init()
Probe pin initialization routine.
Definition: probe.c:27
#define STATE_ALARM
In alarm state. Locks out all g-code processes. Allows settings access.
Definition: system.h:73
system_t sys
Declare system global variable structure.
Definition: main.c:25
int32_t sys_position[N_AXIS]
NOTE: These position variables may need to be declared as volatiles, if problems arise.
Definition: system.h:130
volatile uint8_t sys_rt_exec_state
Global realtime executor bitflag variable for state management. See EXEC bitmasks.
Definition: system.h:134
void serial_init()
Definition: serial.c:64
memset(pl_data, 0, sizeof(plan_line_data_t))
Zero pl_data struct.
void settings_init()
Initialize the config subsystem.
Definition: settings.c:299
uint8_t r_override
Rapids override value in percent.
Definition: system.h:120
settings_t settings
Definition: settings.c:24
void sleep_init()
Initialization routine for sleep timer.
Definition: sleep.c:40
int32_t sys_probe_position[N_AXIS]
Last probe position in machine coordinates and steps.
Definition: system.h:131
void system_init()
Initialize the serial protocol.
Definition: system.c:24
volatile uint8_t sys_rt_exec_accessory_override
Global realtime executor bitflag variable for spindle/coolant overrides.
Definition: system.h:137
int main(void)
Definition: main.c:28
void st_reset()
Reset and clear stepper subsystem variables.
Definition: stepper.c:476
#define STATE_IDLE
Define system state bit map. The state variable primarily tracks the individual functions.
Definition: system.h:72
#define bit_istrue(x, mask)
Definition: nuts_bolts.h:60