UINT的结构Bitfield转换

发布于 2025-01-25 11:48:21 字数 3064 浏览 4 评论 0 原文

我正在努力编写AC类,其中包含几个应该转换为UINT8_T的联合结构Bitfields。 是的,我知道这是一种危险的设计,因为它依赖于体系结构...但是请帮助我以这种方式进行修复。

我的完整代码可以在此处找到: (最有可能的)关键零件是

template <uint16_t MCP3x6x_DEVICE_TYPE>
class MCP3x6x {
  typedef union {
    struct {
      enum adc_mode        : 2;
      enum cs_sel cs_sel   : 2;
      enum clk_sel clk_sel : 2;
      uint8_t config0      : 2;
    } config0 = MCP3x6x_DEFAULT_CONFIG0;

    inline status_t write() { return _write(_data, MCP3x6x_CMD_IWRITE | MCP3x6x_ADR_CONFIG0); };
    inline status_t read() { return _read(_data, MCP3x6x_CMD_IREAD | MCP3x6x_ADR_CONFIG0); };

   private:
    uint8_t _data;
  } config0_t;


  union {
    struct registers{
      config0_t config0;
// ...other members...
    } settings_default = {MCP3x6x_DEFAULT_CONFIG0, MCP3x6x_DEFAULT_CONFIG1,   MCP3x6x_DEFAULT_CONFIG2,
                  MCP3x6x_DEFAULT_CONFIG3, MCP3x6x_DEFAULT_IRQ,       MCP3x6x_DEFAULT_MUX,
                  MCP3x6x_DEFAULT_SCAN,    MCP3x6x_DEFAULT_TIMER,     MCP3x6x_DEFAULT_OFFSETCAL,
                  MCP3x6x_DEFAULT_GAINCAL, MCP3x6x_DEFAULT_RESERVED1, MCP3x6x_DEFAULT_RESERVED2,
                  MCP3x6x_DEFAULT_LOCK,    MCP3x6x_DEVICE_TYPE, 0x0000};

    inline status_t write() {
      return _write(settings, MCP3x6x_CMD_IWRITE | MCP3x6x_ADR_CONFIG0, 28);
    };
    inline status_t read() {
      return _read(this->_data, MCP3x6x_CMD_IREAD | MCP3x6x_ADR_CONFIG0, 28);
    };

   private:
    uint8_t _data[28];
  } settings;
}

错误日志

In file included from /home/user/Projects/lib_MCP3x6x/examples/analogRead/analogRead.ino:11:
src/MCP3x6x.h: In instantiation of 'constexpr MCP3x6x<15>::<unnamed union>::<constructor>()':
src/MCP3x6x.h:381:62:   required from 'MCP3x6x<MCP3x6x_DEVICE_TYPE>::MCP3x6x(uint8_t, SPIClass*) [with short unsigned int MCP3x6x_DEVICE_TYPE = 15; uint8_t = unsigned char]'
/home/user/Projects/lib_MCP3x6x/examples/analogRead/analogRead.ino:13:9:   required from here
src/MCP3x6x.h:364:27: error: could not convert 'MCP3x6x_DEFAULT_CONFIG0' from 'const uint8_t' {aka 'const unsigned char'} to 'MCP3x6x<15>::config0_t'
  364 |     } settings_default = {MCP3x6x_DEFAULT_CONFIG0, MCP3x6x_DEFAULT_CONFIG1,   MCP3x6x_DEFAULT_CONFIG2,
      |                           ^~~~~~~~~~~~~~~~~~~~~~~
      |                           |
      |                           const uint8_t {aka const unsigned char}

完整错误日志:

According to the error src/MCP3x6x.h:369:19: error: could not convert 'MCP3x6x_DEFAULT_CONFIG0' from 'const uint8_t' {aka 'const unsigned char'} to 'MCP3x6x<15> ; :: config0_t' default_config0(= uint_8t)的值无法转换为我不知道如何解决的config0_t,我会怀疑在同一联盟中_data会发生这种情况。 哦,使用这些枚举似乎也打破了一些东西,没有弄清楚那是什么。

I am struggling writing a c class containing several union struct bitfields which should convert to uint8_t.
yes, I am aware this is a dangerous design as it relies on the architecture... but please help me to fix it that way.

my full code can be found here: https://gist.github.com/nerdyscout/cd6a2e257053a87f994f9a51a0b1c9be
the (most likely) critical parts are

template <uint16_t MCP3x6x_DEVICE_TYPE>
class MCP3x6x {
  typedef union {
    struct {
      enum adc_mode        : 2;
      enum cs_sel cs_sel   : 2;
      enum clk_sel clk_sel : 2;
      uint8_t config0      : 2;
    } config0 = MCP3x6x_DEFAULT_CONFIG0;

    inline status_t write() { return _write(_data, MCP3x6x_CMD_IWRITE | MCP3x6x_ADR_CONFIG0); };
    inline status_t read() { return _read(_data, MCP3x6x_CMD_IREAD | MCP3x6x_ADR_CONFIG0); };

   private:
    uint8_t _data;
  } config0_t;


  union {
    struct registers{
      config0_t config0;
// ...other members...
    } settings_default = {MCP3x6x_DEFAULT_CONFIG0, MCP3x6x_DEFAULT_CONFIG1,   MCP3x6x_DEFAULT_CONFIG2,
                  MCP3x6x_DEFAULT_CONFIG3, MCP3x6x_DEFAULT_IRQ,       MCP3x6x_DEFAULT_MUX,
                  MCP3x6x_DEFAULT_SCAN,    MCP3x6x_DEFAULT_TIMER,     MCP3x6x_DEFAULT_OFFSETCAL,
                  MCP3x6x_DEFAULT_GAINCAL, MCP3x6x_DEFAULT_RESERVED1, MCP3x6x_DEFAULT_RESERVED2,
                  MCP3x6x_DEFAULT_LOCK,    MCP3x6x_DEVICE_TYPE, 0x0000};

    inline status_t write() {
      return _write(settings, MCP3x6x_CMD_IWRITE | MCP3x6x_ADR_CONFIG0, 28);
    };
    inline status_t read() {
      return _read(this->_data, MCP3x6x_CMD_IREAD | MCP3x6x_ADR_CONFIG0, 28);
    };

   private:
    uint8_t _data[28];
  } settings;
}

error log

In file included from /home/user/Projects/lib_MCP3x6x/examples/analogRead/analogRead.ino:11:
src/MCP3x6x.h: In instantiation of 'constexpr MCP3x6x<15>::<unnamed union>::<constructor>()':
src/MCP3x6x.h:381:62:   required from 'MCP3x6x<MCP3x6x_DEVICE_TYPE>::MCP3x6x(uint8_t, SPIClass*) [with short unsigned int MCP3x6x_DEVICE_TYPE = 15; uint8_t = unsigned char]'
/home/user/Projects/lib_MCP3x6x/examples/analogRead/analogRead.ino:13:9:   required from here
src/MCP3x6x.h:364:27: error: could not convert 'MCP3x6x_DEFAULT_CONFIG0' from 'const uint8_t' {aka 'const unsigned char'} to 'MCP3x6x<15>::config0_t'
  364 |     } settings_default = {MCP3x6x_DEFAULT_CONFIG0, MCP3x6x_DEFAULT_CONFIG1,   MCP3x6x_DEFAULT_CONFIG2,
      |                           ^~~~~~~~~~~~~~~~~~~~~~~
      |                           |
      |                           const uint8_t {aka const unsigned char}

full error log:https://gist.github.com/nerdyscout/ef7e8a89ba6a99e3e1f78d2d02f3ab87

According to the error src/MCP3x6x.h:369:19: error: could not convert 'MCP3x6x_DEFAULT_CONFIG0' from 'const uint8_t' {aka 'const unsigned char'} to 'MCP3x6x<15>::config0_t' the value of default_config0 (=uint_8t) can not be converted to config0_t which I do not know how to resolve, I would have suspected this should happen with _data in the same union.
oh and using those enums seems to break something as well, havn't figured out what that is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文