AnsweredAssumed Answered

stm32 adc multi channel

Question asked by j0hn on Jun 8, 2013
Latest reply on Jun 14, 2013 by fm
thi is my code .when i debug it i faced  a error .i dont know how to fix it .plz guide me .the error is :-GCC HOME: C:\Program Files (x86)\GNU Tools ARM Embedded\4.7 2013q1\bin
compile:
    [mkdir] Created dir: C:\CooCox\CoIDE\workspace\rrr\rrr\Debug\bin
    [mkdir] Created dir: C:\CooCox\CoIDE\workspace\rrr\rrr\Debug\obj
       [cc] 9 total files to be compiled.
       [cc] arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -Wall -ffunction-sections -g -O0 -flto -fno-builtin -c -DSTM32F407VG -DSTM32F4XX -DUSE_STDPERIPH_DRIVER -D__ASSEMBLY__ -IC:\CooCox\CoIDE\workspace\rrr\cmsis -IC:\CooCox\CoIDE\workspace -IC:\CooCox\CoIDE\workspace\rrr\cmsis_lib -IC:\CooCox\CoIDE\workspace\rrr -IC:\CooCox\CoIDE\workspace\rrr\cmsis_lib\include -IC:\CooCox\CoIDE\workspace\rrr\cmsis_boot C:\CooCox\CoIDE\workspace\rrr\cmsis_lib\source\stm32f4xx_pwr.c C:\CooCox\CoIDE\workspace\rrr\cmsis_boot\startup\startup_stm32f4xx.c C:\CooCox\CoIDE\workspace\rrr\main.c C:\CooCox\CoIDE\workspace\rrr\cmsis_lib\source\stm32f4xx_rcc.c C:\CooCox\CoIDE\workspace\rrr\cmsis_lib\source\stm32f4xx_adc.c C:\CooCox\CoIDE\workspace\rrr\cmsis_lib\source\stm32f4xx_gpio.c C:\CooCox\CoIDE\workspace\rrr\cmsis_boot\system_stm32f4xx.c C:\CooCox\CoIDE\workspace\rrr\cmsis_lib\source\stm32f4xx_dma.c C:\CooCox\CoIDE\workspace\rrr\cmsis_lib\source\stm32f4xx_tim.c
       [cc] C:\CooCox\CoIDE\workspace\rrr\main.c: In function 'main':
       [cc] C:\CooCox\CoIDE\workspace\rrr\main.c:61:1: warning: control reaches end of non-void function [-Wreturn-type]
       [cc] Starting link
       [cc] arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -g -nostartfiles -flto -Wl,-Map=rrr.map -O0 -Wl,--gc-sections -Wl,--entry=main -LC:\CooCox\CoIDE\configuration\ProgramData\rrr -Wl,-TC:\CooCox\CoIDE\configuration\ProgramData\rrr/arm-gcc-link.ld -g -o rrr.elf ..\obj\stm32f4xx_pwr.o ..\obj\startup_stm32f4xx.o ..\obj\main.o ..\obj\stm32f4xx_rcc.o ..\obj\stm32f4xx_adc.o ..\obj\stm32f4xx_gpio.o ..\obj\system_stm32f4xx.o ..\obj\stm32f4xx_dma.o ..\obj\stm32f4xx_tim.o
       [cc] C:\Users\AKG\AppData\Local\Temp\cciWMwmp.s: Assembler messages:
       [cc] C:\Users\AKG\AppData\Local\Temp\cciWMwmp.s:180: Error: offset out of range
       [cc] C:\Users\AKG\AppData\Local\Temp\cciWMwmp.s:181: Error: offset out of range
       [cc] c:/program files (x86)/gnu tools arm embedded/4.7 2013q1/bin/../lib/gcc/arm-none-eabi/4.7.3/../../../../arm-none-eabi/bin/ld.exe: lto-wrapper failed
       [cc] lto-wrapper: C:\Program Files (x86)\GNU Tools ARM Embedded\4.7 2013q1\bin\arm-none-eabi-gcc returned 1 exit status
       [cc] collect2.exe: error: ld returned 1 exit status


#include "stm32f4xx_adc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_dma.h"


