Excerpts from Tim Hardisty's message of February 26, 2023 12:17 pm: > > On 25/02/2023, 11:47, "Karel Kočí" <cyn...@email.cz <mailto:cyn...@email.cz>> > wrote: > >>I would use union (that is host ordering) and to convert to specific ordering >>you can use functions like htobe32 (big endian) and htole32 (little endian), >> those are available. > >> Excerpts from Tim Hardisty's message of February 25, 2023 11:26 am: >>> As is so often the case, I need to pack an array of 4x uint8_t into a >>> uint32_t. Obviously there are many ways to do this and we all have our >>> favourite. For NuttX: >>> >>> 1) have I missed a library function that does this already? >>> 2) to cope with big and little end is there a NuttX CONFIG or other >>> parameter that will give the endian-ness of the arch being built to ensure >>> the byte packing is done correctly? > > When using a union is it fair to assume that it is the responsibility of an > app rather than a driver to sort endian-ness? I can find no references in the > NuttX code base to changing endian-ness? > > Perhaps I'm over-thinking! >
The example might be helpful. Lets start with absolute basic just to be sure we understand each other: union { uint32_t u32; uint8_t u8[4]; } foo; foo.u32 = 42; // This uses host's endianness and the compiler's responsibility // The following prints most significant byte on the big endian and least // significant byte on the little endian. Probably nothing new... printf("%d\n", foo.u8[0]); The point is that compiler always knows the correct endianness and uses it. That is also what you accept as argument in most functions (commonly expected when order is not documented). Now when you are implementing chip specific driver you know endianness and thus there is no need to do swap when it matches. On the other hand, when you are implementing generic driver, expected to be running on any CPU, you need to ensure that swap happens when ever it is required (endianness doesn't match). The common approach, not just in NuttX, are those functions I pointed previously. The manual page for them is endian (https://man7.org/linux/man-pages/man3/endian.3.html). I think that you are overthinking it and you missed the library call. 😉
pgpfoXYVOnpu_.pgp
Description: PGP signature