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.

CC2640R2F: What I wish I knew about controlling Date and Time

Part Number: CC2640R2F
Other Parts Discussed in Thread: SYSBIOS

I have this working now, but here is what I wish I knew on how to set the TIRTOS date and time.

1. Example code floating around uses the seconds module to get and set date and time.  So you add the .h files to the code messing with time like the following:

    #include <time.h>

    #include <ti/sysbios/hal/Seconds.h>

    What is missing from doc is you need to include the module from XDC to get a project to link.  I put it in \TOOLS\app_ble.cfg file (there are other posts on this):

    var Seconds = xdc.useModule('ti.sysbios.hal.Seconds');

2. The seconds_set() function is used to set the time.  It expects a Linux Epoc time as input.  This routine does not deal with time zone.  There is only a raw gmt value. 

    To get the local time you have to adjust the epoc time with the time zone adjustment before calling seconds_set();

3. I used windows + python to generate the EPOC time stamp.  mktime() -> returns GMT(0).  I hardcoded a structure with local time and called mktime to generate an epoc

    time.  It was gmt(0). The following is code to generates a EPOC time that can be passed to seconds_set() to store the time.

   localTm = time.localtime();

print(localTm.tm_gmtoff)
#-----------------------------------------------------------------------------------------------------
# TI rtos only keeps GMT. It will not make standard adjustments for timezone. So we make the
# adjustments here. And update the GMT time with the timezone adjustment
#----------------------------------------------------------------------------------------------------
epocTmInt = int(time.mktime(time.localtime())) + localTm.tm_gmtoff
#----------------------------------------------------------------------------------------------------
# Convert to a byte array for sending over BLE
#----------------------------------------------------------------------------------------------------
epocTmBytes = epocTmInt.to_bytes(4, byteorder='little')

4. Using local_time is for output only.  I initialized a structure and passed to mktime(), got an epoc time and passed it to seconds_set().  All executed fine, but ltm in, did not match ltm out.

  my_time = mktime(ltm);

  Seconds_set(my_time);

  t = Seconds_get();

 memset(ltm,0,sizeof(ltm));

 ltm = localtime(&t1);

  Pass in # years since 1970, what comes out seems to be # years since 1900.  The day did not match because I was crossing day boundaries.  This means you need to use a EPOC time to set time. 

5. I spent a lot of time with a hang when I called asctime().  It seems that this routine takes a lot of space in the stack.  I had to increase the stack size from 1000 to 2000 to get it to execute without hanging.

At this point we can mark this closed, but I wish this was all documented.