Skip to content

Commit f6c269b

Browse files
oganigljmaralo
andauthored
Center Aligned (#503)
* some changes to make work centeraligned * added a new pwm_type called is_center_aligned * Add dual center PWM and revert changes to phased config * take values as references in for loop --------- Co-authored-by: jmaralo <martinezalonsojn@gmail.com> Co-authored-by: Juan Martinez <114561048+jmaralo@users.noreply.github.com>
1 parent d8ec77a commit f6c269b

7 files changed

Lines changed: 72 additions & 12 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"

Inc/HALAL/Models/TimerPeripheral/TimerPeripheral.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class TimerPeripheral {
3030
public:
3131
enum PWM_MODE : uint8_t {
3232
NORMAL = 0,
33-
PHASED = 1
33+
PHASED = 1,
34+
CENTER_ALIGNED = 2
3435
};
3536

3637
enum TIM_TYPE {
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+
};

Inc/HALAL/Services/PWM/PWM/PWM.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class PWM {
2020
float duty_cycle;
2121
uint32_t frequency;
2222
bool is_on = false;
23+
bool is_center_aligned = false;
2324
static constexpr float CLOCK_FREQ_MHZ_WITHOUT_PRESCALER = 275;
2425
static constexpr float clock_period_ns = (1/CLOCK_FREQ_MHZ_WITHOUT_PRESCALER)*1'000;
2526
bool is_initialized = false;

Src/HALAL/Models/TimerPeripheral/TimerPeripheral.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,21 @@ 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-
for (PWMData pwm_data : init_data.pwm_channels) {
50+
handle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
51+
for (PWMData &pwm_data : init_data.pwm_channels) {
5152
if (pwm_data.mode == PHASED) {
5253
handle->Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
54+
break;
55+
} else if (pwm_data.mode == CENTER_ALIGNED) {
56+
handle->Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED3;
57+
handle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
5358
break;
5459
}
5560
}
5661
handle->Init.Period = init_data.period;
57-
handle->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
58-
handle->Init.RepetitionCounter = 0;
59-
handle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
60-
62+
handle->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
63+
handle->Init.RepetitionCounter = 0;
64+
6165
if (init_data.type == BASE) {
6266
if (HAL_TIM_Base_Init(handle) != HAL_OK){
6367
ErrorHandler("Unable to init base timer on %d", name.c_str());
@@ -87,7 +91,7 @@ void TimerPeripheral::init() {
8791
ErrorHandler("Unable to configure master synchronization on %d", name.c_str());
8892
}
8993

90-
for (pair<uint32_t, uint32_t> channels_rising_falling : init_data.input_capture_channels) {
94+
for (pair<uint32_t, uint32_t> &channels_rising_falling : init_data.input_capture_channels) {
9195
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
9296
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
9397
sConfigIC.ICFilter = 0;
@@ -103,7 +107,7 @@ void TimerPeripheral::init() {
103107
}
104108
}
105109

106-
for (PWMData pwm_data : init_data.pwm_channels) {
110+
for (PWMData &pwm_data : init_data.pwm_channels) {
107111
sConfigOC.OCPolarity = init_data.polarity;
108112
sConfigOC.OCNPolarity = init_data.negated_polarity;
109113
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
@@ -155,7 +159,7 @@ void TimerPeripheral::init() {
155159
}
156160

157161
void TimerPeripheral::start() {
158-
for (TimerPeripheral timer : timers) {
162+
for (TimerPeripheral &timer : timers) {
159163
if (timer.is_registered()) {
160164
timer.init();
161165
}
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+
}

Src/HALAL/Services/PWM/PWM/PWM.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ PWM::PWM(Pin& pin) {
1919
TimerPeripheral& timer = TimerPeripheral::available_pwm.at(pin).first;
2020
TimerPeripheral::PWMData& pwm_data = TimerPeripheral::available_pwm.at(pin).second;
2121

22-
if (pwm_data.mode != TimerPeripheral::PWM_MODE::NORMAL) {
23-
ErrorHandler("Pin %s is not registered as a NORMAL PWM", pin.to_string());
22+
if (pwm_data.mode == TimerPeripheral::PWM_MODE::CENTER_ALIGNED) {
23+
is_center_aligned = true;
24+
}
25+
if(pwm_data.mode == TimerPeripheral::PWM_MODE::PHASED){
26+
ErrorHandler("Pin %s is registered as Phased being a single PWM", pin.to_string());
2427
}
2528

2629
peripheral = &timer;
@@ -59,9 +62,12 @@ void PWM::set_duty_cycle(float duty_cycle) {
5962
}
6063

6164
void PWM::set_frequency(uint32_t frequency) {
65+
if(is_center_aligned){
66+
frequency = 2*frequency;
67+
}
6268
this->frequency = frequency;
6369
TIM_TypeDef& timer = *peripheral->handle->Instance;
64-
timer.ARR = (HAL_RCC_GetPCLK1Freq()*2 / (timer.PSC+1)) / frequency;
70+
timer.ARR = (HAL_RCC_GetPCLK1Freq() * 2 / (timer.PSC + 1)) / frequency;
6571
set_duty_cycle(duty_cycle);
6672
}
6773

0 commit comments

Comments
 (0)