ADC works for buffer =1, but not more.

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

ADC works for buffer =1, but not more.

Post by rew »

As a side note: Lots of "simple" applications need to "occasionally" read an analog value. The "fancy" features of the ADC with DMA autotriggering etc etc. is all not necessary, just a "what is the voltage on the pin now" function would suffice.

I've written such a function (or copied it from somewhere...)

Code: Select all

// This is STM32F0xx specific!
int read_adc_naiive(uint8_t channel)
{
  int i, t;
#define NN 1 // NN != 1 doesn't work (yet?)
  adcsample_t samples[NN];
  ADCConversionGroup grpp;

  grpp.circular = FALSE;
  grpp.num_channels = 1;
  grpp.end_cb = NULL;
  grpp.error_cb = adcerrorcallback;
  grpp.cfgr1 =  ADC_CFGR1_RES_12BIT;
  grpp.tr = ADC_TR(0, 0);
  grpp.smpr = ADC_SMPR_SMP_28P5;
  grpp.chselr = 1 << channel;

  adcSTM32SetCCR( ADC_CCR_TSEN | ADC_CCR_VREFEN);

  adcConvert (&ADCD1, &grpp, samples, NN);

  t = 0;
  for (i=0;i<NN;i++)
    t += samples[i];

  return t;
}

Why does this work for NN=1, but not for other values? What am I doing wrong? Do I need to set up a timer to trigger the ADC conversions? (As you can see from the comment: I'm currently using an STM32F030... The '072s and the new PCBs should arrive tomorrow)

P.S. In main I have a simple:

Code: Select all

  adcStart(&ADCD1, NULL);

PPS: static initialization of most of grpp would be more efficient, but this way I now have easy control over all the fields.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: ADC works for buffer =1, but not more.

Post by Giovanni »

Hi,

Probably this:

Code: Select all

  grpp.num_channels = 1;

Giovanni
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

Re: ADC works for buffer =1, but not more.

Post by rew »

The "num channels" is the number of different channels, right?

If I wanted to sample 12 each of, AIN2 and AIN5 (i.e. twenty four total ADC conversions), I'd set num channels to TWO, the last argument of adcConvert to 12, and chselr to (1 << 2) | ( 1 << 5), right?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: ADC works for buffer =1, but not more.

Post by Giovanni »

num_channels is the number of channels in conversion group, the parameter of adcConvert() is the number of times the channel group must be converted.

Giovanni
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

Re: ADC works for buffer =1, but not more.

Post by rew »

Yes, so for my "simple" conversion, I want a conversion group with just one channel, but convert that single channel multiple times. So I pass the "NN" to the adcConvert routine.

As I understand things, this routine will simply software-trigger the ADC. But is there an arrangement for the "second" trigger after the first conversion? (IIRC the hardware will run a conversion group if you software tirgger it, and then wait for a second "trigger" before it does the second run of the conversion group. In this case that should be a new software trigger).
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: ADC works for buffer =1, but not more.

Post by Giovanni »

The driver puts the ADC in continuous mode so there are no multiple triggers, it is started once.

Giovanni
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

Re: ADC works for buffer =1, but not more.

Post by rew »

I didn't know that existed, I checked the datasheet and it does.... I'll see if I can program a register dump for the ADC module.
Post Reply