How to Enable or Disable a System Exception of the Cortex-M4 Processor in an STM32F407G Discovery Board in C

STM32F407G discovery board



In this article, we show how to enable or disable a system exception of the cortex-M4 processor in an STM32F407G discovery board in C.

A system exception is an exception that is caused by some type of system error.

In the case of a MemManage fault, this is an exception that occurs because of a memory protection related fault.

In the case of a BusFault, this is an exception that occurs because of a memory related fault for an instruction or data memory transaction.

In the case of a UsageFault, this is an exception that occurs because of a fault related to instruction execution, such as an undefined instruction.

So in order to enable or disable a system exception, such as MemManage, BusFault, or UsageFault exceptions, there are System Control Block registers, which contain all registers that deal with system exceptions.

Below are a list of the System Control Block registers of the Cortex-M4 processor.


System control block registers of the cortex-M4 processor


So you can see that there are a list of registers.

The register that allows us to enable or disable a system exception is the System Handler Control and State Register (SHCSR).

The System Handle Control and State Register (SHCSR) is shown below.


System Handler Control and State Register (SHCSR) of the cortex-M4 processor


You can see that bits 16, 17, and 18 are the enable bits for the MemManage, BusFault, and UsageFault exceptions. If set to 1, these system exceptions are enabled. If set to 0, these system exceptions are disabled.

In the code below, we set bit 16 to 1, enabling the system exception for the MemManage system exception.

This code goes within the msp.c file, which for an STM32F4xx discovery board is, stm32f4xx_hal_msp.c





So this file must include the "stm32f4xx_hal.h" header file.

Inside of the HAL_MspInit() function, we place our code low level initializations for the processor.

We enable the MemManage system exception through the line, SCB->SHCSR |= 1 << 16;

The code to disable this same system exception is to do it by clearing bit 16.

This is shown below.





So the code above disables the MemManage system exception.

The code to enable all 3 system exceptions, MemManage, BusFault, and UsageFault, is shown below.





0x7 is 111 in binary.

So this turns on bits 16, 17, and 18, which are the enable bits for hte MemManage, BusFault, and UsageFault system exceptions.

The only other thing that's important to be done is the main.c file.

This main.c file needs the HAL_Init() function, which then goes to our mcp.c file through the HAL_MspInit() function and initializes the low-level configurations of the processor.

The code below is all that is needed for the main.c file within the Core directory.





So we must include 2 header files within this file.

We then have our HAL_Init(), which is the responsible for many initialization configurations, including low-level configuration settings for our processor.

And this is how to enable or disable a system exception of the cortex-M4 processor in an STM32F407G microcontroller board.



Related Resources





HTML Comment Box is loading comments...