Differential ADC

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
antd_a
Posts: 3
Joined: Mon Nov 10, 2014 7:19 pm

Differential ADC

Post by antd_a »

Hi everybody!

I am a newbie in using ChibiOS and I would need to use the ADCs of my STM32F3DISCOVERY board in differential mode.
Reading the microcontroller reference manual, I understood what should happen at low level to do so. Anyway crossing these info with the ADC driver written in ChibiOS, it seems to me that it has not been implemented this function yet. :(

Am I right? I hope no... If so, can anyone help me to use this function?

Thanks in advance,
Antonio.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Differential ADC

Post by Giovanni »

Hi,

The driver should work as-is, differential or single ended makes no difference. You must setup the DIFSEL register after starting the driver but before starting a conversion. See the STM32 Reference Manual for details.

Giovanni
antd_a
Posts: 3
Joined: Mon Nov 10, 2014 7:19 pm

Re: Differential ADC

Post by antd_a »

Hi Giovanni,
and thanks for your reply.

I tried to follow your suggestion and I wrote this simple code for testing:

Code: Select all

 #include "ch.h"
#include "hal.h"

#define ADC_NUM_CHANNELS   1
#define ADC_BUF_DEPTH      128

static adcsample_t ADCsamples[ADC_NUM_CHANNELS*ADC_BUF_DEPTH] = {0};
static uint32_t average = 0;


static const ADCConversionGroup adcgrpcfg =
{
  FALSE,
  ADC_NUM_CHANNELS,
  NULL,
  NULL,
  ADC_CFGR1_RES_12BIT,      /* CFGR    */
  ADC_TR(0, 4095),          /* TR1     */
  ADC_CCR_CKMODE_ADCCK,     /* CCR     */
  {                         /* SMPR[2] */
   ADC_SMPR1_SMP_AN2(ADC_SMPR_SMP_1P5),
   0
  },
  {                         /* SQR[4]  */
    ADC_SQR1_SQ1_N(ADC_CHANNEL_IN2),
    0,
    0,
    0
  }
};


int main(void)
{
  int i = 0;

  halInit();
  chSysInit();

  palSetGroupMode(GPIOA, PAL_PORT_BIT(1) | PAL_PORT_BIT(2), 0, PAL_MODE_INPUT_ANALOG);

  adcStart(&ADCD1, NULL);

  ADC1->DIFSEL = ADC_DIFSEL_DIFSEL_1;

  adcConvert(&ADCD1, &adcgrpcfg, &ADCsamples[0], ADC_BUF_DEPTH);

  for (i = 0; i < ADC_BUF_DEPTH; i++)
  {
    average = average + ADCsamples[i];
  }
  average = average/ADC_BUF_DEPTH;

  while (TRUE)
   {
    chThdSleepMilliseconds(1);
   }

  return 0;
}


I expected to be able to convert the voltage drop between pins PA1 (channel 2 of ADC1) and PA2 (channel 2 of ADC1), but it does not work. It just converts the voltage drop between pins PA1 and GND, as in single-ended mode. I tried to move the DIFSEL setting before "adcStart", but with the same result.

Have you any idea about what can be wrong? Is the way I set DIFSEL right? (building the project does not return any error or warning)

Thank you in advance...
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Differential ADC

Post by Giovanni »

Not sure, never used the differential mode but it is just matter of enabling it. Are you sure you are using the right DIFSEL bit?

Giovanni
neums
Posts: 137
Joined: Fri May 08, 2015 1:09 pm
Has thanked: 10 times
Been thanked: 5 times

Re: Differential ADC

Post by neums »

Hello,
will there be a possibility to set up the differential mode with the ADCConversionGroup in future?

Thanks :-)
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Differential ADC

Post by Giovanni »

Hi,

It should be already possible, what do you think is missing?

Giovanni
neums
Posts: 137
Joined: Fri May 08, 2015 1:09 pm
Has thanked: 10 times
Been thanked: 5 times

Re: Differential ADC

Post by neums »

Hello,
i am working with the STM32F303 now. Via ADCConversionGroup defined in adc_lld the ADCx_DIFSEL register is not accessible. You have to initialize this register directly in the ADCD handle which is a possible way but not really a nice way, isn't it?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Differential ADC

Post by Giovanni »

Of course, so I need to add the register to the configuration.

Giovanni
antd_a
Posts: 3
Joined: Mon Nov 10, 2014 7:19 pm

Re: Differential ADC

Post by antd_a »

I think it would be very valued, thanks :D

Antonio
neums
Posts: 137
Joined: Fri May 08, 2015 1:09 pm
Has thanked: 10 times
Been thanked: 5 times

Re: Differential ADC

Post by neums »

Hello,
did you already add this register to the ADCConversionGroup?
Post Reply