From ebaeab1917b3a41d5b6cc64810603e354c9a2449 Mon Sep 17 00:00:00 2001 From: tomcombriat Date: Mon, 6 Jan 2025 22:29:57 +0100 Subject: [PATCH] Fix EXTERNAL_TIMED for ESP32 Removed outdated comments --- internal/MozziGuts_impl_ESP32.hpp | 55 ++++++++++++++++++------------- internal/config_checks_esp32.h | 2 +- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/internal/MozziGuts_impl_ESP32.hpp b/internal/MozziGuts_impl_ESP32.hpp index 4a4d35378..b0f41525f 100644 --- a/internal/MozziGuts_impl_ESP32.hpp +++ b/internal/MozziGuts_impl_ESP32.hpp @@ -95,35 +95,46 @@ inline void audioOutput(const AudioOutput f) { #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED) } // namespace MozziPrivate -# include + +#include namespace MozziPrivate { -void CACHED_FUNCTION_ATTR timer0_audio_output_isr(void *) { - TIMERG0.int_clr_timers.t0 = 1; - TIMERG0.hw_timer[0].config.alarm_en = 1; - defaultAudioOutput(); -} + + bool CACHED_FUNCTION_ATTR timer_on_alarm_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx) + { + defaultAudioOutput(); + return true; + } + #endif static void startAudio() { #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED) // for external audio output, set up a timer running a audio rate - static intr_handle_t s_timer_handle; - const int div = 2; - timer_config_t config = { - .alarm_en = (timer_alarm_t)true, - .counter_en = (timer_start_t)false, - .intr_type = (timer_intr_mode_t) TIMER_INTR_LEVEL, - .counter_dir = TIMER_COUNT_UP, - .auto_reload = (timer_autoreload_t) true, - .divider = div // For max available precision: The APB_CLK clock signal is running at 80 MHz, i.e. 2/80 uS per tick - // Min acceptable value is 2 + + static gptimer_handle_t gptimer = NULL; + gptimer_config_t timer_config = { + .clk_src = GPTIMER_CLK_SRC_DEFAULT, + .direction = GPTIMER_COUNT_UP, + .resolution_hz = 40 * 1000 * 1000, // 40MHz + }; - timer_init(TIMER_GROUP_0, TIMER_0, &config); - timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0); - timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, 80000000UL / MOZZI_AUDIO_RATE / div); - timer_enable_intr(TIMER_GROUP_0, TIMER_0); - timer_isr_register(TIMER_GROUP_0, TIMER_0, &timer0_audio_output_isr, nullptr, 0, &s_timer_handle); - timer_start(TIMER_GROUP_0, TIMER_0); + gptimer_new_timer(&timer_config, &gptimer); + + gptimer_alarm_config_t alarm_config; // note: inline config for the flag does not work unless we have access to c++20, hence the manual attributes setting. + alarm_config.reload_count = 0; + alarm_config.alarm_count = (40000000UL / MOZZI_AUDIO_RATE); + alarm_config.flags.auto_reload_on_alarm = true; + + gptimer_set_alarm_action(gptimer, &alarm_config); + + gptimer_event_callbacks_t cbs = { + .on_alarm = timer_on_alarm_cb, // register user callback + }; + + gptimer_register_event_callbacks(gptimer,&cbs,NULL); + gptimer_enable(gptimer); + gptimer_start(gptimer); + #elif !MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_CUSTOM) static const i2s_config_t i2s_config = { diff --git a/internal/config_checks_esp32.h b/internal/config_checks_esp32.h index 5912cff11..a03c3da19 100644 --- a/internal/config_checks_esp32.h +++ b/internal/config_checks_esp32.h @@ -115,7 +115,7 @@ MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_NONE) #endif #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_INTERNAL_DAC, MOZZI_OUTPUT_I2S_DAC, MOZZI_OUTPUT_PDM_VIA_I2S) -# include +//# include # if !defined(MOZZI_IS2_PORT) # define MOZZI_I2S_PORT I2S_NUM_0 # endif