/* ======================================================================
 *   Copyright (C) 2022 Texas Instruments Incorporated
 *
 *   All rights reserved. Property of Texas Instruments Incorporated.
 *   Restricted rights to use, duplicate or disclose this code are
 *   granted through contract.
 *
 *   The program may not be used without the written permission
 *   of Texas Instruments Incorporated or against the terms and conditions
 *   stipulated in the agreement under which this program has been
 *   supplied.
 * ==================================================================== */

/**
 *  \file     CDD_I2c.c
 *
 *  \brief    This file contains CDD I2C MCAL driver implementation.
 *
 */
 
/*  -------------------------------------------------------------------------------------------------------------------
*  FILE DESCRIPTION
*  -------------------------------------------------------------------------------------------------------------------
*         File:  CDD_I2C.c
*    Component:  AM263x MCAL Driver
*       Module:  CDD_I2CDriver
*    Generator:  None
*
*  Description: This component provides services for initialization and control of the microcontroller internal
*                I2C using complex device driver (CDD).
*********************************************************************************************************************/
#ifdef __cplusplus
extern "C"{
#endif


/******************************************************************************
**                       Include Files                                      **
******************************************************************************/

#include "Cdd_I2c.h"
#include "Cdd_I2c_Priv.h"

#if (I2C_DEV_ERROR_DETECT == STD_ON)
#include"Det.h"
#endif /* End DET */

/******************************************************************************
**                          Macro Definitions                               **
******************************************************************************/

/******************************************************************************
**                           Version Check                                   **
******************************************************************************/
#if ((CDDI2C_VERSION_AR_RELEASE_MAJOR_VERSION != (4U)) || \
    (CDDI2C_VERSION_AR_RELEASE_MINOR_VERSION != (3U)) ||  \
    (CDDI2C_VERSION_AR_RELEASE_REVISION_VERSION != (1U)))
    #error "CDD_I2c: AUTOSAR Version Numbers of CDD_I2c are different!!"
#endif

#if ((CDDI2C_VERSION_SW_MAJOR_VERSION != (0U)) || \
    (CDDI2C_VERSION_SW_MINOR_VERSION != (9U))  || \
	(CDDI2C_VERSION_SW_PATCH_VERSION != (0U)))
    #error "CDD_I2c: Software Version Numbers are inconsistent!!"
#endif


/******************************************************************************
**                      Global variables                                     **
******************************************************************************/
#define I2C_START_SEC_VAR_INIT_UNSPECIFIED
#include "I2c_MemMap.h"
P2CONST(I2C_ConfigType, AUTOMATIC, I2C_CONST)
                                    I2c_GpConfigType;
#define I2C_STOP_SEC_VAR_INIT_UNSPECIFIED
#include "I2c_MemMap.h"
									
#define I2C_START_SEC_VAR_INIT_UNSPECIFIED
#include "I2c_MemMap.h"
/* It's store the channel configuration pointer */
P2CONST(I2C_ChannelConfigType, AUTOMATIC, I2C_CONST)
                                    I2c_GpChannelConfigDataPtr;
#define I2C_STOP_SEC_VAR_INIT_UNSPECIFIED
#include "I2c_MemMap.h"

#define I2C_START_SEC_VAR_INIT_UNSPECIFIED
#include "I2c_MemMap.h"
/* It's store the sequence configuration pointer */
P2CONST(I2C_SequenceConfigType, AUTOMATIC, I2C_CONST)
                                    I2c_GpSequenceConfigDataPtr;
#define I2C_STOP_SEC_VAR_INIT_UNSPECIFIED
#include "I2c_MemMap.h"

/* this variable is used to store I2C module status */
#define I2C_START_SEC_VAR_INIT_UNSPECIFIED
#include "I2c_MemMap.h"
VAR(StatusType, AUTOMATIC) I2c_GenModuleState = I2C_UNINIT;
#define I2C_STOP_SEC_VAR_INIT_UNSPECIFIED
#include "I2c_MemMap.h"

#define I2C_START_SEC_VAR_CLEARED_16
#include "I2c_MemMap.h"
VAR(I2C_SequenceType, AUTOMATIC) I2c_GddSeqFrameCount;
#define I2C_STOP_SEC_VAR_CLEARED_16
#include "I2c_MemMap.h"

/* this variable is used to store sequence frame */
#define I2C_START_SEC_VAR_CLEARED_UNSPECIFIED
#include "I2c_MemMap.h"
VAR(I2C_FrameType, AUTOMATIC) I2c_GaaSeqFrame[I2C_MAX_SEQUENCE_CONFIG];
#define I2C_STOP_SEC_VAR_CLEARED_UNSPECIFIED
#include "I2c_MemMap.h"

