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/>.
26 #include "drivers/io.h"
27 #include "drivers/pwm_output.h"
29 #include "pg/beeper_dev.h"
31 #include "sound_beeper.h"
34 static IO_t beeperIO
= DEFIO_IO(NONE
);
35 static bool beeperInverted
= false;
36 static uint16_t beeperFrequency
= 0;
39 static pwmOutputPort_t beeperPwm
;
40 static uint16_t freqBeep
= 0;
42 static void pwmWriteBeeper(bool on
)
49 *beeperPwm
.channel
.ccr
= (PWM_TIMER_1MHZ
/ freqBeep
) / 2;
50 beeperPwm
.enabled
= true;
52 *beeperPwm
.channel
.ccr
= 0;
53 beeperPwm
.enabled
= false;
57 static void pwmToggleBeeper(void)
59 pwmWriteBeeper(!beeperPwm
.enabled
);
62 static void beeperPwmInit(const ioTag_t tag
, uint16_t frequency
)
64 const timerHardware_t
*timer
= timerAllocate(tag
, OWNER_BEEPER
, 0);
65 IO_t beeperIO
= IOGetByTag(tag
);
67 if (beeperIO
&& timer
) {
68 beeperPwm
.io
= beeperIO
;
69 IOInit(beeperPwm
.io
, OWNER_BEEPER
, 0);
71 IOConfigGPIO(beeperPwm
.io
, IOCFG_AF_PP
);
73 IOConfigGPIOAF(beeperPwm
.io
, IOCFG_AF_PP
, timer
->alternateFunction
);
76 pwmOutConfig(&beeperPwm
.channel
, timer
, PWM_TIMER_1MHZ
, PWM_TIMER_1MHZ
/ freqBeep
, (PWM_TIMER_1MHZ
/ freqBeep
) / 2, 0);
78 *beeperPwm
.channel
.ccr
= 0;
79 beeperPwm
.enabled
= false;
85 void systemBeep(bool onoff
)
88 if (beeperFrequency
== 0) {
89 IOWrite(beeperIO
, beeperInverted
? onoff
: !onoff
);
93 pwmWriteBeeper(onoff
);
101 void systemBeepToggle(void)
104 if (beeperFrequency
== 0) {
107 #ifdef USE_PWM_OUTPUT
115 void beeperInit(const beeperDevConfig_t
*config
)
118 beeperFrequency
= config
->frequency
;
119 if (beeperFrequency
== 0) {
120 beeperIO
= IOGetByTag(config
->ioTag
);
121 beeperInverted
= config
->isInverted
;
123 IOInit(beeperIO
, OWNER_BEEPER
, 0);
124 IOConfigGPIO(beeperIO
, config
->isOpenDrain
? IOCFG_OUT_OD
: IOCFG_OUT_PP
);
128 #ifdef USE_PWM_OUTPUT
130 const ioTag_t beeperTag
= config
->ioTag
;
131 beeperPwmInit(beeperTag
, beeperFrequency
);