/**********************************************************************************************************************
 *  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:  Pwm.h
 *    Component:  Tms570_AutosarPwmDriver
 *       Module:  PwmDriver
 *    Generator:  -
 *
 *  Description:  This component provides services for initialization and control of the microcontroller internal
 *                PWM unit (pulse width modulation). The PWM module generates pulses with variable pulse width.
 *                It allows the selection of the duty cycle and the signal period time.
 *
 *
 *********************************************************************************************************************/

/**********************************************************************************************************************
 *---------------------------------------------------------------------------------------------------------------------
 * Author:  Vineeth Thomas Alex
 *---------------------------------------------------------------------------------------------------------------------
 * Revision History
 *---------------------------------------------------------------------------------------------------------------------
 * Version     Date           Author                   Change ID          Description
 *---------------------------------------------------------------------------------------------------------------------
 * 02.21.00    30Sep2014     Vineeth Thomas Alex      SDOCM00113122      Initial version adapted from TMS570LS3137
 * 02.21.01    23Aug2016     Vishwanath Reddy         SDOCM00122620      Vresion info updated. 
 * 02.30.00    01Dec2016     Sudhakar Singh           None               Update version info
 * 05.30.00    15Mar2017     Sudhakar Singh           SDOCM00122834      Version supports TMS570LSxx Family of Devices.
 *********************************************************************************************************************/
#ifndef PWM_H
#define PWM_H

#define PWM_SOURCE
/**********************************************************************************************************************
 * INCLUDES
 *********************************************************************************************************************/
/* include of configuration public part */
#include "Pwm_Cfg.h"
#include "Std_Types.h"
/**********************************************************************************************************************
 *  GLOBAL CONSTANT MACROS
 *********************************************************************************************************************/

/* AUTOSAR Release Version Information */

#define PWM_AR_RELEASE_MAJOR_VERSION       (0x04U)
#define PWM_AR_RELEASE_MINOR_VERSION       (0x00U)
#define PWM_AR_RELEASE_REVISION_VERSION    (0x03U)

#define PWM_VENDOR_ID              (44U)
#define PWM_MODULE_ID              (121U)

#define PWM_AR_MAJOR_VERSION               (5U)
#define PWM_AR_MINOR_VERSION       (5U)
#define PWM_AR_PATCH_VERSION               (0U)

/* Component Version Information */
#define PWM_SW_MAJOR_VERSION               (5U)
#define PWM_SW_MINOR_VERSION               (30U)
#define PWM_SW_PATCH_VERSION               (0U)

/* ----------Error Codes in Development-Mode--------------------------------- */
/* SourceId : PWM_SourceId_001 */
/* DesignId : PWM_DesignId_06  */
/* Requirements : AR_PWM_SR7, AR_PWM_SR40, AR_PWM_SR8, AR_PWM_SR9, AR_PWM_SR10, AR_PWM_SR11, AR_PWM_SR42, AR_PWM_SR12*/
#ifndef PWM_E_PARAM_CONFIG
#define PWM_E_PARAM_CONFIG              ( 0x10U ) /* invalid ptr to cfg       */
#endif
#ifndef PWM_E_UNINIT
#define PWM_E_UNINIT                    ( 0x11U ) /* pwm not initialized      */
#endif
#ifndef PWM_E_PARAM_CHANNEL
#define PWM_E_PARAM_CHANNEL             ( 0x12U ) /* parameter invalid        */
#endif
#ifndef PWM_E_PERIOD_UNCHANGEABLE
#define PWM_E_PERIOD_UNCHANGEABLE       ( 0x13U ) /* fixed period channel     */
#endif
#ifndef PWM_E_ALREADY_INITIALIZED
#define PWM_E_ALREADY_INITIALIZED       ( 0x14U ) /* pwm already initialized  */
#endif
#ifndef PWM_E_PARAM_POINTER
#define PWM_E_PARAM_POINTER             ( 0x20U ) /* Vinfo Pointer is NULL    */
#endif
#ifndef PWM_E_PARAM_PERIOD
#define PWM_E_PARAM_PERIOD              ( 0x21U ) /* Period is out of range   */
#endif
#ifndef PWM_E_NOTIFY_NULL
#define PWM_E_NOTIFY_NULL               ( 0x22U ) /* function pointer is NULL */
#endif

/* ----------Pwm Service Identification-------------------------------------- */
#define PWM_SID_INIT                    ( 0U )
#define PWM_SID_DE_INIT                 ( 1U )
#define PWM_SID_SET_DUTY_CYCLE          ( 2U )
#define PWM_SID_SET_PERIOD_AND_DUTY     ( 3U )
#define PWM_SID_SET_OUTPUT_TO_IDLE      ( 4U )
#define PWM_SID_GET_OUTPUT_STATE        ( 5U )
#define PWM_SID_DISABLE_NOTIFICATION    ( 6U )
#define PWM_SID_ENABLE_NOTIFICATION     ( 7U )
#define PWM_SID_GET_VERSION_INFO        ( 8U )
#define PWM_SID_CONFIG_REG_READ_BACK	( 9U )

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

