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

/* Timer INT period, unit:ms */
#define             TIMEPERIOD                  2000

int16_t             i16VSP                      = -1;


 /*************************************************************************************************************************
 *
 * @brief      In this case, a voltage was given to the GPIO10/4/6, then ADC Sampler A/B/C will sample the voltage respectively.
 *             After SOC6 had converted done, SOC10 is the next in round robin normally, but we had set the SOC0~4 the highest
 *             priority now, so, when SOC4 and SOC10 triggered by timer1 simultaneously, SOC4 will  be converted first.
 *
 *************************************************************************************************************************/


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/4/6 as the corresponding SOC Positive pin, while the GND is the Negative pin,
   * and the SOC4 and SOC10 is triggered by timer1.
   */
  ADC_EasyInit1(ADC_SOC_6,ADC6_GPIO6,ADCTRIG_Software);
  ADC_EasyInit1(ADC_SOC_4,ADC4_GPIO4,ADCTRIG_Timer1);
  ADC_EasyInit1(ADC_SOC_10,ADC10_GPIO10,ADCTRIG_Timer1);

  /* Set high priority for SOC0~4 */
  ADC_SetSOCPriority(ADCSOCPRICTL_BIT_PRIORITY_CUTOFF_AT_SOC5);

  /* Enable G-INT for SOC4/6/10 */
  NVIC_EnableIRQ(ADC4_IRQn);
  NVIC_EnableIRQ(ADC6_IRQn);
  NVIC_EnableIRQ(ADC10_IRQn);

  /* Enbale G-INT for TIMER1 */
  NVIC_EnableIRQ(TIMER1_IRQn);

  /* Use software to trigger ADC SOC6 to work */
  ADC_SoftwareTrigger(ADC_SOC_6);

  /* Init TIMER1 to count for 2000ms */
  TIMER_Init(TIMER1, TIMEPERIOD);

  /* Enbale TIMER1 to generate a signal to trigger ADC SOC to work */
  TIMER_EnableADCSOC(TIMER1);

  while(1)
  {

  }
}

void TIMER1_IRQHandler(void)
{
  TIMER_ClearInt(TIMER1);
  TIMER_DisableInt(TIMER1);
  TIMER_DisableADCSOC(TIMER1);
}

void ADC6_IRQHandler(void)
{
  /* Result gotten from 'ADC_GetResult()' can be a negtive value or a positive value*/
  i16VSP = ADC_GetResult(ADC_SOC_6);

  printf("ADC SOC6 Voltage = %2fV(Raw data is %d)\n", (double)ValueToVoltage(i16VSP),i16VSP);

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

  /* Clear SOC6 INT */
  ADC_ClearInt6();

  /* Clear SOC6 INT flag */
  ADC_ClearInt(ADC_SOC_6);

  i16VSP = -1;
}

void ADC4_IRQHandler(void)
{
  /* Result gotten from 'ADC_GetResult()' can be a negtive value or a positive value */
  i16VSP = ADC_GetResult(ADC_SOC_4);

  printf("ADC SOC4 Voltage = %2fV(Raw data is %d)\n", (double)ValueToVoltage(i16VSP),i16VSP);

  /* Result gotten from 'ADC_GetTrimResult1()' is a positive value */
  i16VSP = ADC_GetTrimResult1(ADC_SOC_4);

  printf("ADC SOC4 Voltage = %2fV(Trim data is %d)\n", (double)ValueToVoltage(i16VSP),i16VSP);

  /* Clear ADC SOC4 INT */
  ADC_ClearInt4();

  /* Clear ADC SOC4 INT flag */
  ADC_ClearInt(ADC_SOC_4);

  i16VSP = -1;
}

void ADC10_IRQHandler(void)
{
  /* Result gotten from 'ADC_GetResult()' can be a negtive value or a positive value */
  i16VSP = ADC_GetResult(ADC_SOC_10);

  printf("ADC SOC10 Voltage = %2fV(Raw data is %d)\n", (double)ValueToVoltage(i16VSP),i16VSP);

  /* Result gotten from 'ADC_GetTrimResult1()' is a positive value */
  i16VSP = ADC_GetTrimResult1(ADC_SOC_10);

  printf("ADC SOC10 Voltage = %2fV(Trim data is %d)\n", (double)ValueToVoltage(i16VSP),i16VSP);

  /* Clear ADC SOC10 INT */
  ADC_ClearInt10();

  /* Clear ADC SOC10 INT flag */
  ADC_ClearInt(ADC_SOC_10);

  i16VSP = -1;
}
