AnsweredAssumed Answered

STM32F1 ADC3 Issue

Question asked by mcg.pro on Feb 15, 2016
Latest reply on Feb 17, 2016 by mcg.pro

Hi,
I am having problems Reading from ADC3. I have ADC1 and ADC3 set up on separate DMA channels. I have a separate task to fill a single array with both ADCs, however I can't get anything from ADC3, in fact declaring ADC_Configuration{} will cause the program to crash. I have posted the config below, I believe the problem lies here. If it helps I can post the interrupt functions and array task too if that helps

I am using an STM32F103ZE micro. 

u16 ADC1_Channels[MAX_ADC1_CHANNELS];
u16 ADC3_Channels[MAX_ADC3_CHANNELS];
u16 ADC_Channels[MAX_ADC_CHANNELS];
 
void gpioADC1(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
 
    /* Configure PA.xx (ADC Channel1) as analog inputs -------------------------*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 |GPIO_Pin_3 | GPIO_Pin_6 |GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init(GPIOA, &GPIO_InitStructure); 
     
  /* Configure PB.xx (ADC Channel1) as analog inputs -------------------------*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init(GPIOB, &GPIO_InitStructure); 

}
 
void gpioADC3(void)
{
     
  GPIO_InitTypeDef GPIO_InitStructure;
 
/* Configure PF.xx (ADC Channel3) as analog inputs -------------------------*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init(GPIOF, &GPIO_InitStructure); 
}
void ADC1_Configuration(void)
{
    ADC_InitTypeDef ADC_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
     
    RCC_ADCCLKConfig(RCC_PCLK2_Div6);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_ADC1, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
     
    /* GPIO configuration ------------------------------------------------------*/
    gpioADC1();
 
    NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                                           
    NVIC_Init(&NVIC_InitStructure);
 
    ADC_DeInit(ADC1);
    DMA_DeInit(DMA1_Channel1);
 
    // DMA1 channel1 configuration ----------------------------------------------
    DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
    DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC1_Channels;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    DMA_InitStructure.DMA_BufferSize = MAX_ADC1_CHANNELS;
 
    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_Circular;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    DMA_Init(DMA1_Channel1, &DMA_InitStructure);
    DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
      /* Enable DMA1 channel1 */
  DMA_Cmd(DMA1_Channel1, ENABLE);
 
     
    // ADC1 configuration ------------------------------------------------------
    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
    ADC_InitStructure.ADC_ScanConvMode = ENABLE;
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfChannel = MAX_ADC1_CHANNELS;
    ADC_Init(ADC1, &ADC_InitStructure);
        // Enable ADC1 DMA
    ADC_DMACmd(ADC1, ENABLE);
     
        ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1,  ADC_SampleTime_71Cycles5);   //Sensor 4  PC1
        ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 2,  ADC_SampleTime_71Cycles5);       //Sensor 5  PF8
        ADC_RegularChannelConfig(ADC1, ADC_Channel_2,  3,  ADC_SampleTime_71Cycles5);       //Sensor 6  PA2
        ADC_RegularChannelConfig(ADC1, ADC_Channel_6,  4,  ADC_SampleTime_71Cycles5);       //Sensor 7  PA6
        ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 5,  ADC_SampleTime_71Cycles5);       //Sensor 8  PC4
        ADC_RegularChannelConfig(ADC1, ADC_Channel_8,  6,  ADC_SampleTime_71Cycles5);       //Sensor 9  PB0
        ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 7,  ADC_SampleTime_71Cycles5);       //Sensor 12 PC0
        ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 8,  ADC_SampleTime_71Cycles5);       //Sensor 13 PC2
        ADC_RegularChannelConfig(ADC1, ADC_Channel_0,  9,  ADC_SampleTime_71Cycles5);       //Sensor 14 PA0
        ADC_RegularChannelConfig(ADC1, ADC_Channel_3,  10,  ADC_SampleTime_71Cycles5);  //Sensor 15 PA3
        ADC_RegularChannelConfig(ADC1, ADC_Channel_7,  11,  ADC_SampleTime_71Cycles5);  //Sensor 17 -ve 1   PA7
        ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 12,  ADC_SampleTime_71Cycles5);  //Sensor 18 -ve 2   PC5
        ADC_RegularChannelConfig(ADC1, ADC_Channel_9,  13,  ADC_SampleTime_71Cycles5);  //Sensor 19 -ve 3   PB1
        ADC_RegularChannelConfig(ADC1, ADC_Channel_1,  14,  ADC_SampleTime_71Cycles5);  // ADC PA1
         
    //////////////////////////////////////////////////////////////////////////////////////
     
    // Enable ADC1
    ADC_Cmd(ADC1, ENABLE);
     
    // Enable ADC1 reset calibaration register
    ADC_ResetCalibration(ADC1);
     
    // Check the end of ADC1 reset calibration register
    while(ADC_GetResetCalibrationStatus(ADC1));
     
    // Start ADC1 calibaration
    ADC_StartCalibration(ADC1);
     
    // Check the end of ADC1 calibration
    while(ADC_GetCalibrationStatus(ADC1));
}
 
