STM32/ARM Cortex-M3 HOWTO: Development under Ubuntu (Debian)

Basic NVIC, HardFault

In the mini example there I use something called NVIC, and at position 3 and 4 we call some kind of "handler"...

On the Documentation page I have a link to a document called "RM0008, STM32F101xx, STM32F102xx, STM32F103xx, STM32F105xx and STM32F107xx advanced ARM-based 32-bit MCUs", and in "Table 53. Vector table for other STM32F10xxx devices" we find this (and more).

0x0000_0000 -
0x0000_0004 Reset
0x0000_0008 NMI
0x0000_000C HardFault

And that is what we have been playing with in the mini example, but what is that HardFault part?

If we look up HardFault in "PM0056, STM32F10xxx Cortex-M3 programming manual", we find this text.

-"Hard fault A hard fault is an exception that occurs because of an error during exception processing, or because an exception cannot be managed by any other exception mechanism."

So what does that really mean?

Let's introduce a bad error, something like a bad function pointer. Say hello to the 0x DEAD BABE :)

Filename: main.c

int main(void)
{
    int i=0;

    while(1)
    {
        i++;

        int (*fpBabe)() = 0xDEADBABE;
        fpBabe();
    }
}

If you run this program it will fail, there is no question about it. Setting pc to 0xDEADBABE is not really a good idea... But what happens if you do that anyway?

Let's connect our gdb client and run the program until it fail, and then we will end up with something like this.



The core has totally freak out and throw us a "HardFault", it has used what is at 0x0000_000C. And since we have our "Default_Handler" there, this is what we will see.

Now we don't have any good information about what has happend, so we don't know why we ended up in this horrible place. The next step would be to write a error handler, that could collect and save enough information so your debug would become easier...

But since we don't have that, we need to reboot the mcu and step the area until we find the trouble.

Later we can look into how we can do this better, but this basic example illustrates quite well how the Cortex-M3 core handles some errors.

Hope you don't end up in this state when you start writing you own programs :)

This page is licensed with Creative Commons ShareAlike 3.0