Advice on getting ADC data out

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: barthess, RoccoMarco, utzig, lbednarz, tfAteba

User avatar
Giovanni
Site Admin
Posts: 10952
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 340 times
Been thanked: 281 times
Contact:

Re: Advice on getting ADC data out

Postby Giovanni » Tue Feb 10, 2015 4:23 pm

That would be great, thank you.

Giovanni

IgorEE
Posts: 21
Joined: Fri Nov 14, 2014 4:40 pm

Re: Advice on getting ADC data out

Postby IgorEE » Wed Feb 11, 2015 7:49 pm

Just a note to anyone following this thread - I've put a post up explaining what I've been trying to accomplish in this thread. You can read it here: http://www.elektricks.net/stm32f4-disco ... on-system/

Cheers,
-Igor

NikolaG
Posts: 3
Joined: Wed Jun 28, 2017 1:46 pm
Has thanked: 2 times

Re: Advice on getting ADC data out

Postby NikolaG » Wed Jun 28, 2017 2:29 pm

Hi all,

I know this is an old thread, but my question is related to this great project that IgorEE shared with us, so I am posting here.
I used this source project, and with some modifications, I have managed to send data from 3 ADC channels at 103 kHz sample rate. The original project was in chibios v2.6, I updated it to v16.1 from chibiosStudio.
But, I have encountered problem with (I believe) ADC when moving to 175 kHz. This increase in sample rate was performed by changing ADC_SAMPLE_56 to ADC_SAMPLE_28 in ADCConversionGroup.
The problem is that, with this higher sample rate, I occasionally receive two subsequent samples with the same value, which is illustrated in figure below (10kHz sine wave), where difference (derivative) jumps to zero. This does not happen often, but it does and messes up my measurements.
adc holds on value.png

I have also attached zipped main.c file. Briefly, here I used adc callback to post messages with samples. In every second callback, I skip samples[1 and 2], so I effectively got 1chanel at 175 kHz and two others with half sample rate. These messages are received in USBCommsThread, and, after some manipulations sent to the PC with chSequentialStreamWrite().

I would be very grateful if someone could explain me why this freezing happens, and how it can be repaired.
Sorry if I said something inappropriate, I am Chibios newbie.

Kind regards,
Nikola
Attachments
main.rar
(2.84 KiB) Downloaded 18 times

User avatar
Giovanni
Site Admin
Posts: 10952
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 340 times
Been thanked: 281 times
Contact:

Re: Advice on getting ADC data out

Postby Giovanni » Wed Jun 28, 2017 4:38 pm

Hi,

It is possible that with the increased rate the system is not able to process the callbacks fast enough.

Giovanni

NikolaG
Posts: 3
Joined: Wed Jun 28, 2017 1:46 pm
Has thanked: 2 times

Re: Advice on getting ADC data out

Postby NikolaG » Wed Jun 28, 2017 6:00 pm

Hi Giovanni,
Thank you for your answer.
In adc callback routine, led pad is set/cleared on every entry/exit. I have monitored this signal with oscilloscope and, as I saw, this signal is in set state less than 10% of time for this sample rate. These pulses are not of identical width, but I never saw any kind of overlap.
I have to point out an error in my previous post. By analyzing derivative of sine wave in the above figure, it first jumps up, then drops to zero. So I think that adc duplicates sample that is somewhere in between of the true ones. Could this be related to some internal counters?
Regards,
Nikola

User avatar
Giovanni
Site Admin
Posts: 10952
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 340 times
Been thanked: 281 times
Contact:

Re: Advice on getting ADC data out

Postby Giovanni » Thu Jun 29, 2017 8:08 am

Hi,

The driver has no internal counters, the transfer is done in HW between ADC and DMA, once it started the driver does nothing except serving IRQs.

There must be a non-obvious reason.

Giovanni

NikolaG
Posts: 3
Joined: Wed Jun 28, 2017 1:46 pm
Has thanked: 2 times

Re: Advice on getting ADC data out

Postby NikolaG » Thu Jun 29, 2017 5:04 pm

Hi again,

So I tried to use gpt (GPTD2) to trigger ADC hoping that it will solve this issue.
As I saw in some other threads, something similar is given in examples STM32F7xx\GPT-ADC\.
Following this example, I tried to change settings in ADCConversionGroup by changing ADC_CR2_SWSTART to something that looks like:

Code: Select all

