/*********************************************************************************************************************
 *  COPYRIGHT
 *  ------------------------------------------------------------------------------------------------------------------
 *  \verbatim
 *
 *                 TEXAS INSTRUMENTS INCORPORATED PROPRIETARY INFORMATION
 *
 *                 Property of Texas Instruments, Unauthorized reproduction and/or distribution
 *                 is strictly prohibited.  This product  is  protected  under  copyright  law
 *                 and  trade  secret law as an  unpublished work.
 *                 (C) Copyright Texas Instruments-2014.  All rights reserved.
 *
 *  \endverbatim
 *  ------------------------------------------------------------------------------------------------------------------
 * 
 *  ------------------------------------------------------------------------------------------------------------------
 *  FILE DESCRIPTION
 *  ------------------------------------------------------------------------------------------------------------------
 *         File:  Lin.h
 *    Component:  Tms570_AutosarLinDriver
 *       Module:  LinDriver
 *    Generator:  -
 *
 *  Description:  API to hardware abstraction of the LIN-driver             
 * -------------------------------------------------------------------------------------------------------------------
 * Author:  Prathap Srinivasan
 *--------------------------------------------------------------------------------------------------------------------
 * Revision History
 *--------------------------------------------------------------------------------------------------------------------
 * Version        Date         Author             Change ID        Description
 *--------------------------------------------------------------------------------------------------------------------
 * 02.10.00       24Jan2013   Prathap Srinivasan  0000000000000   - Initial Version adapted from TMS570LS1227
 * 02.10.01		  14Mar2013   Vishwanath Reddy	  SDOCM00099800   - Fixed issue with the receive data 		
 * 02.20.00		  23Aug2013   Vishwanath Reddy	  SDOCM00103357   - Version info updated.
 *                                                                  Prototypes added for implementing Safety API's. 
 *                                                                  ISR prototypes moved under system constants.
 *                                                                  ISR prototypes will only be included when ISR is
 *                                                                  of CAT1 or void type.
 *                                                                  Modified definitions of all DET macros.
 * 02.20.02		  14Jul2014   Vishwanath Reddy	  SDOCM00112032   - Version info updated.
 *                                                                  Added macros for reporting Bit Error,  
 *                                                                  Overrun Error, Checksum Error,No Response Error,
 *                                                                  Frame Error,Physical Bus Error, Parity Error, 
 *                                                                  Sync Field Error to DEM.
 *                                                                  Design tags added.
 * 02.20.03		  08Aug2014   Vishwanath Reddy	  SDOCM00112419   - Version info updated.
 * 02.21.00		  30Sep2014   Vishwanath Reddy	  SDOCM00113130   - Version info updated. Additional DEM Errors are now
 *                                                                  referenced from Tresos.
 * 02.21.01		  05Nov2014   Vishwanath Reddy	  SDOCM00113739   - Version info updated.
 * 02.21.02		  29Apr2016   Vishwanath Reddy	  SDOCM00122618   - Version info updated.XDM file updated to confirm
 *                                                                  to AS4.0.3 specifications. 
 * 02.30.00       01Dec2016   Sudhakar Singh         None         - Update version info 
 * 05.30.00       15Mar2017   Sudhakar Singh      SDOCM00122834    Version supports TMS570LSxx Family of Devices.
 ********************************************************************************************************************/
#ifndef LIN_H
#define LIN_H

/*********************************************************************************************************************
 * INCLUDES
 ********************************************************************************************************************/
#include "ComStack_Types.h"
#include "Compiler.h"
#include "Platform_Types.h"
#include "Std_Types.h"
#include "EcuM_Cbk.h"
#include "Lin_GeneralTypes.h"
#include "Lin_Cfg.h"

/*********************************************************************************************************************
 *  VERSION DEFINES
 ********************************************************************************************************************/

#define LIN_SW_MAJOR_VERSION               (5U)
#define LIN_SW_MINOR_VERSION               (30U)
#define LIN_SW_PATCH_VERSION               (0U)

