SparkFun Forums 

Where electronics enthusiasts find answers.

Everything ARM and LPC
By bobbyc
#134020
I'm working with the LPCXpresso with the LPC1769 and writing some fairly basic C code using the LPCXpresso tool (Eclipse port) and I'm running into an odd problem. I have ensured that my configurations seem correct for the LPC1769 (target MCU is set, memory segments seem reasonable, etc). I'm seeing what at first appeared to be sporadic Hard Faults. The debugger breaks as soon as the Hard Fault handler is entered and I see the following register values (I only posted a relevant subset of values):

R0: 0xfff7f38
R1: 0x10007f48
SP: 0x0fff7f18
LR: 0xfffffff9
CGSR: 0x1000 (Stack Error)
Faults: StkErr

After tracing back through the assembly things seem to be running well until I hit the following lines (which represent a call to function "send_client_build_version", which takes no parameters and has no return value):

send_client_build_version:
00000a08: ...t_build_version+0 push {lr}
00000a0a: ...t_build_version+2 sub.w sp, sp, #65536 ; 0x10000 <-- this line seems to be at fault
00000a0e: ...t_build_version+6 sub sp, #4

For reference, the values of SP before and after 0a0a (sub.w line above) are included below:
SP(before 0a0a): 0x10007f40 (well within the SRAM range)
SP(after 0a0a): 0xFFF7F41 (well outside the SRAM range)

Of course, once the SP is set to 0xFF7F41 (which is in a reserved area) any attempt to push onto the stack results in a Hard Fault.

What could cause a pretty basic function call to result in such an obviously destructive line of code?

Thanks in advance
By bobbyc
#134061
I figured out what was going on, and I'm embarrassed to say it was exactly what one would expect (but with a bit of a twist).

One of the libraries I was using allocated a very large data segment (exactly 65536 bytes in fact), which was being allocated on the stack when this function was called. That's the straightforward part. The twist is that for most of the time I was running this code, this structure was being optimized out by the compiler. When I added seemingly unrelated functionality (for some reason I haven't been able to figure out) the compiler stopped optimizing out this structure, even though it wasn't being used.