/**********************************************************************************************************************
 *  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 - 2011.  All rights reserved.
 *
 *  \endverbatim
 *  -------------------------------------------------------------------------------------------------------------------
 *  FILE DESCRIPTION
 *  -------------------------------------------------------------------------------------------------------------------
 *         File:  Spi.c
 *      Project:  Tms570_AutosarSpiDriver
 *       Module:  SpiDriver
 *    Generator:  -
 *
 *  Description:  This file implements the Spi Api for the spi communication driver.
 *---------------------------------------------------------------------------------------------------------------------
 * Author:  Prathap Srinivasan
 *---------------------------------------------------------------------------------------------------------------------
 * Revision History
 *---------------------------------------------------------------------------------------------------------------------
 * Version        Date         Author               Change ID        Description
 *---------------------------------------------------------------------------------------------------------------------
 * 02.10.00       24Jan2013    Prathap Srinivasan  0000000000000    Initial Version adapted for TMS570LS1227
 * 02.20.00       08Aug2013    Enrique Lizarraga   SDOCM00103201    -Updated version number.
 *                                                                  -Renamed CS identifiers
 *                                                                  -Added traceability tags
 *                                                                  -Added Loopback mode enable/disable API's
 *                                                                  -Added protection to DET error codes to avoid
 *                                                                   redefinition
 *                                                                  -Removed function-like macro Spi_DetReportError
 *                                                                   and replaced by direct DET API call.
 * 02.21.00       30Sep2014    Enrique Lizarraga   SDOCM00112317    -Update version info
 *                                                                  -Added traceability tags
 *                                                                  -Removed unused code
 *                                                                  -MISRA fixes
 * 02.21.01       29Feb2016    Enrique Lizarraga   SDOCM00121246    -Update version info
 * 02.22.00       18Jul2016    Prathap Srinivasan  SDOCM00122408    -Update version info
 * 																	-Added Sync Polling Mode Support
 * 02.22.01       10Aug2016    Prathap Srinivasan  SDOCM00122593    -Bug Fix: Spi_IrqTxRxERR function checks for
 *                                                                   wrong error status bit
 * 02.22.02       23Aug2016    Vishwanath Reddy    SDOCM00121246    -Update version info
 * 02.22.03       26Sep2016    Prathap Srinivasan  SDOCM00122695    -Update version info
 * 02.30.00       01Dec2016    Sudhakar Singh         None          -Update version info
 * 05.30.00       15Mar2017    Sudhakar Singh      SDOCM00122834    Version supports TMS570LSxx Family of Devices.
 *
 *********************************************************************************************************************/

#ifndef SPI_H
#define SPI_H

/**********************************************************************************************************************
 * INCLUDES
 *********************************************************************************************************************/
/* SourceId : SPI_SourceId_014 */
/* DesignId : SPI_DesignId_019 */
/* Requirements : AR_SPI_SR11, AR_SPI_SR12, AR_SPI_SR125, AR_SPI_SR126 */
#include "Std_Types.h"
#include "Spi_Cfg.h"
#include "Spi_Dlc.h"
#include "Spi_Irq.h"

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

/* vendor and module identification */
#define SPI_VENDOR_ID              (44U)
#define SPI_MODULE_ID              ((uint16)83U)
#define SPI_INSTANCE_ID            ((uint8)0U)

/* AUTOSAR Software Specification Version Information */
#define SPI_AR_RELEASE_MAJOR_VERSION       (0x04U)
#define SPI_AR_RELEASE_MINOR_VERSION       (0x00U)
#define SPI_AR_RELEASE_REVISION_VERSION    (0x03U)

/* AUTOSAR Software Specification Version Information */
#define SPI_AR_MAJOR_VERSION       (0x03U)
#define SPI_AR_MINOR_VERSION       (0x02U)
#define SPI_AR_PATCH_VERSION       (0x00U)

/* Component Version Information */
#define SPI_SW_MAJOR_VERSION               (5U)
#define SPI_SW_MINOR_VERSION               (30U)
#define SPI_SW_PATCH_VERSION               (0U)

/* HWUnit Index */
#define SPI_UNIT0    ((Spi_HWUnitType)0U)
#define SPI_UNIT1    ((Spi_HWUnitType)1U)
#define SPI_UNIT2    ((Spi_HWUnitType)2U)
#define SPI_UNIT3    ((Spi_HWUnitType)3U)
#define SPI_UNIT4    ((Spi_HWUnitType)4U)

