RTOS+ arm7 typecast problem

joelcote wrote on Wednesday, July 29, 2009:

Hi,

I use eclipse+yagarto+the exemple 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 and go in data abort of boot.s. Can someone 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.

thank

ben_fnr wrote on Wednesday, July 29, 2009:

sounds like an alignment problem.  var1 is probably at an odd address which is fine for byte access but not for 16/32 bit access

Ben

ben_fnr wrote on Wednesday, July 29, 2009:

have you tried the reverse ?

int short var2 = 0x1234;
char* var1;

var1 = (char*)&var2;

Ben

joelcote wrote on Wednesday, July 29, 2009:

yes I have the problem only when the var1 have a odd address, how can I solve that?
No I didn’t tried the inverse, because in you exemple var1 is only a ptr, I want the value to be copied and be able to modify var2 without modify the value of var1

Joël

ben_fnr wrote on Wednesday, July 29, 2009:

You could try using the aligned attribute

http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html

joelcote wrote on Wednesday, July 29, 2009:

That doesn’t work because in my real projet i’m in a structure (who is packed to be sure no memory space is left between two variable), So if I have this kind of structure
var1[2];
var2;
var3[2];
and I use var3 it is sure that the address of var3 is odd and that doesn’t work on the arm, but I don’t know why because on the rabbit i’m am able to do that

ben_fnr wrote on Wednesday, July 29, 2009:

Assuming the structure has to be packed and you can’t change the order of the structure elements you will have to copy the short int a byte at a time.

If you can change the order of the elements then delcare var3 after var 1 and both will probably be aligned on an even address, assuming the structure is aligned that way.

This is not really the correct forum for discussing this as it is not a problem with FreeRTOS or related in anyway to it. You would still have your problem even if you used no RTOS.

joelcote wrote on Wednesday, July 29, 2009:

Do you know better forum where I can post ? I tried sparfun and atmel

thank

aturowski wrote on Wednesday, July 29, 2009:

Thisshould work:

char var1[2];
int short var2 = 0x1234;

var1[1] = (char)  (var2 & 0xFF);
var1[0] = (char) (var2 >>8);

Maybe not most effective, but it will work? BTW why are you doing such ugly casting anyway?

joelcote wrote on Wednesday, July 29, 2009:

I know this work, but it’s take more time to the cpu to do it. Why did you find this cast ugly, I find it so beautiful :P. With this cast the cpu copy the value in one shot and this fast when you have to do many.