diff --git a/board/stm32h7/sound.h b/board/stm32h7/sound.h index aeee2c71..378534a1 100644 --- a/board/stm32h7/sound.h +++ b/board/stm32h7/sound.h @@ -7,8 +7,10 @@ __attribute__((section(".sram4"))) static uint16_t sound_tx_buf[2][SOUND_TX_BUF_ __attribute__((section(".sram4"))) static uint32_t mic_rx_buf[2][MIC_RX_BUF_SIZE]; #define SOUND_IDLE_TIMEOUT 4U +#define MIC_SKIP_BUFFERS 2U // Skip first 2 buffers (1024 samples = ~21ms at 48kHz) static uint8_t sound_idle_count; static uint8_t mic_idle_count; +static uint8_t mic_buffer_count; void sound_tick(void) { if (sound_idle_count > 0U) { @@ -23,6 +25,7 @@ void sound_tick(void) { mic_idle_count--; if (mic_idle_count == 0U) { register_clear_bits(&DFSDM1_Channel0->CHCFGR1, DFSDM_CHCFGR1_DFSDMEN); + mic_buffer_count = 0U; } } } @@ -31,13 +34,21 @@ void sound_tick(void) { static void DMA1_Stream0_IRQ_Handler(void) { __attribute__((section(".sram4"))) static uint16_t tx_buf[MIC_TX_BUF_SIZE]; - DMA1->LIFCR |= 0x7D; // clear flags + DMA1->LIFCR |= 0x7DU; // clear flags - // process samples - uint8_t buf_idx = (((DMA1_Stream0->CR & DMA_SxCR_CT) >> DMA_SxCR_CT_Pos) == 1U) ? 0U : 1U; - for (uint16_t i=0U; i < MIC_RX_BUF_SIZE; i++) { - tx_buf[2U*i] = ((mic_rx_buf[buf_idx][i] >> 16U) & 0xFFFFU); - tx_buf[(2U*i)+1U] = tx_buf[2U*i]; + if (mic_buffer_count < MIC_SKIP_BUFFERS) { + // Send silence during settling + mic_buffer_count++; + for (uint16_t i = 0U; i < MIC_TX_BUF_SIZE; i++) { + tx_buf[i] = 0U; + } + } else { + // process samples + uint8_t buf_idx = (((DMA1_Stream0->CR & DMA_SxCR_CT) >> DMA_SxCR_CT_Pos) == 1U) ? 0U : 1U; + for (uint16_t i=0U; i < MIC_RX_BUF_SIZE; i++) { + tx_buf[2U*i] = ((mic_rx_buf[buf_idx][i] >> 16U) & 0xFFFFU); + tx_buf[(2U*i)+1U] = tx_buf[2U*i]; + } } BDMA->IFCR |= BDMA_IFCR_CGIF1; @@ -59,7 +70,7 @@ static void BDMA_Channel0_IRQ_Handler(void) { // wait until we're done playing the current buf for (uint32_t timeout_counter = 100000U; timeout_counter > 0U; timeout_counter--){ uint8_t playing_buf = (DMA1_Stream1->CR & DMA_SxCR_CT) >> DMA_SxCR_CT_Pos; - if ((playing_buf != playback_buf) || (!(DMA1_Stream1->CR & DMA_SxCR_EN))) { + if ((playing_buf != playback_buf) || ((DMA1_Stream1->CR & DMA_SxCR_EN) == 0U)) { break; } }