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

/*Variable for ADC result*/
int32_t        i32VSP;

ErrorStatus    eStatus = ERROR;


/**************************************************************************
 *
 * @brief      PGA-ADC Calibration Function to get PGA-ADC system calibration parameter
 *              (1) Enable Channel Fractional Result;
 *              (2) First PGA_P PGA_N connect to 1.65V, result is Vadc_out1;                                         
 *              (3) In 4x amplification factor, PGA_P connect to 2.25V, 
 *               PGA_N connect to 1.65V, result is Vadc_out2;                                    
 *              (4) ADCOFFSET=Vadc_out1;
 *              (5) ADCGAIN=32768*  (4*(0.6V/3.657143*8192))/(Vadc_out2-Vadc_out1);
 *
 *        Key_points:
 *              (1) Can only use ADC_SetChannelOffset() and ADC_SetChannelGain() 
 *              to set ADCOFFSET and ADCGAIN in PGA-ADC Channel, other Channel
 *              is prohibit.
 *              (2) When PGA-ADC Channel is trim, can not use this Channel
 *              to measure ADC alone, because the PGA-ADC trim parameter is 
 *              not suitable to ADC alone
 *
 **************************************************************************/


int main()
{
    uint32_t j;
    
    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);

    /*
     * Set PGA in differential ended mode.
     *
     * 1.Set the GPIO8(ADC8) as the P-input of the PGA0.
     *
     * 2.Set the GPIO9(ADC9) as the N-input of the PGA0.
     *
     * 3.Set the PGA0 amplitude scale as 4x.
     *
     * 4.Enable the PGA0.
     */
    GPIO_SetPinChannel(GPIO_8, GPIO8_ADC8);
    GPIO_SetPinChannel(GPIO_9, GPIO9_ADC9);
    PGA_DifferentialInit(PGA0, PGA0_CH_P_ADC8, PGA0_CH_N_ADC9, PGA_SCALE_4X);

    /*
     * ADC Init.
     *
     * 1.Set the ADC as differential mode.
     *
     * 2.Use SHA to sample the PGA0P and PGA0N which had been set as the P/N channel.
     *
     * 3.Set software as the tigger model.
     */
    eStatus = ADC_Init(ADC_SOC_0, ADC_SHA_P_PGA0P_OUT, ADC_SHA_N_PGA0N_OUT, SHA, ADCTRIG_Software);
    if (ERROR != eStatus)
    {
        printf("ADC_Init ERROR\n");
        return 1;
    }

    printf("ChannelGain %d\n", ADC->ADCGAIN[ADC_SOC_0].all);
    printf("ChannelOffset %d\n", ADC->ADCOFFSET[ADC_SOC_0].all);
    
    ADC->ADCGAIN[ADC_SOC_0].all   = 32768;
    ADC->ADCOFFSET[ADC_SOC_0].all = 0;
    
    printf("ChannelGain %d\n", ADC->ADCGAIN[ADC_SOC_0].all);
    printf("ChannelOffset %d\n", ADC->ADCOFFSET[ADC_SOC_0].all);

    /* Set sample and convert time */
    ADC_SetSampleAndConvertTime(ADC_SOC_0, 8000U, ADC_DEFAULT_CONVERSION_TIME_NS);

    ADC_SetAverageCnt(ADC_SOC_0, ADCSOCCTL0_BIT_AVGCNT_AVG_128);

    for(j = 0; j < 5; j ++)
    {
        /* Use software to trigger ADC SOC0 to work */
        ADC_SoftwareTrigger(ADC_SOC_0);

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

        /* Get trim result */
        i32VSP = ADC_GetTrimResult2(ADC_SOC_0);

        /* Clear ADC SOC0 INT flag */
        ADC_ClearInt(ADC_SOC_0);

        /* Print the difference voltage value of GPIO8/9 */
        printf("GPIO8/9 difference voltage is %2fV(Trim data is %d)\n", (double)ValueToVoltage(i32VSP), i32VSP);

        Delay_Ms(3000);
    }
    
    while (1)
    {
        
    }
}
