凡是在Cortex M系列内核上写过程序的程序员, 没有不知道Hard Fault的. 大多数程序出现问题的表现就是进入Hard Fault.
但是进入Hard Fault的原因是甚么, 为了查明这个问题, 不少程序员为之废寝忘食...
Hard Fault的设计初衷就是为了程序出错之后提供一个预警机制, 但是进入Hard Fault的原因有很多. 所以把这个问题研究透对调试排错意义很大.
相比于其他型号, Cortex M0只有一种Fault就是Hard Fault. 本帖以几个实验稍稍探究一下子发生Fault的原因.
首先看手册, Cortex M0发生Hard Fault的原因有如下几种:
Faults are a subset of exceptions, see Exception model on page 2-19. All faults result in
the HardFault exception being taken or cause lockup if they occur in the NMI or
HardFault handler. The faults are:
? execution of an SVC instruction at a priority equal or higher than SVCall
? execution of a BKPT instruction without a debugger attached
? a system-generated bus error on a load or store
? execution of an instruction from an XN memory address
? execution of an instruction from a location for which the system generates a bus fault
? a system-generated bus error on a vector fetch
? execution of an Undefined instruction
? execution of an instruction when not in Thumb-State as a result of the T-bit being previously cleared to 0
? an attempted load or store to an unaligned address.