/******************************************************************************
 * @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
#define               SAMPLETIMENS                 500
#define               CONVTIMENS                   500

int16_t               i16VSP                       = -1;


 /*************************************************************************************************************************
 *
 * @brief      In this case, a voltage was given to the GPIO9/2/12, then ADC Sampler C/A/B will sample the voltage respectively,
 *             after SOC9 had converted done, the EOC will trigger SOC12 start to work(SOC12 is the next SOC in round robin
 *             normally), when SOC12 convertion is done, the EOC will trigger SOC2. As a result, SOC9, SOC12, SOC2 will work
 *             one by one.
 *
 *************************************************************************************************************************/


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 GPIO2/9/12 as the ADC soc2/9/12 Positive pin respectively, the Negative pin of them is GND */
  ADC_EasyInit1(ADC_SOC_9,ADC9_GPIO9,ADCTRIG_Software);
  ADC_EasyInit1(ADC_SOC_2,ADC2_GPIO2,ADCTRIG_Software);
  ADC_EasyInit1(ADC_SOC_12,ADC12_GPIO12,ADCTRIG_Software);
  
  /* 
   * Default convert and sample time was 150ns in 'ADC_EasyInit1', reset the value here .
   * if the value is too small, when we handle event in SOC9 INT Handler, SOC2 and SOC12 may be done during the SOC9 handler period,
   * as a result, when SOC9 INT done, SOC2 and SOC12 INT come to CPU simutaneously, then CPU will process SOC2 INT firstly, and then
   * SOC12 INT, this is not we want.
   */
  ADC_SetSampleAndConvertTime(ADC_SOC_9,SAMPLETIMENS,CONVTIMENS);
  ADC_SetSampleAndConvertTime(ADC_SOC_2,SAMPLETIMENS,CONVTIMENS);
  ADC_SetSampleAndConvertTime(ADC_SOC_14,SAMPLETIMENS,CONVTIMENS);

  /* Enbale SOC2/12 can be triggered by signal come from ADC EOC(end of convert) INT */
  ADC_EnableIntTriggerSOC(ADC_SOC_2);
  ADC_EnableIntTriggerSOC(ADC_SOC_12);

  /* Set the trigger source of SOC2/12 as the EOC of SOC9 */
  ADC_SelectIntTriggerSOC(ADC_SOC_12, ADC_INT_9);
  ADC_SelectIntTriggerSOC(ADC_SOC_2, ADC_INT_9);

  /* Enable G-INT for SOC9/2/12 */
  NVIC_EnableIRQ(ADC9_IRQn);
  NVIC_EnableIRQ(ADC2_IRQn);
  NVIC_EnableIRQ(ADC12_IRQn);

  /* Use software to trigger SOC9 to work */
  ADC_SoftwareTrigger(ADC_SOC_9);

  while(1)
  {

  }
}

void ADC9_IRQHandler(void)
{

  /* Result gotten from 'ADC_GetResult()' can be a negtive value or a positive value */ 
  i16VSP = ADC_GetResult(ADC_SOC_9);

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

  /* Clear SOC INT */
  ADC_ClearInt9();

  /* Clear SOC INT flag */
  ADC_ClearInt(ADC_SOC_9);

  i16VSP = -1;
}

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

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

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

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

  /* Clear SOC INT */
  ADC_ClearInt2();

  /* Clear SOC INT flag */
  ADC_ClearInt(ADC_SOC_2);

  i16VSP = -1;
}

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

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

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

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

  /* Clear SOC INT */
  ADC_ClearInt12();

  /* Clear SOC INT flag */
  ADC_ClearInt(ADC_SOC_12);

  i16VSP = -1;
}
