/*
*   Copyright 2020 NXP
*
*   NXP Confidential. This software is owned or controlled by NXP and may only be used strictly
*   in accordance with the applicable license terms. By expressly accepting
*   such terms or by downloading, installing, activating and/or otherwise using
*   the software, you are agreeing that you have read, and that you agree to
*   comply with and are bound by, such license terms. If you do not agree to
*   be bound by the applicable license terms, then you may not retain,
*   install, activate or otherwise use the software.
*
*   This file contains sample code only. It is not part of the production code deliverables.
*/

#ifdef __cplusplus
extern "C" {
#endif


/*==================================================================================================
*                                        INCLUDE FILES
* 1) system and project includes
* 2) needed interfaces from external units
* 3) internal and external interfaces from this unit
==================================================================================================*/

#include "Clock_Ip.h"
#include "Emios_Pwm_Ip.h"
#include "Emios_Mcl_Ip.h"
#include "Emios_Icu_Ip.h"
#include "Siul2_Port_Ip.h"
#include "IntCtrl_Ip.h"
#include "Emios_Icu_Ip_Irq.h"

/*==================================================================================================
*                          LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
==================================================================================================*/


/*==================================================================================================
*                                       LOCAL MACROS
==================================================================================================*/
#define DELAY_TIMER       (uint32)1000U

#define INSTANCE_0        (uint8)0U
#define INSTANCE_1        (uint8)1U
#define INSTANCE_2        (uint8)2U

#define CHANNEL_1  		  (uint8)1U
#define CHANNEL_2		  (uint8)2U
#define CHANNEL_3		  (uint8)3U
#define CHANNEL_4		  (uint8)4U
#define CHANNEL_5		  (uint8)5U
#define CHANNEL_6		  (uint8)6U
#define CHANNEL_8		  (uint8)8U
/*==================================================================================================
*                                      LOCAL CONSTANTS
==================================================================================================*/


/*==================================================================================================
*                                      LOCAL VARIABLES
==================================================================================================*/


/*==================================================================================================
*                                      GLOBAL CONSTANTS
==================================================================================================*/
//#define SAIC_MODE

/*==================================================================================================
*                                      GLOBAL VARIABLES
==================================================================================================*/
#ifdef SAIC_MODE
static eMios_Icu_Ip_DutyCycleType Saic_duty_cycle;
static uint32 Saic_duty_cycle_array[50];
#else
static eMios_Icu_Ip_DutyCycleType Ipwm_duty_cycle;
static uint32 Ipwm_duty_cycle_array[50];
#endif
/*==================================================================================================
*                                   LOCAL FUNCTION PROTOTYPES
==================================================================================================*/

void TestDelay(uint32 delay);
/*==================================================================================================
*                                       LOCAL FUNCTIONS
==================================================================================================*/
void TestDelay(uint32 delay)
{
    static volatile uint32 DelayTimer = 0;
    while(DelayTimer<delay)
    {
        DelayTimer++;
    }
    DelayTimer=0;
}

/*==================================================================================================
*                                       GLOBAL FUNCTIONS
==================================================================================================*/
/**
* @brief        Main function of the example
* @details      Initialize the used drivers and uses the Pwm driver to control the led brightness
*               and blink a LED
*/
int main (void)
{
	uint32 index = 0;

    /* Initialize clock */
    Clock_Ip_Init(&Clock_Ip_aClockConfig[0]);

    /* Initialize all pins using the Port driver */
    Siul2_Port_Ip_Init(NUM_OF_CONFIGURED_PINS_PortContainer_0_BOARD_InitPeripherals, g_pin_mux_InitConfigArr_PortContainer_0_BOARD_InitPeripherals);

#ifdef SAIC_MODE
    /* Install Emios ISR */
    IntCtrl_Ip_Init(&IntCtrlConfig_0);
#endif

    /* Initialize Emios_Mcl driver */
    Emios_Mcl_Ip_Init(INSTANCE_0, &Emios_Mcl_Ip_0_Config);
    Emios_Mcl_Ip_Init(INSTANCE_1, &Emios_Mcl_Ip_1_Config);
    Emios_Mcl_Ip_Init(INSTANCE_2, &Emios_Mcl_Ip_2_Config);


    /* Initialize Emios_Pwm_Ip driver */
    /* Channel configurations for Emios instance 0 */
    Emios_Pwm_Ip_InitChannel(EMIOS_PWM_IP_I0_CH1_CFG, &Emios_Pwm_Ip_I0_Ch1);
    Emios_Pwm_Ip_InitChannel(EMIOS_PWM_IP_I0_CH2_CFG, &Emios_Pwm_Ip_I0_Ch2);
    Emios_Pwm_Ip_InitChannel(EMIOS_PWM_IP_I0_CH3_CFG, &Emios_Pwm_Ip_I0_Ch3);
    Emios_Pwm_Ip_InitChannel(EMIOS_PWM_IP_I0_CH4_CFG, &Emios_Pwm_Ip_I0_Ch4);
    /* Channel configurations for Emios instance 2 */
    Emios_Pwm_Ip_InitChannel(EMIOS_PWM_IP_I2_CH8_CFG, &Emios_Pwm_Ip_I2_Ch8);

    /* Setup new Dead Time to the pin (lead edge)*/
    Emios_Pwm_Ip_SetDeadTime(INSTANCE_0, CHANNEL_1, 1000);
    Emios_Pwm_Ip_SetDeadTime(INSTANCE_0, CHANNEL_2, 0);
    /* Setup new Dead Time to the pin (trail edge)*/
    Emios_Pwm_Ip_SetDeadTime(INSTANCE_0, CHANNEL_1, 0);
    Emios_Pwm_Ip_SetDeadTime(INSTANCE_0, CHANNEL_2, 1000);

    /* De-Initialize Emios_Pwm_Ip driver */
    Emios_Pwm_Ip_DeInitChannel(INSTANCE_0, CHANNEL_1);
    Emios_Pwm_Ip_DeInitChannel(INSTANCE_0, CHANNEL_2);

    /* Setup new Phase Shift to the pin*/
    Emios_Pwm_Ip_SetPhaseShift(INSTANCE_0, CHANNEL_3, 1000);
    Emios_Pwm_Ip_SetPhaseShift(INSTANCE_0, CHANNEL_4, 0);
    /* Setup new Phase Shift to the pin*/
    Emios_Pwm_Ip_SetPhaseShift(INSTANCE_0, CHANNEL_3, 0);
    Emios_Pwm_Ip_SetPhaseShift(INSTANCE_0, CHANNEL_4, 1000);

    /* De-Initialize Emios_Pwm_Ip driver */
    Emios_Pwm_Ip_DeInitChannel(INSTANCE_0, CHANNEL_3);
    Emios_Pwm_Ip_DeInitChannel(INSTANCE_0, CHANNEL_4);

    /* Initialize Emios_Icu_Ip driver */
    Emios_Icu_Ip_Init(INSTANCE_1, &eMios_Icu_Ip_1_Config_PB);
    
#ifdef SAIC_MODE
    Emios_Pwm_Ip_SetDutyCycle(INSTANCE_2, CHANNEL_8, 24);
    Emios_Icu_Ip_StartSignalMeasurement(INSTANCE_1, CHANNEL_5);

    for(index = 0;50 > index;index++)
    {
    	Emios_Icu_Ip_GetDutyCycleValues(INSTANCE_1, CHANNEL_5, &Saic_duty_cycle);
    	Saic_duty_cycle_array[index] = (Saic_duty_cycle.ActiveTime * 100) / Saic_duty_cycle.PeriodTime;
        TestDelay(DELAY_TIMER);
    }
#else
    Emios_Pwm_Ip_SetDutyCycle(INSTANCE_2, CHANNEL_8, 36);
    Emios_Icu_Ip_StartSignalMeasurement(INSTANCE_1, CHANNEL_6);

    for(index = 0;50 > index;index++)
    {
    	/* Get ActiveTime and PeriodTime */
    	Emios_Icu_Ip_GetPulseWidth(INSTANCE_1, CHANNEL_6);
    	Emios_Icu_Ip_GetDutyCycleValues(INSTANCE_1, CHANNEL_6, &Ipwm_duty_cycle);
    	Ipwm_duty_cycle_array[index] = (Ipwm_duty_cycle.ActiveTime * 100) / Ipwm_duty_cycle.PeriodTime;
    }
#endif

    /* De-Initialize Emios_Pwm_Ip driver */
    Emios_Pwm_Ip_DeInitChannel(INSTANCE_2, CHANNEL_8);

    return 0U;
}

#ifdef __cplusplus
}
#endif

/** @} */
