This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430 GCC and <machine/endian.h>

I would expect that this header includes the usual endian conversion macros/functions like htons, htonl, betohs, etc, but it doesn't. Well, this is not a big problem as I can write my own functions for this. However, I noticed that in endian.h the BYTE_ORDER  variable gets defined to BIG_ENDIAN which is a serious ploblem if someone tries to use a portable code.

Am I doing something, wrong or is it really a bug?
My GCC version (I am using CCS v6.1.1:

msp430-elf-gcc.exe (GCC) 4.9.1 20140707 (prerelease (msp430-14r1-364)) (GNUPro 1
4r1) (Based on: GCC 4.8 GDB 7.7 Binutils 2.24 Newlib 2.1)
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Thanks,

Gabor

  • Functions like htons() must be defined in <arpa/inet.h>.

    My <machine/endian.h> defines BYTE_ORDER as LITTLE_ENDIAN. Doesn't your <machine/ieeefp.h> contain the correct setting for MSP430?

  • It is definitely wrong. But on the plus side, it doesn't include any functions that would misbehave. Why this is wrong is a mystery because mspgcc (from before when TI started maintaining GCC for the MSP430) got it right:

    sourceforge.net/.../endian.h
  • Please check that

    • your <machine/endian.h> includes <sys/config.h>; and that
    • your <sys/config.h> includes <machine/ieeefp.h>; and that
    • your <machine/ieeefp.h> sets __IEEE_LITTLE_ENDIAN if __MSP430__ is set.

  • config.h and ieeefp.h don't have any effect because endian.h defines BIG_ENDIAN as 4321 if it isn't defined. It also sets LITTLE_ENDIAN to 1234 if it isn't defined. Looking at those two other headers, these symbols aren't defined. Therefore they both get defined in endian.h

    BYTE_ORDER is set to either LITTLE_ENDIAN or BIG_ENDIAN depending on IEEE_LITTLE_ENDIAN. Perhaps there has been a change in the preferred way of doing things so that rather than checking to see if LITTLE_ENDIAN is defined, the correct test is: "#if BYTE_ORDER == LITTLE_ENDIAN"

    But the byte swapping functions are still missing from endian.h.
  • You are misunderstanding how the logic works. <machine/ieeefp.h> does set __IEEE_LITTLE_ENDIAN, and that is the symbol used by <machine/endian.h> to decide the value of BYTE_ORDER.

    Please note that <machine/endian.h> is an internal header that is probably not meant to be used by applications.

    POSIX specifies that htons() and friends must be defined in <arpa/inet.h>. When using gcc, try using <byteswap.h>.

  • Hmmm, I thought I said that BYTE_ORDER is set depending on IEEE_LITTLE_ENDIAN so I don't know what you think I misunderstood.

    There is no byteswap.h among the include files for TI GCC. mspgcc had it but not the TI version.
  • Yep, no byteswap.h.

    I need to make a correction though. I rechecked and BYTE_ORDER does get defined to LITTLE_ENDIAN eventually, as  stated, if  __MSP430__ is set. What must have confused me is that the field gets greyed out in Code Composer Studio (I'm using v6.1.1) Funny that it doesn't know that the complier I'm using for the project will set __MSP430__

    ...

**Attention** This is a public forum