Page 1 of 1

STM32F103C8T6 DMA ADC not working

Posted: Thu Apr 09, 2020 9:14 am
by nikjayswal
i am using stm32f103c8t6. i am working on osciloscope project the input frequency is 2KHz Square wave. the problem i getting is not showing proper square wave on arduino Serial plotter but when i change 2KHz into 99Khz the Serial Plotter showing Square Wave. is Arduino Code is Wrong ?


Code: Select all


#include <STM32ADC.h>
#include <HardwareTimer.h>
STM32ADC myADC(ADC1); 

uint8 pin = PA7; 

volatile static bool dma1_ch1_Active; //flag for interrupt

float usPeriod,timer_freq,ary[320];
uint16_t timPeriod_c;

#define maxSamples 1000
uint16_t buffer[maxSamples];

void setup() {
  Serial.begin(115200); 
  pinMode(pin, INPUT_ANALOG);//AD pin.
  usPeriod = 250;
  timer_freq = 1000000/usPeriod;
  timPeriod_c = 72000000/timer_freq;
  
}



void loop() {  
      // Take our samples
      DMA();
      if(dma1_ch1_Active == 0){
        for(int x=0; x<maxSamples; x++){
          Serial.println(buffer[x]);
        }
      }
}

void DMA(){
  
        Timer3.setPrescaleFactor(1);
        Timer3.setOverflow(timPeriod_c);
        
//  timer_set_period(Timer3, usPeriod);
  Timer3.setMasterModeTrGo(TIMER_CR2_MMS_UPDATE);
//  myADC.calibrate();
  myADC.setSampleRate(ADC_SMPR_1_5); //default - myADC.setSampleRate(ADC_SMPR_1_5); 
  myADC.setPins(&pin, 1);
  dma1_ch1_Active = 1;
  myADC.setTrigger(ADC_EXT_EV_TIM3_TRGO);
  myADC.setDMA(buffer, maxSamples, (DMA_MINC_MODE | DMA_TRNS_CMPLT), DMA1_CH1_Event);  
//  myADC.setDMA(buffer, maxSamples, (DMA_MINC_MODE | DMA_TRNS_CMPLT), DmaIRQ);
    while (dma1_ch1_Active >= 1){}; //wait for DMA to complete.  
//  dma1_ch1_Active = 0;
//  myADC.startConversion();
//  myADC.setTrigger(ADC_EXT_EV_SWSTART);
    dma_disable(DMA1, DMA_CH1); //End of trasfer, disable DMA and Continuous mode.
        
  }

 
static void DMA1_CH1_Event() {
  dma1_ch1_Active = 0;
}

Re: STM32F103C8T6 DMA ADC not working

Posted: Thu Apr 09, 2020 6:25 pm
by stevestrong
Please post a simple test sketch without any tft functions or other libs, which can be tested by any others.
Please use the code tag for posting code (see the 5th icon from left when editing the post).

How do you generate and measure the input frequency?

Re: STM32F103C8T6 DMA ADC not working

Posted: Thu Apr 09, 2020 7:13 pm
by nikjayswal
Edited and removed tft functions. The square wave frequency generated from another stm32f103 MCU and frequency measured with Digital Frequency Meter.

Re: STM32F103C8T6 DMA ADC not working

Posted: Thu Apr 09, 2020 7:41 pm
by stevestrong
Have you ever heard about aliasing? And Nyquist theorem?
If not, I strongly advise to get information about it.

Short answer: the sampling frequency should be at least double of the minimum sampled frequency.
In practice, factor 10 is actually recommended.
With 4 kHz sampling freq you cannot get useful data for input signals above 400Hz.
Everything you measure now makes no sense.
Increase the sampling frequency to at least 100kHz, even better 1MHz.
Of course, your data buffer should be then increased respectively.
Then you will see better results.

Re: STM32F103C8T6 DMA ADC not working

Posted: Sun Apr 12, 2020 12:23 pm
by nikjayswal
as you said to increase Sampling frequency factor of 10. i had increased the frequency and it's working good but when i had increased the input frequency above 10KHz it's not working properly i also tried
Timer3.setPrescaleFactor(1);
Timer3.setOverflow(1);
but same problem. as other tool as in the Link it has same processor but it is working above 50Khz
https://jyetech.com/dso-138-oscilloscope-diy-kit/

video : https://www.youtube.com/watch?v=x19kwG-wJRI

Re: STM32F103C8T6 DMA ADC not working

Posted: Sun Apr 12, 2020 4:13 pm
by stevestrong
You cannot use any frequency to drive the ADC, because it has its internal limitations.
So even if you drive the trigger to 18MHz, it will still not work.
As homework: calculate the maximum acquisition rate of the ADC.

You can find some more info here: https://web.archive.org/web/20190316214 ... =19&t=1847

Re: STM32F103C8T6 DMA ADC not working

Posted: Sun Apr 12, 2020 7:30 pm
by ag123
if you are using stm32f103, and a timer, the max sample rates that i can get using the timer to drive the adc is about 500 khz (ksps)
https://github.com/ag88/GirinoSTM32F103duino