Selaa lähdekoodia

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

Mike La Spina 3 vuotta sitten
vanhempi
commit
127a3ad831
No account linked to committer's email address
3 muutettua tiedostoa jossa 24 lisäystä ja 26 poistoa
  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 Näytä tiedosto

35
 
35
 
36
 void spiBegin() {
36
 void spiBegin() {
37
   #if PIN_EXISTS(SD_SS)
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
   #endif
46
   #endif
40
   SET_OUTPUT(SD_SCK_PIN);
47
   SET_OUTPUT(SD_SCK_PIN);
41
   SET_INPUT(SD_MISO_PIN);
48
   SET_INPUT(SD_MISO_PIN);
42
   SET_OUTPUT(SD_MOSI_PIN);
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
 #if NONE(SOFTWARE_SPI, FORCE_SOFT_SPI)
54
 #if NONE(SOFTWARE_SPI, FORCE_SOFT_SPI)

+ 4
- 4
Marlin/src/HAL/AVR/fast_pwm.cpp Näytä tiedosto

70
 
70
 
71
     #ifdef TCCR0A
71
     #ifdef TCCR0A
72
       case TIMER0B:   // Protected timer, but allow setting the duty cycle on OCR0B for pin D4 only
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
     #endif
74
     #endif
75
 
75
 
76
     #if HAS_TCCR2
76
     #if HAS_TCCR2
187
     const Timer timer = get_pwm_timer(pin);
187
     const Timer timer = get_pwm_timer(pin);
188
     if (timer.isPWM) {
188
     if (timer.isPWM) {
189
       if (timer.n == 0) {
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
       else if (!timer.isProtected) {
193
       else if (!timer.isProtected) {
194
         const uint16_t top = timer.n == 2 ? TERN(USE_OCR2A_AS_TOP, *timer.OCRnQ[0], 255) : *timer.ICRn;
194
         const uint16_t top = timer.n == 2 ? TERN(USE_OCR2A_AS_TOP, *timer.OCRnQ[0], 255) : *timer.ICRn;
197
       }
197
       }
198
     }
198
     }
199
     else
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 Näytä tiedosto

58
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
58
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
59
 #define STEPPER_ISR_ENABLED()             TEST(TIMSK1, OCIE1A)
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
 FORCE_INLINE void HAL_timer_start(const uint8_t timer_num, const uint32_t) {
65
 FORCE_INLINE void HAL_timer_start(const uint8_t timer_num, const uint32_t) {
66
   switch (timer_num) {
66
   switch (timer_num) {
87
     case MF_TIMER_TEMP:
87
     case MF_TIMER_TEMP:
88
       // Use timer0 for temperature measurement
88
       // Use timer0 for temperature measurement
89
       // Interleave temperature interrupt with millies interrupt
89
       // Interleave temperature interrupt with millies interrupt
90
-      OCR0B = 128;
90
+      OCR0A = 128;
91
       break;
91
       break;
92
   }
92
   }
93
 }
93
 }
180
     :                                   \
180
     :                                   \
181
     : [timsk0] "i" ((uint16_t)&TIMSK0), \
181
     : [timsk0] "i" ((uint16_t)&TIMSK0), \
182
       [timsk1] "i" ((uint16_t)&TIMSK1), \
182
       [timsk1] "i" ((uint16_t)&TIMSK1), \
183
-      [msk0] "M" ((uint8_t)(1<<OCIE0B)),\
183
+      [msk0] "M" ((uint8_t)(1<<OCIE0A)),\
184
       [msk1] "M" ((uint8_t)(1<<OCIE1A)) \
184
       [msk1] "M" ((uint8_t)(1<<OCIE1A)) \
185
     : \
185
     : \
186
   ); \
186
   ); \
193
 
193
 
194
 /* 14 cycles maximum latency */
194
 /* 14 cycles maximum latency */
195
 #define HAL_TEMP_TIMER_ISR() \
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
   __asm__ __volatile__ ( \
199
   __asm__ __volatile__ ( \
200
     A("push r16")                       /* 2 Save R16 */ \
200
     A("push r16")                       /* 2 Save R16 */ \
201
     A("in r16, __SREG__")               /* 1 Get SREG */ \
201
     A("in r16, __SREG__")               /* 1 Get SREG */ \
223
     A("push r30")                       \
223
     A("push r30")                       \
224
     A("push r31")                       \
224
     A("push r31")                       \
225
     A("clr r1")                         /* C runtime expects this register to be 0 */ \
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
     A("pop r31")                        \
227
     A("pop r31")                        \
228
     A("pop r30")                        \
228
     A("pop r30")                        \
229
     A("pop r27")                        \
229
     A("pop r27")                        \
251
     A("reti")                           /* 4 Return from interrupt */ \
251
     A("reti")                           /* 4 Return from interrupt */ \
252
     :                                   \
252
     :                                   \
253
     : [timsk0] "i"((uint16_t)&TIMSK0),  \
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
 #endif // HAL_TEMP_TIMER_ISR
260
 #endif // HAL_TEMP_TIMER_ISR

Loading…
Peruuta
Tallenna