Hardfault

Resources: ese_general_examples\basics\hardfault

Goal

Use the debugger and the hardfault handler to solve the hardfault errors in this project.

Hardfaults

A hardfault exception is a special type of exception generated by the Cortex-M core. It is generally speaking not possible to recover from a hardfault exception. Examples of hardfault exceptions for the MCXA153 microcontrollers are:

  • Accessing a peripheral register when the clock to that peripheral has not been enabled.
  • Accessing a peripheral register when it is not released from reset.
  • Unaligned memory access – for example trying to read or write a 32-bit integer from an address that is not a multiple of four.

When a hardfault exception occurs, the Cortex-M core will generate a hardfault interrupt. The function prototype of this ISR is:

void HardFault_Handler(void);

The debugger can be used to verify if a hardfault has occurred, for example by setting a breakpoint in the hardfault ISR. As it is not possible to recover from a hardfault, hardfault ISRs often implement an endless loop.

Final assignment

The project contains three mistakes. Use the debugger as follows to solve the three mistakes:

  • Build the project
  • Start the debugger
  • Step through the code until the hardfault ISR is executed
  • What instruction caused the hardfault ISR to execute?
  • Stop the debugger
  • Solve the problem

Repeat the above steps until all problems are solved. When all problems are solved correctly, the green RGB LED blinks.