cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L476 LPTIM1 counter register

turboscrew
Senior III
Posted on October 06, 2017 at 21:42

I know the counter register read is not very reliable:

Bits 15:0 CNT: Counter value

When the LPTIM is running with an asynchronous clock, reading the LPTIM_CNT register may

return unreliable values. So in this case it is necessary to perform two consecutive read accesses

and verify that the two returned values are identical.

It should be noted that for a reliable LPTIM_CNT register read access, two consecutive read

accesses must be performed and compared. A read access can be considered reliable when the

values of the two consecutive read accesses are equal.

But how many reads is normal?

I tried to find the 'edge' by:

uint32_t read_count()

{

    uint32_t tmp1, tmp2;

    tmp1 = HAL_LPTIM_ReadCounter(&hlptim1); // hlptim1 is a global variable

    while ((tmp2 = HAL_LPTIM_ReadCounter(&hlptim1)) != tmp1) tmp1 = tmp2;

    return tmp1;

}

uint32_t poll_count_change()

{

    uint32_t tmp;

    tmp = read_count();

    while (read_count() == tmp);

    return get_systicks();

}

the worst case seemed to be that the poll_count_change() took about 45ms!

Is that anywhere near normal?

The LPTIM1 was running on LSE and with prescaler 2 and the processor was running @ 48MHz.

#stm32l4 #lptim
13 REPLIES 13
henry.dick
Senior II
Posted on February 14, 2018 at 18:38

How many times to read? As many as necessary. It is essentially asking you to do a double read.

Something like this will work:

  Do (

    Tmp = lptim' cnt

  While (tmp != Lptim' cnt)

  Return tmp

No need to be fancier than that.

Posted on February 14, 2018 at 17:44

Have you tried to set RCC_CCIPR.LPTIM1SEL or .LPTIM2SET appropriately to your request, i.e. to 0b10?

JW

Posted on February 15, 2018 at 15:28

Hi Jan

      Here is a small snippet of my settings.I was trying to generate interrupts every 1msec... The plan is to set the ARR to 1000 and timer tick to 1 microseconds. So I configured the lPTIM2 to clock out of HSI16 and prescaler to 16 and thus I think i should get 1 micro tick. Then with ARR set to 1000 we have a 1msec interrupt. Any suggestions as to why the interrupt does not happen?. Thanks.

LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE_HSI);//Use clock source as HSI

LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_LPTIM2);//This is enabling the APB1

//Set Interrupt enable

/* Configure the NVIC to handle TIM1 update interrupt */

NVIC_SetPriority(LPTIM2_IRQn,0);

NVIC_EnableIRQ(LPTIM2_IRQn);

/* Time base configuration */

TIM_TimeBaseStructure.ClockSource = LL_LPTIM_CLK_SOURCE_INTERNAL; /* TIM1 is a 16bit timer */

TIM_TimeBaseStructure.Prescaler =4;

LL_LPTIM_Init(sParam->LPTIMx, &TIM_TimeBaseStructure);

LL_LPTIM_EnableIT_ARRM(LPTIM2);

LL_LPTIM_Enable(LPTIM2);

hal_LPTIM_SetAutoreload(LPTIM2,1000);

LL_LPTIM_OperatingMode(LPTIM2,LL_LPTIM_OPERATING_MODE_CONTINUOUS);
Posted on February 15, 2018 at 20:49

Hi Jan

  I got the LPTIM to work. One thing was I had not turned on HSI clock. The APB clocks were derived out of MSI and I had not noticed if HSI was ON. So now I got it working.