/* ======================================================================
 *   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.
 * ==================================================================== */

 /*****************************************************************************
**                      MISRA C Rule Violations                              **
*******************************************************************************
** MISRA C RULE VIOLATION:                                                   **
** Message       : Msg(5:0306) [I] Cast between a pointer to object and an   **
**                 integral type.                                            **
** Rule          : MISRA C:2012 Rule-11.4;                                   **
**                 REFERENCE - ISO:C90-6.3.4 Cast Operators - Semantics      **
** Justification : Typecasting is done as per the register size, to access   **
**                 hardware registers.                                       **
** Verification  : However, part of the code is verified manually and it is  **
**                 not having any impact.                                    **
** Reference     : Look for START Msg (5:0306)  and                          **
**                 END Msg (5:0306) tags in the code.                        **
**                                                                           **
******************************************************************************/

#ifdef __cplusplus
extern "C"{
#endif

/******************************************************************************
**                          Include Section                                  **
******************************************************************************/

#include "Cdd_I2c.h"
#include "Cdd_I2c_Priv.h"
#include "hw_types.h"
/******************************************************************************
**                          Macro Definitions                                **
******************************************************************************/

/******************************************************************************
**                          Version Check                                    **
******************************************************************************/

/******************************************************************************
**                      Global variables                                     **
******************************************************************************/

/*****************************************************************************
**                          Function Prototypes                             **
*****************************************************************************/
static FUNC(void, I2C_CODE) I2C_SendStopCom(void);

extern P2CONST(I2C_ConfigType, AUTOMATIC, I2C_CONST)I2c_GpConfigType;

extern P2VAR(I2c_ChannelVarDescType, AUTOMATIC, I2C_VAR_PTR) I2c_GpChannelVarDescType;
/******************************************************************************
**                      Function Definitions                                  *
******************************************************************************/
static FUNC(uint32, I2C_CODE) I2C_waitStatus(VAR(uint32, AUTOMATIC) status);
static FUNC(void, I2C_CODE) I2CSetDataCount(uint32 LBaseAddress, uint32 LData);
static FUNC(void, I2C_CODE) Send_AddrCommand(void);
static FUNC(void, I2C_CODE) Setup_StartBit(uint32 LBaseAddress);
static FUNC(void, I2C_CODE) Setup_I2cControl(VAR(I2C_TransmissionCmdType, AUTOMATIC) TransmissionType);
static FUNC(Std_ReturnType, I2C_CODE) I2C_WrBuf (VAR(uint8, AUTOMATIC) SlaveAddress,
            VAR(I2C_MemAddressType, AUTOMATIC) Eep_Address,
            P2CONST(uint16, AUTOMATIC,I2C_APL_DATA)DataBufferPtr,
            VAR(I2C_DataLengthType, AUTOMATIC) Length,
            VAR(I2C_SequenceType, AUTOMATIC) Sequence,
			VAR(I2C_DataLengthType, AUTOMATIC) CmdLength,
			VAR(I2C_TransmissionCmdType, AUTOMATIC) Dirxn);

static FUNC(Std_ReturnType, I2C_CODE) I2C_Write(uint16 Byte);
			
static FUNC(Std_ReturnType, I2C_CODE) I2C_SendAddr
            (VAR(I2C_AddressType, AUTOMATIC) SlaveAddress,
            VAR(I2C_MemAddressType, AUTOMATIC) EepAddr,
            VAR(uint8, AUTOMATIC) CmdLength,
			VAR(uint8, AUTOMATIC) Length,
			VAR(I2C_TransmissionCmdType, AUTOMATIC) Dirxn);
			
static FUNC(Std_ReturnType, I2C_CODE) I2C_Read
            (P2VAR(uint16, AUTOMATIC, I2C_APL_DATA) Byte);

static FUNC(void, I2C_CODE) I2c_CommFailFirstLevel (
            VAR(I2C_SequenceType, AUTOMATIC) SequenceId,
            VAR(I2C_TransmissionCmdType, AUTOMATIC)TransCmd);
			
static FUNC(void, I2C_CODE) I2c_CommFailSecondLevel (
            VAR(I2C_SequenceType, AUTOMATIC) SequenceId,
            VAR(uint8, AUTOMATIC) CommStatus,
            VAR(I2C_TransmissionCmdType, AUTOMATIC)TransCmd);
			
static FUNC(void, I2C_CODE) I2c_CommFailThirdLevel (
            VAR(I2C_SequenceType, AUTOMATIC) SequenceId,
            VAR(uint8, AUTOMATIC) CommStatus,
            VAR(I2C_TransmissionCmdType, AUTOMATIC)TransCmd);
			
static FUNC(void, I2C_CODE) I2c_CommStatusUpdate (
            VAR(I2C_SequenceType, AUTOMATIC) SequenceId,
            VAR(uint8, AUTOMATIC) CommStatus,
            VAR(uint8, AUTOMATIC) TerminateLevel,
            VAR(I2C_TransmissionCmdType, AUTOMATIC)TransCmd);
			
static FUNC(Std_ReturnType, I2C_CODE) I2C_WrBuf (VAR(uint8, AUTOMATIC) SlaveAddress,
            VAR(I2C_MemAddressType, AUTOMATIC) Eep_Address,
            P2CONST(uint16, AUTOMATIC,I2C_APL_DATA)DataBufferPtr,
            VAR(I2C_DataLengthType, AUTOMATIC) Length,
            VAR(I2C_SequenceType, AUTOMATIC) Sequence,
			VAR(I2C_DataLengthType, AUTOMATIC) CmdLength,
			VAR(I2C_TransmissionCmdType, AUTOMATIC) Dirxn);
			
static FUNC(Std_ReturnType, I2C_CODE) I2C_RdBuf
            (VAR(uint8, AUTOMATIC) SlaveAddress,
            VAR(I2C_MemAddressType, AUTOMATIC) Eep_Address,
            P2VAR(uint16, AUTOMATIC,I2C_VAR_PTR)DataBufferPtr,
            VAR(I2C_DataLengthType, AUTOMATIC) Length,
            VAR(I2C_SequenceType, AUTOMATIC) Sequence,
			VAR(I2C_DataLengthType, AUTOMATIC) CmdLength,
			VAR(I2C_TransmissionCmdType, AUTOMATIC) Dirxn);
			
static FUNC(void, I2C_CODE) I2c_DeleteDuplicateChFromStatusBuf
        (VAR(I2C_ChannelType, AUTOMATIC) DupliCateCh);

			
static FUNC(Std_ReturnType, I2C_CODE) I2c_Idle (void);


static FUNC(void, I2C_CODE) I2C_Stop ();

static FUNC(void, I2C_CODE) I2C_Stop_Comm ();
/******************************************************************************
 ** Service name       : I2C_waitStatus                                      **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : status                                              **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : LddRetVal                                           **
 **                                                                          **
 ** Description        : Service is waiting for change status of ICMSR.      **
 *****************************************************************************/

static FUNC(void, I2C_CODE) I2CSetDataCount(uint32 LBaseAddress, uint32 LData)
{
	(void)regWriteReadback((LBaseAddress), M_ELEVEN, M_ZERO, LData);
}

static FUNC(void, I2C_CODE) Setup_StartBit(uint32 LBaseAddress)
{
	(void)regWriteReadback((LBaseAddress), M_THIRTEEN, M_THIRTEEN, (uint32)0x1);
}

static FUNC(void, I2C_CODE) Send_AddrCommand(void)
{
  VAR(uint32, AUTOMATIC) LI2cBaseAddr;
	
  /*Get the base address based on the configurations*/
  LI2cBaseAddr = GET_I2CBaseAddress(I2c_GpConfigType);
	
  (void)regWriteReadback((LI2cBaseAddr + I2C_ICMDR), M_FIFTEEN, M_ZERO, (uint32)0x4620U);
}

static FUNC(void, I2C_CODE) Setup_I2cControl(VAR(I2C_TransmissionCmdType, AUTOMATIC) TransmissionType)
{
  VAR(uint32, AUTOMATIC) LI2cBaseAddr;  
  
  LI2cBaseAddr = GET_I2CBaseAddress(I2c_GpConfigType);
	 
  if(I2C_WRITE == TransmissionType)
  {	
	(void)regWriteReadback((LI2cBaseAddr + I2C_ICMDR), M_TEN, M_TEN, (uint32)0x1U);
  }
  
  if(I2C_READ == TransmissionType)
  {
    (void)regWriteReadback((LI2cBaseAddr + I2C_ICMDR), M_NINE, M_NINE, (uint32)0x0);
  }		
}

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

static FUNC(uint32, I2C_CODE) I2C_waitStatus
            (VAR(uint32, AUTOMATIC) status)
{
  VAR(uint32, AUTOMATIC) LulCount = I2C_POLTIMEOUT;
  VAR(uint8, AUTOMATIC) LddRetVal;

  LddRetVal = E_NOT_OK;
  while (LulCount != I2C_NUM_0)
  {
  /*Check if I2C Status Register are set*/
    if ((I2c_GpHwReg->ulRI2CICSTRReg & status) == status)
    {
      /*Check Master arbitration lost*/
      if ((I2c_GpHwReg->ulRI2CICSTRReg & I2C_MASTERSTATUS_AL) ==
           I2C_MASTERSTATUS_AL)
      {
        LddRetVal = I2C_MASTERSTATUS_AL;

      }/*Check no acknowledgment lost*/
      else if ((I2c_GpHwReg->ulRI2CICSTRReg & I2C_MASTERSTATUS_NACK) ==
                I2C_MASTERSTATUS_NACK)
      {
        LddRetVal = I2C_MASTERSTATUS_NACK;
      }
      else
      {
        LddRetVal = E_OK;
      }
      break;
	}
    LulCount--;
	
  }
  if (LulCount == I2C_NUM_0)
  {
    LddRetVal = I2C_TIME_OUT;
  }
  return (LddRetVal);
}
#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2C_Stop                                            **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : None                                                **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Service is waiting for stop communication.          **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

static FUNC(void, I2C_CODE) I2C_Stop ()
{
  VAR(uint32, AUTOMATIC) LI2cBaseAddr;

  LI2cBaseAddr = GET_I2CBaseAddress(I2c_GpConfigType);

  I2C_ENTER_CRITICAL_SEC
  /*clearing master control register for next i2c operation*/
  (void)regWriteReadback((LI2cBaseAddr + I2C_ICMDR), M_THIRTY_ONE, M_ZERO, (uint32)0x0000U);
  /*clearing master status register for next i2c operation*/
  (void)regWriteReadback((LI2cBaseAddr + I2C_ICSTR), M_THIRTY_ONE, M_ZERO, (uint32)0x0000U);
	
  I2C_EXIT_CRITICAL_SEC
}
#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
** Service name : I2C_Stop_Comm                                              **
** 																			 **
** Service ID[hex] : NA                                                      **
**                                                                           **
** Sync/Async : Synchronous                                                  **
**                                                                           **
** Reentrancy : Non Reentrant                                                **
**                                                                           **
** Parameters (in) : None                                                    **
**                                                                           **
** Parameters (inout) : None                                                 **
**                                                                           **
** Parameters (out) : None                                                   **
**                                                                           **
** Return value : None                                                       **
**                                                                           **
** Description : Service is waiting for stop communication.                  **
******************************************************************************/
#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

static FUNC(void, I2C_CODE) I2C_Stop_Comm(VAR(uint32, AUTOMATIC) LI2cBaseAddr)
{
  I2C_ENTER_CRITICAL_SEC
  /*Setting STP Bit*/
  (void)regWriteReadback((LI2cBaseAddr), M_ELEVEN, M_ELEVEN, (uint32)0x1U);
  
  //while((HW_RD_FIELD32((LI2cBaseAddr),CSL_I2C_ICMDR_STP) == 1))
  //{}

  I2C_EXIT_CRITICAL_SEC
}
#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_Idle                                            **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : None                                                **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Service is waiting for I2C bus Idle.                **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

static FUNC(Std_ReturnType, I2C_CODE) I2c_Idle (void)
{
  VAR(uint32, AUTOMATIC) LulCount;
  VAR(Std_ReturnType, AUTOMATIC) LddRetVal;

  LulCount = I2C_POLTIMEOUT * I2C_IDLE_TIME;
  LddRetVal = E_OK;
  /*Wait for Time-out*/
  while (LulCount != I2C_NUM_0)
  {
    /*If check I2C Bus is free to acquire*/
    if ((I2c_GpHwReg->ulRI2CICIMCRReg & I2C_MASTERSTATUS_SCD) ==
         I2C_NUM_0)
    {
      LddRetVal = E_OK;
      break;
	}
    LulCount--;
  }

  if ((LulCount == I2C_NUM_0) && (I2c_GenHandling == I2C_RECURRENT))
  {
	I2c_GpHwReg->ulRI2CICIMCRReg = I2c_GpHwReg->ulRI2CICIMCRReg & (uint8)(~I2C_MASTERSTATUS_SCD);
    I2C_Stop();
    LulCount = I2C_POLTIMEOUT * I2C_IDLE_TIME;
    while (LulCount != I2C_NUM_0)
    {
      /*If check I2C Bus is free to acquire*/
      if ((I2c_GpHwReg->ulRI2CICIMCRReg & I2C_MASTERSTATUS_SCD) ==
           I2C_NUM_0)
      {
        LddRetVal = E_OK;
        break;
      }
      else
      {
        /* do nothing */
      }
      LulCount--;
    }
  }
  else
  {
    /* do nothing */
  }

  if(LulCount == I2C_NUM_0)
  {
    LddRetVal = I2C_TIME_OUT;
  }
  else
  {
    /* do nothing */
  }
  return (LddRetVal);
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2C_SendStopCom                                     **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : None                                                **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Service is setting stop for I2C communication.      **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

static FUNC(void, I2C_CODE) I2C_SendStopCom (void)
{
  VAR(uint32, AUTOMATIC) LI2cBaseAddr;
	
  LI2cBaseAddr = GET_I2CBaseAddress(I2c_GpConfigType);
  
  I2C_ENTER_CRITICAL_SEC
  /*Configuring master cont register to send stop condition*/
  I2c_GpHwReg->ulRI2CICIMCRReg = (I2C_MASTERCONTROL_MDBS
           | I2C_MASTERCONTROL_MIE | I2C_MASTERCONTROL_FSB);
		   
  (void)regWriteReadback((LI2cBaseAddr+I2C_ICMDR), M_FIFTEEN, M_ZERO, (uint32)0x1800U);
  I2C_EXIT_CRITICAL_SEC
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2C_SendAddr                                        **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : SlaveAddress, EepAddr, DirTxn                       **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : LddRetVal                                           **
 **                                                                          **
 ** Description        : Service is writing I2C command and address on Bus.  **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

static FUNC(Std_ReturnType, I2C_CODE) I2C_SendAddr
            (VAR(I2C_AddressType, AUTOMATIC) SlaveAddress,
            VAR(I2C_MemAddressType, AUTOMATIC) EepAddr,
            VAR(uint8, AUTOMATIC) CmdLength,
			VAR(uint8, AUTOMATIC) Length,
			VAR(I2C_TransmissionCmdType, AUTOMATIC) Dirxn)

{
  VAR(Std_ReturnType, AUTOMATIC) LddRetVal = E_OK;
  VAR(uint32, AUTOMATIC) LI2cBaseAddr;
  VAR(uint8, AUTOMATIC) LI2cLength;
  VAR(uint8, AUTOMATIC) DirTxn;
	
  LI2cBaseAddr = GET_I2CBaseAddress(I2c_GpConfigType);
  
  if(I2C_READ == Dirxn)
  {
    LI2cLength = CmdLength;
  }
  else
  {
	LI2cLength = Length;
  }
  
  I2C_ENTER_CRITICAL_SEC
  
  /*loading slave address in master address register along
  with write operation*/
  (void)regWriteReadback((LI2cBaseAddr+I2C_ICSAR), M_EIGHT, M_ZERO, SlaveAddress);
	
  I2CSetDataCount((LI2cBaseAddr + I2C_ICCNT), LI2cLength);
	
  Send_AddrCommand();
      
  Setup_StartBit(LI2cBaseAddr + I2C_ICMDR);
  
  while((HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_ICXRDY) == 0) || (HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_BB) == 0))
  {}
 
  (void)regWriteReadback((LI2cBaseAddr+I2C_ICDXR), M_FOURTEEN, M_ZERO,(EepAddr >> 0));

  while(HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_ICXRDY) == 0)
  {}
	
  if(CmdLength == I2C_NUM_2)
  {
	  (void)regWriteReadback((LI2cBaseAddr+I2C_ICDXR), M_FOURTEEN, M_ZERO, (uint8)(EepAddr >> 8));
	  
	  
	  while(HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_ICXRDY) == 0)
	  {}
  }
  I2C_EXIT_CRITICAL_SEC
  
  return (LddRetVal);
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2C_Write                                           **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : byte                                                **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : LddRetVal                                           **
 **                                                                          **
 ** Description        : Service is writing data on I2C Bus.                 **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"


