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

#include "app.h" 

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

#define ADCX    (ADC2)

/******************************************************************************
* @brief:  ADC Interrupt callback.
* @param:  None.
* @return: None
******************************************************************************/
void ADC_IRQCallback(void)
{
    if(SET == ADC_GetITStatus(ADCX, ADC_IT_FLAG_JEOC))
    {
        gadcInjectConFlag = 1;
        gadcBuffer[0] = ADC_GetInjectedConversionValue(ADCX, ADC_JDR_1);
        ADC_ClearITPendingBit(ADCX, ADC_IT_FLAG_JEOC);
    }  
}

/******************************************************************************
* @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_InjectedSequencerLengthConfig(ADCX, 1);//注入通道序列长度
    ADC_InjectedChannelConfig(ADCX, ADC_CHANNEL_15, ADC_INJECTED_RANK_1, ADC_SMP_CLOCK_320);

}


/******************************************************************************
* @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_JEOC);
    
    //使能注入通道转换结束中断
    ADC_ITConfig(ADCX, ADC_IT_FLAG_JEOC, ENABLE);   

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

}

/******************************************************************************
* @brief : ADC function test.
* @param : None
* @return: None
******************************************************************************/
void APP_ADC_Test(void)
{
    uint32_t i;
    
    printfS("ADC Interrupt test \r\n");
    
    ADC_GPIO_Config();
    
    ADC_Common_Register_Config();
    
    ADC_Config();
    
    NVIC_ClearPendingIRQ(ADC12_IRQn);	
    NVIC_SetPriority(ADC12_IRQn, 5);
    NVIC_EnableIRQ(ADC12_IRQn);
    
    while(1)
    {
        if(gadcInjectConFlag)
        {
            gadcInjectConFlag = 0;   
            printfS("The adc convert result : Channel %d = %d\r\n", gadcBuffer[0]>>16 & 0xFF, gadcBuffer[0] & 0xFFF);        

        }
        else 
        {
            printfS("Wait for ADC Irq \r\n");
            ADC_InjectedStart_IT();
        }
        DelayMs(1000);     
    }
    
    
    
}






