/**
*   @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_hardtrap_test.c
 *  @version    1.0
 *  @brief      This file has the implementation for the interrupt servicing test
 *              diagnostics
 *  @see        diag_interrupt.h
 *************************************************************************/
#include "diag_interrupt.h"
#include "diag_interrupt_shared.h"
#include "diag.h"

static DIAG_INTERRUPT_HARDTRAP_RUN_STATUS diagInduceHardTrap;

uint32_t DIAG_INTERRUPT_HardTrapTest(void)
{
    uint32_t retVal;
    uint16_t timeOut;
    volatile uint32_t *intrControlReg5Ptr;
/*LDRA_EXCLUDE 554 S <justification start> pointer to an SFR with that attribute,violation is ignored <justification end> */	
    intrControlReg5Ptr = (volatile uint32_t *)INTERRUPT_CONTROL_REG_ADDR;
    diagInduceHardTrap = INTR_HARDTRAP_DIAG_RUN_TRUE;
    /*Set the Software trap bit */
    *intrControlReg5Ptr |= INTERRUPT_CONTROL_REG_SOFT_MASK;
    /*LDRA_EXCLUDE 105 D <justification start> timeout variable is a part of the no analysis annotation, violation ignored <justification end> */
    timeOut = (uint16_t)INTCON5_SOFT_FLAG_RESET_TIMEOUT;
    /*Wait for the SOFT bit to be zero, or the timeout occurs*/
    /*Check if the loop exited because of the Software generated hard trap interrupt bit being cleared in ISR*/
      
    while(((*intrControlReg5Ptr & INTERRUPT_CONTROL_REG_SOFT_MASK) == INTERRUPT_CONTROL_REG_SOFT_MASK) && (timeOut != 0U))
    {
        timeOut -= 1U;
    }
    /*The FAIL condition cannot be simulated in hardware, hence the LDRA annotation*/
/*LDRA_NOANALYSIS*/   
    if((*intrControlReg5Ptr & (~INTERRUPT_CONTROL_REG_SOFT_MASK)) == 0U)
    {
        DIAG_INTERRUPT_SetRetValue(DIAG_PASS);
    }
    else
    {
        DIAG_INTERRUPT_SetRetValue(DIAG_FAIL);
    }
/*LDRA_ANALYSIS*/  
    retVal = DIAG_INTERRUPT_GetRetValue();
    return (retVal);
}

DIAG_INTERRUPT_HARDTRAP_RUN_STATUS DIAG_INTERRUPT_isTrapInducedByDiag(void)
{
    return diagInduceHardTrap;
}

void DIAG_INTERRUPT_resetDiagInducedTrapStatus(void)
{
    diagInduceHardTrap = INTR_HARDTRAP_DIAG_RUN_FALSE;
}
