Tactif CIE
Well-known member
I'm trying to implement a fault handler in an app outside of Teensyduino or Platform.io, everything being compiled from Makefile.
I borrowed some code snippets and info but can't get a working solution
The relevant parts of my main.cpp file are :
I've several problems :
1) in mk20dx128.c all the fault handlers are weak aliased to fault_isr() function - So my own fault_isr() should take precedence to the one provided by teensy core - This is not the case, I only get some handling when defining
but never with
BTW, fault_isr is not defined as a weak symbol - Is it the reason my function not being called ?
2) Is the initialization of SCB_SHCSR correct ? Is SCB_SHCSR the right register ?
3) I don't really understand in which conditions (which faults) the handler would be called - Tried to understand from the Cortex M4 documentation but it's still very unclear to me
Side note : in the above code, the 'term' variable is only an instance of a custom class deriving from usb_serial_class
I borrowed some code snippets and info but can't get a working solution
The relevant parts of my main.cpp file are :
Code:
#define SCB_SHCSR_USGFAULTENA (uint32_t)1 << 18
#define SCB_SHCSR_BUSFAULTENA (uint32_t)1 << 17
#define SCB_SHCSR_MEMFAULTENA (uint32_t)1 << 16
void __attribute__((naked)) fault_isr(void) {
uint32_t *sp = 0;
asm volatile("TST LR, #0x4\n\t" // Test EXC_RETURN number in LR bit 2
"ITE EQ\n\t" // if zero (equal) then
"MRSEQ %0, MSP\n\t" // Main Stack was used, put MSP in sp
"MRSNE %0, PSP\n\t" // else Process stack was used, put PSP in sp
: "=r"(sp)
:
: "cc");
term.println("*** Crashed ***");
term.print("PC=");
term.print(sp[6], 16); // 6 * 4 = 24 -> PC
term.print(" LR=");
term.print(sp[5], 16); // 5 * 4 = 20 -> ??
term.print(" make crash");
term.println();
term.flush();
while (1) {
// keep polling some communication while in fault
// mode, so we don't completely die.
if (SIM_SCGC4 & SIM_SCGC4_USBOTG)
usb_isr();
if (SIM_SCGC4 & SIM_SCGC4_UART0)
uart0_status_isr();
if (SIM_SCGC4 & SIM_SCGC4_UART1)
uart1_status_isr();
if (SIM_SCGC4 & SIM_SCGC4_UART2)
uart2_status_isr();
}
}
extern "C" int main(void) {
SCB_SHCSR |= SCB_SHCSR_BUSFAULTENA | SCB_SHCSR_USGFAULTENA | SCB_SHCSR_MEMFAULTENA;
setup();
while (1) {
loop();
yield();
}
}
I've several problems :
1) in mk20dx128.c all the fault handlers are weak aliased to fault_isr() function - So my own fault_isr() should take precedence to the one provided by teensy core - This is not the case, I only get some handling when defining
Code:
void __attribute__((naked)) hard_fault_isr(void)
Code:
void __attribute__((naked)) fault_isr(void)
2) Is the initialization of SCB_SHCSR correct ? Is SCB_SHCSR the right register ?
3) I don't really understand in which conditions (which faults) the handler would be called - Tried to understand from the Cortex M4 documentation but it's still very unclear to me
Side note : in the above code, the 'term' variable is only an instance of a custom class deriving from usb_serial_class