STM32F0 ADC Example not working?

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

Moderators: RoccoMarco, barthess

Tinkerer
Posts: 19
Joined: Wed Apr 19, 2017 2:47 pm
Has thanked: 4 times

STM32F0 ADC Example not working?

Postby Tinkerer » Mon Nov 12, 2018 4:33 pm

Dear reader,

I'm trying to run the STM32F0XX-ADC example on a bare STM32F051K8 microcontroller.
Although it compiles and runs on the hardware, it seems to not do any conversions.

Configuration:

* ChibiStudio 20
* ChibiOS 18.2
* Started with start_gcc70.bat
* Connected to ST-Link/V2

Description of symptoms:

I compile the program, and flash it to the device using the ST-Link. The program runs OK, I can set breakpoints in the main loop and have the program stop there. Because the device is an QFN32, I changed board.h to sample on PORTA pin 1. However, I do not see changing values in [monospace]samples2[/monospace] .
To be sure whether the ADC was running I put a breakpoint on lines 35 and 46, in the callbacks of the ADC. These are never reached.

Am I missing a vital part of configuration (clock assignment, ...)?

I've looked at this code and the ADC Kickstart example, but can't figure out why the values aren't updating. Any ideas would be appreciated.


main.cpp:

Code: Select all

/*
    ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

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

#define ADC_GRP1_NUM_CHANNELS   1
#define ADC_GRP1_BUF_DEPTH      8

#define ADC_GRP2_NUM_CHANNELS   4
#define ADC_GRP2_BUF_DEPTH      16

static adcsample_t samples1[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];
static adcsample_t samples2[ADC_GRP2_NUM_CHANNELS * ADC_GRP2_BUF_DEPTH];

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

  (void)adcp;
  if (samples2 == buffer) {
    nx += n;
  }
  else {
    ny += n;
  }
}

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

  (void)adcp;
  (void)err;
}

/*
 * ADC conversion group.
 * Mode:        Linear buffer, 8 samples of 1 channel, SW triggered.
 * Channels:    IN10.
 */
static const ADCConversionGroup adcgrpcfg1 = {
  FALSE,
  ADC_GRP1_NUM_CHANNELS,
  NULL,
  adcerrorcallback,
  ADC_CFGR1_CONT | ADC_CFGR1_RES_12BIT,             /* CFGR1 */
  ADC_TR(0, 0),                                     /* TR */
  ADC_SMPR_SMP_1P5,                                 /* SMPR */
  ADC_CHSELR_CHSEL10                                /* CHSELR */
};

/*
 * ADC conversion group.
 * Mode:        Continuous, 16 samples of 8 channels, SW triggered.
 * Channels:    IN10, IN11, Sensor, VRef.
 */
static const ADCConversionGroup adcgrpcfg2 = {
  TRUE,
  ADC_GRP2_NUM_CHANNELS,
  adccallback,
  adcerrorcallback,
  ADC_CFGR1_CONT | ADC_CFGR1_RES_12BIT,             /* CFGR1 */
  ADC_TR(0, 0),                                     /* TR */
  ADC_SMPR_SMP_28P5,                                /* SMPR */
  ADC_CHSELR_CHSEL10 | ADC_CHSELR_CHSEL11 |
  ADC_CHSELR_CHSEL16 | ADC_CHSELR_CHSEL17           /* CHSELR */
};

/*
 * Red LEDs blinker thread, times are in milliseconds.
 */
static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg) {

  (void)arg;
  chRegSetThreadName("blinker");
  while (true) {
    palSetPad(GPIOC, GPIOC_LED4);
    chThdSleepMilliseconds(500);
    palClearPad(GPIOC, GPIOC_LED4);
    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();

  /*
   * Setting up analog inputs used by the demo.
   */
  palSetGroupMode(GPIOC, PAL_PORT_BIT(0) | PAL_PORT_BIT(1),
                  0, PAL_MODE_INPUT_ANALOG);

  /*
   * Creates the blinker thread.
   */
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

  /*
   * Activates the ADC1 driver and the temperature sensor.
   */
  adcStart(&ADCD1, NULL);
  adcSTM32SetCCR(ADC_CCR_VBATEN | ADC_CCR_TSEN | ADC_CCR_VREFEN);

  /*
   * Linear conversion.
   */
  adcConvert(&ADCD1, &adcgrpcfg1, samples1, ADC_GRP1_BUF_DEPTH);
  chThdSleepMilliseconds(1000);

  /*
   * Starts an ADC continuous conversion.
   */
  adcStartConversion(&ADCD1, &adcgrpcfg2, samples2, ADC_GRP2_BUF_DEPTH);

  /*
   * Normal main() thread activity, in this demo it does nothing.
   */
  while (true) {
    if (palReadPad(GPIOA, GPIOA_BUTTON)) {
      adcStopConversion(&ADCD1);
      adcSTM32SetCCR(0);
    }
    chThdSleepMilliseconds(500);
  }
}


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

Re: STM32F0 ADC Example not working?

Postby Giovanni » Mon Nov 12, 2018 7:13 pm

Hi,

Try using -O0 as optimization or you may not hit breakpoints, it is pretty common because the compiler rearranges the code a lot.

Also, is samples1 filled?

Giovanni

Tinkerer
Posts: 19
Joined: Wed Apr 19, 2017 2:47 pm
Has thanked: 4 times

Re: STM32F0 ADC Example not working?

Postby Tinkerer » Tue Nov 13, 2018 9:15 am

Hello Giovanni,

Thanks, that helped. I tried -O0, and it still didn't work as expected. Then, by stepping through the code I found that the breakpoint is never hit because it was on the "(void) adcp;" declaration line instead of the line below. Human error....

Next I reset to -O2, which gave me the strange behaviour again, that a breakpoint in the main loop is never hit. I'd guess this is an optimization thingy. When I now re-enabled the breakpoint in the interrupt routine, I could see that sampling was going fine.

User avatar
wurstnase
Posts: 121
Joined: Tue Oct 17, 2017 2:24 pm
Has thanked: 43 times
Been thanked: 30 times
Contact:

Re: STM32F0 ADC Example not working?

Postby wurstnase » Tue Nov 13, 2018 2:38 pm

Instead of -O0 you could compile it with -Og which could be a little bit more optimized while being able to debug the code.
\o/ Nico


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 11 guests