/* this variable is used to store communication handling type */
#define I2C_START_SEC_VAR_INIT_UNSPECIFIED
#include "I2c_MemMap.h"
VAR(I2C_HandlingType, AUTOMATIC) I2c_GenHandling;
#define I2C_STOP_SEC_VAR_INIT_UNSPECIFIED
#include "I2c_MemMap.h"

/* this variable is used to store channel count */
#define I2C_START_SEC_VAR_CLEARED_16
#include "I2c_MemMap.h"
VAR(I2C_ChannelType, AUTOMATIC) I2C_GddChaCount;
#define I2C_STOP_SEC_VAR_CLEARED_16
#include "I2c_MemMap.h"

/* this variable is used to cancel sequence id */
#define I2C_START_SEC_VAR_CLEARED_16
#include "I2c_MemMap.h"
VAR(I2C_SequenceType, AUTOMATIC) I2c_GddCancelSeq;
#define I2C_STOP_SEC_VAR_CLEARED_16
#include "I2c_MemMap.h"

#define I2C_START_SEC_VAR_CLEARED_UNSPECIFIED
#include "I2c_MemMap.h"
VAR(I2C_SequenceStatusType, AUTOMATIC)I2c_GaaSeqStatus[I2C_MAX_SEQUENCE_CONFIG];
#define I2C_STOP_SEC_VAR_CLEARED_UNSPECIFIED
#include "I2c_MemMap.h"

/* this variable is used to store sequence count */
#define I2C_START_SEC_VAR_CLEARED_16
#include "I2c_MemMap.h"
VAR(I2C_SequenceType, AUTOMATIC) I2C_GddSeqCount;
#define I2C_STOP_SEC_VAR_CLEARED_16
#include "I2c_MemMap.h"

/* this variable is used to store channel status */
#define I2C_START_SEC_VAR_CLEARED_UNSPECIFIED
#include "I2c_MemMap.h"
VAR(I2C_ChannelStatusType, AUTOMATIC) I2c_GaaChStatus[I2C_MAX_CHANNEL_ACCEPT];
#define I2C_STOP_SEC_VAR_CLEARED_UNSPECIFIED
#include "I2c_MemMap.h"

#define I2C_START_SEC_VAR_CLEARED_UNSPECIFIED
#include "I2c_MemMap.h"
P2VAR(I2c_HardwareConfigType, AUTOMATIC, I2C_CONST) I2c_GpHwReg;
#define I2C_STOP_SEC_VAR_INIT_UNSPECIFIED
#include "I2c_MemMap.h"

#define I2C_START_SEC_VAR_CLEARED_UNSPECIFIED
#include "I2c_MemMap.h"
P2VAR(I2C_HwConfigType, AUTOMATIC, I2C_VAR_PTR) I2c_GpHardwarewReg;
#define I2C_STOP_SEC_VAR_CLEARED_UNSPECIFIED
#include "I2c_MemMap.h"

#define I2C_START_SEC_VAR_CLEARED_UNSPECIFIED
#include "I2c_MemMap.h"
P2VAR(I2c_ChannelVarDescType, AUTOMATIC, I2C_VAR_PTR) I2c_GpChannelVarDescType;
#define I2C_STOP_SEC_VAR_CLEARED_UNSPECIFIED
#include "I2c_MemMap.h"
/******************************************************************************
 **                      Function Definitions                                **
 *****************************************************************************/

