diff --git a/Marlin/src/core/macros.h b/Marlin/src/core/macros.h index 1633d8c7e2..a3b12ac375 100644 --- a/Marlin/src/core/macros.h +++ b/Marlin/src/core/macros.h @@ -50,45 +50,58 @@ #define CYCLES_PER_MICROSECOND (F_CPU / 1000000L) // 16 or 20 on AVR #endif -// Highly granular delays for step pulses, etc. -#define DELAY_0_NOP NOOP -#define DELAY_1_NOP __asm__("nop\n\t") -#define DELAY_2_NOP do{ DELAY_1_NOP; DELAY_1_NOP; }while(0) -#define DELAY_3_NOP do{ DELAY_1_NOP; DELAY_2_NOP; }while(0) -#define DELAY_4_NOP do{ DELAY_1_NOP; DELAY_3_NOP; }while(0) -#define DELAY_5_NOP do{ DELAY_1_NOP; DELAY_4_NOP; }while(0) - +// Processor-level delays for hardware interfaces +#ifndef _NOP + #define _NOP() do { __asm__ volatile ("nop"); } while (0) +#endif #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; \ + case 20: _NOP(); case 19: _NOP(); case 18: _NOP(); case 17: _NOP(); \ + case 16: _NOP(); case 15: _NOP(); case 14: _NOP(); case 13: _NOP(); \ + case 12: _NOP(); case 11: _NOP(); case 10: _NOP(); case 9: _NOP(); \ + case 8: _NOP(); case 7: _NOP(); case 6: _NOP(); case 5: _NOP(); \ + case 4: _NOP(); case 3: _NOP(); case 2: _NOP(); case 1: _NOP(); \ } +#define DELAY_0_NOP NOOP +#define DELAY_1_NOP DELAY_NOPS( 1) +#define DELAY_2_NOP DELAY_NOPS( 2) +#define DELAY_3_NOP DELAY_NOPS( 3) +#define DELAY_4_NOP DELAY_NOPS( 4) +#define DELAY_5_NOP DELAY_NOPS( 5) +#define DELAY_10_NOP DELAY_NOPS(10) +#define DELAY_20_NOP DELAY_NOPS(20) -#define DELAY_10_NOP do{ DELAY_5_NOP; DELAY_5_NOP; }while(0) -#define DELAY_20_NOP do{ DELAY_10_NOP; DELAY_10_NOP; }while(0) - -#if CYCLES_PER_MICROSECOND == 16 - #define DELAY_1US do { DELAY_10_NOP; DELAY_5_NOP; DELAY_1_NOP; }while(0) +#if CYCLES_PER_MICROSECOND <= 200 + #define DELAY_100NS DELAY_NOPS((CYCLES_PER_MICROSECOND + 9) / 10) #else - #define DELAY_1US DELAY_20_NOP + #define DELAY_100NS DELAY_20_NOP #endif -#define DELAY_2US do{ DELAY_1US; DELAY_1US; }while(0) -#define DELAY_3US do{ DELAY_1US; DELAY_2US; }while(0) -#define DELAY_4US do{ DELAY_1US; DELAY_3US; }while(0) -#define DELAY_5US do{ DELAY_1US; DELAY_4US; }while(0) -#define DELAY_6US do{ DELAY_1US; DELAY_5US; }while(0) -#define DELAY_7US do{ DELAY_1US; DELAY_6US; }while(0) -#define DELAY_8US do{ DELAY_1US; DELAY_7US; }while(0) -#define DELAY_9US do{ DELAY_1US; DELAY_8US; }while(0) -#define DELAY_10US do{ DELAY_1US; DELAY_9US; }while(0) + +// Microsecond delays for hardware interfaces +#if CYCLES_PER_MICROSECOND <= 20 + #define DELAY_1US DELAY_NOPS(CYCLES_PER_MICROSECOND) + #define DELAY_US(X) \ + switch (X) { \ + case 20: DELAY_1US; case 19: DELAY_1US; case 18: DELAY_1US; case 17: DELAY_1US; \ + case 16: DELAY_1US; case 15: DELAY_1US; case 14: DELAY_1US; case 13: DELAY_1US; \ + case 12: DELAY_1US; case 11: DELAY_1US; case 10: DELAY_1US; case 9: DELAY_1US; \ + case 8: DELAY_1US; case 7: DELAY_1US; case 6: DELAY_1US; case 5: DELAY_1US; \ + case 4: DELAY_1US; case 3: DELAY_1US; case 2: DELAY_1US; case 1: DELAY_1US; \ + } +#else + #define DELAY_US(X) delayMicroseconds(X) // May not be usable in CRITICAL_SECTION + #define DELAY_1US DELAY_US(1) +#endif +#define DELAY_2US DELAY_US( 2) +#define DELAY_3US DELAY_US( 3) +#define DELAY_4US DELAY_US( 4) +#define DELAY_5US DELAY_US( 5) +#define DELAY_6US DELAY_US( 6) +#define DELAY_7US DELAY_US( 7) +#define DELAY_8US DELAY_US( 8) +#define DELAY_9US DELAY_US( 9) +#define DELAY_10US DELAY_US(10) +#define DELAY_20US DELAY_US(20) // Remove compiler warning on an unused variable #define UNUSED(x) (void) (x) diff --git a/Marlin/src/module/temperature.cpp b/Marlin/src/module/temperature.cpp index de5812dfd4..ad40433f87 100644 --- a/Marlin/src/module/temperature.cpp +++ b/Marlin/src/module/temperature.cpp @@ -1632,9 +1632,7 @@ void Temperature::disable_all_heaters() { WRITE(MAX6675_SS, 0); // enable TT_MAX6675 - // ensure 100ns delay - a bit extra is fine - asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz - asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz + DELAY_100NS; // Ensure 100ns delay // Read a big-endian temperature value max6675_temp = 0;