/* System.c - Handles system level commands and real-time processes Part of Grbl-Advanced Copyright (c) 2014-2016 Sungeun K. Jeon for Gnea Research LLC Copyright (c) 2017-2024 Patrick F. Grbl-Advanced 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. Grbl-Advanced 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. You should have received a copy of the GNU General Public License along with Grbl-Advanced. If not, see . */ #include #include #include #include "Config.h" #include "GCode.h" #include "GPIO.h" #include "MotionControl.h" #include "Protocol.h" #include "Report.h" #include "Settings.h" #include "Stepper.h" #include "System.h" #include "ToolChange.h" #include "System32.h" // Declare system global variable structure System_t sys; // Real-time machine (aka home) position vector in steps. int32_t sys_position[N_AXIS]; // Last probe position in machine coordinates and steps. int32_t sys_probe_position[N_AXIS]; // Probing state value. Used to coordinate the probing cycle with stepper ISR. volatile uint8_t sys_probe_state; // Global realtime executor bitflag variable for state management. See EXEC bitmasks. volatile uint16_t sys_rt_exec_state; // Global realtime executor bitflag variable for setting various alarms. volatile uint8_t sys_rt_exec_alarm; // Global realtime executor bitflag variable for motion-based overrides. volatile uint8_t sys_rt_exec_motion_override; // Global realtime executor bitflag variable for spindle/coolant overrides. volatile uint8_t sys_rt_exec_accessory_override; //static uint8_t initial_state = 0; static uint8_t last_state = 0; void System_Init(void) { GPIO_InitGPIO(GPIO_SYSTEM); last_state = 0; sys.system_flags |= BITFLAG_ENABLE_SYSTEM_INPUT; System_GetControlState(false); } void System_Clear(void) { // Clear system struct variable. memset(&sys, 0, sizeof(System_t)); // Set overrides to 100% sys.f_override = DEFAULT_FEED_OVERRIDE; sys.r_override = DEFAULT_RAPID_OVERRIDE; sys.spindle_speed_ovr = DEFAULT_SPINDLE_SPEED_OVERRIDE; sys.system_flags |= BITFLAG_ENABLE_SYSTEM_INPUT; } void System_ResetPosition(void) { // Clear machine position. memset(sys_position, 0 , sizeof(sys_position)); } // Returns control pin state as a uint8 bitfield. Each bit indicates the input pin state, where // triggered is 1 and not triggered is 0. Invert mask is applied. Bitfield organization is // defined by the CONTROL_PIN_INDEX in the header file. uint8_t System_GetControlState(bool held) { uint8_t control_state = 0; uint8_t pin = ((GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)< 255)) { return STATUS_INVALID_STATEMENT; } return Settings_StoreGlobalSetting((uint8_t)parameter, value); } } } // If '$' command makes it to here, then everything's ok. return STATUS_OK; } void System_FlagWcoChange(void) { #ifdef FORCE_BUFFER_SYNC_DURING_WCO_CHANGE Protocol_BufferSynchronize(); #endif sys.report_wco_counter = 0; } // Returns machine position of axis 'idx'. Must be sent a 'step' array. // NOTE: If motor steps and machine position are not in the same coordinate frame, this function // serves as a central place to compute the transformation. float System_ConvertAxisSteps2Mpos(const int32_t *steps, const uint8_t idx) { float pos = 0.0; if (steps) { #ifdef COREXY if (idx == X_AXIS) { pos = (float)system_convert_corexy_to_x_axis_steps(steps) / settings.steps_per_mm[idx]; } else if (idx == Y_AXIS) { pos = (float)system_convert_corexy_to_y_axis_steps(steps) / settings.steps_per_mm[idx]; } else { pos = steps[idx] / settings.steps_per_mm[idx]; } #else if (settings.steps_per_mm[idx] > 0.0) { pos = steps[idx] / settings.steps_per_mm[idx]; } #endif } return pos; } void System_ConvertArraySteps2Mpos(float *position, const int32_t *steps) { if (position) { for (uint8_t idx = 0; idx < N_AXIS; idx++) { position[idx] = System_ConvertAxisSteps2Mpos(steps, idx); } } return; } // CoreXY calculation only. Returns x or y-axis "steps" based on CoreXY motor steps. #ifdef COREXY int32_t system_convert_corexy_to_x_axis_steps(const int32_t *steps) { return ((steps[A_MOTOR] + steps[B_MOTOR])/2); } int32_t system_convert_corexy_to_y_axis_steps(const int32_t *steps) { return ((steps[A_MOTOR] - steps[B_MOTOR])/2); } #endif // Checks and reports if target array exceeds machine travel limits. uint8_t System_CheckTravelLimits(const float *target) { for (uint8_t idx = 0; idx < N_AXIS; idx++) { if (BIT_IS_TRUE(settings.flags_ext, BITFLAG_HOMING_FORCE_SET_ORIGIN)) { // When homing forced set origin is enabled, soft limits checks need to account for directionality. // NOTE: max_travel is stored as negative if (BIT_IS_TRUE(settings.homing_dir_mask, BIT(idx))) { if (target[idx] < 0 || target[idx] > -settings.max_travel[idx]) { return true; } } else { if (target[idx] > 0 || target[idx] < settings.max_travel[idx]) { return true; } } } else { // NOTE: max_travel is stored as negative if (target[idx] > 0 || target[idx] < settings.max_travel[idx]) { return true; } } } return false; } // Special handlers for setting and clearing Grbl's real-time execution flags. inline void System_SetExecStateFlag(uint16_t mask) { uint32_t primask = __get_PRIMASK(); __disable_irq(); sys_rt_exec_state |= (mask); __set_PRIMASK(primask); } inline void System_ClearExecStateFlag(uint16_t mask) { uint32_t primask = __get_PRIMASK(); __disable_irq(); sys_rt_exec_state &= ~(mask); __set_PRIMASK(primask); } inline void System_SetExecAlarm(uint8_t code) { uint32_t primask = __get_PRIMASK(); __disable_irq(); sys_rt_exec_alarm = code; __set_PRIMASK(primask); } inline void System_ClearExecAlarm(void) { uint32_t primask = __get_PRIMASK(); __disable_irq(); sys_rt_exec_alarm = 0; __set_PRIMASK(primask); } inline void System_SetExecMotionOverrideFlag(uint8_t mask) { uint32_t primask = __get_PRIMASK(); __disable_irq(); sys_rt_exec_motion_override |= (mask); __set_PRIMASK(primask); } inline void System_SetExecAccessoryOverrideFlag(uint8_t mask) { uint32_t primask = __get_PRIMASK(); __disable_irq(); sys_rt_exec_accessory_override |= (mask); __set_PRIMASK(primask); } inline void System_ClearExecMotionOverride(void) { uint32_t primask = __get_PRIMASK(); __disable_irq(); sys_rt_exec_motion_override = 0; __set_PRIMASK(primask); } inline void System_ClearExecAccessoryOverrides(void) { uint32_t primask = __get_PRIMASK(); __disable_irq(); sys_rt_exec_accessory_override = 0; __set_PRIMASK(primask); }