/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.3
 * @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

/* This macro is used to get the PWM period with a specified frequency */
#define              PWMPeriod(u32PWMFreqHz)              ((CLOCK_GetModuleClock(PWM_MODULE)) / u32PWMFreqHz) / 2;

#define              PWM_FREQ                             2000                                /* 2kHz PWM */
#define              PWM_DB_NS                            1000                                /* 1000ns */

uint32_t             u32PWMPeriod;
int32_t              i32result0;
int32_t              i32result1;
int32_t              i32result2;

ErrorStatus          eStatus = ERROR;

/*************************************************************************************************************************
*
* @brief      In this case, PWM0SOCA trigger the ADC SOC0 event with three sampler(A/B/C) is working, but only the channel
*             selecting information is valid in the configuration of SOC1 and SOC2, other configuration information coming
*             from SOC0, The ADC will start conversion base on the configuration of SOC0, sys will not start SOC1/2 conversion,
*             after all conversion is done, ADC0 INT assert.
*
*             In order to get the right result, you need to give a voltage to GPIO8/4/14 simultaneously.
*
*************************************************************************************************************************/


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

    /* Initial Basic Complementary PWM */
    PWM_ComplementaryPairChannelInit(PWM0, PWM_FREQ, PWM_DB_NS);

    /* Set PWM5B output 50% duty waveform */
    u32PWMPeriod = PWMPeriod(PWM_FREQ);
    PWM_SetCMPA(PWM0, (u32PWMPeriod * 3) / 4);

    /* Start counting */
    PWM_RunCounter(PWM0);

    /* PWM trigger timing selecting */
    PWM_SetSOCATiming(PWM0, EQU_ZERO);

    /* PWM trigger period selecting */
    PWM_SetSOCAPeriod(PWM0, ON_15TH_EVENT);

    /* Enable PWM SOCA trigger */
    PWM_EnableSOCA(PWM0);

    /* INT service routine configuration */
    NVIC_SetPriority(ADC0_IRQn, 1);
    NVIC_EnableIRQ(ADC0_IRQn);
    
    /*
     * ADC SOC0 Init
     */
    GPIO_SetPinChannel(GPIO_8, GPIO8_ADC8);
    eStatus = ADC_Init(ADC_SOC_0, ADC_SHA_P_ANA_IN8, ADC_SHA_N_GND, SHA_AND_SHB_SHC, ADCTRIG_PWM0SOCA);
    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_PWM0SOCA);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }

    GPIO_SetPinChannel(GPIO_14, GPIO14_ADC14);
    eStatus = ADC_Init(ADC_SOC_2, ADC_SHC_P_ANA_IN14, ADC_SHC_N_GND, SHC, ADCTRIG_PWM0SOCA);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }
    
    while (1)
    {

    }
}

void ADC0_IRQHandler(void)
{
    /* The result of SHA/B/C is stored in RESULT0/1/2 respectively */
    /* Result gotten from 'ADC_GetResult()' can be a negative value or a positive value*/
    i32result0 = ADC_GetResult(ADC_SOC_0);
    i32result1 = ADC_GetResult(ADC_SOC_1);
    i32result2 = ADC_GetResult(ADC_SOC_2);
    printf("ADC SOC0 Voltage = %2fV(Raw data is %d)\n", (double)ValueToVoltage(i32result0), i32result0);

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

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

    /* Clear SOC0 INT */
    ADC_ClearInt0();

    Delay_Ms(3000);
}
