/************  Copyright 2004-2009 FICOSA CORPORATIVE ELECTRONICS  ************
| Language:       |  MISRA C
| Controller:      |  dsPIC33
| Requirements:
|-------------|------------------------------------------------------------
| Project:          |   082_TCU01_F1_TCU_EMU_RSA
|------------------------------------------------------------------------------
| HISTORY OF MODIFICATIONS
|   Date  -  Coder -                   Description
| 07/02/10   AC    Creation of the file.
|------------------------------------------------------------------------------
| FILE DESCRIPTION:
| Configuration source file of the transport protocol (ISO15765_2) layer.
| This source file must contain the transport protocol callback routine
| TPDynamicParametersInitCallback to initialize the dynamic configuration
| parameters for each TP instance defined in TP_CFG.h.
******************************************************************************/

/* -------------------------------- Includes -------------------------------- */
#include "Std_Types.h"
#include "DiagnosticR/Comp_ISO_15765_2/TP.h"
#include "DiagnosticR/ProjectCfg.h"
#include "TP_CFG.h"
#include "DiagnosticR/FicOsek/FicOsekCom.h"
#include "DiagnosticR/UDS\Iso15765_layer3/Iso15765_3_CFG.h"
#include "DiagnosticR/Comp_ISO_15765_3/Iso15765_3.h"

/* -------------------------------- Defines --------------------------------- */

/* Size of the buffer that must store the largest frame for the 		*/
/* MPDT TX and RX instances												*/
/* The size of the buffer must be declared using the macro 				*/
/* SIZE_ALLOC_BUFFER to assure that no overflows will happen during     */
/* transmission or reception of frames larger than single frames        */
#define TP_MPDT_TX_LEN             	(SIZE_ALLOC_BUFFER(30))
#define TP_MPDT_RX_LEN             	(SIZE_ALLOC_BUFFER(30))

/* For diagnostics TP instance I have the length of the buffers already */
/* defined in the Iso15765_3 layer                                      */

/* ------------------------------- Data Types ------------------------------- */

/* ---------------------------- Global Variables ---------------------------- */

/* Definition of the buffer to allocate functional frames. As functional      */
/* frames are not needed in any of my TP instances I declare it as a dummy    */
/* buffer. The size of the functional buffer must be always of 7 bytes        */
/* because this is the maximum length that could be send in a SF */
//static UI_8 diag_rx_fun_dummy_buf[7] = { 0,0,0,0,0,0,0 };

/* Definition of the transmission and reception TP buffers for MPDT */
//static UI_8 tp_mpdt_tx_buf[TP_MPDT_TX_LEN];
//static UI_8 tp_mpdt_rx_buf[TP_MPDT_RX_LEN];

/* For diagnostics TP instance I have the buffers already defined in the */
/* Iso15765_3 layer                                                      */

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

/* -------------------------------- Routines -------------------------------- */

