diff --git a/radio/src/audio.h b/radio/src/audio.h index ac356863a..0f4821a82 100644 --- a/radio/src/audio.h +++ b/radio/src/audio.h @@ -512,6 +512,9 @@ class AudioQueue { bool isEmpty() const { return fragmentsFifo.empty(); }; void wakeup(); bool started() const { return _started; }; +#if defined(AUDIO_UNMUTE_DELAY) + tmr10ms_t lastAudioPlayTime = 0; +#endif AudioBufferFifo buffersFifo; diff --git a/radio/src/targets/common/arm/stm32/audio_dac_driver.cpp b/radio/src/targets/common/arm/stm32/audio_dac_driver.cpp index d050382a2..a99020dda 100644 --- a/radio/src/targets/common/arm/stm32/audio_dac_driver.cpp +++ b/radio/src/targets/common/arm/stm32/audio_dac_driver.cpp @@ -88,33 +88,61 @@ void dacInit() NVIC_SetPriority(AUDIO_DMA_Stream_IRQn, 7); } +#if defined(AUDIO_MUTE_GPIO_PIN) +void audioMute() +{ +#if defined(AUDIO_UNMUTE_DELAY) + tmr10ms_t now = get_tmr10ms(); + if (!audioQueue.lastAudioPlayTime) { + // we start the mute delay now + audioQueue.lastAudioPlayTime = now; + } + else if (now - audioQueue.lastAudioPlayTime > AUDIO_MUTE_DELAY / 10) { + // delay expired, we may mute + GPIO_SetBits(AUDIO_MUTE_GPIO, AUDIO_MUTE_GPIO_PIN); + } +#else + // mute + GPIO_SetBits(AUDIO_MUTE_GPIO, AUDIO_MUTE_GPIO_PIN); +#endif +} + +void audioUnmute() +{ +#if defined(AUDIO_UNMUTE_DELAY) + // if muted + if (GPIO_ReadOutputDataBit(AUDIO_MUTE_GPIO, AUDIO_MUTE_GPIO_PIN)) { + // ..un-mute + GPIO_ResetBits(AUDIO_MUTE_GPIO, AUDIO_MUTE_GPIO_PIN); + RTOS_WAIT_MS(AUDIO_UNMUTE_DELAY); + } + // reset the mute delay + audioQueue.lastAudioPlayTime = 0; +#else + GPIO_ResetBits(AUDIO_MUTE_GPIO, AUDIO_MUTE_GPIO_PIN); +#endif +} +#endif + void audioConsumeCurrentBuffer() { if (!nextBuffer) { nextBuffer = audioQueue.buffersFifo.getNextFilledBuffer(); if (nextBuffer) { #if defined(AUDIO_MUTE_GPIO_PIN) - // if muted - if (GPIO_ReadOutputDataBit(AUDIO_MUTE_GPIO, AUDIO_MUTE_GPIO_PIN)) { - // ..un-mute - GPIO_ResetBits(AUDIO_MUTE_GPIO, AUDIO_MUTE_GPIO_PIN); -#if defined(AUDIO_UNMUTE_DELAY) - RTOS_WAIT_MS(AUDIO_UNMUTE_DELAY); + audioUnmute(); #endif - } -#endif - AUDIO_DMA_Stream->CR &= ~DMA_SxCR_EN ; // Disable DMA channel - AUDIO_DMA->HIFCR = DMA_HIFCR_CTCIF5 | DMA_HIFCR_CHTIF5 | DMA_HIFCR_CTEIF5 | DMA_HIFCR_CDMEIF5 | DMA_HIFCR_CFEIF5 ; // Write ones to clear bits + AUDIO_DMA_Stream->CR &= ~DMA_SxCR_EN; // Disable DMA channel + AUDIO_DMA->HIFCR = DMA_HIFCR_CTCIF5 | DMA_HIFCR_CHTIF5 | DMA_HIFCR_CTEIF5 | DMA_HIFCR_CDMEIF5 | DMA_HIFCR_CFEIF5; // Write ones to clear bits AUDIO_DMA_Stream->M0AR = CONVERT_PTR_UINT(nextBuffer->data); AUDIO_DMA_Stream->NDTR = nextBuffer->size; - AUDIO_DMA_Stream->CR |= DMA_SxCR_EN | DMA_SxCR_TCIE ; // Enable DMA channel and interrupt - DAC->SR = DAC_SR_DMAUDR1 ; // Write 1 to clear flag - DAC->CR |= DAC_CR_EN1 | DAC_CR_DMAEN1 ; // Enable DAC + AUDIO_DMA_Stream->CR |= DMA_SxCR_EN | DMA_SxCR_TCIE; // Enable DMA channel and interrupt + DAC->SR = DAC_SR_DMAUDR1; // Write 1 to clear flag + DAC->CR |= DAC_CR_EN1 | DAC_CR_DMAEN1; // Enable DAC } #if defined(AUDIO_MUTE_GPIO_PIN) else { - // mute - GPIO_SetBits(AUDIO_MUTE_GPIO, AUDIO_MUTE_GPIO_PIN); + audioMute(); } #endif } diff --git a/radio/src/targets/horus/hal.h b/radio/src/targets/horus/hal.h index e7afa6ddb..38f017bcb 100644 --- a/radio/src/targets/horus/hal.h +++ b/radio/src/targets/horus/hal.h @@ -547,7 +547,8 @@ #endif #if defined(RADIO_T16) - #define AUDIO_UNMUTE_DELAY 150 + #define AUDIO_UNMUTE_DELAY 120 // ms + #define AUDIO_MUTE_DELAY 500 // ms #endif // I2C Bus