Skip to content

Commit c659cda

Browse files
committed
some changes to make work centeraligned
1 parent d47da3d commit c659cda

3 files changed

Lines changed: 18 additions & 12 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: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +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) {
52-
handle->Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
53-
break;
56+
handle->Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED3;
57+
handle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
58+
break;
5459
}
5560
}
56-
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-
61+
62+
6163
if (init_data.type == BASE) {
6264
if (HAL_TIM_Base_Init(handle) != HAL_OK){
6365
ErrorHandler("Unable to init base timer on %d", name.c_str());
@@ -111,8 +113,7 @@ void TimerPeripheral::init() {
111113
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
112114

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

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)