AnsweredAssumed Answered

STM Timer OC Polarity runtime change

Question asked by GPTechinno on Jan 14, 2016
Latest reply on Jun 22, 2016 by FTITI.Walid
Hi,

I have setup a OnePulse mode on a channel of a STM32F446 Timer TIM5_CH1 :
void TIM_MS_Init(void) {
    TIM_OC_InitTypeDef sConfigOC;
    /* Compute the prescaler value to have TIM5 counter clock equal to 2 kHz -> precision 1 ms on pulse width */
    uint32_t uwPrescalerValue = (uint32_t)(((SystemCoreClock / 2) / (2000)) - 1);
 
    TimMsHandle.Instance = TIM_MS;
    TimMsHandle.Init.Period = 10;
    TimMsHandle.Init.Prescaler = uwPrescalerValue;
    TimMsHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    TimMsHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
    TimMsHandle.Init.RepetitionCounter = 0;
 
    HAL_TIM_OnePulse_Init(&TimMsHandle, TIM_OPMODE_SINGLE);
 
    sConfigOC.OCMode = TIM_OCMODE_PWM2;
    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
//  sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
    sConfigOC.Pulse = 1;
    HAL_TIM_OC_ConfigChannel(&TimMsHandle, &sConfigOC, TIM_MS_START_OUT_CHANNEL);
}
Note it is a mix setup between a OnePulse and a regular OC, because I need to manually launch the OnePulse signal, not triggered on a Input Compare pin.

Later in runtime, here is my manual launch :
uint16_t pulse_width; // this contain the desire pusle width
__HAL_TIM_SET_COUNTER(&TimMsHandle, 1);
HAL_TIM_OC_Start(&TimMsHandle, TIM_MS_START_OUT_CHANNEL);
__HAL_TIM_SET_AUTORELOAD(&TimMsHandle, pulse_width * 2);
Note I need immediat launch (no delay needed).

With logic anaylzer, I can observe the desired signal, but I have trouble setting up the signal initial state. With this init above :
- I get a HIGH Level signal before the launch
- Then after first launch, I observe a falling edge only. It should be the second part of the OnePulse because I used OCPolarity = TIM_OCPOLARITY_HIGH
- After second (and ongoing) launch, I observe a good looking OnePulse with both edge (rising then falling) with desired timings.

So it look like everything is fine, except the signal initial state, it should be LOW Level is select a TIM_OCPOLARITY_HIGH.

Does anybody know how to affect signal initial state ? I tryed to seting the pin as GPIO_OUTPUT_PP, then write to LOW, but when TIM setup use the PIN as AF, it goes back to HIGH...

Even stranger, if in run time I use :
TIM_SET_CAPTUREPOLARITY(&TimMsHandle, TIM_MS_START_OUT_CHANNEL, TIM_OCPOLARITY_HIGH);
or
TIM_SET_CAPTUREPOLARITY(&TimMsHandle, TIM_MS_START_OUT_CHANNEL, TIM_OCPOLARITY_LOW);
I got different results :
- if transistion from TIM_OCPOLARITY_HIGH to TIM_OCPOLARITY_LOW, I can see the signal doing a rising edge as desired (in order to prepare for a going-low-pulse), and later OnePulse are as expected "falling-then-rising-edge"
- if transition from TIM_OCPOLARITY_LOW to TIM_OCPOLARITY_HIGH, I don't see any edge (signal remain HIGH) and later on generated OnePulse are also "falling-then-rising-edge" logic, look like it stayed in TIM_OCPOLARITY_LOW mode...

So I managed to go from TIM_OCPOLARITY_HIGH to TIM_OCPOLARITY_LOW, but not back to TIM_OCPOLARITY_HIGH !!!

Does anybody have an explanation ?

Thanks,
Georges Palauqui

Outcomes