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

int32_t             i32PPUResult;


 /*************************************************************************************************************************
 *
 * @brief      In this test case, we set GPIO8 as ADC function and give the GPIO different voltage, as a result, we can 
 *             printf info in interruput handle if the gave voltage is over/under the configured TZHI/TZLO register bits.
 *
 *************************************************************************************************************************/


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 */
  ADC_EasyInit1(ADC_SOC_0,ADC8_GPIO8,ADCTRIG_Software);

  /* Init ADC PPU */
  ADC_PPUInit(ADC_PPU_0, 0, 100, ADC_PPU_CODE_MINUS_REF);

  /* Enable zero-crossing interrupt of ADC PPU0 */
  ADC_EnablePPUInt(ADC_PPU_0, ADC_PPU_CROSS_ZERO);
  /* Enable too high interrupt of ADC PPU0 */
  ADC_EnablePPUInt(ADC_PPU_0, ADC_PPU_TOO_HIGH);
  /* Enable too low interrupt of ADC PPU0 */
  ADC_EnablePPUInt(ADC_PPU_0, ADC_PPU_TOO_LOW);

  /* Set PPU0 high threshold is 1500 */
  ADC_SetPPUTooHighThreshold(ADC_PPU_0, 1500);
  /* Set PPU0 low threshold is 1000 */
  ADC_SetPPUTooLowThreshold(ADC_PPU_0, 1000);

  /* Clear SOC0 INT flag */
  ADC_ClearInt0();

  /* Clear PPU Unit INT */
  ADC_ClearPPUGlobalInt(ADC_PPU_0);
  ADC_ClearPPUInt(ADC_PPU_0, ADC_PPU_TOO_LOW);
  ADC_ClearPPUInt(ADC_PPU_0, ADC_PPU_TOO_HIGH);
  ADC_ClearPPUInt(ADC_PPU_0, ADC_PPU_CROSS_ZERO);

  NVIC_EnableIRQ(ADCPPU0_IRQn);

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

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

  while(1){

  }
}

void ADCPPU0_IRQHandler(void)
{
  if(ADC_GetPPUIntFlag(ADC_PPU_0, ADC_PPU_CROSS_ZERO) != 0)
    printf("Zero-cross INT had happened\n");

  if(ADC_GetPPUIntFlag(ADC_PPU_0, ADC_PPU_TOO_HIGH) != 0)
    printf("ADC channel voltage too-high INT had happened\n");

  if(ADC_GetPPUIntFlag(ADC_PPU_0, ADC_PPU_TOO_LOW) != 0)
    printf("ADC channel voltage too-low INT had happened\n");

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

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

  i32PPUResult = ADC_GetPPUResult(ADC_PPU_0);
  printf("SOC0 Comparison data : %d\n",i32PPUResult);

  i32PPUResult = ADC_GetSOCDelay(ADC_PPU_0);
  printf("The period between the trigger signal and the start of converting : %d\n",i32PPUResult);

  /* Clear the PPU0 INTs and global INT of PPU0 */
  ADC_ClearPPUInt(ADC_PPU_0, ADC_PPU_CROSS_ZERO);
  ADC_ClearPPUInt(ADC_PPU_0, ADC_PPU_TOO_HIGH);
  ADC_ClearPPUInt(ADC_PPU_0, ADC_PPU_TOO_LOW);
  ADC_ClearPPUGlobalInt(ADC_PPU_0);

  /* Continues to use software to trigger the ADC to work */
  ADC_SoftwareTrigger(ADC_SOC_0);

  Delay_Ms(2000);
}