static FUNC(Std_ReturnType, I2C_CODE) I2C_Write(uint16 Byte)
{
  VAR(uint32, AUTOMATIC) LI2cBaseAddr;
  VAR(uint8, AUTOMATIC) LddRetVal = E_OK;
	
  LI2cBaseAddr = GET_I2CBaseAddress(I2c_GpConfigType);
  I2C_ENTER_CRITICAL_SEC
  /*load the byte to transmit in master transmit register*/
  
  (void)regWriteReadback((LI2cBaseAddr+I2C_ICDXR), M_SIX, M_ZERO, (uint8)Byte);
  
  while(HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_ICXRDY) == 0)
  {}
 
  I2C_EXIT_CRITICAL_SEC
  
  return LddRetVal;
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2C_Read                                            **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Byte                                                **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : LddRetVal                                           **
 **                                                                          **
 ** Description        : Service is reading data from I2C Bus.               **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

static FUNC(Std_ReturnType, I2C_CODE) I2C_Read
            (P2VAR(uint16, AUTOMATIC, I2C_APL_DATA) Byte)
{
  VAR(Std_ReturnType, AUTOMATIC) LddRetVal = E_OK;
  VAR(uint32, AUTOMATIC) LI2cBaseAddr;
	
  LI2cBaseAddr = GET_I2CBaseAddress(I2c_GpConfigType);
 
  *Byte = (uint8)HW_RD_REG32_RAW(LI2cBaseAddr+I2C_ICDRR);

  return(LddRetVal);
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"
/******************************************************************************
 ** Service name       : I2c_CommFailFirstLevel                              **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : SequenceId, CommStatus, TransCmd                    **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Service is updating sequence and channel status.    **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

static FUNC(void, I2C_CODE) I2c_CommFailFirstLevel (
            VAR(I2C_SequenceType, AUTOMATIC) SequenceId,
            VAR(I2C_TransmissionCmdType, AUTOMATIC)TransCmd)
{
  if (I2c_GddCancelSeq == SequenceId)
  {
    I2c_UpdateSeqStatus(SequenceId, I2C_COM_CANCELED);
    /* status update for and channel */
    if (TransCmd == I2C_READ)
    {
      
    }
    else
    {
      
    }
  }
  else
  {
    I2c_UpdateSeqStatus(SequenceId, I2C_TIME_OUT);
    /* status update for and channel */
    if (TransCmd == I2C_READ)
    {
     
    }
    else
    {
      
    }
  } 
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"
/******************************************************************************
 ** Service name       : I2c_CommFailThirdLevel                              **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : SequenceId, CommStatus, TransCmd                    **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Service is updating sequence and channel status.    **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

static FUNC(void, I2C_CODE) I2c_CommFailSecondLevel (
            VAR(I2C_SequenceType, AUTOMATIC) SequenceId,
            VAR(uint8, AUTOMATIC) CommStatus,
            VAR(I2C_TransmissionCmdType, AUTOMATIC)TransCmd)
{
  VAR(uint8, AUTOMATIC) LucCommStatus;
  LucCommStatus = I2C_NUM_0;
  if (CommStatus == I2C_MASTERSTATUS_SCD)
  {
    LucCommStatus = I2C_ARBITRATION;
    I2c_UpdateSeqStatus(SequenceId, LucCommStatus);
    /* status update for and channel */
    
  }
  else if (CommStatus == I2C_MASTERSTATUS_AAS)
  {
    LucCommStatus = I2C_ADDR_NAK;
    I2c_UpdateSeqStatus(SequenceId, LucCommStatus);
  }
  else if (CommStatus == I2C_TIME_OUT)
  {
    LucCommStatus = I2C_TIME_OUT;
    I2c_UpdateSeqStatus(SequenceId, LucCommStatus);
  }
  else
  {
    /* do nothing */
  }

  if (I2c_GddCancelSeq == SequenceId)
  {
    if(LucCommStatus != I2C_NUM_0)
    {
      /* Do Nothing */
    }
    else
    {
      I2c_UpdateSeqStatus(SequenceId, I2C_COM_CANCELED);
    }
    if (TransCmd == I2C_READ)
    {
      
    }
    else
    {
      
    }
  }
  else
  {
    if (TransCmd == I2C_READ)
    {
      
    }
    else
    {
      
    }
  }
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_CommStatusUpdate                                **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : SequenceId, CommStatus, TerminateLevel, TransCmd    **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Service is updating sequence and channel status.    **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

