Skip to content

Commit

Permalink
reduce code size
Browse files Browse the repository at this point in the history
  • Loading branch information
Paciente8159 committed Sep 25, 2024
1 parent ad9ca42 commit 40c3e23
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 84 deletions.
8 changes: 8 additions & 0 deletions uCNC/src/cnc_hal_config_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ extern "C"

#ifdef ENABLE_RT_PROBE_CHECKING
#undef PROBE_ISR
#ifdef mcu_enable_probe_isr
#undef mcu_enable_probe_isr
#define mcu_enable_probe_isr()
#endif
#ifdef mcu_disable_probe_isr
#undef mcu_disable_probe_isr
#define mcu_disable_probe_isr()
#endif
#endif

#ifdef ENABLE_RT_LIMITS_CHECKING
Expand Down
54 changes: 9 additions & 45 deletions uCNC/src/core/interpolator.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,11 +612,12 @@ void itp_run(void)
segm_steps = (uint16_t)(remaining_steps - profile_steps_limit);
}

// The DSS (Dynamic Step Spread) algorithm reduces stepper vibration by spreading step distribution at lower speads.
// This is done by oversampling the Bresenham line algorithm by multiple factors of 2.
// This way stepping actions fire in different moments in order to reduce vibration caused by the stepper internal mechanics.
// This works in a similar way to Grbl's AMASS but has a modified implementation to minimize the processing penalty on the ISR and also take less static memory.
// DSS never loads the step generating ISR with a frequency above half of the absolute maximum frequency
// The DSS (Dynamic Step Spread) algorithm reduces stepper vibration by spreading step distribution at lower speads.
// This is done by oversampling the Bresenham line algorithm by multiple factors of 2.
// This way stepping actions fire in different moments in order to reduce vibration caused by the stepper internal mechanics.
// This works in a similar way to Grbl's AMASS but has a modified implementation to minimize the processing penalty on the ISR and also take less static memory.
// DSS never loads the step generating ISR with a frequency above half of the absolute maximum frequency
float max_step_rate = 1000000.f / g_settings.max_step_rate;
#if (DSS_MAX_OVERSAMPLING != 0)
float dss_speed = MAX(INTERPOLATOR_FREQ, current_speed);
uint8_t dss = 0;
Expand All @@ -627,7 +628,7 @@ void itp_run(void)
dss_speed = fast_flt_mul2(dss_speed);
// clamp top speed
current_speed = fast_flt_mul2(current_speed);
current_speed = MIN(current_speed, g_settings.max_step_rate);
current_speed = MIN(current_speed, max_step_rate);
dss = 1;
}
#endif
Expand All @@ -646,11 +647,11 @@ void itp_run(void)

// completes the segment information (step speed, steps) and updates the block
sgm->remaining_steps = segm_steps << dss;
dss_speed = MIN(dss_speed, g_settings.max_step_rate);
dss_speed = MIN(dss_speed, max_step_rate);
mcu_freq_to_clocks(dss_speed, &(sgm->timer_counter), &(sgm->timer_prescaller));
#else
sgm->remaining_steps = segm_steps;
current_speed = MIN(current_speed, g_settings.max_step_rate);
current_speed = MIN(current_speed, max_step_rate);
mcu_freq_to_clocks(MAX(INTERPOLATOR_FREQ, current_speed), &(sgm->timer_counter), &(sgm->timer_prescaller));
#endif

Expand Down Expand Up @@ -1328,43 +1329,6 @@ MCU_CALLBACK void mcu_step_cb(void)
#endif
}

// void itp_nomotion(uint8_t type, uint16_t delay)
// {
// while (itp_sgm_is_full())
// {
// if (!cnc_dotasks())
// {
// return;
// }
// }

// itp_sgm_data[itp_sgm_data_write].block = NULL;
// //clicks every 100ms (10Hz)
// if (delay)
// {
// mcu_freq_to_clocks(10, &(itp_sgm_data[itp_sgm_data_write].timer_counter), &(itp_sgm_data[itp_sgm_data_write].timer_prescaller));
// }
// else
// {
// mcu_freq_to_clocks(g_settings.max_step_rate, &(itp_sgm_data[itp_sgm_data_write].timer_counter), &(itp_sgm_data[itp_sgm_data_write].timer_prescaller));
// }
// itp_sgm_data[itp_sgm_data_write].remaining_steps = MAX(delay, 0);
// itp_sgm_data[itp_sgm_data_write].feed = 0;
// itp_sgm_data[itp_sgm_data_write].flags = type;
// #if TOOL_COUNT > 0
// if (g_settings.laser_mode)
// {
// itp_sgm_data[itp_sgm_data_write].spindle = 0;
// itp_sgm_data[itp_sgm_data_write].spindle_inv = false;
// }
// else
// {
// planner_get_spindle_speed(1, &(itp_sgm_data[itp_sgm_data_write].spindle), &(itp_sgm_data[itp_sgm_data_write].spindle_inv));
// }
// #endif
// itp_sgm_buffer_write();
// }

