/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.2
 *
 * @date        2023-03-31
 *
 * @attention
 *
 *  Copyright (C) 2023 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "main.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup ADC_ContinuousConversion
  @{
*/

/** @defgroup ADC_ContinuousConversion_Functions Functions
  @{
*/

/* ADC Init */
void ADC_Init(void);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDInit(LED3);

    APM_MINI_LEDOff(LED2);
    APM_MINI_LEDOff(LED3);

    ADC_Init();

    while(1)
    {
    }
}

/*!
 * @brief       ADC Calibration
 *
 * @param       None
 *
 * @retval      None
 */
void ADC_Calibration(void)
{
    uint8_t i;
    int8_t offset = 0;
    uint16_t adcData = 0;

    ADC_SetOffset(0);
    ADC_EnableCompensation();
    ADC_ClearStatusFlag(ADC_FLAG_CC);
    ADC_Enable();
    ADC_StartConversion();

    for(i = 0; i < 10; i++)
    {
        while(ADC_ReadStatusFlag(ADC_FLAG_CC) == RESET);
        ADC_ClearStatusFlag(ADC_FLAG_CC);
    }
    ADC_Disable();

    adcData = ADC_ReadData();
    offset = (int8_t)(0x800 - adcData);
    ADC_SetOffset(offset);

    ADC_DisableCompensation();
    ADC_ClearStatusFlag(ADC_FLAG_CC);
}

/*!
 * @brief       ADC initialization
 *
 * @param       None
 *
 * @retval      None
 */
void ADC_Init(void)
{
    ADC_Config_T adcConfig;
    GPIO_Config_T gpioConfig;

    /* ADC GPIO configuration */
    gpioConfig.mode = GPIO_MODE_IN_FLOATING;
    gpioConfig.speed = GPIO_SPEED_10MHz;
    gpioConfig.intEn = GPIO_EINT_DISABLE;
    gpioConfig.pin = GPIO_PIN_3;
    GPIO_Config(GPIOD, &gpioConfig);

    ADC_SetMode(ADC_MODE_SINGLE_END);

    /* ADC configuration */
    ADC_ConfigStructInit(&adcConfig);
    adcConfig.channel = ADC_CHANNEL_4;
    adcConfig.convMode = ADC_CONV_MODE_CONTINUOUS;
    adcConfig.interrupt = ADC_INT_CC;
    ADC_Config(&adcConfig);

    /* ADC Calibration */
    ADC_Calibration();

    NVIC_EnableIRQRequest(ADC_IRQn, 0X01);
    ADC_Enable();
    ADC_StartConversion();
}

/*!
 * @brief       ADC interrupt service routine
 *
 * @param       None
 *
 * @retval      None
 */
void ADC_Isr(void)
{
    uint16_t adcData = 0;
    uint16_t voltage = 0;

    if(ADC_ReadIntFlag(ADC_INT_FLAG_CC) == SET)
    {
        ADC_ClearIntFlag(ADC_INT_FLAG_CC);

        adcData = ADC_ReadData();
        /* voltage(mV) =  adcData * (3300mV / 4095) */
        voltage = (adcData * 3300) / 4095;

        /* voltage greater than 3100mv */
        if(voltage > 3100)
        {
            APM_MINI_LEDOn(LED2);
            APM_MINI_LEDOff(LED3);
        }
        /* voltage less than 800mv */
        else if(voltage < 800)
        {
            APM_MINI_LEDOn(LED3);
            APM_MINI_LEDOff(LED2);
        }
        /* 800mv <= voltage <= 3100 */
        else
        {
            APM_MINI_LEDOn(LED2);
            APM_MINI_LEDOn(LED3);
        }
    }
}

/**@} end of group ADC_ContinuousConversion_Functions */
/**@} end of group ADC_ContinuousConversion */
/**@} end of group Examples */
