Skip to content

Commit 2574f20

Browse files
committed
Merge branch 'Class_DWT' of https://github.com/HyperloopUPV-H8/ST-LIB into Class_DWT
2 parents b41ae93 + c659cda commit 2574f20

3 files changed

Lines changed: 15 additions & 10 deletions

File tree

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_phased = 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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,19 @@ 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.Period = init_data.period;
51+
handle->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
52+
handle->Init.RepetitionCounter = 0;
53+
handle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
54+
for (PWMData pwm_data : init_data.pwm_channels) {
5155
if (pwm_data.mode == PHASED) {
5256
handle->Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED3;
5357
handle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
5458
break;
5559
}
5660
}
57-
handle->Init.Period = init_data.period;
58-
handle->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
59-
handle->Init.RepetitionCounter = 0;
60-
handle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
61-
61+
62+
6263
if (init_data.type == BASE) {
6364
if (HAL_TIM_Base_Init(handle) != HAL_OK){
6465
ErrorHandler("Unable to init base timer on %d", name.c_str());
@@ -112,8 +113,7 @@ void TimerPeripheral::init() {
112113
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
113114

114115
if (pwm_data.mode == PHASED) {
115-
//ASSYMETRIC_MODE_1 means one output per pair of registers (CCR1 - CCR2) for example
116-
sConfigOC.OCMode = TIM_OCMODE_ASSYMETRIC_PWM1;
116+
sConfigOC.OCMode = TIM_OCMODE_PWM1;
117117
if (HAL_TIM_PWM_ConfigChannel(handle, &sConfigOC, pwm_data.channel) != HAL_OK) {
118118
ErrorHandler("Unable to configure a PWM channel on %d", name.c_str());
119119
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ 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) {
22+
if (pwm_data.mode != TimerPeripheral::PWM_MODE::NORMAL) {
23+
is_phased = true;
2324
ErrorHandler("Pin %s is not registered as a NORMAL PWM", pin.to_string());
2425
}
2526

@@ -59,9 +60,12 @@ void PWM::set_duty_cycle(float duty_cycle) {
5960
}
6061

6162
void PWM::set_frequency(uint32_t frequency) {
63+
if(is_phased){
64+
frequency = 2*frequency;
65+
}
6266
this->frequency = frequency;
6367
TIM_TypeDef& timer = *peripheral->handle->Instance;
64-
timer.ARR = (HAL_RCC_GetPCLK1Freq()*2 / (timer.PSC+1)) / frequency;
68+
timer.ARR = (HAL_RCC_GetPCLK1Freq() * 2 / (timer.PSC + 1)) / frequency;
6569
set_duty_cycle(duty_cycle);
6670
}
6771

0 commit comments

Comments
 (0)