Browse Source

Tone fixes/changes (#10151)

Bob-the-Kuhn 6 years ago
parent
commit
53362b81cc

+ 9
- 8
Marlin/src/HAL/HAL_DUE/HAL_timers_Due.cpp View File

@@ -61,15 +61,15 @@
61 61
 // --------------------------------------------------------------------------
62 62
 
63 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 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,6 +100,7 @@ void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
100 100
   pmc_enable_periph_clk((uint32_t)irq);
101 101
   NVIC_SetPriority(irq, TimerConfig [timer_num].priority);
102 102
 
103
+  // wave mode, reset counter on match with RC,
103 104
   TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK1);
104 105
 
105 106
   TC_SetRC(tc, channel, VARIANT_MCK / 2 / frequency);

+ 12
- 0
Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h View File

@@ -109,11 +109,23 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
109 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 118
 FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks) {
113 119
   const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks;
114 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 129
 void HAL_timer_enable_interrupt(const uint8_t timer_num);
118 130
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
119 131
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);

+ 7
- 3
Marlin/src/HAL/HAL_DUE/Tone.cpp View File

@@ -34,13 +34,15 @@ static pin_t tone_pin;
34 34
 volatile static int32_t toggles;
35 35
 
36 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 41
 void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration) {
41 42
   tone_pin = _pin;
42 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 46
   HAL_timer_enable_interrupt(TONE_TIMER_NUM);
45 47
 }
46 48
 
@@ -52,11 +54,13 @@ void noTone(const pin_t _pin) {
52 54
 HAL_TONE_TIMER_ISR {
53 55
   static uint8_t pin_state = 0;
54 56
   HAL_timer_isr_prologue(TONE_TIMER_NUM);
57
+
55 58
   if (toggles) {
56 59
     toggles--;
57 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 66
 #endif // ARDUINO_ARCH_SAM

+ 1
- 1
Marlin/src/Marlin.cpp View File

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

Loading…
Cancel
Save