cancel
Showing results for 
Search instead for 
Did you mean: 

Timer 2 (32 bit) Acting like 16 bit with encoder (stm32L4)

shingadaddy
Senior
Posted on July 05, 2016 at 20:59

Howdy folks -

Those who want to make use of a 32 bit timer (used with an encoder function or even otherwise)  will find that they might get an unexpected AUTO-RELOAD of the max 16 bit value instead of the max 32 bit value. (Rolls over at 0xFFFF) That might be because of the example code you used to init the time base config. In stm32l4xx_hal_tim.c, the function TIM_Base_SetConfig() can be modified like so:

/* Set the

Autoreload

value */

TIMx->

ARR

= 0xFFFFFFFF;

// (uint32_t)Structure->Period ;

 Period is defined as 0xFFFF in other init spots so you can clobber that to whatever max value you want, up to a 32 bit value,

for the 32 bit timers

. Trying that value on a 16 bit timer might get your complainer to complain.

Just passing on my trip points here and there.
4 REPLIES 4
Posted on July 06, 2016 at 01:50

Always important to define the wrap point with appropriate Period setting.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
shingadaddy
Senior
Posted on July 06, 2016 at 15:15

Yeah being a generally critical parameter, you can't necessarily rely on those POWER ON RESET VALUES specified in the manual when you have example code ''Helping you out!'' Especially since it was probably for one of the 16 bit timers in the first place. 😉

shingadaddy
Senior
Posted on July 06, 2016 at 15:52

And if you're following things to the strict level and using the function

HAL_TIM_Encoder_Init ( which calls TIM_Base_SetConfig() )

You can set ->Period here

htim2.

Init

.

CounterMode

= TIM_COUNTERMODE_UP;

htim2.

Init

.

Period

= 0xFFFFFFFF;

htim2.

Init

.

ClockDivision

= TIM_CLOCKDIVISION_DIV1;

sConfig.

EncoderMode

= TIM_ENCODERMODE_TI1;

sConfig.

IC1Polarity

= TIM_ICPOLARITY_RISING;

sConfig.

IC1Selection

= TIM_ICSELECTION_DIRECTTI;

sConfig.

IC1Prescaler

= TIM_ICPSC_DIV1;

sConfig.

IC1Filter

= 0; 

sConfig.

IC2Polarity

= TIM_ICPOLARITY_RISING;

sConfig.

IC2Selection

= TIM_ICSELECTION_DIRECTTI;

sConfig.

IC2Prescaler

= TIM_ICPSC_DIV1;

sConfig.

IC2Filter

= 0; 

 

HAL_TIM_Encoder_Init(&htim2, &sConfig);

A little more fitting to the example code way, than hacking it in with the previous suggestion I made.

Walid FTITI_O
Senior II
Posted on July 11, 2016 at 16:02

Hi ,

The AHB and APB bridge interfaces have a fixed width which is one word . (As mentionned by ARM

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0226a/I144880.html

). So, all registers are aligned in 4 bytes to be written only in one word.

-Hannibal-