Skip to main content
HIm
Associate II
February 11, 2020
Solved

__attribute__ ((aligned (32))) doesn't work.

  • February 11, 2020
  • 3 replies
  • 6559 views

i'm working with spc570s and using gnu free gccc in spc5studio.

In my program I need to apply __attribute__(( aligned(32))) to an const struct variable.

I tried like this but it does't work.

for example.

typedef struct

{

uint32_t aa;

uint 32_ bb;

...

}A

static const A ***[2]  __attribute__(( aligned(32)))

i want to allocate *** with starting address 0xdddddd00(20,40, 60).

Thank you in advance.

    This topic has been closed for replies.
    Best answer by alister

    First, align the structure, not the variable. See https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-aligned-type-attribute. For example

    typedef struct
    {
     uint32_t first;
     uint32_t second;
    } __attribute__((aligned(32))) myStruct_t;
     
    myStruct_t myStruct[2];

    Next, control its placement in memory. Your post suggests you've already done this somehow. Otherwise for example

    myStruct_t myStruct[2] __attribute__((section(".myDevice")));

    And in your linker command file

    MEMORY
    {
    <snip>
     
     MY_DEVICE (rw) : ORIGIN = 0xdddddd00, LENGTH = 64 
    }
     
    SECTIONS
    {
    <snip>
     
     .myDevice (NOLOAD) :
     {
     *(.myDevice)
     } >MY_DEVICE
    }

    Finally, as you're accessing a device of some kind and the number and order of bus accesses is probably significant, you ought tell the compiler that with a volatile qualification on each of the structure members, for example

    typedef struct
    {
     volatile uint32_t first;
     volatile uint32_t second;
    } __attribute__((aligned(32))) myStruct_t;

    3 replies

    Ozone
    Principal
    February 11, 2020

    > I tried like this but it does't work.

    How did you determine it doesn't work ?

    > i want to allocate *** with starting address 0xdddddd00(20,40, 60).

    Not sure what that is supposed to mean.

    __align__ (32) means an address divisible by 32, i.e. the least significant 5 bits are zero.

    With writing "0xdddddd00", you seem to assume the last 8 bits are zero, i.e. align (256).

    HIm
    HImAuthor
    Associate II
    February 12, 2020

    ​I compiled and checked the address of the variable in the map file. start address of variable is 0xddddddF0.

    I want the starting address of one of variable to be located at an address that starts with an even number as 32(0x20)byte boundrary( i.e. 0xddddddE0, 0xddddddC0).

    0690X00000D9h8DQAR.jpg

    Uwe Bonnes
    Chief
    February 11, 2020

    No output/errors/warnings from the compiler? What does the compiler do?

    HIm
    HImAuthor
    Associate II
    February 12, 2020

    ​There is no errors/warnings. i'm using GNU GCC in SPC5Studio.

    Ozone
    Principal
    February 12, 2020

    I don't use SPC tools, or GCC version. Never had such trouble with other gcc tools.

    Assuming it might be a compiler/linker bug, have you tried the next larger alignment size, i.e. __attribute__(( aligned(64))) ?

    alister
    alisterBest answer
    Senior III
    February 13, 2020

    First, align the structure, not the variable. See https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-aligned-type-attribute. For example

    typedef struct
    {
     uint32_t first;
     uint32_t second;
    } __attribute__((aligned(32))) myStruct_t;
     
    myStruct_t myStruct[2];

    Next, control its placement in memory. Your post suggests you've already done this somehow. Otherwise for example

    myStruct_t myStruct[2] __attribute__((section(".myDevice")));

    And in your linker command file

    MEMORY
    {
    <snip>
     
     MY_DEVICE (rw) : ORIGIN = 0xdddddd00, LENGTH = 64 
    }
     
    SECTIONS
    {
    <snip>
     
     .myDevice (NOLOAD) :
     {
     *(.myDevice)
     } >MY_DEVICE
    }

    Finally, as you're accessing a device of some kind and the number and order of bus accesses is probably significant, you ought tell the compiler that with a volatile qualification on each of the structure members, for example

    typedef struct
    {
     volatile uint32_t first;
     volatile uint32_t second;
    } __attribute__((aligned(32))) myStruct_t;

    HIm
    HImAuthor
    Associate II
    February 25, 2020

    Thank you for your reply​