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_DAC12_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 handle
.Instance
= TIM6
;
53 handle
.Init
.Period
= 0xFF;
54 handle
.Init
.Prescaler
= 0;
56 handle
.Init
.ClockDivision
= TIM_CLOCKDIVISION_DIV1
;
57 handle
.Init
.CounterMode
= TIM_COUNTERMODE_UP
;
58 handle
.Init
.RepetitionCounter
= 0;
59 HAL_TIM_Base_Init(&handle
);
61 TIM_MasterConfigTypeDef sMasterConfig
;
62 sMasterConfig
.MasterSlaveMode
= TIM_MASTERSLAVEMODE_ENABLE
;
63 sMasterConfig
.MasterOutputTrigger
= TIM_TRGO_UPDATE
;
64 HAL_TIMEx_MasterConfigSynchronization(&handle
, &sMasterConfig
);
66 HAL_TIM_Base_Start(&handle
);
68 sConfig
.DAC_Trigger
= DAC_TRIGGER_T6_TRGO
;
69 sConfig
.DAC_OutputBuffer
= DAC_OUTPUTBUFFER_ENABLE
;
70 HAL_DAC_ConfigChannel(&hdac
, &sConfig
, DAC_CHANNEL_1
);
72 HAL_DACEx_NoiseWaveGenerate(&hdac
, DAC_CHANNEL_1
, DAC_LFSRUNMASK_BITS10_0
);
73 HAL_DAC_SetValue(&hdac
, DAC_CHANNEL_1
, DAC_ALIGN_12B_L
, 0xcd00);
75 HAL_DAC_Start(&hdac
, DAC_CHANNEL_1
);
80 void audioPlayTone(uint8_t tone
)
82 handle
.Init
.Period
= 64 + (MAX(TONE_MIN
,MIN(tone
, TONE_MAX
)) * TONE_SPREAD
);
83 TIM_Base_SetConfig(handle
.Instance
, &handle
.Init
);
86 void audioSilence(void)
88 HAL_DAC_Stop(&hdac
, DAC_CHANNEL_1
);
89 HAL_TIM_Base_Stop(&handle
);