/* ---------------------------------------------------------------------------
 * testuart2.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: 
 * Contains BUS System Test Functions
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, Mar 10, 2008, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

/* APPLICATION NOTE FOR BUS TEST!!!!
 * 
 * THIS TEST ONLY WILL WORK IF DEVICE IS RUNNING IN A SINGLE MODE. IF DEVICE
 * IS INITIALIZED WITH BUS PROTOCOL (ANYWAY IF MASTER OR SLAVE), THIS TEST 
 * WILL CAUSE THE HARDWARE TO CRASH
 */


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

#include "FreeRTOS.h"
#include "task.h"
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */ 
#include "testBUS.h"
#include "bus.h"
#include "uart2.h"
#include "uart3.h"
#include "SerOut.h"
#include "taskfunctions.h"
#include "ledfunctions.h"

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

/* ---------------------------------------------------------------------------
 * Global variable definitions
 * ---------------------------------------------------------------------------
 */
/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */
char BUS1Buffer[20] = " 'BUSTEST first' ";   /* first Teststring            */
char BUS2Buffer[20] = " 'BUSTEST second' ";  /* second Teststring           */

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

extern void onUart2TxFinished();
extern void onUart3TxFinished();


BOOLEAN testbusStart(void) 
{
    /*Local Variable Declaration */
    BOOLEAN BUSTestResult = FALSE;
    BOOLEAN BUSTest11 = FALSE;
    BOOLEAN BUSTest12 = FALSE; 
    BOOLEAN BUSTest21 = FALSE;
    BOOLEAN BUSTest22 = FALSE; 
    BOOLEAN receive;
    UINT8 buffer;
    
	
	BUSTest11 = DoBUS1Test();            /* Test Bus1                        */
	BUSTest12 = DoBUS2Test();            /* Test Bus2                        */


	sendString (SerOutPort, TRUE, noteMessage, /* Send Text notification    */
	        NewLine, "\tSWITCH BUS CONNECTOR AND PRESS ENTER", NewLine);

	do                                  /* do-while loop for String input   */
        {
        receive = serGet(SerOutPort, &buffer);
        } while (buffer != 13);         /*Read input until ENTER-Key        */
	
    BUSTest21 = DoBUS1Test();           /* Test Bus1                        */
    BUSTest22 = DoBUS2Test();           /* Test Bus2                        */
	
	if ((BUSTest11 == TRUE) && (BUSTest12 == TRUE)
	  &&(BUSTest21 == TRUE) && (BUSTest22 == TRUE))
	    {                               /* If both Bustests passed          */
		BUSTestResult = TRUE;           /* set Test Result to TRUE          */
	    }

	return BUSTestResult;               /* Return Test Result               */
}
    
BOOLEAN DoBUS1Test (void)
{
    /* Local Variable Declaration */
    BOOLEAN Uart2TestResult = TRUE;
	UINT16 B1received_int = 0;
    UINT16 B1send_int = 0;
    UINT8 B1receiveBuffer[50];

	
    sendString (SerOutPort, TRUE, testMessage,
            "\tWrite String ", BUS1Buffer, " on BUS1... "); 
    busWrite ( BUS1, sizeof (BUS1Buffer), (UINT8 *) BUS1Buffer);
                                        /* Send Teststring on Bus1          */
    vTaskDelay (500);                   /* Wait to InputRegister to be ready*/
    B1send_int = sizeof (BUS1Buffer);   /* calculate Length of Teststring   */  
    B1received_int = busRead (BUS2, (UINT8 *)B1receiveBuffer);
                                        /* Read Teststring on Bus2          */
    sendString (SerOutPort, TRUE, testMessage,
            "\tRead ", (char *) B1receiveBuffer, " on BUS2"); 
                                        /* Message out received String      */
    if (B1send_int != B1received_int)   /* If received String is a fault    */
    {
        Uart2TestResult = FALSE;        /* Set Bus1 TestResult to FALSE     */
    }
    return Uart2TestResult;             /* Return Test Result               */
}

BOOLEAN DoBUS2Test (void)
{
    /* See "DoBUS1Test" for comments */
	
    BOOLEAN Uart3TestResult = TRUE;
	UINT16 B2received_int = 0;
    UINT16 B2send_int = 0;
    UINT8 B2receiveBuffer[50];
	
    sendString (SerOutPort, TRUE, testMessage,
            "\tWrite String ", BUS2Buffer, " on BUS2... "); 
    busWrite ( BUS2, sizeof (BUS2Buffer), (UINT8 *) BUS2Buffer);
    vTaskDelay( 500 );
    B2send_int = sizeof (BUS2Buffer);   
    B2received_int = busRead (BUS1, (UINT8 *)B2receiveBuffer);
    sendString (SerOutPort, TRUE, testMessage,
            "\tRead ", (char *) B2receiveBuffer, " on BUS1"); 

    if (B2send_int != B2received_int)
        {
        Uart3TestResult = FALSE;
        }
    return Uart3TestResult;
}

BOOLEAN Uart2LoopBackTest (void)
{
    /* UART2 is in LoopBack by forcing it to receive its own Transmission   */
	FIO4CLR |= (1 << 1);                /* Enable Receiver                  */
	FIO4SET |= (1 << 0);                /* Enable Transmitter               */

	/* Local Variable Declaration */
	BOOLEAN Result = TRUE;              /* LoopBack Test Result             */
	char EBBuffer[10] = "placed";       /* Teststring                       */
    UINT16 EBsend_int = 0;
    UINT16 EBreceived_int = 0;
    UINT8 EBreceiveBuffer[10];
	
    uart2Write( (char *)EBBuffer, sizeof(EBBuffer));    /* Write Teststring */
	EBsend_int = sizeof (EBBuffer);     /* Calculate Length                 */
	vTaskDelay (500);                   /* Wait to InputRegister to be ready*/
	EBreceived_int = busRead (BUS2, (UINT8 *)EBreceiveBuffer);
	                                    /* Read/Delete received String      */
	EBreceived_int = busRead (BUS1, (UINT8 *)EBreceiveBuffer);
	                                    /* Read/Delete received String      */
	if (EBsend_int != EBreceived_int)   /* If received String is a fault    */
	    {
	    Result = FALSE;                 /* Set Result to FALSE              */
	    }
	
	onUart2TxFinished();                /* Switch back to receive Mode      */
	return Result;                      /* Return Result                    */
}

BOOLEAN Uart3LoopBackTest (void)
{
    /* UART3 is in LoopBack by forcing it to receive its own Transmission  */
	FIO4CLR |= (1 << 3);     /* Enable Receiver */
	FIO4SET |= (1 << 2);     /* Enable Transmitter */

	BOOLEAN Result = TRUE;
	
    char EBBuffer[10] = "placed";
    UINT16 EBsend_int = 0;
    UINT16 EBreceived_int = 0;
    UINT8 EBreceiveBuffer[10];
	
	uart3Write( (char *)EBBuffer, sizeof(EBBuffer));
	EBsend_int = sizeof (EBBuffer);
	vTaskDelay (500);
	EBreceived_int = busRead (BUS1, (UINT8 *)EBreceiveBuffer);
	EBreceived_int = busRead (BUS2, (UINT8 *)EBreceiveBuffer);
	if (EBsend_int != EBreceived_int)
	    {
	    Result = FALSE;
	    }             
	onUart3TxFinished();
	return Result;
}
