From e02a5a637112817601abcc7f7188bfa2bc3e8b5c Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Tue, 11 Apr 2017 02:33:46 -0500 Subject: [PATCH] Add macros for delays by machine cycle --- Marlin/macros.h | 40 +++++++++++++++++++++++++++++ Marlin/servo.h | 2 +- Marlin/ultralcd_st7920_u8glib_rrd.h | 7 ----- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/Marlin/macros.h b/Marlin/macros.h index d84c277d58..a916908358 100644 --- a/Marlin/macros.h +++ b/Marlin/macros.h @@ -39,6 +39,46 @@ // Clock speed factor #define CYCLES_PER_MICROSECOND (F_CPU / 1000000UL) // 16 or 20 +// Highly granular delays for step pulses, etc. +#define DELAY_0_NOP NOOP +#define DELAY_1_NOP __asm__("nop\n\t") +#define DELAY_2_NOP DELAY_1_NOP; DELAY_1_NOP +#define DELAY_3_NOP DELAY_1_NOP; DELAY_2_NOP +#define DELAY_4_NOP DELAY_1_NOP; DELAY_3_NOP +#define DELAY_5_NOP DELAY_1_NOP; DELAY_4_NOP + +#define DELAY_NOPS(X) \ + switch (X) { \ + case 20: DELAY_1_NOP; case 19: DELAY_1_NOP; \ + case 18: DELAY_1_NOP; case 17: DELAY_1_NOP; \ + case 16: DELAY_1_NOP; case 15: DELAY_1_NOP; \ + case 14: DELAY_1_NOP; case 13: DELAY_1_NOP; \ + case 12: DELAY_1_NOP; case 11: DELAY_1_NOP; \ + case 10: DELAY_1_NOP; case 9: DELAY_1_NOP; \ + case 8: DELAY_1_NOP; case 7: DELAY_1_NOP; \ + case 6: DELAY_1_NOP; case 5: DELAY_1_NOP; \ + case 4: DELAY_1_NOP; case 3: DELAY_1_NOP; \ + case 2: DELAY_1_NOP; case 1: DELAY_1_NOP; \ + } + +#define DELAY_10_NOP DELAY_5_NOP; DELAY_5_NOP +#define DELAY_20_NOP DELAY_10_NOP; DELAY_10_NOP + +#if CYCLES_PER_MICROSECOND == 16 + #define DELAY_1US DELAY_10_NOP; DELAY_5_NOP; DELAY_1_NOP +#else + #define DELAY_1US DELAY_20_NOP +#endif +#define DELAY_2US DELAY_1US; DELAY_1US +#define DELAY_3US DELAY_1US; DELAY_2US +#define DELAY_4US DELAY_1US; DELAY_3US +#define DELAY_5US DELAY_1US; DELAY_4US +#define DELAY_6US DELAY_1US; DELAY_5US +#define DELAY_7US DELAY_1US; DELAY_6US +#define DELAY_8US DELAY_1US; DELAY_7US +#define DELAY_9US DELAY_1US; DELAY_8US +#define DELAY_10US DELAY_1US; DELAY_9US + // Remove compiler warning on an unused variable #define UNUSED(x) (void) (x) diff --git a/Marlin/servo.h b/Marlin/servo.h index 07532bc303..ad75b6baab 100644 --- a/Marlin/servo.h +++ b/Marlin/servo.h @@ -82,7 +82,7 @@ // Say which 16 bit timers can be used and in what order #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) - //#define _useTimer1 + //#define _useTimer1 // Timer 1 is used by the stepper ISR #define _useTimer3 #define _useTimer4 #if !HAS_MOTOR_CURRENT_PWM diff --git a/Marlin/ultralcd_st7920_u8glib_rrd.h b/Marlin/ultralcd_st7920_u8glib_rrd.h index 66e1f17224..cb063f8e99 100644 --- a/Marlin/ultralcd_st7920_u8glib_rrd.h +++ b/Marlin/ultralcd_st7920_u8glib_rrd.h @@ -43,13 +43,6 @@ //set optimization so ARDUINO optimizes this file #pragma GCC optimize (3) -#define DELAY_0_NOP NOOP -#define DELAY_1_NOP __asm__("nop\n\t") -#define DELAY_2_NOP __asm__("nop\n\t" "nop\n\t") -#define DELAY_3_NOP __asm__("nop\n\t" "nop\n\t" "nop\n\t") -#define DELAY_4_NOP __asm__("nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t") - - // If you want you can define your own set of delays in Configuration.h //#define ST7920_DELAY_1 DELAY_0_NOP //#define ST7920_DELAY_2 DELAY_0_NOP