/* Buffer definitions IB=internal, EB=external buffers  */
#define SPI_IB      (0U)
#define SPI_EB      (1U)
#define SPI_IB_EB   (2U)

/* SourceId : SPI_SourceId_029 */
/* DesignId : SPI_DesignId_025 */
/* Requirements : AR_SPI_SR99 */
/* DET Error Codes */
#ifndef SPI_E_PARAM_CHANNEL
#define SPI_E_PARAM_CHANNEL         ((uint8)0x0AU) /* wrong channel  parameter given */
#endif
#ifndef SPI_E_PARAM_JOB
#define SPI_E_PARAM_JOB             ((uint8)0x0BU) /* wrong job      parameter given */
#endif
#ifndef SPI_E_PARAM_SEQ
#define SPI_E_PARAM_SEQ             ((uint8)0x0CU) /* wrong sequence parameter given */
#endif
#ifndef SPI_E_PARAM_LENGTH
#define SPI_E_PARAM_LENGTH          ((uint8)0x0DU) /* wrong length   parameter given */
#endif
#ifndef SPI_E_PARAM_UNIT
#define SPI_E_PARAM_UNIT            ((uint8)0x0EU) /* wrong HWUnit   parameter given */
#endif
#ifndef SPI_E_PARAM_POINTER
#define SPI_E_PARAM_POINTER         ((uint8)0x10U) /* API called with a NULL pointer */
#endif
#ifndef SPI_E_UNINIT
#define SPI_E_UNINIT                ((uint8)0x1AU) /* Error code for not initialized module */
#endif
#ifndef SPI_E_SEQ_PENDING
#define SPI_E_SEQ_PENDING           ((uint8)0x2AU) /* Error code if service called in a wrong sequence */
#endif
#ifndef SPI_E_SEQ_IN_PROCESS
#define SPI_E_SEQ_IN_PROCESS        ((uint8)0x3AU) /* transmission of sync. sequence in progress */
#endif
#ifndef SPI_E_ALREADY_INITIALIZED
#define SPI_E_ALREADY_INITIALIZED   ((uint8)0x4AU) /* Init() service called twice without DeInit() */
#endif

/* These are the service IDs of the functions in the ASR SWS specification */
#define SPI_SID_INIT                  (0x00U)
#define SPI_SID_DEINIT                (0x01U)
#define SPI_SID_WRITE_IB              (0x02U)
#define SPI_SID_ASYNC_TRANSMIT        (0x03U)
#define SPI_SID_READ_IB               (0x04U)
#define SPI_SID_SETUP_EB              (0x05U)
#define SPI_SID_GET_STATUS            (0x06U)
#define SPI_SID_GET_JOB_RESULT        (0x07U)
#define SPI_SID_GET_SEQ_RESULT        (0x08U)
#define SPI_SID_GETVERSION_INFO       (0x09U)
#define SPI_SID_SYNC_TRANSMIT         (0x0AU)
#define SPI_SID_GET_UNIT_STATUS       (0x0BU)
#define SPI_SID_CANCEL                (0x0CU)
#define SPI_SID_SET_ASYNC_MODE        (0x0DU)
#define SPI_SID_MAINFUNCTION_HANDLING (0x10U)
#define SPI_SID_MAINFUNCTION_DRIVING  (0x11U)

/**********************************************************************************************************************
 *  GLOBAL FUNCTION MACROS
 *********************************************************************************************************************/

/**********************************************************************************************************************
 *  GLOBAL DATA TYPES AND STRUCTURES
 *********************************************************************************************************************/
/* Driver status enumerations */
/* SourceId : SPI_SourceId_015 */
/* DesignId : SPI_DesignId_020 */
/* Requirements : AR_SPI_SR133 */
typedef enum
{
    SPI_UNINIT = 0U,
    SPI_IDLE = 1U,
    SPI_BUSY = 2U
}   Spi_StatusType;

/* SourceId : SPI_SourceId_016 */
/* DesignId : SPI_DesignId_020 */
/* Requirements : AR_SPI_SR142 */
typedef enum
{
    SPI_JOB_OK = 0U,
    SPI_JOB_QUEUED = 1U,
    SPI_JOB_PENDING = 2U,
    SPI_JOB_ACTIVE = 3U,    /* this is an internal state */
    SPI_JOB_FAILED = 4U
} Spi_JobResultType;

