AnsweredAssumed Answered

USB Lib: Enable 4-Byte alignment ALWAYS (not only for DMA!!!)

Question asked by bil.til on Jun 21, 2012
Latest reply on Jun 22, 2012 by bil.til
The USB EP buffers defined in usbd_cc_core.cpp definitely NEED to be 4-byte aligned. Otherwise you end up in Memory-Alignment fault / Hard fault (if it is enabled by setting SCB->CCR |= 0x18 - this you definitely should always do).

To get this, please take care that the macros __ALIGN_BEGIN and __ALIGN_END are ALWAYS defined correctly. To get this done, please change the code segment in usb_conf.h to the following:
  #if defined   (__GNUC__)        /* GNU Compiler */
    #define __ALIGN_END    __attribute__ ((aligned (4)))
    #define __ALIGN_BEGIN         
    #define __ALIGN_END
    #if defined   (__CC_ARM)      /* ARM Compiler */
      #define __ALIGN_BEGIN    __align(4)  
    #elif defined (__ICCARM__)    /* IAR Compiler */
      #define __ALIGN_BEGIN
    #elif defined  (__TASKING__)  /* TASKING Compiler */
      #define __ALIGN_BEGIN    __align(4)
    #endif /* __CC_ARM */  
  #endif /* __GNUC__ */
//  #define __ALIGN_BEGIN
//  #define __ALIGN_END   
(Comment out the first line and the last 4 lines, so that __ALIGN_BEGIN and __ALIGN_END always do their job).

Otherwise, if you define some char field with "char acEndpointBuf[64]", this will possibly NOT be aligned, which then leads to running into HardFault trap.

(I had a USB software running nicely, and when changing the module order in the Keil project list, suddently I got such HardFault trap - looking into it, I recognized that by changing the module order, the linkage was changed, and my Endpoint Buffers lost their 4-byte alignment ... - this took me some hours).