From c134c59c951bbfb628df561110750b21a4c5c08a Mon Sep 17 00:00:00 2001 From: Comma Device Date: Mon, 18 Nov 2024 22:44:30 +0000 Subject: [PATCH] fix noise --- board/stm32h7/sound.h | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/board/stm32h7/sound.h b/board/stm32h7/sound.h index f40a22e5b2..6251936c62 100644 --- a/board/stm32h7/sound.h +++ b/board/stm32h7/sound.h @@ -5,23 +5,41 @@ __attribute__((section(".sram4"))) static uint16_t sound_rx_buf[2][SOUND_RX_BUF_ static uint8_t sound_idle_count; +void sound_tick(void) { + if (sound_idle_count > 0U) { + sound_idle_count--; + if (sound_idle_count == 0U) { + current_board->set_amp_enabled(false); + } + } +} + // Playback processing static void BDMA_Channel0_IRQ_Handler(void) { __attribute__((section(".sram4"))) static uint16_t tx_buf[SOUND_TX_BUF_SIZE]; BDMA->IFCR |= BDMA_IFCR_CGIF0; // clear flag - // process samples (shift to 12b and bias to be unsigned) - // since we are playing mono and receiving stereo, we take every other sample uint8_t buf_idx = (((BDMA_Channel0->CCR & BDMA_CCR_CT) >> BDMA_CCR_CT_Pos) == 1U) ? 0U : 1U; + + // process samples (shift to 12b and bias to be unsigned) + bool sound_playing = false; for (uint16_t i=0U; i < SOUND_RX_BUF_SIZE; i += 2U) { + // since we are playing mono and receiving stereo, we take every other sample tx_buf[i/2U] = ((sound_rx_buf[buf_idx][i] + (1UL << 14)) >> 3); + if (sound_rx_buf[buf_idx][i] > 0U) { + sound_playing = true; + } } - if (sound_idle_count == 0U) { - current_board->set_amp_enabled(true); + // manage amp state + if (sound_playing) { + if (sound_idle_count == 0U) { + current_board->set_amp_enabled(true); + } + sound_idle_count = 4U; } - sound_idle_count = 2U; + sound_tick(); DMA1->LIFCR |= 0xF40; DMA1_Stream1->CR &= ~DMA_SxCR_EN; @@ -30,15 +48,6 @@ static void BDMA_Channel0_IRQ_Handler(void) { DMA1_Stream1->CR |= DMA_SxCR_EN; } -void sound_tick(void) { - if (sound_idle_count > 0U) { - sound_idle_count--; - if (sound_idle_count == 0U) { - current_board->set_amp_enabled(false); - } - } -} - void sound_init(void) { REGISTER_INTERRUPT(BDMA_Channel0_IRQn, BDMA_Channel0_IRQ_Handler, 64U, FAULT_INTERRUPT_RATE_SOUND_DMA)