Browse Source

🐛 Fix AVR 644/1284 Timer / PWM conflicts (#23629)

Mike La Spina 2 years ago
parent
commit
127a3ad831
No account linked to committer's email address
3 changed files with 24 additions and 26 deletions
  1. 9
    11
      Marlin/src/HAL/AVR/HAL_SPI.cpp
  2. 4
    4
      Marlin/src/HAL/AVR/fast_pwm.cpp
  3. 11
    11
      Marlin/src/HAL/AVR/timers.h

+ 9
- 11
Marlin/src/HAL/AVR/HAL_SPI.cpp View File

@@ -35,22 +35,20 @@
35 35
 
36 36
 void spiBegin() {
37 37
   #if PIN_EXISTS(SD_SS)
38
-    OUT_WRITE(SD_SS_PIN, HIGH);
38
+    // Do not init HIGH for boards with pin 4 used as Fans or Heaters or otherwise, not likely to have multiple SPI devices anyway.
39
+    #if defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || defined(__AVR_ATmega1284P__)
40
+      // SS must be in output mode even it is not chip select
41
+      SET_OUTPUT(SD_SS_PIN);
42
+    #else
43
+      // set SS high - may be chip select for another SPI device
44
+      OUT_WRITE(SD_SS_PIN, HIGH);
45
+    #endif
39 46
   #endif
40 47
   SET_OUTPUT(SD_SCK_PIN);
41 48
   SET_INPUT(SD_MISO_PIN);
42 49
   SET_OUTPUT(SD_MOSI_PIN);
43 50
 
44
-  #if DISABLED(SOFTWARE_SPI)
45
-    // SS must be in output mode even it is not chip select
46
-    //SET_OUTPUT(SD_SS_PIN);
47
-    // set SS high - may be chip select for another SPI device
48
-    //#if SET_SPI_SS_HIGH
49
-      //WRITE(SD_SS_PIN, HIGH);
50
-    //#endif
51
-    // set a default rate
52
-    spiInit(1);
53
-  #endif
51
+  IF_DISABLED(SOFTWARE_SPI, spiInit(SPI_HALF_SPEED));
54 52
 }
55 53
 
56 54
 #if NONE(SOFTWARE_SPI, FORCE_SOFT_SPI)

+ 4
- 4
Marlin/src/HAL/AVR/fast_pwm.cpp View File

@@ -70,7 +70,7 @@ const Timer get_pwm_timer(const pin_t pin) {
70 70
 
71 71
     #ifdef TCCR0A
72 72
       case TIMER0B:   // Protected timer, but allow setting the duty cycle on OCR0B for pin D4 only
73
-        return Timer({ { &TCCR0A, nullptr, nullptr }, { (uint16_t*)&OCR0B, nullptr, nullptr }, nullptr, 0, 0, true, true });
73
+        return Timer({ { &TCCR0A, nullptr, nullptr }, { (uint16_t*)&OCR0A, (uint16_t*)&OCR0B, nullptr }, nullptr, 0, 1, true, true });
74 74
     #endif
75 75
 
76 76
     #if HAS_TCCR2
@@ -187,8 +187,8 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
187 187
     const Timer timer = get_pwm_timer(pin);
188 188
     if (timer.isPWM) {
189 189
       if (timer.n == 0) {
190
-        TCCR0A |= _BV(COM0B1); // Only allow a TIMER0B select and OCR0B duty update for pin D4 outputs no frequency changes are permited.
191
-        OCR0B = v;
190
+        _SET_COMnQ(timer, timer.q, COM_CLEAR_SET);  // Only allow a TIMER0B select...
191
+        _SET_OCRnQ(timer, timer.q, v);              // ...and OCR0B duty update. For output pin D4 no frequency changes are permitted.
192 192
       }
193 193
       else if (!timer.isProtected) {
194 194
         const uint16_t top = timer.n == 2 ? TERN(USE_OCR2A_AS_TOP, *timer.OCRnQ[0], 255) : *timer.ICRn;
@@ -197,7 +197,7 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
197 197
       }
198 198
     }
199 199
     else
200
-      digitalWrite(pin, v < 128 ? LOW : HIGH);
200
+      digitalWrite(pin, v < v_size / 2 ? LOW : HIGH);
201 201
   }
202 202
 }
203 203
 

+ 11
- 11
Marlin/src/HAL/AVR/timers.h View File

@@ -58,9 +58,9 @@ typedef uint16_t hal_timer_t;
58 58
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
59 59
 #define STEPPER_ISR_ENABLED()             TEST(TIMSK1, OCIE1A)
