Soft RTC
I am using SimpleIDE 0-9-28 on a Windows 7 PC. This is a program that jazzed provided a few years back, which ran as expected on the previous versions of SimpleIDE, 0-8-5 being the last version; now, the program hangs at the settime() function. I have tried some different settings but I can not get it to work. Are there some specials settings that have to be turned on in this version of SimpleIDE?
Ray
Ray
/*
raytime.c
*/
#include "simpletools.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/rtc.h>
#include <propeller.h>
#define BUFFERLEN 10
void printtime(void);
int getdst(void);
int prompttime(char *prompt);
void settime(void);
int main()
{
// Add startup code here.
_rtc_start_timekeeping_cog();
pause(300);
printf("Soft RTC.\n");
printtime();
settime();
for(;;)
{
pause(1000);
printtime();
}
return 0;
}
void printtime(void)
{
time_t now;
now = time(NULL);
printf("%s", asctime(localtime(&now)));
}
int getdst(void)
{
int rc = 0;
printf("Use daylight savings time [y/n] ?");
fflush(stdout);
rc = (getchar() == 'y') ? 1 : 0;
getchar();
return rc;
}
int prompttime(char *prompt)
{
int rc = 0;
char *endp;
char buffer[BUFFERLEN];
do {
printf("Enter %s: ",prompt);
fflush(stdout);
fgets(buffer,BUFFERLEN,stdin);
fflush(stdin);
rc = strtol(buffer, &endp, 10);
if(endp == buffer)
{
if('\n' == *endp)
{
rc = 0;
break;
}
printf("Invalid entry \"%c....\" Please enter a number.\n", *endp);
}
} while(endp == buffer);
return rc;
}
void settime(void)
{
struct timeval tv;
struct tm t;
t.tm_isdst = getdst();
t.tm_year = prompttime("Year")-1900;
t.tm_mon = prompttime("Month")-1;
t.tm_mday = prompttime("Day of the month");
t.tm_hour = prompttime("Hour");
t.tm_min = prompttime("Minute");
t.tm_sec = prompttime("Second");
tv.tv_sec = mktime(&t);
settimeofday(&tv, 0);
printf("Set time all done.\n");
}

Comments
Very odd. I ran the example on linux and it worked perfectly, but on windows there are issues.
For Windows:
1. In Simple Terminal click to check "Echo On"
2. Click options and click to uncheck "Enter is NL"
3. Make sure "Swap Receive CR/NL" is not checked.
I'll file a bug to set the terminal appropriately for window.
I have no idea at this time why we need to check "Echo On" in windows.
The same thing happens if I remove simpletools and run with propeller-load -r -t
Andy, we have a decision to make.
Ray
Read this: http://propgcc.googlecode.com/hg/doc/Library.html#RTC
The timekeeping cog is written in ASM, so there are no concerns about moving from LMM/CMM to XMM.
This function starts another cog to monitor the elapsed time. If your main code calls a time related function at least once every 50 seconds or so (before the 32 bit hardware CNT overflows) then it isn't strictly necessary... but if you miss a CNT window the time will get screwed up. With another COG keeping track of the time there will never be a problem, As Steve mentioned the RTC COG code is in assembly, and works in any memory mode including XMM.