/**
*   @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_outputtest.c
*  @version     1.0
*  @brief       This file has the implementation for the GPIO  
                Output Test diagnostic. @see diag_gpio.h
*
*************************************************************************/
/*Function headers*/
#include <stdint.h>

#include "diag_gpio.h"
#include "diag_gpio_shared.h"

uint32_t DIAG_GPIO_OutputTest(uint32_t* outputPort, uint16_t outputPinNum,
                                uint32_t* inputPort, uint16_t inputPinNum)
{
    volatile uint32_t retVal;
    uint32_t outputPortVal, outputLatVal;
    uint32_t outputMask;
    uint32_t latPortOutput; 
    gpioPortRegAddr_t outputPortReg;
    /* Check if the outputPort and inputPort addresses provided are valid */
    if((outputPort == NULL) || (inputPort == NULL) || 
            (outputPinNum > DIAG_GPIO_MAX_PIN_NUMBER) || (inputPinNum > DIAG_GPIO_MAX_PIN_NUMBER))
    {
        DIAG_GPIO_SetRetValue(DIAG_INVALID_PARAM);
    }
    else
    {
        /*LDRA_EXCLUDE 606 S <justification start> This is not a function pointer. Violation ignored <justification end>*/
		outputPortReg = (gpioPortRegAddr_t)outputPort;
        outputPortVal = (*outputPortReg)[DIAG_GPIO_PORT_OFFSET];
        outputMask = 1U << outputPinNum;
        outputPortVal = (outputPortVal & outputMask) >> outputPinNum;
        outputLatVal = (*outputPortReg)[DIAG_GPIO_LAT_OFFSET];
        
        outputLatVal = (outputLatVal & outputMask) >> outputPinNum;
        latPortOutput = outputLatVal ^ outputPortVal;
        if(latPortOutput == 0U)
        {
            DIAG_GPIO_CompareOutputInputPorts((uint32_t *)outputPort, outputPinNum,
                    (uint32_t *)inputPort, inputPinNum);           
        }
        else
        {
            DIAG_GPIO_SetRetValue(DIAG_FAIL);
        }
    } 
    
    retVal = DIAG_GPIO_GetRetValue();
    return retVal;
}