#define LIN_AR_RELEASE_MAJOR_VERSION     (4U)
#define LIN_AR_RELEASE_MINOR_VERSION     (0U)
#define LIN_AR_RELEASE_REVISION_VERSION  (3U)

/*********************************************************************************************************************
 *  GLOBAL CONSTANT MACROS
 ********************************************************************************************************************/

#define LIN_VENDOR_ID ((uint16)44U)
#define LIN_MODULE_ID ((uint16)82U)

/* Service ID */
#define LIN_INIT_ID                               ((uint8)0x00U)
#define LIN_GETVERSIONINFO_ID                     ((uint8)0x01U)
#define LIN_SENDFRAME_ID                          ((uint8)0x04U)
#define LIN_GOTOSLEEP_ID                          ((uint8)0x06U)
#define LIN_WAKEUP_ID                             ((uint8)0x07U)
#define LIN_GETSTATUS_ID                          ((uint8)0x08U)
#define LIN_GOTOSLEEPINTERNAL_ID                  ((uint8)0x09U)
#define LIN_CHECKWAKEUP_ID                        ((uint8)0x0AU)
/* Additional Service IDs not mentioned in AUTOSAR specification */
#define LIN_INTERRUPT_ID                          ((uint8)0x90U)

/* Values for LIN_CONFIG_VARIANT */
#define LIN_CFGVAR_PRECOMPILE                     0x01U
#define LIN_CFGVAR_LINKTIMEPOSTBUILD              0x02U

/* Error codes */

/* Development error codes from AUTOSAR specification */
/* SourceId : LIN_SourceId_014 */
/* DesignId : LIN_DesignId_004 */
/* Requirements : AR_LIN_SR49, AR_LIN_SR50, AR_LIN_SR51, AR_LIN_SR52, AR_LIN_SR53, AR_LIN_SR54, 
                  AR_LIN_SR55, AR_LIN_SR56 */
#ifndef LIN_E_UNINIT
#define LIN_E_UNINIT                              ((uint8)0x00U)
#endif
#ifndef LIN_E_CHANNEL_UNINIT
#define LIN_E_CHANNEL_UNINIT                      ((uint8)0x01U)
#endif
#ifndef LIN_E_INVALID_CHANNEL
#define LIN_E_INVALID_CHANNEL                     ((uint8)0x02U)
#endif
#ifndef LIN_E_INVALID_POINTER
#define LIN_E_INVALID_POINTER                     ((uint8)0x03U)
#endif
#ifndef LIN_E_STATE_TRANSITION
#define LIN_E_STATE_TRANSITION                    ((uint8)0x04U)
#endif
#ifndef LIN_E_PARAM_POINTER
#define LIN_E_PARAM_POINTER                       ((uint8)0x05U)
#endif

/* Development error codes from AUTOSAR specification extensions */
#ifndef LIN_E_CONFIG
#define LIN_E_CONFIG                              ((uint8)0x60U)     /* invalid configuration */
#endif
/*********************************************************************************************************************
 *  GLOBAL FUNCTION MACROS
 ********************************************************************************************************************/
/*********************************************************************************************************************
 *  GLOBAL DATA TYPES AND STRUCTURES
 ********************************************************************************************************************/

typedef P2VAR(uint8, TYPEDEF, LIN_APPL_VAR)   Lin_u8PtrType;

typedef VAR(uint8, TYPEDEF)                   Lin_StateType;

#define Lin_ChannelDataBufferSize             0x08U

#define  LIN_ISR_VOID  0U
#define  LIN_ISR_CAT1  1U
#define  LIN_ISR_CAT2  2U

/* This Type is used to Lin Module Initialization
 * Note: It is done here since LIN_UNINIT cannot be added to
 * Channel Status type.*/
typedef enum
{
  LIN_INIT = 0xAAU,
  LIN_UNINIT = 0x55U
}Lin_ModuleInitStatusType;

