Page 1 of 1

ADC Sampling rate configuration HELP!

Posted: Mon Nov 21, 2016 10:28 pm
by golb
hello,
I'm very new to chibiOS and programming in general and I was wondering if someone would please point out where I could change the speed of the ADC sampling rate.

I am currently trying to sample a signal with a frequency of 60Hz, however I find myself unable to get more then one value per period (if I am actually getting values from the same period in the signal)...

I am using a "ADC_SAMPLE_3" macro, which to my understanding is supposed to be the fastest sampling rate for my board (3 cycles). Considering my board should be running at 216Mhz and its ADC has a clock frequency of 36Mhz, this settings should be more than enough to sample correctly a 60 Hz signal.

Here's the parameters that I have configured.

Code: Select all

/*
 * ADC driver system settings.
 */
#define STM32_ADC_ADCPRE                    ADC_CCR_ADCPRE_DIV4
#define STM32_ADC_USE_ADC1                  TRUE
#define STM32_ADC_USE_ADC2                  TRUE
#define STM32_ADC_USE_ADC3                  TRUE
#define STM32_ADC_ADC1_DMA_STREAM           STM32_DMA_STREAM_ID(2, 4)
#define STM32_ADC_ADC2_DMA_STREAM           STM32_DMA_STREAM_ID(2, 2)
#define STM32_ADC_ADC3_DMA_STREAM           STM32_DMA_STREAM_ID(2, 1)
#define STM32_ADC_ADC1_DMA_PRIORITY         2
#define STM32_ADC_ADC2_DMA_PRIORITY         2
#define STM32_ADC_ADC3_DMA_PRIORITY         2
#define STM32_ADC_IRQ_PRIORITY              6
#define STM32_ADC_ADC1_DMA_IRQ_PRIORITY     6
#define STM32_ADC_ADC2_DMA_IRQ_PRIORITY     6
#define STM32_ADC_ADC3_DMA_IRQ_PRIORITY     6


Code: Select all

/*
 * Board oscillators-related settings.
 */
#if !defined(STM32_LSECLK)
#define STM32_LSECLK                32768U
#endif

#if !defined(STM32_HSECLK)
#define STM32_HSECLK                8000000U
#endif

#define STM32_HSE_BYPASS


Code: Select all

/**
 * @name    Absolute Maximum Ratings
 * @{
 */
/**
 * @brief   Minimum ADC clock frequency.
 */
#define STM32_ADCCLK_MIN        600000

/**
 * @brief   Maximum ADC clock frequency.
 */
#if defined(STM32F4XX) || defined(__DOXYGEN__)
#define STM32_ADCCLK_MAX        36000000
#else
#define STM32_ADCCLK_MAX        30000000
#endif
/** @} */


it's very probable I'm not even showing the appropiate section of the code, like I said im very new to this and I am having a difficult time learning where to look for the things that I need inside the project files...

please help me.

Re: ADC Sampling rate configuration HELP!

Posted: Mon Nov 21, 2016 10:37 pm
by Giovanni
Hi,

There are two ways:

1) Use a GPT timer to trigger the ADC, see the example here: \testhal\STM32\STM32F7xx\GPT-ADC. This is the best way when accuracy is needed.

2) Simply use the ADC in a loop doing single conversions and use chThdSleepMilliseconds() for timing, your rate is quite slow so you can do this.

Giovanni

Re: ADC Sampling rate configuration HELP!

Posted: Mon Dec 05, 2016 4:58 pm
by golb
Hello again

I was wondering if you could enlighten me with some information about the ADC Prescaler.

I have a stm32f746 board and its adc clock its defined in adc_lld.h like this:

Code: Select all

 * @brief   Minimum ADC clock frequency.
 */
#define STM32_ADCCLK_MIN        600000

/**
 * @brief   Maximum ADC clock frequency.
 */
#if defined(STM32F4XX) || defined(__DOXYGEN__)
#define STM32_ADCCLK_MAX        36000000
#else
#define STM32_ADCCLK_MAX        30000000
#endif


I am assuming (because I havent found the file where the ADC_clk frequency is being set and if I have I dont think I understood it very well) that the clock frequency my ADC is using is 30Mhz. That said, here are my questions:

the ADC clock divider settings are defined like this:

Code: Select all


/**
 * @name    ADC clock divider settings
 * @{
 */
#define ADC_CCR_ADCPRE_DIV2     0
#define ADC_CCR_ADCPRE_DIV4     1
#define ADC_CCR_ADCPRE_DIV6     2
#define ADC_CCR_ADCPRE_DIV8     3
/** @} */



and this is the configuration of the ADC that I am using:

Code: Select all


/*
 * ADC driver system settings.
 */
#define STM32_ADC_ADCPRE                    ADC_CCR_ADCPRE_DIV4
#define STM32_ADC_USE_ADC1                  TRUE
#define STM32_ADC_USE_ADC2                  TRUE
#define STM32_ADC_USE_ADC3                  TRUE
#define STM32_ADC_ADC1_DMA_STREAM           STM32_DMA_STREAM_ID(2, 4)
#define STM32_ADC_ADC2_DMA_STREAM           STM32_DMA_STREAM_ID(2, 2)
#define STM32_ADC_ADC3_DMA_STREAM           STM32_DMA_STREAM_ID(2, 1)
#define STM32_ADC_ADC1_DMA_PRIORITY         2
#define STM32_ADC_ADC2_DMA_PRIORITY         2
#define STM32_ADC_ADC3_DMA_PRIORITY         2
#define STM32_ADC_IRQ_PRIORITY              6
#define STM32_ADC_ADC1_DMA_IRQ_PRIORITY     6
#define STM32_ADC_ADC2_DMA_IRQ_PRIORITY     6
#define STM32_ADC_ADC3_DMA_IRQ_PRIORITY     6



by using ADC_CCR_ADCPRE_DIV4, I would be dividing 30Mhz/4 and using the result as my new clock frequency? this appears to be the default configuration, but I also see that the MACRO has a value of 1, I assume this is the amount of bits used to configure the ADC register, am I correct?

#2 if I use a higher divider I would be able to get a lower frequency, correct? lets say I set the adc to use a prescaler: ADC_CCR_ADCPRE_DIV8 I would get 30MHz/8 or 30MHZ/3?

#3 Could anyone pls indicate where exactly I could see my ADC current clock speed? so that I can know how my prescaler affects my conversion rate.

thank you so much for your help.

Re: ADC Sampling rate configuration HELP!

Posted: Mon Dec 05, 2016 6:27 pm
by Giovanni
Hi,

The ADC uses the PCLK2 clock divided by that value. The value of PCLK2 depends on settings in mcuconf.h.

Note that this kind of information have to be searched on the STM32 Reference Manual, those definitions follow strictly the bits on the ADC CCR register.

See "15.3.3 ADC clock" and "5.2 Clocks".

Giovanni