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.

RTOS/CC2640R2F: get microseconds with RTC

Part Number: CC2640R2F

Tool/software: TI-RTOS

Hi, I'm using a CC2640R2 With the example of Project Zero from simplelink_cc2640r2_sdk_1_50_00_58.

I require to get microseconds timestamp for an integral that I'm doing, then I created this function based on examples it's on milliseconds just to test:

void get_ms(unsigned long * timestamp){

	Timestamp_getFreq(&freq);
	xdc_runtime_Types_Timestamp64 t_64;

	uint32_t t_32 = Timestamp_get32();
	Timestamp_get64(&t_64);

	timestamp[0] = (unsigned long) ((t_32*1000)/ freq.lo); //Unsigned long = u32
    log_i("Freq: %d %d 32= %d 64 = %d %d. ", freq.hi, freq.lo, t_32, t_64.hi, t_64.lo);

}

But when I printed I noticed that the frequency was wrong: Freq: 0 65536

#000003 [ 0.013 ] INFO: (delays.c:26) Freq: 0 65536 t_32= 874 t_64 = 0 874.

#000004 [ 0.013 ] INFO: (EM7180.c:149) Timestamp ms = 13

I found here that this is because it's set to use the RTC, and they suggest to change some lines if he is ok when the clock stalls Since I'm using the Bluetooth example I don't want to change anything in the clock because I don't know if it will stop working. 

So I searched for how to get the timestamp from the RTC, but I can only find how to get seconds: here but this is more like the calendar. In the same post the suggest to read the 

http://www.ti.com/lit/ug/spruex3u/spruex3u.pdf but in the section 5.5 Timestamp Module, it takes me again to what I did before :/.

Thansk for your help

  • Hi Jose,

    You could use the "Seconds" module if you want a complete TI-RTOS module that returns seconds + nanoseconds:

    http://dev.ti.com/tirex/content/simplelink_cc2640r2_sdk_2_20_00_49/docs/tirtos/sysbios/docs/cdoc/index.html

    Another way to do it would be to simple use the actual RTC value (this is basically how most implementations does it under the hood). Take the "Seconds" module for example:

    UInt32 Seconds_getTime(Seconds_Time *ts)
    {
        volatile UInt32 seconds;
        volatile UInt32 subseconds;
        UInt64 temp;
        UInt64 nsecs;
    
        /* read current RTC count */
        temp = AONRTCCurrent64BitValueGet();
        seconds = (UInt32) (temp >> 32);
        subseconds = (UInt32) (temp & 0xFFFFFFFF);
    
        /* adjust seconds count with refSeconds and setSeconds */
        seconds = (seconds - Seconds_module->refSeconds) +
            Seconds_module->setSeconds;
    
        ts->secs = seconds;
    
        /*
         *  Throw away the lower 16 bits of the subseconds since this
         *  used for temparature correction and does not accurately
         *  reflect the time.
         */
        subseconds = subseconds >> 16;
    
        nsecs = (1000000000 * (UInt64)subseconds) / 65536;
        ts->nsecs = (UInt32)nsecs;
    
        return (0);
    }

    As you see, you could use the AONRTCCurrent64BitValueGet() driverLib function to get the current RTC sec + subsec value and then use this to calculate your microseconds value.

  • Thanks for pointing my to that info!

    I'll test it.

    Thanks