typedef struct Lin_ChannleConfigTypeTag
{
  VAR(uint16, TYPEDEF)                        Lin_ChannelId;
  VAR(uint32, TYPEDEF)                        Lin_BaudRateReg;
  VAR(uint32, TYPEDEF)                        Lin_MaxBaudSelReg;
  VAR(uint32, TYPEDEF)                        Lin_Compare;  
  VAR(uint16, TYPEDEF)                        Lin_WakeupSupport;
  VAR(EcuM_WakeupSourceType, TYPEDEF)         Lin_EcuMWakeupSource;
  CONSTP2VAR(void, TYPEDEF, LIN_VAR_REGS)     Lin_HardwareBaseAdress;
}Lin_ChannelConfigType;

/* ROM pointer to config struct */
typedef P2CONST(Lin_ChannelConfigType, TYPEDEF, LIN_PBCFG)   Lin_ChannelConfigPtrType;

typedef struct Lin_ChannelDataTypeTag
{
  VAR(Lin_ChannelConfigPtrType, TYPEDEF)      Lin_ChannelConfigPtr;
  VAR(Lin_FrameResponseType, TYPEDEF)         Lin_Drc;
  VAR(Lin_StateType, TYPEDEF)                 Lin_State;
  VAR(Lin_StatusType, TYPEDEF)                Lin_Status;
  VAR(Lin_ModuleInitStatusType, TYPEDEF)      Lin_ModuleInit_Status;
  VAR(uint8, TYPEDEF)                         Lin_DataBuffer[Lin_ChannelDataBufferSize];
}Lin_ChannelDataType;

/* SourceId : LIN_SourceId_015 */
/* DesignId : LIN_DesignId_003 */
/* Requirements : AR_LIN_SR72, AR_LIN_SR73 */
typedef struct Lin_ConfigTypeTag
{
  VAR(Lin_ChannelConfigPtrType, TYPEDEF)      Lin_ChannelConfigPtr;
}Lin_ConfigType;

#if(STD_ON == LIN_SAFETI_API)
/* LIN Config registers */
typedef struct Lin_LinConfigRegTag
{
	uint32              Lin_ReadBack_SCIGCR0;                     /* Global Control Register 0            */
	uint32              Lin_ReadBack_SCIGCR1;                     /* Global Control Register 1            */
	uint32              Lin_ReadBack_SCISETINT;                   /* Interrupt Enable Register            */
	uint32              Lin_ReadBack_BRSR;                        /* Baud Rate Selection Register         */
	uint32              Lin_ReadBack_SCIPIO0;                     /* Pin Control Register 0               */
	uint32              Lin_ReadBack_LINCOMP;               		 /* LIN Compare Register                 */
	uint32              Lin_ReadBack_LINMASK;                     /* LIN Mask Register                    */
	uint32              Lin_ReadBack_MBRSR;                      /* Maximum Baud Rate Selection Reg      */
} Lin_LinConfigReg;
extern Lin_LinConfigReg Lin_LinConfig;
#endif


/*********************************************************************************************************************
 *  GLOBAL DATA PROTOTYPES
 ********************************************************************************************************************/

/*********************************************************************************************************************
 *  GLOBAL FUNCTION PROTOTYPES
 ********************************************************************************************************************/

/**********************************************************************************************************************
 *  Lin_Init
 *********************************************************************************************************************/
/*! \brief      LIN Driver Module Initialization
 *  \param[in]  Config: Pointer to a selected configuration structure
 *  \param[in]  -
 *  \return     -
 *  \return     -
 *  \pre        -
 *  \context    Called by upper layer.
 *  \note       This service function has to be called before any other LIN driver function.
 *  \note       The service ID of this function is LIN_INIT_ID
 *  \sws req    
 *********************************************************************************************************************/

FUNC(void, LIN_CODE)           Lin_Init(P2CONST(Lin_ConfigType, AUTOMATIC, LIN_PBCFG) Config);

/**********************************************************************************************************************
 *  Lin_SendFrame
 *********************************************************************************************************************/
