This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Hard Fault in cortex m4

Hello All,

Good Morning!

I am working on Cortex m4.

I have read following about hard fault ,

"Bus Fault: detects memory access errors on instruction fetch, data read/write, interrupt vector fetch, and register stacking (save/restore) on interrupt (entry/exit)

Memory Management Fault: detects memory access violations to regions that are defined in the Memory Management Unit (MPU); for example code execution from a memory region with read/write access only.

Usage Fault: detects execution of undefined instructions, unaligned memory access for load/store multiple. When enabled, divide-by-zero and other unaligned memory accesses are also detected. Hard Fault: is caused by Bus Fault, Memory Management Fault, or Usage Fault if their handler cannot be executed"

Can anyone please explain with examples about Bus, Memory, Usage fault?

Can you please share any good links to read more about them in reference with ARM cortex m4 architecture?

Further, I have following code written in C running on cortex m4,

typedef struct {

  uint32_t nodeId;

  uint32_t systemId;

  int8_t serialNumber[16];

}sysConfig_t;

typedef struct {

  pktHeader_t pktHeader;

  uint8_t data[120];

}packet_t;

void *data;     //(this is pointer to array, i don't know exact alignment of this array )

packet_t *pkt = (uint8_t *)data;

sysConfig_t *sysData;

sysData = (sysConfig_t *)pkt->data;

printf(" %d %d ",sysData->nodeId, sysData->systemId);

I am getting Hard fault at printf statement. Why?

Can you please help?

According to me cortex m4 is based on arm v6 which supports handling unaligned addresses.

Can we discuss?

Thanks for reading my post.

Thanks

Hemant Undale.

Parents
  • I'd like to add a minor contribution to the other answers; it's about debugging in smaller steps.

    I suggest that above your existing printf, place the following line:

    printf(" [0x%08x] ",sysData); fflush(stdout);
    

    -So you will know what the actual value of sysData is, before you try to print the contents.

    This will help you becoming sure of why the error happens.

    Note: The fflush(stdout); is necessary, because the "\n" is omitted; however, it's sometimes a good idea to have the fflush(stdout); present, so you are sure that you get the complete debug output, before the execution continues.

    Now, when you see the output in the square brackets, make sure the address is divisible by 4 (at least). If it's not divisible by 4, you have an alignment error.

    Also, check that the address is within the RAM section (or Flash section); eg. the address should most likely start with 0x200..... - sometimes 0x100.....; depending on the memory configuration for that MCU.

    If the address is outside the addressable space (see your MCU's datasheet), then the pointer is incorrect.

    If the address is within the allowed address space, and it's divisible by 4, but not divisible by 8, then you're probably facing a LDRD problem as mentioned by sim.

    Remember these five main debugging rules:

    • If something crashes, it's a pointer-bug.
    • If you have a periodic bug, it's usually an uninitialized variable or a race condition.
    • If your graphics is drawing strangely, your calculations are wrong.
    • lockup / deadlock: You have an infinite loop.
    • If you have a floating point bug, your code runs on a Pentium.

    (Hmm, I don't know if I reproduced them exactly as I was taught, but they're probably close enough to be useful).

Reply
  • I'd like to add a minor contribution to the other answers; it's about debugging in smaller steps.

    I suggest that above your existing printf, place the following line:

    printf(" [0x%08x] ",sysData); fflush(stdout);
    

    -So you will know what the actual value of sysData is, before you try to print the contents.

    This will help you becoming sure of why the error happens.

    Note: The fflush(stdout); is necessary, because the "\n" is omitted; however, it's sometimes a good idea to have the fflush(stdout); present, so you are sure that you get the complete debug output, before the execution continues.

    Now, when you see the output in the square brackets, make sure the address is divisible by 4 (at least). If it's not divisible by 4, you have an alignment error.

    Also, check that the address is within the RAM section (or Flash section); eg. the address should most likely start with 0x200..... - sometimes 0x100.....; depending on the memory configuration for that MCU.

    If the address is outside the addressable space (see your MCU's datasheet), then the pointer is incorrect.

    If the address is within the allowed address space, and it's divisible by 4, but not divisible by 8, then you're probably facing a LDRD problem as mentioned by sim.

    Remember these five main debugging rules:

    • If something crashes, it's a pointer-bug.
    • If you have a periodic bug, it's usually an uninitialized variable or a race condition.
    • If your graphics is drawing strangely, your calculations are wrong.
    • lockup / deadlock: You have an infinite loop.
    • If you have a floating point bug, your code runs on a Pentium.

    (Hmm, I don't know if I reproduced them exactly as I was taught, but they're probably close enough to be useful).

Children
No data