60 60
 
61
-#define ENABLE_TEMPERATURE_INTERRUPT()     SBI(TIMSK0, OCIE0B)
62
-#define DISABLE_TEMPERATURE_INTERRUPT()    CBI(TIMSK0, OCIE0B)
63
-#define TEMPERATURE_ISR_ENABLED()         TEST(TIMSK0, OCIE0B)
61
+#define ENABLE_TEMPERATURE_INTERRUPT()     SBI(TIMSK0, OCIE0A)
62
+#define DISABLE_TEMPERATURE_INTERRUPT()    CBI(TIMSK0, OCIE0A)
63
+#define TEMPERATURE_ISR_ENABLED()         TEST(TIMSK0, OCIE0A)
64 64
 
65 65
 FORCE_INLINE void HAL_timer_start(const uint8_t timer_num, const uint32_t) {
66 66
   switch (timer_num) {
@@ -87,7 +87,7 @@ FORCE_INLINE void HAL_timer_start(const uint8_t timer_num, const uint32_t) {
87 87
     case MF_TIMER_TEMP:
88 88
       // Use timer0 for temperature measurement
89 89
       // Interleave temperature interrupt with millies interrupt
90
-      OCR0B = 128;
90
+      OCR0A = 128;
91 91
       break;
92 92
   }
93 93
 }
@@ -180,7 +180,7 @@ void TIMER1_COMPA_vect() { \
180 180
     :                                   \
181 181
     : [timsk0] "i" ((uint16_t)&TIMSK0), \
182 182
       [timsk1] "i" ((uint16_t)&TIMSK1), \
183
-      [msk0] "M" ((uint8_t)(1<<OCIE0B)),\
183
+      [msk0] "M" ((uint8_t)(1<<OCIE0A)),\
184 184
       [msk1] "M" ((uint8_t)(1<<OCIE1A)) \
185 185
     : \
186 186
   ); \
@@ -193,9 +193,9 @@ void TIMER1_COMPA_vect_bottom()
193 193
 
194 194
 /* 14 cycles maximum latency */
195 195
 #define HAL_TEMP_TIMER_ISR() \
196
-extern "C" void TIMER0_COMPB_vect() __attribute__ ((signal, naked, used, externally_visible)); \
197
-extern "C" void TIMER0_COMPB_vect_bottom()  asm ("TIMER0_COMPB_vect_bottom") __attribute__ ((used, externally_visible, noinline)); \
198
-void TIMER0_COMPB_vect() { \
196
+extern "C" void TIMER0_COMPA_vect() __attribute__ ((signal, naked, used, externally_visible)); \
197
+extern "C" void TIMER0_COMPA_vect_bottom()  asm ("TIMER0_COMPA_vect_bottom") __attribute__ ((used, externally_visible, noinline)); \
198
+void TIMER0_COMPA_vect() { \
199 199
   __asm__ __volatile__ ( \
200 200
     A("push r16")                       /* 2 Save R16 */ \
201 201
     A("in r16, __SREG__")               /* 1 Get SREG */ \
@@ -223,7 +223,7 @@ void TIMER0_COMPB_vect() { \
223 223
     A("push r30")                       \
224 224
     A("push r31")                       \
225 225
     A("clr r1")                         /* C runtime expects this register to be 0 */ \
226
-    A("call TIMER0_COMPB_vect_bottom")  /* Call the bottom handler - No inlining allowed, otherwise registers used are not saved */   \
226
+    A("call TIMER0_COMPA_vect_bottom")  /* Call the bottom handler - No inlining allowed, otherwise registers used are not saved */   \
227 227
     A("pop r31")                        \
228 228
     A("pop r30")                        \
229 229
     A("pop r27")                        \
@@ -251,10 +251,10 @@ void TIMER0_COMPB_vect() { \
251 251
     A("reti")                           /* 4 Return from interrupt */ \
252 252
     :                                   \
253 253
     : [timsk0] "i"((uint16_t)&TIMSK0),  \
254
-      [msk0] "M" ((uint8_t)(1<<OCIE0B)) \
254
+      [msk0] "M" ((uint8_t)(1<<OCIE0A)) \
255 255
     : \
256 256
   ); \
257 257
 } \
258
-void TIMER0_COMPB_vect_bottom()
258
+void TIMER0_COMPA_vect_bottom()
259 259
 
260 260
 #endif // HAL_TEMP_TIMER_ISR

Loading…
Cancel
Save