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.

[参考译文] TMS320F280039C:如何取消对齐结构字节

Guru**** 2779745 points
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/c2000-microcontrollers-group/c2000/f/c2000-microcontrollers-forum/1384026/tms320f280039c-how-to-unalign-struct-bytes

器件型号:TMS320F280039C

工具与软件:

typedef struct
{
    float  a;
    float  b;
    int    c;
    float  d;
    float  e;
    int    f;
    int    g;
    Uint16 h;
    float  i;
    float  j;
    int    k;
    int    l;
    float  m;
    float  n;
    float  o;
    int    p;
    int    q;
    }Test_dat_strut1;
这是我用于测试的结构、我在仿真中观察到变量 c 占据32位、我尝试将__attribute__((packed))和#pragma pack (1)命令添加到结构定义中、但这些命令均无效、因为此结构将用于通信、因此如何取消对齐该结构

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    遗憾的是、C2000编译器不支持任何用于打包结构成员的方法。   

    Unknown 说:
    由于此结构将用于通信、如何取消对齐结构[/quot]

    通过添加所需大小的额外未使用成员、手动强制结构成员到从开始到所需的偏移量。  为了确保操作正确、可以使用与此方法类似的技术、让编译器告诉您给定成员的偏移量(以位为单位)。  编写类似于...的代码

    #include <stddef.h>   /* for offsetof */
    #include <limits.h>   /* for CHAR_BIT */
    
    /* define Test_dat_strut1 here */
    
    int test_c()
    {
        return offsetof(Test_dat_strut1, c) * CHAR_BIT;
    }

    使用选项构建该代码 -- src_interlist .  这告诉编译器保留自动生成的汇编文件、并向其中添加注释、以便更易于理解。  汇编文件与源文件名相同、但扩展名更改为 .asm .  检查文件以查看类似于...的行

    ;----------------------------------------------------------------------
    ;  29 | return offsetof(Test_dat_strut1, c) * CHAR_BIT;                        
    ;----------------------------------------------------------------------
            MOVB      AL,#64                ; [CPU_ALU] |29|
      

    这会告诉您成员的偏移量 C. 距结构体开头64位。  宏 偏移 char_bit 来自 C 标准。  因此您可以将该技术用于其他编译器、以确保获得相同的偏移。

    谢谢。此致、

    -George.