/* ---------------------------------------------------------------------------
 * testrtc.c                                           (c) 2008 Micro-key bv
 * ---------------------------------------------------------------------------
 * Micro-key bv
 * Industrieweg 28, 9804 TG Noordhorn
 * Postbus 92, 9800 AB Zuidhorn
 * The Netherlands
 * Tel: +31 594 503020
 * Fax: +31 594 505825
 * Email: support@microkey.nl  
 * Web: www.microkey.nl
 * ---------------------------------------------------------------------------
 * Description: 
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, Feb 26, 2008, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * System include files
 * ---------------------------------------------------------------------------
 */
#include "lpc23xx.h"
#include "types.h"

/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "task.h"
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */ 
#include "testrtc.h"
#include "rtc.h"
#include "SerOut.h"
#include "ledfunctions.h"
#include "dio.h"
#include "testdio.h"
#include "taskfunctions.h"
/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */
/* Clock defines to set Clock to a known Value */
#define dsec     5
#define dmin    10
#define dhour   15
#define dday    20
#define ddow     2
#define dmon     3
#define dyear 3250
#define ddoy   350
/* ---------------------------------------------------------------------------
 * Global variable definitions
 * ---------------------------------------------------------------------------
 */
/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */

t_rtc rtcWriteValue =                   /* Variable of RTC-used Type to set */
{	dsec,                               /* RTC to a known Value             */
	dmin,
	dhour,
	dday,
	ddow,
	dmon,
	dday,
	dyear,
	ddoy};

t_rtc rtcReadValue;                     /* Variable of RTC-used Type to     */
                                        /* read RTC Values                  */
t_rtc *rtcPtr;                          /* Pointer of RTC-Used Type         */
/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */
BOOLEAN testrtcStart(void)
{
    BOOLEAN testrtcResult = TRUE;       /* BOOLEAN Variable for Test Result */
    
    DoRTCWrite();                       /* Write known Value to RTC         */
    dioWrite (0, 1, TRUE);
    dioWrite (0, 3, TRUE);
    testrtcResult = DoRTCRead();        /* Read RTC Values                  */

    return (testrtcResult);             /* Return Test Result               */
}


void DoRTCWrite (void)
{
    rtcPtr = &rtcWriteValue;            /* Set Pointer                      */
	sendString (SerOutPort, TRUE, testMessage, 
	        "\tWrite Custom CLOCK Data to RTC Register",
			Dummy, NewLine);            /* Message Output                   */
    rtcWrite (rtcPtr);                  /* Write Values to RTC              */
}
	

BOOLEAN DoRTCRead (void)
{	
	BOOLEAN rtcReadResult; 
    rtcPtr = &rtcReadValue;             /* Set Pointer                      */
     
	sendString (SerOutPort, TRUE, testMessage,
	        "\tAttempt to read RTC Values: ",
			Dummy, Dummy);              /* Message Output                   */
	
	 rtcRead (rtcPtr);                  /* Read RTC Values                  */
     
     /* Message out all read Values compared with written Values            */
	 sendString (SerOutPort, TRUE, testMessage, "\tRTC Seconds:     ",
			 ItoDStr (rtcReadValue.sec), Dummy);
     sendString (SerOutPort, FALSE, testMessage, 
             "\t(Should be: ", ItoDStr (dsec), ")");
     vTaskDelay (200);
     sendString (SerOutPort, TRUE, testMessage, "\tRTC Minutes:     ",
    		 ItoDStr (rtcReadValue.min), Dummy);
     sendString (SerOutPort, FALSE, testMessage, 
             "\t(Should be: ", ItoDStr (dmin), ")");
     vTaskDelay (200);
     sendString (SerOutPort, TRUE, testMessage, "\tRTC Hours:       ",
    		 ItoDStr (rtcReadValue.hour), Dummy);
     sendString (SerOutPort, FALSE, testMessage, 
             "\t(Should be: ", ItoDStr (dhour), ")");
     vTaskDelay (200);
     sendString (SerOutPort, TRUE, testMessage, "\tRTC Day:         ",
    		 ItoDStr (rtcReadValue.day), Dummy);
     sendString (SerOutPort, FALSE, testMessage, 
             "\t(Should be: ", ItoDStr (dday), ")");
     vTaskDelay (200);
     sendString (SerOutPort, TRUE, testMessage, "\tRTC Day of Week: ",
    		 ItoDStr (rtcReadValue.dow), Dummy);
     sendString (SerOutPort, FALSE, testMessage, 
             "\t(Should be: ", ItoDStr (ddow), ")");
     vTaskDelay (200);
     sendString (SerOutPort, TRUE, testMessage, "\tRTC Month:       ",
    		 ItoDStr (rtcReadValue.mon), Dummy);
     sendString (SerOutPort, FALSE, testMessage, 
             "\t(Should be: ", ItoDStr (dmon), ")");
     vTaskDelay (200);
     sendString (SerOutPort, TRUE, testMessage, "\tRTC Year:        ",
    		 ItoDStr (rtcReadValue.year), Dummy);
     sendString (SerOutPort, FALSE, testMessage, 
             "\t(Should be: ", ItoDStr (dyear), ")");
     vTaskDelay (200);
     sendString (SerOutPort, TRUE, testMessage, "\tRTC Day of Year: ",
    		 ItoDStr (rtcReadValue.doy), Dummy);
     sendString (SerOutPort, FALSE, testMessage, 
             "\t(Should be: ", ItoDStr (ddoy), ")");

     if ((dsec == rtcReadValue.sec)   && /* Compare read Values with        */
         (dmin == rtcReadValue.min)   && /* written Values                  */
         (dhour == rtcReadValue.hour) &&
         (dday == rtcReadValue.day)   &&
         (ddow == rtcReadValue.dow)   &&
         (dmon == rtcReadValue.mon)   &&
         (dyear == rtcReadValue.year) &&
         (ddoy == rtcReadValue.doy))
     {                                  /* If all Values equals             */

         rtcRead (rtcPtr);              /* Read RTC Values again            */
    	 if (dsec != rtcReadValue.sec)  /* If RTC runs (seconds-compare)    */
    	 {
    	     rtcReadResult = TRUE;      /* Set Test Result to TRUE          */
    	 }
    	 else                           /* If Run Test fails                */
    	 {
    		 rtcReadResult = FALSE;     /* Set Test Result to FALSE         */
    	 }
     }
     else                               /* If Read Test Fails               */
     {
    	 rtcReadResult = FALSE;         /* Set Test Result to FALSE         */
     }
     
     return rtcReadResult;              /* Return Test Result               */
}
