/**
*   @copyright       © 2026 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. 
*
*************************************************************************/

/**
 *  @file       diag_interrupt_external_input.c
 *  @version    1.0
 *  @brief      This file has the implementation for the external input tests
 *  @see        diag_interrupt.h
 *************************************************************************/
#include "diag_interrupt.h"
#include "diag_interrupt_shared.h"
#include "diag.h"

/*Volatile is needed as this variable is updated in the ISR*/
static volatile uint32_t extIntrTestCompleted = DIAG_INTERRUPT_EXTR_TEST_IN_PROGRESS;

static DIAG_INTERRUPT_EXT_INTR_RUN_STATUS intrExtInputTestRunningStatus = INTR_EXT_DIAG_RUN_FALSE;



void DIAG_INTERRUPT_ExternalSetIntrTestCompleted(uint32_t extIntrTestStatus)
{
    extIntrTestCompleted = extIntrTestStatus;
}

static uint32_t DIAG_INTERRUPT_ExternalGetIntrTestStatus(void)
{
    return(extIntrTestCompleted);
}

static void DIAG_INTERRUPT_TriggerAndTestExternalInterrupts(uint32_t interruptNum,
                                                     uint32_t interruptPol,
                                                     void (*trigForInterrupt)(void))
{
    uint16_t timeOut;
    volatile uint32_t interruptStatus = DIAG_INTERRUPT_EXTR_TEST_IN_PROGRESS;
    /*This register controls the interrupt polarity*/
/*LDRA_EXCLUDE 554 S <justification start> pointer to an SFR which has that attribute, hence the violation is ignored <justification end> */    
    volatile uint32_t *intrControlReg2Ptr = (volatile uint32_t *)INTERRUPT_CTRL_REG2_ADDR;
    /*Enable the interrupt polarity*/
    if(interruptPol == (uint32_t)INTERRUPT_ON_POSITIVE_EDGE)
    {
        *intrControlReg2Ptr  &= (interruptPol << interruptNum);
    }
    else
    {
        *intrControlReg2Ptr  |= (interruptPol << interruptNum);
    }
    /*Set the timeout*/
    timeOut = EXTERNAL_INPUT_REG_FLAG_TIMEOUT;
    /*Enable the trigger*/
    trigForInterrupt();
    /*Wait for the interrupt flag to be reset or a timeout*/
    
    while((interruptStatus == (uint32_t)DIAG_INTERRUPT_EXTR_TEST_IN_PROGRESS) && (timeOut > 0U))
    {
        timeOut -= 1U;
        interruptStatus = DIAG_INTERRUPT_ExternalGetIntrTestStatus();
    }
    
    /*Check if the loop exited because of the interrupt flag and not timeout*/
    if(interruptStatus == DIAG_INTERRUPT_EXTR_TEST_COMPLETED)
    {
        DIAG_INTERRUPT_SetRetValue(DIAG_PASS);
    }
    else
    {
        DIAG_INTERRUPT_SetRetValue(DIAG_FAIL);
    }
}


uint32_t DIAG_INTERRUPT_ExternalInputTest(DIAG_INTERRUPT_EXTERNAL_INTR_NUM interruptNum, 
                                                                                DIAG_INTERRUPT_POLARITY interruptPol, 
                                                                                void (*trigForInterrupt)())
{
    uint32_t retVal;
    DIAG_INTERRUPT_ExternalSetIntrTestCompleted(DIAG_INTERRUPT_EXTR_TEST_IN_PROGRESS);
/*LDRA_EXCLUDE 151 D <justification start> during API execution it can be interrupted by ISR, hence the violation is ignored <justification end> */    
    intrExtInputTestRunningStatus = INTR_EXT_DIAG_RUN_TRUE;
    if(trigForInterrupt == NULL)
    {
        DIAG_INTERRUPT_SetRetValue(DIAG_INVALID_PARAM);
    }
    else
    {
        DIAG_INTERRUPT_TriggerAndTestExternalInterrupts((uint32_t)interruptNum,(uint32_t)interruptPol,trigForInterrupt);
    }
    retVal = DIAG_INTERRUPT_GetRetValue();
/*LDRA_EXCLUDE 151 D <justification start> during API execution it can be interrupted by ISR, hence the violation is ignored <justification end> */    
    intrExtInputTestRunningStatus = INTR_EXT_DIAG_RUN_FALSE;
    return (retVal);
}

DIAG_INTERRUPT_EXT_INTR_RUN_STATUS DIAG_INTERRUPT_IsExtIntrTestRunning(void)
{
    return (intrExtInputTestRunningStatus);
}
