AnsweredAssumed Answered

STM32F4: ADC1 + EXTI11 binding problem

Question asked by Keaven on Dec 5, 2014
Latest reply on Dec 8, 2014 by Keaven
Dear all,

I need to use the external trigger with ADC1 but I am not able to do it.  I have done my code in step.  I did make work my ADC1 with TIM2 as internal triggered.  That worked well.  Then I have I have generated a PWM on port PB7 with timer 4 , that worked well.  I used the pin PC11 as input and use the PWM to trigger the interrupt routine correctly.  Now , I need to use that trigger as input trigger for my ADC1 but I cannot can get it work properly.  I must missed a configuration step to link the two together.   I put my code below to see if you can pointed it out.

Note that I am using also the DMA to acquire 2048 converted values.  (that is working well with the internal timer trigger)

Thanks in advance to all.

void DoADCConversion (void)
{
    //ADC Conversion
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef   NVIC_InitStructure;
      uint32_t PeriodCounter = (90000000 / 1500000); 1.5 MHz based on 90MHz
 
    //Clock Enable
    //Ouput IO for timing PA & PG
    //Analog Input and DMA2
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB |
                           RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_DMA2 |
                           RCC_AHB1Periph_GPIOG, ENABLE);
    //ADC1
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
 
    //TIMER4
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
 
    /* Configure PG6 in output pushpull mode */
    /* Global Acquisition Time */
    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOG, &GPIO_InitStructure);
 
    /* Conversion Time */
    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
 
   //Configure ADC3 Channel13 pin as analog input
    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
 
    //PC11 for external trigger
    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource11);
 
    //EXTI Configuration Line 11
    EXTI_DeInit();
    EXTI_StructInit(&EXTI_InitStructure);
    EXTI_InitStructure.EXTI_Line = EXTI_Line11;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);
 
    //PWM
    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
 
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
 
    //Time base configuration
    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.TIM_Period = (PeriodCounter - 1);
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
 
    // PWM1 Mode configuration: Timer 4 Channel 2
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = ((PeriodCounter / 2) - 1);
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC2Init(TIM4, &TIM_OCInitStructure);
 
    TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
 
    // TIM2 enable counter
    TIM_Cmd(TIM4, ENABLE);
 
    // DMA2 Stream0 channel2 configuration
    DMA_DeInit(DMA2_Stream0); //Reset DMA to default values
    DMA_InitStructure.DMA_Channel = DMA_Channel_0;
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS;
    DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCxConvertedValue[0];
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
    DMA_InitStructure.DMA_BufferSize = 2140;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
    DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
    DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    DMA_Init(DMA2_Stream0, &DMA_InitStructure);
    DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);
 
    DMA_Cmd(DMA2_Stream0, ENABLE);
 
    //ADC Common Init
    ADC_CommunStructure.ADC_Prescaler = ADC_Prescaler_Div2;
    ADC_CommunStructure.ADC_Mode = ADC_Mode_Independent;
    //DMA Mode for multi ADC
    ADC_CommunStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
    //Delay in between two sampling
    ADC_CommunStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
    ADC_CommonInit(&ADC_CommunStructure);
 
    //Init ADC1
    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
    //Group channel scan
    ADC_InitStructure.ADC_ScanConvMode = DISABLE;
    //Single or Continuous
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_RisingFalling;
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_Ext_IT11;;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    //Number of conversion in group
    ADC_InitStructure.ADC_NbrOfConversion = 1;
    ADC_Init(ADC1, &ADC_InitStructure);
 
    // Enable ADC1 DMA
    ADC_DMACmd(ADC1, ENABLE);
 
    // ADC1 regular channel3 configuration
    ADC_RegularChannelConfig(ADC1, ADCx_CHANNEL, 1, ADC_SampleTime_3Cycles);
 
    // Enable DMA request after last transfer (Single-ADC mode)
    ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
 
    ADC_Cmd(ADC1, ENABLE);
 
    GPIOG->BSRRL = GPIO_Pin_6;
    ADC_SoftwareStartConv(ADC1);
 
    //Wait for end of transfer
    while(DMA_GetFlagStatus(DMA2_Stream0, DMA_FLAG_TCIF0) == RESET);
 
    GPIOG->BSRRH = GPIO_Pin_6;
 
    DMA_ClearFlag(DMA2_Stream0, DMA_FLAG_TCIF0);
 
    return;
}

Outcomes