#define ADC_CDR_ADDRESS    ((uint32_t)0x40012308)
__IO uint32_t ADCValue[9];

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

uint16_t PWM_PrescalerValue = 0;
uint16_t PWM_Period = 4095;

uint16_t PWM_CCR1 = 0;
uint16_t PWM_CCR2 = 0;
uint16_t PWM_CCR3 = 0;
uint16_t PWM_CCR4 = 0;
uint16_t PWM_CCR5 = 0;
uint16_t PWM_CCR6 = 0;

void  USBConfig();
void  DMA_ADC_Config();
void  ADC__SoftwareStartConv();
void  ADC_Config();
void  PWM_Config();
void  TIM8_Config();
void TIM8_IRQHandler();



int main(void)
{
  RCC_ClocksTypeDef RCC_Clocks;

 /* SysTick end of count event each 10ms */
  RCC_GetClocksFreq(&RCC_Clocks);
 // SysTick_Config(RCC_Clocks.
HCLK_Frequency / 100);

 /* USB configuration */
 // USBConfig();


  //Configuration of the PWM (Timers) channels used
  PWM_Config();

}

void ADC_Config(void)
{
      GPIO_InitTypeDef      GPIO_AdcInit;
     ADC_InitTypeDef       ADC_InitStructure;
     ADC_CommonInitTypeDef ADC_CommonInitStructure;

      /* Deinitializes the ADC peripheral registers */
      ADC_DeInit();

     /* Enable the GPIOC Clock & ADC1 Periph Clock */
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE);
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 | RCC_APB2Periph_ADC3, ENABLE);

      /* Configure the  PA0 pin (ADC123_IN0)
       *                PA1 pin (ADC123_IN1)
       *                PA2 pin (ADC123_IN2)
      *                PA3 pin (ADC123_IN3)
     *                PA4 pin (ADC12_IN4) */
   GPIO_AdcInit.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
      GPIO_AdcInit.GPIO_Mode = GPIO_Mode_AN;
      GPIO_AdcInit.GPIO_PuPd = GPIO_PuPd_NOPULL;
      GPIO_Init(GPIOA, &GPIO_AdcInit);

      /* Configure the  PB0 pin (ADC12_IN8)
       *                PB1 pin (ADC12_IN9) */
      GPIO_AdcInit.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
      GPIO_AdcInit.GPIO_Mode = GPIO_Mode_AN;
      GPIO_AdcInit.GPIO_PuPd = GPIO_PuPd_NOPULL;
      GPIO_Init(GPIOB, &GPIO_AdcInit);

      /* Configure the  PC0 pin (ADC123_IN10)
       *                PC2 pin (ADC123_IN12) */
     GPIO_AdcInit.GPIO_Pin = GPIO_Pin_0  |  GPIO_Pin_2;
      GPIO_AdcInit.GPIO_Mode = GPIO_Mode_AN;
      GPIO_AdcInit.GPIO_PuPd = GPIO_PuPd_NOPULL;
      GPIO_Init(GPIOC, &GPIO_AdcInit);

      /* ADC Common configuration -- ADC_CCR Register */
     ADC_CommonInitStructure.ADC_Mode = ADC_TripleMode_RegSimult ;
      ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_10Cycles;
      ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1  ;
      ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
      ADC_CommonInit(&ADC_CommonInitStructure);


     /* ADC1 regular channel 6 configuration -- ADC_CR1, ADC_CR2, ADC_SQR1 Register */
     ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
      ADC_InitStructure.ADC_ScanConvMode = ENABLE;
      ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
      ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
     ADC_InitStructure.ADC_ExternalTrigConv = 0;
     ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
      ADC_InitStructure.ADC_NbrOfConversion = 3;
      ADC_Init(ADC1, &ADC_InitStructure);
     ADC_Init(ADC2, &ADC_InitStructure);
     ADC_Init(ADC3, &ADC_InitStructure);

      /* ADC1 regular channel6 configuration -- ADCx->SMPR1,SMPR2 et ADCx->SQR1,SQR2,SQR3
       *              channel8 */
      ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_15Cycles );
      ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 2, ADC_SampleTime_15Cycles );
      ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 3, ADC_SampleTime_15Cycles );

    /* ADC2 regular channel12 configuration -- ADCx->SMPR1,SMPR2 et ADCx->SQR1,SQR2,SQR3  */
      ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 1, ADC_SampleTime_15Cycles );
     ADC_RegularChannelConfig(ADC2, ADC_Channel_4, 2, ADC_SampleTime_15Cycles );
      ADC_RegularChannelConfig(ADC2, ADC_Channel_1, 3, ADC_SampleTime_15Cycles );

      /* ADC3 regular channel12 configuration -- ADCx->SMPR1,SMPR2 et ADCx->SQR1,SQR2,SQR3  */
      ADC_RegularChannelConfig(ADC3, ADC_Channel_0, 1, ADC_SampleTime_15Cycles );
      ADC_RegularChannelConfig(ADC3, ADC_Channel_3, 2, ADC_SampleTime_15Cycles );
      ADC_RegularChannelConfig(ADC3, ADC_Channel_10, 3, ADC_SampleTime_15Cycles );

      /* DMA2 Stream0 channel0 configuration */
     DMA_ADC_Config();

      /* Enable DMA request after last transfer (Multi-ADC mode)  */
      ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);

      /* Enable ADC1 -- ADC_CR2_ADON */
      ADC_Cmd(ADC1, ENABLE);

      /* Enable ADC2 */
      ADC_Cmd(ADC2, ENABLE);

      /* Enable ADC3 */
     ADC_Cmd(ADC3, ENABLE);

      /* Enable ADC1 DMA since ADC1 is the Master*/
      ADC_DMACmd(ADC1, ENABLE);
}




