Skip to content

Commit

Permalink
add template for buzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
PonomarevDA committed Jan 12, 2024
1 parent fa4f690 commit 43e333a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 50 deletions.
17 changes: 16 additions & 1 deletion Src/dronecan_application/modules/buzzer.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
// Copyright (C) 2023 Dmitry Ponomarev <[email protected]>
// Distributed under the terms of the GPL v3 license, available in the file LICENSE.

#include "pmu.hpp"
#include "buzzer.hpp"
#include "dronecan.h"
#include "main.h"
#include "params.hpp"
#include "string_params.hpp"
#include "storage.h"
#include "periphery/pwm/pwm.hpp"

Buzzer::Buzzer() {

}

int8_t Buzzer::init() {
return 0;
}

void set_params(uint32_t frequency, uint32_t duration) {

}

void process() {

}
7 changes: 7 additions & 0 deletions Src/dronecan_application/modules/buzzer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
extern "C" {
#endif

#include <stdint.h>

class Buzzer {
Buzzer();
int8_t init();
void set_params(uint32_t frequency, uint32_t duration);
void process();
};

#ifdef __cplusplus
}
Expand Down
8 changes: 3 additions & 5 deletions Src/periphery/pwm/pwm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ extern "C" {
#endif

/**
* @note PWM pinout related to RaccoonLab Mini v2 node
* @note PWM pinout related to VTOL PMU node
*/
enum class PwmPin {
PWM_1, // PB7
PWM_2, // PB6
PWM_3, // PB4
PWM_4, // PB5
PWM_BUZZER, // PB7, TIM4_CH2
PWM_AMOUNT,
};

Expand All @@ -26,6 +23,7 @@ class PwmPeriphery {
public:
static int8_t init(PwmPin pin);
static void set_duration(const PwmPin pin, uint32_t duration_us);
static void set_frequency(const PwmPin pin, uint32_t frequency_hz);
static uint32_t get_duration(PwmPin pin);
};

Expand Down
62 changes: 18 additions & 44 deletions Src/platform/stm32f103/pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,17 @@
#include "periphery/pwm/pwm.hpp"
#include "main.h"

extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim4;

int8_t PwmPeriphery::init(PwmPin pwm_pin) {
switch (pwm_pin) {
case PwmPin::PWM_1:
case PwmPin::PWM_BUZZER:
if (HAL_OK != HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2)) {
return -1;
}
TIM4->CCR2 = 1000;
break;

case PwmPin::PWM_2:
if (HAL_OK != HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1)) {
return -1;
}
TIM4->CCR1 = 1000;
break;

case PwmPin::PWM_3:
if (HAL_OK != HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1)) {
return -1;
}
TIM3->CCR1 = 1000;
break;

case PwmPin::PWM_4:
if (HAL_OK != HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2)) {
return -1;
}
TIM3->CCR2 = 1000;
break;

default:
return -1;
}
Expand All @@ -46,20 +24,28 @@ int8_t PwmPeriphery::init(PwmPin pwm_pin) {

void PwmPeriphery::set_duration(const PwmPin pwm_pin, uint32_t duration_us) {
switch (pwm_pin) {
case PwmPin::PWM_1:
case PwmPin::PWM_BUZZER:
TIM4->CCR2 = duration_us;
break;

case PwmPin::PWM_2:
TIM4->CCR1 = duration_us;
default:
break;
}
}

case PwmPin::PWM_3:
TIM3->CCR1 = duration_us;
break;
void PwmPeriphery::set_frequency(const PwmPin pwm_pin, uint32_t frequency_hz) {
if (frequency_hz == 0) {
return;
}

volatile uint32_t* arr_reg;
uint16_t period_us;

case PwmPin::PWM_4:
TIM3->CCR2 = duration_us;
switch (pwm_pin) {
case PwmPin::PWM_BUZZER:
arr_reg = &(htim4.Instance->ARR);
period_us = 1000000 / frequency_hz;
*arr_reg = period_us;
break;

default:
Expand All @@ -71,22 +57,10 @@ uint32_t PwmPeriphery::get_duration(PwmPin pwm_pin) {
uint32_t pwm_duration;

switch (pwm_pin) {
case PwmPin::PWM_1:
case PwmPin::PWM_BUZZER:
pwm_duration = TIM4->CCR2;
break;

case PwmPin::PWM_2:
pwm_duration = TIM4->CCR1;
break;

case PwmPin::PWM_3:
pwm_duration = TIM3->CCR1;
break;

case PwmPin::PWM_4:
pwm_duration = TIM3->CCR2;
break;

default:
pwm_duration = 0;
break;
Expand Down
4 changes: 4 additions & 0 deletions Src/platform/ubuntu/pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ int8_t PwmPeriphery::init(PwmPin pwm_pin) {
void PwmPeriphery::set_duration(const PwmPin pwm_pin, uint32_t duration_us) {
(void)pwm_pin;
(void)duration_us;
}

void PwmPeriphery::set_frequency(const PwmPin pwm_pin, uint32_t frequency_hz) {
(void)pwm_pin;
(void)frequency_hz;
}

uint32_t PwmPeriphery::get_duration(PwmPin pwm_pin) {
Expand Down

0 comments on commit 43e333a

Please sign in to comment.