/* ---------------------------------------------------------------------------
 * testeeprom.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 11, 2008, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

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


/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "Task.h"
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */ 
#include "testeeprom.h"
#include "eeprom.h"
#include "SerOut.h"
#include "dio.h"
/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Global variable definitions
 * ---------------------------------------------------------------------------
 */
/* Use same Arrays as Flash Test to minimize Memory usage */
// \MARK ARRAYS NOT NECESSARY ON OLIMEXBOARD
#if ((PINSET_TESTER == 1) || (PINSET_TESTER == 2)) 
UINT8 eepromResponseString[3000];                                         
UINT8 eepromTestString[3000];
#endif

/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */
UINT16 adres_var[2];

/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */


BOOLEAN testeepromStart(void)
{
    BOOLEAN eepromTestResult;
    
    eepromTestResult = doeepromTest();
    
    return (eepromTestResult);
}

BOOLEAN doeepromTest (void)
{
    BOOLEAN testFailed= FALSE;
    
// \MARK TEST NOT NECESSARY ON OLIMEXBOARD
#if ((PINSET_TESTER == 1) || (PINSET_TESTER == 2)) 	    
	/*Local Variable Declaration */
	UINT8 testByte;
	UINT8 testString[] = "Dit is een oefening";
    UINT16 index;
	UINT8 oneByte = 0;
	
	sendString (COM2, TRUE, testMessage, 
	        "\tWrite Byte 0x34 to Adress 256", Dummy, Dummy);
	eepromWrite (256, 0x34);           /* Write one Byte to EEPROM         */
	eepromRead (256, &testByte);       /* Read written Byte from EEPROM    */
	testFailed = (testByte != 0x34);    /* compare read and written Bytes   */
    if (testFailed == FALSE)
        {
    	sendString (COM2, FALSE, testMessage, 
    	        "\t DONE", Dummy, Dummy);
    	dioWrite (0, 1, TRUE);
        }
    else
        {
    	sendString (COM2, FALSE, testMessage,
    	        "\t FAILURE", Dummy, Dummy);
        dioWrite (0, 0, TRUE);
        }
	
	if (!testFailed)                    /* If no Test fail up to here       */
	    {
		sendString (COM2, TRUE, testMessage,
		        "\tWrite 10 Bytes to Adress 511", Dummy, Dummy);
		eepromWriteBuffer( 511, testString, 10);
	                                    /* Write Teststring to EEPROM with  */
	                                    /* width of 10 Bytes                */
	    eepromReadBuffer( 511, eepromResponseString, 10);
	                                    /* Read written String from EEPROM  */
	       
	    for (index = 0; index < 10; index++)
	        {                           /* Compare both Strings             */
	        if (eepromResponseString[index] != testString[index])
	        	{                       /* If Strings are not equal         */
	        	testFailed = TRUE;      /* Set Test Result to FALSE         */      
	        	}
	        }
	    if (testFailed == FALSE)
	        {
	    	sendString (COM2, FALSE, testMessage, 
	    	        "\t DONE", Dummy, Dummy);
	    	dioWrite (0, 3, TRUE);
	        }
	    else
	        {
	    	sendString (COM2, FALSE, testMessage, 
	    	        "\t FAILURE", Dummy, Dummy);
	        dioWrite (0, 2, TRUE);
	        }
	    }
	    
	if (!testFailed)                    /* If no Test fail up to here       */
	    {
		sendString (COM2, TRUE, testMessage,
		        "\tWrite 3000 Byte to Adress 500", Dummy, Dummy);
		int loopcnt;
	    for (loopcnt=0; loopcnt < 3000; loopcnt++)  
	        {                           /* Fill flashTestString            */
	        eepromTestString[loopcnt] = oneByte++;
	        }
	    eepromWriteBuffer( 500, eepromTestString, 3000);
	                                    /* Write flashTestString to EEPROM */
	                                    /* with width of 3000 Bytes         */
	    eepromReadBuffer( 500, eepromResponseString, 3000); 
                                        /* Read written String from EEPROM  */

	    for (index = 0; index < 3000; index++)
	        {                           /* Compare both Strings             */
	        if (eepromResponseString[index] != eepromTestString[index]) 
	            {                       /* If Strings are not equal         */
	            testFailed = TRUE;      /* Set Test Result to FALSE         */
	            }
	        }
	    if (testFailed == FALSE)
	        {
	    	sendString (COM2, FALSE, testMessage, "\t DONE", Dummy, Dummy);
	    	dioWrite (0, 5, TRUE);
	        }
	    else
	        {
	    	sendString (COM2, FALSE, testMessage, "\t FAILURE", Dummy, Dummy);
	        dioWrite (0, 4, TRUE);
	        }
	    }
#endif
    return (!testFailed);               /* Return Test Result               */
}
