typecast arm problem

Hi

I use eclipse+yagarto+the exemple getting-started-project-1.3-at91sam7x-ek or RTOS “ARM7_AT91SAM7X256_Eclipse”. I have a problem when I do this

char var1[2];

int short var2 = 0x1234;

(int short)var1 = var2

When I do the typecast the arm stop working if the address is odd. Can you tell me why the arm his not able to do that and how I can solve my problem, because with the Rabbit I was able to do that. I know I can use memcpy, but with this cast I save some CPU time.

ARM instruction set does not allow unaligned memory access

char can be at any address

short must be half word aligned

int must be word aligned.

do you know how to solve or bypass the problem of the cast without a mask bit, memcpy?

casting a scalar to a pointer is a big no-no on anyone’s CPU.

Rethink the technique.

it was done a lot in olden days and led to numerous problems in portability and compiler vendor + CPU type dependencies.

And don’t forget that casting between different types of pointers breaks strict aliasing rules, something that’s no longer legal in C99, and will lead to mysterious code errors when turning on compiler optimization.

If you use GCC you could try the union hack, like this

union CharToShortCast
{
    char as_char[2];
    short as_short;
}

Creating instances of this union allows to access memory as a short or as an array of char. The compiler will take care of half-word alignment.

Also keep in mind that it is a GCC extension to the C language, and therefore your code won’t be portable between compilers.