
/**
 \verbatim
 ******************************************************************************
 * Copyright (c) Infineon Technologies AG, all rights reserved.
 ******************************************************************************
 * This source code and any compilation or derivative thereof is the sole
 * property of Infineon Technologies AG and is provided pursuant to a
 * Software License  Agreement. This code is the proprietary information of
 * Infineon Technologies AG and is confidential in nature. Its use and
 * dissemination by any other party other than Infineon Technologies AG
 * is strictly limited by the confidential information provisions of the
 * Agreement referenced above.
 ******************************************************************************
 \endverbatim
 ******************************************************************************
 * \file        .c
 * \ingroup     XXX - Xxxxxx
 * \brief       XXXX.
 * \details
 * \author
 * \date        \$LastChangedDate:: 2009-05-14 #$
 * \version     \$Revision: 1383 $
 * \attention   -
 ******************************************************************************
 */

/**
 ******************************************************************************
 * \defgroup    XXX - Xxxxx
 * \brief       XXXXXX
 * \details     -
 * \attention   -
 ******************************************************************************
 */


 /*
 ******************************************************************************
 * Standard include files
 ******************************************************************************
 */
#include "Ifx_Types.h"
#include "IfxCpu_Intrinsics.h"
#include "IfxScuWdt.h"
#include "Ifx_reg.h"
#include <Port/Io/IfxPort_Io.h>
#include "IfxGtm.h"
#include "IfxCpu_Irq.h"
#include "IfxGtm_Tom.h"
#include "IfxGtm_Tom_PwmHl.h"

#include <Asclin/Asc/IfxAsclin_Asc.h>

#include "string.h"

#define TRIBOARD_TC275C
#include "cint.h"
#include "interrupts.h"

/*
 ******************************************************************************
 * Project include files
 ******************************************************************************
 */

/* MISRA RULE 19.15 VIOLATION:  The following exclusion was added because of the
 *                              inclusion of multiple headers, which are the
 *                              same. This could result in redefinitions of
 *                              types causing out of boundary scenario's.
 *                              The violation may only be turned off for header
 *                              files of external units.
 *                              Since all header files are protected against
 *                              multiple inclusion, no redefinitions can occur.
 */
/*lint -save -e537 */
/*lint -restore */

/*
 ******************************************************************************
 * Types
 ******************************************************************************
 */


/*
 ******************************************************************************
 * Macro defines
 ******************************************************************************
 */
#define TOS_CPU0		0u
#define GPT120_T6SPRN	10
#define GPT120_T3SPRN	20

#define ASC_TX_BUFFER_SIZE 64
#define ASC_RX_BUFFER_SIZE 64

//Priorities
#define IFX_INTPRIO_ASCLIN0_TX 1
#define IFX_INTPRIO_ASCLIN0_RX 2
#define IFX_INTPRIO_ASCLIN0_ER 3
#define IFX_INTPRIO_ASCLIN3_TX 4
#define IFX_INTPRIO_ASCLIN3_RX 5
#define IFX_INTPRIO_ASCLIN3_ER 6
/*
 ******************************************************************************
 * Variable declarations
 ******************************************************************************
 */
uint8 ASC1_data;
uint8 ASC1data[0x100];
uint8 ASC1dataIndx;

uint8 ASC_data;
uint8 ASCdata[0x100];
uint8 ASCdataIndx;

uint32 RateDivider = 0 ;
uint32 FlashCount = 0 ;

static uint8 ascTxBuffer0[ASC_TX_BUFFER_SIZE + sizeof(Ifx_Fifo) + 8];
static uint8 ascRxBuffer0[ASC_RX_BUFFER_SIZE + sizeof(Ifx_Fifo) + 8];

static uint8 ascTxBuffer3[ASC_TX_BUFFER_SIZE + sizeof(Ifx_Fifo) + 8];
static uint8 ascRxBuffer3[ASC_RX_BUFFER_SIZE + sizeof(Ifx_Fifo) + 8];

uint8 data2[100];
uint8 commsTimeout = 0;
/*
 ******************************************************************************
 * Routine prototypes
 ******************************************************************************
 */
void GPT120_Init(void);
char sendCmd(char*,IfxAsclin_Asc,char*,char*);
char setEcho(IfxAsclin_Asc, boolean);

/*
 ******************************************************************************
 * Routine implementation
 ******************************************************************************
 */
//Create the asc structure
static IfxAsclin_Asc asc0;
IfxAsclin_Asc_Config ascConfig0;
static IfxAsclin_Asc asc3;
IfxAsclin_Asc_Config ascConfig3;


/**
 ******************************************************************************
 * \brief       GPRS ASCLIN Example
 * \details     An example using the Asclin ILLDs to control a GPRS module.
 * \param[in]   -
 * \param[out]  -
 * \return      Void
 * \pre         None.
 * \post        -
 * \attention   -
 ******************************************************************************
 */

/* ASC Interrupts */

void asclin0TxISR(int i)
{
	IfxAsclin_Asc_isrTransmit(&asc0);
}

void asclin0RxISR(int i)
{
	IfxAsclin_Asc_isrReceive(&asc0);

}

void asclin0ErISR(int i)
{
	IfxAsclin_Asc_isrError(&asc0);
}

void asclin3TxISR(int i)
{
	IfxAsclin_Asc_isrTransmit(&asc3);
}

void asclin3RxISR(int i)
{
	IfxAsclin_Asc_isrReceive(&asc3);

}

void asclin3ErISR(int i)
{
	IfxAsclin_Asc_isrError(&asc3);
}


Ifx_SizeT count0;

int core0_main (void)
{

    /* UART SETUP 0  */

    /* create module config  */
    IfxAsclin_Asc_initModuleConfig(&ascConfig0, &MODULE_ASCLIN0);

    /* Adjust sample point to suit HW (default values do not work)  */
    ascConfig0.baudrate.oversampling = 15;
    ascConfig0.bitTiming.medianFilter = 3;
    ascConfig0.bitTiming.samplePointPosition = 9;

    /* set the desired baudrate  */
    ascConfig0.baudrate.prescaler = 1;
    ascConfig0.baudrate.baudrate = 38400; /* FDR values will be calculated in initModule */

    /* ISR priorities and interrupt target */
    ascConfig0.interrupt.txPriority = IFX_INTPRIO_ASCLIN0_TX;
    ascConfig0.interrupt.rxPriority = IFX_INTPRIO_ASCLIN0_RX;
    ascConfig0.interrupt.erPriority = IFX_INTPRIO_ASCLIN0_ER;
    ascConfig0.interrupt.typeOfService = (IfxSrc_Tos)IfxCpu_getCoreId();

    /* FIFO configuration */
    ascConfig0.txBuffer = &ascTxBuffer0;
    ascConfig0.txBufferSize = ASC_TX_BUFFER_SIZE;
    ascConfig0.rxBuffer = &ascRxBuffer0;
    ascConfig0.rxBufferSize = ASC_RX_BUFFER_SIZE;

    /* pin configuration */
    const IfxAsclin_Asc_Pins pins0 = {
        NULL_PTR,                           IfxPort_InputMode_pullUp,    /* CTS pin not used */
        &IfxAsclin0_RXB_P15_3_IN,   IfxPort_InputMode_pullUp,   		 /* Rx pin */
        NULL_PTR,                          IfxPort_OutputMode_pushPull,  /* RTS pin not used */
        &IfxAsclin0_TX_P15_2_OUT,   IfxPort_OutputMode_pushPull, 		 /* Tx pin */
        IfxPort_PadDriver_cmosAutomotiveSpeed1
    };
    ascConfig0.pins = &pins0;

    /* initialize module */
    /* IfxAsclin_Asc asc defined globally */
    IfxAsclin_Asc_initModule(&asc0, &ascConfig0);

    /*Install Interrupts */
    _install_int_handler(IFX_INTPRIO_ASCLIN0_TX, asclin0TxISR, 0);
    _install_int_handler(IFX_INTPRIO_ASCLIN0_RX, asclin0RxISR, 0);
    _install_int_handler(IFX_INTPRIO_ASCLIN0_ER, asclin0ErISR, 0);

    /* UART SETUP 3 */

    /* create module config */
    IfxAsclin_Asc_initModuleConfig(&ascConfig3, &MODULE_ASCLIN3);

    /* Adjust sample point to suit HW (default values do not work) */
    ascConfig3.baudrate.oversampling = 15;
    ascConfig3.bitTiming.medianFilter = 3;
    ascConfig3.bitTiming.samplePointPosition = 9;

    /* set the desired baudrate */
    ascConfig3.baudrate.prescaler = 1;
    ascConfig3.baudrate.baudrate = 9600; /* FDR values will be calculated in initModule */

    /* ISR priorities and interrupt target */
    ascConfig3.interrupt.txPriority = IFX_INTPRIO_ASCLIN3_TX;
    ascConfig3.interrupt.rxPriority = IFX_INTPRIO_ASCLIN3_RX;
    ascConfig3.interrupt.erPriority = IFX_INTPRIO_ASCLIN3_ER;
    ascConfig3.interrupt.typeOfService = (IfxSrc_Tos)IfxCpu_getCoreId();

    /* FIFO configuration */
    ascConfig3.txBuffer = &ascTxBuffer3;
    ascConfig3.txBufferSize = ASC_TX_BUFFER_SIZE;
    ascConfig3.rxBuffer = &ascRxBuffer3;
    ascConfig3.rxBufferSize = ASC_RX_BUFFER_SIZE;

    /* pin configuration */
    const IfxAsclin_Asc_Pins pins3 = {
        NULL_PTR,                           IfxPort_InputMode_pullUp,   /* CTS pin not used */
        &IfxAsclin3_RXD_P32_2_IN,   IfxPort_InputMode_pullUp,   		/* Rx pin           */
        NULL_PTR,                          IfxPort_OutputMode_pushPull, /* RTS pin not used */
        &IfxAsclin3_TX_P15_7_OUT,   IfxPort_OutputMode_pushPull, 		/* Tx pin           */
        IfxPort_PadDriver_cmosAutomotiveSpeed1
    };
    ascConfig3.pins = &pins3;

    /* initialize module */
    /* IfxAsclin_Asc asc; defined globally */
    IfxAsclin_Asc_initModule(&asc3, &ascConfig3);

    /* Install Interrupts */
    InterruptInit();
    _install_int_handler(IFX_INTPRIO_ASCLIN3_TX, asclin3TxISR, 0);
    _install_int_handler(IFX_INTPRIO_ASCLIN3_RX, asclin3RxISR, 0);
    _install_int_handler(IFX_INTPRIO_ASCLIN3_ER, asclin3ErISR, 0);

    IfxCpu_enableInterrupts();

    /* MAIN LOOP */
    ASCdataIndx = 0x00;
  	ASC1dataIndx = 0x00u;

    Ifx_SizeT length = 3;

    count0 = 0x00;

   	while(1)
   	{

   	    /* how many bytes have been received? */
   	    count0 = IfxAsclin_Asc_getReadCount(&asc0);

   	    /* limit to our buffer size */
   	    count0 = count0 < 100 ? count0 : 100;

   	    count0 = 1;
   	    ASCdata[0] = 'A';

    	/* transfer received data into buffer */
        IfxAsclin_Asc_write(&asc3, ASCdata, &count0, TIME_INFINITE);

    }


    return (1);
}


/* T3 Interrupt Routine every 125us */
/* Modulation interrupt 			*/
IFX_INTERRUPT(ISR_PWM_modulation,0, GPT120_T3SPRN)
{


}


/* T6 Interrupt Routine 1ms   */
/* Note sequence interrupt    */
IFX_INTERRUPT(ISR_flashled,0, GPT120_T6SPRN)
{

	if(RateDivider%1000 == 0u)
	{

		commsTimeout++;

	}

	RateDivider++;

}

char data[100];
char recvBuffer[100];

char sendCmd(char *cmd, IfxAsclin_Asc asc,char *endStrOK, char *endStrER)
{
	memset(recvBuffer,'\0',100);

	/* Define a length of 1 */
	Ifx_SizeT length1 = 1;

	/* Define a carriage return */
	char carRet[1] = {0x0D};
	Ifx_SizeT count;

	/* Define buffers */
	/* Work out the length of the Command */
	Ifx_SizeT len;
	len = strlen(cmd);

	/* Write Command to GPRS followed by a carriage return. */
	IfxAsclin_Asc_write(&asc, cmd, &len, TIME_INFINITE);
	IfxAsclin_Asc_write(&asc, carRet, &length1, TIME_INFINITE);

	/* Keep looping until the expected response = the received response */
	commsTimeout = 0;
	while(commsTimeout < 5)
	{
		/* Get Number of bytes in buffer */
		count = IfxAsclin_Asc_getReadCount(&asc);
		/* If any bytes in buffer */
		if(count)
		{
			/* Read count number of bytes */
			IfxAsclin_Asc_read(&asc, data,&count,TIME_INFINITE);
			/* Set last char of the buffer to end char */
			data[count] = '\0';
			/* Add buffer to the end of the overall message */
			strcat(recvBuffer,data);
		}
		/* If the endString is in the Buffer then return OK */
		if(strstr(recvBuffer,endStrOK))
		{
			return 0;
		}
		if (strstr(recvBuffer,endStrER))
		{
			return 2;
		}

	}
	return 1;
	/*Return Codes
	     * 0=OK
	     * 1=Timeout
	     * 2=Error Cmd received
	     */

}

char setEcho(IfxAsclin_Asc asc, boolean state)
{
	char result = 1;
	if(state)
	{
		result = sendCmd("ATE1",asc,"OK","ERROR");
	}
	else
	{
		result = sendCmd("ATE0",asc,"OK","ERROR");
	}
	return result;
}

void GPT120_Init(void)
{

    /* Set P33.10 as Out */
    P33_IOCR8.B.PC10 = (1u << 4u);

#ifdef _TEST_BEAT_

    /* Enable speaker pin */
    P33_IOCR0.B.PC0 = (1u << 4u);

#endif

    IfxScuWdt_clearCpuEndinit(IfxScuWdt_getCpuWatchdogPassword());

    while(GPT120_CLC.B.DISS == 1u)
    {

    	GPT120_CLC.B.DISR = 0u;
    }

    IfxScuWdt_setCpuEndinit(IfxScuWdt_getCpuWatchdogPassword());

    GPT120_T6.U = 10;
    RateDivider = 125;
    GPT120_CAPREL.U = 12500u;
    GPT120_T6CON.U = (3u << 11) | (1u << 15u) | (1u << 7u) ; /* T6 reload from CAPREL, T6 counts down */

    SRC_GPT120T6.U = (TOS_CPU0 << 11u) |(1u << 10u) | (GPT120_T6SPRN << 0u);

    GPT120_T6CON.B.T6R = 1u;

    /* Setup T3 for PWM update timebase */
    /* Fin = 3.2MHz */

    GPT120_T2.U = 400 ;       /* 0.000125 second overflow rate for T3 */
    GPT120_T2CON.U = 0x0027 ; /* T2 is reload register */
    GPT120_T3CON.U = 0x0000 ; /* 0.312 us per count    */
    GPT120_T3CON.B.T3UD = 1 ;

    SRC_GPT120T3.U = (TOS_CPU0 << 11u) |(1u << 10u) | (GPT120_T3SPRN << 0u);

    GPT120_T3CON.B.T3R = 1u ;

}




