#include #include #include #include #include "inc/hw_sysctl.h" #include "driverlib/rom.h" #include "driverlib/sysctl.h" #include "grlib/grlib.h" #include "drivers/cfal96x64x16.h" #include "grlib/context.c" #include "grlib/string.c" #include "utils/ustdlib.h" #include "inc/hw_hibernate.h" tContext g_sContext; tRectangle sRect; char buff[20]; void ClrScreen(void); static const time_t g_psDaysToMonth[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; void ClrScreen() { sRect.i16XMin = 160; sRect.i16YMin = 132; sRect.i16XMax = 312; sRect.i16YMax = 232; GrContextForegroundSet(&g_sContext, ClrRed); GrRectFill(&g_sContext, &sRect); GrFlush(&g_sContext); } void ulocaltime(time_t timer, struct tm *tm) { time_t temp, months; // // Extract the number of seconds, converting time to the number of minutes. // temp = timer / 60; tm->tm_sec = timer - (temp * 60); timer = temp; // // Extract the number of minutes, converting time to the number of hours. // temp = timer / 60; tm->tm_min = timer - (temp * 60); timer = temp; // // Extract the number of hours, converting time to the number of days. // temp = timer / 24; tm->tm_hour = timer - (temp * 24); timer = temp; // // Compute the day of the week. // tm->tm_wday = (timer + 6) % 7; // // Compute the number of leap years that have occurred since 1968, the // first leap year before 1970. For the beginning of a leap year, cut the // month loop below at March so that the leap day is classified as February // 29 followed by March 1, instead of March 1 followed by another March 1. // timer += 366 + 365; temp = timer / ((5 * 365) + 1); if((timer - (temp * ((5 * 365) + 1))) > (31 + 28)) { temp++; months = 12; } else { months = 2; } // // Extract the year. // tm->tm_year = ((timer - temp) / 365) + 68; timer -= ((tm->tm_year - 68) * 365) + temp; // // Extract the month. // for(temp = 0; temp < months; temp++) { if(g_psDaysToMonth[temp] > timer) { break; } } tm->tm_mon = temp - 1; // // Extract the day of the month. // tm->tm_mday = timer - g_psDaysToMonth[temp - 1] + 1; } static int ucmptime(struct tm *t1, struct tm *t2) { // // Compare each field in descending signficance to determine if // greater than, less than, or equal. // if(t1->tm_year > t2->tm_year) { return(1); } else if(t1->tm_year < t2->tm_year) { return(-1); } else if(t1->tm_mon > t2->tm_mon) { return(1); } else if(t1->tm_mon < t2->tm_mon) { return(-1); } else if(t1->tm_mday > t2->tm_mday) { return(1); } else if(t1->tm_mday < t2->tm_mday) { return(-1); } else if(t1->tm_hour > t2->tm_hour) { return(1); } else if(t1->tm_hour < t2->tm_hour) { return(-1); } else if(t1->tm_min > t2->tm_min) { return(1); } else if(t1->tm_min < t2->tm_min) { return(-1); } else if(t1->tm_sec > t2->tm_sec) { return(1); } else if(t1->tm_sec < t2->tm_sec) { return(-1); } else { // // Reaching this branch of the conditional means that all of the // fields are equal, and thus the two times are equal. // return(0); } } time_t umktime(struct tm *timeptr) { // struct tm sTimeGuess; unsigned long ulTimeGuess = 0x80000000; unsigned long ulAdjust = 0x40000000; int iSign; // // Seed the binary search with the first guess. // ulocaltime(ulTimeGuess, &sTimeGuess); iSign = ucmptime(timeptr, &sTimeGuess); // // While the time is not yet found, execute a binary search. // while(iSign && ulAdjust) { // // Adjust the time guess up or down depending on the result of the // last compare. // ulTimeGuess = ((iSign > 0) ? (ulTimeGuess + ulAdjust) : (ulTimeGuess - ulAdjust)); ulAdjust /= 2; // // Compare the new time guess against the time pointed at by the // function parameters. // ulocaltime(ulTimeGuess, &sTimeGuess); iSign = ucmptime(timeptr, &sTimeGuess); } // // If the above loop was exited with iSign == 0, that means that the // time in seconds was found, so return that value to the caller. // if(iSign == 0) { return(ulTimeGuess); } // // Otherwise the time could not be converted so return an error. // else { return((unsigned long)-1); } } int main(void) { uint32_t ui32Idx; tRectangle sRect; ROM_FPULazyStackingEnable(); // // Set the clocking to run from the PLL. // ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); // // Initialize the display driver. // CFAL96x64x16Init(); // // Initialize the graphics context. // GrContextInit(&g_sContext, &g_sCFAL96x64x16); GrContextFontSet(&g_sContext, g_psFontCm18i); GrFlush(&g_sContext); while(1) { struct tm my_date_time; time_t seconds_to_write; seconds_to_write=umktime(&my_date_time); if(seconds_to_write ==(uint32_t)(-1)) { } else HWREG(HIB_RTCLD) = seconds_to_write; ulocaltime(HWREG(HIB_RTCLD), &my_date_time); sprintf(buff,"%d",&my_date_time); sRect.i16XMin =0; sRect.i16YMin = 30; sRect.i16XMax = GrContextDpyWidthGet(&g_sContext) - 1; sRect.i16YMax = 56; GrContextForegroundSet(&g_sContext, ClrRed); GrRectFill(&g_sContext, &sRect); GrContextForegroundSet(&g_sContext, ClrYellow); GrStringDraw(&g_sContext,buff, -1, 1, 30, 0); SysCtlDelay(5000000); } }