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);
70 IOConfigGPIOAF(beeperPwm
.io
, IOCFG_AF_PP
, timer
->alternateFunction
);
72 pwmOutConfig(&beeperPwm
.channel
, timer
, PWM_TIMER_1MHZ
, PWM_TIMER_1MHZ
/ freqBeep
, (PWM_TIMER_1MHZ
/ freqBeep
) / 2, 0);
74 *beeperPwm
.channel
.ccr
= 0;
75 beeperPwm
.enabled
= false;
81 void systemBeep(bool onoff
)
84 if (beeperFrequency
== 0) {
85 IOWrite(beeperIO
, beeperInverted
? onoff
: !onoff
);
89 pwmWriteBeeper(onoff
);
97 void systemBeepToggle(void)
100 if (beeperFrequency
== 0) {
103 #ifdef USE_PWM_OUTPUT
111 void beeperInit(const beeperDevConfig_t
*config
)
114 beeperFrequency
= config
->frequency
;
115 if (beeperFrequency
== 0) {
116 beeperIO
= IOGetByTag(config
->ioTag
);
117 beeperInverted
= config
->isInverted
;
119 IOInit(beeperIO
, OWNER_BEEPER
, 0);
120 IOConfigGPIO(beeperIO
, config
->isOpenDrain
? IOCFG_OUT_OD
: IOCFG_OUT_PP
);
124 #ifdef USE_PWM_OUTPUT
126 const ioTag_t beeperTag
= config
->ioTag
;
127 beeperPwmInit(beeperTag
, beeperFrequency
);