ADC and shell strange behavior

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

Moderator: RoccoMarco

Post Reply
tinito
Posts: 112
Joined: Tue Jun 07, 2011 10:32 am
Has thanked: 1 time
Been thanked: 1 time

ADC and shell strange behavior

Post by tinito »

I've experienced really strange behavior when working with the ADC and the shell over UART on a STM32F103 MD device. I'm running ChibiOS/RT 2.6.3. I've enabled all debug checks.
The system halts (port_halt()) from a debug check called while waiting for characters on the serial port, in particular the chDbgCheck(n > 0, "chIQReadTimeout"); check called by shellGetLine(), which in turns calls chIQReadTimeout(). The strange fact is that if I inspect n with gdb, it is set to 1, as expected, but the debug check triggers anyway.
Looking at dbg_panic_msg, the content is not a string.
I suspect there is some overflow around (DMA writing outside the buffer?), but I can't find where and why. The eclipse plugins reports that thread stacks are almost empty.
The other strange thing is that compiling with -O3 apparently solve the problems, but with -O0 the system systematically hangs in this point.

Any hints on where to look to debug this?

My code is pretty simple and standard:

Code: Select all


/* Shell manager initialization. */
shellInit();

/* Start the serial driver. */
sdStart(&SERIAL_DRIVER, NULL);

/* Start the ADC driver. */
adcStart(&ADC_DRIVER, NULL);

/* Starts an ADC continuous conversion. */
adcStartConversion(&ADC_DRIVER, &adcgrpcfg, adc_samples, ADC_BUF_DEPTH);

for (;;) {
   if (!shelltp)
      shelltp = shellCreate(&shell_cfg, SHELL_WA_SIZE, NORMALPRIO);
   else if (chThdTerminated(shelltp)) {
      chThdRelease(shelltp);    /* Recovers memory of the previous shell.   */
      shelltp = NULL;           /* Triggers spawning of a new shell.        */
   }
   
   chThdSleepMilliseconds(500);
}


Here the configuration for the conversion group:

Code: Select all

#define ADC_NUM_CHANNELS   1
#define ADC_BUF_DEPTH      8

adcsample_t adc_samples[ADC_NUM_CHANNELS * ADC_BUF_DEPTH];

static const ADCConversionGroup adcgrpcfg = {
   TRUE,
   ADC_NUM_CHANNELS,
   NULL,
   NULL,
   0, 0,                         /* CR1, CR2 */
   0,
   ADC_SMPR2_SMP_AN3(ADC_SAMPLE_239P5), /* SMPR2 */
   ADC_SQR1_NUM_CH(ADC_NUM_CHANNELS),
   0,                            /* SQR2 */
   ADC_SQR3_SQ1_N(ADC_CHANNEL_IN3)
};
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 and shell strange behavior

Post by Giovanni »

Hi,

I would try increasing stacks, including the idle thread stack, because -O0 wastes a lot of space in stack frames. There is another thread discussing a similar problem too.

You may also use the ChibiOS plugin in order to inspect the stacks usage.

Giovanni
colin
Posts: 149
Joined: Thu Dec 22, 2011 7:44 pm

Re: ADC and shell strange behavior

Post by colin »

Is there a stack checking feature in ChibiOS that can help to catch stack overflows near where they occur?
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 and shell strange behavior

Post by Giovanni »

Yes, in chconf.h, among a lot of other debug options, I recommend enabling all of them because they can catch a lot of anomalies.

Giovanni
tinito
Posts: 112
Joined: Tue Jun 07, 2011 10:32 am
Has thanked: 1 time
Been thanked: 1 time

Re: ADC and shell strange behavior

Post by tinito »

Thanks Giovanni for your fast reply.
I was already using the ChibiOS/RT Eclipse plugin, and it looks like threads still have a lot of empty stack (>1kb free).
Any chance of an overflow of DMA while writing into the ADC samples buffer?
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 and shell strange behavior

Post by Giovanni »

That would be a bug in the ADC driver, you may test this by allocating a larger samples buffer and pre-filling it with a pattern (0x5555 for example).
If the ADC driver overwrites the filler placed over area the ADC is supposed to overwrite then there is a bug.

Giovanni
Post Reply