Use the cached value of useDshotTelemetry to ensure consistent runtime use if dshot_b...
[betaflight.git] / src / main / drivers / sound_beeper.c
blob5d52bf0fc68cc49f67711b539e4e66396b4fa301
1 /*
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)
8 * any later version.
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/>.
21 #include <stdbool.h>
22 #include <stdint.h>
24 #include "platform.h"
26 #include "drivers/io.h"
27 #include "drivers/pwm_output.h"
29 #include "pg/beeper_dev.h"
31 #include "sound_beeper.h"
33 #ifdef USE_BEEPER
34 static IO_t beeperIO = DEFIO_IO(NONE);
35 static bool beeperInverted = false;
36 static uint16_t beeperFrequency = 0;
38 #ifdef USE_PWM_OUTPUT
39 static pwmOutputPort_t beeperPwm;
40 static uint16_t freqBeep = 0;
42 static void pwmWriteBeeper(bool on)
44 if (!beeperPwm.io) {
45 return;
48 if (on) {
49 *beeperPwm.channel.ccr = (PWM_TIMER_1MHZ / freqBeep) / 2;
50 beeperPwm.enabled = true;
51 } else {
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);
71 freqBeep = frequency;
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;
78 #endif
79 #endif
81 void systemBeep(bool onoff)
83 #ifdef USE_BEEPER
84 if (beeperFrequency == 0) {
85 IOWrite(beeperIO, beeperInverted ? onoff : !onoff);
87 #ifdef USE_PWM_OUTPUT
88 else {
89 pwmWriteBeeper(onoff);
91 #endif
92 #else
93 UNUSED(onoff);
94 #endif
97 void systemBeepToggle(void)
99 #ifdef USE_BEEPER
100 if (beeperFrequency == 0) {
101 IOToggle(beeperIO);
103 #ifdef USE_PWM_OUTPUT
104 else {
105 pwmToggleBeeper();
107 #endif
108 #endif
111 void beeperInit(const beeperDevConfig_t *config)
113 #ifdef USE_BEEPER
114 beeperFrequency = config->frequency;
115 if (beeperFrequency == 0) {
116 beeperIO = IOGetByTag(config->ioTag);
117 beeperInverted = config->isInverted;
118 if (beeperIO) {
119 IOInit(beeperIO, OWNER_BEEPER, 0);
120 IOConfigGPIO(beeperIO, config->isOpenDrain ? IOCFG_OUT_OD : IOCFG_OUT_PP);
122 systemBeep(false);
124 #ifdef USE_PWM_OUTPUT
125 else {
126 const ioTag_t beeperTag = config->ioTag;
127 beeperPwmInit(beeperTag, beeperFrequency);
129 #endif
130 #else
131 UNUSED(config);
132 #endif