I have a problem with endianness.
I have a code written in big endian. But the code i found in Renesas app notes is little endian probably. And i do not know how to adapt the code into big endian in my project. Could anybody help me to adapt it? What do i need to pay attention on? How is it made?
If you change endianness of the CPU you must pay attention to byte order in multi-byte variables/structures and bit -order.
Example (changing lowest byte of 4-byte variable):
((uint8_t*)&test_var) = 0; /* big-endian */
((uint8_t*)&test_var) = 0; /* little-endian */
test_var &= ~0x000000FFULL; /* endian-independent-way */
Also pay attention to bit order in bit-fields. I don't know for sure if they match, but you can check it by yourself. If they differ, you would need to rewrite declarations for bit-fields.
If you transfer any data to/from your mcu, pay attention to endianness of incoming data. If endianness match you need to do nothing. In other case,byte reversing will be required.
In reply to Maxim Salov:
Thanks again for your answer.
But i did not really understand your explanation. Could you please make it clear for me?
Lets say i had sample code from the renesas web for the SCI and i want to merge this code into my own project which is big endian.
Should i only change the byte-order of multi-byte variables/structures?
Lets say i have a variable inside the sci code as: var_a=0x5467;
What i understand is that i need to change var_a=0x6754;
is that right? do you exactly mean this?
In reply to burky:
Maxim re-cap it in a "nut shell" for you is good.
It is hard to cover everything you will encounter in your port.
I covered some in these issues in the article:
We cover additional Endianness issues in my 8-32 bit migration lecture at DevCon 2012 if you want to review (should be posted in the near future on RenesasInteractive, sorry it is not there yet).
No. Variables solely used inside MCU should not be changed if are accessed in regular way (without pointer typecasting etc.).
So if you use SCI, then you recieve or transmit data. With single byte characters everything is ok (for example ASCII text). But if you want to transmit two (or more) byte integer endianness come into play.
Example: you need to transmit decimal value 2012 (0x07DC) in binary form. Your MCU (big-endian) will transmit first byte 0x07 and second 0xDC. If reciever is your PC (little-endian) it will combine these two bytes in opposite order and you'll get 0xDC07 (decimal 56327).