Page 2 of 3

Re: HardFault and debug with messagebox

Posted: Tue Feb 21, 2017 9:35 pm
by omcdr
I'm testing my code, sometimes I got HardFault, sometimes chSysHalt with reason

Name : reason
Details:0x8016ae0 <__func__.5691.lto_priv.256> "chSysUnlock"
Default:0x8016ae0 <__func__.5691.lto_priv.256> "chSysUnlock"
Decimal:134310624

Name : *reason
Details:99 'c'


Will it be helpful in finding issue in my code ? Could you explain this reason ?

Re: HardFault and debug with messagebox

Posted: Tue Feb 21, 2017 9:43 pm
by Giovanni
Probably you have a call to an I-class function and do not perform a reschedule before reaching chSysUnlock(), priorities are not in correct order thus the assertion, however I don't see this in the code you posted.

Giovanni

Re: HardFault and debug with messagebox

Posted: Wed Feb 22, 2017 12:01 pm
by omcdr
At this moment I'm testing simple thread with uart3 print. Uart3 is used in this thread only. I don't use I-class function.
Here is my test code, only one from 4 buff operation is active in test.
I got hardfault or pannic with sprintf function only, I have test another lite sprintf version like xsprintf with the same issue.

Code: Select all

char buff[40] = "buf init                  \n\r";

void uart3Print(const char *string) {
   uartStartSend(&UARTD3, strlen(string), string);
}

void get_dec_str(uint8_t* str, size_t len, uint32_t val)
{
   uint8_t i;
   for (i = 1; i <= len; i++)
   {
      str[len - i] = (uint8_t) ((val % 10UL) + '0');
      val /= 10;
   }
}

THD_WORKING_AREA(waThreadDump, 256);
THD_FUNCTION(threadDump, arg)
{
   int x = 1, y = 2, z = 3;

   while (1)
   {
      buff[9] = x++; //works
      get_dec_str(&buff[11], 3, x++); //works

      sprintf(buff, 15, "buf sprintf %d \n\r", x); //HardFault
      
      sprintf(buff, 38, "OUT: 1:%d 2:%d 3:%d\n\r", x, y, z); //panic
      
      uart3Print(buff);
      chThdSleepMilliseconds(5000);
   }
}

Re: HardFault and debug with messagebox

Posted: Wed Feb 22, 2017 12:04 pm
by Giovanni
You need to provide information about the system when the halt happens, stack traces would help.

Giovanni

Re: HardFault and debug with messagebox

Posted: Wed Feb 22, 2017 8:51 pm
by omcdr
Here is my stack trace

stack.png

Re: HardFault and debug with messagebox

Posted: Wed Feb 22, 2017 9:10 pm
by Giovanni
Hi,

The problem happened before the call to uartStartSend() probably, try placing:

chSysLock();
chsysUnlock();

just before calling uartStartSend(), if I am right the assertion will be triggered in the unlock.

Giovanni

Re: HardFault and debug with messagebox

Posted: Wed Feb 22, 2017 9:40 pm
by omcdr
With code

Code: Select all

   chSysLock();
   chSysUnlock();
   uartStartSend(&UARTD3, strlen(string), string);


I got Image

with

Code: Select all

   chSysLock();
   uartStartSend(&UARTD3, strlen(string), string);

I got
Image

and with code

Code: Select all

   chSysLock();
   uartStartSend(&UARTD3, strlen(string), string);
   chSysUnlock();   

Image

Re: HardFault and debug with messagebox

Posted: Wed Feb 22, 2017 10:04 pm
by Giovanni
Only the first one is relevant, it shows that the problem is not there, just detected there.

You must have an I-class function called somewhere in a thread (not ISR) not followed by a reschedule or a more hidden problem.

Are you IRQs initialized at correct priority levels? 0 and 1 are forbidden.

Giovanni

Re: HardFault and debug with messagebox

Posted: Thu Feb 23, 2017 9:50 pm
by omcdr
My IRQs have 3 and more priority levels.
I have only one thread.

This thread works, I have added delays.

Code: Select all

char buff[40] = "buf start                 \n\r";
THD_WORKING_AREA(waThreadDump, 256);
THD_FUNCTION(threadDump, arg)
{
   int x = 1, y = 2, z = 3, v = 4, w = 5;

   while (1)
   {
      uart3Print(buff);
      chThdSleepMilliseconds(500);
      sprintf(buff, "OUT: 1:%d 2:%d 3:%d 4:%d 5:%d\n\r", x++, y++, z, v, w);
      chThdSleepMilliseconds(1000);
   }
}


If I start second thread with led blink

Code: Select all

THD_WORKING_AREA(waThreadLed, 128);
THD_FUNCTION(threadLed, arg)
{
   while (1)
   {
      LED1_ON;
      chThdSleepMilliseconds(10);
      LED1_OFF;
      chThdSleepMilliseconds(1000);
   }
}


I got hardfault with call stack:
Image

Re: HardFault and debug with messagebox

Posted: Fri Feb 24, 2017 9:02 am
by Giovanni
Which compiler are you using? those are completely random problems and I see nothing obvious in your code.

Giovanni