typedef enum
{
    SPI_DLC_OK = 0U,
    SPI_DLC_PENDING = 1U,
    SPI_DLC_FAILED = 2U
}Spi_HwUnitResultType;

/* SourceId : SPI_SourceId_017 */
/* DesignId : SPI_DesignId_020 */
/* Requirements : AR_SPI_SR147 */
typedef enum
{
    SPI_SEQ_OK = 0U,
    SPI_SEQ_PENDING = 1U,
    SPI_SEQ_FAILED = 2U,
    SPI_SEQ_CANCELLED = 3U
} Spi_SeqResultType;

typedef enum
{
    SPI_MSB = 0U,
    SPI_LSB = 1U
} Spi_TransferType;

/* type for SpiCsPolarity and SpiShiftClockIdleLevel */
typedef enum
{
    SPI_LOW = STD_LOW,
    SPI_HIGH
} Spi_LevelType;

/* type for  SpiCsIdentifier */
typedef enum
{
    SPI_SCS0 = 0U,
    SPI_SCS1,
    SPI_SCS2,
    SPI_SCS3
} Spi_ScsType;

/* type for SpiCsMode */
typedef enum
{
    SPI_SINGLE = 0U,
    SPI_CONTINOUS = 1U
} Spi_CsModeType;

/* enums end */

/* Driver, job, sequence status types */

/* SourceId : SPI_SourceId_018 */
/* DesignId : SPI_DesignId_020 */
/* Requirements : AR_SPI_SR155 */
typedef uint16 Spi_NumberOfDataType;
/* SourceId : SPI_SourceId_019 */
/* DesignId : SPI_DesignId_020 */
/* Requirements : AR_SPI_SR158, AR_SPI_SR159 */
typedef uint16 Spi_JobType;
/* SourceId : SPI_SourceId_020 */
/* DesignId : SPI_DesignId_020 */
/* Requirements : AR_SPI_SR153, AR_SPI_SR154 */
typedef uint8  Spi_DataType;
/* SourceId : SPI_SourceId_021 */
/* DesignId : SPI_DesignId_020 */
/* Requirements : AR_SPI_SR156, AR_SPI_SR157 */
typedef uint8  Spi_ChannelType;
/* SourceId : SPI_SourceId_022 */
/* DesignId : SPI_DesignId_020 */
/* Requirements : AR_SPI_SR160, AR_SPI_SR161 */
typedef uint8  Spi_SequenceType;
/* SourceId : SPI_SourceId_023 */
/* DesignId : SPI_DesignId_020 */
/* Requirements : AR_SPI_SR162, AR_SPI_SR163 */
typedef uint8  Spi_HWUnitType;
typedef uint8  Spi_ExternalDeviceType;

/* SourceId : SPI_SourceId_024 */
/* DesignId : SPI_DesignId_020 */
/* Requirements : AR_SPI_SR130 */
/* spi external device structure */
typedef struct
{
    Spi_ScsType    Spi_DioPin;        /* holds the configured Dio pin for toggling CS */
    uint16         Spi_CsProperty;    /* low byte the CS idle level, high byte res. */
    uint16         Spi_Baudrate;
    uint8          Spi_Mode;          /* Mode 0 = {0=CPOL,0=CPHA}; Mode 1={0,1}; Mode 2={1,0} Mode 3={1,1} */
    uint16         Spi_ClkDelays[2U]; /* includes CS2Clk, Clk2Cs, Clk2Ena, Ena2Clk */
    uint16         Spi_CsIdleTime;    /* CS idle time if single mode is chosen */
    Spi_CsModeType Spi_CsMode;        /* select single or continuous mode */
    uint8          Spi_HwInstance;    /* SPI HW Instance */
}Spi_ExternalDeviceConfigType;

/* descriptor type definition for buffers */
typedef struct
{
    P2VAR(Spi_DataType, AUTOMATIC, SPI_APPL_DATA) Spi_Tx_pt;  /* Transmit buffer pointer */
    P2VAR(Spi_DataType, AUTOMATIC, SPI_APPL_DATA) Spi_Rx_pt;  /* Receive buffer pointer */
    uint16 Spi_TxRxLength;    /* length to send and receive */
} Spi_BufferDescriptorType;

