STM32F4-Discovery I/O Queue Chprintf Stack OverFlow Problem

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

Moderator: RoccoMarco

Post Reply
franek.bmw
Posts: 57
Joined: Thu Mar 13, 2014 11:15 am
Has thanked: 2 times

STM32F4-Discovery I/O Queue Chprintf Stack OverFlow Problem

Post by franek.bmw »

Hello,
I wrote a simple program to test queues on STM32F4-Discovery with Chibios 2.6.3. In the first thread I add to the queue an element (a letter from A to F). In the second thread I take element of the queue and I display it using Chprintf. In addition, in the first thread I also toggle the LED.
The program works well for a few minutes and then freezes and pops up an error "stack overflow". I have increased the size of all possible stacks, but it did not help.

I have set all the debugging options as in the document: http://www.chibios.org/dokuwiki/doku.ph ... ebug_guide


Does anyone know what is wrong in my program?


Code: Select all

/*
    ChibiOS/RT - Copyright (C) 2006-2013 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"
#include "chprintf.h"

#define PORT_IDLE_THREAD_STACK_SIZE 0x0400

static uint8_t queueBuffer[20];


static INPUTQUEUE_DECL(inputQueueTest, &queueBuffer, 2000, NULL, NULL);

const SerialConfig sd2_config =
{
      // SerialConfig for STM32 platforms
      921600, // bit rate
      0, // CR1 register settings
      0, // CR2 register settings
      0, // CR3 register settings
};

static WORKING_AREA(waThread1, 16000);
static msg_t HeartBeatThread(void *arg) {

   (void)arg;
   chRegSetThreadName("LED_HEARTBEAT");

   CANRxFrame rxmsg1;
   static int i = 0;

   while (TRUE)
   {
      palSetPad(GPIOD, GPIOD_LED3);
      chThdSleepMilliseconds(100);
      palClearPad(GPIOD, GPIOD_LED3);
      chThdSleepMilliseconds(100);
      palSetPad(GPIOD, GPIOD_LED3);
      chThdSleepMilliseconds(100);
      palClearPad(GPIOD, GPIOD_LED3);
      chThdSleepMilliseconds(700);

      chSysLock();
      int pull = chIQPutI(&inputQueueTest, 'A' + i);
      chSysUnlock();
      if(pull == Q_OK)
      {
         chprintf((BaseSequentialStream*)&SD2, "put ok\n");
      }
      if(pull == Q_FULL)
      {
         chprintf((BaseSequentialStream*)&SD2, "put full\n");
      }
      i++;
      if (i == 6)
      {
         i = 0;
      }
   }
}


static WORKING_AREA(testQueue_wa, 8192);
static msg_t testQueue(void *arg)
{
   (void)arg;
   while(TRUE)
   {
      int Element = chIQGetTimeout(&inputQueueTest, MS2ST(200));

      if(Element == Q_TIMEOUT)
      {
         chprintf((BaseSequentialStream*)&SD2, "TIMEOUT\n");
      }
      else if (Element == Q_RESET)
      {
         chprintf((BaseSequentialStream*)&SD2, "RESET\n");
      }
      else
      {
         chprintf((BaseSequentialStream*)&SD2, "element is %c\n", Element);
      }
   }
}



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


   chIQInit(&inputQueueTest, (uint8_t *)&queueBuffer, 2000, NULL, NULL);
   /*
    * Activates the serial driver 2 using the driver default configuration.
    * PA2(TX) and PA3(RX) are routed to USART2.
    */
   sdStart(&SD2, &sd2_config);
   palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));
   palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7));



   chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, HeartBeatThread, NULL);
   chThdCreateStatic(testQueue_wa, sizeof(testQueue_wa), NORMALPRIO, testQueue, NULL);

   while (TRUE)
   {
      chThdSleepMilliseconds(1000);
   }
}
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F4-Discovery I/O Queue Chprintf Stack OverFlow Prob

Post by Giovanni »

Hi,

Could it be that 2000 in the queue declaration?, you buffer is just 20 bytes long. The queue overflows after a while.

Giovanni
franek.bmw
Posts: 57
Joined: Thu Mar 13, 2014 11:15 am
Has thanked: 2 times

Re: STM32F4-Discovery I/O Queue Chprintf Stack OverFlow Prob

Post by franek.bmw »

Hey,
Thank you very much! It helped and now it works. I do not understand, however, why the queue overflows, if every 200ms I take an element from the queue and only once per second I give the element to the queue. Can you explain this?

I also have another question: I tried to run the plugin ChibiosRT for Eclipse, but it does not work. Could you describe step by step what should be done to use this plugin?
I use Eclipse Juno, OpenOCD_0.7.0 and toolchain from CodeSourcery (arm-2011.03-42-arm-none-eabi) on Ubuntu 12.04.

At last, how can I find out how much free space is available on the stack without ChibiOS Plugin?

Best regards,
Frank
Post Reply