Problems get ADC running on STM32F103RE

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
elims
Posts: 5
Joined: Mon May 14, 2012 10:02 pm

Problems get ADC running on STM32F103RE

Post by elims »

Hi,

since a couple of days I try to get my head around ChibiOS. So far I managed to get the ARMCM3-STM32F103-demo running with Eclipse and the Yagarto GCC together with my board (Futurlec ET-ARM STM32 Stamp with STM32F103RET6). Now I'm trying to get the peripherials to work I plan to use. chprintf over SD1 worked well, but right now I'm stuck with the ADC. I tried to adopt the ARMCM3-STM32F100-DISCOVERY-demo sample to work with my board. But the moment adcStart(&ADCD1, NULL); is invoked the program stops. Unfortunately I don't have a JTAG-debugger yet. Even the serial transmission started shortly befor stops and I only recieve the first 6 characters.

Below is my main.c. Maybe someone can point me into the right direction.

Best regards

Code: Select all

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


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

/* Total number of channels to be sampled by a single ADC operation.*/
#define ADC_GRP1_NUM_CHANNELS   2

/* Depth of the conversion buffer, channels are sampled four times each.*/
#define ADC_GRP1_BUF_DEPTH      4

/*
 * ADC samples buffer.
 */
static adcsample_t samples[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];

/*
 * ADC conversion group.
 * Mode:        Linear buffer, 4 samples of 2 channels, SW triggered.
 * Channels:    IN1   (41.5 cycles sample time)
 *              Sensor (239.5 cycles sample time)
 */
static const ADCConversionGroup adcgrpcfg = {
  FALSE,
  ADC_GRP1_NUM_CHANNELS,
  adccb,
  NULL,
  /* HW dependent part.*/
  0,
  ADC_CR2_TSVREFE,
  ADC_SMPR1_SMP_AN10(ADC_SAMPLE_41P5) | ADC_SMPR1_SMP_SENSOR(ADC_SAMPLE_239P5),
  0,
  ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS),
  0,
  ADC_SQR3_SQ2_N(ADC_CHANNEL_IN10) | ADC_SQR3_SQ1_N(ADC_CHANNEL_SENSOR)
};


/*
 * ADC end conversion callback.
 * The PWM channels are reprogrammed using the latest ADC samples.
 * The latest samples are transmitted into a single SPI transaction.
 */
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) {
    adcsample_t avg_ch1, avg_ch2;

    /* Calculates the average values from the ADC samples.*/
    avg_ch1 = (samples[0] + samples[2] + samples[4] + samples[6]) / 4;
    avg_ch2 = (samples[1] + samples[3] + samples[5] + samples[7]) / 4;

    chprintf(&SD1,"ADC_IN10: %u, ADC_Sensor: %u\n", avg_ch1, avg_ch2);

  }
}

/*
 * Red LED blinker thread, times are in milliseconds.
 */
static WORKING_AREA(waAliveBlink, 128);
static msg_t AliveBlink(void *arg) {

  (void)arg;
  chRegSetThreadName("blinker");
  while (TRUE) {
    palClearPad(GPIOB, GPIOB_LED);
    chThdSleepMilliseconds(500);
    palSetPad(GPIOB, GPIOB_LED);
    chThdSleepMilliseconds(500);
  }
}

/*
 * 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();

  /*
   * Creates the blinker thread.
   */
  chThdCreateStatic(waAliveBlink, sizeof(waAliveBlink), NORMALPRIO, AliveBlink, NULL);

  /*
   * Activates the serial driver 2 using the driver default configuration.
   */
  sdStart(&SD1, NULL);
  chprintf(&SD1,"Futurlec STM32-Stamp\n");

  /*
   * Initializes the ADC driver 1.
   * The pin PC0 on the port GPIOC is programmed as analog input.
   */
  palSetGroupMode(GPIOC, PAL_PORT_BIT(0), 0, PAL_MODE_INPUT_ANALOG);
  adcStart(&ADCD1, NULL);

  /*
   * Normal main() thread activity, in this demo it does nothing except
   * sleeping in a loop and check the button state.
   */
  while (TRUE) {
   adcStartConversion(&ADCD1, &adcgrpcfg, samples, ADC_GRP1_BUF_DEPTH);
    chThdSleepMilliseconds(1000);
  }
}
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Problems get ADC running on STM32F103RE

Post by Giovanni »

Hi,

Probably the problem is that you call chPrintf() from a callback, callbacks are invoked in ISR context, only I-class functions should be used in ISR context. chprintf() calls the serial driver which is not i-class.
The problem is that synchronous operations cannot be used in ISRs, you cannot "wait" there.

If you need another example of ADC usage you could look at the test demo under ./testhal/STM32F1xx/ADC.

Giovanni
elims
Posts: 5
Joined: Mon May 14, 2012 10:02 pm

Re: Problems get ADC running on STM32F103RE

Post by elims »

Hi Giovanni,

thanks for the hint. I just tried the ./testhal/STM32F1xx/ADC and now the LED is blinking. Seems the better starting point for further tests.

Best regards
Post Reply