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 at misaligned memcpy memset

we made some simple tests with STM32F100 Value Line Eval Board:

//------------------------------------------------------------------------------
// Variables
static unsigned char sDstBuf[1024]; // 1KiB
static unsigned char sSrcBuf[sizeof(sDstBuf)];

printf("Copying words from misaligned src to aligned dst buffer... ");
memset(sDstBuf, 0xcd, sizeof(sDstBuf));

with optimize Level 3, optimize for time this takes
120usec

with optimize Level 0
155usec

almost the same if memcpy is used:
memcpy(sDstBuf, (const void *)0xcd, sizeof(sDstBuf));

It runs into hard fault, if optimize Level >=1 and optimise for time is not set.

I think this is a compiler error..

We ran into this before with MDK 4.60, now we use 4.70A

Werner

Parents
  • Just got answer from Keil:

    This isn't actually a compiler fault, as unless the integer pointer is declared
    as packed, it must be suitably aligned. It just happens to be fortuitous
    that at -O0 it generates safe code.

    I was not aware of this. I also can't remember seeing the key word packed in any of the Cortex M3 Software Examples from ST / TI (Luminary) or NXP..

    thank you all again also for exhorting to dig deeper into the machine / assembly fundamentals. With 8051 uControllers which I did before this was relatively easy. With the nicely available librarys and examples for Luminary, which was my starting point some years ago, it was tempting to just make programs and develop applications and products which fortunately worked so far without much trouble.

Reply
  • Just got answer from Keil:

    This isn't actually a compiler fault, as unless the integer pointer is declared
    as packed, it must be suitably aligned. It just happens to be fortuitous
    that at -O0 it generates safe code.

    I was not aware of this. I also can't remember seeing the key word packed in any of the Cortex M3 Software Examples from ST / TI (Luminary) or NXP..

    thank you all again also for exhorting to dig deeper into the machine / assembly fundamentals. With 8051 uControllers which I did before this was relatively easy. With the nicely available librarys and examples for Luminary, which was my starting point some years ago, it was tempting to just make programs and develop applications and products which fortunately worked so far without much trouble.

Children