You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.3 KiB
51 lines
1.3 KiB
#include <libopencm3/stm32/gpio.h>
|
|
#include <libopencm3/stm32/rcc.h>
|
|
#include <libopencm3/stm32/timer.h>
|
|
|
|
#include <buzzer.h>
|
|
#include <macros.h>
|
|
|
|
struct buzzer buzzers[] = {
|
|
{ TIM1, TIM_OC1 },
|
|
{ TIM2, TIM_OC2 },
|
|
{ TIM3, TIM_OC1 },
|
|
{ TIM14, TIM_OC1 },
|
|
};
|
|
|
|
size_t nbuzzers = count_of(buzzers);
|
|
|
|
void buzzer_init(struct buzzer *buzzer)
|
|
{
|
|
timer_reset(buzzer->timer);
|
|
|
|
timer_set_mode(buzzer->timer, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
|
|
|
|
timer_set_prescaler(buzzer->timer, 48);
|
|
timer_set_repetition_counter(buzzer->timer, 0);
|
|
timer_enable_preload(buzzer->timer);
|
|
timer_enable_break_main_output(buzzer->timer);
|
|
timer_continuous_mode(buzzer->timer);
|
|
|
|
timer_disable_oc_output(buzzer->timer, buzzer->channel);
|
|
timer_set_oc_mode(buzzer->timer, buzzer->channel, TIM_OCM_PWM1);
|
|
timer_set_oc_value(buzzer->timer, buzzer->channel, 0);
|
|
timer_enable_oc_output(buzzer->timer, buzzer->channel);
|
|
}
|
|
|
|
void buzzer_enable(struct buzzer *buzzer)
|
|
{
|
|
timer_enable_counter(buzzer->timer);
|
|
}
|
|
|
|
void buzzer_disable(struct buzzer *buzzer)
|
|
{
|
|
timer_disable_counter(buzzer->timer);
|
|
}
|
|
|
|
void buzzer_set_freq(struct buzzer *buzzer, uint32_t freq)
|
|
{
|
|
uint32_t period = 1000000 / freq;
|
|
|
|
timer_set_period(buzzer->timer, period);
|
|
timer_set_oc_value(buzzer->timer, buzzer->channel, period / 2);
|
|
}
|
|
|