This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TMS320F28335: How can I measure the elapsed time between each loop?

Part Number: TMS320F28335
Other Parts Discussed in Thread: CONTROLSUITE, C2000WARE

Tool/software: Code Composer Studio

Hello

I am trying to measure the elapsed time between each time running the code. I looked at example "CpuTimer" but I don't know if I can do it with this example because I don't want to have interrupts I just want to measure the elapsed time. How should I measure it? Also I don't want just to see the time elapsed. I want to be able to use it in my code. for example at first "t=0", then one cycle later it will be "1 micro-seconds". I want to be able to use this time in my code.

While searching the forum I saw a post that said we can use eCAP to do this, but I don't know how. If it is possible this way, can you please explain it or refer me to an example?

Best,

Ata

  • Hi Ata,

    For measuring elapsed time within your code and using these times within your program, CPU timer and eCap are your two options.

    1) CPU Timer:

    You're correct, you will not be using interrupts for this method. If you're going off of the CPU Timer example from ControlSuite or C2000ware you can remove anything related to interrupts ("InitPieCtrl();", "InitPieVectTable();", etc.). You will initialize and configure your CPU Timer, I suggest making the period the largest possible (0xFFFFFFFF). Then Reset/Start the timer at the beginning of the code portion you want to measure, save the initial CPU timer value to a variable, then after the program portion save the CPU timer value to another variable. The difference of these values will be your elapsed time in clock cycles (You will have to convert if you want seconds/minutes). I'll provide an example below...

    void StartTimer(void)
    {
        cpuTime1 = 0;
    
        EALLOW;
        CpuTimer2Regs.TCR.bit.TRB = 1;        // Reset CPU timer to period value
        CpuTimer2Regs.TCR.bit.TSS = 0;        // Start or reset CPU timer 2
        cpuTime1 = CpuTimer2Regs.TIM.all;     // Get value of CPU Timer 2 before code
        EDIS;
    }
    
    void StopTimer(void)
    {
        EALLOW;
        cpuTime2 = CpuTimer2Regs.TIM.all;    // Get value of CPU Timer 2 after code
        cpuTime = cpuTime1 - cpuTime2;     // Calculate time using cpuTimer2
        CpuTimer2Regs.TCR.bit.TSS = 1;        // Start or reset CPU timer 2
        EDIS;
    }

    2) eCap:

    The eCap method works similar to measuring a pulse width using an oscilloscope. So for your case, it's like setting a GPIO pin high when the code portion starts and returning it to low after the code finishes, and then measuring the pulse width for the elapsed time. You would send the output pin you're setting high to an eCap input pin on your board to accomplish this. I believe the ecap_capture_pwm example in ControlSuite would be a good place to start if you want to use this method.

    Example below...

    GpioDataRegs.GPADAT.bit.GPIO1 = 1;    // Start timing, Ecap
    // Code to be timed here...
    GpioDataRegs.GPADAT.bit.GPIO1 = 0;   // Stop timing, Ecap
    ecap_time = ECap1Regs.CAP2;          // Calculate time using ECap2 (will be in clock cycles)

    You can save any of these registers to a variable, i suggest they be Uint32. Both of these methods will give you nearly the same timing.

    Hope this helps, let me know if you have any further questions.

    Best,

    Kevin

  • Hi Kevin,

    Thank you so much for your help.

    I modified the example code of "CpuTimer" just to measure the elapsed time, using your suggestions. Could you please see if this code is true?

    #include "DSP28x_Project.h"     // Device Headerfile and Examples Include File
    
    //
    // Function Prototype statements
    //
    
    
    void StartTimer(void);
    void StopTimer(void);
    double mytime.seconds; 
    
    //
    // Main
    //
    void main(void)
    {
    
        InitSysCtrl();
    
      
        DINT;
    
    
    
        IER = 0x0000;
        IFR = 0x0000;
    
    
        InitCpuTimers();   // For this example, only initialize the Cpu Timers
    
    
        StartTimer();
        //my code;
        StopTimer();
        mytime.seconds=cpuTime*(1/150000000);
        
    }
    
        void StartTimer(void)
        {
        Uint32 cpuTime1 = 0;
    
            EALLOW;
            CpuTimer2Regs.TCR.bit.TRB = 1;        // Reset CPU timer to period value
            CpuTimer2Regs.TCR.bit.TSS = 0;        // Start or reset CPU timer 2
            cpuTime1 = CpuTimer2Regs.TIM.all;     // Get value of CPU Timer 2 before code
            EDIS;
        }
    
        void StopTimer(void)
        {
            Uint32 cpuTime2 = 0;
            EALLOW;
            cpuTime2 = CpuTimer2Regs.TIM.all;    // Get value of CPU Timer 2 after code
            cpuTime = cpuTime2 - cpuTime1;     // Calculate time using cpuTimer2
            CpuTimer2Regs.TCR.bit.TSS = 1;        // Start or reset CPU timer 2
            EDIS;
        }
    

    Best,

    Ata

  • Ata,

    That looks pretty close. The only thing I think you need to do is make cpuTime1, cpuTime2, and cpuTime global variables.

    You can test your code just by running a for loop that counts up in place of "//my code".

    int i = 0;

    for(i=0; i < 90000; i++){}

    Hope it works for you,
    Kevin