/**
*   @copyright       © 2025 Microchip Technology Inc.
    Microchip Technology Inc. and its subsidiaries. You may use this
    software and any derivatives exclusively with Microchip products.

    THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
    EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
    WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
    PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
    WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.

    IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
    INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
    WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
    BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
    FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
    ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
    THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.

    MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
    TERMS. 
*
*************************************************************************/

#include "test.h"
#include "diag_clock.h"
#include <stddef.h>
#include <stdint.h>
#include <xc.h>

static uint32_t testResult;
static uint32_t status;
    
#if DEMO_CLOCK_AVAILABLE

void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK1FInterrupt(void)
{
    DIAG_CLOCK_SetClockMonFaultStatus(DIAG_CLOCKMON_FAULT_DETECTED);
    CM1CONbits.ON = 0;
    _C1FAILIF = 0;
}

void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK1WInterrupt(void)
{
    DIAG_CLOCK_SetClockMonFaultStatus(DIAG_CLOCKMON_FAULT_DETECTED);
    CM1CONbits.ON = 0;
    _C1WARNIF = 0;
}

void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK1MInterrupt(void)
{
    _C1MONIF = 0;
}

void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK1RInterrupt(void)
{
    _C1RDYIF = 0;
}

void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK2FInterrupt(void)
{
    DIAG_CLOCK_SetClockMonFaultStatus(DIAG_CLOCKMON_FAULT_DETECTED);
    CM2CONbits.ON = 0;
    _C2FAILIF = 0;
}
void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK2WInterrupt(void)
{
        DIAG_CLOCK_SetClockMonFaultStatus(DIAG_CLOCKMON_FAULT_DETECTED);
    CM2CONbits.ON = 0;
    _C2WARNIF = 0;
} 

void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK2MInterrupt(void)
{
   _C2MONIF = 0;
}

void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK2RInterrupt(void)
{
   _C2RDYIF = 0;
}

void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK3FInterrupt(void)
{
   _C3FAILIF = 0;
}


void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK3WInterrupt(void)
{
   _C3WARNIF = 0;
}

void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK3MInterrupt(void)
{
   _C3MONIF = 0;
    
}

void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK3RInterrupt(void)
{
   _C3RDYIF = 0;
}

void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK4FInterrupt(void)
{
   _C4FAILIF = 0;
}
void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK4WInterrupt(void)
{
   _C4WARNIF = 0;
}

void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK4MInterrupt(void)
{
  _C4MONIF = 0;
}

void __attribute__ ( ( interrupt, no_auto_psv ) ) _CLK4RInterrupt(void)
{
   _C4RDYIF = 0;
}

void Test_ClockMonInit_LowWarningThreshold(void)
{
    IEC0bits.C2WARNIE = 1;
    CM2CONbits.ON = 0;
    CM2WINPR = 0x8000;
    CM2HFAIL = 0x4500;
    CM2LFAIL = 0x3500;
    CM2HWARN = 0x4100;
    CM2LWARN = 0x3600;
}

void Test_ClockMonInit_HighWarningThreshold(void)
{
    IEC0bits.C2WARNIE = 1;
    CM2CONbits.ON = 0;
    CM2WINPR = 0x8000;
    CM2HFAIL = 0x4500;
    CM2LFAIL = 0x3500;
    CM2HWARN = 0x4100;
    CM2LWARN = 0x3600;
}

void Test_ClockMonInit_LowFailThreshold(void)
{
    IEC0bits.C2FAILIE = 1;
    CM2CONbits.ON = 0;
    CM2WINPR = 0x8000;
    CM2HFAIL = 0x4500;
    CM2LFAIL = 0x3500;
    CM2HWARN = 0x4100;
    CM2LWARN = 0x3600;
}

void Test_ClockMonInit_HighFailThreshold(void)
{
    IEC0bits.C2FAILIE = 1;
    CM2CONbits.ON = 0;
    CM2WINPR = 0x8000;
    CM2HFAIL = 0x4500;
    CM2LFAIL = 0x3500;
    CM2HWARN = 0x4100;
    CM2LWARN = 0x3600;
}

uint32_t DEMO_CLOCK_TestTask()
{
    status = DEMO_TEST_PASS;
        
    Test_ClockMonInit_LowFailThreshold();
    testResult = DIAG_CLOCK_FscmTest(DIAG_CLOCK_MON_INSTANCE_2,DIAG_CLOCK_BFRC,DIAG_CLOCK_FRC,DIAG_CLOCK_CATASTROPHIC_FLTINJ);               
    if(testResult == DIAG_FAIL)
    {
        status = DEMO_TEST_FAIL;
    }
    
    Test_ClockMonInit_LowWarningThreshold();
    testResult = DIAG_CLOCK_FscmTest(DIAG_CLOCK_MON_INSTANCE_2,DIAG_CLOCK_BFRC,DIAG_CLOCK_FRC,DIAG_CLOCK_LOW_FREQ_DRIFT_FLTINJ);               
    if(testResult == DIAG_FAIL)
    {
        status = DEMO_TEST_FAIL;
    }
        
    Test_ClockMonInit_HighWarningThreshold();
    testResult = DIAG_CLOCK_FscmTest(DIAG_CLOCK_MON_INSTANCE_2,DIAG_CLOCK_BFRC,DIAG_CLOCK_FRC,DIAG_CLOCK_HIGH_FREQ_DRIFT_FLTINJ);               
    if(testResult == DIAG_FAIL)
    {
        status = DEMO_TEST_FAIL;
    }
            
    Test_ClockMonInit_HighFailThreshold();
    testResult = DIAG_CLOCK_FscmTest(DIAG_CLOCK_MON_INSTANCE_2,DIAG_CLOCK_BFRC,DIAG_CLOCK_FRC,DIAG_CLOCK_CATASTROPHIC_FLTINJ);               
    if(testResult == DIAG_FAIL)
    {
        status = DEMO_TEST_FAIL;
    }
    
    return status;
}
#endif
