Page 1 of 2

chThdCreateFromHeap migration issue

Posted: Fri Jun 09, 2017 9:12 pm
by omcdr
I'm trying to migrate my project from ChibiOS161 to latest ChibiOS GitHub commit.
In ChibiOS161 my shell task was working correctly, but in latest commit I got hardfault in chThdCreateFromHeap function.

It was

Code: Select all

cmd_console.threadp = chThdCreateFromHeap(NULL, CONSOLE_WA_SIZE, NORMALPRIO, cmd_console_thd, (void *) &cmd_console);

now is

Code: Select all

cmd_console.threadp = chThdCreateFromHeap(NULL, CONSOLE_WA_SIZE, "cmd", NORMALPRIO, cmd_console_thd, (void *) &cmd_console);


I got hardfault in chThdCreateFromHeap chSysLock() line.
CH_CFG_USE_DYNAMIC and CH_CFG_USE_HEAP are set to TRUE.

What is difference ? How to migrate my cmd task ?

Re: chThdCreateFromHeap migration issue

Posted: Sat Jun 10, 2017 9:23 am
by Giovanni
No enough info, step into the code and see what is causing the fault.

That function works in the test suite.

Giovanni

Re: chThdCreateFromHeap migration issue

Posted: Wed Jun 14, 2017 6:19 am
by omcdr
cmd_console thread is similar to THD_FUNCTION(console, arg) in this project, line 62:
https://github.com/NicoHub/hydrafw/blob/master/main.c

I have checked step by step debug, I always got hardfault at chSysLock() line in chThdCreateFromHeap function.
Do you have idea why older chThdCreateFromHeap function works without hardfault ?

Re: chThdCreateFromHeap migration issue

Posted: Wed Jun 14, 2017 7:46 am
by Giovanni
You need to step in asm code and see which instruction causes it. It does not happen here. Also enable debug functions in chconf.h.

Giovanni

Re: chThdCreateFromHeap migration issue

Posted: Wed Jun 14, 2017 4:24 pm
by omcdr
With step by step debug I got chSysHalt("SV#4"); in _dbg_check_lock(void) function with reason 'S'

Struct ch.dbg has values:

Code: Select all

panic_msg   0x55555555    0x20000D48   char const *   
isr_cnt   0x55555555   0x20000D4C   cnt_t   
lock_cnt   0x55555555   0x20000D50   cnt_t[code]   


Does that explain crash ?

Re: chThdCreateFromHeap migration issue

Posted: Wed Jun 14, 2017 6:48 pm
by Giovanni
Hi,

If the system is halted then it is not a crash, there is a difference. #SV4 means that chSysLock() has been called twice, this triggers the state checker.

See here: http://www.chibios.org/dokuwiki/doku.ph ... ebug_guide

Do you have ISRs in you application? if so then make sure to sue legal priority levels 3..15.

Giovanni

Re: chThdCreateFromHeap migration issue

Posted: Fri Jun 16, 2017 9:20 pm
by omcdr
I have disabled all IRQ.

Now my code is simple:

Code: Select all

#define THD1_WA_SIZE THD_WORKING_AREA_SIZE(2048)
THD_FUNCTION(thd1, arg)
{
   while (TRUE)
   {
      LED3_ON;
      chThdSleepMilliseconds(10);
      LED3_OFF;
      chThdSleepMilliseconds(1000);
   }
}

int main(void)
{
   halInit();
   chSysInit();

   thread_t *tp = chThdCreateFromHeap(NULL, THD1_WA_SIZE, "thd1", NORMALPRIO + 3, thd1, (void *) 0);

   while (TRUE)
   {
      chThdSleepMilliseconds(1000);
   }
}


I got chSysHalt 0x800A898 "SV#4" with reason 'S'

I'm using IAR compiler. Is it IAR problem ?

Re: chThdCreateFromHeap migration issue

Posted: Sat Jun 17, 2017 4:52 pm
by omcdr
This code with GCC 4.9 compiler works correctly.
How to fix it for IAR compiler ?

Re: chThdCreateFromHeap migration issue

Posted: Mon Jun 26, 2017 7:55 pm
by omcdr
I'm trying to run this dynamic thread with chThdCreateFromMemoryPool

Code: Select all

#define MEMORY_POOL_SIZE 1024
static uint32_t data[MEMORY_POOL_SIZE];
static memory_pool_t mp1;

chPoolObjectInit(&mp1, MEMORY_POOL_SIZE, NULL);
chPoolFree(&mp1, &data[0]);
cmd_console.threadp = chThdCreateFromMemoryPool(&mp1, "cmd", NORMALPRIO + 5, cmd_console_thd, (void *) &cmd_console);
chThdWait(cmd_console.threadp);


I got chSysHalt in chThdCreateFromMemoryPool, cmpiled under IAR compiller
reason 0x800D0E0 "chThdCreateSuspendedI" R4 char const *
'c' (0x63) 0x0800D0E0 char


Do you have an idea how to fix it for IAR compiller?

Re: chThdCreateFromHeap migration issue

Posted: Mon Jun 26, 2017 8:02 pm
by Giovanni
The problem I can see is that you are using MEMORY_POOL_SIZE both as the size of an array of uint32_t and as the size in bytes expected by the pool function.

You need to check which condition fails in chThdCreateSuspendedI(), most likely you have an alignment problem. See in the test code where chThdCreateFromMemoryPool() is used, your example is right there.

Does the test suite succeed on your board using IAR?

Giovanni