/************  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 "Global.h"
#include "ProjectCFG.h"
#include "TP.h"
#include "TP_CFG.h"
#include "FicOsekCom.h"
#include "Iso15765_3_CFG.h"
#include "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 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;
    
    /* 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_DIAGONCAN_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)TP_DIAG_RX_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 = 0x50;
        /* 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)1000;
        /* 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)1000;
        /* 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)0;
        /* 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)1;
		/* Number of FC wait the TP can handle before rising directly an error during the transmission when a FC wait is received */
		tp_init.max_fc_wait = (UI_8) 0xFF;
        /* 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_rx_buf;
        /* Initialize the buffer pointer where the diagnostics RX functional request information is set */
        tp_init.rx_fun_buffer = dummy_buf;
        /* Initialize the pointer of the diagnostics OSEKCOM routine that notifies the reception of the physical frame */
        tp_init.np_get_rx_phy_notif = &ReadFlagRxSigDToolToTcu;
        /* Initialize the pointer of the diagnostics OSEKCOM routine that notifies the transmission of the physical frame */
        tp_init.np_get_tx_phy_notif = &ReadFlagTxSigTcuToDTool;
        /* Initialize the pointer of the diagnostics OSEKCOM routine that notifies the timeout error of the TX physical frame. */
        /* This OSEKCOM routine will be created when the TX notification error is requested and the timeout for this must be   */
        /* set to the AR/AS timeout. For diagnostics we have a timeout of 1000 ms for AR/AS configured in the CAN database. */
        tp_init.np_err_tx_phy_notif = &ReadFlagTxErrorSigTcuToDTool;
        /* Set the OSEKCOM diagnostics signal to get the RX physical frame */
        tp_init.sig_np_rx_phy = SIG_DTOOLTOTCU;
        /* Set the OSEKCOM signal to request the transmission of the diagnostics TX physical frame */
        tp_init.sig_np_tx_phy = SIG_TCUTODTOOL;
        /* 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 = SIGDUMMY;
        /* Call the ISO15765_2 configuration routine to initialize the diagnostics instance with the configured parameters */
        TPInitData(tp_hdl, &tp_init);
        break;
    /* Initialize the user configuration parameters of the transport protocol instance used for MPDT */
    case ISO15765_2_MPDT_HANDLER:
        /* Maximum size allowed for the MDPT buffer. */
        /* The buffer must be reserved having into account the MACRO "SIZE_ALLOC_BUFFER" to avoid overflows */
        tp_init.max_frm_size = TP_MPDT_RX_LEN; //ACF optimitzar rps=30, tps=20   DOC: CRS
        /* Flag to configure fixed or variable length in the CAN frames TX */
        /* MPDT frm_size_fixed = TRUE: Defined in the renault document CRS */
        tp_init.frm_size_fixed = TRUE;
        /* Special value 0xFF is requiered in the padding value of MPDT, defined in the renault document CRS */
        tp_init.tx_padding_value = 0xFF;
        /* Maximum timeout in ms between Consecutive frames before aborting the reception due to time out */
        /* MPDT CR timer: Defined in the renault document CRS, pages 7 and 17 */
        tp_init.cr_timer = (UI_16)500;
        /* 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 */
        /* MPDT BS timer: Defined in the renault document CRS, pages 7 and 17 */
        tp_init.bs_timer = (UI_16)500;
        /* 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 MDPT this requirement is defined in the renault document CRS */
        tp_init.stmin_timer = (UI_16)10;
        /* Number of consecutive frames that the MPDT 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;
        /* Initialize the buffer pointer where the MPDT TX positive response physical information is set */
        tp_init.tx_phy_buffer = tp_mpdt_tx_buf;
        /* Initialize the buffer pointer where the MPDT RX physical request information is set */
        tp_init.rx_phy_buffer = tp_mpdt_rx_buf;
        /* Initialize the buffer pointer where the MPDT RX functional request information is set */
        tp_init.rx_fun_buffer = dummy_buf;
        /* Initialize the pointer of the MPDT OSEKCOM routine that notifies the reception of the physical frame */
        tp_init.np_get_rx_phy_notif = &ReadFlagRxSigTcuRxMpdt;
        /* Initialize the pointer of the MPDT OSEKCOM routine that notifies the transmission of the physical frame */
        tp_init.np_get_tx_phy_notif = &ReadFlagTxSigTcuTxMpdt;
        /* Initialize the pointer of the MPDT OSEKCOM routine that notifies the timeout error of the TX physical frame */
        /* This OSEKCOM routine will be created when the TX notification error is requested and the timeout for this must be */
        /* set to the AR/AS timeout. For MPDT we have a timeout of 50 ms for AR/AS configured in the CAN database. */
        tp_init.np_err_tx_phy_notif = &ReadFlagTxErrorSigTcuTxMpdt;
        /* Set the MPDT OSEKCOM signal to get the RX physical frame */
        tp_init.sig_np_rx_phy = SIG_TCURXMPDT;
        /* Set the OSEKCOM signal to request the MPDT transmission of the TX physical frame */
        tp_init.sig_np_tx_phy = SIG_TCUTXMPDT;
        /* Initialize the MPDT 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 MPDT RX functional frame to DUMMY signal becuase we must not receive functional frames */
        tp_init.sig_np_rx_fun = SIGDUMMY;
        /* Call the ISO15765_2 configuration routine to initialize the MPDT instance with the configured parameters */
        TPInitData(tp_hdl, &tp_init);
        break;
    default:
        break;
    }
}
