/* ---------------------------------------------------------------------------
 * testpower.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, Mar 25, 2008, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

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

#include "FreeRTOS.h"
#include "task.h"
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */ 
#include "testpower.h"
#include "power.h"
#include "SerOut.h"

/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Global variable definitions
 * ---------------------------------------------------------------------------
 */
#define lowerlimit5V   4800             /* Lower VCC Limit                  */
#define higherlimit5V  5200             /* Higher VCC Limit                 */

#define lowerlimit24V   23000           /* Lower 24V Limit                  */
#define higherlimit24V  25000           /* Higher 24V Limit                 */


/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */
static UINT8 PWRTest;        /* Debugable Variable, to show Bus Test Result */
/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */

BOOLEAN testpowerStart(void)
{
    BOOLEAN PowerTestResult;            /* BOOLEAN Variable for Test Result */
    
	PowerTestResult = DoVoltageTest();  /* Do Power Test                    */
	return PowerTestResult;             /* Return Test Result               */
}
BOOLEAN DoVoltageTest (void)
{
    /* Local Variable Declaration */
	BOOLEAN VTestResult = TRUE;
    static UINT16 vMeasure5V;           /* Debugable                        */
    static UINT16 vMeasure24V;
    
    PWRTest = 0x11;
    
    /* Test for 5 V VCC Power Supply                                        */
    vMeasure5V = powerVccVoltage();     /* Read current VCC Voltage         */
    /* Message out Measure Value and Test Limits                            */
    sendString (SerOutPort, TRUE, testMessage, "\t5V Powertest", NewLine, 
    		"\tLimits (lower/higher):  ");
    sendString (SerOutPort, FALSE, testMessage, 
            ItoDStr (lowerlimit5V), devider, Dummy);
    sendString (SerOutPort, FALSE, testMessage, 
            Dummy, Dummy, ItoDStr (higherlimit5V));
    sendString (SerOutPort, TRUE, testMessage, 
            "\tDetected ", ItoDStr(vMeasure5V), " mV");
    
    if ((vMeasure5V < lowerlimit5V) || (vMeasure5V > higherlimit5V))
    {                                   /* If measured Value out of Limits  */
        VTestResult = FALSE;            /* Set Test Result to FALSE         */
        PWRTest &= ~(0x01);
    }
    
    vTaskDelay (500);                   /* Wait for 500 ms                  */
    
    /* Test for 24 V Power Supply                                           */
    vMeasure24V = powerV24Voltage();    /* Read current power supply Voltage*/
    /* Message out Measure Value and Test Limits                            */
    sendString (SerOutPort, TRUE, testMessage, "\n\r\t24V Powertest", NewLine, 
    		"\tLimits (lower/higher): ");
    sendString (SerOutPort, FALSE, testMessage,
            ItoDStr (lowerlimit24V), devider, Dummy);
    sendString (SerOutPort, FALSE, testMessage,
            Dummy, Dummy, ItoDStr (higherlimit24V));
    sendString (SerOutPort, TRUE, testMessage,
            "\tDetected ", ItoDStr(vMeasure24V), " mV");
    
    if ((vMeasure24V < lowerlimit24V) || (vMeasure24V > higherlimit24V))
    {                                   /* If measured Value out of Limits  */
        VTestResult = FALSE;            /* Set Test Result to FALSE         */
        PWRTest &= ~(0x10);
    }
    
    return VTestResult;                 /* Return Test Result               */
}


