github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/runtime/runtime_cortexm_hardfault.go (about)

     1  //go:build atsamd21 || nrf51
     2  
     3  package runtime
     4  
     5  import (
     6  	"unsafe"
     7  )
     8  
     9  // This function is called at HardFault.
    10  // Before this function is called, the stack pointer is reset to the initial
    11  // stack pointer (loaded from addres 0x0) and the previous stack pointer is
    12  // passed as an argument to this function. This allows for easy inspection of
    13  // the stack the moment a HardFault occurs, but it means that the stack will be
    14  // corrupted by this function and thus this handler must not attempt to recover.
    15  //
    16  // For details, see:
    17  // https://community.arm.com/developer/ip-products/system/f/embedded-forum/3257/debugging-a-cortex-m0-hard-fault
    18  // https://blog.feabhas.com/2013/02/developing-a-generic-hard-fault-handler-for-arm-cortex-m3cortex-m4/
    19  //
    20  //export handleHardFault
    21  func handleHardFault(sp *interruptStack) {
    22  	print("fatal error: ")
    23  	if uintptr(unsafe.Pointer(sp)) < 0x20000000 {
    24  		print("stack overflow")
    25  	} else {
    26  		// TODO: try to find the cause of the hard fault. Especially on
    27  		// Cortex-M3 and higher it is possible to find more detailed information
    28  		// in special status registers.
    29  		print("HardFault")
    30  	}
    31  	print(" with sp=", sp)
    32  	if uintptr(unsafe.Pointer(&sp.PC)) >= 0x20000000 {
    33  		// Only print the PC if it points into memory.
    34  		// It may not point into memory during a stack overflow, so check that
    35  		// first before accessing the stack.
    36  		print(" pc=", sp.PC)
    37  	}
    38  	println()
    39  	abort()
    40  }