/* global status of the pwm driver */
#define PWM_STATUS_UNINIT             ((uint8)(0U))
#define PWM_STATUS_INIT               ((uint8)(1U))

/* 16bit value representing 100% of a period value */
/* SourceId : PWM_SourceId_010 */
/* DesignId : PWM_DesignId_09  */
/* Requirements : AR_PWM_SR21 */
#define PWM_DUTY_100_PERCENT            (0x8000U)

/* The maximum value for Period in the HET */
#define PWM_PERIOD_MAX               (0x1ffffffU)
#define PWM_CHANNEL_MAX                     (32U)

/* The Values for Pwm_NotifyActivationState_au8[] */
#define PWM_NO_EDGES                         (0U)
#define PWM_EDGE                             (1U)

#define PWM_PWCNT_OFFSET                    (0U)
#define PWM_CNT_OFFSET                      (16U)
#define PWM_DJZ_OFFSET                      (32U)
#define PWM_MOV_DUTYCYCLE_OFFSET            (48U)
#define PWM_MOV_PERIOD_OFFSET               (64U)

#define PWM_CHANNEL_OFFSET                  (16U)
#define PWM_DUTYCYCLE_OFFSET                 (7U)
#define PWM_IRQ_CHANNEL_OFFSET              (17U)

#define PWM_HET_IRQ_MASK            (0xffff0000U)
#define PWM_HET_IRQ_OFFSET                  (16U)
#define PWM_HET_RAM_SIZE                    (80U)


#define PWM_HET_IRQ_CLEAR           (HetReg->HetIrqClear_u32)
#define PWM_HET_IRQ_SET             (HetReg->HetIrqSet_u32)


#define PWM_HET1_RAM_ADDR             ((P2VAR(Pwm_HetMemSectionType, PWM_VAR, HET_RAM)) 0xFF460300U)
#define PWM_HET1_REG_ADDR             ((P2VAR(Pwm_HetRegisterType, PWM_VAR, MSR_REGSPACE)) 0xFFF7B800U)

#define PWM_HET1RAM                 (PWM_HET1_RAM_ADDR->HetInstructions_at)

#define PWM_HET1_DATA_OUT            (PWM_HET1_REG_ADDR->HetDataOut_u32)
#define PWM_HET1_DATA_SET            (PWM_HET1_REG_ADDR->HetDataSet_u32)
#define PWM_HET1_DATA_CLEAR          (PWM_HET1_REG_ADDR->HetDataClear_u32)
#define PWM_HET1_IRQ_SET             (PWM_HET1_REG_ADDR->HetIrqSet_u32)
#define PWM_HET1_IRQ_CLEAR           (PWM_HET1_REG_ADDR->HetIrqClear_u32)
#define PWM_HET1_OFF2                (PWM_HET1_REG_ADDR->HetOff2_u32)

#define PWM_HET2_RAM_ADDR            ((P2VAR(Pwm_HetMemSectionType, PWM_VAR, HET_RAM)) 0xFF440300U)
#define PWM_HET2_REG_ADDR            ((P2VAR(Pwm_HetRegisterType, PWM_VAR, MSR_REGSPACE)) 0xFFF7B900U)

#define PWM_HET2RAM                  (PWM_HET2_RAM_ADDR->HetInstructions_at)

#define PWM_HET2_DATA_OUT            (PWM_HET2_REG_ADDR->HetDataOut_u32)
#define PWM_HET2_DATA_SET            (PWM_HET2_REG_ADDR->HetDataSet_u32)
#define PWM_HET2_DATA_CLEAR          (PWM_HET2_REG_ADDR->HetDataClear_u32)
#define PWM_HET2_IRQ_SET             (PWM_HET2_REG_ADDR->HetIrqSet_u32)
#define PWM_HET2_IRQ_CLEAR           (PWM_HET2_REG_ADDR->HetIrqClear_u32)
#define PWM_HET2_OFF2                (PWM_HET2_REG_ADDR->HetOff2_u32)

/**********************************************************************************************************************
     *  LOCAL DATA TYPES AND STRUCTURES
**********************************************************************************************************************/
/*  Mapping of port registers as data struct  */
typedef struct
{
   uint32 Dummy1_u32[4U];
   uint32 HetOff2_u32;
   uint32 HetIrqSet_u32;
   uint32 HetIrqClear_u32;
   uint32 Dummy2_u32[14U];
   uint32 HetDataOut_u32;
   uint32 HetDataSet_u32;
   uint32 HetDataClear_u32;
} Pwm_HetRegisterType;

