Skip to content

Commit c02bc38

Browse files
descipherthinkyhead
authored andcommitted
🐛 Fix AVR 644/1284 Timer / PWM conflicts (#23629)
1 parent 68dfc50 commit c02bc38

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

Marlin/src/HAL/AVR/HAL_SPI.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,20 @@
3535

3636
void spiBegin() {
3737
#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
3946
#endif
4047
SET_OUTPUT(SD_SCK_PIN);
4148
SET_INPUT(SD_MISO_PIN);
4249
SET_OUTPUT(SD_MOSI_PIN);
4350

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));
5452
}
5553

5654
#if NONE(SOFTWARE_SPI, FORCE_SOFT_SPI)

Marlin/src/HAL/AVR/fast_pwm.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const Timer get_pwm_timer(const pin_t pin) {
7070

7171
#ifdef TCCR0A
7272
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 });
7474
#endif
7575

7676
#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
187187
const Timer timer = get_pwm_timer(pin);
188188
if (timer.isPWM) {
189189
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.
192192
}
193193
else if (!timer.isProtected) {
194194
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
197197
}
198198
}
199199
else
200-
digitalWrite(pin, v < 128 ? LOW : HIGH);
200+
digitalWrite(pin, v < v_size / 2 ? LOW : HIGH);
201201
}
202202
}
203203

Marlin/src/HAL/AVR/timers.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ typedef uint16_t hal_timer_t;
5858
#define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
5959
#define STEPPER_ISR_ENABLED() TEST(TIMSK1, OCIE1A)
6060

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)
6464

6565
FORCE_INLINE void HAL_timer_start(const uint8_t timer_num, const uint32_t) {
6666
switch (timer_num) {
@@ -87,7 +87,7 @@ FORCE_INLINE void HAL_timer_start(const uint8_t timer_num, const uint32_t) {
8787
case MF_TIMER_TEMP:
8888
// Use timer0 for temperature measurement
8989
// Interleave temperature interrupt with millies interrupt
90-
OCR0B = 128;
90+
OCR0A = 128;
9191
break;
9292
}
9393
}
@@ -180,7 +180,7 @@ void TIMER1_COMPA_vect() { \
180180
: \
181181
: [timsk0] "i" ((uint16_t)&TIMSK0), \
182182
[timsk1] "i" ((uint16_t)&TIMSK1), \
183-
[msk0] "M" ((uint8_t)(1<<OCIE0B)),\
183+
[msk0] "M" ((uint8_t)(1<<OCIE0A)),\
184184
[msk1] "M" ((uint8_t)(1<<OCIE1A)) \
185185
: \
186186
); \
@@ -193,9 +193,9 @@ void TIMER1_COMPA_vect_bottom()
193193

194194
/* 14 cycles maximum latency */
195195
#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() { \
199199
__asm__ __volatile__ ( \
200200
A("push r16") /* 2 Save R16 */ \
201201
A("in r16, __SREG__") /* 1 Get SREG */ \
@@ -223,7 +223,7 @@ void TIMER0_COMPB_vect() { \
223223
A("push r30") \
224224
A("push r31") \
225225
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 */ \
227227
A("pop r31") \
228228
A("pop r30") \
229229
A("pop r27") \
@@ -251,10 +251,10 @@ void TIMER0_COMPB_vect() { \
251251
A("reti") /* 4 Return from interrupt */ \
252252
: \
253253
: [timsk0] "i"((uint16_t)&TIMSK0), \
254-
[msk0] "M" ((uint8_t)(1<<OCIE0B)) \
254+
[msk0] "M" ((uint8_t)(1<<OCIE0A)) \
255255
: \
256256
); \
257257
} \
258-
void TIMER0_COMPB_vect_bottom()
258+
void TIMER0_COMPA_vect_bottom()
259259

260260
#endif // HAL_TEMP_TIMER_ISR

0 commit comments

Comments
 (0)