mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-13 19:40:31 +03:00
pwm_beeper_pico.c updates after code review.
This commit is contained in:
parent
3721debfdd
commit
31ac50dfc2
1 changed files with 12 additions and 8 deletions
|
@ -30,14 +30,14 @@
|
||||||
#include "hardware/gpio.h"
|
#include "hardware/gpio.h"
|
||||||
#include "hardware/pwm.h"
|
#include "hardware/pwm.h"
|
||||||
|
|
||||||
static bool beeperInit;
|
static bool beeperConfigured;
|
||||||
static bool beeperEnabled;
|
static bool beeperEnabled;
|
||||||
static int beeperGPIO;
|
static int beeperGPIO;
|
||||||
static uint pwmSlice;
|
static uint pwmSlice;
|
||||||
|
|
||||||
void pwmWriteBeeper(bool on)
|
void pwmWriteBeeper(bool on)
|
||||||
{
|
{
|
||||||
if (beeperInit) {
|
if (beeperConfigured) {
|
||||||
gpio_set_outover(beeperGPIO, on ? GPIO_OVERRIDE_NORMAL : GPIO_OVERRIDE_LOW);
|
gpio_set_outover(beeperGPIO, on ? GPIO_OVERRIDE_NORMAL : GPIO_OVERRIDE_LOW);
|
||||||
pwm_set_enabled(pwmSlice, on);
|
pwm_set_enabled(pwmSlice, on);
|
||||||
beeperEnabled = on;
|
beeperEnabled = on;
|
||||||
|
@ -58,9 +58,15 @@ void beeperPwmInit(const ioTag_t tag, uint16_t frequency)
|
||||||
IOInit(beeperIO, OWNER_BEEPER, 0);
|
IOInit(beeperIO, OWNER_BEEPER, 0);
|
||||||
|
|
||||||
// f = sysclk / div / wrap
|
// f = sysclk / div / wrap
|
||||||
uint32_t clock_divide = 64; // gives a decent range of frequencies (down to 36Hz) for RP2350 at 150MHz
|
// max clock divide is just under 256. Divide of 128 allows down to ~20Hz or so (at 150MHz).
|
||||||
float wrap_f = ((float)SystemCoreClock) / frequency / clock_divide;
|
uint32_t clock_divide;
|
||||||
uint16_t wrap = (uint16_t)(wrap_f > 65535 ? 65535 : wrap_f);
|
for (clock_divide = 1; clock_divide < 256; clock_divide *= 2) {
|
||||||
|
if (SystemCoreClock / frequency / clock_divide < 0xffff) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t wrap = MIN(0xffff, SystemCoreClock / frequency / clock_divide);
|
||||||
pwm_config cfg = pwm_get_default_config();
|
pwm_config cfg = pwm_get_default_config();
|
||||||
pwm_config_set_clkdiv_int(&cfg, clock_divide);
|
pwm_config_set_clkdiv_int(&cfg, clock_divide);
|
||||||
pwm_config_set_wrap(&cfg, wrap);
|
pwm_config_set_wrap(&cfg, wrap);
|
||||||
|
@ -69,9 +75,7 @@ void beeperPwmInit(const ioTag_t tag, uint16_t frequency)
|
||||||
gpio_set_function(beeperGPIO, GPIO_FUNC_PWM);
|
gpio_set_function(beeperGPIO, GPIO_FUNC_PWM);
|
||||||
pwm_set_gpio_level(beeperGPIO, wrap >> 1); // 50% square wave
|
pwm_set_gpio_level(beeperGPIO, wrap >> 1); // 50% square wave
|
||||||
beeperEnabled = false;
|
beeperEnabled = false;
|
||||||
beeperInit = true;
|
beeperConfigured = true;
|
||||||
} else {
|
|
||||||
beeperInit = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue