cancel
Showing results for 
Search instead for 
Did you mean: 

The mysterious __packed uint32_t in USB_ReadPacket and USB_WritePacket

PHolt.1
Senior III

Does anyone here know what is the point of this construct, which GCC (shipped with Cube IDE) doesn't like anyway?

I posted details here

https://www.eevblog.com/forum/programming/packed-attribute-warning/

12 REPLIES 12

> __packed is a macro that expands to: __attribute__((__packed__))

IMHO this can be explained as follows (not an authoritative opinion of course, please feel free to correct):

The __packed in that source is a keyword of IAR compiler. Later when support for gcc had been added, __packed was defined as "__attribute__((__packed__))".

But for gcc, meaning of __attribute__((__packed__)) has a subtle difference, in some contexts where the IAR __packed is valid, the gcc attribute is not.

Basically gcc has two distinct contexts for __attribute__((packed)): on variables and on structs (see here)

Modern GCC compiler warns if the attribute appears in awkward place where it has no effect,

but where IAR compiler may accept it.

Similar thing about under-cover implementation of __UNALIGNED... (which are ARM-specific CMSIS definitions). Different compilers have subtle differences.

Bottom line:

  • Do not ignore compiler warnings. They are your friends.
  • Try to understand the warnings and read the fine manuals, as time allows.
  • Look at the generated code (disassembly)

PHolt.1
Senior III

I solved it by removing the "packed" and ensure that the buffer is aligned (by the fact that it comes from a malloc() which has a guaranteed 8- alignment).

Of course as per good embedded practice there is no free(); the malloc() is done at startup only.

Also on the 32F4 you can do word ops without alignment being needed; other types differ I believe. It is slower though if not aligned, although I doubt anybody will tell the difference in practice.

PavelA., great info, thanks.

The post you linked-to mentions that the gcc docs for "packed" have different sections for "variable attributes" and "type attributes".

I realized that in my post, I misquoted the gcc docs. In quoting the current gcc docs, I accidentally quoted the "type attributes" specs instead of the "variable attribute" specs.

I updated my post to fix that.

The fix was instructive, as those specs specifically say that for variables, "packed" can only be specified for "structure members".