/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.0
 * @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
#define               SAMPLETIMENS                 300
#define               CONVTIMENS                   700


int32_t               i32VSP                       = -1;
ErrorStatus           eStatus                      = ERROR;

/*************************************************************************************************************************
*
* @brief      In this case, a voltage was given to the input GPIO, after SOC9 had converted done, the EOC will trigger 
*             SOC12 start to work(SOC12 is the next SOC in round robin normally), when SOC12 conversion is done, the EOC
*             will trigger SOC2. As a result, SOC9, SOC12, SOC2 will work one by one.
*
*************************************************************************************************************************/


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);
    
    /* Enable G-INT for SOC9/2/12 */
    NVIC_EnableIRQ(ADC9_IRQn);
    NVIC_EnableIRQ(ADC2_IRQn);
    NVIC_EnableIRQ(ADC12_IRQn);
    
#if defined(SPD1188)
    /* ADC Init */
    GPIO_SetPinChannel(GPIO_8, GPIO8_ADC8);
    eStatus = ADC_Init(ADC_SOC_9, ADC_SHA_P_ANA_IN8, ADC_SHA_N_GND, SHA, ADCTRIG_Software);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }

    GPIO_SetPinChannel(GPIO_9, GPIO9_ADC9);
    eStatus = ADC_Init(ADC_SOC_2, ADC_SHA_P_GND, ADC_SHA_N_ANA_IN9, SHA, ADCTRIG_Software);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }

    GPIO_SetPinChannel(GPIO_10, GPIO10_ADC10);
    eStatus = ADC_Init(ADC_SOC_12, ADC_SHA_P_ANA_IN10, ADC_SHA_N_GND, SHA, ADCTRIG_Software);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }
#else
    /* ADC Init */
    GPIO_SetPinChannel(GPIO_0, GPIO0_ADC0);
    eStatus = ADC_Init(ADC_SOC_9, ADC_SHA_P_ANA_IN0, ADC_SHA_N_GND, SHA, ADCTRIG_Software);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }

    GPIO_SetPinChannel(GPIO_1, GPIO1_ADC1);
    eStatus = ADC_Init(ADC_SOC_2, ADC_SHA_P_GND, ADC_SHA_N_ANA_IN1, SHA, ADCTRIG_Software);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }

    GPIO_SetPinChannel(GPIO_4, GPIO4_ADC4);
    eStatus = ADC_Init(ADC_SOC_12, ADC_SHB_P_ANA_IN4, ADC_SHB_N_GND, SHB, ADCTRIG_Software);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }
#endif

    /*
     * Default convert and sample time was 150ns in 'ADC_Init', 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 all come to MCU, then MCU 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_12, SAMPLETIMENS, CONVTIMENS);

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

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

    while (1)
    {

    }
}

void ADC9_IRQHandler(void)
{
    i32VSP = ADC_GetResult(ADC_SOC_9);

    /* Clear SOC INT */
    ADC_ClearInt9();

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

    i32VSP = -1;
}

void ADC2_IRQHandler(void)
{
    i32VSP = ADC_GetResult(ADC_SOC_2);

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

    /* Clear SOC INT */
    ADC_ClearInt2();

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

    i32VSP = -1;
}

void ADC12_IRQHandler(void)
{
    i32VSP = ADC_GetResult(ADC_SOC_12);

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

    /* Clear soc12 INT */
    ADC_ClearInt12();

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

    i32VSP = -1;
}