/*! \brief      The function Lin_SendFrame generates the header part of a LIN frame on the addressed LIN channel, and
                the response if Master Response type is selected.
 *  \param[in]  Channel: LIN channel to be addressed
 *  \param[in]  PduInfoPtr: Pointer to PDU containing the PID, Checksum model, Response type, Dl and SDU data pointer
 *  \return     E_OK: send command has been accepted
 *  \return     E_NOT_OK: send command has not been accepted, development or production error occurred
 *  \pre        The LIN driver shall have been initialized before this service is called.
 *              The LIN channel shall be in the LIN_CH_OPERATIONAL state.
 *  \context    Called by upper layer.
 *  \note       The service ID of this function is LIN_SENDFRAME_ID
 *  \sws req    
 *********************************************************************************************************************/
FUNC(Std_ReturnType, LIN_CODE) Lin_SendFrame(VAR(uint8, AUTOMATIC) Channel, \
                                                        P2VAR(Lin_PduType, AUTOMATIC, LIN_APPL_VAR) PduInfoPtr);

/**********************************************************************************************************************
 *  Lin_GoToSleep
 *********************************************************************************************************************/
/*! \brief      The function Lin_GoToSleep transmits a go-tosleep-command on the addressed LIN channel.
 *  \param[in]  Channel: LIN channel to be addressed
 *  \param[in]  -
 *  \return     E_OK: Sleep command has been accepted
 *  \return     E_NOT_OK: Sleep command has not been accepted, development or production error occurred
 *  \pre        The LIN driver shall have been initialized before this service is called.
 *  \context    Called by upper layer.
 *  \note       If supported by HW the LIN hardware unit maybe set to reduced power operation mode.
 *  \note       The service ID of this function is LIN_GOTOSLEEP_ID
 *  \sws req    
 *********************************************************************************************************************/
FUNC(Std_ReturnType, LIN_CODE) Lin_GoToSleep(VAR(uint8, AUTOMATIC) Channel);

/**********************************************************************************************************************
 *  Lin_GoToSleepInternal
 *********************************************************************************************************************/
/*! \brief      Same function as Lin_GoToSleep but without sending a go-to-sleep-command on the bus.
 *  \param[in]  Channel: LIN channel to be addressed
 *  \param[in]  -
 *  \return     E_OK: Sleep command has been accepted
 *  \return     E_NOT_OK: Sleep command has not been accepted, development or production error occurred
 *  \pre        The LIN driver shall have been initialized before this service is called.
 *  \context    Called by upper layer.
 *  \note       The service ID of this function is LIN_GOTOSLEEPINTERNAL_ID
 *  \sws req    
 *********************************************************************************************************************/
FUNC(Std_ReturnType, LIN_CODE) Lin_GoToSleepInternal(VAR(uint8, AUTOMATIC) Channel);

/**********************************************************************************************************************
 *  Lin_WakeUp
 *********************************************************************************************************************/
/*! \brief      This function is used to generate wakeup frame.
 *  \param[in]  Channel: LIN channel to be addressed
 *  \param[in]  -
 *  \return     E_OK: Wake-up request has been accepted
 *  \return     E_NOT_OK: Wake-up request has not been accepted, development or production error occurred
 *  \pre        The LIN driver shall have been initialized before this service is called.
 *  \context    Called by upper layer.
 *  \note       The service ID of this function is LIN_WAKEUP_ID
 *  \sws req    
 *********************************************************************************************************************/
FUNC(Std_ReturnType, LIN_CODE) Lin_Wakeup(VAR(uint8, AUTOMATIC) Channel);

/**********************************************************************************************************************
 *  Lin_GetStatus
 *********************************************************************************************************************/
/*! \brief      This function is called to get the current status of the Lin Driver. It can also be used to get the 
 *              received data in case of Slave_response.
 *  \param[in]  Channel: LIN channel to be addressed
 *  \param[in]  Lin_SduPtr: Pointer to pointer to shadow buffer or memory mapped LIN Hardware receive buffer
 *  \return     Lin_StatusType: Information about the current message state.
 *  \return     -
 *  \pre        The LIN driver shall have been initialized before this service is called.
 *  \context    Called by upper layer.
 *  \note       The service ID of this function is LIN_GETSTATUS_ID
 *  \sws req    
 *********************************************************************************************************************/
