Browse Source

Add HAL_timer_start for AVR, use stepper timer to time pulses

Scott Lahteine 6 years ago
parent
commit
3701869e6c

+ 6
- 4
Marlin/src/HAL/HAL_AVR/HAL.cpp View File

@@ -34,6 +34,7 @@
34 34
 // --------------------------------------------------------------------------
35 35
 
36 36
 #include "../../inc/MarlinConfig.h"
37
+#include "HAL.h"
37 38
 
38 39
 // --------------------------------------------------------------------------
39 40
 // Externals
@@ -74,9 +75,11 @@
74 75
 // --------------------------------------------------------------------------
75 76
 
76 77
 #if ENABLED(SDSUPPORT)
78
+
77 79
   #include "../../sd/SdFatUtil.h"
78 80
   int freeMemory() { return SdFatUtil::FreeRam(); }
79
-#else
81
+
82
+#else // !SDSUPPORT
80 83
 
81 84
 extern "C" {
82 85
   extern char __bss_end;
@@ -93,7 +96,6 @@ extern "C" {
93 96
   }
94 97
 }
95 98
 
96
-#endif //!SDSUPPORT
97
-
98
-#endif
99
+#endif // !SDSUPPORT
99 100
 
101
+#endif // __AVR__

+ 32
- 3
Marlin/src/HAL/HAL_AVR/HAL.h View File

@@ -124,7 +124,7 @@ extern "C" {
124 124
 
125 125
 #define STEP_TIMER_NUM          1
126 126
 #define TEMP_TIMER_NUM          0
127
-#define PULSE_TIMER_NUM         TEMP_TIMER_NUM
127
+#define PULSE_TIMER_NUM         STEP_TIMER_NUM
128 128
 
129 129
 #define HAL_STEPPER_TIMER_RATE  HAL_TIMER_RATE
130 130
 #define HAL_TICKS_PER_US        ((HAL_STEPPER_TIMER_RATE) / 1000000) // Cannot be of type double
@@ -139,7 +139,7 @@ extern "C" {
139 139
 #define TIMER_OCR_0             OCR0A
140 140
 #define TIMER_COUNTER_0         TCNT0
141 141
 
142
-#define PULSE_TIMER_PRESCALE    8
142
+#define PULSE_TIMER_PRESCALE    STEPPER_TIMER_PRESCALE
143 143
 
144 144
 #define ENABLE_STEPPER_DRIVER_INTERRUPT()  SBI(TIMSK1, OCIE1A)
145 145
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
@@ -149,7 +149,36 @@ extern "C" {
149 149
 #define DISABLE_TEMPERATURE_INTERRUPT()    CBI(TIMSK0, OCIE0B)
150 150
 #define TEMPERATURE_ISR_ENABLED()         TEST(TIMSK0, OCIE0B)
151 151
 
152
-#define HAL_timer_start(timer_num, frequency)
152
+FORCE_INLINE void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
153
+  UNUSED(frequency);
154
+  switch (timer_num) {
155
+    case STEP_TIMER_NUM:
156
+      // waveform generation = 0100 = CTC
157
+      SET_WGM(1, CTC_OCRnA);
158
+
159
+      // output mode = 00 (disconnected)
160
+      SET_COMA(1, NORMAL);
161
+
162
+      // Set the timer pre-scaler
163
+      // Generally we use a divider of 8, resulting in a 2MHz timer
164
+      // frequency on a 16MHz MCU. If you are going to change this, be
165
+      // sure to regenerate speed_lookuptable.h with
166
+      // create_speed_lookuptable.py
167
+      SET_CS(1, PRESCALER_8);  //  CS 2 = 1/8 prescaler
168
+
169
+      // Init Stepper ISR to 122 Hz for quick starting
170
+      // (F_CPU) / (STEPPER_TIMER_PRESCALE) / frequency
171
+      OCR1A = 0x4000;
172
+      TCNT1 = 0;
173
+      break;
174
+
175
+    case TEMP_TIMER_NUM:
176
+      // Use timer0 for temperature measurement
177
+      // Interleave temperature interrupt with millies interrupt
178
+      OCR0B = 128;
179
+      break;
180
+  }
181
+}
153 182
 
154 183
 #define _CAT(a, ...) a ## __VA_ARGS__
155 184
 #define HAL_timer_set_compare(timer, compare) (_CAT(TIMER_OCR_, timer) = compare)

+ 2
- 21
Marlin/src/module/stepper.cpp View File

@@ -1993,27 +1993,8 @@ void Stepper::init() {
1993 1993
     E_AXIS_INIT(4);
1994 1994
   #endif
1995 1995
 
1996
-  #ifdef __AVR__
1997
-    // waveform generation = 0100 = CTC
1998
-    SET_WGM(1, CTC_OCRnA);
1999
-
2000
-    // output mode = 00 (disconnected)
2001
-    SET_COMA(1, NORMAL);
2002
-
2003
-    // Set the timer pre-scaler
2004
-    // Generally we use a divider of 8, resulting in a 2MHz timer
2005
-    // frequency on a 16MHz MCU. If you are going to change this, be
2006
-    // sure to regenerate speed_lookuptable.h with
2007
-    // create_speed_lookuptable.py
2008
-    SET_CS(1, PRESCALER_8);  //  CS 2 = 1/8 prescaler
2009
-
2010
-    // Init Stepper ISR to 122 Hz for quick starting
2011
-    OCR1A = 0x4000;
2012
-    TCNT1 = 0;
2013
-  #else
2014
-    // Init Stepper ISR to 122 Hz for quick starting
2015
-    HAL_timer_start(STEP_TIMER_NUM, 122);
2016
-  #endif
1996
+  // Init Stepper ISR to 122 Hz for quick starting
1997
+  HAL_timer_start(STEP_TIMER_NUM, 122);
2017 1998
 
2018 1999
   ENABLE_STEPPER_DRIVER_INTERRUPT();
2019 2000
 

+ 1
- 8
Marlin/src/module/temperature.cpp View File

@@ -1234,14 +1234,7 @@ void Temperature::init() {
1234 1234
     HAL_ANALOG_SELECT(FILWIDTH_PIN);
1235 1235
   #endif
1236 1236
 
1237
-  // todo: HAL: fix abstraction
1238
-  #ifdef __AVR__
1239
-    // Use timer0 for temperature measurement
1240
-    // Interleave temperature interrupt with millies interrupt
1241
-    OCR0B = 128;
1242
-  #else
1243
-    HAL_timer_start(TEMP_TIMER_NUM, TEMP_TIMER_FREQUENCY);
1244
-  #endif
1237
+  HAL_timer_start(TEMP_TIMER_NUM, TEMP_TIMER_FREQUENCY);
1245 1238
   ENABLE_TEMPERATURE_INTERRUPT();
1246 1239
 
1247 1240
   #if HAS_AUTO_FAN_0

Loading…
Cancel
Save