STM32f3Discovery : ADC conversion triggered by an EXTI line

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

Moderator: RoccoMarco

Post Reply
giazax
Posts: 12
Joined: Tue Nov 26, 2013 4:43 pm

STM32f3Discovery : ADC conversion triggered by an EXTI line

Post by giazax »

Hi,
i want to use the exti line to start an adc conversion.

are there any example about this topic?

any suggestion will be apprectiated

giancarlo
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32f3Discovery : ADC conversion triggered by an EXTI l

Post by Giovanni »

I don't have an example but it is very simple, just call adcStartConversionI() from the EXT callback, the conversion end is signaled by the ADC callback.

Remember that you have to put "I" APIs within chSysLockFromIsr() and chSysUnlockFromIsr().

Giovanni
giazax
Posts: 12
Joined: Tue Nov 26, 2013 4:43 pm

Re: STM32f3Discovery : ADC conversion triggered by an EXTI l

Post by giazax »

hi giovanni,
this is my code, after you suggestion but i'm not lucky.... :-)

Code: Select all

static void extcb1(EXTDriver *extp, expchannel_t channel) {
  static VirtualTimer vt4;
  (void)extp;
  (void)channel;
 
  chSysLockFromIsr();
  if (chVTIsArmedI(&vt4))
    chVTResetI(&vt4);
   adcStartConversionI(&ADCD1, &adcgrpcfg1, samples1, ADC_GRP1_BUF_DEPTH);

/* Disattiva il Sample dopo 200mS del trigger*/
  chVTSetI(&vt4, MS2ST(200), AttivaSampleHold, NULL);
  chSysUnlockFromIsr();

}


and the adc conversiongoup is

Code: Select all

/*
 * ADC conversion group.
 * Mode:        Linear buffer, 8 samples of 2 channels, SW triggered.
 * Channels:    IN7 = PC1, IN8 = PC2.
 *              IN5 = PF4 -----------CASO RAMONA INPUT ADC!!!!!!
 */
static const ADCConversionGroup adcgrpcfg1 = {
  FALSE,
  ADC_GRP1_NUM_CHANNELS,
  adccallback,
  adcerrorcallback,
  0,                        /* CFGR    */
  ADC_TR(0, 4095),          /* TR1     */
  0,                        /* CCR     */
  {                         /* SMPR[2] */
    0,
    0
  },
  {                         /* SQR[4]  */
    ADC_SQR1_SQ1_N(ADC_CHANNEL_IN5) | ADC_SQR1_SQ2_N(ADC_CHANNEL_IN8),
    0,
    0,
    0
  }
};


....what's wrog with it?

thanks

giancarlo
giazax
Posts: 12
Joined: Tue Nov 26, 2013 4:43 pm

Re: STM32f3Discovery : ADC conversion triggered by an EXTI l

Post by giazax »

have i to configure the ADC_CFGR registers?

any suggestion will be apprectiated

thanks

giancarlo
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32f3Discovery : ADC conversion triggered by an EXTI l

Post by Giovanni »

Hi,

The code looks correct, probably it is a problem with the conversion group structure, look at the F4 ADC example under ./testhal.

It would be useful to specify what you expect from the code and what you get instead.

Giovanni
giazax
Posts: 12
Joined: Tue Nov 26, 2013 4:43 pm

Re: STM32f3Discovery : ADC conversion triggered by an EXTI l

Post by giazax »

