Skip to content

Commit

Permalink
Merge pull request #2891 from iNavFlight/de_f7_pwm_fix
Browse files Browse the repository at this point in the history
Fix timer initialization issues with HAL (F7)
  • Loading branch information
digitalentity authored Mar 7, 2018
2 parents 8c5e2cd + d6a6364 commit a818e2e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
13 changes: 4 additions & 9 deletions src/main/drivers/pwm_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,10 @@ static void pwmOCConfig(TIM_TypeDef *tim, uint8_t channel, uint16_t value, uint8
TIM_OC_InitTypeDef TIM_OCInitStructure;

TIM_OCInitStructure.OCMode = TIM_OCMODE_PWM1;

if (output & TIMER_OUTPUT_N_CHANNEL) {
TIM_OCInitStructure.OCNIdleState = TIM_OCNIDLESTATE_RESET;
TIM_OCInitStructure.OCNPolarity = (output & TIMER_OUTPUT_INVERTED) ? TIM_OCNPOLARITY_LOW : TIM_OCNPOLARITY_HIGH;
} else {
TIM_OCInitStructure.OCIdleState = TIM_OCIDLESTATE_SET;
TIM_OCInitStructure.OCPolarity = (output & TIMER_OUTPUT_INVERTED) ? TIM_OCPOLARITY_LOW : TIM_OCPOLARITY_HIGH;
}

TIM_OCInitStructure.OCIdleState = TIM_OCIDLESTATE_SET;
TIM_OCInitStructure.OCPolarity = (output & TIMER_OUTPUT_INVERTED) ? TIM_OCPOLARITY_LOW : TIM_OCPOLARITY_HIGH;
TIM_OCInitStructure.OCNIdleState = TIM_OCNIDLESTATE_SET;
TIM_OCInitStructure.OCNPolarity = (output & TIMER_OUTPUT_INVERTED) ? TIM_OCNPOLARITY_LOW : TIM_OCNPOLARITY_HIGH;
TIM_OCInitStructure.Pulse = value;
TIM_OCInitStructure.OCFastMode = TIM_OCFAST_DISABLE;

Expand Down
1 change: 1 addition & 0 deletions src/main/drivers/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ void timerStart(void);
void timerForceOverflow(TIM_TypeDef *tim);

uint8_t timerClockDivisor(TIM_TypeDef *tim);
uint32_t timerClock(TIM_TypeDef *tim);

void configTimeBase(TIM_TypeDef *tim, uint16_t period, uint8_t mhz); // TODO - just for migration

Expand Down
20 changes: 6 additions & 14 deletions src/main/drivers/timer_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "platform.h"

#include "build/atomic.h"
#include "build/debug.h"

#include "common/utils.h"

Expand Down Expand Up @@ -257,11 +258,7 @@ void configTimeBase(TIM_TypeDef *tim, uint16_t period, uint8_t mhz)
timeHandle[timerIndex].Handle.Instance = tim;

timeHandle[timerIndex].Handle.Init.Period = (period - 1) & 0xffff; // AKA TIMx_ARR
if (tim == TIM1 || tim == TIM8 || tim == TIM9 || tim == TIM10 || tim == TIM11)
timeHandle[timerIndex].Handle.Init.Prescaler = (HAL_RCC_GetPCLK2Freq() * 2 / ((uint32_t)mhz * 1000000)) - 1;
else
timeHandle[timerIndex].Handle.Init.Prescaler = (HAL_RCC_GetPCLK1Freq() * 2 * 2 / ((uint32_t)mhz * 1000000)) - 1;

timeHandle[timerIndex].Handle.Init.Prescaler = (timerClock(tim) / ((uint32_t)mhz * 1000000)) - 1;
timeHandle[timerIndex].Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
timeHandle[timerIndex].Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
timeHandle[timerIndex].Handle.Init.RepetitionCounter = 0x0000;
Expand All @@ -270,23 +267,18 @@ void configTimeBase(TIM_TypeDef *tim, uint16_t period, uint8_t mhz)
if (tim == TIM1 || tim == TIM2 || tim == TIM3 || tim == TIM4 || tim == TIM5 || tim == TIM8 || tim == TIM9)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
sClockSourceConfig.ClockFilter = 0;
sClockSourceConfig.ClockPolarity = TIM_CLOCKPOLARITY_NONINVERTED;
sClockSourceConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
memset(&sClockSourceConfig, 0, sizeof(sClockSourceConfig));
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&timeHandle[timerIndex].Handle, &sClockSourceConfig) != HAL_OK)
{
if (HAL_TIM_ConfigClockSource(&timeHandle[timerIndex].Handle, &sClockSourceConfig) != HAL_OK) {
return;
}
}
if (tim == TIM1 || tim == TIM2 || tim == TIM3 || tim == TIM4 || tim == TIM5 || tim == TIM8 )
{
TIM_MasterConfigTypeDef sMasterConfig;
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
memset(&sMasterConfig, 0, sizeof(sMasterConfig));
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&timeHandle[timerIndex].Handle, &sMasterConfig) != HAL_OK)
{
if (HAL_TIMEx_MasterConfigSynchronization(&timeHandle[timerIndex].Handle, &sMasterConfig) != HAL_OK) {
return;
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/drivers/timer_stm32f7xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,9 @@ uint8_t timerClockDivisor(TIM_TypeDef *tim)
UNUSED(tim);
return 1;
}

uint32_t timerClock(TIM_TypeDef *tim)
{
UNUSED(tim);
return SystemCoreClock;
}

0 comments on commit a818e2e

Please sign in to comment.