/******************************************************************************
 ** Service name       : I2c_Init                                            **
 **                                                                          **
 ** Service ID[hex]    : 0x00                                                **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : ConfigPtr                                           **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : This service is to initialize the I2C driver.       **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) I2c_Init
              (P2CONST(I2C_ConfigType, AUTOMATIC, I2C_CONST) ConfigPtr)
{
  P2CONST(I2C_ConfigType, AUTOMATIC, I2C_CONST) LpConfigPtr;
  P2CONST(I2C_HwConfigType, AUTOMATIC, I2C_CONST) LpHwConfigPtr;
  VAR(uint8, AUTOMATIC) LddCount = 0U;
  VAR(uint8, AUTOMATIC) Count = 0U;

  /* Check if NULL Pointer is passed */
  if (ConfigPtr == NULL_PTR)
  {
    /* DET check */
    #if (I2C_DEV_ERROR_DETECT == STD_ON)

    /* Report to DET */
    (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
          I2C_SID_INIT,I2C_E_PARAM_POINTER);

    #endif
  }
  else if (I2c_GenModuleState == I2C_IDLE)
  {
    /* DET check */
    #if (I2C_DEV_ERROR_DETECT == STD_ON)

    /* Report to DET */
    (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
                  I2C_SID_INIT, I2C_E_ALREADY_INITIALIZED);
    #endif

  }
  else if (I2c_GenModuleState == I2C_UNINIT)
  {    
    LpConfigPtr = ConfigPtr;
	I2c_GpConfigType = ConfigPtr;
	I2c_GpSequenceConfigDataPtr = I2c_GpConfigType->I2c_SequenceCfgPtr;
	I2c_GpChannelConfigDataPtr = I2c_GpConfigType->I2c_ChannelCfgPtr;
    /* Reset sequence queue container */
    I2c_GddSeqFrameCount = I2C_NUM_0; 
    I2c_GaaSeqFrame[I2c_GddSeqFrameCount].I2c_SequenceID = INVALID_SEQUENCE;
    I2c_InitHw(LpConfigPtr);	
    I2c_GenHandling = I2C_RECURRENT;
    I2C_GddChaCount = I2C_NUM_0;
    I2c_GddCancelSeq = 0;
	I2c_GenModuleState = I2C_IDLE;
	
    // I2c_GpChannelConfigDataPtr[LddCount].I2cChannelVar->I2cChannelStatus = I2C_IDLE;
	
    // I2c_GpChannelConfigDataPtr[LddCount].I2cChannelVar->I2cSequenceStatus = I2C_SEQ_OK;

  }
  else
  {
    /* do nothing */
  }
}
#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_DeInit                                          **
 **                                                                          **
 ** Service ID[hex]    : 0x01                                                **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : None                                                **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : This service is to de-initialized the I2C driver.   **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(Std_ReturnType, I2C_CODE) I2c_DeInit (P2CONST(I2C_ConfigType, AUTOMATIC, I2C_CONST)ConfigPtr)
{
  VAR(Std_ReturnType, AUTOMATIC)LddRetVal = 0U;
  P2CONST(I2C_ConfigType, AUTOMATIC, I2C_CONST) LpConfigPtr;
  VAR(I2C_DataLengthType, AUTOMATIC) LddCount = 0U;
  /* check I2C module status */
  
  for(LddCount = I2C_NUM_0; LddCount < I2c_GpConfigType->I2c_NumOfCh; LddCount++)
  {
    if (I2c_GpChannelConfigDataPtr[LddCount].I2cChannelVar->I2cChannelStatus == I2C_BUSY)
    {
      LddRetVal = E_NOT_OK;
    }
    else if (I2c_GpChannelConfigDataPtr[LddCount].I2cChannelVar->I2cChannelStatus == I2C_UNINIT)
    {
      #if (I2C_DEV_ERROR_DETECT == STD_ON)

      (void) Det_ReportError (CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
             I2C_SID_DEINIT, I2C_E_UNINIT);
    
      #endif

      LddRetVal = E_NOT_OK;
    }
  }
  #if (LddRetVal == E_OK)
  {
    LpConfigPtr = ConfigPtr;
    I2c_UnInitHW(LpConfigPtr);
	I2c_GenModuleState = I2C_UNINIT;
    LddRetVal = E_OK;
  }
  #endif
  return (LddRetVal);
}
#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_SetupEB                                         **
 **                                                                          **
 ** Service ID[hex]    : 0x03                                                **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Channel, TxDataBufferPtr, RxDataBufferPtr, Length   **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : Std_ReturnType                                      **
 **                                                                          **
 ** Description        : This service is to use store corresponding channel  **
 **                      data. EB - External Buffer                          **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(Std_ReturnType, I2C_CODE)I2c_SetupEB(
                        VAR(I2C_ChannelType, AUTOMATIC) Channel,
                        VAR(I2C_DataConstPtrType, AUTOMATIC) TxDataBufferPtr,
                        VAR(I2C_DataPtrType, AUTOMATIC) RxDataBufferPtr,
                        VAR(I2C_DataLengthType, AUTOMATIC) Length)
{
  /*local variable is used to return I2c_SetupEB status */
  P2CONST(I2C_ChannelConfigType, AUTOMATIC, I2C_APPL_DATA) lpChannel_t;
 
  VAR(Std_ReturnType, AUTOMATIC)LddRetVal = E_NOT_OK;
  VAR(Std_ReturnType, AUTOMATIC)LddRetChValidate = 0U;
  /* default status for I2c_SetupEB */
  LddRetVal = E_NOT_OK;
  if (I2c_GpChannelConfigDataPtr[Channel].I2cChannelVar->I2cChannelStatus == I2C_UNINIT)
  {
    /* DET check */
    #if (I2C_DEV_ERROR_DETECT == STD_ON)

      (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
                        I2C_SID_SETUPEB,I2C_E_UNINIT);

    #endif
  }
  else if (Channel >= I2c_GpConfigType->I2c_NumOfCh)
  {
    /* DET check for channel parameter */
    #if (I2C_DEV_ERROR_DETECT == STD_ON)

    (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
                        I2C_SID_SETUPEB,I2C_E_PARAM_CHANNEL);

    #endif

  }
  else
  {
    if (I2C_MAX_CHANNEL_ACCEPT > I2C_GddChaCount)
    {
      /* This condition will will push default data on this channel */
      if ((TxDataBufferPtr != NULL_PTR) && (RxDataBufferPtr != NULL_PTR))
      {    
        #if (I2C_DEV_ERROR_DETECT == STD_ON)
        
        (void) Det_ReportError (CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
                               I2C_SID_SETUPEB, I2C_E_PARAM_TRANS_BUFFER);
        #endif
      }/* This condition will create a TX transmission on this channel */
      else if (TxDataBufferPtr != NULL_PTR)
      {
		lpChannel_t = &(I2c_GpConfigType->I2c_ChannelCfgPtr[Channel]);
		I2c_GpChannelVarDescType = lpChannel_t->I2cChannelVar;
	    if( (Length == I2C_NUM_0)||
	        (Length > I2c_GpChannelVarDescType->I2cDataLength))
        {
          /* DET check for length parameter */
          #if (I2C_DEV_ERROR_DETECT == STD_ON)

          (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
              I2C_SID_SETUPEB, I2C_E_PARAM_LENGTH);

          #endif
        }
        else
        {
		  I2c_GpChannelVarDescType->I2cDataBufferPtr = (I2C_DataPtrType)TxDataBufferPtr;
		  I2c_GpChannelVarDescType->I2cDataLength = Length;
		  I2c_GpChannelVarDescType->I2cChannelDirection = I2C_WRITE;
          LddRetVal = E_OK;
        }
      }
	  /*This condition will create a RX Reception on this channel*/
      else if (RxDataBufferPtr != NULL_PTR)
      {
		lpChannel_t = &(I2c_GpConfigType->I2c_ChannelCfgPtr[Channel]);
		I2c_GpChannelVarDescType = lpChannel_t->I2cChannelVar;
        if((Length == I2C_NUM_0)||
	        (Length > I2c_GpChannelVarDescType->I2cDataLength))
        {
          /* DET check for length parameter */
          #if (I2C_DEV_ERROR_DETECT == STD_ON)

          (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
              I2C_SID_SETUPEB, I2C_E_PARAM_LENGTH);
          #endif
        }
        else
        {
		  I2c_GpChannelVarDescType->I2cDataBufferPtr = RxDataBufferPtr;
		  I2c_GpChannelVarDescType->I2cDataLength = Length;
		  I2c_GpChannelVarDescType->I2cChannelDirection = I2C_READ;
          LddRetVal = E_OK;
        }
      }/* This is invalid condition and the service will reject the request */
      else
      {
        LddRetVal = E_NOT_OK;
      }
      /* check for return value */
      I2c_QueueChanelStatusUpdate(Channel, LddRetVal);
      I2C_GddChaCount++;
    }
    else
    {
      /* do nothing */
    }
  }
  return (LddRetVal);
}
#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
** Syntax            : I2c_SetupEBDynamic                                    **
**                                                                           **
** Service ID        : 0x04                                                  **
**                                                                           **
** Sync/Async        : Synchronous                                           **
**                                                                           **
** Reentrancy        : Non Re-entrant                                        **
**                                                                           **
** Parameters (in)   : Channel, Address, TxDataBufferPtr, RxDataBufferPtr,   **
**                     Length                                                **
**                                                                           **
** Parameters (inout): None                                                  **
**                                                                           **
**                                                                           **
** Parameters (out)  : None                                                  **
**                                                                           **
** Return value      : Std_ReturnType                                        **
**                                                                           **
** Global Variables  : I2c_GenModuleState                                    **
**                                                                           **
** Functions invoked : None                                                  **
**                                                                           **
** Registers Used    : None                                                  **
**                                                                           **
** Description       : The service conducts a buffer setup for later         **
**                     transmission of a channel.                            **
******************************************************************************/
#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(Std_ReturnType, I2C_CODE) I2c_SetupEBDynamic(
                        VAR(I2C_ChannelType, AUTOMATIC) Channel,
                        VAR(I2C_AddressType, AUTOMATIC) Address,
                        VAR(I2C_DataConstPtrType, AUTOMATIC) TxDataBufferPtr,
                        VAR(I2C_DataPtrType, AUTOMATIC) RxDataBufferPtr,
                        VAR(I2C_DataLengthType, AUTOMATIC) Length)
{
  /*local variable is used to return I2c_SetupEBDynamic status */
  P2CONST(I2C_ChannelConfigType, AUTOMATIC, I2C_APPL_DATA) lpChannel_t;
  
  VAR(Std_ReturnType, AUTOMATIC)LddRetVal = E_NOT_OK;
  VAR(Std_ReturnType, AUTOMATIC)LddRetChValidate = 0U;

  LddRetChValidate = I2c_ChannelValidate(Channel);
  /* default status for I2c_SetupEBDynamic */
  LddRetVal = E_NOT_OK;

  /* check for module initialization */
  if (I2c_GpChannelConfigDataPtr[Channel].I2cChannelVar->I2cChannelStatus == I2C_UNINIT)
  {
    /* DET check */
    #if (I2C_DEV_ERROR_DETECT == STD_ON)

       /* Report to DET for module uninitialized */
    (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
                      I2C_SID_SETUPEBDYNAMIC,I2C_E_UNINIT);

    #endif
  }/* check for proper length parameter */
  else if ( Channel > I2c_GpConfigType->I2c_NumOfCh)
  {
    /* DET check for channel parameter */
    #if (I2C_DEV_ERROR_DETECT == STD_ON)

    (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
              I2C_SID_SETUPEBDYNAMIC,I2C_E_PARAM_CHANNEL);

    #endif
  }
  else
  {
    if (I2C_MAX_CHANNEL_ACCEPT > I2C_GddChaCount)
    {
      /* This condition will will push default data on this channel */
      if ((TxDataBufferPtr != NULL_PTR)&&(RxDataBufferPtr != NULL_PTR))
      {
		  #if (I2C_DEV_ERROR_DETECT == STD_ON)
          (void) Det_ReportError (CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
                                  I2C_SID_SETUPEBDYNAMIC, I2C_E_PARAM_TRANS_BUFFER);
          #endif
      }/* This condition will create a TX transmission on this channel */
      else if (TxDataBufferPtr != NULL_PTR)
      {
		lpChannel_t = &(I2c_GpConfigType->I2c_ChannelCfgPtr[Channel]);
		I2c_GpChannelVarDescType = lpChannel_t->I2cChannelVar;
        if((Length == I2C_NUM_0)||(Length > I2c_GpChannelVarDescType->I2cDataLength))
        {
          /* DET check for length parameter */
          #if (I2C_DEV_ERROR_DETECT == STD_ON)
          (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
                  I2C_SID_SETUPEBDYNAMIC, I2C_E_PARAM_LENGTH);
          #endif
        }
        else
        {		  
          LddRetVal = E_OK;
        }
      }/*This condition will create a RX transmission on this channel*/

      else if (RxDataBufferPtr != NULL_PTR)
      {
		lpChannel_t = &(I2c_GpConfigType->I2c_ChannelCfgPtr[Channel]);
		I2c_GpChannelVarDescType = lpChannel_t->I2cChannelVar;
        if((Length == I2C_NUM_0)||(Length > I2c_GpChannelVarDescType->I2cDataLength))
        {
          /* DET check for length parameter */
          #if (I2C_DEV_ERROR_DETECT == STD_ON)

          (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
              I2C_SID_SETUPEBDYNAMIC, I2C_E_PARAM_LENGTH);
          #endif
        }/* This is invalid condition and the service will reject the request */
        else
        {
          I2c_QueueRxDataBufferPtr(Channel, RxDataBufferPtr, Length);
          LddRetVal = E_OK;
        }
      }
      else
      {
        /* check for valid address */
        if((Address <= (I2C_AddressType)(0x08)) ||
            (Address >= (I2C_AddressType)(120)))
        {
          /* DET check */
          #if (I2C_DEV_ERROR_DETECT == STD_ON)

           /* Report to DET */
          (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
              I2C_SID_SETUPEBDYNAMIC,I2C_E_PARAM_ADDRESS);
          #endif
        }
        else
        {
          LddRetVal = E_NOT_OK;
        }
      }
      I2c_QueueChanelStatusUpdate(Channel, LddRetVal);
      I2C_GddChaCount++;
    }
  }
  return (LddRetVal);
}
#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_AsyncTransmit                                   **
 **                                                                          **
 ** Service ID[hex]    : 0x05                                                **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Sequence                                            **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : Std_ReturnType                                      **
 **                                                                          **
 ** Description        : This service is accept sequence for I2C             **
 **                      communication.                                      **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(Std_ReturnType, I2C_CODE) I2c_AsyncTransmit
        (VAR(I2C_SequenceType, AUTOMATIC) Sequence)
{
  /* local variable is used to return current API status i.e.*/
  /* I2c_AsyncTransmit*/
  VAR(Std_ReturnType, AUTOMATIC) LddRetVal = 0U;
  VAR(Std_ReturnType, AUTOMATIC) LddRetSeqValidate = 0U;
  VAR(Std_ReturnType, AUTOMATIC) LddCount = 0U;
  P2CONST(I2C_ChannelConfigType, AUTOMATIC, I2C_CONST) I2c_LpChanlConfig;
  /* default status for module I2c_AsyncTransmit */
  LddRetSeqValidate = I2c_SequenceValidate(Sequence);
  
  for (LddCount = I2C_NUM_0; LddCount < I2C_MAX_SEQUENCE_CONFIG; LddCount++)
  {
    if( I2c_GpSequenceConfigDataPtr[LddCount].I2cSequenceId == Sequence )
    {
      /* MISRA Violation: START Msg(5:0306) */
      I2c_LpChanlConfig = 
				  I2c_GpConfigType->I2c_SequenceCfgPtr[LddCount].I2c_ChannelList[0];
      /* MISRA Violation: END Msg(5:0306) */
      break;
    }
  }
  /* check for module initialization */
  if (I2c_LpChanlConfig->I2cChannelVar->I2cChannelStatus == I2C_UNINIT)
  {
    /* DET check */
    #if (I2C_DEV_ERROR_DETECT == STD_ON)

    /* Report to DET */
    (void) Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
            I2C_SID_ASYNCTRANSMIT, I2C_E_UNINIT);
    #endif
    LddRetVal = E_NOT_OK;
  }
  else if (E_NOT_OK == LddRetSeqValidate)
  {
    /* DET check for valid sequence*/
    #if (I2C_DEV_ERROR_DETECT == STD_ON)

    /* Report to DET */
    (void) Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
           I2C_SID_ASYNCTRANSMIT,I2C_E_PARAM_SEQ);

    #endif

    LddRetVal = E_NOT_OK;
    I2c_GaaSeqStatus[I2C_GddSeqCount].SequenceId = Sequence;
    I2c_LpChanlConfig->I2cChannelVar->I2cSequenceStatus = I2C_SEQ_FAILED;
  }
  else
  {
    /* Check number of sequence in Queue */
    if (I2c_GddSeqFrameCount < I2C_SEQ_QUEUED)
    {
      I2c_LpChanlConfig->I2cChannelVar->I2cChannelStatus = I2C_BUSY;
      /* sequence transmission */
      I2c_DeleteDuplicateSeqFromStatusBuf(Sequence);
      I2c_GaaSeqFrame[I2c_GddSeqFrameCount].I2c_SequenceID = Sequence;
      I2c_GddSeqFrameCount++;
      I2c_GaaSeqFrame[I2c_GddSeqFrameCount].I2c_SequenceID = INVALID_SEQUENCE;
      LddRetVal = E_OK;
      /* update current sequence status to pending */
      I2c_GaaSeqStatus[I2C_GddSeqCount].SequenceId = Sequence;
      I2c_GpChannelConfigDataPtr->I2cChannelVar->I2cSequenceStatus = I2C_SEQ_PENDING;
    }
    else
    {
      LddRetVal = E_NOT_OK;
      /* update current sequence status to Failed */
      I2c_GaaSeqStatus[I2C_GddSeqCount].SequenceId = Sequence;
      I2c_GpChannelConfigDataPtr->I2cChannelVar->I2cSequenceStatus = I2C_SEQ_FAILED;
    }
    I2C_GddSeqCount++;
  }
  return (LddRetVal);
}
#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_GetStatus                                       **
 **                                                                          **
 ** Service ID[hex]    : 0x0C 			                                     **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : None                                                **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : LenRetVal                                           **
 **                                                                          **
 ** Description        : This service is provide I2C driver.                 **
 **                                                                          **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(StatusType, I2C_CODE) I2c_GetStatus (void )
{
  /*local variable for returning module status*/
  VAR(StatusType, AUTOMATIC) LenRetVal;

  if (I2c_GenModuleState == I2C_UNINIT)
  {
    /* DET check */
    #if (I2C_DEV_ERROR_DETECT == STD_ON)
    /* Report to DET */
    (void) Det_ReportError (CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
           I2C_SID_GETSTATUS, I2C_E_UNINIT);
    #endif
    LenRetVal = I2c_GenModuleState;
  }
  else
  {
    LenRetVal = I2c_GenModuleState;
  }
  /*check for maximum channel*/
  return (LenRetVal);
}
#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_GetResult                                       **
 **                                                                          **
 ** Service ID[hex]    : 0x07                                                **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Channel                                             **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : LenRetVal                                           **
 **                                                                          **
 ** Description        : This service is provide channel status.             **
 **                                                                          **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(I2CChannelResultType, I2C_CODE)I2c_GetResult
        (VAR(I2C_ChannelType, AUTOMATIC) Channel)