static FUNC(void, I2C_CODE) I2c_CommFailThirdLevel (
            VAR(I2C_SequenceType, AUTOMATIC) SequenceId,
            VAR(uint8, AUTOMATIC) CommStatus,
            VAR(I2C_TransmissionCmdType, AUTOMATIC)TransCmd)
{
  VAR(uint8, AUTOMATIC) LucCommStatus;
  LucCommStatus = I2C_NUM_0;
  /* status update for and channel */

  if(CommStatus == I2C_TXNAK)
  {
    LucCommStatus = I2C_TXNAK;
    I2c_UpdateSeqStatus(SequenceId, LucCommStatus);
  }
  else if (CommStatus == I2C_TIME_OUT)
  {
    LucCommStatus = I2C_TIME_OUT;
    I2c_UpdateSeqStatus(SequenceId, LucCommStatus);
  }
  else
  {
    /* Do Nothing */
  }

  if (I2c_GddCancelSeq == SequenceId)
  {
    if(LucCommStatus != I2C_NUM_0)
    {
      /* Do Nothing */
    }
    else
    {
      I2c_UpdateSeqStatus(SequenceId, I2C_COM_CANCELED);
    }
    if (TransCmd == I2C_READ)
    {

    }
    else
    {

    }
  }
  else
  {
    I2c_UpdateSeqStatus(SequenceId, I2C_TIME_OUT);

    if (TransCmd == I2C_READ)
    {

    }
    else
    {

    }
  }
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"
/******************************************************************************
 ** Service name       : I2c_CommStatusUpdate                                **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : SequenceId, CommStatus, TerminateLevel, TransCmd    **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Service is updating sequence and channel status.    **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

static FUNC(void, I2C_CODE) I2c_CommStatusUpdate (
            VAR(I2C_SequenceType, AUTOMATIC) SequenceId,
            VAR(uint8, AUTOMATIC) CommStatus,
            VAR(uint8, AUTOMATIC) TerminateLevel,
            VAR(I2C_TransmissionCmdType, AUTOMATIC)TransCmd)
{
  if (TerminateLevel == I2C_NUM_1)
  {
    I2c_CommFailFirstLevel (SequenceId, TransCmd);
  }
  else if (TerminateLevel == I2C_NUM_2)
  {
    I2c_CommFailSecondLevel (SequenceId, CommStatus, TransCmd);
  }
  else if (TerminateLevel == I2C_NUM_3)
  {
    I2c_CommFailThirdLevel (SequenceId, CommStatus, TransCmd);
  }
  else
  {
    /* status update for and channel */

    if (TransCmd == I2C_READ)
    {

    }
    else
    {
      
    }
    I2c_UpdateSeqStatus (SequenceId, I2C_NUM_0);
  }
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2C_WrBuf                                           **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : SlaveAddress, Eep_Address, DataBufferPtr, Length,   **
 **                      Sequence                                            **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : LddRetVal                                           **
 **                                                                          **
 ** Description        : Service is used for write string data on the bus.   **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"
