This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Why does a do-nothing main.c HardFault when run with a softdevice?

If you have the most trivial main.c possible, with a main() which does nothing at all, compile, link and load it at 0x00000000, it runs fine and does nothing .. that's basically the main.c from the template example.

If you build the same exact code, link it at 0x14000, load a 110 softdevice onto the chip and then flash the code at 0x14000, the device HardFaults. It HardFaults before it even gets to main().

Why is this? I assume that the Softdevice requires one or more of the low-level handlers to be defined and if they're not, ends up branching to nowhere and HardFaulting .. but which are they? It can't be anything which is actually done in main(), it never gets there, so it must be named, linked-in routines which handle interrupts while the Reset_Handler() code is running.

I've debugged it at length. The code always makes it into the Reset_Handler() [at 0x14xxx], it rarely gets out of it. Somewhere during the copy to RAM it HardFaults. If you single step it all the way through, which takes a good long while, it makes it to main(), but if you let it run, it immediately fails. I assume an interrupt the softdevice set up before it branched to the user code Reset_Handler() fires and fails to find something it needs.

The reason I'm trying to do something so stupid is to come up with the simplest template do-nothing main.c which works with or without a loaded softdevice. This is part of the, currently going fairly well, work I'm doing to build natively with Eclipse. I just need the simplest main.c I can which works with or without a softdevice as an example file.

  • I must admit that I've never used gdb on the command line - at least not in Windows. I'm going to come back to this a bit later after I make sure I understand the ble_app_lbs app a bit better and do some debugging via a scope. I hate to give up on using gdb for long, but the boss doesn't want to let me spend the time to get it working properly at this point. sigh

  • Running gdb on the command line is easy, and shouldn't take you more than 3 minutes:

    1. Start the GDB server, make sure to select the correct device, interface and interface frequency (nRF51822, SWD, 1000 kHz).
    2. Open a shell of some kind.
    3. Navigate to your project folder (i.e. where you have your .out/.elf file).
    4. Start GDB, passing the file as an argument (arm-none-eabi-gdb _build/ble_app_hrs_s110.out or similar).
    5. Once GDB has started, connect to the GDB server by writing "target remote localhost:2331" and pressing enter.

    Also, did you try out the Eclipse trick I linked to above? Should be worth a try at least. :)

  • I saw Jim's suggestion that worked for him, switching from the DSF to the Standard GDB launcher. I tried that and it reported "Error creating session". I was worth a try, I agree. I'll give the command-line gdb a try. Thanks

  • I cam back to try command-line GDB connecting to the J-link GDB server today. I am able to connect, but I get similar problems:

    (gdb) break main Breakpoint 1 at 0x14774: file barebones.c, line 265. (gdb) target remote localhost:2331 Remote debugging using localhost:2331 Error in re-setting breakpoint 1: Cannot access memory at address 0x14774 0x00000000 in ?? () (gdb) bt #0 0x00000000 in ?? () #1 0x00000000 in ?? () (gdb)

    I'll take you up on the offer to look at my project. What's the best way to share a project? Zip it up and attach it to one of these posts? I'll do that.

  • Here's the ble_app_lbs project that I modified all zipped up. It should include the eclipse project, the makefile it's built with, and all source except a few SDK includes. I'm using SDK 4.4 and BLE stack v5 for this one.

    Let me know if there's anything else I can provide. This one has me a bit stumped right now.

    The Makefile is hacked from another project and something is messed up with dependencies such that a clean is necessary. The following sequence works for me:

    make clean make debug (or make release)

    ble_app_lbs.zip

Related