Shop OBEX P1 Docs P2 Docs Learn Events
Setting Date/Time in Propeller C programs — Parallax Forums

Setting Date/Time in Propeller C programs

iseriesiseries Posts: 1,495
edited 2015-02-04 03:58 in Learn with BlocklyProp
Since the Propeller doesn't have an operating system or real time clock how do I set the Data/Time so that when I write to the SD card the files show up with a more current date/time stamp. Also if I build a clock on one of the Cogs can I push the current date time to the time functions.

Mike

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2015-02-02 10:03
    PropGCC uses dosfs for the file driver. dosfs.c contains the following function for setting the date and time.
    void dfs_setDefaultFileDateTime(struct tm* tm)
    {
        DefaultFileTime = (tm->tm_hour << 11) | (tm->tm_min << 5) | (tm->tm_sec >> 1);
        DefaultFileDate = ((tm->tm_year - 80) << 9) | ((tm->tm_mon + 1) << 5) | tm->tm_mday;
    }
    
    So you need to declare a tm struct, and initialize the struct with the date and time before calling dfs_setDefaultFileDateTime. The code would look something like this.
    #include <time.h>
    
    int main()
    {
      struct tm ts;
    
      ts.tm_year = 115; // year - 1900
      ts.tm_mon = 2;
      ts.tm_mday = 2;
      ts.tm_hour = 11;
      ts.tm_min = 59;
      ts.tm_sec = 23;
      dfs_setDefaultFileDateTime(&ts);
    
      ...
    
    }
    
  • iseriesiseries Posts: 1,495
    edited 2015-02-02 11:50
    That's fine for creating the file but what about using the time function. How do I make that work.

    I want to be able to write time stamped entries to the SD card.

    Mike
  • Dave HeinDave Hein Posts: 6,347
    edited 2015-02-02 14:55
    I looked into the PropGCC libraries, and there is support for a RTC. PropGCC has a soft RTC that you can run in its own cog by calling _rtc_start_timekeeping_cog(). Or you can avoid using an extra cog by calling the time() function periodically. It has to be called at least every 54 seconds to avoid missing a CNT wrap. You can also use an external RTC by setting the function pointers _rtc_settime and _rtc_gettime to your own settime and gettime functions.

    Here's an example of using the soft RTC in its own cog.
    #include <stdio.h>
    #include <time.h>
    #include <sys/time.h>
    
    int main()
    {
      struct timeval tv;
      time_t currtime, lasttime;
      extern int (*_rtc_settime)(const struct timeval *tv);
    
      _rtc_start_timekeeping_cog();
    
      tv.tv_usec = 0;
      tv.tv_sec = 1422915748;
      settimeofday(&tv, NULL);
    
      while (1)
      {
        if (time(&currtime) != lasttime)
        {
          lasttime = currtime;
          printf("%s\n", asctime(localtime(&currtime)));
        }
      }
    }
    
  • iseriesiseries Posts: 1,495
    edited 2015-02-03 03:29
    Looks like that should work.

    Thank you

    I thought there was a simpler answer other than reverse engineering the code.

    But that only creates a show stopper. Adding the SD routine and the I/O routine along with the fwrite functions uses all the available memory and my program will no longer fit.

    All for NOT.

    Mike
  • RsadeikaRsadeika Posts: 3,837
    edited 2015-02-03 04:08
    But that only creates a show stopper. Adding the SD routine and the I/O routine along with the fwrite functions uses all the available memory and my program will no longer fit.

    All for NOT.
    I am not sure what your program constraints are, but you could try running your program in XMMC mode. I have a program, that I am using, for data logging, compiles in at ~60K, it does the job though. If you decide to use XMMC mode, you will not be able to run additional COGs, in this mode, just an advanced warning. Also make sure that you are using SimpleIDE RC2, to avoid any other problems.

    Ray
  • Dave HeinDave Hein Posts: 6,347
    edited 2015-02-03 04:56
    Or if you are building in LMM mode try changing to CMM. Also, the standard printf routines take a lot of memory. Try setting the simple_printf mode, or use the tiny library.
  • iseriesiseries Posts: 1,495
    edited 2015-02-04 03:58
    I am using CMM for my code.

    By the numbers:

    sd_mount = 10K
    fopen/close/write = 2K
    time/asctime/localtime = 3K
    fprintf = 10K

    That leave about 5K for my code which is using 7 cogs and tops out at 10K.

    Together I'm at 32k.

    spending 25K just to track status information is a little rich.

    Mike
Sign In or Register to comment.