/*
system.c - Handles system level commands and real-time processes
Part of Grbl
Copyright (c) 2014-2016 Sungeun K. Jeon for Gnea Research LLC
Grbl 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 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. If not, see .
*/
#include "grbl.h"
void system_init()
{
CONTROL_DDR &= ~(CONTROL_MASK); // Configure as input pins
#ifdef DISABLE_CONTROL_PIN_PULL_UP
CONTROL_PORT &= ~(CONTROL_MASK); // Normal low operation. Requires external pull-down.
#else
CONTROL_PORT |= CONTROL_MASK; // Enable internal pull-up resistors. Normal high operation.
#endif
CONTROL_PCMSK |= CONTROL_MASK; // Enable specific pins of the Pin Change Interrupt
PCICR |= (1 << CONTROL_INT); // Enable Pin Change Interrupt
}
// 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_control_get_state()
{
uint8_t control_state = 0;
uint8_t pin = (CONTROL_PIN & CONTROL_MASK);
#ifdef INVERT_CONTROL_PIN_MASK
pin ^= INVERT_CONTROL_PIN_MASK;
#endif
if (pin) {
#ifdef ENABLE_SAFETY_DOOR_INPUT_PIN
if (bit_isfalse(pin,(1< 255)) { return(STATUS_INVALID_STATEMENT); }
return(settings_store_global_setting((uint8_t)parameter, value));
}
}
}
return(STATUS_OK); // If '$' command makes it to here, then everything's ok.
}
void system_flag_wco_change()
{
#ifdef FORCE_BUFFER_SYNC_DURING_WCO_CHANGE
protocol_buffer_synchronize();
#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_convert_axis_steps_to_mpos(int32_t *steps, uint8_t idx)
{
float pos;
#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
pos = steps[idx]/settings.steps_per_mm[idx];
#endif
return(pos);
}
void system_convert_array_steps_to_mpos(float *position, int32_t *steps)
{
uint8_t idx;
for (idx=0; 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); }
#endif
}
return(false);
}
// Special handlers for setting and clearing Grbl's real-time execution flags.
void system_set_exec_state_flag(uint8_t mask) {
uint8_t en = __get_PRIMASK();
__disable_irq();
sys_rt_exec_state |= (mask);
if(en) __enable_irq();
}
void system_clear_exec_state_flag(uint8_t mask) {
uint8_t en = __get_PRIMASK();
__disable_irq();
sys_rt_exec_state &= ~(mask);
if(en) __enable_irq();
}
void system_set_exec_alarm(uint8_t code) {
uint8_t en = __get_PRIMASK();
__disable_irq();
sys_rt_exec_alarm = code;
if(en) __enable_irq();
}
void system_clear_exec_alarm() {
uint8_t en = __get_PRIMASK();
__disable_irq();
sys_rt_exec_alarm = 0;
if(en) __enable_irq();
}
void system_set_exec_motion_override_flag(uint8_t mask) {
uint8_t en = __get_PRIMASK();
__disable_irq();
sys_rt_exec_motion_override |= (mask);
if(en) __enable_irq();
}
void system_set_exec_accessory_override_flag(uint8_t mask) {
uint8_t en = __get_PRIMASK();
__disable_irq();
sys_rt_exec_accessory_override |= (mask);
if(en) __enable_irq();
}
void system_clear_exec_motion_overrides() {
uint8_t en = __get_PRIMASK();
__disable_irq();
sys_rt_exec_motion_override = 0;
if(en) __enable_irq();
}
void system_clear_exec_accessory_overrides() {
uint8_t en = __get_PRIMASK();
__disable_irq();
sys_rt_exec_accessory_override = 0;
if(en) __enable_irq();
}