Cross Compiling Endian Issues - How To Avoid

Official FreeEMS vanilla firmware development, the heart and soul of the system!
Post Reply
User avatar
Fred
Moderator
Posts: 15432
Joined: Tue Jan 15, 2008 2:31 pm
Location: Home sweet home!
Contact:

Cross Compiling Endian Issues - How To Avoid

Post by Fred »

Spudmn, you did some cross compiling of the firmware recently, and noted endian-ness issues. How can we avoid these? Are they a product of masks? What if we used bit fields instead? Educate me :-) I'm curious, this is your thread, go! :-)
DIYEFI.org - where Open Source means Open Source, and Free means Freedom
FreeEMS.org - the open source engine management system
FreeEMS dev diary and its comments thread and my turbo truck!
n00bs, do NOT PM or email tech questions! Use the forum!
The ever growing list of FreeEMS success stories!
User avatar
Spudmn
LQFP112 - Up with the play
Posts: 232
Joined: Thu Feb 10, 2011 12:27 am
Location: Auckland, NZ

Re: Cross Compiling Endian Issues - How To Avoid

Post by Spudmn »

The only endian problem I have found in FreeEMS so far is with the “typedef union”

Code: Select all

typedef struct {
        unsigned thisPair: 4;
        unsigned lastPair: 4;
} twoPairs;
 
typedef union {
        twoPairs pairs;
        unsigned char pattern;
} match;
 

When matches.pattern is used on a PC (Unit Testing) the answer is nibble swapped.

The hack I used to get around this was to do this.

Code: Select all

#ifndef UNIT_TEST
typedef struct {
                unsigned lastPair: 4;
                unsigned thisPair: 4;
} twoPairs;
 
typedef union {
                twoPairs pairs;
                unsigned char pattern;
} match;
#else
 
typedef struct {
        unsigned thisPair: 4;
        unsigned lastPair: 4;
} twoPairs;
 
typedef union {
        twoPairs pairs;
        unsigned char pattern;
} match;
 
#endif

There is another option that uses macros to swap the byte order if required. This could be done so that there are no extra instructions for code compiled for the HC12

Code: Select all

#if(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
// PC Unit Testing
#elif (MC6811)
#define BIG_ENDIAN
#else
#error Unknown Byte Order
#endif

#if BIG_ENDIAN
   #define READ_UNION_8(x) (x)
#else
  #define READ_UNION_8(x) ((((x) & 0xF0) >> 4) | (((x) & 0x0F) << 4))
#endif

//Example of use

if(READ_UNION_8(matches.pattern) == MatchedPairMatchedPair){      //         | small | small | small | - All periods match, could be anywhere, unless...
....
}
I have updated the unit test example to use the macro above.

https://github.com/Spudmn/Missing_teeth_Unit_Test
User avatar
Fred
Moderator
Posts: 15432
Joined: Tue Jan 15, 2008 2:31 pm
Location: Home sweet home!
Contact:

Re: Cross Compiling Endian Issues - How To Avoid

Post by Fred »

I just went through your repo....

Not happy!

.....

We need to get this integrated into the main repo :-)

I LOVE it! Genius work, and clean! 10/10!

OT: Talk folder structure with me. How to integrate it into the existing setup with its own make file etc? Lead the way!

<3

Fred.
DIYEFI.org - where Open Source means Open Source, and Free means Freedom
FreeEMS.org - the open source engine management system
FreeEMS dev diary and its comments thread and my turbo truck!
n00bs, do NOT PM or email tech questions! Use the forum!
The ever growing list of FreeEMS success stories!
User avatar
Fred
Moderator
Posts: 15432
Joined: Tue Jan 15, 2008 2:31 pm
Location: Home sweet home!
Contact:

Re: Cross Compiling Endian Issues - How To Avoid

Post by Fred »

Forked, cloned:

Feature request: Make file for CLI build please! :-)

make
make test
<results>

? You said you were busy, so no rush, but I'm keen to play!

Fred.
DIYEFI.org - where Open Source means Open Source, and Free means Freedom
FreeEMS.org - the open source engine management system
FreeEMS dev diary and its comments thread and my turbo truck!
n00bs, do NOT PM or email tech questions! Use the forum!
The ever growing list of FreeEMS success stories!
User avatar
Fred
Moderator
Posts: 15432
Joined: Tue Jan 15, 2008 2:31 pm
Location: Home sweet home!
Contact:

Re: Cross Compiling Endian Issues - How To Avoid

Post by Fred »

Keen to setup a framework for decoder testing with CSV data input from saleae dump (or similar) such that we can run it through in a more bulk format. For example, I have some saleae logs of a 30-2 setup that I couldn't get a clean sync on. Peter has some logs of a bad pattern that he shouldn't get sync on, and so on. If we could extend that to all decoders in some low effort way, that'd be great. It'd also rule out the need for a JimStim/FredStim like device.

Disclaimer: I've had a couple of ciders.

Fred.
DIYEFI.org - where Open Source means Open Source, and Free means Freedom
FreeEMS.org - the open source engine management system
FreeEMS dev diary and its comments thread and my turbo truck!
n00bs, do NOT PM or email tech questions! Use the forum!
The ever growing list of FreeEMS success stories!
Post Reply