AnsweredAssumed Answered

ADc via timer2 and data is sent to dma on stm32f429

Question asked by shoaib_ullah.muhamma on Mar 8, 2016
Latest reply on Mar 8, 2016 by Clive One

Hi,

I configured 8 channels of an adc at 8KHz via timer2 and data is being copied into dma,
how can i see that data and perform some operation on it?
There is a pointer where data has been copied but not able to understand how should i copy data from that location into another pointer?

 

#include "stm32f4xx_adc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include <stdio.h>
#include "ringbuffer.h"
#include "adcx_generic.h"


typedef struct
{
uint16_t data[8];
    uint16_t  idx_datadet;
    float time_ts;
}
ADC_RawValue;



void RCC_Configuration(void)
{
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 | RCC_APB2Periph_ADC3, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
 
/**************************************************************************************/
 
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
 
  /* ADC Channel  0 -> PA0 ADC123_IN0
     ADC Channel  1 -> PA1 ADC123_IN1
     ADC Channel  2 -> PA2 ADC123_IN2
     ADC Channel 10 -> PC0 ADC123_IN10
     ADC Channel 11 -> PC1 ADC123_IN11
     ADC Channel 12 -> PC2 ADC123_IN12
   */
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2| GPIO_Pin_3 | GPIO_Pin_4| GPIO_Pin_5 | GPIO_Pin_6| GPIO_Pin_7 | GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_Init(GPIOC, &GPIO_InitStructure);
}
 
/**************************************************************************************/
 
void ADC_Configuration(void)
{
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
  ADC_InitTypeDef ADC_InitStructure;
 
  /* ADC Common Init */
  ADC_CommonInitStructure.ADC_Mode = ADC_TripleMode_RegSimult;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;                 // 3 half-words one by one, 1 then 2 then 3
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInit(&ADC_CommonInitStructure);
 
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;                                                 // 2 Channels
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;                                 // Conversions Triggered
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 8;
  ADC_Init(ADC1, &ADC_InitStructure);
  ADC_Init(ADC2, &ADC_InitStructure);                             // Mirror on ADC2
  ADC_Init(ADC3, &ADC_InitStructure);                                 // Mirror on ADC3
 
  /* ADC1 regular channel 0 & 10 configuration */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_0,  1, ADC_SampleTime_15Cycles); // PA0
  ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 2, ADC_SampleTime_15Cycles); // PC0
 
  /* ADC2 regular channel 1 & 11 configuration,initially it was adc2 and for rank it was 1 ,2 */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_1,  3, ADC_SampleTime_15Cycles); // PA1
  ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 4, ADC_SampleTime_15Cycles); // PC1
 
  /* ADC3 regular channel 2 & 12 configuration, initially it was adc3 and for rank it was 1 ,2  */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_2,  5, ADC_SampleTime_15Cycles); // PA2
  ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 6, ADC_SampleTime_15Cycles); // PC2
    
     ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 7, ADC_SampleTime_15Cycles); // PA2
  ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 8, ADC_SampleTime_15Cycles); // PC2
 
  /* Enable DMA request after last transfer (Multi-ADC mode)  */
  ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);
 
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);
 
  /* Enable ADC2 */
  ADC_Cmd(ADC2, ENABLE);
 
  /* Enable ADC3 */
  ADC_Cmd(ADC3, ENABLE);
}
 
/**************************************************************************************/
 
#define BUFFERSIZE  (40 * 6 * 2) //  40KHz x6 x2 HT/TC at 1KHz

//#define BUFFERSIZE  16

uint16_t ADCTripleConvertedValues[BUFFERSIZE];                 // Filled as pairs ADC1, ADC2, ADC3
 
static void DMA_Configuration(void)
{
  DMA_InitTypeDef DMA_InitStructure;
 
  DMA_InitStructure.DMA_Channel = DMA_Channel_0;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCTripleConvertedValues[0];
//    DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCTripleConvertedValues;
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(uint32_t)&ADC1->DR; // CDR_ADDRESS; Packed ADC1, ADC2
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStructure.DMA_BufferSize = BUFFERSIZE; // Count of 16-bit words
 // DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Enable;
  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_FIFOMode = DMA_FIFOMode_Enable;
  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);
 
  /* Enable DMA Stream Half / Transfer Complete interrupt */
  DMA_ITConfig(DMA2_Stream0, DMA_IT_TC | DMA_IT_HT, ENABLE);
 
  /* DMA2_Stream0 enable */
  DMA_Cmd(DMA2_Stream0, ENABLE);
}
 
/**************************************************************************************/
 
void TIM2_Configuration(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
 
  /* Time base configuration */
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);    
  TIM_TimeBaseStructure.TIM_Period = ((SystemCoreClock / 2) / 40000) - 1;      // 40 KHz, from 84 MHz TIM2CLK (ie APB1 = HCLK/4, TIM2CLK = HCLK/2)
//    TIM_TimeBaseStructure.TIM_Period = 10500 - 1;                                                             // 8 KHz
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
 
  /* TIM2 TRGO selection */
  TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);                                     // ADC_ExternalTrigConv_T2_TRGO
 
  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);
}
 
/**************************************************************************************/
 
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
 
  /* Enable the DMA Stream IRQ Channel */
  NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
 


/**************************************************************************************/
 
void DMA2_Stream0_IRQHandler(void) // Called at 1 KHz for 8 KHz sample rate
{

    
  /* Test on DMA Stream Half Transfer interrupt */
  if(DMA_GetITStatus(DMA2_Stream0, DMA_IT_HTIF0))
  {
    /* Clear DMA Stream Half Transfer interrupt pending bit */
    DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_HTIF0);
 
    //    ADC_RawValue = (uint32_t)&ADCTripleConvertedValues[0];
        
  }
 
  /* Test on DMA Stream Transfer Complete interrupt */
  if(DMA_GetITStatus(DMA2_Stream0, DMA_IT_TCIF0))
  {
    /* Clear DMA Stream Transfer Complete interrupt pending bit */
    DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TCIF0);
 
 
  }
}
 
/**************************************************************************************/
 
int main(void)
{
  RCC_Configuration();
 
  GPIO_Configuration();
 
  NVIC_Configuration();
 
  TIM2_Configuration();
 
  DMA_Configuration();
 
  ADC_Configuration();
 
 
  /* Start ADC1 Software Conversion */
  ADC_SoftwareStartConv(ADC1);
 
  while(1); // Don't want to exit
}
 
 

Outcomes