union用法

文章目录


union

union有很多成员,在使用时只对其中的一个成员生效。

这样可以省空间。

联合体总大小 = 最大成员大小 = 8

关于字节对齐,可以填充无用字节或者用汇编#pragma pack来指定下来的声明用几字节对齐。

c 复制代码
#pragma pack(push, 1)      // 把当前对齐值压栈,并设为 1 字节对齐
typedef struct __attribute__((packed)) {
    uint8_t  a;
    uint16_t b;  
    uint32_t c;
} Msg_t;
#pragma pack(pop)          // 恢复成 push 之前的对齐值(通常是 4/8)

比如下列的就是union的省空间用法,这样这一个8字节的union可以用来表示非常多不同的结构体。

c 复制代码
union dev_dev_io_cmd_resp
{
    uint8_t rev[8];
    struct dev_dev_dido_cmd0_resp0
    {
        // bit 0 - 6: tsdev_dev_dido_set_glb_par
        // bit 7: op_valid_ok (cmd: cmd valid;  resp: op ok)
        uint8_t cmd;
        uint8_t is_12v;
        uint16_t report_cycle_ms; // tsdev_dev_dido_io_report report cycle
    } cmd0_resp0;
    struct dev_dev_dido_cmd1_resp1
    {
        // bit 0 - 6: tsdev_dev_dido_set_bridge
        // bit 7: op_valid_ok (cmd: cmd valid;  resp: op ok)
        uint8_t cmd;
        uint8_t dido_1_2; // dido1 bridge dido2,0: reset; 1: set; 2: not change,
        uint8_t dido_3_4; // dido3 bridge dido4,0: reset; 1: set; 2: not change,
    } cmd1_resp1;
    struct dev_dev_dido_cmd2_resp2
    {
        // bit 0 - 6: tsdev_dev_dido_mode
        // bit 7: op_valid_ok (cmd: cmd valid;  resp: op ok)
        uint8_t cmd;
        uint8_t dido_idx;
        uint16_t dido_input_thread;   // 0-5000, unit is 1mV
        uint8_t dido_mode0;           // bit0: io type: 0: io; 1: pwm
        uint8_t dido_mode1;           // bit0-1: 0: input; 1: push-pull; 2: pull(p-mos) only; 3: push(n-mos) only /// ? out high/low level
        uint16_t pwm_report_cycle_ms; // tsdev_dev_dido_pwm_report report cycle
    } cmd2_resp2;
    struct dev_dev_aiao_cmd3_resp3
    {
        // bit 0 - 6: tsdev_dev_aiao_mode
        // bit 7: op_valid_ok (cmd: cmd valid;  resp: op ok)
        uint8_t cmd;
        uint8_t aiao_idx;
        uint8_t aiao_mode; // 0: input, 1: output
        uint8_t rev;
        uint16_t report_cycle_ms;
    } cmd3_resp3;

    struct dev_dev_dido_cmd4_resp4
    {
        // bit 0 - 6: tsdev_dev_dido_read/tsdev_dev_dido_write
        // bit 7: op_valid_ok (cmd: cmd valid;  resp: op ok)
        uint8_t cmd;
        uint8_t dido_idx;
        uint8_t io_status;
        uint8_t rev;
    } cmd4_resp4;
    struct dev_dev_dido_cmd5_resp5
    {
        // bit 0 - 6: tsdev_dev_pwm_read/tsdev_dev_pwm_write
        // bit 7: op_valid_ok (cmd: cmd valid;  resp: op ok)
        uint8_t cmd;
        uint8_t pwm_idx;
        uint16_t duty; // uint is 0.01%
        uint32_t freq; // unit is hz
    } cmd5_resp5;
};
相关推荐
承渊政道14 小时前
C++学习之旅【C++Stack和Queue类介绍—入门指南与核心概念解析】
c语言·数据结构·c++·学习·visual studio
CQ_YM14 小时前
ARM之uart
c语言·arm开发·单片机·嵌入式硬件
杜子不疼.15 小时前
【Linux】基础IO(三):文件描述符与重定向
linux·c语言·开发语言·人工智能
沃尔特。1 天前
直流无刷电机FOC控制算法
c语言·stm32·嵌入式硬件·算法
轻微的风格艾丝凡1 天前
C语言内联函数(inline)与宏函数(#define)技术文档
c语言
龚礼鹏1 天前
图像显示框架八——BufferQueue与BLASTBufferQueue(基于android 15源码分析)
android·c语言
WK100%1 天前
二叉树经典OJ题
c语言·数据结构·经验分享·笔记·链表
宫瑾1 天前
【C语言】嵌入式C加强学习
java·c语言·学习
程序猿编码1 天前
高性能HTTP服务压测工具:设计思路与实现原理(C/C++代码实现)
c语言·网络·c++·网络协议·tcp/ip·http