static FUNC(Std_ReturnType, I2C_CODE) I2C_WrBuf (VAR(uint8, AUTOMATIC) SlaveAddress,
            VAR(I2C_MemAddressType, AUTOMATIC) Eep_Address,
            P2CONST(uint16, AUTOMATIC,I2C_APL_DATA)DataBufferPtr,
            VAR(I2C_DataLengthType, AUTOMATIC) Length,
            VAR(I2C_SequenceType, AUTOMATIC) Sequence,
			VAR(I2C_DataLengthType, AUTOMATIC) CmdLength,
			VAR(I2C_TransmissionCmdType, AUTOMATIC) Dirxn)
{
  VAR(Std_ReturnType, AUTOMATIC) LddRetVal;
  VAR(uint8, AUTOMATIC) LucSteps;
  VAR(I2C_DataLengthType, AUTOMATIC) LddCount ;
  VAR(I2C_DataLengthType, AUTOMATIC)  LddLastByteNum;
  P2CONST(uint16, AUTOMATIC,I2C_APL_DATA) LpTempPtr;
  VAR(uint32, AUTOMATIC) LBaseAddress = 0U;
	
  LBaseAddress = GET_I2CBaseAddress(I2c_GpConfigType);
  
  LddRetVal = E_NOT_OK;
  
  LddLastByteNum = Length;
  LucSteps = I2C_NUM_0;
  LpTempPtr = DataBufferPtr;

  do
  {
    switch(LucSteps)
    {
      case I2C_CHK_BUS_FREE:
      {
        /*checking whether i2c bus is ideal */
        LddRetVal = I2c_Idle();
        break;
      }
      case I2C_SEND_ADDRESS:
      {
        /*Send I2C address over I2C bus*/
        LddRetVal = I2C_SendAddr(SlaveAddress,Eep_Address,CmdLength, Length, Dirxn);
        break;
      }
      case I2C_PERFORM_WRITE:
      {
	    Setup_I2cControl(I2C_WRITE);
        /*Send data byte to write to data over I2C bus to write on chip*/
        for (LddCount=I2C_NUM_0; LddCount<(Length-CmdLength); LddCount++)
        {
          if(LddLastByteNum == (LddCount + CmdLength))
          {
              
          }
		  else
		  {
            LddRetVal = I2C_Write(LpTempPtr[LddCount]);
		  }
		
          if ((LddLastByteNum == (LddCount + CmdLength)) &&
              (LddRetVal != I2C_TXNAK))
          { 
            LddRetVal = E_OK;          
          }
          else if (I2c_GddCancelSeq == Sequence)
          {
            LddRetVal = I2C_COM_CANCELED;
          }
          else
          {
            /* Do Nothing */
          }
          if (LddRetVal != E_OK)
          {
            break;
          }
        }
        /*send stop condition on I2C */
        break;
      }
      case I2C_MONITER_STOP:
      {
        /*monitor whether stop is successfully transmitted on i2c*/
       while((HW_RD_FIELD32((LBaseAddress + I2C_ICSTR),CSL_I2C_ICSTR_ICXRDY) == 0))
       {}
       
		I2C_Stop_Comm(LBaseAddress+I2C_ICMDR);
		
		I2CSetDataCount((LBaseAddress + I2C_ICCNT), 0);
      }
      default:
        break;
    }
    
	LucSteps++;

    if (LddRetVal != E_OK)
    {
      /*send stop condition on I2C */
      I2C_Stop_Comm(LBaseAddress+I2C_ICMDR);
       
 	  /*monitor whether stop is successfully transmitted on i2c*/
      I2C_Stop();
      /*changing job result as job failed*/
    }
    else if (I2c_GddCancelSeq == Sequence)
    {
      LddRetVal = I2C_COM_CANCELED;
      if (LucSteps != I2C_NUM_1)
      {
        /*send stop condition on I2C */
        I2C_Stop_Comm(LBaseAddress+I2C_ICMDR);
        /*monitor whether stop is successfully transmitted on i2c*/
        I2C_Stop();
      }
    }
    else
    {
      /* do nothing */
    }
  }
  while((LddRetVal == E_OK) &&(LucSteps < I2C_NUM_4));

  I2c_CommStatusUpdate(Sequence,LddRetVal, LucSteps, I2C_WRITE);
  
  return(LddRetVal);
}


#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2C_RdBuf                                           **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : SlaveAddress, Eep_Address, DataBufferPtr, Length,   **
 **                      Sequence                                            **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : LddRetVal                                           **
 **                                                                          **
 ** Description        : Service is used for Read string data from the bus.  **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(Std_ReturnType, I2C_CODE) I2C_RdBuf
            (VAR(uint8, AUTOMATIC) SlaveAddress,
            VAR(I2C_MemAddressType, AUTOMATIC) Eep_Address,
            P2VAR(uint16, AUTOMATIC,I2C_VAR_PTR)DataBufferPtr,
            VAR(I2C_DataLengthType, AUTOMATIC) Length,
            VAR(I2C_SequenceType, AUTOMATIC) Sequence,
			VAR(I2C_DataLengthType, AUTOMATIC) CmdLength,
			VAR(I2C_TransmissionCmdType, AUTOMATIC) Dirxn)