/* SourceId : SPI_SourceId_025 */
/* DesignId : SPI_DesignId_020 */
/* Requirements : AR_SPI_SR129 */
/* spi channel configuration structure */
typedef struct
{
    P2VAR(Spi_BufferDescriptorType, AUTOMATIC, SPI_VAR_INIT) Spi_Descriptor;
    Spi_ChannelType      Spi_ChannelId;
    uint8                Spi_ChannelBuf;     /* Buffer Type IB/EB */
    uint8                Spi_DataWidth;      /* width of clock frame in bits */
    uint16               Spi_DefaultTxData;  /* Default Transmit Value */
    Spi_NumberOfDataType Spi_BufLength;      /* max Data length external or internal buffer */
    uint8                Spi_TxStart;        /* start with MSB or LSB */
} Spi_ChannelConfigType;

/* SourceId : SPI_SourceId_026 */
/* DesignId : SPI_DesignId_020 */
/* Requirements : AR_SPI_SR130, AR_SPI_SR132 */
/* spi job configuration structure */
typedef struct
{
    P2FUNC(void, SPI_APPL_CODE, Spi_JobEndNotification)(void);            /* Job notification */
    P2CONST(Spi_ChannelType, AUTOMATIC, SPI_PBCFG) Spi_ChannelList;       /* Channel index list */
    Spi_ExternalDeviceType Spi_ExDev;           /* external device assigned to a job */
    uint8          Spi_JobID;                   /* job ID */
    uint8          Spi_JobPrio;                 /* job priority */
    Spi_HWUnitType Spi_HWUnitID;                /* HWUnit */
} Spi_JobConfigType;

/* SourceId : SPI_SourceId_027 */
/* DesignId : SPI_DesignId_020 */
/* Requirements : AR_SPI_SR131 */
/* spi sequence configuration structure */
typedef struct
{
    P2FUNC(void, SPI_APPL_CODE, Spi_SequenceEndNotification)(void);  /* sequence end notification */
    P2CONST(Spi_JobType, AUTOMATIC, SPI_PBCFG) Spi_JobList;      /* job index list */
    Spi_SequenceType Spi_SeqId;                    /* sequence id */
    uint8            Spi_SeqInt;                   /* sequence interruptible */
} Spi_SequenceConfigType;

typedef struct
{
    P2VAR(Spi_JobResultType, AUTOMATIC, SPI_VAR_ZERO_INIT) Spi_JobResult;      /* job result      */
    P2VAR(Spi_SeqResultType, AUTOMATIC, SPI_VAR_ZERO_INIT) Spi_SeqResult;      /* sequence result */
    P2VAR(Spi_HwUnitResultType, AUTOMATIC, SPI_VAR_ZERO_INIT) Spi_DlcResult;   /* hardware result */
} Spi_ResultType;

/* SourceId : SPI_SourceId_028 */
/* DesignId : SPI_DesignId_020 */
/* Requirements : AR_SPI_SR127, AR_SPI_SR128 */
/* spi runtime structure */
typedef struct
{
    P2CONST(Spi_ExternalDeviceConfigType, AUTOMATIC, SPI_PBCFG) Spi_DevCfg;
    P2CONST(Spi_ChannelConfigType, AUTOMATIC, SPI_PBCFG) Spi_ChannelCfg;
    P2CONST(Spi_JobConfigType, AUTOMATIC, SPI_PBCFG) Spi_JobCfg;
    P2CONST(Spi_SequenceConfigType, AUTOMATIC, SPI_PBCFG) Spi_SeqCfg;
    P2CONST(Spi_ResultType, AUTOMATIC, SPI_VAR_INIT) Spi_GlobalStatus_pt;
    uint8 Spi_MaxExtDevs;      /* maximum number of external devices */
    uint8 Spi_MaxChannels;     /* maximum number of channels */
    uint8 Spi_MaxJobs;         /* maximum number of jobs */
    uint8 Spi_MaxSequences;    /* maximum number of sequences */
    uint8 Spi_MaxDlcs;         /* maximum number of dlc's */
} Spi_ConfigType;

typedef struct
{
    uint32 Spi_GCR0 ;         /* SPI Global Control Register 0 */
    uint32 Spi_GCR1 ;         /* SPI Global Control Register 1 */
    uint32 Spi_DEF  ;         /* SPI Default Chip Select Register */
    uint32 Spi_EN   ;         /* Multi-buffer Mode Enable Register */
}Spi_RegisterReadbackType;

