Skip to content

Commit 7c24d06

Browse files
committed
Add dual center PWM and revert changes to phased config
1 parent 5027e63 commit 7c24d06

4 files changed

Lines changed: 58 additions & 7 deletions

File tree

Inc/HALAL/HALAL.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "HALAL/Services/ADC/ADC.hpp"
1111
#include "HALAL/Services/PWM/PWM/PWM.hpp"
1212
#include "HALAL/Services/PWM/DualPWM/DualPWM.hpp"
13+
#include "HALAL/Services/PWM/DualCenterPWM/DualCenterPWM.hpp"
1314
#include "HALAL/Services/PWM/PhasedPWM/PhasedPWM.hpp"
1415
#include "HALAL/Services/PWM/DualPhasedPWM/DualPhasedPWM.hpp"
1516
#include "HALAL/Services/Time/Time.hpp"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include "HALAL/Services/PWM/DualPWM/DualPWM.hpp"
4+
5+
class DualCenterPWM : public DualPWM {
6+
public:
7+
DualCenterPWM(Pin& pin, Pin& pin_negated);
8+
9+
void set_frequency(uint32_t freq_in_hz);
10+
};

Src/HALAL/Models/TimerPeripheral/TimerPeripheral.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,20 @@ void TimerPeripheral::init() {
4747
handle->Instance = handle_to_timer[handle];
4848
handle->Init.Prescaler = init_data.prescaler;
4949
handle->Init.CounterMode = TIM_COUNTERMODE_UP;
50-
handle->Init.Period = init_data.period;
51-
handle->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
52-
handle->Init.RepetitionCounter = 0;
53-
handle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
50+
handle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
5451
for (PWMData pwm_data : init_data.pwm_channels) {
5552
if (pwm_data.mode == PHASED) {
53+
handle->Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
54+
break;
55+
} else if (pwm_data.mode == CENTER_ALIGNED) {
5656
handle->Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED3;
5757
handle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
58-
break;
58+
break;
5959
}
6060
}
61-
61+
handle->Init.Period = init_data.period;
62+
handle->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
63+
handle->Init.RepetitionCounter = 0;
6264

6365
if (init_data.type == BASE) {
6466
if (HAL_TIM_Base_Init(handle) != HAL_OK){
@@ -113,7 +115,8 @@ void TimerPeripheral::init() {
113115
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
114116

115117
if (pwm_data.mode == PHASED) {
116-
sConfigOC.OCMode = TIM_OCMODE_PWM1;
118+
//ASSYMETRIC_MODE_1 means one output per pair of registers (CCR1 - CCR2) for example
119+
sConfigOC.OCMode = TIM_OCMODE_ASSYMETRIC_PWM1;
117120
if (HAL_TIM_PWM_ConfigChannel(handle, &sConfigOC, pwm_data.channel) != HAL_OK) {
118121
ErrorHandler("Unable to configure a PWM channel on %d", name.c_str());
119122
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "HALAL/Services/PWM/DualCenterPWM/DualCenterPWM.hpp"
2+
3+
DualCenterPWM::DualCenterPWM(Pin& pin, Pin& pin_negated) {
4+
if (not TimerPeripheral::available_dual_pwms.contains({pin, pin_negated})) {
5+
ErrorHandler(
6+
"Pins %s and %s are not registered as an available Dual PWM",
7+
pin.to_string(), pin_negated.to_string());
8+
}
9+
10+
TimerPeripheral& timer =
11+
TimerPeripheral::available_dual_pwms.at({pin, pin_negated}).first;
12+
TimerPeripheral::PWMData& pwm_data =
13+
TimerPeripheral::available_dual_pwms.at({pin, pin_negated}).second;
14+
15+
peripheral = &timer;
16+
channel = pwm_data.channel;
17+
18+
if (pwm_data.mode != TimerPeripheral::PWM_MODE::CENTER_ALIGNED) {
19+
ErrorHandler(
20+
"Pins %s and %s are not registered as a CENTER ALIGNED PWM",
21+
pin.to_string(), pin_negated.to_string());
22+
}
23+
24+
Pin::inscribe(pin, TIMER_ALTERNATE_FUNCTION);
25+
Pin::inscribe(pin_negated, TIMER_ALTERNATE_FUNCTION);
26+
timer.init_data.pwm_channels.push_back(pwm_data);
27+
28+
duty_cycle = 0;
29+
}
30+
31+
void DualCenterPWM::set_frequency(uint32_t freq_in_hz) {
32+
this->frequency = freq_in_hz;
33+
TIM_TypeDef& timer = *peripheral->handle->Instance;
34+
timer.ARR =
35+
(HAL_RCC_GetPCLK1Freq() * 2 / (timer.PSC + 1)) / (frequency * 2);
36+
set_duty_cycle(duty_cycle);
37+
}

0 commit comments

Comments
 (0)