
/******************************************************************************
*@file  : app.c
*@brief : application
******************************************************************************/

#include "app.h" 

#define ADC_NUM    (1)
uint32_t gadcBuffer[ADC_NUM]; 
static volatile uint32_t gadcWatchdogFlag = 0;                

#define ADCX    (ADC1)


/******************************************************************************
* @brief:  User key initialize
* @param:  None.
* @return: None
******************************************************************************/
void UserKEY_Init(void)
{ 
    GPIO_InitTypeDef GPIO_InitStruct;

    __RCC_GPIOC_CLK_ENABLE();

    /* Configure PC13 pin as input */
    GPIO_InitStruct.Pin       = GPIO_PIN_13;
    GPIO_InitStruct.Mode      = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull      = GPIO_PULLUP;
    GPIO_InitStruct.Alternate = GPIO_FUNCTION_0;
    GPIO_Init(GPIOC, &GPIO_InitStruct);  
}

/******************************************************************************
* @brief:  detection button is pressed
* @param:  None.
* @return: None
******************************************************************************/
bool UserKEY_Get(void)
{
    if (GPIO_PIN_RESET == GPIO_ReadPin(GPIOC, GPIO_PIN_13)) 
    {
        DelayMs(20);
        
        if (GPIO_PIN_RESET == GPIO_ReadPin(GPIOC, GPIO_PIN_13)) 
        {
            return true;
        }
    }

    return false;
}




/******************************************************************************
* @brief:  ADC Interrupt callback.
* @param:  None.
* @return: None
******************************************************************************/
void ADC_IRQCallback(void)
{
    if(SET == ADC_GetITStatus(ADCX, ADC_IT_FLAG_AWD))
    {
        gadcWatchdogFlag = 1;
        ADC_ClearITPendingBit(ADCX, ADC_IT_FLAG_AWD);
    }	
}

/******************************************************************************
* @brief : ADC GPIO config.
* @param : None
* @return: None
******************************************************************************/
void ADC_GPIO_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
   
    __RCC_GPIOC_CLK_ENABLE();
    
    /* Configure ADC Channel_15 pin as analog input */
    GPIO_InitStruct.Pin       = GPIO_PIN_5;
    GPIO_InitStruct.Mode      = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull      = GPIO_NOPULL;
    GPIO_Init(GPIOC, &GPIO_InitStruct);   
}

/******************************************************************************
* @brief : ADC Common register config.
* @param : None
* @return: None
******************************************************************************/
void ADC_Common_Register_Config(void)
{
    ADC_CommonInitTypeDef ADC_CommonInitStructure;

    ADC_CommonInitStructure.ADC_Mode = ADC_MODE_INDEPENDENT;//双ADC模式选择
    ADC_CommonInitStructure.ADC_Prescaler = ADC_CLOCK_DIV4;//ADC_CLK分频选择
    ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAACCESSMODE_DISABLED;//双ADC下DMA功能选择
    ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TWOSAMPLINGDELAY_5CYCLES;//2个采样阶段之间的延迟
    ADC_CommonInit(&ADC_CommonInitStructure);
}

/******************************************************************************
* @brief : ADC config.
* @param : None
* @return: None
******************************************************************************/
void ADC_Config(void)
{
    ADC_InitTypeDef ADC_InitStructure;
    
    ADC_InitStructure.ADC_Resolution = ADC_RESOLUTION_12B;//分辨率
    ADC_InitStructure.ADC_ContinuousConvMode = ADC_CONT_DISABLE;//连续转换模式
    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;//外部触发边沿选择
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_EXTERNAL_TIG0;//触发模式
    ADC_InitStructure.ADC_DataAlign = ADC_DATAALIGN_RIGHT;//数据对齐
    ADC_InitStructure.ADC_NbrOfConversion = ADC_NUM;//规则通道序列长度
    ADC_Init(ADCX, &ADC_InitStructure);
    
    //注入通道设置
    ADC_InjectedChannelConfig(ADCX, ADC_CHANNEL_15, ADC_INJECTED_RANK_1, ADC_SMP_CLOCK_320);
}


/******************************************************************************
* @brief : ADC AnalogWDG config.
* @param : None
* @return: None
******************************************************************************/
void ADC_AnalogWDGConfig(void)
{
    //选择模拟看门狗注入通道
    ADC_AnalogWatchdogInjectChannelConfig(ADCX, ADC_CHANNEL_15);
    
    //设置模拟看门狗阈值
    ADC_AnalogWatchdogThresholdsConfig(ADCX, 4000, 2000);
    
    //开启模拟看门狗
    ADC_AnalogWatchdogCmd(ADCX, ADC_ANALOGWATCHDOG_JCH_SINGLE);
}


/******************************************************************************
* @brief:  Enable ADC, start conversion of injected channel with interruption.
* @param:  None.
* @return: None
******************************************************************************/
void ADC_InjectedStart_IT(void)
{  
    //开启ADC
    ADC_Cmd(ADCX, ENABLE);

    //清除模拟看门狗标志位
    ADC_ClearFlag(ADCX, ADC_IT_FLAG_AWD);
    
    //使能模拟看门狗中断
    ADC_ITConfig(ADCX, ADC_IT_FLAG_AWD, ENABLE);   

    //开始转换注入通道
    ADC_SoftwareStartInjectedConv(ADCX);	

}

/******************************************************************************
* @brief : ADC function test.
* @param : None
* @return: None
******************************************************************************/
void APP_ADC_Test(void)
{
    uint32_t i;
    
    printfS("ADC Analog WatchDog test \r\n");
    
    UserKEY_Init();
    
    ADC_GPIO_Config();
    
    ADC_Common_Register_Config();
    
    ADC_Config();
    
    ADC_AnalogWDGConfig();
    
    NVIC_ClearPendingIRQ(ADC12_IRQn);	
    NVIC_SetPriority(ADC12_IRQn, 5);
    NVIC_EnableIRQ(ADC12_IRQn);
    
    while(1)
    { 
        
        if(true == UserKEY_Get())
        {
            ADC_InjectedStart_IT();
        }
        if(gadcWatchdogFlag)
        {
            printfS("Injected Channel Level Out Of Window. \r\n");
            gadcWatchdogFlag = 0;
        } 
        if(SET == ADC_GetFlagStatus(ADCX, ADC_IT_FLAG_JEOG))
        {
            ADC_ClearFlag(ADCX, ADC_IT_FLAG_JEOG);
       
            gadcBuffer[0] = ADC_GetInjectedConversionValue(ADCX, ADC_JDR_1);
            printfS("The adc convert result : Channel %d = %d\r\n", gadcBuffer[0]>>16 & 0xFF, gadcBuffer[0] & 0xFFF);  
        }
       
    }
    
    
    
}






