Stack overflow from chprintf() in RTC callback

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
scdwyer
Posts: 2
Joined: Thu Nov 06, 2014 12:14 am

Stack overflow from chprintf() in RTC callback

Post by scdwyer »

Hello,

I am writing some test code to get a feel for how different HAL APIs work, and right now I am working on the RTC interface.

I am using an STM32F1xx (103RB), with arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors) 4.8.3 20140228 (release), and ChibiOS/RT 2.6.7.

Everything so far is working as expected. I can set and get the rtc time and alarm, and the fat time, from main() after chSysInit(). I can also enable the callback, which works as expected, except for one thing, I get a stack overflow if my chprintf() string is too long inside the callback:

Code: Select all

void rtc_alarm_handler(RTCDriver *rtcp, rtcevent_t event) {
  RTCAlarm rtcalarm;
  if (event == RTC_EVENT_ALARM) {
    chprintf(SD2bssp,"Alarm Handler: Alarm Expired! Set again.\n");
    // NOTE the above printf string causes a stack overflow...
    // The one below DOESN'T overflow
    // chprintf(SD2bssp,"ALARM\n");
    rtcGetAlarm(rtcp,0,&rtcalarm);
    rtcalarm.tv_sec += 30;
    rtcSetAlarm(rtcp,0,&rtcalarm);
  }
  return;
}


The backtrace gives:

Code: Select all

(gdb) bt
#0  0x08000352 in port_halt () at ../.././ChibiOS-RT/os/ports/GCC/ARMCMx/chcore.c:48
#1  0x08000668 in chDbgPanic (msg=msg@entry=0x8002080 "stack overflow") at ../.././ChibiOS-RT/os/kernel/src/chdebug.c:274
#2  0x08000806 in chSchGoSleepS (newstate=<optimized out>) at ../.././ChibiOS-RT/os/kernel/src/chschd.c:128
#3  0x08000852 in chSchGoSleepTimeoutS (newstate=newstate@entry=13 '\r', time=<optimized out>) at ../.././ChibiOS-RT/os/kernel/src/chschd.c:202
#4  0x08000c30 in qwait (qp=<optimized out>, time=<optimized out>) at ../.././ChibiOS-RT/os/kernel/src/chqueues.c:76
#5  0x08000db4 in chOQPutTimeout (oqp=0x20000e50 <SD2+48>, b=<optimized out>, time=4294967295) at ../.././ChibiOS-RT/os/kernel/src/chqueues.c:335
#6  0x08001854 in chvprintf (chp=0x20000e20 <SD2>, fmt=0x800214b "m Expired! Set again.\n", fmt@entry=0x55555555 <Address 0x55555555 out of bounds>, ap=..., ap@entry=...)
    at ../.././ChibiOS-RT/os/various/chprintf.c:129
#7  0x08001a94 in chprintf (chp=<optimized out>, fmt=0x8002138 "Alarm Handler: Alarm Expired! Set again.\n") at ../.././ChibiOS-RT/os/various/chprintf.h:73
#8  0x08001af8 in rtc_alarm_handler (rtcp=0x20000e1c <RTCD1>, event=<optimized out>) at main.c:42
#9  0x080013f4 in Vector4C () at ../.././ChibiOS-RT/os/hal/platforms/STM32/RTCv1/rtc_lld.c:116
#10 <signal handler called>
#11 0x55555554 in ?? ()
#12 0x55555554 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)


I think that maybe I need to increase the stack size for whatever stack is being used by the callback. I tried adding #define PORT_INT_REQUIRED_STACK 128 to chconf.h, but this doesn't solve the problem. My question is, where should I be looking to modify the stacks appropriately, if that is the issue, and if it isn't, what should I be looking for?

Other code in main() just reads the rtc time, sys time and alarm, and prints to SD2 with chprintf. Also the typical example blinker thread is running. Let me know if more details are needed.

Thanks!
-Stephen
skute
Posts: 64
Joined: Wed Aug 29, 2012 10:17 pm

Re: Stack overflow from chprintf() in RTC callback

Post by skute »

You shouldn't be using chprintf within the callback as it's in an interrupt context. When the string is longer than the queue can accept it will block and attempt to context switch from an interrupt context which is not possible.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Stack overflow from chprintf() in RTC callback

Post by Giovanni »

Correct.

Please enable the "State Checker", assertions and checks in chconf.h,those will find all errors of this kind.

Giovanni
scdwyer
Posts: 2
Joined: Thu Nov 06, 2014 12:14 am

Re: Stack overflow from chprintf() in RTC callback

Post by scdwyer »

Ah, that makes sense. Thanks for the feedback. I will try all of the debug checks.
Post Reply