{
  VAR(I2CChannelResultType, AUTOMATIC) LenRetVal;
  VAR(I2C_ChannelType, AUTOMATIC) LddChCount;
  /*default status of the sequence*/
  LenRetVal = I2C_CH_FAILED;

  /*check for module initialization*/
  if (I2c_GenModuleState == I2C_UNINIT)
  {
    /* DET check */
    #if (I2C_DEV_ERROR_DETECT == STD_ON)
    /* Report to DET */
    (void) Det_ReportError (CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
            I2C_SID_GETRESULT, I2C_E_UNINIT);
    #endif
  }/*check for maximum channel*/
  else if (E_NOT_OK == I2c_ChannelValidate(Channel))
  {
    /* DET check */
    #if (I2C_DEV_ERROR_DETECT == STD_ON)

    /* Report to DET */
    (void) Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
           I2C_SID_GETRESULT, I2C_E_PARAM_CHANNEL);
    #endif
  }
  else
  {
    for (LddChCount = I2C_NUM_0; LddChCount<I2C_GddChaCount;
         LddChCount++)
    {
      if (Channel == I2c_GaaChStatus[LddChCount].ChannelId)
      {
        LenRetVal = I2c_GaaChStatus[LddChCount].ChannelStatus;
        break;
      }
    }
  }
  return (LenRetVal);
}
#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_GetSequenceResult                               **
 **                                                                          **
 ** Service ID[hex]    : 0x08                                                **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Sequence                                            **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : LenRetVal                                           **
 **                                                                          **
 ** Description        : This service is provide sequence status.            **
 **                                                                          **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(I2C_SeqResultType, I2C_CODE)I2c_GetSequenceResult(
                        VAR(I2C_SequenceType, AUTOMATIC) Sequence)
{
  VAR(I2C_SeqResultType, AUTOMATIC)LenRetVal;
  VAR(uint8, AUTOMATIC) LddSeqCount;

  LenRetVal = I2C_SEQ_FAILED;

  if (I2c_GenModuleState == I2C_UNINIT)
  {
    /* DET check */
    #if (I2C_DEV_ERROR_DETECT == STD_ON)

    /* Report to DET */
    (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
          I2C_SID_GETSEQUENCERESULT, I2C_E_UNINIT);
    #endif
  }
  else if (E_NOT_OK == I2c_SequenceValidate(Sequence))
  {
    /* DET check */
    #if (I2C_DEV_ERROR_DETECT == STD_ON)

    /* Report to DET */
    (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
          I2C_SID_GETSEQUENCERESULT, I2C_E_PARAM_SEQ);

    #endif
  }
  else
  {
    for (LddSeqCount=I2C_NUM_0; LddSeqCount <I2C_GddSeqCount;
         LddSeqCount++)
    {
      if (Sequence == I2c_GaaSeqStatus[LddSeqCount].SequenceId)
      {
        LenRetVal =
             I2c_GaaSeqStatus[LddSeqCount].SequenceStatus;
        break;
      }
      else
      {
        /* do nothing */
      }
    }
  }
  return (LenRetVal);
}
#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_Cancel                                          **
 **                                                                          **
 ** Service ID[hex]    : 0x06                                                **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Sequence                                            **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : This service is cancel given sequence.              **
 **                                                                          **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) I2c_Cancel (VAR(I2C_SequenceType, AUTOMATIC) Sequence)
{
  VAR(I2C_SequenceType, AUTOMATIC) LddSeqCount;

  if (I2c_GpChannelConfigDataPtr->I2cChannelVar->I2cChannelStatus == I2C_UNINIT)
  {
    /* Det check */
    #if (I2C_DEV_ERROR_DETECT == STD_ON)

    /* Report to DET */
    (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
          I2C_SID_CANCEL, I2C_E_UNINIT);

    #endif
  }
  else if (E_NOT_OK == I2c_SequenceValidate(Sequence))
  {
    /* Det check */
    #if (I2C_DEV_ERROR_DETECT == STD_ON)

      /* Report to DET */
      (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
            I2C_SID_CANCEL, I2C_E_PARAM_SEQ);

    #endif
  }
  else
  {
    for (LddSeqCount = I2C_NUM_0; LddSeqCount <I2C_GddSeqCount;
         LddSeqCount++)
    {
      if (Sequence == I2c_GaaSeqStatus[I2C_GddSeqCount].SequenceId)
      {
        I2c_GpChannelConfigDataPtr->I2cChannelVar->I2cSequenceStatus = I2C_SEQ_CANCELLED;
        break;
      }
    }
    I2c_CancelSequence(Sequence);
  }
}
#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_GetVersionInfo                                  **
 **                                                                          **
 ** Service ID[hex]    : 0x02                                                 **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : VersioninfoPtr                                      **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : This service is provide version information.        **
 **                                                                          **
 *****************************************************************************/

