ESP32 Tone Generator (#20704)

This commit is contained in:
EvilGremlin 2021-01-14 11:33:50 +03:00 committed by GitHub
parent ecf5f5d21d
commit 8049db20ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 3 deletions

View file

@ -90,6 +90,13 @@ extern uint16_t HAL_adc_result;
// Public functions // 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 // clear reset reason
void HAL_clear_reset_source(); void HAL_clear_reset_source();

View file

@ -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 <https://www.gnu.org/licenses/>.
*
*/
/**
* 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

View file

@ -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_0, STEPPER_TIMER_PRESCALE, stepTC_Handler }, // 0 - Stepper
{ TIMER_GROUP_0, TIMER_1, TEMP_TIMER_PRESCALE, tempTC_Handler }, // 1 - Temperature { 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_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
}; };
// ------------------------ // ------------------------

View file

@ -44,6 +44,9 @@ typedef uint64_t hal_timer_t;
#ifndef PWM_TIMER_NUM #ifndef PWM_TIMER_NUM
#define PWM_TIMER_NUM 2 // index of timer to use for PWM outputs #define PWM_TIMER_NUM 2 // index of timer to use for PWM outputs
#endif #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 #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 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_PRESCALE 1000 // prescaler for setting Temp timer, 72Khz
#define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
@ -90,11 +95,15 @@ typedef uint64_t hal_timer_t;
#ifndef HAL_PWM_TIMER_ISR #ifndef HAL_PWM_TIMER_ISR
#define HAL_PWM_TIMER_ISR() extern "C" void pwmTC_Handler() #define HAL_PWM_TIMER_ISR() extern "C" void pwmTC_Handler()
#endif #endif
#ifndef HAL_TONE_TIMER_ISR
#define HAL_TONE_TIMER_ISR() extern "C" void toneTC_Handler()
#endif
extern "C" { extern "C" {
void tempTC_Handler(); void tempTC_Handler();
void stepTC_Handler(); void stepTC_Handler();
void pwmTC_Handler(); void pwmTC_Handler();
void toneTC_Handler();
} }
// ------------------------ // ------------------------

View file

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