Page 1 of 1

ADC and shell strange behavior

Posted: Wed Apr 23, 2014 11:48 am
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)
};

Re: ADC and shell strange behavior

Posted: Wed Apr 23, 2014 12:45 pm
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

Re: ADC and shell strange behavior

Posted: Fri Apr 25, 2014 2:32 pm
by colin
Is there a stack checking feature in ChibiOS that can help to catch stack overflows near where they occur?

Re: ADC and shell strange behavior

Posted: Fri Apr 25, 2014 2:43 pm
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

Re: ADC and shell strange behavior

Posted: Mon Apr 28, 2014 9:38 am
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?

Re: ADC and shell strange behavior

Posted: Mon Apr 28, 2014 10:50 am
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