typedef volatile P2VAR(Pwm_HetRegisterType, PWM_VAR, MSR_REGSPACE) Pwm_HetRegisterPtrType;

typedef struct
{
   Het_Pwm_InstructionType HetInstructions_at[PWM_HET_RAM_SIZE];
}Pwm_HetMemSectionType;

typedef unsigned long long int Pwm_Uint64;

#if(STD_ON == PWM_SAFETI_API)
/* PWM Config registers */
typedef struct PwmConfigRegTag
{
	uint32              Pwm_ReadBack_GCR;                 /* NHET Global Control Register      */
	uint32              Pwm_ReadBack_PFR;                 /* NHET Pre-scale Factor Register    */
	uint32              Pwm_ReadBack_PRY;                 /* NHET Interrupt Priority Register  */
	uint32              Pwm_ReadBack_DIR;                 /* NHET Direction Register           */
	uint32              Pwm_ReadBack_PDR;                 /* NHET Open Drain Register          */
	uint32              Pwm_ReadBack_PDIS;           	  /* NHET Pull Disable Register        */
	uint32              Pwm_ReadBack_PSL;                 /* NHET Pull Select Register         */
} PwmConfigReg;
#endif
/**********************************************************************************************************************
 *  GLOBAL FUNCTION PROTOTYPES
 *********************************************************************************************************************/

#if (STD_ON == PWM_VERSION_INFO_API)
   FUNC(void, PWM_CODE) Pwm_GetVersionInfo( P2VAR(Std_VersionInfoType, AUTOMATIC, PWM_APPL_DATA) versioninfo );
#endif

/* Initializes PWM unit and sets default period and duty cycle values from
 * parameter ConfigPtr.
 */
FUNC(void, PWM_CODE) Pwm_Init( P2CONST(Pwm_ConfigType, AUTOMATIC, PWM_PBCFG) ConfigPtr );

#if ( STD_ON == PWM_DE_INIT_API )
   /* De-initializes PWM unit */
   FUNC(void, PWM_CODE) Pwm_DeInit( void );
#endif

#if (STD_ON == PWM_SET_DUTY_CYCLE_API )
   /* Sets parameter DutyCycle as new duty cycle for channel ChannelNumber.
    * DutyCycle is a percentage of the period with 0x8000 being equivalent
    * to 100%.                                                                */
   FUNC(void, PWM_CODE) Pwm_SetDutyCycle ( Pwm_ChannelType ChannelNumber, uint16 DutyCycle );
#endif

#if (STD_ON == PWM_SET_PERIOD_AND_DUTY_API )
   /* Sets DutyCycle and Period of ChannelNumber. DutyCycle is a percentage of
    * the period with 0x8000 being equivalent to 100%.                         */
   FUNC(void, PWM_CODE) Pwm_SetPeriodAndDuty (Pwm_ChannelType ChannelNumber, Pwm_PeriodType  Period, uint16 DutyCycle);
#endif

#if (STD_ON == PWM_SET_OUTPUT_TO_IDLE_API )
   /* Sets output state immediately to idle level. */
   FUNC(void, PWM_CODE) Pwm_SetOutputToIdle( Pwm_ChannelType ChannelNumber );
#endif

#if (STD_ON == PWM_GET_OUTPUT_STATE_API )
   /* Returns the output state of ChannelNumber, if supported. */
   FUNC(Pwm_OutputStateType, PWM_CODE) Pwm_GetOutputState ( Pwm_ChannelType ChannelNumber );
#endif

#if (STD_ON == PWM_NOTIFICATION_SUPPORTED)
   /* Disables notification for channel ChannelNumber.*/
   FUNC(void, PWM_CODE) Pwm_DisableNotification ( Pwm_ChannelType ChannelNumber );
#endif

#if (STD_ON == PWM_NOTIFICATION_SUPPORTED)
   /* Enables notification for channel ChannelNumber.*/
   FUNC(void, PWM_CODE) Pwm_EnableNotification (Pwm_ChannelType ChannelNumber, Pwm_EdgeNotificationType Notification);
#endif

#if(STD_ON == PWM_SAFETI_API)
   /*Readback of configuration registers*/
   FUNC(Std_ReturnType, PWM_CODE) Pwm_ConfigRegReadBack(Pwm_HetType Het, PwmConfigReg* PwmConfigReg_pt);
#endif

/* ----------------------------------------------------------------------------------------------------------------- */
#endif /*ifndef PWM_H */

/**********************************************************************************************************************
 *  END OF FILE: Pwm.h
 *********************************************************************************************************************/