/* enabling or disabling the version info API depending upon the
 pre-configured MACRO */
#if (I2C_VERSION_INFO_API    == STD_ON)
#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE)I2c_GetVersionInfo
        (P2VAR(Std_VersionInfoType, AUTOMATIC, I2C_VAR_PTR) VersioninfoPtr)
{
  if (NULL_PTR == VersioninfoPtr)
  {
    #if (I2C_DEV_ERROR_DETECT == STD_ON)
    /* Report to DET */
    (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
           I2C_SID_GETVERSIONINFO,I2C_E_PARAM_VINFO_PTR);
    #endif
  }
  else
  {
    /* Copy the vendor Id */
    VersioninfoPtr->vendorID = (uint16)CDDI2C_VENDOR_ID;
    /* Copy the module Id */
    VersioninfoPtr->moduleID = (uint16)CDDI2C_MODULE_ID;
    /* Copy Software Major Version */
    VersioninfoPtr->sw_major_version = CDDI2C_VERSION_SW_MAJOR_VERSION;
    /* Copy Software Minor Version */
    VersioninfoPtr->sw_minor_version = CDDI2C_VERSION_SW_MINOR_VERSION;
    /* Copy Software Patch Version */
    VersioninfoPtr->sw_patch_version = CDDI2C_VERSION_SW_PATCH_VERSION;
  }
}
#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

