/*!
* @file     Gpt.h
*
* @version  1.0.0
*
* @brief    AUTOSAR Gpt Driver header file
*
* @attention
*
* Project Name      : AUTOSAR MCAL 
*
* Platform          : Arm
*
* Autosar Standard  : Classic Platform 4.4.0
*
* Compiled Version  : G32A10xx_MCAL_1_0_0_01-Otc-25
*
* Copyright (C) 2026 Geehy Semiconductor
*
* You may not use this file except in compliance with the
* GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
*
* The program is only for reference, which is distributed in the hope
* that it will be usefull and instructional for customers to develop
* their software. Unless required by applicable law or agreed to in
* writing, the program is distributed on an "AS IS" BASIS, WITHOUT
* ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
* See the Geehy Semiconductor Software License Agreement for the governing permissions
* and limitations under the License.
*/

#ifndef GPT_H
#define GPT_H

#ifdef __cplusplus
extern "C" {
#endif

#include "Gpt_Cfg.h"
#include "EcuM_Cfg.h"

/** @addtogroup G32A10xx_StdPeriphDriver
  @{
*/

/** @defgroup GPT_Driver
  @{
*/

/** @defgroup GPT_Macros
  @{
*/
/**************************************************************************
                    VENDOR ID & AUTOSAR VERSION INFO
**************************************************************************/
/** Vendor ID (vendorId) of the dedicated implementation of this module according to the AUTOSAR vendor list. */
#define GPT_H_VENDOR_ID                     0x00C1U
/* Module ID of this module, as defined in the BSW Module List. */
#define GPT_H_MODULE_ID                     0x0064U
/** Major version number of AUTOSAR release on which the appropriate implementation is based on. */
#define GPT_H_AR_RELEASE_MAJOR_VERSION      0x04U
/** Minor version number of AUTOSAR release on which the appropriate implementation is based on. */
#define GPT_H_AR_RELEASE_MINOR_VERSION      0x04U
/** Revision version number of AUTOSAR release on which the appropriate implementation is based on. */
#define GPT_H_AR_RELEASE_REVISION_VERSION   0x00U
/** Major version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define GPT_H_SW_MAJOR_VERSION              0x01U
/** Minor version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define GPT_H_SW_MINOR_VERSION              0x00U
/** Patch level version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define GPT_H_SW_PATCH_VERSION              0x00U

/*******************************************************************************
                                ERROR CODES
*******************************************************************************/
/** API service called without module initialization */
#define GPT_E_UNINIT                      ((uint8)0x0AU)

/** API service called when timer channel is still busy (running) */
#define GPT_E_BUSY                        ((uint8)0x0BU)

/** API service called when driver is in wrong mode */
#define GPT_E_MODE                        ((uint8)0x0CU)

/** API service for initialization called when already initialized */
#define GPT_E_ALREADY_INITIALIZED         ((uint8)0x0DU)

/** API error return code: Init function failed */
#define GPT_E_INIT_FAILED                 ((uint8)0x0EU)

/** API parameter checking: invalid channel */
#define GPT_E_PARAM_CHANNEL               ((uint8)0x14U)

/** API parameter checking: invalid value */
#define GPT_E_PARAM_VALUE                 ((uint8)0x15U)

/** API parameter checking: invalid pointer */
#define GPT_E_PARAM_POINTER               ((uint8)0x16U)

/** API parameter checking: invalid Predef Timer */
#define GPT_E_PARAM_PREDEF_TIMER          ((uint8)0x17U)

/** API parameter checking: invalid mode */
#define GPT_E_PARAM_MODE                  ((uint8)0x1FU)

/** Report development error when call a function in error status*/
#define GPT_E_INVALID_CALL                ((uint8)0xA0U)

/** eg. API Gpt_SetClockPreScalerMode service called with wrong parameter */
#define GPT_E_PARAM_CLOCK_MODE            ((uint8)0xA1U)

/*******************************************************************************
                            API Service ID List
*******************************************************************************/
#define GPT_INSTANCE_MODULE_ID            ((uint8)0x00U)

/** Service ID: Gpt_GetVersionInfo() */
#define GPT_GETVERSIONINFO_SRVID          ((uint8)0x00U)

/** Service ID: Gpt_Init() */
#define GPT_INIT_SRVID                    ((uint8)0x01U)

/** Service ID: Gpt_DeInit() */
#define GPT_DEINIT_SRVID                  ((uint8)0x02U)

/** Service ID: Gpt_GetTimeElapsed() */
#define GPT_TIMEELAPSED_SRVID             ((uint8)0x03U)

/** Service ID: Gpt_GetTimeRemaining() */
#define GPT_TIMEREMAINING_SRVID           ((uint8)0x04U)

/** Service ID: Gpt_StartTimer() */
#define GPT_STARTTIMER_SRVID              ((uint8)0x05U)

/** Service ID: Gpt_StopTimer() */
#define GPT_STOPTIMER_SRVID               ((uint8)0x06U)

/** Service ID: Gpt_EnableNotification() */
#define GPT_ENABLENOTIFICATION_SRVID      ((uint8)0x07U)

/** Service ID: Gpt_DisableNotification() */
#define GPT_DISABLENOTIFICATION_SRVID     ((uint8)0x08U)

/** Service ID: Gpt_SetMode() */
#define GPT_SETMODE_SRVID                 ((uint8)0x09U)

/** Service ID: Gpt_DisableWakeup() */
#define GPT_DISABLEWAKEUP_SRVID           ((uint8)0x0AU)

/** Service ID: Gpt_EnableWakeup() */
#define GPT_ENABLEWAKEUP_SRVID            ((uint8)0x0BU)

/** Service ID: Gpt_CheckWakeup() */
#define GPT_CHECKWAKEUP_SRVID             ((uint8)0x0CU)

/**@} end of group GPT_Macros */
/*******************************************************************************
                            ENUM DEFINITION
*******************************************************************************/
/** @defgroup GPT_Enumerations
  @{
*/

/**
 * @brief used for selection of different power modes.
 */
typedef enum
{
    GPT_MODE_NORMAL = 0U, //!< normal power mode
    GPT_MODE_SLEEP  = 1U  //!< sleep power mode
} Gpt_ModeType;


/**
 * @brief Gpt timer count mode select.
 */
typedef enum
{
    GPT_COUNT_MODE_CONTINUOUS = 0U, //!< continuous timer count mode
    GPT_COUNT_MODE_ONESHOT    = 1U  //!< oneshot timer count mode
} Gpt_CountModeType;


/**
 * @brief the selection of channel status type.
 */
typedef enum
{
    GPT_CHANNELSTATUS_UNINITIALIZED = 0,    //!< GPT channel status - uninitialized
    GPT_CHANNELSTATUS_INITIALIZED   = 1,    //!< GPT channel status - initialized
    GPT_CHANNELSTATUS_STOPPED       = 2,    //!< GPT channel status - stopped
    GPT_CHANNELSTATUS_EXPIRED       = 3,    //!< GPT channel status - expired
    GPT_CHANNELSTATUS_RUNNING       = 4     //!< GPT channel status - running
} Gpt_ChannelStatusType;


/**
 * @brief The ID number of the Tmr used by GPT.
 */
typedef enum
{
    GPT_TMR_RETAIN = 0U,    //!< tmr retain
    GPT_TMR1 = 1U,  //!< tmr1 ID
    GPT_TMR2 = 2U,  //!< tmr2 ID
    GPT_TMR3 = 3U,  //!< tmr3 ID
    GPT_TMR4 = 4U,  //!< tmr4 ID
    GPT_TMR6 = 6U,  //!< tmr6 ID
    GPT_TMR7 = 7U,  //!< tmr7 ID
    GPT_TMR8 = 8U   //!< tmr8 ID
} Gpt_TimerHwIdType;


/**
 * @brief tmr channel number
 */
typedef enum
{
    TMR_CHANNEL_RETAIN = 0U,    //!< tmr channel retain

    TMR_CHANNEL_1 = 0x1U,       //!< tmr1 channel ID
    TMR_CHANNEL_2 = 0x2U,       //!< tmr1 channe2 ID
    TMR_CHANNEL_3 = 0x3U,       //!< tmr1 channe3 ID
    TMR_CHANNEL_4 = 0x4U,       //!< tmr1 channe4 ID
    
    TMR_1_CHANNEL_1 = 0x11U,    //!< tmr1 channel ID
    TMR_1_CHANNEL_2 = 0x12U,    //!< tmr1 channe2 ID
    TMR_1_CHANNEL_3 = 0x13U,    //!< tmr1 channe3 ID
    TMR_1_CHANNEL_4 = 0x14U,    //!< tmr1 channe4 ID
    
    TMR_2_CHANNEL_1 = 0x21U,    //!< tmr2 channel ID
    TMR_2_CHANNEL_2 = 0x22U,    //!< tmr2 channe2 ID
    TMR_2_CHANNEL_3 = 0x23U,    //!< tmr2 channe3 ID
    TMR_2_CHANNEL_4 = 0x24U,    //!< tmr2 channe4 ID
    
    TMR_3_CHANNEL_1 = 0x31U,    //!< tmr3 channel ID
    TMR_3_CHANNEL_2 = 0x32U,    //!< tmr3 channe2 ID
    TMR_3_CHANNEL_3 = 0x33U,    //!< tmr3 channe3 ID
    TMR_3_CHANNEL_4 = 0x34U,    //!< tmr3 channe4 ID
    
    TMR_4_CHANNEL_1 = 0x41U,    //!< tmr4 channel ID
    TMR_4_CHANNEL_2 = 0x42U,    //!< tmr4 channe2 ID
    TMR_4_CHANNEL_3 = 0x43U,    //!< tmr4 channe3 ID
    TMR_4_CHANNEL_4 = 0x44U,    //!< tmr4 channe4 ID
    
    TMR_6_CHANNEL   = 0x61U,    //!< tmr6 channe ID
    TMR_7_CHANNEL   = 0x71U,    //!< tmr7 channe ID
    TMR_8_CHANNEL   = 0x81U     //!< tmr8 channe ID
} Gpt_TimerHwChannelIdType;

/**@} end of group GPT_Enumerations */
/*******************************************************************************
                            STRUCT DEFINITION
*******************************************************************************/
/** @defgroup GPT_Typedef
  @{
*/

/** Gpt channel notification type. */
typedef P2FUNC(void, GPT_APPL_CODE, Gpt_ChannelNotificationType)(void);

/** Numeric ID of a GPT channel */
typedef uint8  Gpt_ChannelType;

/** Type for reading and setting the timer values */
typedef uint32 Gpt_ValueType;

/**@} end of group GPT_Typedef */

/** @defgroup GPT_Structure
  @{
*/

/**
* @brief the struct of Gpt Channel runtime Info
*/
typedef struct
{
#if(STD_ON  == GPT_ENABLE_DISABLE_NOTIFICATION_API_ENABLE)
    VAR(boolean, GPT_VAR) notificationEnabled; //!< notificatuon enable
#endif
#if((STD_ON  == GPT_ENABLE_DISABLE_WAKEUP_FUNCTIONALITY_API_ENABLE) && (STD_ON == GPT_REPORT_WAKEUP_SOURCE_ENABLE))
    VAR(boolean, GPT_VAR) wakeupEnabled;   //!< wakeup enabled flag at runtime
    VAR(boolean, GPT_VAR) wakeupGenerated; //!< indicate wakeup has precessed
#endif
} Gpt_ChannelInfoType;

/**
* @brief the struct of Gpt channel config
*/
typedef struct {
    Gpt_ChannelType             ChannelId;  //!< channle id
    Gpt_TimerHwChannelIdType    HwChannel;  //!< Corresponding hardware channel
    Gpt_ChannelNotificationType Notification; //!< User-defined callback function (if present)
    Gpt_ValueType               ChannelValueMax; //!< Maximum channel count
    boolean                     EnableWakeup; //!< Wake-up Function Switch
    VAR(EcuM_WakeupSourceType, GPT_VAR) Gpt_WakeupSource; //!< Wake-up Source Number
} Gpt_ChannelConfigType;

/**
* @brief the struct of gpt hw unit config
*/
typedef struct
{
    Gpt_TimerHwIdType HardwareUnitId;   //!< Hardware TMR Number
    Gpt_CountModeType ChannelMode;   //!< Channel Mode
    uint8_t ChannelNum;   //!< Number of channelsid
    uint16_t ChannelPrescaler;   //!< Prescaler Value
} Gpt_HwUnitConfigType;

/**
* @brief the struct of gpt config
*/
typedef struct
{
    CONST(Gpt_ChannelType, GPT_CONST) Gpt_ChannelCount; //!< Total Number of Channels
    CONST(Gpt_ChannelType, GPT_CONST) (*gptHwMapPtr); //!< Channel user return survey table
    CONST(Gpt_HwUnitConfigType, GPT_CONST) (*gptTmrConfigPtr);  //!< TMR Configuration
    CONST(Gpt_ChannelConfigType, GPT_CONST) (*ChannelConfig); //!< Channel Configuration
} Gpt_ConfigType;

/**
* @brief the struct of gpt runtime state
*/
typedef struct {
    Gpt_ModeType ModuleState;
    Gpt_ChannelStatusType ChannelState[GPT_CHANNEL_IDX_NUM]; 
    Gpt_ValueType MaxValueState[GPT_CHANNEL_IDX_NUM];  
    boolean IsrState[GPT_CHANNEL_IDX_NUM]; 
    boolean WakeupState[GPT_CHANNEL_IDX_NUM];  
} Gpt_RuntimeStateType;


typedef struct {
    uint32 channelCCValue[6];
} Gpt_ChannelCCValueType;

/**@} end of group GPT_Structure */
/*******************************************************************************
                             CONSTANT DEFINITION
*******************************************************************************/
/** @defgroup GPT_Variable
  @{
*/

#define GPT_START_SEC_VAR_INIT_UNSPECIFIED
#include "Gpt_MemMap.h"

/** point to GPT driver coniguration data. */
extern P2CONST(Gpt_ConfigType, GPT_VAR, GPT_APPL_CONST) gs_gptConfigPtr;

#define GPT_STOP_SEC_VAR_INIT_UNSPECIFIED
#include "GPT_MemMap.h"

#define GPT_START_SEC_VAR_NO_INIT_UNSPECIFIED
#include "Gpt_MemMap.h"

extern VAR(Gpt_ChannelInfoType, GPT_VAR) gs_gptChannelInfo[GPT_CHANNEL_IDX_NUM];

extern VAR(Gpt_RuntimeStateType,GPT_VAR) gs_GptRuntimeState;

#define GPT_STOP_SEC_VAR_NO_INIT_UNSPECIFIED
#include "Gpt_MemMap.h"

extern const Gpt_ConfigType g_gptConfg;

/**@} end of group GPT_Variable */
/*******************************************************************************
                            FUNCTION DECLARATION
*******************************************************************************/
#define GPT_START_SEC_CODE
#include "Gpt_MemMap.h"

/** @defgroup GPT_Fuctions
  @{
*/

FUNC(void, GPT_CODE) Gpt_Init(P2CONST(Gpt_ConfigType, AUTOMATIC, GPT_APPL_CONST) ConfigPtr);
FUNC(void, GPT_CODE) Gpt_StartTimer(VAR(Gpt_ChannelType, AUTOMATIC) Channel, VAR(Gpt_ValueType, AUTOMATIC) Value);
FUNC(void, GPT_CODE) Gpt_StopTimer(VAR(Gpt_ChannelType, AUTOMATIC) Channel);

#if (GPT_TIME_ELAPSED_API_ENABLE == STD_ON)
FUNC(Gpt_ValueType, GPT_CODE) Gpt_GetTimeElapsed(VAR(Gpt_ChannelType, AUTOMATIC) Channel);
#endif

#if (GPT_TIME_REMAINING_API_ENABLE == STD_ON)
FUNC(Gpt_ValueType, GPT_CODE) Gpt_GetTimeRemaining(VAR(Gpt_ChannelType, AUTOMATIC) Channel);
#endif

#if(STD_ON == GPT_ENABLE_DISABLE_NOTIFICATION_API_ENABLE)
FUNC(void, GPT_CODE) Gpt_EnableNotification(VAR(Gpt_ChannelType, AUTOMATIC) Channel);
FUNC(void, GPT_CODE) Gpt_DisableNotification(VAR(Gpt_ChannelType, AUTOMATIC) Channel);
#endif

#if (STD_ON  == GPT_ENABLE_DISABLE_WAKEUP_FUNCTIONALITY_API_ENABLE)
FUNC(void, GPT_CODE) Gpt_EnableWakeup(VAR(Gpt_ChannelType, AUTOMATIC) Channel);
FUNC(void, GPT_CODE) Gpt_DisableWakeup(VAR(Gpt_ChannelType, AUTOMATIC) Channel);
FUNC(void, GPT_CODE) Gpt_CheckWakeup(VAR(EcuM_WakeupSourceType, AUTOMATIC) WakeupSource);
FUNC(void, GPT_CODE) Gpt_SetMode(VAR(Gpt_ModeType, AUTOMATIC) Mode);
#endif

/**@} end of group GPT_Fuctions */

#define GPT_STOP_SEC_CODE
#include "Gpt_MemMap.h"

#ifdef __cplusplus
}
#endif

#endif /* GPT_TMR_TYPES_H */

/**@} end of group GPT_Driver */
/**@} end of group G32A10xx_StdPeriphDriver */