{
  VAR(Std_ReturnType, AUTOMATIC) LddRetVal ;
  VAR(I2C_DataLengthType, AUTOMATIC) LddLastByteNum;
  VAR(uint8, AUTOMATIC) LucSteps;
  P2VAR(uint16, AUTOMATIC,I2C_VAR_PTR) LpTempPtr;
  VAR(I2C_DataLengthType, AUTOMATIC) LddCount;
  VAR(uint32, AUTOMATIC) LBaseAddress = 0U;
	
  LBaseAddress = GET_I2CBaseAddress(I2c_GpConfigType);

  LddLastByteNum = Length;
  LddRetVal = E_NOT_OK;

  LucSteps = I2C_NUM_0;
  LpTempPtr = DataBufferPtr;

  do
  {
    /*following the stepwise approach to complete read operation*/
    switch(LucSteps)
    {
      case I2C_CHK_BUS_FREE:
      {
        /*checking whether i2c bus is ideal for next operation*/
        LddRetVal = I2c_Idle();
        break;
      }
      case I2C_SEND_ADDRESS:
      {
        /*sending chip address and memory location to read on i2c*/
        LddRetVal = I2C_SendAddr(SlaveAddress,Eep_Address,CmdLength, Length, Dirxn);
        break;
      }
      case I2C_PERFORM_READ:
      {		
	    I2CSetDataCount((LBaseAddress + I2C_ICCNT), (Length-CmdLength));
		
		Setup_I2cControl(I2C_READ);
		
		Setup_StartBit(LBaseAddress+I2C_ICMDR);
	
        /*reading all the bytes from the eeprom chip on i2c*/
        for (LddCount = I2C_NUM_0; LddCount<(Length-CmdLength); LddCount++)
        {		  
          if (LddCount < ((Length-CmdLength) - 1))
          { 
            while(HW_RD_FIELD32((LBaseAddress + I2C_ICSTR),CSL_I2C_ICSTR_RSFULL) == 0)
            {}
          }
		  else
		  {
			while(HW_RD_FIELD32((LBaseAddress + I2C_ICSTR),CSL_I2C_ICSTR_ARDY) == 0)
            {}
		  }
          
		  LpTempPtr[LddCount] = (uint8)HW_RD_REG32_RAW(LBaseAddress+I2C_ICDRR);
		  
          if (I2c_GddCancelSeq == Sequence)
          {
            LddRetVal = I2C_COM_CANCELED;
          }
        }
        break;
      }
      case I2C_MONITER_STOP:
      {
		(void)regWriteReadback((LBaseAddress+I2C_ICMDR), M_FIFTEEN, M_FIFTEEN, (uint32)0x0);
        /*monitor whether stop is successfully transmitted on i2c*/
		I2C_Stop_Comm(LBaseAddress+I2C_ICMDR);
		
		while((HW_RD_FIELD32((LBaseAddress + I2C_ICSTR),CSL_I2C_ICSTR_BB) == 1) && (HW_RD_FIELD32((LBaseAddress + I2C_ICSTR),CSL_I2C_ICSTR_SCD) == 0) )
        {}
        break;
      }
      default:
        break;
    }
    LucSteps++;
    if (LddRetVal != E_OK)
    {
      /*send stop condition on I2C */
      I2C_Stop_Comm(LBaseAddress+I2C_ICMDR);
      /*monitor whether stop is successfully transmitted on i2c*/
      /*making status as not ok*/
    }
    else if (I2c_GddCancelSeq == Sequence)
    {
      LddRetVal = I2C_COM_CANCELED;
      if (LucSteps != I2C_NUM_1)
      {
        /*send stop condition on I2C */
        I2C_Stop_Comm(LBaseAddress+I2C_ICMDR);
        /*monitor whether stop is successfully transmitted on i2c*/
      }
      /*making status as not ok*/
    }
    else
    {
      /* do nothing */
    }
  }
  while((LddRetVal == E_OK) && (LucSteps < I2C_NUM_4));

  I2c_CommStatusUpdate(Sequence,LddRetVal, LucSteps, I2C_READ);
  return (LddRetVal);
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_SequenceTransmission                            **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : None                                                **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : LddRetVal                                           **
 **                                                                          **
 ** Description        : Service is calling communication service for I2C.   **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(Std_ReturnType, I2C_CODE) I2c_SequenceTransmission()
{
  P2CONST(I2C_ChannelConfigType, AUTOMATIC, I2C_CONST) I2c_LpChanlConfig;
  I2C_DataPtrType LpTxRxbufptr;
  VAR(Std_ReturnType, AUTOMATIC) LddRetVal;
  VAR(I2C_SequenceType, AUTOMATIC) LddSequenceId;
  VAR(I2C_MemAddressType, AUTOMATIC) LddMemaddr;
  VAR(I2C_AddressType, AUTOMATIC) LddSlaveAddr;
  VAR(I2C_DataLengthType, AUTOMATIC) LddLength;
  VAR(I2C_DataLengthType, AUTOMATIC) LddCount;
  VAR(I2C_TransmissionCmdType, AUTOMATIC)LenTransCmd;
  VAR(I2C_DataLengthType, AUTOMATIC)LddCommandLength;
  VAR(I2C_DataLengthType, AUTOMATIC) LddSeqCount;

  LddRetVal = E_NOT_OK;

  LddMemaddr = I2C_NUM_0;
 
  /* finding the sequence ID for execution */
  
  I2c_GpSequenceConfigDataPtr = I2c_GpConfigType->I2c_SequenceCfgPtr;
  
  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 )
    {
      /* MISRA Violation: START Msg(5:0306) */
      I2c_GpHwReg =
			(I2c_HardwareConfigType*)I2c_GpHardwarewReg[LddCount].
            hwUnitId;
      /* MISRA Violation: END Msg(5:0306) */
      break;
    }
  }

  LddSeqCount = LddCount; 
   
   for (LddCount = I2C_NUM_0; LddCount < I2c_GpSequenceConfigDataPtr[LddSeqCount].I2cNoofChannels; LddCount++)
   {
	   I2c_LpChanlConfig = 
				  I2c_GpConfigType->I2c_SequenceCfgPtr[LddSeqCount].I2c_ChannelList[LddCount];
				  
	    LenTransCmd = I2c_LpChanlConfig->I2cChannelVar->I2cChannelDirection;
				  
		LddSlaveAddr = I2c_LpChanlConfig->I2cChannelVar->I2cSlaveAddress;
		
		LddLength = I2c_LpChanlConfig->I2cChannelVar->I2cDataLength;
		
		LpTxRxbufptr = I2c_LpChanlConfig->I2cChannelVar->I2cDataBufferPtr;
		
		if(LddSlaveAddr == 0x50)
	    {
		  LddCommandLength = I2C_NUM_2;
	    }
	    else if(LddSlaveAddr == 0x20)
	    {
		  LddCommandLength = I2C_NUM_1;
	    }
	    else
	    {
		  LddCommandLength = I2C_NUM_0;
	    }
		
		if (LddCommandLength == I2C_NUM_1)
		{
		  LddMemaddr = (LpTxRxbufptr[I2C_NUM_0]);
		}
		else if (LddCommandLength == I2C_NUM_4)
		{
		  /* do nothing */
		}
		else
		{
		  LddMemaddr = LpTxRxbufptr[I2C_NUM_0];
		  LddMemaddr = (I2C_MemAddressType)(LddMemaddr<<I2C_NUM_8);
		  LddMemaddr |= LpTxRxbufptr[I2C_NUM_1];
		}
		
		LpTxRxbufptr = &LpTxRxbufptr[LddCommandLength];
   
	  if (LenTransCmd == I2C_READ)
	  {		
			/* service is called for Read operation */
			LddRetVal = I2C_RdBuf(LddSlaveAddr, LddMemaddr,
						LpTxRxbufptr,
						LddLength,
						LddSequenceId, LddCommandLength, I2C_READ);
	  }
	  else if (LenTransCmd == I2C_WRITE)
	  {
			/* service is called for write operation */
		  LddRetVal= I2C_WrBuf(LddSlaveAddr, LddMemaddr,
						LpTxRxbufptr,
						LddLength,
						LddSequenceId, LddCommandLength, I2C_WRITE);
	  }
	  else
	  {
		/* do nothing */
	  }
   }
  return LddRetVal;
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_DeleteDuplicateSeqFromStatusBuf                 **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : DupliCateSeq                                        **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Function is handling duplicate Sequence.            **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) I2c_DeleteDuplicateSeqFromStatusBuf
            (VAR(I2C_SequenceType, AUTOMATIC) DupliCateSeq)
{
  VAR(I2C_DataLengthType, AUTOMATIC) LddCount;
  /* find and delete duplicate sequence */
  for (LddCount = I2C_NUM_0; LddCount<I2C_GddSeqCount; LddCount++)
  {
    /* checking the duplicate sequence in status buffer */
    if (I2c_GaaSeqStatus[LddCount].SequenceId == DupliCateSeq)
    {
      break;
    }
  }
  if (I2c_GaaSeqStatus[LddCount].SequenceId == DupliCateSeq)
  {
    for(LddCount = I2C_NUM_0; LddCount<I2C_GddSeqCount; LddCount++)
    {
      I2c_GaaSeqStatus[LddCount] = I2c_GaaSeqStatus[LddCount+I2C_NUM_1];
    }
    I2C_GddSeqCount--;
  }
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_DeleteExcutedSeqFromQueue                       **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : SeqQueueNum                                         **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Function is deleting Sequence from Sequence buffer. **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) I2c_DeleteExcutedSeqFromQueue
        (VAR(I2C_DataLengthType, AUTOMATIC) SeqQueueNum)
{
  VAR(I2C_DataLengthType, AUTOMATIC) LddCount;
  /* deleting the last executed sequence frame from queue */
  if (I2c_GaaSeqFrame[SeqQueueNum].I2c_SequenceID == I2c_GddCancelSeq)
  {
    I2c_GddCancelSeq = I2C_NUM_0;
  }

  for (LddCount = SeqQueueNum; LddCount<I2c_GddSeqFrameCount; LddCount++)
  {
    I2c_GaaSeqFrame[LddCount] = I2c_GaaSeqFrame[LddCount+I2C_NUM_1];
  }

  I2c_GddSeqFrameCount--;
  if ((I2c_GddCancelSeq != I2C_NUM_0) && (I2c_GddSeqFrameCount != I2C_NUM_0))
  {
    I2c_UpdateSeqStatus(I2c_GddCancelSeq, I2C_COM_CANCELED);
    for (LddCount = I2C_NUM_0; LddCount<I2c_GddSeqFrameCount; LddCount++)
    {
      if (I2c_GaaSeqFrame[LddCount].I2c_SequenceID == I2c_GddCancelSeq)
      {
        break;
      }
    }
    for(; LddCount<I2c_GddSeqFrameCount; LddCount++)
    {
      I2c_GaaSeqFrame[LddCount] = I2c_GaaSeqFrame[LddCount+I2C_NUM_1];
        I2c_GaaSeqFrame[LddCount] = I2c_GaaSeqFrame[LddCount+I2C_NUM_1];
    }
    I2c_GddCancelSeq = 0;
    I2c_GddSeqFrameCount--;
  }
  /* if queue is empty , module shall be set as IDLE */
  if (I2c_GddSeqFrameCount == I2C_NUM_0)
  {
    I2c_GenModuleState = I2C_IDLE;
  }
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_DeleteDuplicateChFromStatusBuf                  **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : DupliCateCh                                         **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Function is handling duplicate Channel.             **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

static FUNC(void, I2C_CODE) I2c_DeleteDuplicateChFromStatusBuf
        (VAR(I2C_ChannelType, AUTOMATIC) DupliCateCh)
{
  VAR(I2C_DataLengthType, AUTOMATIC) LddCount;
  /* find and delete duplicate channel */
  for (LddCount = I2C_NUM_0; LddCount < I2C_GddChaCount; LddCount++)
  {
    /* checking the duplicate channel buffer */
    if (I2c_GaaChStatus[LddCount].ChannelId == DupliCateCh)
    {
      break;
    }
  }

  if (I2c_GaaChStatus[LddCount].ChannelId == DupliCateCh)
  {
    for(; LddCount<I2C_GddChaCount; LddCount++)
    {
      I2c_GaaChStatus[LddCount] = I2c_GaaChStatus[LddCount + I2C_NUM_1];
    }
    I2C_GddChaCount--;
  }
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : DeleteChaAndSeqFromStatuBuf                         **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : DupliCateCh                                         **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Function is used to delete Sequence from            **
 **                      I2c_GaaSeqStatus buffer                             **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) DeleteChaAndSeqFromStatuBuf(void)
{
  VAR(I2C_DataLengthType, AUTOMATIC) LddSeqCount;
  VAR(I2C_SequenceType, AUTOMATIC) LddSeqIdDelete;
  VAR(I2C_ChannelType, AUTOMATIC) LddChCount;
  VAR(I2C_ChannelType, AUTOMATIC) LddNumChDelete;
  VAR(I2C_TransmissionCmdType, AUTOMATIC)LenTransCmd;
  /* // VAR(I2C_SequenceType, AUTOMATIC) LddGlobalSeqCount; */
  LddNumChDelete = I2C_NUM_0;
  /* checking the sequence buffer is full? */

  if (I2C_SEQ_QUEUED < I2C_GddSeqCount)
  {
    LddSeqIdDelete = I2c_GaaSeqStatus[I2C_NUM_0].SequenceId;
    for (LddSeqCount = I2C_NUM_0; LddSeqCount < I2C_GddSeqCount;
          LddSeqCount++)
    {
      I2c_GaaSeqStatus [LddSeqCount]
        = I2c_GaaSeqStatus[LddSeqCount + I2C_NUM_1];
	}
    I2C_GddSeqCount--;
    /* deleting the sequence status from status buffer */
    for (LddSeqCount =I2C_NUM_0; LddSeqCount < I2C_MAX_SEQUENCE_CONFIG;
          LddSeqCount++)
    {
      if (I2c_GpSequenceConfigDataPtr[LddSeqCount].I2cSequenceId ==
           LddSeqIdDelete)
      {
        /* deleting the channel status from status buffer */
        for (LddChCount =I2C_NUM_0; LddChCount<I2C_GddChaCount;
                   LddChCount++)
        {
          I2c_GaaChStatus[LddChCount] =
                      I2c_GaaChStatus[LddChCount+LddNumChDelete];
        }
        break;
      }
    }
    I2C_GddChaCount = I2C_GddChaCount - LddNumChDelete;
  }
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_UpdateSeqStatus                                 **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Sequence, SeqStat                                   **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Function is updating Sequence into I2c_GaaSeqStatus **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) I2c_UpdateSeqStatus
                     (VAR(I2C_SequenceType, AUTOMATIC) Sequence,
                     VAR(uint8, AUTOMATIC) SeqStat)
{
  VAR(I2C_SequenceType, AUTOMATIC) LddSeqcount;
  /* updating the sequence status */
  for (LddSeqcount=I2C_NUM_0; LddSeqcount<I2C_GddSeqCount; LddSeqcount++)
  {
    if (Sequence == I2c_GaaSeqStatus[LddSeqcount].SequenceId)
    {
      if((SeqStat == I2C_ADDR_NAK) || (SeqStat == I2C_TXNAK))
      {
        I2c_GaaSeqStatus[LddSeqcount].SequenceStatus = I2C_SEQ_NACK;
      }
      else if (SeqStat == I2C_NUM_0)
      {
        I2c_GaaSeqStatus[LddSeqcount].SequenceStatus = I2C_SEQ_OK;
      }
      else if (SeqStat == I2C_COM_CANCELED)
      {
        I2c_GaaSeqStatus[LddSeqcount].SequenceStatus =
                   I2C_SEQ_CANCELLED;

      }
      else
      {
        I2c_GaaSeqStatus[LddSeqcount].SequenceStatus =
                   I2C_SEQ_FAILED;
      }
    }
  }
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_UpdateSeqStatus                                 **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Channel, ChStat                                     **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Function is updating channel into I2c_GaaChStatus.  **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) I2c_UpdateChannelStatus
                     (VAR(I2C_ChannelType, AUTOMATIC) Channel,
                     VAR(uint8, AUTOMATIC) ChStat)
{
  VAR(I2C_ChannelType, AUTOMATIC) LddChCount;
  /* updating the channel status */
  for (LddChCount=I2C_NUM_0; LddChCount<I2C_GddChaCount; LddChCount++)
  {
    if (Channel == I2c_GaaChStatus[LddChCount].ChannelId)
    {
      if (ChStat == I2C_NUM_0)
      {
        I2c_GaaChStatus[LddChCount].ChannelStatus = I2C_CH_RESULT_OK;
      }
      else if (ChStat == I2C_NUM_1)
      {
        I2c_GaaChStatus[LddChCount].ChannelStatus = I2C_CH_PENDING;
      }
      else if (ChStat == I2C_COM_CANCELED)
      {
        I2c_GaaChStatus[LddChCount].ChannelStatus = I2C_CHL_CANCELED;
      }
      else if (ChStat == I2C_COM_FAIL)
      {
        I2c_GaaChStatus[LddChCount].ChannelStatus = I2C_CH_FAILED;
      }
      else if (ChStat == I2C_TIME_OUT)
      {
        I2c_GaaChStatus[LddChCount].ChannelStatus = I2C_CH_RESULT_BUSFAIL;
      }
      else if (ChStat == I2C_ARBITRATION)
      {
        I2c_GaaChStatus[LddChCount].ChannelStatus = I2C_CH_RESULT_ARBFAIL;
      }
      else if (ChStat == I2C_ADDR_NAK)
      {
        I2c_GaaChStatus[LddChCount].ChannelStatus =
                    I2C_CH_RESULT_NACKFAIL;
      }
      else if (ChStat == I2C_TXNAK)
      {
        I2c_GaaChStatus[LddChCount].ChannelStatus =
                        I2C_CH_RESULT_NACKFAIL;
      }
      else
      {
        /* do nothing */
      }
    }
  }
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_ChannelValidate                                 **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Channel                                             **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Function is use to validate input channel.          **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(Std_ReturnType, I2C_CODE)I2c_ChannelValidate
                              (VAR(I2C_ChannelType, I2C_VAR) Channel)
{
  VAR(Std_ReturnType, I2C_VAR)LddRet_validate = E_NOT_OK;
  P2CONST(I2C_ChannelConfigType, AUTOMATIC, I2C_CONST) LKst_ch_dataptr;
  VAR(I2C_ChannelType, I2C_VAR)LddCount;

  LKst_ch_dataptr = I2c_GpChannelConfigDataPtr;

  /* finding the given channel is correct or not */
  for (LddCount=I2C_NUM_0; LddCount<I2C_MAX_CHANNEL_CONFIG; LddCount++)
  {
    if ((LKst_ch_dataptr[LddCount].I2cChannelId) == Channel)
    {
      break;
    }
  }
  if (LddCount < I2C_MAX_CHANNEL_CONFIG)
  {
    LddRet_validate = E_OK;
  }
  else
  {
    LddRet_validate = E_NOT_OK;
  }
  return (LddRet_validate);
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_SequenceValidate                                **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Sequence                                            **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : LddRet_validate                                     **
 **                                                                          **
 ** Description        : Function is use to validate input sequence.         **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(Std_ReturnType, I2C_CODE)I2c_SequenceValidate
                              (VAR(I2C_SequenceType, I2C_VAR) Sequence)
{
  VAR(Std_ReturnType, I2C_VAR)LddRet_validate;
  P2CONST(I2C_SequenceConfigType, AUTOMATIC, I2C_CONST) LpSeqDataptr;
  VAR(I2C_ChannelType, I2C_VAR)LddCount;

  LpSeqDataptr = I2c_GpConfigType->I2c_SequenceCfgPtr;

  /* finding the given sequence is correct or not */
  for (LddCount=I2C_NUM_0; LddCount<I2C_MAX_SEQUENCE_CONFIG; LddCount++)
  {
    if((LpSeqDataptr[LddCount].I2cSequenceId) == Sequence)
    {
      break;
	}
  }
  if (LddCount < I2C_MAX_SEQUENCE_CONFIG)
  {
    LddRet_validate = E_OK;
  }
  else
  {
    LddRet_validate = E_NOT_OK;
  }
  return (LddRet_validate);
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_InitHw                                          **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : I2c_HardwareConfigPtr                               **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : LddRet_validate                                     **
 **                                                                          **
 ** Description        : Function is use to initialize I2C Engine.           **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) I2c_InitHw(P2CONST(I2C_ConfigType, AUTOMATIC, I2C_CONST) LpConfigPtr)
{
  VAR(uint8, AUTOMATIC) LucCount = 0U;
  VAR(uint32, AUTOMATIC) LI2cBaseAddr = 0U;
  VAR(uint8, AUTOMATIC) LI2cDivisor = 0U;
  VAR(uint8, AUTOMATIC) regval = 0U;
  VAR(uint8, AUTOMATIC) LPrescaleValue = 0U;
  VAR(uint32, AUTOMATIC) LBitRateValue = 0U;
  VAR(uint32, AUTOMATIC) LNodeAddress = 0U;  
  VAR(uint32, AUTOMATIC) outputClk = 0U;  
  VAR(uint32, AUTOMATIC) internalClk = 0U;    
   
  LI2cBaseAddr = GET_I2CBaseAddress(LpConfigPtr);
  
  /*I2C h/w initialization as per I2C Programming guide and Base code of MCU+SDK*/
  /*Putting the I2C module in reset mode*/
  (void)regWriteReadback((LI2cBaseAddr + I2C_ICMDR), M_FIVE, M_FIVE, (uint32)0x0U);
   
  /*Getting the Base Address*/
  switch(LpConfigPtr->I2c_HwCfgPtr->hwBitRate)
  {
    case I2C_100kHz:
	{
	  outputClk = 100000U;
	  internalClk = I2C_MODULE_INTERNAL_CLK_4MHZ;
      break;
	}
	case I2C_400kHz:
	{
	  outputClk = 400000U;
	  internalClk = I2C_MODULE_INTERNAL_CLK_12MHZ;
	  break;
	}
	case I2C_1P0Mhz:
	{
	  outputClk = 1000000U;
	  internalClk = I2C_MODULE_INTERNAL_CLK_12MHZ;
	  break;
	}

	case I2C_3P4Mhz:
	{
	  outputClk = 3400000U;
	  internalClk = I2C_MODULE_INTERNAL_CLK_12MHZ;
	  break;
	}

	default:
	{
	  /* Default case force it to 100 KHZ bit rate */
	  outputClk = 100000U;
	  internalClk = I2C_MODULE_INTERNAL_CLK_4MHZ;
	  break;
	}
  }
  LPrescaleValue = (96000000U / internalClk) - 1;//96000000U
  
  LI2cDivisor = (internalClk / outputClk); //24000000U has to be taken from the MCU 
  LI2cDivisor = LI2cDivisor / 2;
	
  if(true == LpConfigPtr->I2c_HwCfgPtr->hwExtendedAddress)
  {
    (void)regWriteReadback((LI2cBaseAddr + I2C_ICMDR), M_EIGHT, M_EIGHT, (uint32)0x1U);
  }
	
  LNodeAddress = LpConfigPtr->I2c_HwCfgPtr->hwUnitId;
	 
  (void)regWriteReadback((LI2cBaseAddr + I2C_ICOAR), M_THIRTY_ONE, M_ZERO, (uint32)0x10);
	
  (void)regWriteReadback((LI2cBaseAddr + I2C_ICPSC), M_THIRTY_ONE, M_ZERO, LPrescaleValue);
	 
  (void)regWriteReadback((LI2cBaseAddr + I2C_ICCLKL), M_THIRTY_ONE, M_ZERO, (LI2cDivisor - 6U));
	
  (void)regWriteReadback((LI2cBaseAddr + I2C_ICCLKH), M_THIRTY_ONE, M_ZERO, (LI2cDivisor - 6U));
  
  (void)regWriteReadback((LI2cBaseAddr + I2C_ICSTR), M_THIRTY_ONE, M_ZERO, (uint32)0x00000000);
  
  (void)regWriteReadback((LI2cBaseAddr+I2C_ICSAR), M_THIRTY_ONE, M_ZERO, (uint32)0x00000000);
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_UnInitHXW                                       **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : I2c_HardwareConfigPtr                               **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Function is use to De-initialize I2C Engine.        **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE)I2c_UnInitHW(P2CONST(I2C_ConfigType, AUTOMATIC, I2C_CONST) LpConfigPtr)
{
  VAR(uint32, AUTOMATIC) LBaseAddress = 0U;
	
  LBaseAddress = GET_I2CBaseAddress(LpConfigPtr);
  /* I2c h/w is de-initializing under this service */
  /* stop the engine clock */
  I2C_Stop();
  /* Disable the I2C bus */
  I2C_ENTER_CRITICAL_SEC
  (void)regWriteReadback((LBaseAddress + I2C_ICOAR), M_EIGHT, M_ZERO, I2C_DEFAULT_ADDR);
  I2C_EXIT_CRITICAL_SEC
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_Local_SequenceEndNotification                   **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : None                                                **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Function is use to sequence end notification.       **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) I2c_Local_SequenceEndNotification(void)
{
  VAR(I2C_SequenceType, AUTOMATIC) LddSequenceId;
  VAR(I2C_DataLengthType, AUTOMATIC) LddCount;
  /* finding the callback function for executed sequence */
  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)
    {
      break;
    }
  }

  if (I2c_GpSequenceConfigDataPtr[LddCount].I2cSequenceEndNotification !=
                 NULL_PTR)
  {
    /*executing the sequence end notification function*/
    (*I2c_GpSequenceConfigDataPtr[LddCount].I2cSequenceEndNotification)();
  }
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_Local_SequenceErrorNotification                 **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : None                                                **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Function is use to sequence error notification.     **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) I2c_Local_SequenceErrorNotification(void)
{
  I2C_SequenceType LddSequenceId;
  I2C_DataLengthType LddCount;
  /* finding the callback function for executed sequence */
  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)
    {
      break;
    }
  }

  if (I2c_GpSequenceConfigDataPtr[LddCount].I2cSeqErrorNotification !=
                 NULL_PTR)
  {
    /*executing the sequence error notification function*/
    (*I2c_GpSequenceConfigDataPtr[LddCount].I2cSeqErrorNotification)();
  }
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_CancelSequence                                  **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Sequence                                            **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Function is use to cancel running/queued Sequence.  **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) I2c_CancelSequence(
        VAR(I2C_SequenceType, AUTOMATIC) Sequence)
{
  /* It checking requested sequence ID in queued buffer */
  I2c_GddCancelSeq = Sequence;
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_QueueSequenceAddr                               **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Channel, Address                                    **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Function is use to queued I2C adress.               **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) I2c_QueueSequenceAddr(
        VAR(I2C_ChannelType, AUTOMATIC) Channel,
                        VAR(I2C_AddressType, AUTOMATIC) Address)
{
  
}
#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_QueueRxDataBufferPtr                            **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Channel, RxDataBufferPtr                            **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Function is use to queued RxDataBufferPtr.          **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) I2c_QueueRxDataBufferPtr(
        VAR(I2C_ChannelType, AUTOMATIC) Channel,
                        VAR(I2C_DataPtrType, AUTOMATIC) RxDataBufferPtr,
            VAR(I2C_DataLengthType, AUTOMATIC) Length)
{
  /* check for I2C channel for memory address */
  
}

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_QueueTxDataBufferPtr                            **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Channel, TxDataBufferPtr                            **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Function is use to queued TxDataBufferPtr.          **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) I2c_QueueTxDataBufferPtr(
            VAR(I2C_ChannelType, AUTOMATIC) Channel,
                        VAR(I2C_DataConstPtrType, AUTOMATIC) TxDataBufferPtr,
            VAR(I2C_DataLengthType, AUTOMATIC) Length)
{
  /* check for I2C channel for memory address */
 
}


#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

/******************************************************************************
 ** Service name       : I2c_QueueChanelStatusUpdate                         **
 **                                                                          **
 ** Service ID[hex]    : NA                                                  **
 **                                                                          **
 ** Sync/Async         : Synchronous                                         **
 **                                                                          **
 ** Reentrancy         : Non Reentrant                                       **
 **                                                                          **
 ** Parameters (in)    : Channel, TxDataBufferPtr                            **
 **                                                                          **
 ** Parameters (inout) : None                                                **
 **                                                                          **
 ** Parameters (out)   : None                                                **
 **                                                                          **
 ** Return value       : None                                                **
 **                                                                          **
 ** Description        : Function is use to queued Chanel Status into array. **
 *****************************************************************************/

#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

FUNC(void, I2C_CODE) I2c_QueueChanelStatusUpdate(
            VAR(I2C_ChannelType, AUTOMATIC) Channel,
                        VAR(Std_ReturnType, AUTOMATIC) ChaAcceped)
{
  I2c_DeleteDuplicateChFromStatusBuf(Channel);
  /* check for return value */
  if (E_OK == ChaAcceped)
  {
    I2c_GaaChStatus[I2C_GddChaCount].ChannelId = Channel;
    I2c_GaaChStatus[I2C_GddChaCount].ChannelStatus = I2C_CH_PENDING;
  }
  else /* return value is equal to E_NOT_OK */
  {
    I2c_GaaChStatus[I2C_GddChaCount].ChannelId = Channel;
    I2c_GaaChStatus[I2C_GddChaCount].ChannelStatus = I2C_CH_FAILED;
  }
}


FUNC(uint8, I2C_CODE) CheckStatusBits(VAR(I2C_TransmissionCmdType, AUTOMATIC) CommandSequence)
{
	VAR(Std_ReturnType, AUTOMATIC) RetVal = E_OK;
	VAR(uint32, AUTOMATIC) LI2cBaseAddr;
	
	LI2cBaseAddr = GET_I2CBaseAddress(I2c_GpConfigType);
	
	if(HW_RD_FIELD32((LI2cBaseAddr+I2C_ICSTR),CSL_I2C_ICSTR_AL) == 0x1U)
	{
	  RetVal = E_NOT_OK;
	  (void)regWriteReadback((LI2cBaseAddr + I2C_ICSTR), M_ZERO, M_ZERO, (uint32)0x0U);
	}
	if(HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_NACK) == 0x1U)
	{
	  RetVal = E_NOT_OK;
	  (void)regWriteReadback((LI2cBaseAddr + I2C_ICSTR), M_ONE, M_ONE, (uint32)0x0U);
	}
	if(HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_ARDY) == 0x1U)
	{
      RetVal = E_NOT_OK;
      (void)regWriteReadback((LI2cBaseAddr + I2C_ICSTR), M_TWO, M_TWO, (uint32)0x0U);
	}
	
	if(E_OK == RetVal)
	{
	  if(I2C_WRITE == CommandSequence)
	  {
        if(HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_ICXRDY) == 0x1U)
	    {
		}
	  }
	  else if(I2C_READ == CommandSequence)
	  {
        if(HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_ICRRDY) == 0x1U)
	    {
		}
	  }
	  else
	  {
		  RetVal = E_NOT_OK;
	  }
	}
	
	return RetVal;
}

