1
votes

I'm trying to get a C++ project to work on a cortex-m0 processor (the nRF51822 to be specific). I'm using codesourcery lite g++ 2013.05.23. I'm using the clib that comes with codesourcery (newlib) and the CS3 c init routine __cs3_start_c. I've rewritten the __cs3_restart, and __cs3_start_asm functions.

I can get as far as the clib _init function, which is called from something like __libc_init_array. When I execute the instruction at address 00012388, the processor faults.

Since this instruction is a pop, I immediately suspected the stack pointer was invalid; however, the register values captured before the fault indicate otherwise. Any ideas why the processor is faulting? What can I do to init the C++ environment so that I can start running code?

_init:
00012384:  _init+0                push {r3, r4, r5, r6, r7, lr}
00012386:  _init+2                nop ; (mov r8, r8)
00012388:  _init+4                pop {r3, r4, r5, r6, r7}
0001238a:  _init+6                pop {r3}
0001238c:  _init+8                mov lr, r3
0001238e:  _init+10               bx lr

Register Values:

Register       Val: Hex     Val: Dec
r0             0x20008d8    33556696
r1             0x123c0  74688
r2             0xa68    2664
r3             0x2001340    33559360
r4             0x0  0
r5             0x12390  74640
r6             0x0  0
r7             0x12c90  76944
r8             0xffffffff   4294967295
r9             0xffffffff   4294967295
r10            0xffffffff   4294967295
r11            0xffffffff   4294967295
r12            0xffffffff   4294967295
sp             0x2003fb8    0x2003fb8
lr             0xff89   65417
pc             0x12388  0x12388 <_init+4>
xpsr           0x41000003   1090519043
MSP            0x2003fb8    33570744
PSP            0xfffffffc   4294967292
PRIMASK        0x0  0
BASEPRI        0x0  0
FAULTMASK      0x0  0
CONTROL        0x0  0

The code I'm trying compile is the following I don't have any statically allocated classes so I don't quite understand why this code is even running.

Main.cpp
volatile int i = 4;
volatile int j = 0;
volatile int k;
int main(void)
{
    for(;j< i; j++)
    { 
    k = k +2;
    }

TestClass * tc = new TestClass(3);
while(1){};
}

Testclass.h
class TestClass {
public:
    int i;
    TestClass(int num);
    virtual ~TestClass();
};

TestClass.cpp
TestClass::TestClass(int num) {
    this->i = num;
}

TestClass::~TestClass() {
}

Thanks!

1
Where is the C++ code? Do you have any variables that require initialization at startup (i.e. globals)? If so one of them could be the culprit.Captain Obvlious
does int main() { while(1) {}; } work for you?old_timer

1 Answers

2
votes

Sorry to trouble everyone, figured out the issue. The stack pointer was bad. The top of the stack should have been set to 0x20040000, it was actually set to 0x02040000. I'm just surprised the code made it as far as it did without a fault.

Thanks again.