portable way to pack structures

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • portable way to pack structures

    Hi,

    Currently, i’m using the Keil-MDK IDE for my projects and i like to use the embedded studio to test my codes with GCC and/or LLVM.
    Unfortunately, i need packed structures for the communication with other system. I#m using the ARM Compiler Keyword packed for this.

    What would be the right way to declare these structures compiler independent?

    For example:

    C Source Code

    1. typedef __packed struct
    2. {
    3. __packed union
    4. {
    5. __packed struct
    6. {
    7. uint32_t component : 4;
    8. uint32_t unused : 12;
    9. uint32_t size : 11;
    10. uint32_t Flag : 1;
    11. uint32_t typ : 4;
    12. } Header;
    13. unsigned long Header32;
    14. } pkt;
    15. uint32_t infol[INFOSIZE];
    16. __packed union
    17. {
    18. __packed struct
    19. {
    20. uint32_t Data : 16;
    21. uint32_t subdata : 7;
    22. uint32_t busy : 1;
    23. uint32_t Addr : 4;
    24. uint32_t Cmd : 4;
    25. } cmdpkt;
    26. uint32_t Cmd32;
    27. } Cmd[31];
    28. } xyz_TypeDef;
    Display All


    Thank you
    Martin
  • For GCC/LLVM you need to set the attribute packed to the struct or union.
    struct __attribute__ ((__packed__)) my_packed_struct

    To make it compiler independent you would need to wrap this with pre-processor macros based on the toolchain defines.

    Regards
    Johannes
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.