/**********************************************************************************************************************
 *  GLOBAL DATA PROTOTYPES
 *********************************************************************************************************************/
extern CONST(Spi_ConfigType, SPI_PBCFG) SpiRuntime;
/*SAFETYMCUSW 131 S MR:5.2 <APPROVED> "Variable declaration and definition" */
extern P2CONST(Spi_ConfigType, SPI_VAR_ZERO_INIT, SPI_PBCFG) Spi_ConfigPtr_pt;
/*SAFETYMCUSW 131 S MR:5.2 <APPROVED> "Variable declaration and definition" */
extern volatile VAR(Spi_StatusType, SPI_VAR_ZERO_INIT) Spi_DrvStatus;

/**********************************************************************************************************************
 *  GLOBAL FUNCTION PROTOTYPES
 *********************************************************************************************************************/
FUNC(void, SPI_CODE) Spi_Init
(
    P2CONST(Spi_ConfigType, AUTOMATIC, SPI_PBCFG) ConfigPtr
);
FUNC(Std_ReturnType, SPI_CODE) Spi_DeInit(void);
#if ((SPI_CHANNELBUFFERS == SPI_EB) || (SPI_CHANNELBUFFERS == SPI_IB_EB))
FUNC(Std_ReturnType, SPI_CODE) Spi_SetupEB
(
    Spi_ChannelType Channel,
    P2CONST(Spi_DataType, AUTOMATIC, SPI_APPL_DATA) SrcDataBufferPtr,
    P2VAR(Spi_DataType, AUTOMATIC, SPI_APPL_DATA) DesDataBufferPtr,
    Spi_NumberOfDataType Length
);
#endif
#if ((SPI_SCALEABILITY == SPI_LEVEL_1) || (SPI_SCALEABILITY == SPI_LEVEL_2))
FUNC(Std_ReturnType, SPI_CODE) Spi_AsyncTransmit
(
    Spi_SequenceType Sequence
);
#endif
#if ((SPI_SCALEABILITY == SPI_LEVEL_0) || (SPI_SCALEABILITY == SPI_LEVEL_2))
FUNC(Std_ReturnType, SPI_CODE) Spi_SyncTransmit
(
    Spi_SequenceType Sequence
);
#endif
#if (SPI_CANCEL_API == STD_ON)
FUNC(void, SPI_CODE) Spi_Cancel
(
    Spi_SequenceType Sequence
);
#endif
FUNC(Spi_StatusType, SPI_CODE) Spi_GetStatus(void);
FUNC(Spi_JobResultType, SPI_CODE) Spi_GetJobResult
(
    Spi_JobType Job
);
FUNC(Spi_SeqResultType, SPI_CODE) Spi_GetSequenceResult
(
    Spi_SequenceType Seq
);
FUNC(Spi_StatusType, SPI_CODE) Spi_GetHWUnitStatus
(
    Spi_HWUnitType HWUnit
);

#if (SPI_VERSION_INFO_API == STD_ON)
FUNC(void, SPI_CODE) Spi_GetVersionInfo
(
    P2VAR(Std_VersionInfoType, AUTOMATIC, SPI_APPL_DATA) VersioninfoPtr
);
#endif

#if (SPI_LOOPBACK_MODE_API == STD_ON)
FUNC(Std_ReturnType, SPI_CODE)  Spi_EnableLoopbackMode
(
    Spi_HWUnitType HWUnit,
    Spi_LoopbackModeType LBMode
);

FUNC(Std_ReturnType, SPI_CODE)  Spi_DisableLoopbackMode
(
    Spi_HWUnitType HWUnit
);
#endif /* SPI_LOOPBACK_MODE_API == STD_ON */

#if (SPI_REGISTER_READBACK_API == STD_ON)
FUNC(Std_ReturnType, SPI_CODE) Spi_RegisterReadback
(
    P2VAR(Spi_RegisterReadbackType, AUTOMATIC, SPI_APPL_DATA) RegisterReadbackPtr,
    Spi_HWUnitType HWUnit
);
#endif

#endif  /* SPI_H */
/**********************************************************************************************************************
 *  END OF FILE: Spi.h
 *********************************************************************************************************************/
