2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
27 #include "common/maths.h"
29 #include "drivers/audio.h"
31 static DAC_HandleTypeDef hdac
;
32 static TIM_HandleTypeDef handle
;
33 static DAC_ChannelConfTypeDef sConfig
;
35 void audioSetupIO(void)
37 __HAL_RCC_DAC_CLK_ENABLE();
38 __HAL_RCC_TIM6_CLK_ENABLE();
43 GPIO_InitTypeDef GPIO_InitStruct
;
44 GPIO_InitStruct
.Pin
= GPIO_PIN_4
;
45 GPIO_InitStruct
.Mode
= GPIO_MODE_ANALOG
;
46 GPIO_InitStruct
.Pull
= GPIO_NOPULL
;
47 HAL_GPIO_Init(GPIOA
, &GPIO_InitStruct
);
50 void audioGenerateWhiteNoise(void)
52 // TIM6 runs on APB2 at 42Mhz
53 handle
.Instance
= TIM6
;
54 handle
.Init
.Period
= 0xFF;
55 handle
.Init
.Prescaler
= 0;
57 handle
.Init
.ClockDivision
= TIM_CLOCKDIVISION_DIV1
;
58 handle
.Init
.CounterMode
= TIM_COUNTERMODE_UP
;
59 handle
.Init
.RepetitionCounter
= 0;
60 HAL_TIM_Base_Init(&handle
);
62 TIM_MasterConfigTypeDef sMasterConfig
;
63 sMasterConfig
.MasterSlaveMode
= TIM_MASTERSLAVEMODE_ENABLE
;
64 sMasterConfig
.MasterOutputTrigger
= TIM_TRGO_UPDATE
;
65 HAL_TIMEx_MasterConfigSynchronization(&handle
, &sMasterConfig
);
67 HAL_TIM_Base_Start(&handle
);
69 sConfig
.DAC_Trigger
= DAC_TRIGGER_T6_TRGO
;
70 sConfig
.DAC_OutputBuffer
= DAC_OUTPUTBUFFER_ENABLE
;
71 HAL_DAC_ConfigChannel(&hdac
, &sConfig
, DAC_CHANNEL_1
);
73 HAL_DACEx_NoiseWaveGenerate(&hdac
, DAC_CHANNEL_1
, DAC_LFSRUNMASK_BITS10_0
);
74 HAL_DAC_SetValue(&hdac
, DAC_CHANNEL_1
, DAC_ALIGN_12B_L
, 0xcd00);
76 HAL_DAC_Start(&hdac
, DAC_CHANNEL_1
);
81 void audioPlayTone(uint8_t tone
)
83 handle
.Init
.Period
= 64 + (MAX(TONE_MIN
,MIN(tone
, TONE_MAX
)) * TONE_SPREAD
);
84 TIM_Base_SetConfig(handle
.Instance
, &handle
.Init
);
87 void audioSilence(void)
89 HAL_DAC_Stop(&hdac
, DAC_CHANNEL_1
);
90 HAL_TIM_Base_Stop(&handle
);