#endif

/******************************************************************************
 ** Service name       : I2c_SetHandling                                     **
 **                                                                          **
 ** Service ID[hex]    : 0x0B                                                **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Handling                                            **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : This service is setting I2C bus handling mode.      **
 **                                                                          **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) I2c_SetHandling(
         VAR(I2C_HandlingType, AUTOMATIC) Handling)
{
  /* Det check */

  if (I2c_GpChannelConfigDataPtr->I2cChannelVar->I2cChannelStatus == I2C_UNINIT)
  {
    #if (I2C_DEV_ERROR_DETECT == STD_ON)
    /* Report to DET */
    (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
          I2C_SID_SETHANDLING,I2C_E_UNINIT);
    #endif
  }
  else
  {
    I2c_GenHandling = Handling;
  }
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_MainFunction                                    **
 **                                                                          **
 ** Service ID[hex]    : 0x09                                                **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Handling                                            **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : This service is cyclic call, Communication will     **
 **                      happen by this service call.                        **
 **                                                                          **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(uint8, I2C_CODE) I2c_MainFunction ( void )
{
  VAR(Std_ReturnType, AUTOMATIC) LddRetVal;
  VAR(I2C_SequenceType, AUTOMATIC) LddSequenceId;
  VAR(I2C_DataLengthType, AUTOMATIC) LddCount;
  P2CONST(I2C_ChannelConfigType, AUTOMATIC, I2C_CONST) I2c_LpChanlConfig;
  
  LddSequenceId = I2c_GaaSeqFrame[I2C_NUM_0].I2c_SequenceID;
  
  for (LddCount = I2C_NUM_0; LddCount < I2C_MAX_SEQUENCE_CONFIG; LddCount++)
  {
    if( I2c_GpSequenceConfigDataPtr[LddCount].I2cSequenceId == LddSequenceId )
    {
      I2c_LpChanlConfig = 
				  I2c_GpConfigType->I2c_SequenceCfgPtr[LddCount].I2c_ChannelList[0];
	  break;
    }
  }
  
  if (I2c_LpChanlConfig->I2cChannelVar->I2cChannelStatus == I2C_UNINIT)
  {
    #if (I2C_DEV_ERROR_DETECT == STD_ON)
      /* Report to DET */
      (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
            I2C_SID_MAINFUNCTION,I2C_E_UNINIT);
    #endif
  }
  else
  {
    if (I2c_LpChanlConfig->I2cChannelVar->I2cChannelStatus == I2C_BUSY)
    {
      if (I2c_GaaSeqFrame[I2C_NUM_0].I2c_SequenceID != INVALID_SEQUENCE)
      {
        /* I2c_SequenceTransmission is called physical transmission */
        LddRetVal = I2c_SequenceTransmission();

        if (LddRetVal == E_OK)
        {
          /* It's report to called layer to successfully completion. */
          I2c_Local_SequenceEndNotification();
        }
        else
        {
          /* It's report to called layer to unsuccessful */
          I2c_Local_SequenceErrorNotification();
        }
        /* Delete last executed sequence frame */
        I2c_DeleteExcutedSeqFromQueue(I2C_NUM_0);
        /* Delete status for sequence and channel ID if buffer is full */
        DeleteChaAndSeqFromStatuBuf();
      }
    }
    else
    {
      /* do nothing */
    }
  }
  return (LddRetVal);
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_PollingModeProcessing                     		 **
 **                                                                          **
 ** Service ID[hex]    : 0x0A                                                **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Handling                                            **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : This service is cyclic call, Communication will     **
 **                      happen by this service call.                        **
 **                                                                          **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(uint8, I2C_CODE) I2c_PollingModeProcessing( void )
{
  VAR(Std_ReturnType, AUTOMATIC) LddRetVal;
	
  if (I2c_GpChannelConfigDataPtr->I2cChannelVar->I2cChannelStatus == I2C_UNINIT)
  {
    #if (I2C_DEV_ERROR_DETECT == STD_ON)
      /* Report to DET */
      (void)Det_ReportError(CDDI2C_MODULE_ID, CDDI2C_INSTANCE_ID,
            I2C_SID_POLLING_MODE_PROCESSING,I2C_E_UNINIT);
    #endif
  }
  else
  {
    LddRetVal = I2c_PollingMode();
  }
  return (LddRetVal);
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

#ifdef __cplusplus
}
#endif
/*****************************************************************************
**                             End of File                                  **
*****************************************************************************/