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.

LAUNCHXL-CC2640R2: Setting up a GP Timer in Edge-Count-Up Mode to measure the time between two events

Part Number: LAUNCHXL-CC2640R2
Other Parts Discussed in Thread: CC2640

Hello,

My goal is to set up a Timer, which reacts to an edge on the CC2640's Timerpin (DIO23 for Timer0). It should return the counter value, on which the edge was registered, so that the time between two events can be calculated.

Therefore I studied several forum articles, but I was unable to find a fitting one. Also the official TI documentation only provides an example explaining the periodic mode, but gives no hints on the other modes. 

If anybody could provide an decent example or documentation I would be massively thankful.

Here you can see the code I am currently working on, which compiles without mistakes but does not return the counter value:

/* For usleep() */
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>

/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/Timer.h>
#include <ti/drivers/timer/GPTimerCC26XX.h>

#include <ti/drivers/pin/PINCC26XX.h>




/* Board Header file */
#include "Board.h"

/* Callback Function Prototyp */
void timerCallback(GPTimerCC26XX_Handle handle, GPTimerCC26XX_IntMask interruptMask);

/* Variablen */
GPTimerCC26XX_Handle hTimer;

uint32_t tVal_event = 0;


/* Callback Function */
void timerCallback(GPTimerCC26XX_Handle handle, GPTimerCC26XX_IntMask interruptMask) {
    GPIO_write(Board_GPIO_LED1, Board_GPIO_LED_ON);
}


/*
 *  ======== mainThread ========
 */
void *mainThread(void *arg0)
{


        GPTimerCC26XX_Params params;
        GPTimerCC26XX_Params_init(&params);
        params.width = GPT_CONFIG_16BIT;
        params.mode = GPT_MODE_EDGE_TIME_UP;
        params.debugStallMode = GPTimerCC26XX_DEBUG_STALL_OFF;

        hTimer = GPTimerCC26XX_open(Board_GPTIMER0A, &params);

        GPTimerCC26XX_registerInterrupt(hTimer, timerCallback, GPT_INT_CAPTURE);

        GPTimerCC26XX_setLoadValue(hTimer, 0xFFFF);

        GPTimerCC26XX_setCaptureEdge(hTimer, GPTimerCC26XX_BOTH_EDGES);

        GPTimerCC26XX_start(hTimer);


        while(1){

            tVal_event = GPTimerCC26XX_getValue(hTimer);

        }
}

Kind Regards

  • Hello Till,
    I am a bit unsure why you are having tVal_event = GPTimerCC26XX_getValue(hTimer) in the while loop. It should be going into the timerCallback() function when the timer interrupt happens. Also try storing it in an array to see the various times when the interrupt happens. Here is a thread that you can refer to
    e2e.ti.com/.../509701
    Regards,
    Prashanth
  • Hello Prashanth,

    Thanks for your quick reply.

    I added the things you mentioned, but I am still not able to set up the timer correctly. The link you provided does not really help me out, because I understand what the getValue() function does, but the previous settings are unclear to me.

    Here is my updated code:

    /* For usleep() */
    #include <unistd.h>
    #include <stdint.h>
    #include <stddef.h>
    
    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/Timer.h>
    #include <ti/drivers/timer/GPTimerCC26XX.h>
    
    #include <ti/drivers/pin/PINCC26XX.h>
    
    
    /* Board Header file */
    #include "Board.h"
    
    /* Callback Function Prototyp */
    void timerCallback(GPTimerCC26XX_Handle handle, GPTimerCC26XX_IntMask interruptMask);
    
    /* Globale Variablen */
    GPTimerCC26XX_Handle hTimer;
    
    uint32_t tVal_event[100] = {0};
    
    
    
    /* Callback Function */
    void timerCallback(GPTimerCC26XX_Handle handle, GPTimerCC26XX_IntMask interruptMask) {
        static int j;
        tVal_event[j] = GPTimerCC26XX_getValue(hTimer);
      
        if (j < 99){
            j++;
        }else{
           j = 0;
        }
    }
    
    
    /*
     *  ======== mainThread ========
     */
    void *mainThread(void *arg0)
    {
    
            GPTimerCC26XX_Params params;
            GPTimerCC26XX_Params_init(&params);
            params.width = GPT_CONFIG_16BIT;
            params.mode = GPT_MODE_EDGE_TIME_UP;
            params.debugStallMode = GPTimerCC26XX_DEBUG_STALL_OFF;
    
            hTimer = GPTimerCC26XX_open(Board_GPTIMER0A, &params);
    
            GPTimerCC26XX_registerInterrupt(hTimer, timerCallback, GPT_INT_CAPTURE);
    
            GPTimerCC26XX_setLoadValue(hTimer, 0xFFFFFF);
    
            GPTimerCC26XX_setCaptureEdge(hTimer, GPTimerCC26XX_BOTH_EDGES);
    
            GPTimerCC26XX_start(hTimer);
    
          
    
            while(1){
                if (tVal_event[0] > 0){
                    GPIO_write(Board_GPIO_LED1, Board_GPIO_LED_ON);
                }
            }
    }
    

    The interrupt should be triggered, when I press the on-board Button (DIO13), which I wired to the timer pin (DIO23). Then the interrupt should be called and the current tick count should be saved in my array tVal_event. 

    Maybe some more general questions clear up my understanding:

    1. Does the Timer start at 0 when I call the GPTimer..start(..) function and counts up to the value set in GPTimer..setLoadValue(..) periodically?

    2. Do I have to initialize the timerpin somehow or is it automatically set to DIO23 for Timer0?

    3. Are my settings described in the mainThread correct or is something missing, false or unnecessary for my objective?

    Thank you for your help.

    Kind regards