Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Hardfault on cortex M3

Posted by bertino on May 28, 2014


I'm working on an application that sometimes fails and gets into the HardFaultHandler.

I was trying to find a way to understand the cause by implementing your suggestions in: http://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html

but looks like the asm code in your example has a syntax that is not compatible with IAR Embedded Workbench v7.10, for example the decalration :

static void HardFault_Handler( void ) attribute( ( naked ) );

makes IAR complain for a missing "{"

so since assembly directives are not my bread and butter, do you have a reference example based on IAR that I can look at to get over this quickly? Not for lazyness, I actually would like to increment my knowledge on asm but since I have tight deadlines I'm looking for the fast track if there is any suggestion that can help;

Thanks, Adalberto

Hardfault on cortex M3

Posted by rtel on May 28, 2014

The code as posted on the FreeRTOS.org site is using GCC syntax. IAR do a pretty good job of allowing GCC syntax through their compiler, but it looks from your post that the naked attribute is not supported all the same.

There would be a couple of options. First look in the IAR manual to see if they have an equivalent to the naked attribute (which prevents the compiler from inserting any prologue or epilogue code), or second move all the code into an assembly file.

Does your project have any assembly files already? It probably will do for the C start up code and vector table. Somewhere you will have a default HardFault_Handler function, and you would be able to add the assembly code from the web page in that.

If the HardFault_Handler function is a C file and you don't have any assembler files in your project you will have to create your own assembly file and add it to the project. In IAR syntax it would look something like the following:

    RSEG    CODE:CODE(2)

    PUBLIC HardFault_Handler
    EXTERN prvGetRegistersFromStack


    tst lr, #4
    ite eq
    mrseq r0, msp
    mrsne r0, psp
    ldr r1, [r0, #24]
    ldr r2, prvGetRegistersFromStack
    bx r2

Regards, Richard Barry.

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner