Memory alignment problems in AVR32


This post summarizes some memory alignment problems I’ve had with AVR32 and Atmel’s GNU toolchain that ships with Atmel Studio 6.2.

INTRAM section length in the linker script

My problem began when I changed the INTRAM section length in the linker script. The code fails, and I am not even able to start a debug session using JTAGICE mkII. Lengths of 0x00007FFC and 0x00008000 work all right, 0x00007FFF does not. The former are a multiple of 4 i.e. 32-bit aligned, the latter is not.

Casting to a pointer of different type

Casting an unsigned char pointer, to pointer to a multi-byte scalar type such as unsigned int, and accessing it, for example *((unsigned int*)(&array[index])), may lead to Data Read Address exception. If you are stuck in that exception handler, you’ve got a memory read alignment problem.

Passing pointers to unaligned members of packed structs

I haven’t (yet) had any problems with packed structs, but beware of passing pointers to members of such structs to other functions. If your architecture does not support unaligned access (AVR32 being one such architecture), the processor may fault when functions try to read or write memory through misaligned pointers. Passing a pointer to the struct itself is fine because the compiler knows it is packed, and will access its members in a safe way.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s