From 3940405c68c7534d43583f115aaa1e1c6cf00b26 Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Sun, 9 Jun 2024 14:23:05 +0200 Subject: [PATCH] stm32_dac: refactored stm32dac_init() to have a per-instance initialization --- platform/drivers/audio/audio_Mod17.c | 5 ++- platform/drivers/audio/stm32_dac.cpp | 60 ++++++++++++++++------------ platform/drivers/audio/stm32_dac.h | 12 +++--- 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/platform/drivers/audio/audio_Mod17.c b/platform/drivers/audio/audio_Mod17.c index cef8d429..ebecb273 100644 --- a/platform/drivers/audio/audio_Mod17.c +++ b/platform/drivers/audio/audio_Mod17.c @@ -59,13 +59,16 @@ void audio_init() gpio_setMode(SPK_MUTE, OUTPUT); gpio_setMode(MIC_MUTE, OUTPUT); gpio_setMode(AUDIO_MIC, INPUT_ANALOG); + gpio_setMode(AUDIO_SPK, INPUT_ANALOG); gpio_setMode(BASEBAND_RX, INPUT_ANALOG); + gpio_setMode(BASEBAND_TX, INPUT_ANALOG); gpio_setPin(SPK_MUTE); // Off = logic high gpio_clearPin(MIC_MUTE); // Off = logic low max9814_setGain(0); // 40 dB gain - stm32dac_init(); + stm32dac_init(STM32_DAC_CH1); + stm32dac_init(STM32_DAC_CH2); stm32adc_init(STM32_ADC_ADC2); } diff --git a/platform/drivers/audio/stm32_dac.cpp b/platform/drivers/audio/stm32_dac.cpp index b4aebe41..cebbc252 100644 --- a/platform/drivers/audio/stm32_dac.cpp +++ b/platform/drivers/audio/stm32_dac.cpp @@ -1,8 +1,8 @@ /*************************************************************************** - * Copyright (C) 2023 by Federico Amedeo Izzo IU2NUO, * - * Niccolò Izzo IU2KIN * - * Frederik Saraci IU2NRO * - * Silvano Seva IU2KWO * + * Copyright (C) 2023 - 2024 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * * * * 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 * @@ -110,32 +110,42 @@ void __attribute__((used)) DMA1_Stream6_IRQHandler() -void stm32dac_init() +void stm32dac_init(const uint8_t instance) { - // Configure GPIOs - gpio_setMode(GPIOA, 4, INPUT_ANALOG); - gpio_setMode(GPIOA, 5, INPUT_ANALOG); - // Enable peripherals - RCC->APB1ENR |= RCC_APB1ENR_DACEN - | RCC_APB1ENR_TIM6EN - | RCC_APB1ENR_TIM7EN; - __DSB(); + RCC->APB1ENR |= RCC_APB1ENR_DACEN; - // DAC common configuration - DAC->CR = DAC_CR_DMAEN2 // Enable DMA - | DAC_CR_TSEL2_1 // TIM7 as trigger source for CH2 - | DAC_CR_TEN2 // Enable trigger input - | DAC_CR_EN2 // Enable CH2 + switch(instance) + { + case STM32_DAC_CH1: + { + RCC->APB1ENR |= RCC_APB1ENR_TIM6EN; + __DSB(); - | DAC_CR_DMAEN1 // Enable DMA - | 0x00 // TIM6 as trigger source for CH1 - | DAC_CR_TEN1 // Enable trigger input - | DAC_CR_EN1; // Enable CH1 + DAC->CR |= DAC_CR_DMAEN1 // Enable DMA + | 0x00 // TIM6 as trigger source for CH1 + | DAC_CR_TEN1 // Enable trigger input + | DAC_CR_EN1; // Enable CH1 + } + break; - // Register end-of-transfer callbacks - chState[0].stream.setEndTransferCallback(std::bind(stopTransfer, 0)); - chState[1].stream.setEndTransferCallback(std::bind(stopTransfer, 1)); + case STM32_DAC_CH2: + { + RCC->APB1ENR |= RCC_APB1ENR_TIM7EN; + __DSB(); + + DAC->CR |= DAC_CR_DMAEN2 // Enable DMA + | DAC_CR_TSEL2_1 // TIM7 as trigger source for CH2 + | DAC_CR_TEN2 // Enable trigger input + | DAC_CR_EN2; // Enable CH2 + } + break; + + default: + break; + } + + chState[instance].stream.setEndTransferCallback(std::bind(stopTransfer, instance)); } void stm32dac_terminate() diff --git a/platform/drivers/audio/stm32_dac.h b/platform/drivers/audio/stm32_dac.h index 56a320d5..1401b00c 100644 --- a/platform/drivers/audio/stm32_dac.h +++ b/platform/drivers/audio/stm32_dac.h @@ -1,8 +1,8 @@ /*************************************************************************** - * Copyright (C) 2023 by Federico Amedeo Izzo IU2NUO, * - * Niccolò Izzo IU2KIN * - * Frederik Saraci IU2NRO * - * Silvano Seva IU2KWO * + * Copyright (C) 2023 - 2024 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * * * * 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 * @@ -58,8 +58,10 @@ extern const struct audioDriver stm32_dac_audio_driver; /** * Initialize the driver and the peripherals. + * + * @param instance: DAC instance number. */ -void stm32dac_init(); +void stm32dac_init(const uint8_t instance); /** * Shutdown the driver and the peripherals.