Skip to content

Commit

Permalink
add pwmoutput and analogoutput to stm32f767 mcal
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamara Xu committed Oct 14, 2024
1 parent ba75eec commit 5a885f1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
33 changes: 33 additions & 0 deletions firmware/mcal/stm32f767/periph/analog_output.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// @author Tamara Xu
/// @date 2024-10-13

#pragma once

#include <cstdint>

#include "shared/periph/analog_output.h"
#include "shared/util/mappers/clamper.h"
#include "stm32f7xx_hal.h"

namespace mcal::stm32f767::periph {

class AnalogOutput : public shared::periph::AnalogOutput {
public:
AnalogOutput(TIM_HandleTypeDef* htim, uint32_t channel)
: htim_(htim), channel_(channel) {}

void SetVoltage (float voltage) override {
voltage_ = shared::util::Clamper<float>::Evaluate(voltage, 0, 3.3);
uint32_t pulse = (voltage_ * float(__HAL_TIM_GetAutoreload(htim_))) /
(3.3 * 100.0f);

__HAL_TIM_SetCompare(htim_, channel_, pulse);
}

private:
TIM_HandleTypeDef* htim_;
uint32_t channel_;
float voltage_;
};

} // namespace mcal::stm32f767::periph
12 changes: 12 additions & 0 deletions firmware/mcal/stm32f767/periph/pwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,22 @@ class PWMOutput : public shared::periph::PWMOutput {
return float(pulse) / float(period) * 100.0f;
}

void SetFrequency(float frequency) override {
frequency_ = std::max((float) 0, frequency);
uint32_t autoreload = 1/frequency_;

__HAL_TIM_SetAutoreload(htim, autoreload);
}

float GetFrequency() override {
return frequency_;
}

private:
TIM_HandleTypeDef* htim_;
uint32_t channel_;
float duty_cycle_ = 0;
float frequency_;
};

} // namespace mcal::stm32f767::periph

0 comments on commit 5a885f1

Please sign in to comment.