/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V1.8.0
 * @date     31-Dec-2021
 *
 * @note
 * Copyright (C) 2021 Spintrol Limited Corporation. 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"spc1168.h"
#include<stdio.h>

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

int32_t            i32lresult, i32hresult;


 /*************************************************************************************************************************
 *
 * @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      simutaneously by one 
 *                                                           be charged and dischaged 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)Trun off two switch, pull up both Test PIN, so that, the two Test PIN will be 3.3V.
 *                (4)Trun 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
 *             (flotting).
 *
 *************************************************************************************************************************/


int main()
{
  FLASH_WALLOW();
  FLASH_SetTiming(200000000);
  /* Disable flash write access after flash operation had done */
  FLASH_WDIS();

  CLOCK_InitWithRCO(CLOCK_HCLK_200MHZ);

  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 ADC positive PIN(ADC test point) tied to low to discharge */
  ADC->ADCCTL.bit.TIEAP = ADCCTL_BIT_TIEAP_FORCE_LOW;

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

  /*
   * ADC Init, config the GPIO8 as the positive channel and the negtive is GND.
   * Then On two switch.
   */
  ADC_EasyInit1(ADC_SOC_0,ADC8_GPIO8,ADCTRIG_Software); 

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

  /* Wait until ADC conversion finished (Interrupt flag is set) */
  while(!ADC_GetIntFlag(ADC_SOC_0));

  /* Get the result */
  i32lresult = ADC_GetResult(ADC_SOC_0);
  printf("Voltage is (%d) : %0.3fV\n",i32lresult,RESULTTOVOLTAGE(i32lresult));

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

  /* 
   * Last 'ADC_EasyInit1()', GPIO8 was set as analog ADC mode, but pull up and down function
   * is only valid in GPIO mode.
   */
  GPIO_SetPinChannel(GPIO_8, GPIO8_GPIO8);

  /*
   * Set the GPIO8 as pull up model to precharge, 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 ADC positive PIN tied high to charge */
  ADC->ADCCTL.bit.TIEAP = ADCCTL_BIT_TIEAP_FORCE_HIGH;

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

  /*
   * ADC Init, config the GPIO8 as the positive channel and the negtive is GND.
   * Then On two switch.
   */
  ADC_EasyInit1(ADC_SOC_0,ADC8_GPIO8,ADCTRIG_Software);

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

  /* Wait until ADC conversion finished (Interrupt flag is set) */
  while(!ADC_GetIntFlag(ADC_SOC_0));

  /* Get the ADC SOC result */
  i32hresult = ADC_GetResult(ADC_SOC_0);
  printf("Voltage is (%d) : %0.3fV\n",i32hresult,RESULTTOVOLTAGE(i32hresult));

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

  while(1){

  }
}
