This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

LP-AM243: Printing the Stack Frame

Part Number: LP-AM243
Other Parts Discussed in Thread: SYSBIOS

In order to understand where Usage fault came from and not just end in a while(1), we want to be able to print the Stack Frame whenever this exception occurs.

This can be good when debugging outside of CCS, when a customer wants to see the core registers via the console i.e.

The steps that I wanted to implement are:

1. Simulate a fault (I simulated a usage fault)

2. Read the Main Stack Pointer

3. Print the stack frame

This is how I simulated an Usage fault on the M4F core:

uint32_t* pADDR = 0x20000001;

*pADDR = 0xFFFFFFFF;

void (*function_pointer) (void);

function_pointer = pADDR;

function_pointer(); // Here I ended up in Usage fault due the execution of undefined instruction

------------------------------------

at this point, I ended up at:

void HWI_SECTION HwiP_hardFault_handler()
{
volatile uint32_t loop = 1;
while(loop)
;
}

Inside this handler function I wanted to print the Stack Frame that includes all relevant registers that the processor stacked during the move from Thread mode to Handler mode.

The thing is, if I print the Stack Frame from the point I entered to the HwiP_hardFault_handler() function, I will get a wrong stack value. The reason is that MSP will be set to a different value due to the Prologue/ preamble phase of the C function by the  Compiler whenever I reached the handler function.

To avoid that, I can use the ((naked)) attribute  - for this attribute the compiler does not generate prologue and epilogue sequences.

Thus the sequence will be as followed: 

Whenever an hard fault occurs-> I'll go to a ((naked))HwiP_hardFault_handler written in assembly -> I will save the value of the MSP in R0 register -> call the C version of HwiP_hardFault_handler() from the Assembly handler  -> Print the correct stack frame inside the C function -> go to while(1)

 

My question is: Is this something we can do? I was able to easily implement it on ST Nucleo MCU (CM4F) and I wonder if this is something we can implement it on the M4F core of the Sitara by changing the handler of the SDK? 

Here is my implementation on the ST device: