cancel
Showing results for 
Search instead for 
Did you mean: 

STM Timer OC Polarity runtime change

Palauqui.Georges
Associate II
Posted on January 14, 2016 at 15:38

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 #stm32-timer-polarity
2 REPLIES 2
Palauqui.Georges
Associate II
Posted on June 22, 2016 at 14:40

Dear Everybody,

After many months, I still have this issue on the polarity of the OnePulse, specially for runtime change. Does anynody have an explanation ? Or a workaround ?

Thanks,

Georges Palauqui

Walid FTITI_O
Senior II
Posted on June 22, 2016 at 16:32

Hi GPTechinno,

To avoid the delay/ extra pulse before launch, you should  set the UG bit in the EGR register to force the update generation event once the timer is enabled.

-Hannibal-