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

/* As descripted below, ADC result convert to voltage can calculate as the follow */
#define        ValueToVoltage(x)       ((x * 3657) / 8192) /1000

/*Variable for ADC result*/
int16_t        i16VSP;


 /*************************************************************************************************************************
 *
 * @brief      In this case, giving the GPIO8/9 a voltage and set them as the input of PGA, at last, test the voltage from
 *             ADC soc0.
 *
 *             The KEY point : 
 *                            <1>    ADC result calculation formula:
 *
 *                                                 (AIP-AIN)
 *                                   ADC_result = ----------- * 8192
 *                                                   3.657
 *                            <2>    Positive output of PGA(OUTP) must be lowwer then 3V, and negtive output of PGA(OUTN) must be bigger
 *                                   then 0.3V.
 *
 *                                   GPIO8
 *                                    -
 *                                   | |   INP     |\                    |\
 *                                   | |-----------| \        OUTP       | \
 *                                    -            |  \------------------|  \
 *                                                 |   \                 |   \
 *                                   GPIO9         |PGA \                |ADC \--------->result
 *                                    -            |   /      OUTN       |soc0/
 *                                   | |   INN     |  /------------------|  /
 *                                   | |-----------| /                   | /
 *                                    -            |/                    |/
 *
 *************************************************************************************************************************/


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

  /*
   * Set PGA in differetial ended mode.
   * 
   * 1.Set the GPIO8(ADC8) as the P-input of the PGA0.
   *
   * 2.Set the GPIO9(ADC9) as the N-input of the PGA0.
   *
   * 3.Set the PGA0 amplitue scale as 4x.
   *
   * 4.Enable the PGA0.
   */
  PGA_DifferentialInit(PGA0,PGA0_CH_P_ADC8,PGA0_CH_N_ADC9,PGA_SCALE_4X);

  /*
   * ADC Init.
   * 
   * 1.Set the ADC as differential mode.
   * 
   * 2.Use SHA to sample the PGA0P and PGA0N which had been set as the P/N channel.
   * 
   * 3.Set software as the tigger model.
   */
  ADC_EasyInit2(ADC_SOC_0, ADCx_PGA0P, ADCx_PGA0N, ADCTRIG_Software);

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

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

    /* Get trim result */ 
    i16VSP = ADC_GetTrimResult2(ADC_SOC_0);

    /* Clear ADC SOC0 INT flag */ 
    ADC_ClearInt(ADC_SOC_0);

    /* Print the difference voltage value of GPIO8/9 */
    printf("GPIO8/9 difference voltage is %dV\n", ValueToVoltage(i16VSP));

    Delay_Ms(3000);
  }
}