FUNC(uint8, I2C_CODE) I2c_PollingMode(void)
{
  VAR(uint32, AUTOMATIC) LI2cBaseAddr;
  VAR(Std_ReturnType, AUTOMATIC) LddRetVal = E_OK;
  VAR(uint8, AUTOMATIC) CommandSequence = 0U;
  VAR(uint32, AUTOMATIC) writeDataBytes = 0U;
  VAR(uint32, AUTOMATIC) readDataBytes = 0U;
  VAR(I2C_DataConstPtrType, I2C_CONST) Data = 0U;
  VAR(uint8, AUTOMATIC) error = 0U;
  VAR(uint8, AUTOMATIC) fatalerror = 0U;
  VAR(uint8, AUTOMATIC) status = 0U;
  
  /*Capture the Tranmission command based on the configured parameters*/
	
  (void)regWriteReadback((LI2cBaseAddr + I2C_ICSTR), M_FIFTEEN, M_ZERO, (uint32)0xFFFFU);
  /*Check whether the Interrupts are disabled*/
  #if(I2C_USE_INTERRUPTS == STD_OFF)
  {
    /*Get the base address based on the configurations*/
    LI2cBaseAddr = GET_I2CBaseAddress(I2c_GpConfigType);
	
	writeDataBytes = 0;
	
	readDataBytes = 1;
		
    /*Capture the value of BB bit in ICSTR IRQregister*/
	
	if(writeDataBytes != 0U || readDataBytes != 0U)
	{  
	  I2CSetDataCount(LI2cBaseAddr + I2C_ICCNT, readDataBytes); 
	  
      while (HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_BB))
	  {
        /*Nothing*/
	  }
	  
	  (void)regWriteReadback((LI2cBaseAddr + I2C_ICSAR), M_EIGHT, M_ZERO, (uint32)0x60);
		
      Send_AddrCommand();
	
	  LddRetVal = CheckStatusBits(CommandSequence);
	
	  if(E_OK == LddRetVal)
	  {
		if(writeDataBytes != 0)
		{		  
		  Setup_I2cControl(I2C_WRITE);
		  
		  while(writeDataBytes != 0)
		  {
		    Data = I2c_GaaSeqFrame[I2c_GddSeqFrameCount].TxBufferPtr;
		    I2c_GddSeqFrameCount++;
		    while((HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_ICXRDY)) == 0)
		    {}
	        error = ((HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_AL)) |
		            (HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_NACK)) |
				    (HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_SCD)));
		    if(error)
		    {
			  fatalerror = 1;
			  break;
		    }
		    writeDataBytes--;
		  }
		  I2C_Stop_Comm(LI2cBaseAddr + I2C_ICMDR);
          if(fatalerror == 0x01)
		  {
		    if ((error & CSL_I2C_ICSTR_AL_MASK) == 0x01)
		    {
			  status = I2C_STS_ERR_ARBITRATION_LOST;
		    }
		    else if((error & CSL_I2C_ICSTR_NACK_MASK) == 0x01)
		    {
			  status = I2C_STS_ERR_NO_ACK;
		    }
		    else
		    {
			  status = I2C_STS_SUCCESS;
		    }
		  }
	    }
	  }
		
	  if(readDataBytes != 0)
	  {		
		Setup_I2cControl(I2C_READ);
	    
        while(readDataBytes != 0)
		{
          while((HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_ICRRDY)) != 0)
		  {}
		  error = ((HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_AL)) |
		           (HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_NACK)) |
			       (HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICSTR_SCD)));
		  if(error)
		  {
	        break;
		  }
		  I2c_GaaSeqFrame[I2c_GddSeqFrameCount].RxBufferPtr = (uint16*)HW_RD_FIELD32((LI2cBaseAddr + I2C_ICSTR),CSL_I2C_ICDRR_D);
		  I2c_GddSeqFrameCount++;
		  readDataBytes--;
		}
            
		if ((error & CSL_I2C_ICSTR_AL_MASK) == 0x01)
		{
		  status = I2C_STS_ERR_ARBITRATION_LOST;
		}
		else if((error & CSL_I2C_ICSTR_NACK_MASK) == 0x01)
		{
		  status = I2C_STS_ERR_NO_ACK;
		}
		else
		{
		  status = I2C_STS_SUCCESS;
		}
	  }
    }
  }
  #endif
  return (status);
}


#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

FUNC(uint32, I2C_CODE) GET_I2CBaseAddress(P2CONST(I2C_ConfigType, AUTOMATIC, I2C_CONST) LI2cBaseAddr)
{
  uint32 BaseAddress;
	
  switch(LI2cBaseAddr->I2c_HwCfgPtr->hwUnitId)
  {
    case I2C0_CFG:
      BaseAddress = I2C0_BASE_ADDR;
      break;
    case I2C1_CFG:
      BaseAddress = I2C1_BASE_ADDR;
      break;
    case I2C2_CFG:
      BaseAddress = I2C2_BASE_ADDR;
      break;
    case I2C3_CFG:
      BaseAddress = I2C3_BASE_ADDR;
      break;
    default:
      BaseAddress = I2C0_BASE_ADDR;
      break;	
  }
  return (BaseAddress);
}
#ifdef __cplusplus
}
#endif
/*****************************************************************************
**                             End of File                                  **
******************************************************************************/
