Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix errors in TMS pwm #301

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions firmware/mcal/stm32f767/periph/pwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include <algorithm>
#include <cstdint>

#include "shared/periph/pwm.h"
Expand Down Expand Up @@ -47,27 +48,29 @@ class PWMOutput : public shared::periph::PWMOutput {
}

void SetFrequency(float frequency) override {
frequency_ = std::max(kMinimumFrequency, frequency);
uint32_t autoreload = GetTimerFrequency() / frequency_;
float frequency_ = std::max(kMinimumFrequency, frequency);
uint32_t autoreload = static_cast<uint32_t>(
static_cast<float>(GetTimerFrequency()) / frequency_);

__HAL_TIM_SetAutoreload(htim, autoreload);
__HAL_TIM_SetAutoreload(htim_, autoreload);
}

float GetFrequency() override {
float frequency = GetTimerFrequency() / __HAL_TIM_GetAutoreload(htim_);
float frequency = static_cast<float>(GetTimerFrequency()) /
static_cast<float>(__HAL_TIM_GetAutoreload(htim_));

return frequency;
}

private:
TIM_HandleTypeDef* htim_;
// stm32f767 has a 16-bit autoreload register -> min frequency = 1/65535
static constexpr kMinimumFrequency = 0.000015259022f;
static constexpr float kMinimumFrequency = 0.000015259022f;
uint32_t channel_;
float duty_cycle_ = 0;

uint32_t GetTimerFrequency() {
uint32_t tickFreq = __HAL_TIM_GetTickFreq();
uint32_t tickFreq = HAL_GetTickFreq();

return tickFreq;
}
Expand Down
Loading