C error: expression must have a constant value

后端 未结 3 1980
逝去的感伤
逝去的感伤 2020-12-11 20:01

I am writing some embedded code to interface with an external device over SPI. The device has several registers of varying length and to help keep things straight I have def

3条回答
  •  不知归路
    2020-12-11 20:51

    I think this may be a compiler issue, and it would be helpful to know your platform and how you are building this code. I just took most of your code, doctored it up to compile it, and compiled on Linux using gcc. There were no warnings.

    #include
    #include
    #include
    
    typedef struct
    {
        uint16_t    Signed          :1;  // Register is signed or unsigned
        uint16_t    CommLengthBytes :3;  // The width of the register in bytes 
        uint16_t    Address         :12; // Register address
    }ts_register;
    
    int main(int argc, char **argv) {
    
        static const ts_register    SAGCYC      = {0, 1, 0x000};
        static const ts_register    DISNOLOAD   = {0, 1, 0x001};
        static const ts_register    LCYCMODE    = {0, 1, 0x004};
        static const ts_register    IRMSA       = {0, 4, 0x31A};
        static const ts_register    IRMSB       = {0, 4, 0x31B};
        static const ts_register    VRMS        = {0, 4, 0x31C};
    
        ts_register regs_to_read[3] = {VRMS, IRMSA, IRMSB};
    
        return(0);
    }
    

    Have you tried casting the values? It's not always the best thing to do, but will get you around the error.

    Have you considered creating #define entries?

    Also, please be aware consts take a bit of getting used to in C. They do not always behave the way you might expect.

提交回复
热议问题