/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.3
 * @date     30-July-2026
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/

#include<stdio.h>

#if defined(SPC1158)
#include "spc1158.h"
#elif defined(SPD1148)
#include "spd1148.h"
#elif defined(SPD1178)
#include "spd1178.h"
#elif defined(SPD1188)
#include "spd1188.h"
#elif defined(SPD1163)
#include "spd1163.h"
#else
#include "spc1168.h"
#endif

#define             RESULTTOVOLTAGE(x)        (double)(((double)((3657 * x) / 8192)) / 1000)

int32_t             VALUE1 = 0;
int32_t             VALUE2 = 0;
int32_t             VALUE3 = 0;
ErrorStatus         eStatus = ERROR;

/*************************************************************************************************************************
*
* @brief      This test case describes how to detect short status of ADC input PIN, GPIO8 used as the Input PIN Test Point
*             in this demo.
*
*                                  Input PIN                Test Point at
*                                  Test Point                 ADC Input
*                 -----              -----                    -----                    -----------
*                |Input|            |     |        /         |test |        /         |    ADC    |
*                | PIN |------------| PAD |-------��  -------|point|-------��  -------| Controller|
*                 -----              -----        On/Off      -----       On/Off       -----------
*                                     / \          / \          / \         / \
*                                      |            |            |           |
*                                      |             ------------|------------------------------------The two On/Off switch
*                                      |                         |                                    are controlled
*                                       ---------------------------------The two test point will      simultaneously by one
*                                                           be charged and discharged separately       control bit(SHEN bit)
*
*
*             For Short:
*                (1)Turn On two switch, Measure Input PIN Test PIN voltage value and save as VALUE1.
*                (2)Force Input PIN Test Point tie to GND and then measure the voltage value and save as VALUE2.
*                (3)Force Input PIN Test Point tie to AVDD and then measure the voltage value and save as VALUE3.
*             If VALUE1 = VALUE2 = VALUE3, it means Input PIN is short.
*
*************************************************************************************************************************/


int main()
{
    FLASH_WALLOW();

#if defined(SPC1158)
    FLASH_SetTiming(100000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    CLOCK_InitWithRCO(100000000UL);
#else
    FLASH_SetTiming(200000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    CLOCK_InitWithRCO(200000000UL);
#endif

    Delay_Init();

    /*
     * Init the UART
     *
     * 1.Set the GPIO34/35 as UART FUNC
     *
     * 2.Enable the UART CLK
     *
     * 3.Set the rest para
     */
    GPIO_SetPinChannel(GPIO_34, GPIO34_UART_TXD);
    GPIO_SetPinChannel(GPIO_35, GPIO35_UART_RXD);
    CLOCK_EnableModule(UART_MODULE);
    UART_Init(UART, 38400);

    printf("=======               Bgin to measure GPIO8 voltage                     ======\n");

    /* Enable G-INT for SOC0 */
    NVIC_EnableIRQ(ADC0_IRQn);
    
    /*
     * ADC Init, config the GPIO8 as the positive channel and the negative is GND.
     * Then On two switch.
     */
    GPIO_SetPinChannel(GPIO_8, GPIO8_ADC8);
    eStatus = ADC_Init(ADC_SOC_0, ADC_SHC_P_ANA_IN8, ADC_SHC_N_GND, SHC, ADCTRIG_Software);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }

    /* div ADC clk, the more lower clock the more accurate the sample result*/
    CLOCK->ADCCLKCTL.bit.DIV = 0xFF;

    /*
     * set ADC sample time (as the same as the ADC clock), the bigger value of
     * SAMPCNT/CONVCNT, the more accurate the sample result
     */
    ADC->ADCSOCCTL[0].bit.SAMPCNT = 255;   /* SAMPCNT is 8-bit length */
    ADC->ADCSOCCTL[0].bit.CONVCNT = 1;     /* CONVCNT is 7-bit length */

    /* Software trigger */
    ADC_SoftwareTrigger(ADC_SOC_0);

    Delay_Ms(100);

    printf("======= Bgin to try to tied the ADC8 to GND and then sample the voltage ======\n");
    /* Set GPIO8 as GPIO mode */
    GPIO_SetPinChannel(GPIO_8, GPIO8_GPIO8);

    /*
     * Enable GPIO input, otherwise, the input will be always zero and the result of ADC sampler
     * will be always zero.
     */
    PINMUX->GPIO8.bit.IE = 1;

    /*
     * Set the GPIO8 as pull down model to discharge, keep in mind that the pull up and pull down
     * is not exclusive.
     */
    GPIO_DisablePullUp(GPIO_8, GPIO8_GPIO8);
    GPIO_EnablePullDown(GPIO_8, GPIO8_GPIO8);

    /*Delay for a short time to let the discharge over*/
    Delay_Ms(100);

    /*
     * ADC Init, config the GPIO8 as the positive channel and the negative is GND.
     * Then On two switch.
     */
    GPIO_SetPinChannel(GPIO_8, GPIO8_ADC8);
    eStatus = ADC_Init(ADC_SOC_0, ADC_SHC_P_ANA_IN8, ADC_SHC_N_GND, SHC, ADCTRIG_Software);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }

    /* div ADC clk, the more lower clock the more accurate the sample result*/
    CLOCK->ADCCLKCTL.bit.DIV = 0xFF;

    /*
     * set ADC sample time (as the same as the ADC clock), the bigger value of
     * SAMPCNT/CONVCNT, the more accurate the sample result
     */
    ADC->ADCSOCCTL[0].bit.SAMPCNT = 255;   /* SAMPCNT is 8-bit length */
    ADC->ADCSOCCTL[0].bit.CONVCNT = 1;     /* CONVCNT is 7-bit length */

    /* Use software to trigger ADC SOC0 to sample and convert */
    ADC_SoftwareTrigger(ADC_SOC_0);

    Delay_Ms(100);

    printf("======= Bgin to try to tied the ADC8 to AVDD and then sample the voltage ======\n");
    /* Set GPIO8 as GPIO mode */
    GPIO_SetPinChannel(GPIO_8, GPIO8_GPIO8);

    /*
     * Set the GPIO8 as pull up model to pre-charge, keep in mind that the pull up and pull down
     * is not exclusive.
     */
    GPIO_EnablePullUp(GPIO_8, GPIO8_GPIO8);
    GPIO_DisablePullDown(GPIO_8, GPIO8_GPIO8);

    /* Delay for a short time to let the pre-charge over */
    Delay_Ms(100);

    /*
     * ADC Init, config the GPIO8 as the positive channel and the negative is GND.
     * Then On two switch.
     */
    GPIO_SetPinChannel(GPIO_8, GPIO8_ADC8);
    eStatus = ADC_Init(ADC_SOC_0, ADC_SHC_P_ANA_IN8, ADC_SHC_N_GND, SHC, ADCTRIG_Software);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }
    
    /* Use software to trigger ADC SOC0 to sample and convert */
    ADC_SoftwareTrigger(ADC_SOC_0);

    Delay_Ms(100);

    /* Check the result */
    if ((ABS(VALUE1 - VALUE2) < 200) && (ABS(VALUE1 - VALUE3) < 200))
    {
        printf("GPIO8 is short\n");
    }
    else
    {
        printf("GPIO8 is not short\n");
    }

    while (1)
    {

    }
}

void ADC0_IRQHandler(void)
{
    if(VALUE1 == 0)
    {
        /*Get the result*/
        VALUE1 = ADC_GetResult(ADC_SOC_0);
        printf("Voltage is (%d) : %0.3fV\n", VALUE1, RESULTTOVOLTAGE(VALUE1));
    }
    else if(VALUE2 == 0)
    {
        /* Get the result */
        VALUE2 = ADC_GetResult(ADC_SOC_0);
        printf("Voltage is (%d) : %0.3fV\n", VALUE2, RESULTTOVOLTAGE(VALUE2));
    }
    else if(VALUE3 == 0)
    {
        /* Get the result */
        VALUE3 = ADC_GetResult(ADC_SOC_0);
        printf("Voltage is (%d) : %0.3fV\n", VALUE3, RESULTTOVOLTAGE(VALUE3));
    }

    /* Clear SOC0 INT */
    ADC_ClearInt0();
}


