/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.0
 * @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            i32lresult = 0;
int32_t            i32hresult = 0;
ErrorStatus        eStatus    = ERROR;


/*************************************************************************************************************************
*
* @brief      This test case describes how to detect disconnection 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 disconnection:
*                (1)Turn off two switch, pull down both Test PIN, so that, the two Test PIN will be zero voltage.
*                (2)Then, turn on two switch, and measure Input PIN Test PIN voltage value.
*                (3)Turn off two switch, pull up both Test PIN, so that, the two Test PIN will be 3.3V.
*                (4)Turn on two switch and measure Input PIN Test PIN voltage value.
*             If measured value is zero when discharging and 3.3V when charging, it means the Input PIN is disconnecting(floating).
*
*************************************************************************************************************************/


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 try to tied the ADC8 low and then sample the voltage ======\n");
    /* Turn off the two switch */
    ADC_SetSOCSH(ADC_SOC_0, SH_DISABLE);

    /* 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);

    /* Force the SHC positive pin low */
    ADC->ADCCTL.bit.TIECP = ADCCTL_BIT_TIECP_FORCE_LOW;

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

    ADC->ADCCTL.bit.TIECP = ADCCTL_BIT_TIECP_DISABLE;

    /* 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 */

    /* Use software to trigger ADC SOC0 to work */
    ADC_SoftwareTrigger(ADC_SOC_0);

    Delay_Ms(100);

    printf("======= Bgin to try to tied the ADC8 high and then sample the voltage ======\n");
    /* Turn off the two switch */
    ADC_SetSOCSH(ADC_SOC_0, SH_DISABLE);

    /* 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);

    /* Force the SHC positive pin high */
    ADC->ADCCTL.bit.TIECP = ADCCTL_BIT_TIECP_FORCE_HIGH;

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

    ADC->ADCCTL.bit.TIECP = ADCCTL_BIT_TIECP_DISABLE;

    /*
     * 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 work */
    ADC_SoftwareTrigger(ADC_SOC_0);

    Delay_Ms(100);

    /* Check the result */
    if (i32lresult < 300 && i32hresult > 6500)
    {
        printf("GPIO8 is disconnected\n");
    }
    else
    {
        printf("GPIO8 is connected\n");
    }

    while (1)
    {

    }
}

void ADC0_IRQHandler(void)
{
    if(i32lresult == 0)
    {
        /* Get result */
        i32lresult = ADC_GetResult(ADC_SOC_0);

        printf("Voltage is (%d) : %0.3fV\n", i32lresult, RESULTTOVOLTAGE(i32lresult));
    }
    else if(i32hresult == 0)
    {
        /* Get result */
        i32hresult = ADC_GetResult(ADC_SOC_0);

        printf("Voltage is (%d) : %0.3fV\n", i32hresult, RESULTTOVOLTAGE(i32hresult));
    }

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

