Skip to content

Instantly share code, notes, and snippets.

@fstanley
Created August 21, 2015 16:38
Show Gist options
  • Save fstanley/c13b04d8ca7510320135 to your computer and use it in GitHub Desktop.
Save fstanley/c13b04d8ca7510320135 to your computer and use it in GitHub Desktop.
Unit test application to verify the RTC on a NetBurner MOD5441X module
#include <predef.h>
#include <constants.h>
#include <stdio.h>
#include <ucos.h>
#include <init.h>
#include <time.h>
#include <mcf5441x_rtc.h>
#include <HiResTimer.h>
extern "C" {
void UserMain(void * pd);
}
const char * AppName="mcf5441x-rtc";
/**
* struct tm
*
* Member Type Meaning Range
* tm_sec int seconds after the minute 0-60*
* tm_min int minutes after the hour 0-59
* tm_hour int hours since midnight 0-23
* tm_mday int day of the month 1-31
* tm_mon int months since January 0-11
* tm_year int years since 1900
* tm_wday int days since Sunday 0-6
* tm_yday int days since January 1 0-365
* tm_isdst int Daylight Saving Time flag >0 = DST, 0=not DST, <0 = Unknown
*
* tm_sec is generally 0-59. The extra range is to accommodate
* for leap seconds in certain systems.
*
*/
/*
* Creating a comparison operator for struct tm.
* Only comparing values that are used in this test
*/
bool operator==(const struct tm &a, const struct tm &b) {
if (a.tm_sec != b.tm_sec) return false;
if (a.tm_min != b.tm_min) return false;
if (a.tm_hour != b.tm_hour) return false;
if (a.tm_mon != b.tm_mon) return false;
if (a.tm_mday != b.tm_mday) return false;
if (a.tm_year != b.tm_year) return false;
if (a.tm_wday != b.tm_wday) return false;
if (a.tm_yday != b.tm_yday) return false;
return true;
}
/*
* PrintTimeStruct is a helper function for printing struct tm
*/
const char *dow_str[] =
{
"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT", "\0"
};
void PrintTimeStruct(struct tm &bt)
{
iprintf("%d/%d/%d %02d:%02d:%02d %s %d", bt.tm_mon + 1, bt.tm_mday, bt.tm_year + 1900,
bt.tm_hour, bt.tm_min, bt.tm_sec, dow_str[bt.tm_wday], bt.tm_yday);
}
/*
* testRTC
* Provide a time(setValue) and expected time (expectedValue) after delay
*
* Uses format "MM/DD/YY HH/MM/SS day_of_week, day_of_year"
* * day_of_week starts on sunday [0-6]
* * day_of_year starts on Jan 1 [1-366]
*/
bool testRTC(const char* setValue, const char* expectedValue, int delay) {
iprintf("TESTING: %s ( + %d seconds) :",setValue,delay);
struct tm set_tm, incremented_tm, expected_tm;
strptime(setValue,"%D %T %w %j",&set_tm);
strptime(expectedValue,"%D %T %w %j",&expected_tm);
OSTimeDly(1); // Ensure we set the RTC as close to tick as possible
MCF541X_RTCSetTime(set_tm);
OSTimeDly(delay * TICKS_PER_SECOND);
MCF541X_RTCGetTime(incremented_tm);
if (incremented_tm == expected_tm) {
iprintf(" PASSED\r\n",setValue);
return true;
} else {
iprintf(" FAILED\r\nGOT : ",setValue);
PrintTimeStruct(incremented_tm);
iprintf("\r\nEXPECT: ");
PrintTimeStruct(expected_tm);
iprintf("\r\n");
return false;
}
}
void UserMain(void * pd) {
initWithWeb();
iprintf("This application is designed to test the RTC on an MCF5441X chip\r\n");
// Testing RTC by setting it and then reading it back after a short delay
// Minute increment
testRTC("05/24/13 16:45:58 5 144","05/24/13 16:46:02 5 144",4);
// Hour increment
testRTC("12/12/14 09:59:58 5 346","12/12/14 10:00:02 5 346",4);
testRTC("12/10/14 12:59:58 3 344","12/10/14 13:00:02 3 344",4);
// Day increment
testRTC("02/15/17 23:59:57 3 46","02/16/17 00:00:01 4 47",4);
testRTC("04/24/16 23:59:57 6 115","04/25/16 00:00:01 0 116",4);
// Month increment
testRTC("01/31/12 23:59:59 2 31","02/01/12 00:00:03 3 32",4);
testRTC("11/30/12 23:59:59 5 335","12/01/12 00:00:03 6 336",4);
// Year increment
testRTC("12/31/16 23:59:58 6 366","01/01/17 00:00:02 0 1",4);
testRTC("12/31/85 23:59:58 6 365","01/01/86 00:00:02 0 1",4);
// Leap day
testRTC("02/28/12 23:59:59 2 59","02/29/12 00:00:03 3 60",4);
testRTC("02/29/12 23:59:59 3 60","03/01/12 00:00:03 4 61",4);
testRTC("02/28/13 23:59:59 4 59","03/01/13 00:00:03 5 60",4);
testRTC("02/28/14 23:59:59 5 59","03/01/14 00:00:03 6 60",4);
testRTC("02/28/15 23:59:59 6 59","03/01/15 00:00:03 0 60",4);
while (1) {
OSTimeDly(TICKS_PER_SECOND);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment