/**********************************************************************************************************************
 *  COPYRIGHT
 *  -------------------------------------------------------------------------------------------------------------------
 *  \verbatim
 *
 *                 TEXAS INSTRUMENTS INCORPORATED PROPRIETARY INFORMATION
 *
 *                 Property of Texas Instruments, Unauthorized reproduction and/or distribution
 *                 is strictly prohibited.  This product  is  protected  under  copyright  law
 *                 and  trade  secret law as an  unpublished work.
 *                 (C) Copyright Texas Instruments.  All rights reserved.
 *
 *  \endverbatim
 *  -------------------------------------------------------------------------------------------------------------------
 *  FILE DESCRIPTION
 *  -------------------------------------------------------------------------------------------------------------------
 *         File:  Gpt.c
 *    Component:  Tms570_AutosarGptDriver
 *       Module:  GptDriver
 *    Generator:  None
 *
 *  Description: This component provides services for initialization and control of the microcontroller internal
 *                GPT unit (General Purpose Timer).
 *********************************************************************************************************************/

/**********************************************************************************************************************
 *---------------------------------------------------------------------------------------------------------------------
 * Author:  Vishwanath Reddy
 *---------------------------------------------------------------------------------------------------------------------
 * Revision History
 *---------------------------------------------------------------------------------------------------------------------
 * Version        Date         Author               Change ID        Description
 *---------------------------------------------------------------------------------------------------------------------
 * 02.21.00       30Sep14      Vishwanath Reddy     SDOCM00113130    Initial Version
 * 02.21.01       11Mar16      Enrique Lizarraga    SDOCM00121245    Move inclusion of Std_Types.h from C to H file
 * 02.30.00       01Dec2016    Sudhakar Singh           None         Update version info
 * 05.30.00       15Mar2017    Sudhakar Singh       SDOCM00122834    Version supports TMS570LSxx Family of Devices.
 *********************************************************************************************************************/

#define GPT_SOURCE

/**********************************************************************************************************************
 *  INCLUDES
 *********************************************************************************************************************/
#include "Gpt.h"
#include "Gpt_Irq.h"
#include "SchM_Gpt.h"
#if (STD_ON == GPT_DEV_ERROR_DETECT)
    /* Include-File, which contains Errorhook-Function                      */
    #include "Det.h"
#endif  /* (STD_ON == GPT_DEV_ERROR_DETECT) */
/**********************************************************************************************************************
 *  VERSION CHECK
 *********************************************************************************************************************/
#define GPT_SW_C_MAJOR_VERSION               (5U)
#define GPT_SW_C_MINOR_VERSION               (30U)
#define GPT_SW_C_PATCH_VERSION               (0U)
 
 /* vendor specific version information is BCD coded */
#if (  (GPT_SW_MAJOR_VERSION != GPT_SW_C_MAJOR_VERSION) \
    || (GPT_SW_MINOR_VERSION != GPT_SW_C_MINOR_VERSION) \
    || (GPT_SW_PATCH_VERSION != GPT_SW_C_PATCH_VERSION) )
  #error "Vendor specific version numbers of Gpt.c and Gpt.h are inconsistent"
#endif

/* AUTOSAR version information check has to match definition in header file */
#if (  (GPT_AR_RELEASE_MAJOR_VERSION != (4U)) \
    || (GPT_AR_RELEASE_MINOR_VERSION != (0U)) \
    || (GPT_AR_RELEASE_REVISION_VERSION != (3U)) )
  #error "AUTOSAR Specification Version numbers of Gpt.c and Gpt.h are inconsistent!"
#endif

/* versions of included header files shall be checked: configuration */
#if (  (GPT_CFG_MAJOR_VERSION != GPT_SW_C_MAJOR_VERSION) \
    || (GPT_CFG_MINOR_VERSION != GPT_SW_C_MINOR_VERSION) )
  #error "Version numbers of Gpt.c and Gpt_Cfg.h are inconsistent!"
#endif

/* versions of included header files shall be checked: interrupt interface */
#if (  (GPT_IRQ_MAJOR_VERSION != GPT_SW_C_MAJOR_VERSION) \
    || (GPT_IRQ_MINOR_VERSION != GPT_SW_C_MINOR_VERSION) )
  #error "Version numbers of Gpt.c and Gpt_Irq.h are inconsistent!"
#endif

/**********************************************************************************************************************
 *  LOCAL CONSTANT MACROS
 *********************************************************************************************************************/

/* Real Time Interrupt Module Registers */
#define GPT_RTI1_REGISTER_BASE   (0xFFFFFC00U)

#define GPT_RTI1_REG_ABS(x)      (*((Gpt_RegisterPtrType) (GPT_RTI1_REGISTER_BASE + (x))))

/* Global Registers */
/* RTI Global Control Register --> Start/Stop operations of the counter blocks and determines the external timebase */
#define GPT_RTI1_GCTRL            GPT_RTI1_REG_ABS(0x00U)
/* RTI Timebase Control Register --> Selection of Free Running Counter behavior regarding clock source */
#define GPT_RTI1_TBCTRL           GPT_RTI1_REG_ABS(0x04U)
/* RTI Compare Control Register --> Setting dependencies of Free Running Counter instance to Compare instance */
#define GPT_RTI1_COMPCTRL         GPT_RTI1_REG_ABS(0x0CU)
/* RTI Clear/Status Interrupt Register --> Clears Free Running Counter Overflow and Compare Interrupts */
#define GPT_RTI1_SETINT           GPT_RTI1_REG_ABS(0x80U)
/* RTI Set/Status Interrupt Register --> Sets Free Running Counter Overflow and Compare Interrupts */
#define GPT_RTI1_CLEARINT         GPT_RTI1_REG_ABS(0x84U)
/* RTI Interrupt Flag Register -->  Determines pending interrupts */
#define GPT_RTI1_INTFLAG          GPT_RTI1_REG_ABS(0x88U)

/* Counter block Registers */

/* RTI Free Running Counter 0 Register -->   Free Running Counter 0 */
#define GPT_RTI1_FRC0             GPT_RTI1_REG_ABS(0x10U)
/* RTI Up Counter 0 Register -->  Contains value of incrementation in frequency of RTI clock */
#define GPT_RTI1_UC0              GPT_RTI1_REG_ABS(0x14U)
/* RTI Compare Up Counter 0 Register -->   Prescales clock for incrementation of the Free Running Counter 0 */
#define GPT_RTI1_CPUC0            GPT_RTI1_REG_ABS(0x18U)
/* RTI Free Running Counter 1 Register -->   Free Running Counter 1 */
#define GPT_RTI1_FRC1             GPT_RTI1_REG_ABS(0x30U)
/* RTI Up Counter 1 Register -->  Contains value of incrementation in frequency of RTI clock */
#define GPT_RTI1_UC1              GPT_RTI1_REG_ABS(0x34U)
/* RTI Compare Up Counter 1 Register -->   Prescales clock for incrementation of the Free Running Counter 1 */
#define GPT_RTI1_CPUC1            GPT_RTI1_REG_ABS(0x38U)

/* Compare Registers to be mapped to timer channels*/
/* RTI Compare 0 Register -->  Contains value to be compared with Free Running Counter Register */
#define GPT_RTI1_COMP0            GPT_RTI1_REG_ABS(0x50U)
/* RTI Update Compare 0 Register-->Contains value to be added to Compare 0 Register to realize continuous timer mode*/
#define GPT_RTI1_UDCP0            GPT_RTI1_REG_ABS(0x54U)
/* RTI Compare 1 Register -->  Contains value to be compared with Free Running Counter Register */
#define GPT_RTI1_COMP1            GPT_RTI1_REG_ABS(0x58U)
/* RTI Update Compare 1 Register-->Contains value to be added to Compare 1 Register to realize continuous timer mode*/
#define GPT_RTI1_UDCP1            GPT_RTI1_REG_ABS(0x5CU)
/* RTI Compare 2 Register -->  Contains value to be compared with Free Running Counter Register*/
#define GPT_RTI1_COMP2            GPT_RTI1_REG_ABS(0x60U)
/* RTI Update Compare 2 Register-->Contains value to be added to Compare 2 Register to realize continuous timer mode*/
#define GPT_RTI1_UDCP2            GPT_RTI1_REG_ABS(0x64U)
/* RTI Compare 3 Register -->  Contains value to be compared with Free Running Counter Register */
#define GPT_RTI1_COMP3            GPT_RTI1_REG_ABS(0x68U)
/* RTI Update Compare 3 Register-->Contains value to be added to Compare 3 Register to realize continuous timer mode*/
#define GPT_RTI1_UDCP3            GPT_RTI1_REG_ABS(0x6CU)

/*
 *  These masks are used for 32-bit write-access to registers, that influence behavior of the two counter blocks
 *  and 2 compare blocks.
 */

/* Assigned to RTIGCTRL-Register; Value after reset: 0x00000000 */
#define GPT_CNT0EN_MASK          ((uint32)0x00000001)  /* Enables operation of counter block 0: UC0, FRC0 */
#define GPT_CNT1EN_MASK          ((uint32)0x00000002)  /* Enables operation of counter block 1: UC1, FRC1 */

/* Assigned to RTITBCTRL-Register; Value after reset: 0x00000000 */
#define GPT_TBEXT_MASK           ((uint32)0x00000001)  /* Selects clock source of FRC0 from NTU (<--> UC0) */
#define GPT_INC_MASK             ((uint32)0x00000002)  /* Enables incrementation of FRC0 on failing NTU clock */

/* Assigned to RTICOMPCTRL-Register; Value after reset: 0x00000000 */
/* Connects compare block 0 to counter block 1=FRC1 (<--> FRC0) */
#define GPT_COMPSEL0_MASK        ((uint32)0x00000001)
/* Connects compare block 1 to counter block 1=FRC1 (<--> FRC0) */
#define GPT_COMPSEL1_MASK        ((uint32)0x00000010)
/* Connects compare block 2 to counter block 1=FRC1 (<--> FRC0) */
#define GPT_COMPSEL2_MASK        ((uint32)0x00000100)
/* Connects compare block 3 to counter block 1=FRC1 (<--> FRC0) */
#define GPT_COMPSEL3_MASK        ((uint32)0x00001000)

/* Assigned to RTICLEARINT-Register; Value after reset: 0x00000000 */
/* Disables Clear Compare Interrupt 0 =INT0 (<--> bit unchanged) */
#define GPT_CLEARINT0_MASK       ((uint32)0x00000001)
/* Disables Clear Compare Interrupt 1 =INT1 (<--> bit unchanged) */
#define GPT_CLEARINT1_MASK       ((uint32)0x00000002)
/* Disables Clear Compare DMA Request 0 = DMA0 (<--> bit unchanged) */
#define GPT_CLEARDMA0_MASK       ((uint32)0x00000100)
/* Disables Clear Compare DMA Request 1 = DMA1 (<--> bit unchanged) */
#define GPT_CLEARDMA1_MASK       ((uint32)0x00000200)
/* Disables Timebase Interrupt (<--> bit unchanged) */
#define GPT_CLEARTBINT_MASK      ((uint32)0x00010000)
/* Disables FRC0 Overflow Interrupt (<--> bit unchanged) */
#define GPT_CLEAROVL0INT_MASK    ((uint32)0x00020000)
/* Disables FRC1 Overflow Interrupt (<--> bit unchanged) */
#define GPT_CLEAROVL1INT_MASK    ((uint32)0x00040000)

/* Assigned to RTISETINT-Register; Value after reset: 0x00000000 */
/* Enables Clear Compare Interrupt 0 = INT0 (<--> bit unchanged) */
#define GPT_SETINT0_MASK         ((uint32)0x00000001)
/* Enables Clear Compare Interrupt 1 = INT1 (<--> bit unchanged) */
#define GPT_SETINT1_MASK         ((uint32)0x00000002)
/* Enables Clear Compare DMA Request 0 = DMA0 (<--> bit unchanged) */
#define GPT_SETDMA0_MASK         ((uint32)0x00000100)
/* Enables Clear Compare DMA Request 1 = DMA1 (<--> bit unchanged) */
#define GPT_SETDMA1_MASK         ((uint32)0x00000200)
/* Enables Timebase Interrupt (<--> bit unchanged) */
#define GPT_SETTBINT_MASK        ((uint32)0x00010000)
/* Enables FRC0 Overflow Interrupt (<--> bit unchanged) */
#define GPT_SETOVL0INT_MASK      ((uint32)0x00020000)
/* Enables FRC1 Overflow Interrupt (<--> bit unchanged) */
#define GPT_SETOVL1INT_MASK      ((uint32)0x00040000)

/* Assigned to RTIINTFLAG-Register; Value after reset: 0x00000000 */
/* Sets Interrupt Flag 0 to 0 = no interrupt pending (<--> bit unchanged) */
#define GPT_INT0_MASK            ((uint32)0x00000001)
/* Sets Interrupt Flag 1 to 0 = no interrupt pending (<--> bit unchanged) */
#define GPT_INT1_MASK            ((uint32)0x00000002)
/* Sets Interrupt Flag 2 to 0 = no interrupt pending (<--> bit unchanged) */
#define GPT_INT2_MASK            ((uint32)0x00000004)
/* Sets Interrupt Flag 3 to 0 = no interrupt pending (<--> bit unchanged) */
#define GPT_INT3_MASK            ((uint32)0x00000008)
/* Sets Timebase Interrupt Flag to 0 = no interrupt pending (<--> bit unchanged) */
#define GPT_TBINT_MASK           ((uint32)0x00010000)
/* Sets FRC0 Overflow Interrupt Flag to 0 = no interrupt pending (<--> bit unchanged) */
#define GPT_OVL0INT_MASK         ((uint32)0x00020000)
/* Sets FRC1 Overflow Interrupt Flag to 0 = no interrupt pending (<--> bit unchanged) */
#define GPT_OVL1INT_MASK         ((uint32)0x00040000)
/* Masks higher Half-Byte of HWChannel_t (CountByte) to evaluate the Compare Block */
#define HWCHANNEL_MASK           ((uint32)0x00000001)
#define GPT_RTI_MASK             ((uint32)0x00000002)/* Checks if the Rti of the channel is RTI1 or RTI2 */


#define MAX_GPT_CHANNELS    2U

/*Global Suspend and Global Restore */
#define Gpt_GlobalSuspend()  SchM_Enter_Gpt_GPT_EXCLUSIVE_AREA_0()
#define Gpt_GlobalRestore()  SchM_Exit_Gpt_GPT_EXCLUSIVE_AREA_0() 

/**********************************************************************************************************************
 *  LOCAL DATA TYPES AND STRUCTURES
 *********************************************************************************************************************/

/* type describing the register name */
typedef uint32 Gpt_RegisterNameType;

/* pointer to this type */
typedef volatile P2VAR(Gpt_RegisterNameType, AUTOMATIC, GPT_DATA) Gpt_RegisterPtrType;

/**********************************************************************************************************************
 *  LOCAL DATA PROTOTYPES
 *********************************************************************************************************************/

#define GPT_START_SEC_VAR_INIT_UNSPECIFIED
/*SAFETYMCUSW 338 S MR:19.1 <APPROVED> "Memory mapping requires this syntax" */
#include "MemMap.h"
/*GPT Configuration Pointer*/
P2CONST(Gpt_ConfigType, GPT_DATA, GPT_PBCFG)  Gpt_Config_pt = NULL_PTR;

/* Variable to indicate the operation mode of the driver*/
static VAR (Gpt_ModeType, GPT_DATA) Gpt_SleepMode = GPT_MODE_UNITIALIZED;

/*Channel Notification function pointer of configured channels */
static VAR (Gpt_NotifyType, GPT_DATA) Gpt_ChannelNotifyFunctions[MAX_GPT_CHANNELS] =
{
		NULL_PTR,
		NULL_PTR
};


#define GPT_STOP_SEC_VAR_INIT_UNSPECIFIED
/*SAFETYMCUSW 338 S MR:19.1 <APPROVED> "Reason - This is the format to use for specifying memorysections."*/
#include "MemMap.h"

#define GPT_START_SEC_VAR_32BIT
/*SAFETYMCUSW 338 S MR:19.1 <APPROVED> "Reason - This is the format to use for specifying memorysections."*/
#include "MemMap.h"

/* 1) Gpt_IsrIndex variable specifies the channel mode (one shot and continuous mode) configured for each channel.
   2)This will be used in finding the notification function when interrupt occurs.
   3)The following four notification functions will be called depending on the whether
     Gpt is in normal mode or sleep mode and Channel is configured in one shot mode or in continuous mode
     1)Gpt_NotifSingleIsr
     2)Gpt_NotifContIsr
     3)Gpt_WakeupContIsr
     4)Gpt_WakeupSingleIsr
*/
VAR (uint32, GPT_DATA) Gpt_IsrIndex[4U];

/* Variable to indicate the state of the channel */
/*A Channel will be in five states - Uninitialized,Initialized, Running,Stopped,Expired*/
static VAR(Gpt_ChannelStateType, GPT_DATA) Gpt_ChannelState[MAX_GPT_CHANNELS];

#if (STD_ON == GPT_WAKEUP_FUNCTIONALITY_API)

   /* Variable to indicate wakeup is enabled/disabled for the channel (in bit wise )*/
   static VAR (uint32, GPT_DATA) Gpt_ActiveWakeupChannels;

#endif /* (STD_ON == GPT_WAKEUP_FUNCTIONALITY_API) */

#if (STD_ON == GPT_ENABLE_DISABLE_NOTIFICATION_API)

   /* Variable indicate the notification status for each channel (bit wise) */
   /* 1 - Notification is enabled for the corresponding channel
    * 0 - Notification is disabled for the corresponding channel            */
   static VAR (uint32, GPT_DATA) Gpt_ActiveNotifyChannels;
#endif /* (STD_ON == GPT_ENABLE_DISABLE_NOTIFICATION_API) */


#if ((STD_ON == GPT_WAKEUP_FUNCTIONALITY_API) || (STD_ON == GPT_REPORT_WAKEUP_SOURCE))
/*WakeupSource Ref value of configured channels*/
static VAR (EcuM_WakeupSourceType, GPT_DATA) GptWakeupSource[MAX_GPT_CHANNELS];
#endif

#define GPT_STOP_SEC_VAR_32BIT
/*SAFETYMCUSW 338 S MR:19.1 <APPROVED> "Reason - This is the format to use for specifying memorysections."*/
#include "MemMap.h"

#define GPT_START_SEC_CONST_32BIT
/*SAFETYMCUSW 338 S MR:19.1 <APPROVED> "Reason - This is the format to use for specifying memorysections."*/
#include "MemMap.h"

/*The Array of pointers to the call notification Functions */
CONST (Gpt_IsrRefType, GPT_CONST) Gpt_IsrNotifyFunctions[CHANNEL_MODES] =
   {
      &Gpt_NotifContIsr,
      &Gpt_NotifSingleIsr,
      &Gpt_WakeupContIsr,
      &Gpt_WakeupSingleIsr
   };

#define GPT_STOP_SEC_CONST_32BIT
/*SAFETYMCUSW 338 S MR:19.1 <APPROVED> "Reason - This is the format to use for specifying memorysections."*/
#include "MemMap.h"

#define GPT_START_SEC_CODE
/*SAFETYMCUSW 338 S MR:19.1 <APPROVED> "Reason - This is the format to use for specifying memorysections."*/
#include "MemMap.h"

/**********************************************************************************************************************
 *  LOCAL FUNCTION PROTOTYPES
 *********************************************************************************************************************/
static FUNC(void, GPT_CODE) Gpt_SetRegStartTimer (Gpt_ChannelType channel, Gpt_ValueType value );
#if (STD_ON == GPT_WAKEUP_FUNCTIONALITY_API)
   static FUNC(void, GPT_CODE) Gpt_SetSleepMode(void);
   static FUNC(void, GPT_CODE) Gpt_SetNormalMode(void);
#endif

/**********************************************************************************************************************
 *  GLOBAL FUNCTIONS
 ********************************************************************************************************************/

#if (STD_ON == GPT_VERSION_INFO_API)
/*********************************************************************************************************************
 * Function Name : Gpt_GetVersionInfo
 *********************************************************************************************************************/
/* \Description:This service returns the version information of this module.
 *
 *               The version information includes:
 *              - Module Id
 *              - Vendor Id
 *              - Vendor specific version numbers
 *
 *  \param[out]: Std_VersionInfoType  versioninfo        Pointer to where to store the version
 *                                                       information of this module.
 *  \context:    The function can be called on task level.
 * *******************************************************************************************************************/
/* SourceId : GPT_SourceId_011 */
/* DesignId : GPT_DesignId_020  */
/* Requirements : AR_GPT_SR46,AR_GPT_SR47,AR_GPT_SR48,AR_GPT_SR49 */
FUNC(void, GPT_CODE) Gpt_GetVersionInfo ( P2VAR(Std_VersionInfoType, AUTOMATIC, GPT_APPL_DATA) versioninfo )
{   

#if (STD_ON == GPT_DEV_ERROR_DETECT)
   if (NULL_PTR == versioninfo)
   {
      (void)Gpt_Errorhook(GPT_SID_GET_VERSION_INFO, GPT_E_PARAM_POINTER);      
   }
   else
#endif  /* (STD_ON == GPT_CFG_CHK_PARAM_VINFO) */   
   {
      versioninfo->vendorID         = ( GPT_VENDOR_ID );
      versioninfo->moduleID         = ( GPT_MODULE_ID );
      versioninfo->sw_major_version = (uint8)( GPT_SW_MAJOR_VERSION );
      versioninfo->sw_minor_version = (uint8)( GPT_SW_MINOR_VERSION );
      versioninfo->sw_patch_version = (uint8)( GPT_SW_PATCH_VERSION );
   }
} /* Gpt_GetVersionInfo()  */

#endif  /* (STD_ON == GPT_VERSION_INFO_API) */

/**********************************************************************************************************************
 * Function Name : Gpt_Init
 **********************************************************************************************************************/
/*  \Description: Gpt_Init initializes all the configured Gpt channels. This will set the state of the each channel to
 *                "initialized", also set the Gpt driver state to "Normal".

 *  \param[in]:  Gpt_ConfigType* ConfigPtr - configuration structure for initializing the module.
 *  \return:     None
 *  \pre:        -Input parameter must not be NULL.
 *               -Call of service must not interrupt running operation
 *               -Module must be in state uninitalized before calling
 *  \context:    Called by upper layer.
 *  \note:       After initialization, all channels are in "Initialized" state.
 *********************************************************************************************************************/
/* SourceId : GPT_SourceId_012 */
/* DesignId : GPT_DesignId_008  */
/* Requirements : AR_GPT_SR50,AR_GPT_SR51,AR_GPT_SR52,AR_GPT_SR53,AR_GPT_SR54,AR_GPT_SR55,AR_GPT_SR56,AR_GPT_SR57,
*                 AR_GPT_SR58,AR_GPT_SR59,AR_GPT_SR60 */
FUNC(void, GPT_CODE) Gpt_Init( P2CONST( Gpt_ConfigType, AUTOMATIC, GPT_CONST ) configPtr )
{
   Gpt_ChannelType ChannelIdx;/*Configured Channel Index*/
   Gpt_ChannelType Gpt_Channel;   

   /* configPtr must not be NULL. In case it is NULL report an error and
    * return immediately.*/
#if (STD_ON == GPT_DEV_ERROR_DETECT)
   if (NULL_PTR == configPtr)
   {
      (void)Gpt_Errorhook(GPT_SID_INIT, GPT_E_PARAM_POINTER);      
   }    
   else if (NULL_PTR != Gpt_Config_pt)
   {
      /* Gpt module must not be initalized. Otherwise call the Det with GPT_E_ALREADY_INITIALIZED */
	  (void)Gpt_Errorhook(GPT_SID_INIT, GPT_E_ALREADY_INITIALIZED);      
   }
   else
#endif   
   {

      Gpt_Config_pt = configPtr;

      /* Reset wakeup Channels */
#if (STD_ON == GPT_WAKEUP_FUNCTIONALITY_API)
      Gpt_ActiveWakeupChannels = 0U;
#endif  /* (STD_ON == GPT_WAKEUP_FUNCTIONALITY_API) */

      /* Reset the Channel notifications*/
#if (STD_ON == GPT_ENABLE_DISABLE_NOTIFICATION_API)
      Gpt_ActiveNotifyChannels = 0U;
#endif  /* (STD_ON == GPT_ENABLE_DISABLE_NOTIFICATION_API) */

      /* -------------------- Init loop ----------------------------------------
       * Only initialize configured resources, that means, only timers
       * listed in the channel configuration structure.
       */
      for ( ChannelIdx = 0U; ChannelIdx < Gpt_Config_pt->ChannelCount; ChannelIdx++ )
      {

         Gpt_Channel = Gpt_Config_pt->ChannelConfig_pt[ChannelIdx].ChannelId;

         switch (Gpt_Channel)
         {
            case RTI1_CHANNEL1 :
            	if(GPT_RTI1_GCTRL != (GPT_RTI1_GCTRL | (GPT_CNT0EN_MASK)))
            	{
				   /* Operation of counter block */
				   GPT_RTI1_GCTRL = (GPT_RTI1_GCTRL & (~GPT_CNT0EN_MASK));
				   /* Clear RTI clock counter */
				   GPT_RTI1_UC0 = 0U;
				   /* Prescaler clock */
				   GPT_RTI1_CPUC0 = Gpt_Config_pt->ChannelConfig_pt[ChannelIdx].GptChannelTickFrequency;
				   /* Clear Free running counter */
				   GPT_RTI1_FRC0 = 0U;
				   /* Connects compare block to counter block 0) */
				   GPT_RTI1_COMPCTRL = (GPT_RTI1_COMPCTRL & (~GPT_COMPSEL0_MASK));
				   /* Reset the Interrupt Flag */
				   GPT_RTI1_INTFLAG = GPT_INT0_MASK;
				   /*Reset Update counter*/
				   GPT_RTI1_UDCP0 = 0U;
				   /* Register holds compare value */
				   GPT_RTI1_COMP0 = 0U;
				   /*Set the Channel State to "Initialized"*/
				   Gpt_ChannelState[0U] = GPT_INITIALIZED;
            	}
               break;
            case RTI1_CHANNEL2 :
            	if(GPT_RTI1_GCTRL != (GPT_RTI1_GCTRL | (GPT_CNT1EN_MASK)))
            	{
				   /* Operation of counter block */
				   GPT_RTI1_GCTRL = (GPT_RTI1_GCTRL & (~GPT_CNT1EN_MASK));
				   /* Clear RTI clock counter */
				   GPT_RTI1_UC1 = 0U;
				   /* Prescaler clock */
				   GPT_RTI1_CPUC1 = Gpt_Config_pt->ChannelConfig_pt[ChannelIdx].GptChannelTickFrequency;
				   /* Clear Free running counter */
				   GPT_RTI1_FRC1 = 0U;
				   /* Connects compare block to counter block 1 */
				   GPT_RTI1_COMPCTRL = (GPT_RTI1_COMPCTRL | GPT_COMPSEL1_MASK);
				   /* Reset the Interrupt Flag */
				   GPT_RTI1_INTFLAG = GPT_INT1_MASK;
				   /*Reset Update counter*/
				   GPT_RTI1_UDCP1 = 0U;
				   /* Register holds compare value */
				   GPT_RTI1_COMP1 = 0U;
				   /*Set the Channel State to "Initialized"*/
				   Gpt_ChannelState[1U] = GPT_INITIALIZED;
            	}
               break;
            default:
               break;
         } /* End of switch */
      }/*For Loop*/
      /* Set Gpt to Normal mode */
#if (STD_ON == GPT_WAKEUP_FUNCTIONALITY_API)
      Gpt_SleepMode = GPT_MODE_NORMAL;
#endif  /*(STD_ON == GPT_WAKEUP_FUNCTIONALITY_API) */
   }
} /* Gpt_Init */

#if (STD_ON == GPT_DEINIT_API)
/*********************************************************************************************************************
 * Function Name : Gpt_DeInit
*********************************************************************************************************************/
/*  \Description: Gpt_DeInit deinitializes the RTI used by Gpt driver to the power on reset state.
 *                The Gpt driver state is changed to "Uninitialized" state".
                  All the channel registers are cleared to stop the timer channels.
 *                Gpt_Deinit will disable all interrupt notifications, wakeup interrupts.
 *  \param[in]:  void
 *  \return:     None
 *  \pre:        - Call of service must not interrupt running operation
  *  \context:   Called by upper layer.
 *  \note:       After de-initialization, all channels are in "Uninitialized" state
 *********************************************************************************************************************/
/* SourceId : GPT_SourceId_013 */
/* DesignId : GPT_DesignId_009  */
/* Requirements : AR_GPT_SR61,AR_GPT_SR62,AR_GPT_SR63,AR_GPT_SR64,AR_GPT_SR65,AR_GPT_SR66,AR_GPT_SR67,
*                 AR_GPT_SR68,AR_GPT_SR69 */
FUNC(void, GPT_CODE) Gpt_DeInit( void )
{
   Gpt_ChannelType ChannelIdx;/*Configured Channel Index*/
   Gpt_ChannelType Gpt_Channel;
   boolean Dev_Error_Flag = FALSE;

   /* check if the driver has been successfully initialized. if the driver
    * has not been initialized, report an error and return immediately.
    */
#if (STD_ON == GPT_DEV_ERROR_DETECT)
   if (NULL_PTR == Gpt_Config_pt)
   {
      (void)Gpt_Errorhook(GPT_SID_DEINIT, GPT_E_UNINIT);
      Dev_Error_Flag = TRUE;
   }
   else
   {
      /* Check if any channel is in "Running" state,If true report error and
       *  return from the function*/
      for ( ChannelIdx = 0U; ChannelIdx < Gpt_Config_pt->ChannelCount; ChannelIdx++ )
      {
         if(GPT_RUNNING == Gpt_ChannelState[Gpt_Config_pt->ChannelConfig_pt[ChannelIdx].ChannelId])
         {
            /* At least one of the channels is active */
            (void)Gpt_Errorhook ( GPT_SID_DEINIT, GPT_E_BUSY );
            Dev_Error_Flag = TRUE;
            break;
         }
      }
   }
#endif

   /*Return from the Api if Dev_Error_Flag is TRUE */
   if(Dev_Error_Flag == FALSE)
   {
      /* -------------------- DeInit Loop ----------------------------------------*/
      for ( ChannelIdx = 0U; ChannelIdx < Gpt_Config_pt->ChannelCount; ChannelIdx++ )
      {
         Gpt_Channel = Gpt_Config_pt->ChannelConfig_pt[ChannelIdx].ChannelId;

         switch (Gpt_Channel)
         {
            case RTI1_CHANNEL1:
               /* Operation of counter block */
               GPT_RTI1_GCTRL = (GPT_RTI1_GCTRL & (~GPT_CNT0EN_MASK));
               /* Clear RTI clock counter */
               GPT_RTI1_UC0 = 0U;
               /* Prescaler clock */
               GPT_RTI1_CPUC0 = 0U;
               /* Clear Free running counter */
               GPT_RTI1_FRC0 = 0U;
               /*Clear Update Compare Register*/
               GPT_RTI1_UDCP0 = 0U;
               /* Register holds compare value */
               GPT_RTI1_COMP0 = 0U;
               /*Reset Interrupt Notification*/
               GPT_RTI1_CLEARINT = GPT_CLEARINT0_MASK;
               /*Set the Channel State to "UnInitialized"*/
               Gpt_ChannelState[RTI1_CHANNEL1] = GPT_UNINITIALIZED;
               break;
            case RTI1_CHANNEL2 :
               /* Operation of counter block */
               GPT_RTI1_GCTRL = (GPT_RTI1_GCTRL & (~GPT_CNT1EN_MASK));
               /* Clear RTI clock counter */
               GPT_RTI1_UC1 = 0U;
               /* clear Prescaler clock */
               GPT_RTI1_CPUC1 = 0U;
               /* Clear Free running counter */
               GPT_RTI1_FRC1 = 0U;
               /*Clear Update Compare Register*/
               GPT_RTI1_UDCP1 = 0U;
               /* Register holds compare value */
               GPT_RTI1_COMP1 = 0U;
               GPT_RTI1_CLEARINT = GPT_CLEARINT1_MASK;
               /*Set the Channel State to "UnInitialized"*/
               Gpt_ChannelState[RTI1_CHANNEL2] = GPT_UNINITIALIZED;
               break;
            default:
               break;
         } /* End of switch */
      } /* End of for */
#if (STD_ON == GPT_ENABLE_DISABLE_NOTIFICATION_API)
      Gpt_ActiveNotifyChannels = 0U;/* Reset the notification attributes */
#endif /* (STD_ON == GPT_ENABLE_DISABLE_NOTIFICATION_API) */

      Gpt_Config_pt = NULL_PTR;/* Reset configuration pointer */

   }
} /* Gpt_DeInit */
#endif /* GPT_DEINIT_API */

#if (STD_ON == GPT_TIME_ELAPSED_API)
/*********************************************************************************************************************
 * Function name:  Gpt_GetTimeElapsed
 *********************************************************************************************************************/
/*  \Description: Gpt_GetTimeElapsed will return the time elapsed for channel which is referenced.
 *                The user can configure the channel in two modes, One-shot and Continuous mode.
 *                In one shot mode, if the timer is in stopped state, the function will return time value
 *                at the moment of stopping.
 *                If the timer is expired, the function will return the target time configured for the channel.
 *                In Continuous Mode - The elapsed time value will be the value relative to last occurrence.
 *  \param[in]:  Channel - ChannelId of the Gpt channel.
 *  \return:     Time Elapsed value
 *  \pre:        Channel must be a configured ChannelId.
 *  \context:    Called by upper layer.
 *  \note:       The state of the channel will not be changed by calling this function.
 ********************************************************************************************************************/
/* SourceId : GPT_SourceId_014 */
/* DesignId : GPT_DesignId_010  */
/* Requirements : AR_GPT_SR70,AR_GPT_SR71,AR_GPT_SR72,AR_GPT_SR73,AR_GPT_SR74,AR_GPT_SR75,AR_GPT_SR76,AR_GPT_SR77,
*                 AR_GPT_SR78,AR_GPT_SR79 */
FUNC(Gpt_ValueType, GPT_CODE) Gpt_GetTimeElapsed( Gpt_ChannelType channel )
{   
   Gpt_ChannelType   Gpt_Channel;
   Gpt_ValueType     UpdCompare_t = 0U;
   Gpt_ValueType     Compare_t = 0U;
   Gpt_ValueType     FreeRunningCounter_t = 0U;
   uint32            ChannelMode_t = 0U;
   uint32            Return_Value = 0U;

   /* Check if the driver has been successfully initialized. If the driver
    * has not been initialized, report an error and return from Api.
    */
#if (STD_ON == GPT_DEV_ERROR_DETECT )
   if (NULL_PTR == Gpt_Config_pt )
   {
      (void)Gpt_Errorhook(GPT_SID_GET_TIME_ELAPSED, GPT_E_UNINIT);
      Return_Value = 0U;      
   }
   /* Check if parameter channel is in valid range. If its value is out
    * of range report an error and return from the function.    */
   else if (channel >= Gpt_Config_pt->ChannelCount)
   {
      (void)Gpt_Errorhook ( GPT_SID_GET_TIME_ELAPSED, GPT_E_PARAM_CHANNEL );
      Return_Value = 0U;    
   }  
   else   
 #endif   
   {
       /* Check if timer is in "initialized" state, if TRUE return value = 0"*/
       if(GPT_INITIALIZED == Gpt_ChannelState[Gpt_Config_pt->ChannelConfig_pt[channel].ChannelId])
	   {      
		  Return_Value = 0U;
	   }
	   else
	   {
		  Gpt_Channel = Gpt_Config_pt->ChannelConfig_pt[channel].ChannelId;

		  /* Critical section, block the interrupts */
		  Gpt_GlobalSuspend();

		  /*Read the Counter, update counter and free running counter values of the channel*/
		  switch (Gpt_Channel)
		  {
			 case RTI1_CHANNEL1 :
				/*Free running counter value*/
				FreeRunningCounter_t = GPT_RTI1_FRC0;
				/*Update counter value*/
				UpdCompare_t = GPT_RTI1_UDCP0;
				/*Compare register value*/
				Compare_t = GPT_RTI1_COMP0;
				break;
			 case RTI1_CHANNEL2 :
				FreeRunningCounter_t = GPT_RTI1_FRC1;
				UpdCompare_t = GPT_RTI1_UDCP1;
				Compare_t = GPT_RTI1_COMP1;
				break;
			 default:
				break;
		  }
		  ChannelMode_t = (Gpt_Config_pt->ChannelConfig_pt[channel].ChannelMode);

		  /*Check if the channel is in Continuous mode*/
		  if(ChannelMode_t == GPT_CH_MODE_CONTINUOUS)
		  {
			 /*Find the relative difference b/w Compare and Free running counter*/
			 Return_Value = (FreeRunningCounter_t - (Compare_t - UpdCompare_t));
		  }
		  else/*One shot mode*/
		  {
			 /*Return the target time if the channel is expired, else return the relative difference*/
			 if(GPT_EXPIRED == Gpt_ChannelState[Gpt_Channel])
			 {
				Return_Value = Compare_t;
			 }
			 else
			 {
				Return_Value = FreeRunningCounter_t;
			 }
		  }
		  /* Critical section, restore the interrupts */
		  Gpt_GlobalRestore();
	   }
   }
   return Return_Value;
} /* Gpt_GetTimeElapsed */
#endif /* (STD_ON == GPT_CFG_USE_GET_TIME_ELAPSED) */

#if (STD_ON == GPT_TIME_REMAINING_API)
/*******************************************************************************************************************
    Function name:  Gpt_GetTimeRemaining
 ******************************************************************************************************************/
/*  \Description: Gpt_GetTimeRemaining will return the timer value remaining until the target time will be reached
 *                next time.
 *                The remaining time is target time minus time already elapsed.
 *                In one shot mode, if the timer is in stopped state, the function will return remaining time value
 *                at the moment of stopping.
 *                If the timer is expired, the function will return 0.
 *  \param[in]:  Channel - ChannelId of the Gpt channel.
 *  \return:     Time Remaining value
 *  \pre:        Channel value must be a configured ChannelId.
 *  \context:    Called by upper layer.
 *  \note:       The state of the channel will not be changed by calling this function.
 *******************************************************************************************************************/
/* SourceId : GPT_SourceId_015 */
/* DesignId : GPT_DesignId_011  */
/* Requirements : AR_GPT_SR80,AR_GPT_SR81,AR_GPT_SR82,AR_GPT_SR83,AR_GPT_SR84,AR_GPT_SR85,AR_GPT_SR86,AR_GPT_SR87,
*                 AR_GPT_SR88 */
FUNC(Gpt_ValueType, GPT_CODE) Gpt_GetTimeRemaining( Gpt_ChannelType channel )
{
   Gpt_ValueType     Compare_t = 0U;
   Gpt_ChannelType   Gpt_Channel;
   uint32            ChannelMode_t;
   Gpt_ValueType     FreerunningCounter_t = 0U;   
   uint32            Return_Value = 0U;
   /* Check if the driver has been successfully initialized. If the driver
    * has not been initialized, report an error and return from Api.
    */
#if (STD_ON == GPT_DEV_ERROR_DETECT )
   if (NULL_PTR == Gpt_Config_pt )
   {
      (void)Gpt_Errorhook(GPT_SID_GET_TIME_ELAPSED, GPT_E_UNINIT);
      Return_Value = 0U;      
   }
   /* Check if parameter channel is in valid range. If its value is out
    * of range report an error and return from the function.    */
   else if (channel >= Gpt_Config_pt->ChannelCount)
   {
      (void)Gpt_Errorhook ( GPT_SID_GET_TIME_ELAPSED, GPT_E_PARAM_CHANNEL );
      Return_Value = 0U;      
   }   
   else	
#endif   
   {
      /* Check if timer is in "initialized" state, if TRUE return value = 0"*/
	   if(GPT_INITIALIZED == Gpt_ChannelState[Gpt_Config_pt->ChannelConfig_pt[channel].ChannelId])
	   {
			/* The timer is not running, so return 0  */      
			Return_Value = 0U;
	   }
	   else
	   {		  
		  Gpt_Channel = Gpt_Config_pt->ChannelConfig_pt[channel].ChannelId;

		  /* Critical section, block the interrupts */
		  Gpt_GlobalSuspend();
		  
		  switch (Gpt_Channel)
		  {
			 case RTI1_CHANNEL1 :
				/*Read counter and compare values*/
				FreerunningCounter_t = GPT_RTI1_FRC0;
				Compare_t = GPT_RTI1_COMP0;
				break;
			 case RTI1_CHANNEL2 :
				FreerunningCounter_t = GPT_RTI1_FRC1;
				Compare_t = GPT_RTI1_COMP1;
				break;
			 default:
				break;
		  }
		  
		  /* Critical section, restore the interrupts */
		  Gpt_GlobalRestore();

		  /*Read the channel mode from the channel configuration*/
		  ChannelMode_t = (Gpt_Config_pt->ChannelConfig_pt[channel].ChannelMode);

		  /*Check if the channel is in Continuous mode, find the remaining time value*/
		  if(ChannelMode_t == GPT_CH_MODE_CONTINUOUS)
		  {
			 Return_Value = (Compare_t - FreerunningCounter_t);
		  }
		  else/*One shot mode*/
		  {
			 /*If the channel is expired in one-shot mode, return value zero*/
			 if(GPT_EXPIRED == Gpt_ChannelState[Gpt_Channel])
			 {
				Return_Value = 0U;/*Return zero*/
			 }
			 else
			 {
				Return_Value = (Compare_t - FreerunningCounter_t);
			 }
		  }/*else(ChannelMode == GPT_CH_MODE_CONTINUOUS)*/	  
	   }  
   }
    return (Return_Value);
} /* Gpt_GetTimeRemaining */
#endif /* (STD_ON == GPT_CFG_USE_GET_TIME_REMAINING) */

/******************************************************************************************************************
    Function name:  Gpt_StartTimer
 ******************************************************************************************************************/
/*  \Description: Gpt_StartTimer will start the selected timer channel with defined target time.
 *                If the timer channel is enabled for interrupt notification, then interrupt notification will be
 *                triggered after expiration of the selected timer channel.
 *                In one shot mode, if the timer is expired then the channel will be stopped in interrupt subroutine.
 *                The selected channel will be moved to "Running" state after calling this function.
 *  \param[in]:  Channel - ChannelId of the Gpt channel.
 *               Target Time - Target time to be set for selected channel in Ticks.
 *  \return:     None
 *  \pre:        Channel value must be a configured ChannelId.
 *               Target Time must be with in the Max channel Tick value.
 *  \context:    Called by upper layer.
 *  \note:       -
 ******************************************************************************************************************/
/* SourceId : GPT_SourceId_016 */
/* DesignId : GPT_DesignId_012  */
/* Requirements : AR_GPT_SR89,AR_GPT_SR90,AR_GPT_SR91,AR_GPT_SR92,AR_GPT_SR93,AR_GPT_SR94,AR_GPT_SR95,AR_GPT_SR96,
*                 AR_GPT_SR97 */
FUNC(void, GPT_CODE) Gpt_StartTimer ( Gpt_ChannelType channel, Gpt_ValueType value )
{
   uint32 ChannelMode_t;
   Gpt_ChannelType Gpt_Channel;   

   /* Check if the driver has been successfully initialized. If the driver
    * has not been initialized, report an error and return immediately.
    */
#if (STD_ON == GPT_DEV_ERROR_DETECT)
   if (NULL_PTR == Gpt_Config_pt)
   {
      (void)Gpt_Errorhook(GPT_SID_START_TIMER, GPT_E_UNINIT);      
   }
   /* Check if parameter channel is in valid range and not in Uninitialized mode. If its value is out
    * of range report an error and set flag.
    */
   else if (( channel >= Gpt_Config_pt->ChannelCount ) || \
		   (GPT_UNINITIALIZED == Gpt_ChannelState[Gpt_Config_pt->ChannelConfig_pt[channel].ChannelId]))
   {
      (void)Gpt_Errorhook ( GPT_SID_START_TIMER, GPT_E_PARAM_CHANNEL );      
   }
   /* Check if parameter value is in valid range. If "0" is specified as
    * value, report an error and set flag.
    */
   else if ( (value == 0U) || (value >= MAX_RESOLUTION) )
   {
      (void)Gpt_Errorhook ( GPT_SID_START_TIMER, GPT_E_PARAM_VALUE );      
   }
   /*Check if channel is running, report error GPT_E_BUSY if channel already in "Running" state*/
   else if (GPT_RUNNING == Gpt_ChannelState[Gpt_Config_pt->ChannelConfig_pt[channel].ChannelId])
   {
      (void)Gpt_Errorhook ( GPT_SID_START_TIMER, GPT_E_BUSY );      
   }
   else   
#endif   
   {
      /*Read the Channel Id and channel mode from configured channel*/
      Gpt_Channel = Gpt_Config_pt->ChannelConfig_pt[channel].ChannelId;
      ChannelMode_t = Gpt_Config_pt->ChannelConfig_pt[channel].ChannelMode;

      /* Critical section, block the interrupts */
      Gpt_GlobalSuspend();

      /*Set the channel mode for notification index*/
      Gpt_IsrIndex[Gpt_Channel] = ChannelMode_t;

      /*Set the register settings required to start selected channel with defined target time*/
      Gpt_SetRegStartTimer(channel, value);

      /*Set the channel state to "Running"*/
      Gpt_ChannelState[Gpt_Channel] = GPT_RUNNING;

      /* Critical section, restore the interrupts */
      Gpt_GlobalRestore();

    }/*if(Dev_Error_Flag == FALSE)*/

}/* Gpt_StartTimer */

/******************************************************************************************************************
    Function name:  Gpt_SetRegStartTimer
 ******************************************************************************************************************/
/*  \Description: Gpt_SetRegStartTimer will set registers to start the selected timer channel with defined target
 *                time.
 *  \param[in]:   Channel - ChannelId of the Gpt channel.
 *                Target Time - Target time to be set for selected channel in Ticks.
 *  \return:      None
 *  \context:     Called by Gpt_StartTimer function.
 ******************************************************************************************************************/
static FUNC(void, GPT_CODE) Gpt_SetRegStartTimer (Gpt_ChannelType channel, Gpt_ValueType value )
{
   uint32 ChannelMode_t;
   Gpt_ChannelType Gpt_Channel;

   /*Read the Channel Id and channel mode from configured channel*/
   /*SAFETYMCUSW 45 D MR:21.1 <APPROVED> "Pointer is checked for NULL in init function" */
   Gpt_Channel = Gpt_Config_pt->ChannelConfig_pt[channel].ChannelId;
   /*SAFETYMCUSW 45 D MR:21.1 <APPROVED> "Pointer is checked for NULL in init function" */
   ChannelMode_t = Gpt_Config_pt->ChannelConfig_pt[channel].ChannelMode;

   switch (Gpt_Channel)
   {
      case RTI1_CHANNEL1 :
         /*Reset Up counter*/
         GPT_RTI1_UC0 = 0U;
         /*Reset Free running counter*/
         GPT_RTI1_FRC0 = 0U;
         /* Prescaler clock */
         GPT_RTI1_CPUC0 = (Gpt_Config_pt->ChannelConfig_pt[channel].GptChannelTickFrequency + 1U);
         /*Store the referenced channel Call Back Notification function Pointer*/
         Gpt_ChannelNotifyFunctions[Gpt_Channel] = Gpt_Config_pt->ChannelConfig_pt[channel].Notification_pt;

         if(ChannelMode_t == GPT_CH_MODE_ONESHOT)
         {
            GPT_RTI1_COMP0 = value;
            GPT_RTI1_UDCP0 = 0U;
            GPT_RTI1_SETINT = GPT_RTI1_SETINT | GPT_SETINT0_MASK;
         }
         else /*Continuous mode*/
         {
            GPT_RTI1_COMP0 = value;
            GPT_RTI1_UDCP0 = value;
         }
         /*Enable Channel0*/
         GPT_RTI1_GCTRL = GPT_RTI1_GCTRL | GPT_CNT0EN_MASK;
          break;
      case RTI1_CHANNEL2 :
         GPT_RTI1_UC1 = 0U;
         GPT_RTI1_FRC1 = 0U;
         /* Prescaler clock */
         GPT_RTI1_CPUC1 = (Gpt_Config_pt->ChannelConfig_pt[channel].GptChannelTickFrequency + 1U);

         /*Store the referenced channel Call Back Notification function Pointer*/
         Gpt_ChannelNotifyFunctions[Gpt_Channel] = Gpt_Config_pt->ChannelConfig_pt[channel].Notification_pt;
         if(ChannelMode_t == GPT_CH_MODE_ONESHOT)
         {
            GPT_RTI1_COMP1 = value;
            GPT_RTI1_UDCP1 = 0U;
            GPT_RTI1_SETINT = GPT_RTI1_SETINT | GPT_SETINT1_MASK;
         }/*Continuous mode*/
         else
         {
            GPT_RTI1_COMP1 = value;
            GPT_RTI1_UDCP1 = value;
         }
         /*Enable Channel1*/
         GPT_RTI1_GCTRL = GPT_RTI1_GCTRL | GPT_CNT1EN_MASK;
         break;
      default:
         break;
   }
}

/****************************************************************************************************************
    Function name:  Gpt_StopTimer
 ***************************************************************************************************************/
/*  \Description: Gpt_StopTimer will stop the selected timer channel.This will clear all the registers
 *                corresponding to the selected channel.
 *                The state of the timer channel will be changed to "Stopped".If the channel is in state
 *                 "Initialized","Expired", "Stopped" before calling this function, the function will be
 *                 left without any action.
 *  \param[in]:   Channel - ChannelId of the Gpt channel.
 *  \return:      None
 *  \pre:         Channel value must be a configured ChannelId.
 *  \context:     Called by upper layer.
 *  \note:        Channel must be in "Running" state before calling this function
 ****************************************************************************************************************/
/* SourceId : GPT_SourceId_017 */
/* DesignId : GPT_DesignId_013  */
/* Requirements : AR_GPT_SR98,AR_GPT_SR99,AR_GPT_SR100,AR_GPT_SR101,AR_GPT_SR102,AR_GPT_SR103,AR_GPT_SR104,
*                 AR_GPT_SR105 */
FUNC(void, GPT_CODE) Gpt_StopTimer ( Gpt_ChannelType channel )
{   
   Gpt_ChannelType Gpt_Channel;

   /* Check if the driver has been successfully initialized. If the driver
    * has not been initialized, report an error and return immediately.
    */
#if (STD_ON == GPT_DEV_ERROR_DETECT)
   if (NULL_PTR == Gpt_Config_pt)
   {
      (void)Gpt_Errorhook(GPT_SID_STOP_TIMER, GPT_E_UNINIT);      
   }
   /* Check if parameter channel is in valid range. If its value is out
    * of range report an error and return immediately.
    */
   else if(channel >= Gpt_Config_pt->ChannelCount)
   {
      (void)Gpt_Errorhook ( GPT_SID_STOP_TIMER, GPT_E_PARAM_CHANNEL );      
   }
   else   
#endif   
   {
      Gpt_Channel = Gpt_Config_pt->ChannelConfig_pt[channel].ChannelId;
      /*Check for Channel State, if channel is not in "Running" state
       * there will be no state change  */
      if( Gpt_ChannelState[Gpt_Channel] == GPT_RUNNING )
      {
         /* Critical section, block the interrupts */
         Gpt_GlobalSuspend();

         switch (Gpt_Channel)
         {
            case RTI1_CHANNEL1 :
               GPT_RTI1_GCTRL = (GPT_RTI1_GCTRL & (~GPT_CNT0EN_MASK));/*Disable Channel0*/
               break;
            case RTI1_CHANNEL2 :
               GPT_RTI1_GCTRL = (GPT_RTI1_GCTRL & (~GPT_CNT1EN_MASK));/*Disable Channel1*/
               break;
            default:
               break;
         } /* End of switch */

      /* Mark the channel as Stopped */
      Gpt_ChannelState[Gpt_Channel] = GPT_STOPPED;

      /* Critical section, restore the interrupts */
      Gpt_GlobalRestore();
      }/*If(Channel_State == GPT_RUNNING)*/
   }
} /* Gpt_StopTimer */

#if (STD_ON == GPT_ENABLE_DISABLE_NOTIFICATION_API)
/******************************************************************************************************************
    Function name:  Gpt_EnableNotification
 ******************************************************************************************************************/
/*  \Description: Gpt_EnableNotification will enable the interrupt notification for the selected channel.
 *                The SETINT bit in RTI Set interrupt register will be Set to enable the Compare interrupt.
 *                The interrupt is triggered when Free running counter value matches with compare register value.
 *  \param[in]:   Channel - ChannelId of the Gpt channel.
 *  \return:      None
 *  \pre:         Channel value must be a configured ChannelId.
 *  \context:     Called by upper layer.
 *  \note:        Gpt Driver should be initialized before calling this function and Notification call back function
 *                should be configured.
 *******************************************************************************************************************/
/* SourceId : GPT_SourceId_018 */
/* DesignId : GPT_DesignId_014  */
/* Requirements : AR_GPT_SR106,AR_GPT_SR107,AR_GPT_SR108,AR_GPT_SR109,AR_GPT_SR110,AR_GPT_SR111,AR_GPT_SR112 */
FUNC(void, GPT_CODE) Gpt_EnableNotification( Gpt_ChannelType channel )
{
   Gpt_ChannelType Gpt_Channel;   

   /* Check if the driver has been successfully initialized. If the driver
    * has not been initialized, report an error and return immediately.
    */
#if (STD_ON == GPT_DEV_ERROR_DETECT)
   if ( NULL_PTR == Gpt_Config_pt )
   {
      (void)Gpt_Errorhook(GPT_SID_ENABLE_NOTIFY, GPT_E_UNINIT);      
   }
   /* Check if parameter channel is in valid range. If its value is out
    * of range report an error and return immediately.
    */
   else if ( channel >= Gpt_Config_pt->ChannelCount )
   {
      (void)Gpt_Errorhook ( GPT_SID_ENABLE_NOTIFY, GPT_E_PARAM_CHANNEL );      
   }
   else if (NULL_PTR == Gpt_Config_pt->ChannelConfig_pt[channel].Notification_pt)
   {      
      (void)Gpt_Errorhook(GPT_SID_ENABLE_NOTIFY, GPT_E_PARAM_CHANNEL);      
   }
   else
#endif   
   {
      /* Critical section, block the interrupts */
      Gpt_GlobalSuspend();

      Gpt_Channel = Gpt_Config_pt->ChannelConfig_pt[channel].ChannelId;

      switch (Gpt_Channel)/*GPT Channel of Configured channel*/
      {
         case RTI1_CHANNEL1 :
            GPT_RTI1_INTFLAG = GPT_INT0_MASK;  /* Reset Interrupt Flag */
            GPT_RTI1_SETINT = (GPT_RTI1_SETINT | GPT_SETINT0_MASK);
            break;
         case RTI1_CHANNEL2 :
            GPT_RTI1_INTFLAG = GPT_INT1_MASK;  /* Reset Interrupt Flag */
            GPT_RTI1_SETINT = (GPT_RTI1_SETINT | GPT_SETINT1_MASK);
            break;
         default:
            break;
      }
      /* Mark the notification for this channel */
      Gpt_ActiveNotifyChannels = (Gpt_ActiveNotifyChannels | (1u << Gpt_Channel));

      /* Critical section, restore the interrupts */
      Gpt_GlobalRestore();

   }

} /* Gpt_EnableNotification */
#endif /* GPT_ENABLE_DISABLE_NOTIFICATION_API */

#if (STD_ON == GPT_ENABLE_DISABLE_NOTIFICATION_API)
/******************************************************************************************************************
    Function name:  Gpt_DisableNotification
 ******************************************************************************************************************/
/*  \Description: Gpt_DisableNotification will disable the compare interrupt notification for the selected channel.
 *                The SETINT bit in RTI Set interrupt register will be cleared to disable the Compare interrupt.
 *                The interrupt is triggered when Free running counter value matches with compare register value.
 *  \param[in]:   Channel - ChannelId of the Gpt channel.
 *  \return:      None
 *  \pre:         Channel value must be a configured ChannelId.
 *  \context:     Called by upper layer.
 *  \note:        Gpt Driver should be initialized before calling this function.
 ********************************************************************************************************************/
/* SourceId : GPT_SourceId_019 */
/* DesignId : GPT_DesignId_015  */
/* Requirements : AR_GPT_SR113,AR_GPT_SR114,AR_GPT_SR115,AR_GPT_SR116,AR_GPT_SR117,AR_GPT_SR118,AR_GPT_SR119 */
FUNC(void, GPT_CODE) Gpt_DisableNotification( Gpt_ChannelType channel )
{   
   Gpt_ChannelType Gpt_Channel;

   /* Check if the driver has been successfully initialized. If the driver
    * has not been initialized, report an error and return immediately.
    */
#if (STD_ON == GPT_DEV_ERROR_DETECT)
   if ( NULL_PTR == Gpt_Config_pt )
   {
      (void)Gpt_Errorhook(GPT_SID_DISABLE_NOTIFY, GPT_E_UNINIT);      
   }
   /* Check if parameter channel is in valid range. If its value is out
    * of range report an error and return immediately.
    */
   else if ( channel >= Gpt_Config_pt->ChannelCount )
   {
      (void)Gpt_Errorhook ( GPT_SID_DISABLE_NOTIFY, GPT_E_PARAM_CHANNEL );      
   }
   else if (NULL_PTR == Gpt_Config_pt->ChannelConfig_pt[channel].Notification_pt)
   {      
      (void)Gpt_Errorhook(GPT_SID_DISABLE_NOTIFY, GPT_E_PARAM_CHANNEL);    
   }
   else
#endif   
   {
      Gpt_Channel = Gpt_Config_pt->ChannelConfig_pt[channel].ChannelId;

      /* Critical section, block the interrupts */
      Gpt_GlobalSuspend();

      switch (Gpt_Channel)
      {
         case RTI1_CHANNEL1 :
            GPT_RTI1_INTFLAG = GPT_INT0_MASK;  /* Reset Interrupt Flag */
            GPT_RTI1_CLEARINT = GPT_CLEARINT0_MASK; /*Clear Interrupt bit*/
            break;
         case RTI1_CHANNEL2 :
            GPT_RTI1_INTFLAG = GPT_INT1_MASK;  /* Reset Interrupt Flag */
            GPT_RTI1_CLEARINT = GPT_CLEARINT1_MASK;
            break;
         default:
            break;
      }
      /* Mark the notification for this channel */
      Gpt_ActiveNotifyChannels = (uint32)(Gpt_ActiveNotifyChannels & (~((uint32)1u << Gpt_Channel)));

      /* Critical section, restore the interrupts */
      Gpt_GlobalRestore();
}

} /* Gpt_DisableNotification */
#endif /* GPT_ENABLE_DISABLE_NOTIFICATION_API */

#if (STD_ON == GPT_WAKEUP_FUNCTIONALITY_API)
/********************************************************************************************************************
    Function name:  Gpt_SetMode
 ********************************************************************************************************************/
/*  \Description: Gpt_SetMode will set the operation mode of the Gpt driver to the given set mode parameter.
 *                If the parameter mode = Normal, then the function will enable the interrupt notifications
 *                for all the channels which are configured for notification and notification is enabled.
 *                If the Mode = Sleep, the function will enable the wakeup interrupts for all channels which are
 *                configured for wakeup and disable all other channels.All the timer channels which are running will
 *                be stopped in this mode.
 *  \param[in]:   Mode - Normal Mode or Sleep Mode.
 *  \return:      None
 *  \context:     Called by upper layer.
 *  \note:        Gpt Driver should be initialized before calling this function.
 *********************************************************************************************************************/
/* SourceId : GPT_SourceId_020 */
/* DesignId : GPT_DesignId_016  */
/* Requirements : AR_GPT_SR120,AR_GPT_SR121,AR_GPT_SR122,AR_GPT_SR123,AR_GPT_SR124,AR_GPT_SR125,AR_GPT_SR126,
*                 AR_GPT_SR127,AR_GPT_SR128,AR_GPT_SR129,AR_GPT_SR130 */
FUNC(void, GPT_CODE) Gpt_SetMode ( Gpt_ModeType mode )
{
   /* Check if the driver has been successfully initialized. If the driver
    * has not been initialized, report an error and return immediately.
    */
#if (STD_ON == GPT_DEV_ERROR_DETECT)
   if ( NULL_PTR == Gpt_Config_pt )
   {
      (void)Gpt_Errorhook(GPT_SID_SET_MODE, GPT_E_UNINIT);      
   }
   /* Check if the mode parameter is valid. If not report an error and return immediately */
   else if (( mode != GPT_MODE_SLEEP ) && ( mode != GPT_MODE_NORMAL ))
   {
      (void)Gpt_Errorhook ( GPT_SID_SET_MODE, GPT_E_PARAM_MODE );      
   }
   else   
#endif   
   {
      if ( mode == GPT_MODE_SLEEP )
      {
         /* Set sleep mode as Gpt operation mode*/
         Gpt_SetSleepMode();
      }
      else /* Mode == GPT_MODE_NORMAL */
      {
         /* Set normal mode as Gpt operation mode*/
         Gpt_SetNormalMode();
      }
   }
} /* Gpt_SetMode */

/********************************************************************************************************************
    Function name:  Gpt_SetSleepMode
 ********************************************************************************************************************/
/*  \Description: Gpt_SetSleepMode will set the operation mode of the Gpt driver to sleep mode
 *  \param[in]:   None
 *  \return:      None
 *  \context:     Called by Gpt_SetMode function
 *********************************************************************************************************************/
static FUNC(void, GPT_CODE) Gpt_SetSleepMode(void)
{
   /* Setting to sleep mode already done before? */
   if ( Gpt_SleepMode == GPT_MODE_SLEEP )
   {
      /*Do Nothing. Return from the function*/
   }
   else
   {
      /* Perform the sleep sequence */
      Gpt_ChannelType ChannelIdx;
      for ( ChannelIdx = 0U; ChannelIdx < Gpt_Config_pt->ChannelCount; ChannelIdx++ )
      {
         Gpt_ChannelType Gpt_Channel;

         Gpt_Channel = Gpt_Config_pt->ChannelConfig_pt[ChannelIdx].ChannelId;
         if ( (Gpt_ActiveWakeupChannels & (((uint32)1U << Gpt_Channel))) != 0U)
         {
            /* This channel is configured as wakeup enabled */
            /* Assign wakeup index for this timer channel depending on channel mode */
            if (GPT_CH_MODE_ONESHOT == Gpt_Config_pt->ChannelConfig_pt[ChannelIdx].ChannelMode)
            {
               /* one shot timer channel */
               Gpt_IsrIndex[Gpt_Channel] = GPT_CH_MODE_ONESHOT_WAKEUP;
            }
            else
            {
               /* Continuous timer channel */
               Gpt_IsrIndex[Gpt_Channel] = GPT_CH_MODE_CONT_WAKEUP;
            }

            /* Critical section, block the interrupts */
            Gpt_GlobalSuspend();

            /* Enable Interrupt notification */
            switch (Gpt_Channel)/*GPT Channel of Configured channel*/
            {
               case RTI1_CHANNEL1 :
                  GPT_RTI1_INTFLAG = GPT_INT0_MASK;  /* Reset Interrupt Flag */
                  GPT_RTI1_SETINT = (GPT_RTI1_SETINT | GPT_SETINT0_MASK);
                  break;
               case RTI1_CHANNEL2 :
                  GPT_RTI1_INTFLAG = GPT_INT1_MASK;  /* Reset Interrupt Flag */
                  GPT_RTI1_SETINT = (GPT_RTI1_SETINT | GPT_SETINT1_MASK);
                  break;
               default:
                  break;
            }
            /* Critical section, restore the interrupts */
            Gpt_GlobalRestore();
         }
         else
         {
            /* Critical section, block the interrupts */
            Gpt_GlobalSuspend();

            /*Stop timer channel which is not configured for wakeup*/
            Gpt_StopTimer(ChannelIdx);

            /*Disable channel notification*/
            switch (Gpt_Channel)
            {
               case RTI1_CHANNEL1 :
                  GPT_RTI1_INTFLAG = GPT_INT0_MASK;  /* Reset Interrupt Flag */
                  GPT_RTI1_CLEARINT = GPT_CLEARINT0_MASK;
                  break;
               case RTI1_CHANNEL2 :
                  GPT_RTI1_INTFLAG = GPT_INT1_MASK;
                  GPT_RTI1_CLEARINT = GPT_CLEARINT1_MASK;
                  break;
               default:
                  break;
            }
            /* Critical section, restore the interrupts */
            Gpt_GlobalRestore();
         } /* End of if-else ( Gpt_ActiveWakeupChannels & ((uint32)(1 << Gpt_Channel)) ) */
      } /* End of for */

      /* Set marker for sleep mode */
      Gpt_SleepMode = GPT_MODE_SLEEP;
   } /* End of if-else (GPT_MODE_SLEEP == Gpt_SleepMode) */
}

/********************************************************************************************************************
    Function name:  Gpt_SetNormalMode
 ********************************************************************************************************************/
/*  \Description: Gpt_SetNormalMode will set the operation mode of the Gpt driver to normal mode
 *  \param[in]:   None
 *  \return:      None
 *  \context:     Called by Gpt_SetMode function
 *********************************************************************************************************************/
static FUNC(void, GPT_CODE) Gpt_SetNormalMode(void)
{
   /* Check if Setting to normal mode already done before. */
   if ( Gpt_SleepMode == GPT_MODE_NORMAL )
   {
      /* Do Nothing. Return from the function */
   }
   else /*Normal Mode*/
   {
#if (STD_ON == GPT_ENABLE_DISABLE_NOTIFICATION_API)
      Gpt_ChannelType ChannelIdx;/*Logical Channel Number*/
      Gpt_ChannelType Gpt_Channel;/*Gpt Channel*/
	  
      /* Check if Notification enabled for each channel. */
      for ( ChannelIdx = 0U; ChannelIdx < Gpt_Config_pt->ChannelCount; ChannelIdx++ )
      {
         Gpt_Channel = Gpt_Config_pt->ChannelConfig_pt[ChannelIdx].ChannelId;

         if( (Gpt_ActiveNotifyChannels & (((uint32)1U << Gpt_Channel))) != 0U)
         {
            /* Critical section, block the interrupts */
            Gpt_GlobalSuspend();

            Gpt_EnableNotification(ChannelIdx);

            /* Critical section, restore the interrupts */
            Gpt_GlobalRestore();
         }
         else
         {
            /* Critical section, block the interrupts */
            Gpt_GlobalSuspend();

            /*Stop the timer channel in Normal Mode*/
            Gpt_StopTimer(ChannelIdx);

            /*Disable notification for this channel*/
            Gpt_DisableNotification(ChannelIdx);

            /* Critical section, restore the interrupts */
            Gpt_GlobalRestore();
         }
      } /* End of for ChannelIdx */
#endif /* (STD_ON == GPT_ENABLE_DISABLE_NOTIFICATION_API) */
      Gpt_SleepMode = GPT_MODE_NORMAL;/*Set the Gpt mode to Normal*/
   } /* End of if-else (GPT_MODE_NORMAL == Gpt_SleepMode) */
}
#endif /* GPT_WAKEUP_FUNCTIONALITY_API */

#if (STD_ON == GPT_WAKEUP_FUNCTIONALITY_API)
/********************************************************************************************************************
    Function name:  Gpt_DisableWakeup
 ********************************************************************************************************************/
/*  \Description: Gpt_DisableWakeup will disable the wakeup interrupt of the referenced channel.
 *                The function will save the attribute "wakeup disable" of the channel.
 *  \param[in]:   ChannelID
 *  \return:      None
 *  \context:     Called by upper layer.
 *  \note:        Gpt Driver should be initialized before calling this function.
 *********************************************************************************************************************/
/* SourceId : GPT_SourceId_021 */
/* DesignId : GPT_DesignId_017  */
/* Requirements : AR_GPT_SR131,AR_GPT_SR132,AR_GPT_SR133,AR_GPT_SR134,AR_GPT_SR135,AR_GPT_SR136,AR_GPT_SR137 */
FUNC(void, GPT_CODE) Gpt_DisableWakeup ( Gpt_ChannelType channel )
{
   Gpt_ChannelType Gpt_Channel;   

   /* Check if the driver has been successfully initialized. If the driver
    * has not been initialized, report an error and return immediately.
    */
#if (STD_ON == GPT_DEV_ERROR_DETECT)
   if ( NULL_PTR == Gpt_Config_pt )
   {
      (void)Gpt_Errorhook(GPT_SID_DISABLE_WAKEUP, GPT_E_UNINIT);      
   }
   /* Check if parameter channel is in valid range. If its value is out
    * of range report an error and return immediately.
    */
   else if ( channel >= Gpt_Config_pt->ChannelCount )
   {
      (void)Gpt_Errorhook ( GPT_SID_DISABLE_WAKEUP, GPT_E_PARAM_CHANNEL );      
   }
   /* Check if channel is wakeup capable (reference ID != NULL_PTR).
    * If not report an error and return immediately. */
   else if (0U == Gpt_Config_pt->ChannelConfig_pt[channel].GptWakeupSourceRef )
   {
      (void)Gpt_Errorhook ( GPT_SID_DISABLE_WAKEUP, GPT_E_PARAM_CHANNEL );      
   }
   /*Check for GptEnableWakeup, if FALSE report error*/
   /*Get the Gpt Channel Id from the configured channel*/
   else if (FALSE == Gpt_Config_pt->ChannelConfig_pt[channel].GptEnableWakeup )
   {
     (void)Gpt_Errorhook ( GPT_SID_DISABLE_WAKEUP, GPT_E_PARAM_CHANNEL );     
   }
   else
#endif   
   {
      Gpt_Channel =  Gpt_Config_pt->ChannelConfig_pt[channel].ChannelId;

      /* Critical section, block the interrupts */
      Gpt_GlobalSuspend();

      /* Mark channel in wakeup matrix */
      Gpt_ActiveWakeupChannels = (Gpt_ActiveWakeupChannels & (~( (uint32)1U << Gpt_Channel)));

      /* Critical section, restore the interrupts */
      Gpt_GlobalRestore();
   }
} /* Gpt_DisableWakeup */
#endif /* (STD_ON == GPT_WAKEUP_FUNCTIONALITY_API) */

#if (STD_ON == GPT_WAKEUP_FUNCTIONALITY_API)
/********************************************************************************************************************
    Function name:  Gpt_EnableWakeup
 ********************************************************************************************************************/
/*  \Description: Gpt_EnableWakeup will enable the wakeup interrupt of the referenced channel.
 *                The function will save the attribute "wakeup enable" of the channel which only affects the wakeup
 *                interrupt when the driver is in Sleep mode.
 *  \param[in]:   ChannelID
 *  \return:      None
 *  \context:     Called by upper layer.
 *  \note:        Gpt Driver should be initialized before calling this function.
 *********************************************************************************************************************/
/* SourceId : GPT_SourceId_022 */
/* DesignId : GPT_DesignId_018  */
/* Requirements : AR_GPT_SR138,AR_GPT_SR139,AR_GPT_SR140,AR_GPT_SR141,AR_GPT_SR142,AR_GPT_SR143,AR_GPT_SR144 */
FUNC(void, GPT_CODE) Gpt_EnableWakeup ( Gpt_ChannelType channel )
{   
   Gpt_ChannelType Gpt_Channel;

   /* Check if the driver has been successfully initialized. If the driver
    * has not been initialized, report an error and return immediately.
    */
#if (STD_ON == GPT_DEV_ERROR_DETECT)
   if ( NULL_PTR == Gpt_Config_pt )
   {
      (void)Gpt_Errorhook(GPT_SID_ENABLE_WAKEUP, GPT_E_UNINIT);      
   }
   /* Check if parameter channel is in valid range. If its value is out
    * of range report an error and return immediately.
    */
   else if ( channel >= Gpt_Config_pt->ChannelCount )
   {
      (void)Gpt_Errorhook ( GPT_SID_ENABLE_WAKEUP, GPT_E_PARAM_CHANNEL );      
   }
   /* Check for GptEnableWakeup, if FALSE report error*/
   else if(FALSE == Gpt_Config_pt->ChannelConfig_pt[channel].GptEnableWakeup )
   {
      (void)Gpt_Errorhook ( GPT_SID_ENABLE_WAKEUP, GPT_E_PARAM_CHANNEL );      
   }
   /* Check if channel is wakeup capable (reference ID != 0). If not report an error and return immediately. */
   else if (0U == Gpt_Config_pt->ChannelConfig_pt[channel].GptWakeupSourceRef)
   {
      (void)Gpt_Errorhook ( GPT_SID_ENABLE_WAKEUP, GPT_E_PARAM_CHANNEL );      
   }
   else   
#endif   
   {
      /* Critical section, block the interrupts */
      Gpt_GlobalSuspend();

      /*To get the Gpt Channel Id from channel Configuration*/
      Gpt_Channel = Gpt_Config_pt->ChannelConfig_pt[channel].ChannelId;

      /*WakeupSource for the referenced channel is stored in GptWakeupSource*/
      GptWakeupSource[Gpt_Channel] = Gpt_Config_pt->ChannelConfig_pt[channel].GptWakeupSourceRef;

      /* Mark channel in wakeup matrix */
      Gpt_ActiveWakeupChannels = Gpt_ActiveWakeupChannels | ( (uint32) 1U << Gpt_Channel);

      /* Critical section, restore the interrupts */
      Gpt_GlobalRestore();
   }
} /* Gpt_EnableWakeup */
#endif /* (STD_ON == GPT_WAKEUP_FUNCTIONALITY_API) */

#if (STD_ON == GPT_WAKEUP_FUNCTIONALITY_API)
/********************************************************************************************************************
    Function name:  Gpt_CheckWakeup
 ********************************************************************************************************************/
/*  \Description: Gpt_CheckWakeup will check if wakeup capable timer channel is source for a wakeup event
 *                and call EcuM_SetWakeupEvent to indicate the valid timer wakeup event to the EcuM.
 *  \param[in]:   Wakeupsource
 *  \return:      None
 *  \context:     Interrupt Subroutine Call
 *  \note:        Gpt Driver should be in Sleep Mode and timer channel should be configured for wakeup.
 *********************************************************************************************************************/
