/*
 * Copyright 2020 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "fsl_debug_console.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "fsl_tempsensor.h"

#include "dvfs.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/
#define DEMO_TEMP_SENSOR              TMPSNS
#define DEMO_TEMP_LOW_HIGH_IRQn       ANATOP_TEMP_LOW_HIGH_IRQn
#define DEMO_TEMP_PANIC_IRQn          ANATOP_TEMP_PANIC_IRQn
#define DEMO_TEMP_LOW_HIGH_IRQHandler ANATOP_TEMP_LOW_HIGH_IRQHandler
#define DEMO_TEMP_PANIC_IRQHandler    ANATOP_TEMP_PANIC_IRQHandler
#define DEMO_PANIC_ALARM_TEMP         125U
#define DEMO_HIGH_ALARM_TEMP          100U
#define DEMO_LOW_ALARM_TEMP           95U
#define DEMO_RGB565_ENABLE            0
#define FIXED_TEMP                    1
#define ENABLE_PANIC_INT              1

/*******************************************************************************
 * Prototypes
 ******************************************************************************/
#if DEMO_RGB565_ENABLE
extern int rgb565_main(void);
#endif

/*******************************************************************************
 * Variables
 ******************************************************************************/


/*******************************************************************************
 * Code
 ******************************************************************************/

void DEMO_TEMP_LOW_HIGH_IRQHandler(void)
{
    /* Disable high temp interrupt to jump out dead loop */
    //TMPSNS_DisableInterrupt(DEMO_TEMP_SENSOR, kTEMPSENSOR_HighTempInterruptStatusEnable);

    dvfs_low_high_irq_handle(DEMO_TEMP_SENSOR);

    SDK_ISR_EXIT_BARRIER;
}

void DEMO_TEMP_PANIC_IRQHandler(void)
{
    PRINTF("Trig ANATOP_TEMP_PANIC_IRQHandler\r\n");
}

/*!
 * @brief Main function
 */
int main(void)
{
    tmpsns_config_t config;

    /* Board pin, clock, debug console init */
    BOARD_ConfigMPU();
    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();

    PRINTF("\r\n****************************************************\r\n\
********************DVFS example.*******************\r\n\
****************************************************\r\n");

    dump_voltage_and_frequency();

    TMPSNS_GetDefaultConfig(&config);
    config.measureMode   = kTEMPSENSOR_ContinuousMode;
    config.frequency     = 0xFFFF;
    config.panicAlarmTemp = DEMO_PANIC_ALARM_TEMP;
    config.highAlarmTemp = DEMO_HIGH_ALARM_TEMP;
    config.lowAlarmTemp = DEMO_LOW_ALARM_TEMP;

    TMPSNS_Init(DEMO_TEMP_SENSOR, &config);
    TMPSNS_StartMeasure(DEMO_TEMP_SENSOR);
    
    EnableIRQ(DEMO_TEMP_LOW_HIGH_IRQn);
    TMPSNS_EnableInterrupt(DEMO_TEMP_SENSOR, kTEMPSENSOR_LowTempInterruptStatusEnable);
#if ENABLE_PANIC_INT
    //SRC->SRMR = 0xc000; // SRMR[TEMPSENSE_RESET_MODE] = 2'b11 - do not reset anything 
    EnableIRQ(DEMO_TEMP_PANIC_IRQn);
    TMPSNS_EnableInterrupt(DEMO_TEMP_SENSOR, kTEMPSENSOR_PanicTempInterruptStatusEnable);
#endif

    GPT_Configure();

#if DEMO_RGB565_ENABLE
    rgb565_main();
#endif
    
    while (1);
    
}
