/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.1
 * @date     30-July-2026
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. 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<stdio.h>

#if defined(SPC1158)
#include "spc1158.h"
#elif defined(SPD1148)
#include "spd1148.h"
#elif defined(SPD1178)
#include "spd1178.h"
#elif defined(SPD1188)
#include "spd1188.h"
#elif defined(SPD1163)
#include "spd1163.h"
#else
#include "spc1168.h"
#endif

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


int32_t          i32VSP;
ErrorStatus      eStatus = ERROR;


/*************************************************************************************************************************
*
* @brief      In this case, sample A and B simultaneously, but convert one by one
*
*                                   The KEY point :
*                                                      ADC result calculation formula:
*
*                                                                     (AIP-AIN)
*                                                      ADC_result = ----------- * 8192
*                                                                       3.657
*
*                                        GPIOx       Sampler A
*                                                      SOC0
*                                         -            |\
*                                        | |           | \
*                                        | |-----------| /-----------RESULT[0]
*                                         -            |/
*
*                                                     Sampler B
*                                        GPIOy         SOC1
*                                         -            |\
*                                        | |           | \
*                                        | |-----------| /-----------RESULT[1]
*                                         -            |/
*
*************************************************************************************************************************/


int main()
{
    FLASH_WALLOW();

#if defined(SPC1158)
    FLASH_SetTiming(100000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    CLOCK_InitWithRCO(100000000UL);
#else
    FLASH_SetTiming(200000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    CLOCK_InitWithRCO(200000000UL);
#endif

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

    /*
     * when voltage of sample B is converted, End-of-Conversion signal is connect to INT0
     */
    NVIC_EnableIRQ(ADC0_IRQn);
    
#if defined(SPD1163)
    /*
     * ADC SOC0 Init
     */
    GPIO_SetPinChannel(GPIO_0, GPIO0_ADC0);
    eStatus = ADC_Init(ADC_SOC_0, ADC_SHA_P_ANA_IN0, ADC_SHA_N_GND, SHA_AND_SHB, ADCTRIG_Software);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }

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

    /*
     * ADC SOC1 Init, the configuration of SH, trigger source,
     * averaging times, sampling count, conversion count will be
     * ignored and keep the same as SOC0.
     */
    GPIO_SetPinChannel(GPIO_4, GPIO4_ADC4);
    eStatus = ADC_Init(ADC_SOC_1, ADC_SHB_P_ANA_IN4, ADC_SHB_N_GND, SHB, ADCTRIG_Software);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }
#else
    /*
     * ADC SOC0 Init
     */
    GPIO_SetPinChannel(GPIO_10, GPIO10_ADC10);
    eStatus = ADC_Init(ADC_SOC_0, ADC_SHA_P_ANA_IN10, ADC_SHA_N_GND, SHA_AND_SHB, ADCTRIG_Software);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }

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

    /*
     * ADC SOC1 Init, the configuration of SH, trigger source,
     * sampling count, conversion count, averaging time will be
     * ignored and keep the same as SOC0.
     */
    GPIO_SetPinChannel(GPIO_12, GPIO12_ADC12);
    eStatus = ADC_Init(ADC_SOC_1, ADC_SHB_P_ANA_IN12, ADC_SHB_N_GND, SHB, ADCTRIG_Software);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }
#endif

    /* Software trigger */
    ADC_SoftwareTrigger(ADC_SOC_0);

    Delay_Ms(500);

    while (1)
    {

    }
}

void ADC0_IRQHandler(void)
{
    i32VSP = ADC_GetResult(ADC_SOC_0);
    printf("ADC SOC0 Voltage = %2fV(data is %d)\n", (double)ValueToVoltage(i32VSP), i32VSP);

    i32VSP = ADC_GetResult(ADC_SOC_1);
    printf("ADC SOC1 Voltage = %2fV(data is %d)\n", (double)ValueToVoltage(i32VSP), i32VSP);

    /* 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);
}

