Hi All,
I am using EFM32GG11 cpu and have problem with malloc function. It crashes on the first call.
I have specified stack and heap areas in the scatter file.
I would like to debug through malloc function. I am using the standart C library linked to my keil MDK 5 project,
How could i build c runtime and link it to my project?
Thanks,
Define "crashes"
Where does the processor end up?
If you have a Hard Fault you're touching some memory oddly. Check that the memory described to the linker matches the capabilities of the part.
Check how memory is arranged in the .MAP file.
Make sure your SystemInit() code enables clocks, external memories, etc.
Use FromELF to disassemble the .AXF file so you can play along with the debugger. You can step into code where you lack source, it is not that hard.
Pretty sure Keil's code is solid, so issue is likely with structures, or initialization.
Also shouldn't be hard to figure the linked-list implementation, and walk the heap structures.
Check structure alignment, CM4 will fault on unaligned access with LDRD,STRD
Hello,
Thanks for reply. I have checked that HardFauld occur due to read at 0xFFFFFFFF address. Somehow this address is calculated inside malloc function. I currently far from being able to walk in disassembly and check what happens with linked list.
I have some troubles with scatter file. I found an example scatter file without heap definition.
I have defined ARM_LIB_HEAP variable.
ARM_LIB_HEAP 0x2007F000 EMPTY HeapSize {
}
There is a
RW_IRAM1 0x20000158 0x0007EEA8 { ; RW data .ANY (+RW +ZI) }
region for RAM.
I have added another region for heap
RW_IRAM2 0x2007F000 0xC00 { ; RW data .ANY (+RW +ZI) }
Still Hard fault.
I would like to find out how C runtime library initialize it's internal variables for heap base?
Maybe some init function must be called before C runtime?
My systeminit function is called and clocks are initialized, without malloc cpu is working fine.
The Heap/Stack are more usually defined in startup.s
;******************************************************************************* ; ; Amount of memory (in bytes) allocated for Stack ; Tailor this value to your application needs ; <h> Stack Configuration ; <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8> ; </h> Stack_Size EQU 0x0800 AREA STACK, NOINIT, READWRITE, ALIGN=3 Stack_Mem SPACE Stack_Size __initial_sp ; <h> Heap Configuration ; <o> Heap Size (in Bytes) <0x0-0xFFFFFFFF:8> ; </h> Heap_Size EQU 0x2000 AREA HEAP, NOINIT, READWRITE, ALIGN=3 __heap_base Heap_Mem SPACE Heap_Size __heap_limit ;*****************************************************************************
When using MicroLib, exporting
. EXPORT __initial_sp EXPORT __heap_base EXPORT __heap_limit
What the plumbing looks like
From .MAP HEAP 0x20002c50 Section 8192 startup_stm32l432xx.o(HEAP) ... __microlib_freelist 0x20000004 Data 4 mvars.o(.data) __microlib_freelist_initialised 0x20000008 Data 4 mvars.o(.data) ... __heap_base 0x20002c50 Data 0 startup_stm32l432xx.o(HEAP) __heap_limit 0x20004c50 Data 0 startup_stm32l432xx.o(HEAP) __initial_sp 0x20005450 Data 0 startup_stm32l432xx.o(STACK) FromELF Listing i.malloc malloc 0x08006f2c: b5f0 .. PUSH {r4-r7,lr} 0x08006f2e: 300b .0 ADDS r0,r0,#0xb 0x08006f30: 4d15 .M LDR r5,[pc,#84] ; [0x8006f88] = 0x20000008 0x08006f32: f0200107 ... BIC r1,r0,#7 0x08006f36: 2400 .$ MOVS r4,#0 0x08006f38: 4a14 .J LDR r2,[pc,#80] ; [0x8006f8c] = 0x20000004 0x08006f3a: e00f .. B 0x8006f5c ; malloc + 48 0x08006f3c: 6803 .h LDR r3,[r0,#0] 0x08006f3e: 428b .B CMP r3,r1 0x08006f40: d30b .. BCC 0x8006f5a ; malloc + 46 0x08006f42: 428b .B CMP r3,r1 0x08006f44: d905 .. BLS 0x8006f52 ; malloc + 38 0x08006f46: 1a5e ^. SUBS r6,r3,r1 0x08006f48: 1843 C. ADDS r3,r0,r1 0x08006f4a: 6847 Gh LDR r7,[r0,#4] 0x08006f4c: e9c36700 ...g STRD r6,r7,[r3,#0] 0x08006f50: e000 .. B 0x8006f54 ; malloc + 40 0x08006f52: 6843 Ch LDR r3,[r0,#4] 0x08006f54: 6013 .` STR r3,[r2,#0] 0x08006f56: c002 .. STM r0!,{r1} 0x08006f58: e004 .. B 0x8006f64 ; malloc + 56 0x08006f5a: 1d02 .. ADDS r2,r0,#4 0x08006f5c: 6810 .h LDR r0,[r2,#0] 0x08006f5e: 2800 .( CMP r0,#0 0x08006f60: d1ec .. BNE 0x8006f3c ; malloc + 16 0x08006f62: 4620 F MOV r0,r4 0x08006f64: 2800 .( CMP r0,#0 0x08006f66: d102 .. BNE 0x8006f6e ; malloc + 66 0x08006f68: 6828 (h LDR r0,[r5,#0] 0x08006f6a: b108 .. CBZ r0,0x8006f70 ; malloc + 68 0x08006f6c: 2000 . MOVS r0,#0 0x08006f6e: bdf0 .. POP {r4-r7,pc} 0x08006f70: 4a06 .J LDR r2,[pc,#24] ; [0x8006f8c] = 0x20000004 0x08006f72: 4807 .H LDR r0,[pc,#28] ; [0x8006f90] = 0x20002c54 0x08006f74: 6010 .` STR r0,[r2,#0] 0x08006f76: 4a07 .J LDR r2,[pc,#28] ; [0x8006f94] = 0x20004c50 0x08006f78: 1a12 .. SUBS r2,r2,r0 0x08006f7a: f0220207 "... BIC r2,r2,#7 0x08006f7e: e9c02400 ...$ STRD r2,r4,[r0,#0] 0x08006f82: 2001 . MOVS r0,#1 0x08006f84: 6028 (` STR r0,[r5,#0] 0x08006f86: e7d7 .. B 0x8006f38 ; malloc + 12 $d 0x08006f88: 20000008 ... DCD 536870920 __microlib_freelist_initialised 0x08006f8c: 20000004 ... DCD 536870916 __microlib_freelist 0x08006f90: 20002c54 T,. DCD 536882260 __heapbase + 4 0x08006f94: 20004c50 PL. DCD 536890448 __heaplimit $t
View all questions in Keil forum