void DMA_ADC_Config(void)
{
  DMA_InitTypeDef DMA_InitStructure;
  DMA_InitStructure.DMA_Channel = DMA_Channel_0;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCValue;
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC_CDR_ADDRESS;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStructure.DMA_BufferSize = 9;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
 DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
 DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(DMA2_Stream0, & DMA_InitStructure);

  /* DMA2_Stream0 enable */
  DMA_Cmd(DMA2_Stream0, ENABLE);
}


void PWM_Config(void)
{
 /* TIM Configuration */
  TIM8_Config();

 /* Time base configuration */
 TIM_TimeBaseStructure.TIM_Period = PWM_Period;
  TIM_TimeBaseStructure.TIM_Prescaler = PWM_PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = PWM_CCR1;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC1Init(TIM8, &TIM_OCInitStructure);

  TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel2 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = PWM_CCR2;

  TIM_OC2Init(TIM8, &TIM_OCInitStructure);
  TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = PWM_CCR3;

  TIM_OC3Init(TIM8, &TIM_OCInitStructure);
  TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel4 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = PWM_CCR4;

  TIM_OC4Init(TIM8, &TIM_OCInitStructure);
  TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable);

  /* Enable TIM8 Preload register on ARR */
  TIM_ARRPreloadConfig(TIM8, ENABLE);

  /* TIM8 enable counter */
 TIM_Cmd(TIM8, ENABLE);

  /* TIM8 Main Output Enable */
 TIM_CtrlPWMOutputs(TIM8, ENABLE);

  // TIM IT enable
  TIM_ITConfig(TIM8, TIM_IT_Update, ENABLE);

}


void TIM8_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

 /* TIM8 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
  /* GPIOC clocks enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

 /* Connect TIM8 pins to AF2 */
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8);
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM8);
 GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM8);
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM8);
}

void TIM8_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM8, TIM_IT_Update) != RESET)
  {
   TIM_ClearITPendingBit(TIM8, TIM_IT_Update);

    TIM8->CCR1 = ADCValue[0]; // PWM output 1
   TIM8->CCR2 = ADCValue[1]; // PWM output 2
    TIM8->CCR3 = ADCValue[2]; // PWM output 3
   TIM8->CCR4 = ADCValue[3]; // PWM output 4
  }
}


  //Configuration of DMA for the ADC channels
  DMA_ADC_Config();

 //Configuration of the ADC channels used
  ADC_Config();

  /* Start ADC1 Software Conversion */
 // ADC__SoftwareStartConv(ADC1);

Outcomes