Why does TIM_OC1_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config) reset CC1NE always?
Hello,
When I was trying to use PWM complementary outputs of TIM1, I used CubeMX to create the C code but was not able to turn on the N output while the P output shows right period and duty cycle but no deadtime inserted.
Tried many CubeMX configs and compared that to the example in "STM32F3xx_HAL_Driver\...\Examples\TIM\TIM_ComplementarySignals", no obvious difference was found. I found later the CCER was always set to 0x1 instead of 0x5 for both Ch1P and Ch1N to operation, ticked CC1NE in CCER, the N output showed up and both P and N had right deadtime.
Digging deeper, I found TIM_OC1_SetConfig() in stm32fxxx_hal_tim.c as below
static void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
{
uint32_t tmpccmrx;
uint32_t tmpccer;
uint32_t tmpcr2;
/* Disable the Channel 1: Reset the CC1E Bit */
TIMx->CCER &= ~TIM_CCER_CC1E;
/* Get the TIMx CCER register value */
tmpccer = TIMx->CCER;
/* Get the TIMx CR2 register value */
tmpcr2 = TIMx->CR2;
/* Get the TIMx CCMR1 register value */
tmpccmrx = TIMx->CCMR1;
/* Reset the Output Compare Mode Bits */
tmpccmrx &= ~TIM_CCMR1_OC1M;
tmpccmrx &= ~TIM_CCMR1_CC1S;
/* Select the Output Compare Mode */
tmpccmrx |= OC_Config->OCMode;
/* Reset the Output Polarity level */
tmpccer &= ~TIM_CCER_CC1P;
/* Set the Output Compare Polarity */
tmpccer |= OC_Config->OCPolarity;
if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_1))
{
/* Check parameters */
assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
/* Reset the Output N Polarity level */
tmpccer &= ~TIM_CCER_CC1NP;
/* Set the Output N Polarity */
tmpccer |= OC_Config->OCNPolarity;
/* Reset the Output N State */
tmpccer &= ~TIM_CCER_CC1NE;
}
.......
........
}
Here comes to my question, if TIM_CCER_CC1NE is always reset here, how does it respond to the complementary output config? Is there any function to flip this bit?
I set it as htim1.Instance->CCER |= TIM_CCER_CC1E | TIM_CCER_CC1NE, it's not difficult but lost the idea of HAL driver (abstract wrapping).
Why doesn't TIM_OC1_SetConfig() expose the interface for user to control the setting of CC1NE?
I didn't test the "TIM_ComplementarySignals" example because I don't have the exact devices, has anyone tested it? Does it work?
Thank you for finishing reading the long question. Any comments or help are appreciated in advance.
regards,
Yu