/* SourceId : GPT_SourceId_023 */
/* DesignId : GPT_DesignId_019  */
/* Requirements : AR_GPT_SR145,AR_GPT_SR146,AR_GPT_SR147,AR_GPT_SR148,AR_GPT_SR149,AR_GPT_SR150 */
FUNC(void, GPT_CODE) Gpt_CheckWakeup ( EcuM_WakeupSourceType wakeupSource )
{
   Gpt_ChannelType Gpt_Channel = 0U;/*Gpt Channel Id*/
   Gpt_ChannelType ChannelIdx = 0U;/*Configured channel number*/
   EcuM_WakeupSourceType ChlWakeupRef = 0U;/*channel wakeup source*/   
   boolean Wakeup_Channel_Found = FALSE;

   /* Check if the driver has been successfully initialized. If the driver
    * has not been initialized, report an error and return immediately.
    */
#if (STD_ON == GPT_DEV_ERROR_DETECT)
   if ( NULL_PTR == Gpt_Config_pt )
   {
      (void)Gpt_Errorhook(GPT_SID_CBK_CHECK_WAKEUP, GPT_E_UNINIT);
      
   }
   /* Check if wakeupSource is valid (reference ID != 0) */
   else if (wakeupSource == 0U)
   {
      
   }
   else   
#endif   
   {
      /* -------------------- Loop over all channels ----------------------------------------
       * Check if delivered wakeupSource comes from configured wakeup capable timer channel
       */
      for ( ChannelIdx = 0U; ChannelIdx < Gpt_Config_pt->ChannelCount; ChannelIdx++ )
      {
         ChlWakeupRef = (EcuM_WakeupSourceType)(Gpt_Config_pt->ChannelConfig_pt[ChannelIdx].GptWakeupSourceRef );

         if (wakeupSource == ChlWakeupRef)
         {
            /* configured wakeup capable timer found; leave loop */
            Wakeup_Channel_Found = TRUE;
            Gpt_Channel = Gpt_Config_pt->ChannelConfig_pt[ChannelIdx].ChannelId;
            break;
         }
      } /* End of for */

      /* Check if configured wakeup capable timer was found */
      if (Wakeup_Channel_Found == FALSE )
      {
         /* no wakeup capable timer found */
      }
      else
      {
         /* Check if delivered wakeupSource comes from wakeup enabled timer channel */
         if (0U == ( Gpt_ActiveWakeupChannels & (1U << Gpt_Channel) ))
         {
            /*Wakeup Channel is not active, return from the function*/
         }
#if (STD_ON == GPT_REPORT_WAKEUP_SOURCE)
         else
         {
            EcuM_SetWakeupEvent (ChlWakeupRef);
         }
#endif /* (STD_ON == GPT_REPORT_WAKEUP_SOURCE) */		 
      }/*Else (Wakeup_Channel_Found == FALSE ) */
   }/*if(Dev_Error_Flag == FALSE)*/
} /* Gpt_CheckWakeup */
#endif /* (STD_ON == GPT_WAKEUP_FUNCTIONALITY_API) */


#if (STD_ON == GPT_SAFETI_API)
/**********************************************************************************************************************
 *   Function Name : Gpt_ConfigRegReadBack
 *********************************************************************************************************************/
/*! \Description: Register readback to check if static registers have expected values.
 *
 *  \param[out] : GptConfigReg*    GptConfigReg_pt        Pointer to where to store the register
 *                                                        values which are to be read.
 *  \return     : Std_ReturnType   Gpt_RetTmp       	  E_OK Value if no error. E_NOT_OK otherwise.
 *  \context    : Function is called from top layer applications.
 *********************************************************************************************************************/
/* SourceId : GPT_SourceId_024 */
/* DesignId : GPT_DesignId_021  */
/* Requirements : AR_GPT_SR186 */

FUNC(Std_ReturnType, GPT_CODE) Gpt_ConfigRegReadBack( GptConfigReg* GptConfigReg_pt )
{
	
	VAR(Std_ReturnType, AUTOMATIC)                         Gpt_RetTmp;

    Gpt_RetTmp                                  = (Std_ReturnType)E_NOT_OK;
   /*SAFETYMCUSW 440 S MR:11.3 <APPROVED> "Reason - NULL_PTR comes from Compiler.h" */
   if((NULL_PTR != GptConfigReg_pt ) && (NULL_PTR != Gpt_Config_pt))      
   {
	   GptConfigReg_pt->GPT_GCTRL  = GPT_RTI1_GCTRL;
	   GptConfigReg_pt->GPT_TBCTRL  = GPT_RTI1_TBCTRL;
	   GptConfigReg_pt->GPT_COMPCTRL  = GPT_RTI1_COMPCTRL;
	   GptConfigReg_pt->GPT_UDCP0  = GPT_RTI1_UDCP0;
	   GptConfigReg_pt->GPT_UDCP1  = GPT_RTI1_UDCP1;
	   GptConfigReg_pt->GPT_UDCP2  = GPT_RTI1_UDCP2;
	   GptConfigReg_pt->GPT_UDCP3  = GPT_RTI1_UDCP3;
	   
	   Gpt_RetTmp = (Std_ReturnType)E_OK;
   }
   return Gpt_RetTmp;
}

#endif

/***************************************************************************************************************
    Function name:  Gpt_NotifContIsr
 ***************************************************************************************************************/
/*  \Description: This Interrupt notification call function is called when timer channel is configured in
 *                Continuous mode and interrupt notification should be enabled for the channel.
 *                Gpt_StartTimer is called with the referenced channel. Once the timer channel is expired,
 *                interrupt is triggered which calls the Gpt_NotifContIsr call back function inside the timer Isr.
 *  \param[in]:  ChannelID
 *  \return:     None
 *  \context:    Called by ISR.
 *****************************************************************************************************************/

FUNC(void, GPT_CODE) Gpt_NotifContIsr( Gpt_ChannelType GptChannel )
{
   /* Critical section, block the interrupts */
   Gpt_GlobalSuspend();


   GPT_RTI1_INTFLAG = ((uint32)1U << (GptChannel & HWCHANNEL_MASK));

   if ( NULL_PTR != ( Gpt_ChannelNotifyFunctions[GptChannel] ))
   {
      /* Call notification callback */
      Gpt_ChannelNotifyFunctions[GptChannel]();
   }
   /* Critical section, restore the interrupts */
   Gpt_GlobalRestore();

} /* Gpt_NotifContIsr */

/*****************************************************************************************************************
    Function name:  Gpt_NotifSingleIsr
 *****************************************************************************************************************/
/*  \Description: This Interrupt notification callback function is called when timer channel is configured in
 *                Single mode and interrupt notification should be enabled for the channel.
 *                Gpt_StartTimer should be called with the referenced channel. Once the timer channel is expired,
 *                interrupt is triggered and Gpt_NotifSingleIsr call back function is called inside the subroutine.
 *                This call back function will stop the timer channel in One shot mode.
  *  \param[in]:  ChannelID
 *  \return:      None
 *  \context:     Called by ISR.
  *****************************************************************************************************************/

FUNC(void, GPT_CODE) Gpt_NotifSingleIsr( Gpt_ChannelType GptChannel )
{
   /* Critical section, block the interrupts */
   Gpt_GlobalSuspend();

   /*reset registers*/
   switch (GptChannel)
   {
      case RTI1_CHANNEL1 :
         /* Stop Counter Block 0*/
         GPT_RTI1_GCTRL = (GPT_RTI1_GCTRL & (~GPT_CNT0EN_MASK));
         /* Reset the Interrupt Flag */
         GPT_RTI1_INTFLAG = GPT_INT0_MASK;
         break;
      case RTI1_CHANNEL2 :
         /* Stop Counter Block 1*/
         GPT_RTI1_GCTRL = (GPT_RTI1_GCTRL & (~GPT_CNT1EN_MASK));
         /* Reset the Interrupt Flag */
         GPT_RTI1_INTFLAG = GPT_INT1_MASK;
         break;
      default:
         break;
   } /* End of switch */

   /* Mark the channel as inactive */
   Gpt_ChannelState[GptChannel] = GPT_EXPIRED;

   if ( NULL_PTR != ( Gpt_ChannelNotifyFunctions[GptChannel] ))
   {
      /* Call notification callback */
      Gpt_ChannelNotifyFunctions[GptChannel]();
   }
   /* Critical section, restore the interrupts */
   Gpt_GlobalRestore();

} /* Gpt_NotifSingleIsr */

/******************************************************************************************************************
    Function name:  Gpt_WakeupContIsr
 ******************************************************************************************************************/
/*  \Description: Gpt_WakeupContIsr is called when channel is configured for Wakeup and enabled for interrupt
 *                notification.
 *  \param[in]:   ChannelID
 *  \return:      None
 *  \context:     Called by Gpt_ChxIsr.
  ******************************************************************************************************************/

FUNC(void, GPT_CODE) Gpt_WakeupContIsr( Gpt_ChannelType GptChannel )
{
   /* Critical section, block the interrupts */
   Gpt_GlobalSuspend();

  GPT_RTI1_INTFLAG = ((uint32)1U << (GptChannel & HWCHANNEL_MASK));

#if (STD_ON == GPT_REPORT_WAKEUP_SOURCE)
   if(GptWakeupSource[GptChannel] != 0U)
   {
      /* Inform EcuM about wakeup source */
      EcuM_CheckWakeup (GptWakeupSource[GptChannel]);
   }
#endif /* (STD_ON == GPT_REPORT_WAKEUP_SOURCE) */

   /* Critical section, restore the interrupts */
   Gpt_GlobalRestore();

} /* Gpt_WakeupContIsr */

/************************************************************************************************************
    Function name:  Gpt_WakeupSingleIsr
 ************************************************************************************************************/
/*  \Description: Gpt_WakeupSingleIsr is called when channel is configured for Wakeup and
 *                enabled for interrupt notification and timer channel is configured in One-shot mode.
 *  \param[in]:   ChannelID
 *  \return:      None
 *  \context:     Called by Gpt_ChxIsr.
  ***********************************************************************************************************/

FUNC(void, GPT_CODE) Gpt_WakeupSingleIsr( Gpt_ChannelType GptChannel )
{
   /* Critical section, block the interrupts */
   Gpt_GlobalSuspend();

   /*Reset Registers*/
   switch (GptChannel)
   {
      case RTI1_CHANNEL1 :
         /* Operation of counter block */
         GPT_RTI1_GCTRL = (GPT_RTI1_GCTRL & (~GPT_CNT0EN_MASK));
         /* Reset the Interrupt Flag */
         GPT_RTI1_INTFLAG = GPT_INT0_MASK;
         break;
      case RTI1_CHANNEL2 :
         GPT_RTI1_GCTRL = (GPT_RTI1_GCTRL & (~GPT_CNT1EN_MASK));
         GPT_RTI1_INTFLAG = GPT_INT1_MASK;
         break;
      default:
         break;
   } /* End of switch */

#if (STD_ON == GPT_REPORT_WAKEUP_SOURCE)
   if(GptWakeupSource[GptChannel] != 0U)
   {
      /* Inform EcuM about wakeup source */
      EcuM_CheckWakeup (GptWakeupSource[GptChannel]);
   }
#endif

   /* Mark the channel as inactive */
   Gpt_ChannelState[GptChannel] = GPT_EXPIRED;

   /* Critical section, restore the interrupts */
   Gpt_GlobalRestore();

} /* Gpt_WakeupSingleIsr */

#define GPT_STOP_SEC_CODE
/*SAFETYMCUSW 338 S MR:19.1 <APPROVED> "Reason - This is the format to use for specifying memorysections."*/
#include "MemMap.h"



