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.

Parents Reply Children
  • Let me try to ask the question differently.

    I have the template example from the gcc folder. The ordinary one, not the ble template which is really quite full featured, just the one with main.c, system_nrf51.c and gcc_startup_nrf51.s. Those three, from the SDK, nothing else.

    If I compile that, link it and load it on a blank chip at 0x00000000, it runs, you can debug into main().

    if I compile that same thing, link it up at 0x14000 (using one of the other templates in the SDK with the correct Flash and RAM settings), load on a soft device (110) and then load that code at 0x14000, it HardFaults.

    I don't understand why. I have debugged into the Reset_Handler, it's running, it powers up the RAM, it starts copying, it's just running the Reset_Handler code in gcc_startup_nrf51.s as normal. If you single step it all the way (a 20 minute job) to main(), it gets to main(), normally. If you let it run, it doesn't ever get there, it ends up in the HardFault handler.

  • Could you please update your question and share the project and files that you're working with? I can't say I've seen this when I've worked with GCC myself, so I'm having some trouble understanding what might be the cause of it.

  • I'm interested in this thread as I see the same thing (or something very similar) myself. When I let things run with no breakpoints, I see the debugger stop with a signal SIGTRAP 0xfffffffe in ?? ()

    The call stack shows Reset_Handler(). The line that somehow changes the program counter to 0xfffffffe is the BLX R0, where the previous line is LDR R0, =SystemInit

    It seems straightforward so I'm puzzled as to what's happening. The softdevice is loaded and the application is correctly linked such that it resides at 0x14000 and doesn't use RAM below 0x20002000.

  • Have you tried running gdb from the command line? I often find this a usable first step, since Eclipse often seems to complicate things. Also, you can try taking a look on this: https://devzone.nordicsemi.com/index.php/eclipse,-gdb,-macos-so-close

    If you have further problems, can you please share the complete project you're working with, so I can test it here?

  • 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

Related