Skip to content

Commit bfe680a

Browse files
committed
fix(beeper): report PWM init failure instead of failing silently
beeperPwmInit() now returns bool. beeperInit() checks it on both the runtime-assigned pad and the compile-time fallback pad, logging an error via LOG_ERROR(PWM, ...) on failure. The runtime path does not fall through to the compile-time pin on failure, since that pin may have been reassigned to another function. Addresses Qodo review finding on PR #11675.
1 parent 49148fc commit bfe680a

3 files changed

Lines changed: 14 additions & 5 deletions

File tree

src/main/drivers/pwm_output.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ void pwmWriteBeeper(bool onoffBeep)
764764
}
765765
}
766766

767-
void beeperPwmInit(ioTag_t tag, uint16_t frequency)
767+
bool beeperPwmInit(ioTag_t tag, uint16_t frequency)
768768
{
769769
beeperPwm = NULL;
770770

@@ -774,14 +774,17 @@ void beeperPwmInit(ioTag_t tag, uint16_t frequency)
774774
// Attempt to allocate TCH
775775
TCH_t * tch = timerGetTCH(timHw);
776776
if (tch == NULL) {
777-
return;
777+
return false;
778778
}
779779

780780
beeperPwm = &beeperPwmPort;
781781
beeperFrequency = frequency;
782782
IOConfigGPIOAF(IOGetByTag(tag), IOCFG_AF_PP, timHw->alternateFunction);
783783
pwmOutConfigTimer(beeperPwm, tch, PWM_TIMER_HZ, 1000000 / beeperFrequency, (1000000 / beeperFrequency) / 2);
784+
return true;
784785
}
786+
787+
return false;
785788
}
786789

787790
#endif

src/main/drivers/pwm_output.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ bool pwmMotorConfig(const struct timerHardware_s *timerHardware, uint8_t motorIn
5959
void pwmServoPreconfigure(void);
6060
bool pwmServoConfig(const struct timerHardware_s *timerHardware, uint8_t servoIndex, uint16_t servoPwmRate, uint16_t servoCenterPulse, bool enableOutput);
6161
void pwmWriteBeeper(bool onoffBeep);
62-
void beeperPwmInit(ioTag_t tag, uint16_t frequency);
62+
bool beeperPwmInit(ioTag_t tag, uint16_t frequency);
6363

6464
void sendDShotCommand(dshotCommands_e cmd);
6565
void initDShotCommands(void);

src/main/drivers/sound_beeper.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "drivers/time.h"
2424
#include "drivers/io.h"
2525

26+
#include "common/log.h"
2627
#include "drivers/timer.h"
2728
#include "drivers/pwm_mapping.h"
2829
#include "drivers/pwm_output.h"
@@ -84,7 +85,10 @@ void beeperInit(const beeperDevConfig_t *config)
8485
for (int idx = 0; idx < timerHardwareCount; idx++) {
8586
const timerHardware_t *timHw = &timerHardware[idx];
8687
if (timerOverrides(timer2id(timHw->tim))->outputMode == OUTPUT_MODE_BEEPER) {
87-
beeperPwmInit(timHw->tag, BEEPER_PWM_FREQUENCY);
88+
if (!beeperPwmInit(timHw->tag, BEEPER_PWM_FREQUENCY)) {
89+
LOG_ERROR(PWM, "Beeper PWM init failed on assigned pad, beeper disabled");
90+
return;
91+
}
8892
beeperConfigMutable()->pwmMode = true;
8993
systemBeep(false);
9094
return;
@@ -107,7 +111,9 @@ void beeperInit(const beeperDevConfig_t *config)
107111
if (beeperIO) {
108112
IOInit(beeperIO, OWNER_BEEPER, RESOURCE_OUTPUT, 0);
109113
if (beeperConfig()->pwmMode) {
110-
beeperPwmInit(config->ioTag, BEEPER_PWM_FREQUENCY);
114+
if (!beeperPwmInit(config->ioTag, BEEPER_PWM_FREQUENCY)) {
115+
LOG_ERROR(PWM, "Beeper PWM init failed on compile-time pad, beeper disabled");
116+
}
111117
} else {
112118
IOConfigGPIO(beeperIO, config->isOD ? IOCFG_OUT_OD : IOCFG_OUT_PP);
113119
}

0 commit comments

Comments
 (0)