Kaynağa Gözat

Tone fixes/changes (#10151)

Bob-the-Kuhn 7 yıl önce
ebeveyn
işleme
53362b81cc

+ 9
- 8
Marlin/src/HAL/HAL_DUE/HAL_timers_Due.cpp Dosyayı Görüntüle

61
 // --------------------------------------------------------------------------
61
 // --------------------------------------------------------------------------
62
 
62
 
63
 const tTimerConfig TimerConfig [NUM_HARDWARE_TIMERS] = {
63
 const tTimerConfig TimerConfig [NUM_HARDWARE_TIMERS] = {
64
-  { TC0, 0, TC0_IRQn, 0},  // 0 - [servo timer5]
65
-  { TC0, 1, TC1_IRQn, 0},  // 1
66
-  { TC0, 2, TC2_IRQn, 0},  // 2
67
-  { TC1, 0, TC3_IRQn, 2},  // 3 - stepper
64
+  { TC0, 0, TC0_IRQn,  0}, // 0 - [servo timer5]
65
+  { TC0, 1, TC1_IRQn,  0}, // 1
66
+  { TC0, 2, TC2_IRQn,  0}, // 2
67
+  { TC1, 0, TC3_IRQn,  2}, // 3 - stepper
68
   { TC1, 1, TC4_IRQn, 15}, // 4 - temperature
68
   { TC1, 1, TC4_IRQn, 15}, // 4 - temperature
69
-  { TC1, 2, TC5_IRQn, 0},  // 5 - [servo timer3]
70
-  { TC2, 0, TC6_IRQn, 0},  // 6 - tone
71
-  { TC2, 1, TC7_IRQn, 0},  // 7
72
-  { TC2, 2, TC8_IRQn, 0},  // 8
69
+  { TC1, 2, TC5_IRQn,  0}, // 5 - [servo timer3]
70
+  { TC2, 0, TC6_IRQn, 15}, // 6 - tone
71
+  { TC2, 1, TC7_IRQn,  0}, // 7
72
+  { TC2, 2, TC8_IRQn,  0}, // 8
73
 };
73
 };
74
 
74
 
75
 // --------------------------------------------------------------------------
75
 // --------------------------------------------------------------------------
100
   pmc_enable_periph_clk((uint32_t)irq);
100
   pmc_enable_periph_clk((uint32_t)irq);
101
   NVIC_SetPriority(irq, TimerConfig [timer_num].priority);
101
   NVIC_SetPriority(irq, TimerConfig [timer_num].priority);
102
 
102
 
103
+  // wave mode, reset counter on match with RC,
103
   TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK1);
104
   TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK1);
104
 
105
 
105
   TC_SetRC(tc, channel, VARIANT_MCK / 2 / frequency);
106
   TC_SetRC(tc, channel, VARIANT_MCK / 2 / frequency);

+ 12
- 0
Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h Dosyayı Görüntüle

109
   return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_CV;
109
   return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_CV;
110
 }
110
 }
111
 
111
 
112
+FORCE_INLINE static void HAL_timer_set_count(const uint8_t timer_num, const hal_timer_t counter) {
113
+  const tTimerConfig * const pConfig = &TimerConfig[timer_num];
114
+  pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_CV = counter;
115
+}
116
+
117
+// if counter too high then bump up compare
112
 FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks) {
118
 FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks) {
113
   const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks;
119
   const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks;
114
   if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
120
   if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
115
 }
121
 }
116
 
122
 
123
+// if counter too high then clear it
124
+FORCE_INLINE static void HAL_timer_restrain_count(const uint8_t timer_num, const uint16_t interval_ticks) {
125
+  const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks;
126
+  if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_count(timer_num, 0);
127
+}
128
+
117
 void HAL_timer_enable_interrupt(const uint8_t timer_num);
129
 void HAL_timer_enable_interrupt(const uint8_t timer_num);
118
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
130
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
119
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
131
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);

+ 7
- 3
Marlin/src/HAL/HAL_DUE/Tone.cpp Dosyayı Görüntüle

34
 volatile static int32_t toggles;
34
 volatile static int32_t toggles;
35
 
35
 
36
 void toneInit() {
36
 void toneInit() {
37
-  HAL_timer_start(TONE_TIMER_NUM, 1); // Lowest frequency possible
37
+  HAL_timer_start(TONE_TIMER_NUM, 100000);
38
+  HAL_timer_disable_interrupt(TONE_TIMER_NUM);
38
 }
39
 }
39
 
40
 
40
 void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration) {
41
 void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration) {
41
   tone_pin = _pin;
42
   tone_pin = _pin;
42
   toggles = 2 * frequency * duration / 1000;
43
   toggles = 2 * frequency * duration / 1000;
43
-  HAL_timer_set_compare(TONE_TIMER_NUM, VARIANT_MCK / 2 / frequency); // 84MHz / 2 prescaler / Hz
44
+  HAL_timer_set_count(TONE_TIMER_NUM, 0);  // ensure first beep is correct (make sure counter is less than the compare value)
45
+  HAL_timer_set_compare(TONE_TIMER_NUM, VARIANT_MCK / 2 / 2 / frequency); // 84MHz / 2 prescaler / 2 interrupts per cycle /Hz
44
   HAL_timer_enable_interrupt(TONE_TIMER_NUM);
46
   HAL_timer_enable_interrupt(TONE_TIMER_NUM);
45
 }
47
 }
46
 
48
 
52
 HAL_TONE_TIMER_ISR {
54
 HAL_TONE_TIMER_ISR {
53
   static uint8_t pin_state = 0;
55
   static uint8_t pin_state = 0;
54
   HAL_timer_isr_prologue(TONE_TIMER_NUM);
56
   HAL_timer_isr_prologue(TONE_TIMER_NUM);
57
+
55
   if (toggles) {
58
   if (toggles) {
56
     toggles--;
59
     toggles--;
57
     digitalWrite(tone_pin, (pin_state ^= 1));
60
     digitalWrite(tone_pin, (pin_state ^= 1));
58
   }
61
   }
59
-  else noTone(tone_pin);                                  // seems superfluous ?
62
+  else noTone(tone_pin);                         // turn off interrupt
63
+  HAL_timer_restrain_count(TONE_TIMER_NUM, 10);  // make sure next ISR isn't delayed by up to 2 minutes
60
 }
64
 }
61
 
65
 
62
 #endif // ARDUINO_ARCH_SAM
66
 #endif // ARDUINO_ARCH_SAM

+ 1
- 1
Marlin/src/Marlin.cpp Dosyayı Görüntüle

648
 
648
 
649
   #ifdef HAL_INIT
649
   #ifdef HAL_INIT
650
     HAL_init();
650
     HAL_init();
651
-    #if defined(ARDUINO_ARCH_SAM) && PIN_EXISTS(BEEPER)
651
+    #if defined(ARDUINO_ARCH_SAM) && PIN_EXISTS(BEEPER) && ENABLED(SPEAKER)
652
       toneInit();
652
       toneInit();
653
     #endif
653
     #endif
654
   #endif
654
   #endif

Loading…
İptal
Kaydet