Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / sound_beeper.c
blob5ea45f36986b1fb064ca3a1e9bf72b3a67e33749
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 #if defined(STM32F1)
71 IOConfigGPIO(beeperPwm.io, IOCFG_AF_PP);
72 #else
73 IOConfigGPIOAF(beeperPwm.io, IOCFG_AF_PP, timer->alternateFunction);
74 #endif
75 freqBeep = frequency;
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;
82 #endif
83 #endif
85 void systemBeep(bool onoff)
87 #ifdef USE_BEEPER
88 if (beeperFrequency == 0) {
89 IOWrite(beeperIO, beeperInverted ? onoff : !onoff);
91 #ifdef USE_PWM_OUTPUT
92 else {
93 pwmWriteBeeper(onoff);
95 #endif
96 #else
97 UNUSED(onoff);
98 #endif
101 void systemBeepToggle(void)
103 #ifdef USE_BEEPER
104 if (beeperFrequency == 0) {
105 IOToggle(beeperIO);
107 #ifdef USE_PWM_OUTPUT
108 else {
109 pwmToggleBeeper();
111 #endif
112 #endif
115 void beeperInit(const beeperDevConfig_t *config)
117 #ifdef USE_BEEPER
118 beeperFrequency = config->frequency;
119 if (beeperFrequency == 0) {
120 beeperIO = IOGetByTag(config->ioTag);
121 beeperInverted = config->isInverted;
122 if (beeperIO) {
123 IOInit(beeperIO, OWNER_BEEPER, 0);
124 IOConfigGPIO(beeperIO, config->isOpenDrain ? IOCFG_OUT_OD : IOCFG_OUT_PP);
126 systemBeep(false);
128 #ifdef USE_PWM_OUTPUT
129 else {
130 const ioTag_t beeperTag = config->ioTag;
131 beeperPwmInit(beeperTag, beeperFrequency);
133 #endif
134 #else
135 UNUSED(config);
136 #endif