FUNC(Lin_StatusType, LIN_CODE) Lin_GetStatus(VAR(uint8, AUTOMATIC) Channel, \
                                                  P2VAR(uint8 *, AUTOMATIC, LIN_APPL_VAR) Lin_SduPtr);

/**********************************************************************************************************************
 *  Lin_CheckWakeup
 *********************************************************************************************************************/
/*! \brief      This function is called to check if proper wakeup sequence has occured.
 *  \param[in]  Channel: LIN channel to be addressed
 *  \param[in]  -
 *  \return     E_OK: Wake-up sequence has occured.
 *  \return     E_NOT_OK: Wake-up sequence has not occured, development or production error occurred
 *  \pre        The LIN driver shall have been initialized before this service is called.
 *  \context    Called by upper layer.
 *  \note       The service ID of this function is LIN_CHECKWAKEUP_ID
 *  \sws req    
 *********************************************************************************************************************/
FUNC(Std_ReturnType, LIN_CODE) Lin_CheckWakeup(VAR(uint8, AUTOMATIC) Channel);

#if ( LIN_VERSION_INFO_API == STD_ON )
/**********************************************************************************************************************
 *  Lin_GetVersionInfo
 *********************************************************************************************************************/
/*! \brief      This service returns the version information of this module.
 *  \param[in]  versioninfo: Pointer to where to store the version information of this module.
 *  \param[in]  -
 *  \return     -
 *  \return     -
 *  \pre        -
 *  \context    Called by upper layer.
 *  \note       This function shall be pre compile time configurable On/Off by the configuration parameter:
 *              LIN_VERSION_INFO_API
 *  \note       The service ID of this function is LIN_GETVERSIONINFO_ID
 *  \sws req   
 *********************************************************************************************************************/
FUNC(void, LIN_CODE)           Lin_GetVersionInfo(P2VAR(Std_VersionInfoType, AUTOMATIC, LIN_APPL_VAR) versioninfo);
#endif

#if (LIN_LOOPBACKTEST_ENABLE == STD_ON)
/**********************************************************************************************************************
 *  Lin_TestLoopBackModeEnable
 *********************************************************************************************************************/
/*! \brief      This service enables the loopback mode .
 *  \param[in]  Channel: Channel on which loopback is to be enabled.
 *  \param[in]  LoopBackMode : Mode of the loopback
 *  \return     -
 *  \return     -
 *  \pre        -
 *  \context    Called by upper layer.
 *  \note       This function shall be pre compile time configurable On/Off by the configuration parameter:
 *              LIN_LOOPBACKTEST_ENABLE
 *  \note       The service has to be used only while testing and should not be called during actual communication.
 *  \sws req   
 *********************************************************************************************************************/
FUNC(Std_ReturnType, LIN_CODE)  Lin_TestLoopBackModeEnable(VAR(uint8, AUTOMATIC) Channel, \
												 VAR(uint8, AUTOMATIC) LoopBackMode);
/**********************************************************************************************************************
 *  Lin_TestLoopBackModeDisable
 *********************************************************************************************************************/
/*! \brief      This service disables the loopback mode .
 *  \param[in]  Channel: Channel on which loopback is to be disabled.
 *  \param[in]  -
 *  \return     -
 *  \return     -
 *  \pre        -
 *  \context    Called by upper layer.
 *  \note       This function shall be pre compile time configurable On/Off by the configuration parameter:
 *              LIN_LOOPBACKTEST_ENABLE
 *  \note       The service has to be used only while testing and should not be called during actual communication.
 *  \sws req   
 *********************************************************************************************************************/
FUNC(Std_ReturnType, LIN_CODE)  Lin_TestLoopBackModeDisable(VAR(uint8, AUTOMATIC) Channel);
/**********************************************************************************************************************
 *  Lin_LoopBackDataTest
 *********************************************************************************************************************/