void ADC3_Configuration(void)
{
    ADC_InitTypeDef ADC_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
     
     
    //RCC for ADC3 and ADC3 ports
    RCC_ADCCLKConfig(RCC_PCLK2_Div6);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF | RCC_APB2Periph_ADC3, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
     
    /* GPIO configuration ------------------------------------------------------*/
    gpioADC3();
     
    NVIC_InitStructure.NVIC_IRQChannel = DMA2_Channel4_5_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                                           
    NVIC_Init(&NVIC_InitStructure);
     
    ADC_DeInit(ADC3);
    DMA_DeInit(DMA2_Channel5);
     
        // DMA2 channel5 configuration ----------------------------------------------
    DMA_InitStructure.DMA_PeripheralBaseAddr = ADC3_DR_Address;
    DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC3_Channels;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    DMA_InitStructure.DMA_BufferSize = MAX_ADC3_CHANNELS;
 
    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_Circular;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    DMA_Init(DMA2_Channel5, &DMA_InitStructure);
    DMA_ITConfig(DMA2_Channel5, DMA_IT_TC, ENABLE);
      /* Enable DMA2 channel5 */
  DMA_Cmd(DMA2_Channel5, ENABLE);
     
    // ADC3 configuration ------------------------------------------------------
    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
    ADC_InitStructure.ADC_ScanConvMode = ENABLE;
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfChannel = MAX_ADC3_CHANNELS;
    ADC_Init(ADC3, &ADC_InitStructure);
    // Enable ADC3 DMA
    ADC_DMACmd(ADC3, ENABLE);
     
 
    ADC_RegularChannelConfig(ADC3, ADC_Channel_4,  15,  ADC_SampleTime_71Cycles5); //Sensor 1       PF6
    ADC_RegularChannelConfig(ADC3, ADC_Channel_5,  16,  ADC_SampleTime_71Cycles5); //Sensor 10  PF7
    ADC_RegularChannelConfig(ADC3, ADC_Channel_6,  17,  ADC_SampleTime_71Cycles5); //Sensor 2       PF8
    ADC_RegularChannelConfig(ADC3, ADC_Channel_7,  18,  ADC_SampleTime_71Cycles5); //Sensor 11  PF9
    ADC_RegularChannelConfig(ADC3, ADC_Channel_8,  19,  ADC_SampleTime_71Cycles5); //Sensor 3       PF10
 
    //////////////////////////////////////////////////////////////////////////////////////
     
        // Enable ADC3
    ADC_Cmd(ADC3, ENABLE);
         
    // Enable ADC1 reset calibaration register
    ADC_ResetCalibration(ADC3);
     
    // Check the end of ADC1 reset calibration register
    while(ADC_GetResetCalibrationStatus(ADC3));
     
    // Start ADC1 calibaration
    ADC_StartCalibration(ADC3);
     
    // Check the end of ADC1 calibration
    while(ADC_GetCalibrationStatus(ADC3));
}

Outcomes