_unhandled_exception writing to serial

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

Moderator: RoccoMarco

Post Reply
SevenW
Posts: 2
Joined: Sun Oct 05, 2014 2:07 pm

_unhandled_exception writing to serial

Post by SevenW »

I try to debug a hangup when writing to a serial device on a Nucleo F103RB. I am using Chibios V3.
I traced the hangup to the situation where the serial buffer is full, and the OS is going to do a thread switch to Sleep with timeout INFINITE.
The exception occurs (I think) because the current execution pointer ends up in unused memory.

Code: Select all

msg_t oqPutTimeout(output_queue_t *oqp, uint8_t b, systime_t time) {

  osalSysLock();
  while (oqIsFullI(oqp)) {
    msg_t msg;

    if ((msg = osalThreadEnqueueTimeoutS(&oqp->q_waiting, time)) < Q_OK) {
      osalSysUnlock();
      return msg;
    }
  }


osalThreadEnqueueTimeoutS does not return.
Tracing deeper I end up here:

Code: Select all

void chSchGoSleepS(tstate_t newstate) {
  thread_t *otp;

  chDbgCheckClassS();

  (otp = currp)->p_state = newstate;
#if CH_CFG_TIME_QUANTUM > 0
  /* The thread is renouncing its remaining time slices so it will have a new
     time quantum when it will wakeup.*/
  otp->p_preempt = CH_CFG_TIME_QUANTUM;
#endif
  setcurrp(queue_fifo_remove(&ch.rlist.r_queue));
#if defined(CH_CFG_IDLE_ENTER_HOOK)
  if (currp->p_prio == IDLEPRIO) {
    CH_CFG_IDLE_ENTER_HOOK();
  }
#endif
  currp->p_state = CH_STATE_CURRENT;
  chSysSwitch(currp, otp);
}


Where the last line "chSysSwitch(currp, otp);" cause the hangup. Tracing this further I do not enter this function, but end up in some inline ASM code.
SevenW
SevenW
Posts: 2
Joined: Sun Oct 05, 2014 2:07 pm

Re: _unhandled_exception writing to serial

Post by SevenW »

I am affraid it was a stacksize problem.
The stack size check did not lead to a halt, but further playing around and inspecting memory (using fill pattern) made it clear that stack was too limited.

Code: Select all

//debug
#define CH_DBG_ENABLE_STACK_CHECK           TRUE

To bad that this not lead to a halt in my case.

When I use a working area size of 256, it leaves me with 24 spare bytes on the stack. My program addresses a radio device via SPI and prints some status information to the serial device. Only the mentioned Sleep condition uses so much stack space. Printing the first couple of lines to the serial device is not using a lot.

Well, at least my problem is gone by now.
SevenW
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: _unhandled_exception writing to serial

Post by Giovanni »

Hi,

This kind of problems are almost always in one of following categories:
1) Overflows, including stack overflows.
2) Call protocol violations (function called out of proper context).

The debug options in chconf.h are very powerful and can detect most error conditions, it is good practice develop with all options turned on.

Giovanni
Post Reply