/*! \brief      This service checks loopback data.
 *  \param[in]  Channel: Channel on which loopback data has to be checked.
 *  \param[in]  -
 *  \return     -
 *  \return     -
 *  \pre        -
 *  \context    Called by upper layer.
 *  \note       This function shall be pre compile time configurable On/Off by the configuration parameter:
 *              LIN_LOOPBACKTEST_ENABLE
 *  \note       The service has to be used only while testing and should not be called during actual communication.
 *  \sws req   
 *********************************************************************************************************************/
FUNC(Std_ReturnType, LIN_CODE) Lin_LoopBackDataTest(VAR(uint8, AUTOMATIC) Channel);
/**********************************************************************************************************************
 *  Lin_LoopBackSendFrame
 *********************************************************************************************************************/
/*! \brief      The function Lin_LoopBackSendFrame generates the header part of a LIN frame on the addressed LIN 
 *              channel, and the response if Master Response type is selected.
 *  \param[in]  Channel: LIN channel to be addressed
 *  \param[in]  PduInfoPtr: Pointer to PDU containing the PID, Checksum model, Response type, Dl and SDU data pointer
 *  \return     E_OK: send command has been accepted
 *  \return     E_NOT_OK: send command has not been accepted, development or production error occurred
 *  \pre        The LIN driver shall have been initialized before this service is called.
 *              The LIN channel shall be in the LIN_CH_OPERATIONAL state.
 *  \context    Called by upper layer.
 *  \note       The service has to be used only while testing and should not be called during actual communication.
 *  \sws req    
 *********************************************************************************************************************/
FUNC(Std_ReturnType, LIN_CODE) Lin_LoopBackSendFrame(VAR(uint8, AUTOMATIC) Channel, \
                                                        P2VAR(Lin_PduType, AUTOMATIC, LIN_APPL_VAR) PduInfoPtr); 
#endif

#if (LIN_SAFETI_API == STD_ON)
/**********************************************************************************************************************
 *  Lin_ConfigRegReadBack
 *********************************************************************************************************************/
/*! \brief      This service reads the configuration registers.
 *  \param[in]  Channel: Channel on which the configuration registers needs to be read.
 *  \param[in]  -
 *  \return     -
 *  \return     -
 *  \pre        -
 *  \context    Called by upper layer.
 *  \note       This function shall be pre compile time configurable On/Off by the configuration parameter:
 *              LIN_SAFETI_API
 *  \note       The service can be called during normal operation to check if configuration is correct.
 *  \sws req   
 *********************************************************************************************************************/
FUNC(Std_ReturnType, LIN_CODE)  Lin_ConfigRegReadBack(VAR(uint8, AUTOMATIC) Channel);
#endif

/**********************************************************************************************************************
 *  Lin_Interrupt
 *********************************************************************************************************************/
/*! \brief      Interrupt call back function called from the LIN ISR function visible only to Lin.c and Lin_Irq.c
 *  \param[in]  Channel: LIN channel that caused the Interrupt and to be addressed
 *  \param[in]  -
 *  \return     -
 *  \return     -
 *  \pre        User should not call this function directly.
s *  \context    Called with in same Layer.
 *  \note       The service ID of this function is LIN_INTERRUPT_ID
 *  \sws req    
 *********************************************************************************************************************/
FUNC(void, LIN_CODE_ISR)       Lin_Interrupt(VAR(uint8, AUTOMATIC) LinHwChannel);

#if ((LIN_CFG_ISR_TYPE == LIN_ISR_CAT1 ) || (LIN_CFG_ISR_TYPE == LIN_ISR_VOID ))
FUNC(void, LIN_CODE_ISR) LinIsr_0 (void);
#endif

/*********************************************************************************************************************
 *  EXTERN DECLARATIONS
 ********************************************************************************************************************/

extern CONST(Lin_ConfigType, LIN_PBCFG_ROOT) LinGlobalConfig[Lin_TotalNumberOfChannels];

#endif /* LIN_H */

/*********************************************************************************************************************
 *  END OF FILE: Lin.h
 ********************************************************************************************************************/

