/**
*   @copyright       © 2025 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. 
*
*************************************************************************/

#include "test.h"
#include "diag_gpio.h"
#include <stddef.h>
#include <stdint.h>
#include <xc.h>

static uint32_t testResult;
static uint32_t status;

#if DEMO_GPIO_AVAILABLE

void trig_gpioActivityCheck()
{
    _RD6 = ~_RD6;
}

void test_ppsOutputTest()
{
    U1CONbits.TXPOL = 1;
}

void __attribute__((interrupt, no_auto_psv)) _IOIM1Interrupt(void)
{
_IOM1IF = 0; // clear interrupt flag

}
void __attribute__((interrupt, no_auto_psv)) _IOIM2Interrupt(void)
{
_IOM2IF = 0; // clear interrupt flag

}
void __attribute__((interrupt, no_auto_psv)) _IOIM3Interrupt(void)
{
_IOM3IF = 0; // clear interrupt flag

}
void __attribute__((interrupt, no_auto_psv)) _IOIM4Interrupt(void)
{
_IOM4IF = 0; // clear interrupt flag

}
void __attribute__((interrupt, no_auto_psv)) _IOIM5Interrupt(void)
{
_IOM5IF = 0; // clear interrupt flag

}
void __attribute__((interrupt, no_auto_psv)) _IOIM6Interrupt(void)
{
_IOM6IF = 0; // clear interrupt flag

}
void __attribute__((interrupt, no_auto_psv)) _IOIM7Interrupt(void)
{
_IOM7IF = 0; // clear interrupt flag

}
void __attribute__((interrupt, no_auto_psv)) _IOIM8Interrupt(void)
{
_IOM8IF = 0; // clear interrupt flag

}
void __attribute__((interrupt, no_auto_psv)) _IOIM9Interrupt(void)
{
_IOM9IF = 0; // clear interrupt flag

}
void __attribute__((interrupt, no_auto_psv)) _IOIM10Interrupt(void)
{
_IOM10IF = 0; // clear interrupt flag

}
void __attribute__((interrupt, no_auto_psv)) _IOIM11Interrupt(void)
{
_IOM11IF = 0; // clear interrupt flag

}
void __attribute__((interrupt, no_auto_psv)) _IOIM12Interrupt(void)
{
_IOM12IF = 0; // clear interrupt flag

}
void __attribute__((interrupt, no_auto_psv)) _IOIM13Interrupt(void)
{
_IOM13IF = 0; // clear interrupt flag

}
void __attribute__((interrupt, no_auto_psv)) _IOIM14Interrupt(void)
{
_IOM14IF = 0; // clear interrupt flag

}
void __attribute__((interrupt, no_auto_psv)) _IOIM15Interrupt(void)
{
_IOM15IF = 0; // clear interrupt flag

}
void __attribute__((interrupt, no_auto_psv)) _IOIM16Interrupt(void)
{
_IOM16IF = 0; // clear interrupt flag

}

uint32_t DEMO_GPIO_TestTask()
{
    status = DEMO_TEST_PASS;

    _TRISD6 = 0;
    _TRISF0 = 1;
    _ANSELF0 = 0;
    _CNEN0F0 = 1;
    _CNEN1F0 = 1;
    _CNFF0 = 0;
    CNCONFbits.CNSTYLE = 1;    //Config for PORTF
    CNCONFbits.ON = 1; 
    testResult = DIAG_GPIO_ActivityCheck((uint32_t*)PORTF_PTR, 0, 
                    trig_gpioActivityCheck, 5);
    if(testResult == DIAG_FAIL)
    {
        status = DEMO_TEST_FAIL;
    }
    
    _TRISC6 = 1;
    _TRISG0 = 0;
    _CNEN0C6 = 1;
    _CNEN1C6 = 1;
    _CNFC6 = 0;
    CNCONCbits.CNSTYLE = 1;    //Config for PORTC
    CNCONCbits.ON = 1; 
    testResult = DIAG_GPIO_InterruptGenTest((uint32_t*)PORTC_PTR, 6, 
                    (uint32_t*)PORTG_PTR, 0);
    if(testResult == DIAG_FAIL)
    {
        status = DEMO_TEST_FAIL;
    }
        
    _TRISC7 = 1;
    _TRISB11 = 1;
    _ANSELB11 = 0;
    _RC7 = 1;
    testResult = DIAG_GPIO_InputPractice((uint32_t*)PORTC_PTR, 7, 
                    (uint32_t*)PORTB_PTR, 11);
    if(testResult == DIAG_FAIL)
    {
        status = DEMO_TEST_FAIL;
    }
            
    _TRISF1 = 0;
    _TRISD5 = 1;
    _TRISC8 = 0;//led0
    testResult = DIAG_GPIO_OutputTest((uint32_t*)PORTF_PTR, 1, 
                    (uint32_t*)PORTD_PTR, 5);
    if(testResult == DIAG_FAIL)
    {
        status = DEMO_TEST_FAIL;
    }
                
    //Init uart 1 and invert tx line
    U1CON = 0x8000000UL;
    // TXCIF ; RXFOIF ; RXBKIF ; CERIF ; ABDOVIF ; TXCIE ; RXFOIE ; RXBKIE ; FERIE ; CERIE ; ABDOVIE ; PERIE ; TXMTIE ; STPMD ; TXWRE ; RXWM ; TXWM ; 
    U1STAT = 0x2E0080UL;
    // BaudRate 9599.69; Frequency 100000000 Hz; BRG 10417; 
    U1BRG = 0x28B1UL;
    _TRISG5 = 0;
    _LATG5 = 1;//start high

    RPCONbits.IOLOCK = 0; // unlock PPS
    RPOR25bits.RP102R = 0x0013UL;  //RG5->UART1:U1TX;
    RPCONbits.IOLOCK = 1; // lock PPS

    U1CONbits.ON = 1;   // enabling UART ON bit
    U1CONbits.TXEN = 1;
    U1CONbits.RXEN = 1;
    _TRISB12 = 1;
    _ANSELB12 = 0;
    testResult = DIAG_GPIO_PpsOutputConnectionTest((uint32_t*)PORTG_PTR, 5,
                                (uint32_t*)PORTB_PTR, 12, test_ppsOutputTest, 5);
    if(testResult == DIAG_FAIL)
    {
        status = DEMO_TEST_FAIL;
    }
    
    testResult = DIAG_GPIO_IntegrityMonitorTest();
    if(testResult == DIAG_FAIL)
    {
        status = DEMO_TEST_FAIL;
    }
    
    return status;
}
#endif