Browse Source

Add tone support for Due (#9807)

Scott Lahteine 6 years ago
parent
commit
f2c0ed193d
No account linked to committer's email address

+ 7
- 1
Marlin/src/HAL/HAL_DUE/HAL_Due.h View File

@@ -157,11 +157,17 @@ void HAL_enable_AdcFreerun(void);
157 157
 /**
158 158
  * Pin Map
159 159
  */
160
-
161 160
 #define GET_PIN_MAP_PIN(index) index
162 161
 #define GET_PIN_MAP_INDEX(pin) pin
163 162
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
164 163
 
164
+/**
165
+ * Tone
166
+ */
167
+void toneInit();
168
+void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0);
169
+void noTone(const pin_t _pin);
170
+
165 171
 // Enable hooks into idle and setup for USB stack
166 172
 #define HAL_IDLETASK 1
167 173
 #define HAL_INIT 1

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

@@ -67,7 +67,7 @@ const tTimerConfig TimerConfig [NUM_HARDWARE_TIMERS] = {
67 67
   { TC1, 0, TC3_IRQn, 2},  // 3 - stepper
68 68
   { TC1, 1, TC4_IRQn, 15}, // 4 - temperature
69 69
   { TC1, 2, TC5_IRQn, 0},  // 5 - [servo timer3]
70
-  { TC2, 0, TC6_IRQn, 0},  // 6
70
+  { TC2, 0, TC6_IRQn, 0},  // 6 - tone
71 71
   { TC2, 1, TC7_IRQn, 0},  // 7
72 72
   { TC2, 2, TC8_IRQn, 0},  // 8
73 73
 };

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

@@ -45,6 +45,7 @@ typedef uint32_t hal_timer_t;
45 45
 
46 46
 #define STEP_TIMER_NUM 3  // index of timer to use for stepper
47 47
 #define TEMP_TIMER_NUM 4  // index of timer to use for temperature
48
+#define TONE_TIMER_NUM 6  // index of timer to use for beeper tones
48 49
 
49 50
 #define HAL_TIMER_RATE         ((F_CPU) / 2)    // frequency of timers peripherals
50 51
 #define STEPPER_TIMER_PRESCALE 1.0              // prescaler for setting stepper frequency
@@ -64,6 +65,7 @@ typedef uint32_t hal_timer_t;
64 65
 
65 66
 #define HAL_STEP_TIMER_ISR  void TC3_Handler()
66 67
 #define HAL_TEMP_TIMER_ISR  void TC4_Handler()
68
+#define HAL_TONE_TIMER_ISR  void TC6_Handler()
67 69
 
68 70
 #define PULSE_TIMER_NUM STEP_TIMER_NUM
69 71
 #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE

+ 62
- 0
Marlin/src/HAL/HAL_DUE/Tone.cpp View File

@@ -0,0 +1,62 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+/**
24
+ * Description: Tone function for Arduino Due and compatible (SAM3X8E)
25
+ * Derived from http://forum.arduino.cc/index.php?topic=136500.msg2903012#msg2903012
26
+ */
27
+
28
+#ifdef ARDUINO_ARCH_SAM
29
+
30
+#include "HAL_Due.h"
31
+#include "HAL_timers_Due.h"
32
+
33
+static pin_t tone_pin;
34
+volatile static int32_t toggles;
35
+
36
+void toneInit() {
37
+  HAL_timer_start(TONE_TIMER_NUM, 1); // Lowest frequency possible
38
+}
39
+
40
+void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration) {
41
+  tone_pin = _pin;
42
+  toggles = 2 * frequency * duration / 1000;
43
+  HAL_timer_set_compare(TONE_TIMER_NUM, VARIANT_MCK / 2 / frequency); // 84MHz / 2 prescaler / Hz
44
+  HAL_timer_enable_interrupt(TONE_TIMER_NUM);
45
+}
46
+
47
+void noTone(const pin_t _pin) {
48
+  HAL_timer_disable_interrupt(TONE_TIMER_NUM);
49
+  digitalWrite(_pin, LOW);
50
+}
51
+
52
+HAL_TONE_TIMER_ISR {
53
+  static uint8_t pin_state = 0;
54
+  HAL_timer_isr_prologue(TONE_TIMER_NUM);
55
+  if (toggles) {
56
+    toggles--;
57
+    digitalWrite(tone_pin, (pin_state ^= 1));
58
+  }
59
+  else noTone(tone_pin);                                  // seems superfluous ?
60
+}
61
+
62
+#endif // ARDUINO_ARCH_SAM

+ 3
- 1
Marlin/src/HAL/HAL_DUE/usb/usb_task.c View File

@@ -254,8 +254,10 @@ bool usb_task_other_requests(void) {
254 254
   return true;
255 255
 }
256 256
 
257
-
258 257
 void HAL_init(void) {
258
+
259
+  toneInit();
260
+
259 261
   uint16_t *ptr;
260 262
 
261 263
   udd_disable();

Loading…
Cancel
Save