/**
*   @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_gpio_inputpractice.c
*  @version     1.0
*  @brief       This file has the implementation for the GPIO
                Input practice diagnostic. @see diag_gpio.h
*
*************************************************************************/
/*Function headers*/
#include "diag_gpio.h"
#include "diag_gpio_shared.h"

uint32_t DIAG_GPIO_InputPractice(uint32_t* priInputPort, uint16_t priInputPin,
                                uint32_t* secInputPort, uint16_t secInputPin)
{
    volatile uint32_t retVal;
    uint32_t priPortVal, secPortVal;
    uint32_t priMask, secMask;
    uint32_t priPortState, secPortState, portCompare;
    
    /* Check if the priInputPort and secInputPort addresses provided are valid */
    if((priInputPort == NULL) || (secInputPort == NULL) ||
            (priInputPin > DIAG_GPIO_MAX_PIN_NUMBER) || (secInputPin > DIAG_GPIO_MAX_PIN_NUMBER))
    {       
        DIAG_GPIO_SetRetValue(DIAG_INVALID_PARAM);      
    }
    else
    {    
        /* Get the Port Bit values of the two input pins */
        priPortVal = *priInputPort;
        priMask = 1U << priInputPin;
        priPortState = (priPortVal & priMask) >> priInputPin;

        secPortVal = *secInputPort;
        secMask = 1U << secInputPin;
        secPortState = (secPortVal & secMask) >> secInputPin; 
        
        /* Now compare the PORTx bits of both the pins and report any
         error that is found */
        portCompare = priPortState ^ secPortState;
        if(portCompare == 0U)
        {
            DIAG_GPIO_SetRetValue(DIAG_PASS);
        }
        else
        {
            DIAG_GPIO_SetRetValue(DIAG_FAIL);
        }
    }
    retVal = DIAG_GPIO_GetRetValue();
    return retVal;
}
