From 7f8188ccb6eef61c5c4cd291a051c8db7747cb3f Mon Sep 17 00:00:00 2001 From: EvilGremlin Date: Thu, 14 Jan 2021 11:33:50 +0300 Subject: [PATCH] ESP32 Tone Generator (#20704) --- Marlin/src/HAL/ESP32/HAL.h | 7 ++++ Marlin/src/HAL/ESP32/Tone.cpp | 59 +++++++++++++++++++++++++++++++++ Marlin/src/HAL/ESP32/timers.cpp | 2 +- Marlin/src/HAL/ESP32/timers.h | 9 +++++ platformio.ini | 5 +-- 5 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 Marlin/src/HAL/ESP32/Tone.cpp diff --git a/Marlin/src/HAL/ESP32/HAL.h b/Marlin/src/HAL/ESP32/HAL.h index 5ef13e0c21..be87737b0f 100644 --- a/Marlin/src/HAL/ESP32/HAL.h +++ b/Marlin/src/HAL/ESP32/HAL.h @@ -90,6 +90,13 @@ extern uint16_t HAL_adc_result; // Public functions // ------------------------ +// +// Tone +// +void toneInit(); +void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0); +void noTone(const pin_t _pin); + // clear reset reason void HAL_clear_reset_source(); diff --git a/Marlin/src/HAL/ESP32/Tone.cpp b/Marlin/src/HAL/ESP32/Tone.cpp new file mode 100644 index 0000000000..376c0f32e1 --- /dev/null +++ b/Marlin/src/HAL/ESP32/Tone.cpp @@ -0,0 +1,59 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * Copypaste of SAMD51 HAL developed by Giuliano Zaro (AKA GMagician) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * Description: Tone function for ESP32 + * Derived from https://forum.arduino.cc/index.php?topic=136500.msg2903012#msg2903012 + */ + +#ifdef ARDUINO_ARCH_ESP32 + +#include "../../inc/MarlinConfig.h" +#include "HAL.h" + +static pin_t tone_pin; +volatile static int32_t toggles; + +void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration) { + tone_pin = _pin; + toggles = 2 * frequency * duration / 1000; + HAL_timer_start(TONE_TIMER_NUM, 2 * frequency); +} + +void noTone(const pin_t _pin) { + HAL_timer_disable_interrupt(TONE_TIMER_NUM); + WRITE(_pin, LOW); +} + +HAL_TONE_TIMER_ISR() { + HAL_timer_isr_prologue(TONE_TIMER_NUM); + + if (toggles) { + toggles--; + TOGGLE(tone_pin); + } + else noTone(tone_pin); // turn off interrupt +} + +#endif // ARDUINO_ARCH_ESP32 diff --git a/Marlin/src/HAL/ESP32/timers.cpp b/Marlin/src/HAL/ESP32/timers.cpp index 3300aea8a8..57662a6658 100644 --- a/Marlin/src/HAL/ESP32/timers.cpp +++ b/Marlin/src/HAL/ESP32/timers.cpp @@ -45,7 +45,7 @@ const tTimerConfig TimerConfig [NUM_HARDWARE_TIMERS] = { { TIMER_GROUP_0, TIMER_0, STEPPER_TIMER_PRESCALE, stepTC_Handler }, // 0 - Stepper { TIMER_GROUP_0, TIMER_1, TEMP_TIMER_PRESCALE, tempTC_Handler }, // 1 - Temperature { TIMER_GROUP_1, TIMER_0, PWM_TIMER_PRESCALE, pwmTC_Handler }, // 2 - PWM - { TIMER_GROUP_1, TIMER_1, 1, nullptr }, // 3 + { TIMER_GROUP_1, TIMER_1, TONE_TIMER_PRESCALE, toneTC_Handler }, // 3 - Tone }; // ------------------------ diff --git a/Marlin/src/HAL/ESP32/timers.h b/Marlin/src/HAL/ESP32/timers.h index 98386e3980..a47697113d 100644 --- a/Marlin/src/HAL/ESP32/timers.h +++ b/Marlin/src/HAL/ESP32/timers.h @@ -44,6 +44,9 @@ typedef uint64_t hal_timer_t; #ifndef PWM_TIMER_NUM #define PWM_TIMER_NUM 2 // index of timer to use for PWM outputs #endif +#ifndef TONE_TIMER_NUM + #define TONE_TIMER_NUM 3 // index of timer for beeper tones +#endif #define HAL_TIMER_RATE APB_CLK_FREQ // frequency of timer peripherals @@ -59,6 +62,8 @@ typedef uint64_t hal_timer_t; #define STEP_TIMER_MIN_INTERVAL 8 // minimum time in µs between stepper interrupts +#define TONE_TIMER_PRESCALE 1000 // Arbitrary value, no idea what i'm doing here + #define TEMP_TIMER_PRESCALE 1000 // prescaler for setting Temp timer, 72Khz #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency @@ -90,11 +95,15 @@ typedef uint64_t hal_timer_t; #ifndef HAL_PWM_TIMER_ISR #define HAL_PWM_TIMER_ISR() extern "C" void pwmTC_Handler() #endif +#ifndef HAL_TONE_TIMER_ISR + #define HAL_TONE_TIMER_ISR() extern "C" void toneTC_Handler() +#endif extern "C" { void tempTC_Handler(); void stepTC_Handler(); void pwmTC_Handler(); + void toneTC_Handler(); } // ------------------------ diff --git a/platformio.ini b/platformio.ini index 81ed916eeb..24209ba1cf 100644 --- a/platformio.ini +++ b/platformio.ini @@ -1483,12 +1483,13 @@ build_flags = ${stm32_flash_drive.build_flags} # Espressif ESP32 # [env:esp32] -platform = espressif32@1.11.2 +platform = espressif32@2.1.0 board = esp32dev build_flags = ${common.build_flags} -DCORE_DEBUG_LEVEL=0 src_filter = ${common.default_src_filter} + lib_ignore = NativeEthernet -upload_speed = 115200 +upload_speed = 500000 +monitor_speed = 250000 #upload_port = marlinesp.local #board_build.flash_mode = qio