void itp_start(bool is_synched)
{
// starts the step isr if is stopped and there are segments to execute
Expand Down
4 changes: 2 additions & 2 deletions uCNC/src/core/io_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ void io_enable_probe(void)
#ifdef ENABLE_IO_MODULES
EVENT_INVOKE(probe_enable, NULL);
#endif
#ifndef FORCE_SOFT_POLLING
#if !defined(FORCE_SOFT_POLLING) && defined(PROBE_ISR)
mcu_enable_probe_isr();
#endif
io_probe_enabled = true;
Expand All @@ -407,7 +407,7 @@ void io_disable_probe(void)
{
#if ASSERT_PIN(PROBE)
io_probe_enabled = false;
#ifndef FORCE_SOFT_POLLING
#if !defined(FORCE_SOFT_POLLING) && defined(PROBE_ISR)
mcu_disable_probe_isr();
#endif
#ifdef ENABLE_IO_MODULES
Expand Down
4 changes: 4 additions & 0 deletions uCNC/src/hal/boards/avr/boardmap_uno.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ extern "C"
#define DISABLE_MULTISTREAM_SERIAL
#endif

#ifndef DISABLE_RTC_CODE
#define DISABLE_RTC_CODE
#endif

#ifdef EMULATE_GRBL_STARTUP
#undef EMULATE_GRBL_STARTUP
#define EMULATE_GRBL_STARTUP 2
Expand Down
3 changes: 0 additions & 3 deletions uCNC/src/interface/grbl_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,6 @@ void grbl_protocol_cnc_settings(void)
protocol_busy = true;
uint8_t count = settings_count();

// id 0 conversion from frequency to us
grbl_protocol_gcode_setting_line_flt(0, 1000000.f / g_settings.max_step_rate);

for (uint8_t i = 0; i < count; i++)
{
setting_id_t s = {0};
Expand Down
4 changes: 4 additions & 0 deletions uCNC/src/interface/grbl_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ static grbl_stream_getc_cb stream_getc;
static grbl_stream_available_cb stream_available;
static grbl_stream_clear_cb stream_clear;

static FORCEINLINE void grbl_stream_flush(void);

#ifndef DISABLE_MULTISTREAM_SERIAL
grbl_stream_t *default_stream;
static grbl_stream_t *current_stream;
Expand Down Expand Up @@ -266,6 +268,8 @@ uint8_t grbl_stream_write_available(void)
return (RX_BUFFER_SIZE - grbl_stream_available());
}



void grbl_stream_clear(void)
{
#ifndef DISABLE_MULTISTREAM_SERIAL
Expand Down
1 change: 0 additions & 1 deletion uCNC/src/interface/grbl_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ extern "C"
void grbl_stream_start_broadcast(void);
void grbl_stream_putc(char c);
void grbl_stream_printf(const char* fmt, ...);
void grbl_stream_flush(void);

char grbl_stream_getc(void);
char grbl_stream_peek(void);
Expand Down
40 changes: 11 additions & 29 deletions uCNC/src/interface/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,6 @@ static void print_putc(print_putc_cb cb, char **buffer_ref, char c)
}
}

static void print_str(print_putc_cb cb, char **buffer_ref, const char *__s)
{
while (*__s)
{
print_putc(cb, buffer_ref, *__s++);
}
}

static void print_romstr(print_putc_cb cb, char **buffer_ref, const char *__s)
{
char c = rom_read_byte(__s);
while (c)
{
print_putc(cb, buffer_ref, c);
__s++;
c = rom_read_byte(__s);
}
}

#ifndef PRINT_DISABLE_FMT_HEX
static void print_byte(print_putc_cb cb, char **buffer_ref, const uint8_t *data, uint8_t flags)
{
Expand Down Expand Up @@ -179,15 +160,11 @@ void print_fmtva(print_putc_cb cb, char *buffer, const char *fmt, va_list *args)
int i = 0;
int32_t li = 0;
float f, *f_ptr = NULL;
char **buffer_ref = NULL;

char *ptr = buffer;
char **buffer_ref = (!ptr) ? NULL : &ptr;
uint8_t elems = 0;

if (ptr)
{
buffer_ref = &ptr;
}

do
{
c = printf_getc(fmt++);
Expand Down Expand Up @@ -242,12 +219,17 @@ void print_fmtva(print_putc_cb cb, char *buffer, const char *fmt, va_list *args)
/* code */
break;
case 's':
s = (char *)va_arg(*args, char *);
print_str(cb, buffer_ref, s);
break;
case 'S':
s = (const char *)va_arg(*args, const char *);
print_romstr(cb, buffer_ref, s);
for (;;)
{
cval = (c == 's') ? *s++ : rom_read_byte(s++);
if (!cval)
{
break;
}
print_putc(cb, buffer_ref, cval);
}
break;
#ifndef PRINT_DISABLE_FMT_IP
case 'M':
Expand Down
7 changes: 3 additions & 4 deletions uCNC/src/interface/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static uint8_t crc7(uint8_t c, uint8_t crc)
const settings_t __rom__ default_settings =
{
.version = SETTINGS_VERSION,
.max_step_rate = F_STEP_MAX,
.max_step_rate = (1000000.0f / F_STEP_MAX),
#ifdef ENABLE_STEPPERS_DISABLE_TIMEOUT
.step_disable_timeout = 0,
#endif
Expand Down Expand Up @@ -166,7 +166,7 @@ const settings_t __rom__ default_settings =
};

const setting_id_t __rom__ g_settings_id_table[] = {
// {.id = 0, .memptr = &g_settings.max_step_rate, .type = SETTING_TYPE(0)},
{.id = 0, .memptr = &g_settings.max_step_rate, .type = SETTING_TYPE(0)},
#ifdef ENABLE_STEPPERS_DISABLE_TIMEOUT
{.id = 1, .memptr = &g_settings.step_disable_timeout, .type = SETTING_TYPE(3)},
#endif
Expand Down Expand Up @@ -487,8 +487,7 @@ uint8_t settings_change(setting_offset_t id, float value)
// id 0 conversion from us to frequency
if (id == 0)
{
value = 1000000.0f / value;
if (value > F_STEP_MAX)
if (value < (1000000.0f / F_STEP_MAX))
{
return STATUS_MAX_STEP_RATE_EXCEEDED;
}
Expand Down

0 comments on commit 40c3e23

Please sign in to comment.