hi,
I would like to control the ADC via an external trigger, which in my case is a rising edge on a pin.
I checked the pin setting is correct for both the adc and the trigger.
I took the examples in testhal (EXTI and ADC) and work. Now I would like to combine the two.
It happens that the code is locked after the first time i triggered...so i cannot perform anything :(

thanks for your patience
greetings

giancarlo

the main.c code is :

Code: Select all

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

#define ADC_GRP1_NUM_CHANNELS   2
#define ADC_GRP1_BUF_DEPTH      16

static adcsample_t samples1[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];
float sample;
float ADCConversionValue;

/*
 * ADC streaming callback.
 */
size_t nx = 0, ny = 0;
static void adccallback(ADCDriver *adcp, adcsample_t *buffer, size_t n) {
  (void)adcp;
  if (samples1 == buffer) {
    nx += n;

  }
  else {
    ny += n;
  }
}

static void adcerrorcallback(ADCDriver *adcp, adcerror_t err) {
  (void)adcp;
  (void)err;
}

/*
 * ADC conversion group.
 * Mode:        Linear buffer, 16 samples of 2 channels, SW triggered.
 * Channels:    IN7 = PC1, IN8 = PC2.
 *              IN5 = PF4
 */
static const ADCConversionGroup adcgrpcfg1 = {
  FALSE,
  ADC_GRP1_NUM_CHANNELS,
  NULL,
  adcerrorcallback,
  0,                        /* CFGR    */
  ADC_TR(0, 4095),          /* TR1     */
  0,                        /* CCR     */
  {                         /* SMPR[2] */
    0,
    0
  },
  {                         /* SQR[4]  */
    ADC_SQR1_SQ1_N(ADC_CHANNEL_IN5) | ADC_SQR1_SQ2_N(ADC_CHANNEL_IN8),
    0,
    0,
    0
  }
};


static void AttivaSampleHold(void *arg) {
 (void)arg;
  palTogglePad(GPIOE, GPIOE_LED8_ORANGE);
  palTogglePad(GPIOE, GPIOE_LED5_ORANGE);
}



/* Trigger ADC */
    static void extcb1(EXTDriver *extp, expchannel_t channel) {
      static VirtualTimer vt4;
      (void)extp;
      (void)channel;
     
      chSysLockFromIsr();
      if (chVTIsArmedI(&vt4))
        chVTResetI(&vt4);
        adcStartConversionI(&ADCD1, &adcgrpcfg1, samples1, ADC_GRP1_BUF_DEPTH);

        /* Disattiva il Sample dopo 200mS del trigger*/
       chVTSetI(&vt4, MS2ST(200), AttivaSampleHold, NULL);
      chSysUnlockFromIsr();

    }

static void led4off(void *arg) {

  (void)arg;
  palClearPad(GPIOE, GPIOE_LED4_BLUE);
}

/* Triggered when the button is pressed or released. The LED5 is set to ON.*/
static void extcb2(EXTDriver *extp, expchannel_t channel) {
  static VirtualTimer vt4;

  (void)extp;
  (void)channel;

  palSetPad(GPIOE, GPIOE_LED4_BLUE);
  chSysLockFromIsr();
  if (chVTIsArmedI(&vt4))
    chVTResetI(&vt4);

  /* LED4 set to OFF after 200mS.*/
  chVTSetI(&vt4, MS2ST(200), led4off, NULL);
  chSysUnlockFromIsr();
}


/*
 * EXT CONFIGURAZIONE PAD
 */
static const EXTConfig extcfg = {
 {
  {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOA, extcb2},
  {EXT_CH_MODE_DISABLED, NULL},
  {EXT_CH_MODE_DISABLED, NULL},
  {EXT_CH_MODE_DISABLED, NULL},
  {EXT_CH_MODE_DISABLED, NULL},
  {EXT_CH_MODE_DISABLED, NULL},
  {EXT_CH_MODE_RISING_EDGE | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
  {EXT_CH_MODE_DISABLED, NULL},
  {EXT_CH_MODE_DISABLED, NULL},
  {EXT_CH_MODE_DISABLED, NULL},
  {EXT_CH_MODE_DISABLED, NULL},
  {EXT_CH_MODE_DISABLED, NULL},
  {EXT_CH_MODE_DISABLED, NULL},
  {EXT_CH_MODE_DISABLED, NULL},
  {EXT_CH_MODE_DISABLED, NULL},
  {EXT_CH_MODE_DISABLED, NULL},
  {EXT_CH_MODE_DISABLED, NULL}
 }
};


void padInit(void){
 
     palSetPadMode(GPIOE,GPIOE_PIN6,PAL_MODE_INPUT);
     palSetPadMode(GPIOF,GPIOF_PIN4,PAL_MODE_INPUT_ANALOG);
}


/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * Inizializzazione PIN
   */
  padInit();


  /*
   *
   *
   * PE6 TRIGGER    ANALOGICO   INPUT INTERRUPT USCITA COMPARATORE
   * PF4 ---ANALOG INPUT
   * PC2 ---GROUND
   *
   */

    palSetPad(GPIOE, GPIOE_LED5_ORANGE);
    palSetPad(GPIOE, GPIOE_LED8_ORANGE);
    extStart(&EXTD1, &extcfg);
   ADCConversionValue =  (3.3 / 4095);


  while (TRUE) {
        extChannelEnable(&EXTD1, 0);
      palSetPad(GPIOE, GPIOE_LED7_GREEN);
      chThdSleepMilliseconds(100);
      palClearPad(GPIOE, GPIOE_LED7_GREEN);

        //ACCENSIONE E SPEGNIMENTO LED 4 di Colore BLU
        palSetPad(GPIOE, GPIOE_LED9_BLUE);
        chThdSleepMilliseconds(200);
        palClearPad(GPIOE, GPIOE_LED9_BLUE);
  }
  return 0;
}
 
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32f3Discovery : ADC conversion triggered by an EXTI l

Post by Giovanni »

Posting code does not help much.

You need to:
1) enable the debug options in chconf.h
2) after the program stops see the stack trace using the debuger
3) if it stops in port_stop() then read the message pointed by dbg_panic_msg

Giovanni
Post Reply