stm32f0: stm32f1: buzzer: initial implementation
This commit is contained in:
parent
0fb4d67cce
commit
5730538c5f
3 changed files with 113 additions and 0 deletions
13
include/buzzer.h
Normal file
13
include/buzzer.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
#include <libopencm3/stm32/timer.h>
|
||||
|
||||
struct buzzer {
|
||||
uint32_t timer;
|
||||
enum tim_oc_id channel;
|
||||
};
|
||||
|
||||
void buzzer_init(struct buzzer *buzzer);
|
||||
void buzzer_enable(struct buzzer *buzzer);
|
||||
void buzzer_disable(struct buzzer *buzzer);
|
||||
void buzzer_set_freq(struct buzzer *buzzer, uint32_t freq);
|
51
source/platform/stm32f0/buzzer.c
Normal file
51
source/platform/stm32f0/buzzer.c
Normal file
|
@ -0,0 +1,51 @@
|
|||
#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);
|
||||
}
|
49
source/platform/stm32f1/buzzer.c
Normal file
49
source/platform/stm32f1/buzzer.c
Normal file
|
@ -0,0 +1,49 @@
|
|||
#include <libopencm3/stm32/gpio.h>
|
||||
#include <libopencm3/stm32/rcc.h>
|
||||
#include <libopencm3/stm32/timer.h>
|
||||
|
||||
#include <buzzer.h>
|
||||
#include <macros.h>
|
||||
|
||||
struct buzzer buzzers[] = {
|
||||
{ TIM2, TIM_OC3 },
|
||||
{ TIM2, TIM_OC4 },
|
||||
};
|
||||
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, 72);
|
||||
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);
|
||||
}
|
Loading…
Add table
Reference in a new issue