Kinetis MCU bugs when using malloc and structure, go to Default ISR

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Kinetis MCU bugs when using malloc and structure, go to Default ISR

ソリューションへジャンプ
1,190件の閲覧回数
samc
Contributor III

Hello,

 

I use a MKL17Z256 Kinetis MCU and I have a problem with a code with dynamic allocation.

 

All is OK when I use this code :

 

typedef struct

{

       unsigned char Test_U8;

       unsigned short Test_U16;

}test_str;

 

test_str Test_STR;

test_str * PointeurTest_STR = &Test_STR;

 

PointeurTest_STR->Test_U8 = 0;

PointeurTest_STR->Test_U16 = 0;

 

But MCU buggs when I use this one:

 

typedef struct

{

     unsigned char Test_U8;

     unsigned short Test_U16;

}test_str;

 

test_str* Test_STR = malloc(sizeof(test_str));

 

Test_STR->Test_U8 = 0;

Test_STR->Test_U16 = 0;

 

The problem is the MCU fall in defaultISR in startup:MKL.....S file

 

DefaultISR:
    ldr    r0, =DefaultISR
    bx r0

 

What can cause this problem ?

ラベル(1)
0 件の賞賛
返信
1 解決策
1,063件の閲覧回数
bobpaddock
Senior Contributor III

Check the compiler/link options.  Replace -Os with -O2 if you are using GCC.

The L family does not deal with unaligned accesses and the 'Small' model saves space by not aligning things.

This can make the code look like random faults as things are moved around.  Sometimes things end up aligned by accident and it will work.

Indirectly related it, in a structure it is better to put things from the largest to the smallest, that is reverse the unsigned char and unsigned short (better to use <stdint.h> types actually) in the test structure.  Placing things from small to big can waste a lot of memory due to hidden structure alignment bytes.  There are compiler options to pack the structures.

Also check the linker script to see how much heap space is allocated.  Some of the default ones allocate no heap space and the stack collides with things leading to faults.

Perhaps this is why it looks like the heap is at zero in the IDE.

元の投稿で解決策を見る

0 件の賞賛
返信
3 返答(返信)
1,063件の閲覧回数
samc
Contributor III

I forget, the problematic lines are :

Test_STR->Test_U8 = 0;

Test_STR->Test_U16 = 0;

But I think to see the problem, the result of the allocation is on the ISR vectors

Malloc_issue.jpg

How can I avoid this ?

0 件の賞賛
返信
1,064件の閲覧回数
bobpaddock
Senior Contributor III

Check the compiler/link options.  Replace -Os with -O2 if you are using GCC.

The L family does not deal with unaligned accesses and the 'Small' model saves space by not aligning things.

This can make the code look like random faults as things are moved around.  Sometimes things end up aligned by accident and it will work.

Indirectly related it, in a structure it is better to put things from the largest to the smallest, that is reverse the unsigned char and unsigned short (better to use <stdint.h> types actually) in the test structure.  Placing things from small to big can waste a lot of memory due to hidden structure alignment bytes.  There are compiler options to pack the structures.

Also check the linker script to see how much heap space is allocated.  Some of the default ones allocate no heap space and the stack collides with things leading to faults.

Perhaps this is why it looks like the heap is at zero in the IDE.

0 件の賞賛
返信
1,063件の閲覧回数
samc
Contributor III

It wasn't on -Os but -O0, but I changed it to -O2.

I saw Heap Size was at 0x0000 and Heap Stack was at 0x0400. So I changed heap size to 0x0400 and it works well. I just saw in another post that change this heap size resolved the problem. Effectively it works.

I noted your advise about structure configuration.

Thank you,

Sam

0 件の賞賛
返信