chThdCreateFromHeap migration issue

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

Moderators: RoccoMarco, lbednarz, tfAteba

omcdr
Posts: 89
Joined: Wed Aug 17, 2016 3:25 pm
Has thanked: 7 times
Been thanked: 7 times

chThdCreateFromHeap migration issue

Post 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 ?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chThdCreateFromHeap migration issue

Post by Giovanni »

No enough info, step into the code and see what is causing the fault.

That function works in the test suite.

Giovanni
omcdr
Posts: 89
Joined: Wed Aug 17, 2016 3:25 pm
Has thanked: 7 times
Been thanked: 7 times

Re: chThdCreateFromHeap migration issue

Post 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 ?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chThdCreateFromHeap migration issue

Post 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
omcdr
Posts: 89
Joined: Wed Aug 17, 2016 3:25 pm
Has thanked: 7 times
Been thanked: 7 times

Re: chThdCreateFromHeap migration issue

Post 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 ?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chThdCreateFromHeap migration issue

Post 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
omcdr
Posts: 89
Joined: Wed Aug 17, 2016 3:25 pm
Has thanked: 7 times
Been thanked: 7 times

Re: chThdCreateFromHeap migration issue

Post 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 ?
omcdr
Posts: 89
Joined: Wed Aug 17, 2016 3:25 pm
Has thanked: 7 times
Been thanked: 7 times

Re: chThdCreateFromHeap migration issue

Post by omcdr »

This code with GCC 4.9 compiler works correctly.
How to fix it for IAR compiler ?
omcdr
Posts: 89
Joined: Wed Aug 17, 2016 3:25 pm
Has thanked: 7 times
Been thanked: 7 times

Re: chThdCreateFromHeap migration issue

Post 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?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chThdCreateFromHeap migration issue

Post 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
Post Reply