LPTIMER Problem

cancel
Showing results for 
Search instead for 
Did you mean: 

LPTIMER Problem

257 Views
Contributor V

First I setup the timer

static uint32_t added_val = 0;

void LPTMR_Setup(void)
{
    lptmr_config_t lptmr_config;

    lptmr_config.timerMode = kLPTMR_TimerModeTimeCounter;
    lptmr_config.pinSelect = kLPTMR_PinSelectInput_0;
    lptmr_config.pinPolarity = kLPTMR_PinPolarityActiveHigh;
    lptmr_config.enableFreeRunning = true;
    lptmr_config.bypassPrescaler = true;
    lptmr_config.prescalerClockSource = kLPTMR_PrescalerClock_1; //1Khz - 1ms
    lptmr_config.value = kLPTMR_Prescale_Glitch_0;

    LPTMR_Init(LPTMR0, &lptmr_config);

    LPTMR_SetTimerPeriod(LPTMR0, 0xFFFF);

    LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
    EnableIRQ(LPTMR0_IRQn);

    LPTMR_StartTimer(LPTMR0);
}

uint32_t LPTMR_GetCount(void)
{
    uint32_t count = LPTMR_GetCurrentTimerCount(LPTMR0);

    uint32_t total_ms = count + added_val;

    return total_ms;
}


void LPTMR0_IRQHandler(void)
{
    LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);

    added_val += 0xFFFF;

    /*
    Workaround for TWR-KV58: because write buffer is enabled, adding
    memory barrier instructions to make sure clearing interrupt flag completed
    before go out ISR
    */
    __DSB();
    __ISB();
}

Then  I check it

while(1)
    {
        time_stamp = LPTMR_GetCount();
        if (time_stamp >= t1)
        {
            t1 = time_stamp + PERIOD_1;
            GPIO_PortToggle(LED_PORT, (1<<LEDR_PIN));
        }
        if (time_stamp >= t2)
        {
            t2 = time_stamp + PERIOD_2;
            GPIO_PortToggle(LED_PORT, (1<<LEDB_PIN));
        }
        if (time_stamp >= t3)
        {
            t3 = time_stamp + PERIOD_3;
            GPIO_PortToggle(LED_PORT, (1<<LEDG_PIN));
        }
        if (time_stamp >= t4)
        {
            t4 = time_stamp + PERIOD_4;
            GPIO_PortToggle(LED_PORT, (1<<LEDO_PIN));
        }
    }

And I see LEDs blinking at right intervals...till first interrupt occurs.

So the fix in the interrupt routine doesn't work?

0 Kudos
8 Replies

97 Views
Contributor V

uint32_t maximum value is 0xFFFFFFFF. So it takes 2^32 / 0xFFFF = 65535 interrupts to overflow uint32_t.

I set a breakpoint on added_val += 0xFFFF; and I see

1st interrupt - added_val = 65535 (0xFFFF * 1)

2nd interrupt - added_val = 131070 (0xFFFF * 2)

3d interrupt - added_val = 196605 (0xFFFF * 3)

In order to keep total milliseconds count I add a current value to the accumulated value uint32_t total_ms = count + added_val;

0 Kudos

97 Views
NXP TechSupport
NXP TechSupport

Hi,

I am sorry that I mistake the max value. Can you explain your question in detail? What's your board,IDE and SDK?

Have a great day,
TIC

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos

97 Views
Contributor V

My board - TWR-KV58F220M, the IDE - MCUExpresso. The problem - LEDs stop blinking after the first interrupt (also I see the interrupt occurs and added_val updated correctly).

0 Kudos

97 Views
NXP TechSupport
NXP TechSupport

Hi,

When the interrupt runs over, what the time_stamp value is. And what's the comparison result with t1, t2 ,t3, t4? If your  "time_stamp" is bigger than t1,t2,t3,t4, this may cause some problems.

Have a great day,
TIC

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos

97 Views
Contributor V

When I set a break point to uint32_t count = LPTMR_GetCurrentTimerCount(LPTMR0); - count  doesn't change its value. LPTMR_GetCurrentTimerCount doesn't work correctly.

0 Kudos

97 Views
NXP TechSupport
NXP TechSupport

Hi,

lptmr_config.enableFreeRunning = true; Try to change it to false.

Have a great day,
TIC

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

 

0 Kudos

97 Views
Contributor V

Now it's better. Runs several minutes then at some point it stops blinking ( timer not incremented) and after a while starts again. I thought it happens in interrupt but when I set a break point at interrupt I see it's not related. Can't catch where it stops.

0 Kudos

97 Views
NXP TechSupport
NXP TechSupport

Hi,

"added_val" may overflow. " count + added_val"  and " time_stamp + PERIOD_1" may overflow. We should avoid. We would better reset these value in several interrupts. For example, after 100 interrupts, we reset these variable to initial value.

You means timer does not increase. We can test it without debug. 

static uint32_t preValue = 0;

static uint32_t curValue = 0;

uint32_t LPTMR_GetCount(void)
{
    uint32_t count = LPTMR_GetCurrentTimerCount(LPTMR0);

    curValue = count ;

      if(curValue != preValue )

   {

            preValue = curValue ;

            return count + added_val;

   }

      if(curValue  == preValue )

   {

         PRINTF("value %x\r\n",count);

         while(1);

   }

}

Have a great day,
TIC

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos