
/**
 \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   -
 ******************************************************************************
 */

#pragma GCC optimize "-O0" /* Disable optimisation for debugging */

/**
 ******************************************************************************
 * \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


/*
 ******************************************************************************
 * 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;
Ifx_SizeT count0;

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

/*
 ******************************************************************************
 * Routine prototypes
 ******************************************************************************
 */

char sendCmd(char*,IfxAsclin_Asc,char*,char*);

/*
 ******************************************************************************
 * 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 */

IFX_INTERRUPT(asclin0TxISR, 0, IFX_INTPRIO_ASCLIN0_TX)
{
	IfxAsclin_Asc_isrTransmit(&asc0);
}

IFX_INTERRUPT(asclin0RxISR, 0, IFX_INTPRIO_ASCLIN0_RX)
{
	IfxAsclin_Asc_isrReceive(&asc0);
}

IFX_INTERRUPT(asclin0ErISR, 0, IFX_INTPRIO_ASCLIN0_ER)
{
	IfxAsclin_Asc_isrError(&asc0);
}

IFX_INTERRUPT(asclin3TxISR, 0, IFX_INTPRIO_ASCLIN3_TX)
{
	IfxAsclin_Asc_isrTransmit(&asc3);
}

IFX_INTERRUPT(asclin3RxISR, 0, IFX_INTPRIO_ASCLIN3_RX)
{
	IfxAsclin_Asc_isrReceive(&asc3);

}

IFX_INTERRUPT(asclin3ErISR, 0, IFX_INTPRIO_ASCLIN3_ER)
{
	IfxAsclin_Asc_isrError(&asc3);
}



/*** Main Program ***/

int core0_main (void)
{

    /* UART 0 SETUP */

    /* 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 */
    IfxCpu_Irq_installInterruptHandler(asclin0TxISR, IFX_INTPRIO_ASCLIN0_TX);
    IfxCpu_Irq_installInterruptHandler(asclin0RxISR, IFX_INTPRIO_ASCLIN0_RX);
    IfxCpu_Irq_installInterruptHandler(asclin0ErISR, IFX_INTPRIO_ASCLIN0_ER);

    /* UART 3 SETUP */

    /* 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 */
    IfxCpu_Irq_installInterruptHandler(asclin3TxISR, IFX_INTPRIO_ASCLIN3_TX);
    IfxCpu_Irq_installInterruptHandler(asclin3RxISR, IFX_INTPRIO_ASCLIN3_RX);
    IfxCpu_Irq_installInterruptHandler(asclin3ErISR, IFX_INTPRIO_ASCLIN3_ER);

    IfxCpu_enableInterrupts();

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

    count0 = 0x00u;

   	while(1)
   	{

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

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

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

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

    }


    return (1u);
}




