Test Elapsed time based on CTimer module

Document created by xiangjun.rong Employee on Jun 6, 2019
Version 1Show Document
  • View in full screen mode

Test Elapsed time based on CTimer module

 

Sometimes, It is required to test the time which an api function takes when the function is executed, for example, some users want to test the flash erasing time and flash programming time. User can use GPIO to set/clear and use scope to test the GPIO timing to measure the time an api function takes, the method is very simple and straightforward but inaccurate.

The document describes to configure CTimer as a 32bits  free-running counter, user can read the counter value before and after an api function and compute the counter value difference to get the time the api function takes.

The CTimer of LPC54xxx family counts the APB bus clock, the APB bus clock Is driven by 12 MHz FRO, user can use the following code to measure the elapsed time.

For example, we test the elaped time of  delayTimer(10000); function, we get the variable tPoint1, tPoint2. The actual time is (tPoint2- tPoint1)*(1/12000000).

In the example, the tPoint2=110127, the tPoint1=53, the elapsed time is (110127-53)*(1/12000000)=9.172us.

 

 

 

 

//the souce code focuses on LPC54xxx family

uint32_t tPoint1,tPoint2,tPoint3,tDiff;

void test(void)

{

    tPoint1=CTIMER_GetTimerCountValue(CTIMER2);

    //simulate elapsed time

    delayTimer(10000);

    tPoint2=CTIMER_GetTimerCountValue(CTIMER2);

    tDiff=tPoint2-tPoint1;

 

    //simulate elapsed time

    delayTimer(20000);

    tPoint3=CTIMER_GetTimerCountValue(CTIMER2);

    tDiff=tPoint3-tPoint2;

     PRINTF("Time instand:tPoint1=%d, tPoint2=%d, tPoint3=%d \r\n",tPoint1,tPoint2,tPoint3);

}

Snippet of simple source code based on MCUXpresso tools and LPC54618 board developed by XiangJun Rong

#include "fsl_ctimer.h"

void test(void);

void CTimerInit(void);

void delayTimer(uint32_t elapsedTimer);

uint32_t tPoint1,tPoint2,tPoint3,tDiff;

void CTimerInit(void)

{

    ctimer_config_t config;

    ctimer_match_config_t matchConfig;

    /*CTimer use APB bus clock as Timer tick, set the APB bus clock as 12MHz internal FRO */

     CLOCK_AttachClk(kFRO12M_to_ASYNC_APB);

    CTIMER_GetDefaultConfig(&config);

 

    CTIMER_Init(CTIMER2, &config);

 

    matchConfig.enableCounterReset = true;

    matchConfig.enableCounterStop = false;

    matchConfig.matchValue = 0xFFFFFFFF;

    matchConfig.outControl = kCTIMER_Output_NoAction;

    matchConfig.outPinInitState = true;

    matchConfig.enableInterrupt = false;

    CTIMER_SetupMatch(CTIMER2, kCTIMER_Match_3, &matchConfig);

    CTIMER_StartTimer(CTIMER2);

}

 

void test(void)

{

    tPoint1=CTIMER_GetTimerCountValue(CTIMER2);

    //simulate elapsed time

    delayTimer(10000);

    tPoint2=CTIMER_GetTimerCountValue(CTIMER2);

    tDiff=tPoint2-tPoint1;

 

    //simulate elapsed time

    delayTimer(20000);

    tPoint3=CTIMER_GetTimerCountValue(CTIMER2);

    tDiff=tPoint3-tPoint2;

     PRINTF("Time instand:tPoint1=%d, tPoint2=%d, tPoint3=%d \r\n",tPoint1,tPoint2,tPoint3);

}

 

void delayTimer(uint32_t elapsedTimer)

{

    uint32_t i;

    for(i=0; i<elapsedTimer; i++)

    {

        __asm("nop");

    }

}

 

int main(void)

{

   …………………………………………………………………………………………………..

    PRINTF("Elapse time test start: \r\n");

    CTimerInit();

    test();

    for(;;) {}

…………………………………………

}

 

Attachments

    Outcomes