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

Doing stack dump with nRF51

Is it possible to get the state of the uC without being connected to it at the moment of crash?

  • That rather depends by what you mean by crash. If you're going to your error handler or ending up in the hardfault handler, then yes you just implement an error handler or hardfault handler which finds the stack, gets the registers and dumps the last state of them to

    1. RAM if the chip will stay powered for long enough for you to put a debugger on it
    2. flash if you're going to power it down and reconnect to it later.

    If you have a loop-forever error handler, you may not even need to save state, you can connect to the chip whilst it's busy-looping and read the state right from where it left off.

    If you're going to write state to flash then you don't want to use ANY of the library code, just write the registers to clear the pages you want and write them again to dump the data.

    1. Is there sample code somewhere for getting the state of the registers and stack state out from a hardfault?
    2. How exactly can I see where the code is being executed when connecting to an already-running uC?
    1. There's quite a few online - it's usually a bit of assembler jumping to a piece of C. All it's really doing is figuring out which stack was in-use at the time of the fault and pulling the registers from there in order. Google ARM Hardfault Handler to start with. They all need a bit of customization but the principal is simple.

    2. Connect. Break. See where you are. With JLink from the command line that's as easy as typing halt followed by regs. A full IDE debugger will do a little better but the principle is the same.

  • Could you go into the second part a bit more? How exactly do I "connect"? Just starting a new debug session restarts the processor.

Related