static const ADCConversionGroup adcgrpcfg = {
                                             TRUE, // circullar?
                                             ADC_GRP_NUM_CHANNELS,
                                             adccallback,
                                             NULL,
                                             0,                        /* CR1 */
                                             ADC_CR2_EXTSEL | ADC_CR2_EXTSEL_1, /*CR2*/
                                             ADC_SMPR1_SMP_AN10(ADC_SAMPLE_28) | ADC_SMPR1_SMP_AN11(ADC_SAMPLE_28) | //smpr1
                                             ADC_SMPR1_SMP_AN14(ADC_SAMPLE_28) ,  //smpr1
                                             0,  //smpr2
                                             ADC_SQR1_NUM_CH(ADC_GRP_NUM_CHANNELS),    /* SQR1 */
                                             0,                        /* SQR2 */
                                             ADC_SQR3_SQ1_N(ADC_CHANNEL_IN10) | ADC_SQR3_SQ2_N(ADC_CHANNEL_IN11) |
                                             ADC_SQR3_SQ3_N(ADC_CHANNEL_IN14), /* SQR3 */


but I never get into ADC callback.
I searched forum, and saw many simmilar threads, but not the one with the STM32f4.
I also looked in STM32f407 reference manual, "External trigger for regular channels", but I can not put it all together to make it work.
Can you direct me on documentation on how to set CR2 line for gpt ADC triggering?
I know this is not strictly related to chibios, but I am still hoping for some kind of assistance.

Regards,
Nikola

Ayush Kumar
Posts: 1
Joined: Mon Apr 09, 2018 7:59 am

Re: Advice on getting ADC data out

Postby Ayush Kumar » Mon Apr 30, 2018 10:39 am

I am using ADC on STM32F030 enabling 2 ADC channels in continuous mode. But I am getting any output. I per my knowledge there is some configuration issue. Can anyone help me out. thanks in advance.

// Lets configure our ADC first

// ADCConfig structure for stm32 MCUs is empty
static ADCConfig adccfg = {};

// Create buffer to store ADC results. This is
// one-dimensional interleaved array
#define ADC_BUF_DEPTH 16 // depth of buffer
#define ADC_CH_NUM 2 // number of used ADC channels
static adcsample_t samples_buf[ADC_BUF_DEPTH * ADC_CH_NUM]; // results array

// Fill ADCConversionGroup structure fields
static ADCConversionGroup adccg = {
// this 3 fields are common for all MCUs
// set to TRUE if need circular buffer, set FALSE otherwise
TRUE,
// number of channels
(uint16_t)(ADC_CH_NUM),
// callback function, set to NULL for begin
adccb,
NULL,
// Resent fields are stm32 specific. They contain ADC control registers data.
// Please, refer to ST manual RM0008.pdf to understand what we do.
// CR1 register content, set to zero for begin
0x00002000 //Continuous Mode
// Tr register content, set to zero for begin
0,
// SMRP1 register content, set to zero for begin
ADC_SMPR_SMP_239P5,
//no of channels
0x00000008|0x00000004 // channel_2 & channel_3

};

// Thats all with configuration


uint16_t adc_ch2 = 0, adc_ch3 = 0;

void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n) {

(void) buffer; (void) n;
/* Note, only in the ADC_COMPLETE state because the ADC driver fires an
intermediate callback when the buffer is half full.*/
if (adcp->state == ADC_COMPLETE) {

/* Calculates the average values from the ADC samples.*/
adc_ch2 = samples_buf[0];
adc_ch3 = samples_buf[1];
}
}

int main(int argc, char **argv) {
(void)argc;
(void)argv;

halInit();
chSysInit();

// Setup pins of our MCU as analog inputs
palSetPadMode(GPIOA, 2, PAL_MODE_INPUT_ANALOG); // this is 2th channel
palSetPadMode(GPIOA, 3, PAL_MODE_INPUT_ANALOG); // this is 3th channel

// Following 3 functions use previously created configuration
// to initialize ADC block, start ADC block and start conversion.
// &ADCD1 is pointer to ADC driver structure, defined in the depths of HAL.
// Other arguments defined ourself earlier.

adcStart(&ADCD1, NULL);
adcStartConversion(&ADCD1, &adccg, samples_buf, ADC_BUF_DEPTH);
// Thats all. Now your conversion run in background without assistance.
while (TRUE) {
chThdSleep(100);
}
return 0;
}

User avatar
Giovanni
Site Admin
Posts: 10952
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 340 times
Been thanked: 281 times
Contact:

Re: Advice on getting ADC data out

Postby Giovanni » Mon Apr 30, 2018 11:03 am

Hi,

Is the callback triggered? you may try putting a breakpoint there.

Giovanni


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 1 guest