Sfoglia il codice sorgente

Timing functions micros2 and delay2 + fix of delay_keep_alive().

Robert Pelnar 6 anni fa
parent
commit
fc0b55b213
5 ha cambiato i file con 71 aggiunte e 9 eliminazioni
  1. 1 0
      Firmware/Marlin.h
  2. 2 2
      Firmware/Marlin_main.cpp
  3. 0 5
      Firmware/temperature.cpp
  4. 37 2
      Firmware/timer02.c
  5. 31 0
      Firmware/timer02.h

+ 1 - 0
Firmware/Marlin.h

@@ -17,6 +17,7 @@
 #include <avr/eeprom.h>
 #include <avr/interrupt.h>
 
+#include "timer02.h"
 
 #include "fastio.h"
 #include "Configuration.h"

+ 2 - 2
Firmware/Marlin_main.cpp

@@ -7809,10 +7809,10 @@ void delay_keep_alive(unsigned int ms)
         if (ms == 0)
             break;
         else if (ms >= 50) {
-            delay(50);
+            delay2(50);
             ms -= 50;
         } else {
-            delay(ms);
+			delay2(ms);
             ms = 0;
         }
     }

+ 0 - 5
Firmware/temperature.cpp

@@ -45,11 +45,6 @@
 #include "Configuration_prusa.h"
 
 
-extern "C" {
-extern void timer02_init(void);
-extern void timer02_set_pwm0(uint8_t pwm0);
-}
-
 
 //===========================================================================
 //=============================public variables============================

+ 37 - 2
Firmware/timer02.c

@@ -87,7 +87,6 @@ ISR(TIMER2_OVF_vect)
 	// (volatile variables must be read from memory on every access)
 	unsigned long m = timer0_millis;
 	unsigned char f = timer0_fract;
-
 	m += MILLIS_INC;
 	f += FRACT_INC;
 	if (f >= FRACT_MAX)
@@ -95,9 +94,45 @@ ISR(TIMER2_OVF_vect)
 		f -= FRACT_MAX;
 		m += 1;
 	}
-
 	timer0_fract = f;
 	timer0_millis = m;
 	timer0_overflow_count++;
 }
 
+unsigned long micros2()
+{
+	unsigned long m;
+	uint8_t oldSREG = SREG, t;
+	cli();
+	m = timer0_overflow_count;
+#if defined(TCNT2)
+	t = TCNT2;
+#elif defined(TCNT2L)
+	t = TCNT2L;
+#else
+	#error TIMER 2 not defined
+#endif
+#ifdef TIFR2
+	if ((TIFR2 & _BV(TOV2)) && (t < 255))
+		m++;
+#else
+	if ((TIFR & _BV(TOV2)) && (t < 255))
+		m++;
+#endif
+	SREG = oldSREG;	
+	return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
+}
+
+void delay2(unsigned long ms)
+{
+	uint32_t start = micros2();
+	while (ms > 0)
+	{
+		yield();
+		while ( ms > 0 && (micros2() - start) >= 1000)
+		{
+			ms--;
+			start += 1000;
+		}
+	}
+}

+ 31 - 0
Firmware/timer02.h

@@ -0,0 +1,31 @@
+//timer02.h
+// use atmega timer2 as main system timer instead of timer0
+// timer0 is used for fast pwm (OC0B output)
+// original OVF handler is disabled
+#ifndef TIMER02_H
+#define TIMER02_H
+
+#include <inttypes.h>
+
+
+#if defined(__cplusplus)
+extern "C" {
+#endif //defined(__cplusplus)
+
+
+extern uint8_t timer02_pwm0;
+
+extern void timer02_set_pwm0(uint8_t pwm0);
+
+extern void timer02_init(void);
+
+extern unsigned long micros2(void);
+
+extern void delay2(unsigned long ms);
+
+
+#if defined(__cplusplus)
+}
+#endif //defined(__cplusplus)
+
+#endif //TIMER02_H