/*****************************************************************************
| Portability: General
|----------------------------------------------------------------------------
| Routine description:
|  * It is responsability of the user to declare this routine.
|  * This routine is a callback routine that will be called each time that the
|    InicialitzaTPTask is executed. This InicialitzaTPTask must be called at
|    the initializations in main and must be called once for each instance
|    defined in the TP_CFG.h.
|  * Routine to initializate dynamically the parameters of each defined
|    transport protocol instance. The way to configure each transport protocol
|    instance is using the structure t_tp_init which must be given as a pointer
|    to TPInitData routine.
|---------------------------------------------------------------------------
| Parameters description:
/---------------------------------------------------------------------------*/
void TPDynamicParametersInitCallback(UI_8 tp_hdl)
{
    /* Declare the initialization ISO15765_2 structure as a local variable */
    /* in order to not use space in the stack */
    t_tp_init tp_init = {0};	/* MISRA wants all variables to be initialized. */

    /* Switch for each instance of ISO15765_2 declared (TP_NUM_INSTANCES) */
    switch(tp_hdl) {
    /* Initialize the user configuration parameters of the transport protocol instance used for diagnostics */
    case ISO15765_2_REPROGONCAN_HANDLER:
        /* Maximum size allowed for the diagnostics RX buffer. */
        /* The buffer must be reserved having into account the MACRO "SIZE_ALLOC_BUFFER" to avoid overflows */
        tp_init.max_frm_size = (UI_16)(MAX(TP_DIAG_RX_LEN,TP_DIAG_TX_LEN));
        /* Flag to configure fixed or variable length in the CAN frames TX */
        /* Diagnostics frm_size_fixed = TRUE: Defined in the renault document 36-02-031--A_Gb.pdf, page 9, chapter 6 */
        tp_init.frm_size_fixed = TRUE;
		/* Flag to configure if TP RX frames are sensitive to have a fixed length. TRUE indicates that we will ignore TP RX frames */
		/* that have less than 8 bytes */
		tp_init.rx_padding_sensitive = TRUE;
        /* Special value 0x50 is requiered in the padding value for diagnostics TX (only cares if frame size is configured as fixed) */
        /* Defined in the renault document CRS_TCU_NT65612_2009_80_v1.1DRAFT.pdf page 6 */
        tp_init.tx_padding_value = 0xAA;
        /* Maximum timeout in ms between Consecutive frames before aborting the reception due to time out */
        /* Diagnostics CR timer: Defined in the renault document 36-02-031--A_Gb.pdf, pages 7 and 17 */
        tp_init.cr_timer = (UI_16)150;
        /* Time in ms between the first frame and the firs flow control or between the last consecutive frame of a block */
        /* and the flow control before aborting the transmission due to timeout */
        /* Diagnostics BS timer: Defined in the renault document 36-02-031--A_Gb.pdf, pages 7 and 17 */
        tp_init.bs_timer = (UI_16)150;
        /* Time in ms between consecutive frames of the transmitter to allow ourself to process the received frames */
        /* without losing anyone. If the block size if different from 1, this time must be minimum the cycle frequency, */
        /* otherwise if this is 1 the stmin could be 0 */
        /* For diagnostics this requirement is defined in the renault document 36-02-031--A_Gb.pdf, page 7 as 0 */
        tp_init.stmin_timer = (UI_16)20;
        /* Number of consecutive frames that the diagnostics TP will send without waiting for a flow control before continuing. */
        /* If the block size is set to 0 means that only one flow control is needed after receiving the first */
        tp_init.block_size = (UI_8)0;
		/* Number of FC w can handle before rising directly an error during the transmission when a FC wait is received */
		tp_init.max_fc_wait = (UI_8) 0;
        /* Initialize the buffer pointer where the diagnostics TX positive response physical information is set */
        tp_init.tx_phy_buffer = diag_tx_buf;
        /* Initialize the buffer pointer where the diagnostics RX physical request information is set */
        tp_init.rx_phy_buffer = diag_phy_rx_buf;
        /* Initialize the buffer pointer where the diagnostics RX functional request information is set */
        tp_init.rx_fun_buffer = diag_fun_rx_buf;
        /* Set the OSEKCOM diagnostics signal to get the RX physical frame */
        tp_init.sig_np_rx_phy = SIG_DIAGNOSTICREQSWTR; //SIG_DIAGNOSTICREQPRNDL
        /* Set the OSEKCOM signal to request the transmission of the diagnostics TX physical frame */
        tp_init.sig_np_tx_phy = SIG_DIAGNOSTICRESPSWTR; //SIG_DIAGNOSTICRESPSWTR
        /* Initialize the diagnostics RX functional notification routine to NULL because we must not receive functional frames */
        //tp_init.np_get_rx_fun_notif = NULL;
        /* Initialize the signal of the diagnostics RX functional frame to DUMMY signal becuase we must not receive functional frames */
        tp_init.sig_np_rx_fun = SIG_DIAGNOSTICFUNCADDRREQ;
        /* Call the ISO15765_2 configuration routine to initialize the diagnostics instance with the configured parameters */
        TPInitData(tp_hdl, &tp_init);
        break;

    default:
        break;
    }
}
