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

int16_t          i16VSP;


 /*************************************************************************************************************************
 *
 * @brief      In this case, giving the GPIO10/12 a voltage and sample the voltage through sampler A and B simultaneously.
 *
 *             The KEY point : 
 *                                ADC result calculation formula:
 *                                
 *                                               (AIP-AIN)
 *                                ADC_result = ----------- * 8192
 *                                                 3.657
 *
 *                                GPIO10       Sampler A
 *                                              SOC0
 *                                 -            |\
 *                                | |           | \
 *                                | |-----------| /-----------RESULT[0]
 *                                 -            |/
 *
 *                                             Sampler B 
 *                                GPIO12         SOC1
 *                                 -            |\
 *                                | |           | \
 *                                | |-----------| /-----------RESULT[1]
 *                                 -            |/
 *
 *************************************************************************************************************************/


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

  /*
   * ADC Init, Set GPIO10 as the ADC soc0 Positive pin, while the GND is the 
   * Negative pin.
   */
  ADC_EasyInit1(ADC_SOC_0,ADC10_GPIO10,ADCTRIG_Software);

  /* Set SOC0 sample 4 times (2^2) at every convert */
  ADC_SetAverageCnt(ADC_SOC_0, ADCSOCCTL0_BIT_AVGCNT_AVG_4);

  /* Set samplerA and B work simultaneously */
  ADC_SetSOCSH(ADC_SOC_0, ADCSOCCTL0_BIT_SHEN_SHA_AND_SHB);

  /*
   * Set the GPIO12 as the ADC soc1 Positive pin, while the GND is the
   * Negative pin. But the configuration(such as simultaneous mode, trigger
   * source, averaging times, sampling count, conversion count) will be 
   * ignored and keep the same as GPIO8.
   */
  ADC_EasyInit1(ADC_SOC_1,ADC12_GPIO12,ADCTRIG_Software);

  /*
   * Sampler A convert firstly, and then sampler B, so if sampler B sent out
   * EOC, it means nable all conversion is complete. But the INT num is the 
   * SOC0, this must keep in mind, so Enbale the global INT for ADC0.
   */
  NVIC_EnableIRQ(ADC0_IRQn);

  /* Software trigger */
  ADC_SoftwareTrigger(ADC_SOC_0);

  Delay_Ms(500);

  while(1)
  {

  }
}

void ADC0_IRQHandler(void)
{
  /* Result gotten from 'ADC_GetResult()' can be a negtive value or a positive value */
  i16VSP = ADC_GetResult(ADC_SOC_0);
  printf("ADC SOC0 Voltage = %2fV(data is %d)\n", (double)ValueToVoltage(i16VSP),i16VSP);

  /* Result gotten from 'ADC_GetTrimResult1()' is a positive value */
  i16VSP = ADC_GetTrimResult1(ADC_SOC_0);
  printf("ADC SOC0 Voltage = %2fV(Trim data is %d)\n", (double)ValueToVoltage(i16VSP),i16VSP);

  /* Result gotten from 'ADC_GetResult()' can be a negtive value or a positive value */
  i16VSP = ADC_GetResult(ADC_SOC_1);
  printf("ADC SOC1 Voltage = %2fV(data is %d)\n", (double)ValueToVoltage(i16VSP),i16VSP);

  /* Result gotten from 'ADC_GetTrimResult1()' is a positive value */
  i16VSP = ADC_GetTrimResult1(ADC_SOC_1);
  printf("ADC SOC1 Voltage = %2fV(Trim data is %d)\n", (double)ValueToVoltage(i16VSP),i16VSP);

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

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

  Delay_Ms(2000);
}

