/******************************************************************************
 * @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>
#include<stdlib.h>

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

#define DACTestHTH 3920
#define DACTestLTH 3472
#define DACTest165V	512

int16_t i16VSP;


 /*************************************************************************************************************************
 *
 * @brief      In this case, DAC output 1.65V voltage, and let ADC SOC0 sample this voltage, judging the result is reasonable 
 *             or not at last.
 *
 *************************************************************************************************************************/


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

  /* Enbale COMP model */
  CLOCK_EnableModule(COMP_MODULE);

  /* Enable DAC buffer */
  COMP_DACBufferInit(DAC0, FALSE, FALSE);

  /* Set DAC0 output to 1.65v */
  COMP_SetDACValue10Bit(DAC0,DACTest165V);

  /* Set DAC0 as Direct mode(DAC code is immediately update) */
  COMP_SetDACCodeLoadTiming(DAC0, DAC0CTL_BIT_CODELOAD_DIRECT_MODE);

  /* Enable DAC0 */
  COMP_EnableDAC(DAC0);

  /* Init ADC and set DAC buffer as the positive input of the ADC SOC0 */
  ADC_EasyInit1(ADC_SOC_0, ADCx_DAC_BUF, ADCTRIG_Software);

  /* Enable the global INT for the ADC */
  NVIC_EnableIRQ(ADC0_IRQn);

  /* Set ADC as the software trigger model */
  ADC_SoftwareTrigger(ADC_SOC_0);

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

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

  /* If the result is in error range, the test PASS */
  if((abs(i16VSP) > DACTestLTH) && (abs(i16VSP) < DACTestHTH))
    printf("The DAC test PASS\n");
  else
    printf("The DAC test FAIL, raw data is %d\n",i16VSP);

  ADC_ClearInt(ADC_SOC_0);
  
  Delay_Ms(5000);
  
  /* Use software to trigger ADC SOC0 to work */
  ADC_SoftwareTrigger(ADC_SOC_0);
}
