/**
*   @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_flash_biterror_test.c
 *  @version    1.0
 *  @brief      This file has the implementation for the FLASH ECC Single 
 *		        Double bit error test diagnostics. @see diag_flash.h
 *
 *************************************************************************/

#include <xc.h>
#include <stddef.h>
#include "diag_flash.h"
#include "diag_flash_shared.h"

/*Macros for TEST MODE SCENARIOS*/
#define DIAG_FLASH_NORMAL_MODE              0x91U
#define DIAG_FLASH_ERROR_MODE_TEST          0x92U

extern uint32_t diagFlashModeEnabled;
static uint32_t testModeBitError = DIAG_FLASH_NORMAL_MODE;

static uint32_t DIAG_FLASH_SingleErrorDetectionTest(uint32_t *location)
{
    volatile uint32_t readData;
    uint32_t status;
    
    //Configure to introduce single fault error
    NVMCONbits.DRBV = 0;
    NVMECCSTATbits.ESEL = 1U;
    NVMECCCONbits.FLTINJ = 0U;
    IEC0bits.NVMECCIE = 1U;
	/*LDRA_EXCLUDE 439 S <justification start>The address goes in as a value to the register. Violation ignored<justification end>*/
    NVMECCFADDR = (uint32_t)location;
    NVMECCFPTRbits.FLT1LPTR = 0x5U;
    NVMECCFPTRbits.FLT2LPTR = 0xFFU;
    
    if(testModeBitError == DIAG_FLASH_NORMAL_MODE)
    {
        /*LDRA_EXCLUDE 151 D: <justification start> This variable is updated as per the design, violation ignored <justification end> */
        NVMECCCONbits.FLTINJ = 1U;
    }
    else
    {
        /*LDRA_EXCLUDE 151 D: <justification start> This variable is updated as per the design, violation ignored <justification end> */
        NVMECCCONbits.FLTINJ = 0U;
    }
    
    __builtin_repeat_nop(10U);
    
    /*LDRA_EXCLUDE 105 D: <justification start> This variable is updated as per the design, violation ignored <justification end> */
    readData = *location;
    /*Dummy Reassignment to handle warnings */   
    readData = readData;
    
    __builtin_repeat_nop(10U);
    
    //Assign the status of single bit error
    status = (uint32_t)NVMECCSTATbits.SEC;
    
	/*LDRA_EXCLUDE 151 D: <justification start> This variable is updated as per the design, violation ignored <justification end> */
    NVMECCCONbits.FLTINJ = 0U;
    NVMECCSTATbits.SEC = 0U;
    
    return status;
}

static uint32_t DIAG_FLASH_DoubleErrorDetectionTest(uint32_t *location)
{
    volatile uint32_t readData;
    uint32_t status;
    
    //Configure to introduce double fault error
    NVMCONbits.DRBV = 0;
    NVMECCSTATbits.ESEL = 0U;
    NVMECCCONbits.FLTINJ = 0U;
	/*LDRA_EXCLUDE 439 S <justification start>The address goes in as a value to the register. Violation ignored<justification end>*/
    NVMECCFADDR = (uint32_t)location;
    NVMECCFPTRbits.FLT1LPTR = 6U;
    NVMECCFPTRbits.FLT2LPTR = 9U;
    
    if(testModeBitError == DIAG_FLASH_NORMAL_MODE)
    {
        /*LDRA_EXCLUDE 151 D: <justification start> This variable is updated as per the design, violation ignored <justification end> */
        NVMECCCONbits.FLTINJ = 1U;
    }
    else
    {
        /*LDRA_EXCLUDE 151 D: <justification start> This variable is updated as per the design, violation ignored <justification end> */
        NVMECCCONbits.FLTINJ = 0U;
    }
    
    __builtin_repeat_nop(10U);
    
    /*LDRA_EXCLUDE 105 D: <justification start> This variable is updated as per the design, violation ignored <justification end> */
    readData = *location;
    /*Dummy Reassignment to handle warnings */     
    readData = readData;

    __builtin_repeat_nop(10U);
    
    //Assign the status of double bit error
    status = (uint32_t)NVMECCSTATbits.DED;
    
	/*LDRA_EXCLUDE 151 D: <justification start> This variable is updated as per the design, violation ignored <justification end> */
    NVMECCCONbits.FLTINJ = 0U;
    NVMECCSTATbits.DED = 0U;
    
    return status;
}

uint32_t DIAG_FLASH_SingleDoubleErrorDetectionTest(uint32_t *location)
{
    uint32_t resultSingleError;
    uint32_t resultDoubleError;
    uint32_t status;

    if(location != NULL)
	{
        /*LDRA_EXCLUDE 151 D: <justification start> This variable is updated as per the design, violation ignored <justification end> */
        //Enable the global variable to indicate diagnostic mode is enabled
        diagFlashModeEnabled = 1U;

        /*LDRA_EXCLUDE 91 D: <justification start> This variable is updated as per the design, violation ignored <justification end> */
        resultDoubleError = DIAG_FLASH_DoubleErrorDetectionTest(location);
        resultSingleError = DIAG_FLASH_SingleErrorDetectionTest(location);

        if((resultSingleError == 1U) && (resultDoubleError == 1U))
        {
            status = DIAG_PASS;
        }
        else
        {
            status = DIAG_FAIL;
        }

        /*LDRA_EXCLUDE 151 D: <justification start> This variable is updated as per the design, violation ignored <justification end> */
        //Disable the global variable to indicate diagnostic mode is completed
        diagFlashModeEnabled = 0U;
    }
    else
	{
		status = DIAG_INVALID_PARAM;
	}
        
    return status;
}

void DIAG_FLASH_SetTestModeBitError(uint32_t setTestMode)
{
    testModeBitError = setTestMode;
}
