Sfoglia il codice sorgente

ESP32 Tone Generator (#20704)

EvilGremlin 4 anni fa
parent
commit
8049db20ff
Nessun account collegato all'indirizzo email del committer

+ 7
- 0
Marlin/src/HAL/ESP32/HAL.h Vedi File

@@ -90,6 +90,13 @@ extern uint16_t HAL_adc_result;
90 90
 // Public functions
91 91
 // ------------------------
92 92
 
93
+//
94
+// Tone
95
+//
96
+void toneInit();
97
+void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0);
98
+void noTone(const pin_t _pin);
99
+
93 100
 // clear reset reason
94 101
 void HAL_clear_reset_source();
95 102
 

+ 59
- 0
Marlin/src/HAL/ESP32/Tone.cpp Vedi File

@@ -0,0 +1,59 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2020 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
+ * Copypaste of SAMD51 HAL developed by Giuliano Zaro (AKA GMagician)
9
+ *
10
+ * This program is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation, either version 3 of the License, or
13
+ * (at your option) any later version.
14
+ *
15
+ * This program is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
22
+ *
23
+ */
24
+
25
+/**
26
+ * Description: Tone function for ESP32
27
+ * Derived from https://forum.arduino.cc/index.php?topic=136500.msg2903012#msg2903012
28
+ */
29
+
30
+#ifdef ARDUINO_ARCH_ESP32
31
+
32
+#include "../../inc/MarlinConfig.h"
33
+#include "HAL.h"
34
+
35
+static pin_t tone_pin;
36
+volatile static int32_t toggles;
37
+
38
+void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration) {
39
+  tone_pin = _pin;
40
+  toggles = 2 * frequency * duration / 1000;
41
+  HAL_timer_start(TONE_TIMER_NUM, 2 * frequency);
42
+}
43
+
44
+void noTone(const pin_t _pin) {
45
+  HAL_timer_disable_interrupt(TONE_TIMER_NUM);
46
+  WRITE(_pin, LOW);
47
+}
48
+
49
+HAL_TONE_TIMER_ISR() {
50
+  HAL_timer_isr_prologue(TONE_TIMER_NUM);
51
+
52
+  if (toggles) {
53
+    toggles--;
54
+    TOGGLE(tone_pin);
55
+  }
56
+  else noTone(tone_pin);                         // turn off interrupt
57
+}
58
+
59
+#endif // ARDUINO_ARCH_ESP32

+ 1
- 1
Marlin/src/HAL/ESP32/timers.cpp Vedi File

@@ -45,7 +45,7 @@ const tTimerConfig TimerConfig [NUM_HARDWARE_TIMERS] = {
45 45
   { TIMER_GROUP_0, TIMER_0, STEPPER_TIMER_PRESCALE, stepTC_Handler }, // 0 - Stepper
46 46
   { TIMER_GROUP_0, TIMER_1,    TEMP_TIMER_PRESCALE, tempTC_Handler }, // 1 - Temperature
47 47
   { TIMER_GROUP_1, TIMER_0,     PWM_TIMER_PRESCALE, pwmTC_Handler  }, // 2 - PWM
48
-  { TIMER_GROUP_1, TIMER_1,                      1, nullptr }, // 3
48
+  { TIMER_GROUP_1, TIMER_1,    TONE_TIMER_PRESCALE, toneTC_Handler }, // 3 - Tone
49 49
 };
50 50
 
51 51
 // ------------------------

+ 9
- 0
Marlin/src/HAL/ESP32/timers.h Vedi File

@@ -44,6 +44,9 @@ typedef uint64_t hal_timer_t;
44 44
 #ifndef PWM_TIMER_NUM
45 45
   #define PWM_TIMER_NUM         2  // index of timer to use for PWM outputs
46 46
 #endif
47
+#ifndef TONE_TIMER_NUM
48
+  #define TONE_TIMER_NUM        3  // index of timer for beeper tones
49
+#endif
47 50
 
48 51
 #define HAL_TIMER_RATE APB_CLK_FREQ // frequency of timer peripherals
49 52
 
@@ -59,6 +62,8 @@ typedef uint64_t hal_timer_t;
59 62
 
60 63
 #define STEP_TIMER_MIN_INTERVAL   8 // minimum time in µs between stepper interrupts
61 64
 
65
+#define TONE_TIMER_PRESCALE    1000 // Arbitrary value, no idea what i'm doing here
66
+
62 67
 #define TEMP_TIMER_PRESCALE    1000 // prescaler for setting Temp timer, 72Khz
63 68
 #define TEMP_TIMER_FREQUENCY   1000 // temperature interrupt frequency
64 69
 
@@ -90,11 +95,15 @@ typedef uint64_t hal_timer_t;
90 95
 #ifndef HAL_PWM_TIMER_ISR
91 96
   #define HAL_PWM_TIMER_ISR() extern "C" void pwmTC_Handler()
92 97
 #endif
98
+#ifndef HAL_TONE_TIMER_ISR
99
+  #define HAL_TONE_TIMER_ISR() extern "C" void toneTC_Handler()
100
+#endif
93 101
 
94 102
 extern "C" {
95 103
   void tempTC_Handler();
96 104
   void stepTC_Handler();
97 105
   void pwmTC_Handler();
106
+  void toneTC_Handler();
98 107
 }
99 108
 
100 109
 // ------------------------

+ 3
- 2
platformio.ini Vedi File

@@ -1483,12 +1483,13 @@ build_flags       = ${stm32_flash_drive.build_flags}
1483 1483
 # Espressif ESP32
1484 1484
 #
1485 1485
 [env:esp32]
1486
-platform      = espressif32@1.11.2
1486
+platform      = espressif32@2.1.0
1487 1487
 board         = esp32dev
1488 1488
 build_flags   = ${common.build_flags} -DCORE_DEBUG_LEVEL=0
1489 1489
 src_filter    = ${common.default_src_filter} +<src/HAL/ESP32>
1490 1490
 lib_ignore    = NativeEthernet
1491
-upload_speed  = 115200
1491
+upload_speed  = 500000
1492
+monitor_speed = 250000
1492 1493
 #upload_port   = marlinesp.local
1493 1494
 #board_build.flash_mode = qio
1494 1495
 

Loading…
Annulla
Salva