/**
*   @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_crc_functionalTest.c
 *  @version         1.0
 *  @brief           This file has the implementation for the CRC
					 functional test. 
 *	@see 			 diag_crc.h
 *
 *************************************************************************/
 
#include "diag_crc_shared.h"
#include "diag_crc.h"

/* Fixed data used for CRC Functional test function */
static uint8_t	dataBytes[DIAG_CRC_DATA_ARRAY_SIZE] = { 0x12U, 0x34U, 0x56U, 
                                            0x78U, 0x9AU, 0xBCU, 0xDEU, 0xF0U };

/* Status flag variable to indicate any fails */
static uint32_t DIAG_CRC_FunctionalTest_StatusFlag = 0U;	

/*
 * Structure with polynomials and corresponding expected results for each CRC
 * type(1-32)
 */
static const DIAG_CRC_POLY_EXPECTED_VALUES crcPolyExpectedValues[DIAG_CRC_STRUCT_SIZE] =
{
	/* polynomialHigher nibble,polynomialLower nibble,expectedValue1,expectedValue2 */
	{ 0x00000000U, 0x0U, 0x0U },                     /* polyWidth = 1 */
	{ 0x00000001U, 0x0U, 0x1U },                     /* polyWidth = 2 */
	{ 0x00000002U, 0x2U, 0x5U },                     /* polyWidth = 3 */
	{ 0x00000005U, 0x6U, 0xFU },                     /* polyWidth = 4 */
	{ 0x0000000AU, 0x09U, 0x0CU },                   /* polyWidth = 5 */
	{ 0x00000015U, 0x00U, 0x2CU },                  /* polyWidth = 6 */
	{ 0x0000002AU, 0x02U, 0x23U },                  /* polyWidth = 7 */
	{ 0x00000055U, 0x55U, 0x55U },                  /* polyWidth = 8 */
	{ 0x000000AAU, 0x0F9U, 0x137U },                /* polyWidth = 9 */
	{ 0x00000155U, 0x0A5U, 0x115U },               /* polyWidth = 10 */
	{ 0x000002AAU, 0x16CU, 0x706U },               /* polyWidth = 11 */
	{ 0x00000555U, 0x5CAU, 0x5F0U },               /* polyWidth = 12 */
	{ 0x00000AAAU, 0x146FU, 0x0BABU },             /* polyWidth = 13 */
	{ 0x00001555U, 0x0000U, 0x2F60U },            /* polyWidth = 14 */
	{ 0x00002AAAU, 0x637CU, 0x18A9U },            /* polyWidth = 15 */
	{ 0x00005555U, 0x6F5CU, 0xAAAAU },            /* polyWidth = 16 */
	{ 0x0000AAAAU, 0x67EFU, 0xA96FU },            /* polyWidth = 17 */
	{ 0x00015555U, 0x0C963U, 0x2B17DU },          /* polyWidth = 18 */
	{ 0x0002AAAAU, 0x63564U, 0x256F3U },          /* polyWidth = 19 */
	{ 0x00055555U, 0x081BEU, 0xA63C9U },          /* polyWidth = 20 */
	{ 0x000AAAAAU, 0x1FC7FAU, 0x1FF34AU },        /* polyWidth = 21 */
	{ 0x00155555U, 0x01EF440U, 0x00081BEU },     /* polyWidth = 22 */
	{ 0x002AAAAAU, 0x03ACD3DU, 0x02913F0U },     /* polyWidth = 23 */
	{ 0x00555555U, 0xFAD7D0U, 0xB45EEAU },       /* polyWidth = 24 */
	{ 0x00AAAAAAU, 0x9F9EA1U, 0x5A0AEBU },       /* polyWidth = 25 */
	{ 0x01555555U, 0x646946U, 0x1AF8285U },     /* polyWidth = 26 */
	{ 0x02AAAAAAU, 0x1AACE8EU, 0x7B920D8U },    /* polyWidth = 27 */
	{ 0x05555555U, 0x09B61C4DU, 0x05313C13U },  /* polyWidth = 28 */
	{ 0x0AAAAAAAU, 0x13126E21U, 0x1BB6188AU },  /* polyWidth = 29 */
	{ 0x15555555U, 0x22222222U, 0x1CE34918U }, /* polyWidth = 30 */
	{ 0x2AAAAAAAU, 0x430CE993U, 0x70E2A983U }, /* polyWidth = 31 */
	{ 0x55555555U, 0x8D26D871U, 0x22222222U }, /* polyWidth = 32 */
};

/*
 ===============================================================================
    API Functions uint16_t DIAG_CRC_FunctionalTest (void) API 
    function for performing write/read tests of all CPU
    registers Function to perform CRC functional test
 ===============================================================================
 */
uint32_t DIAG_CRC_FunctionalTest(void)
{
	volatile uint32_t	retValue = DIAG_FAIL;	/* Initial Value */
	uint32_t resultCRC1;
	uint32_t resultCRC2;
	uint32_t poly;
	uint64_t expectedValue1;
	uint64_t expectedValue2;
	uint16_t polyWidth = 1U;    /* start with polynomial width of 1 */
	uint16_t index = 0U;


	DIAG_CRC_FunctionalTest_StatusFlag = 0x00000000U;

	while(polyWidth < (uint16_t) DIAG_CRC_NUM_OF_POLYNOMIALS)
	{
		/* Get polynomial and expected results from structure */
		poly = crcPolyExpectedValues[index].poly;

		expectedValue1 = crcPolyExpectedValues[index].expectedVal1;
		resultCRC1 = DIAG_CRC_Compute(dataBytes, poly, polyWidth);

        /* Compare the results with expected values */
		if(resultCRC1 != expectedValue1)			
		{
			DIAG_CRC_FunctionalTest_StatusFlag |= ((uint32_t) 1 << (uint16_t) (index));
		}

		expectedValue2 = crcPolyExpectedValues[index].expectedVal2;
		resultCRC2 = DIAG_CRC_Compute(dataBytes, ~poly, polyWidth);

        /* Compare the results with expected values */
		if(resultCRC2 != expectedValue2)			
		{
			DIAG_CRC_FunctionalTest_StatusFlag |= ((uint32_t) 1 << (uint16_t) (index));
		}

		polyWidth++;
		index++;
	}

	if(DIAG_CRC_FunctionalTest_StatusFlag == 0U)
	{
		retValue = DIAG_PASS;
	}
    else
    {
		retValue = DIAG_FAIL;
	}

	return retValue;
}

/*
 ===============================================================================
    uint32_t DIAG_CRC_FunctionalTest_GetStatus(void) This function must be 
    called immediately after executing the DIAG_CRC_FunctionalTest() function.
    It is used to obtain status variable associated with the diagnostic function.
    This function directly returns the status flag variable
 ===============================================================================
 */
uint32_t DIAG_CRC_FunctionalTest_GetStatus(void)
{
	/* Indicates which CRC type has failed the most recent CRC functional test */
	return(DIAG_CRC_FunctionalTest_StatusFlag); /* Return the error logged by the diagnostic function */
}
