Thread: Struct members initialization fails - do not compile

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    97

    Struct members initialization fails - do not compile

    Hi everyone,

    I want to create a GPIO initialization procedure based on the version of the evaluation board that is used. So, I have a structure with the GPIOs and then two macros for setting the GPIOs

    Code:
    struct GPIO_def  {
      uint8_t def_MUX_ADD0;        /**< MUX Address channel 1 */
      uint8_t def_MUX_ADD1;        /**< MUX Address channel 2 */
      uint8_t def_MUX_ADD2;        /**< MUX Address channel 3 */
      uint8_t def_IMU_wakeUp_INT1; /**< sense IMU's interrupt from channel 1 */
      uint8_t def_IMU_INT2;        /**< IMU Interrupt pin */
      uint8_t def_vibrator;        /**< Control the vibrator - 0: vibrator OFF , 1: vibrator ON */
      uint8_t def_WC_CHG;          /**< Monitor WPT (Wireless Power Transfer) - 0: wireless charging ON , 1: wireless charging OFF */
      uint8_t def_BM;              /**< Battery monitor */
      uint8_t def_PG;              /**< Power good indication of back converter */
      uint8_t def_CTRL;            /**< Enable/disable the auxiliary load output of back converter */
      uint8_t def_ADC_IN;          /**< Read Op Amps output */
    };
    
    
    #define GPIO_EVB_V2_DEF        \
      {                            \
        .def_MUX_ADD0 = 33,        \
        .def_MUX_ADD1 = 34,        \
        .def_MUX_ADD2 = 35,        \
        .def_IMU_wakeUp_INT1 = 39, \
        .def_IMU_INT2 = 40,        \
        .def_WC_CHG = 46,          \
        .def_BM = 47,              \
        .def_PG = 42,              \
        .def_CTRL = 43,            \
        .def_ADC_IN = 3            \
      }
    
    
    #define GPIO_EVB_V3_DEF        \
      {                            \
        .def_MUX_ADD0 = 6,         \
        .def_MUX_ADD1 = 8,         \
        .def_MUX_ADD2 = 41,        \
        .def_IMU_wakeUp_INT1 = 44, \
        .def_IMU_INT2 = 42,        \
        .def_vibrator = 4,         \
        .def_WC_CHG = 5,           \
        .def_BM = 10,              \
        .def_PG = 15,              \
        .def_CTRL = 13,            \
        .def_ADC_IN = 30           \
      }
    Then I have the GPIO_init function to assing the GPIO values based on the version of the evaluation board

    Code:
    void GPIO_init(uint8_t evb) {
    struct GPIO_def GPIO_set;
    
    
      switch (evb) {
    
    
      case EVB_V2: ;
        GPIO_set = GPIO_EVB_V2_DEF;
        break;
    
    
      case EVB_V3: ;
        GPIO_set = GPIO_EVB_V3_DEF;
        break;
    
    
      default:
        break;
      }
    }
    The problem is that when I create the structure instance and use the switch..case statement to assing the GPIO values the program do not compile...
    It returns this error
    https://cboard.cprogramming.com/imag...AASUVORK5CYII=
    but I am sure that I do not have forget any bracket or comma or stuff like that.. I 've noticed that when I initialize like this
    Code:
    struct GPIO_def GPIO_set = GPIO_EVB_V2_DEF;
    before switch statement it compiles..

    How could I work around this?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,631
    You need to use a typecast:
    Code:
    #define GPIO_EVB_V2_DEF        \
      (struct GPIO_def)            \
      {                            \
        .def_MUX_ADD0 = 33,        \
        .def_MUX_ADD1 = 34,        \
        .def_MUX_ADD2 = 35,        \
        .def_IMU_wakeUp_INT1 = 39, \
        .def_IMU_INT2 = 40,        \
        .def_WC_CHG = 46,          \
        .def_BM = 47,              \
        .def_PG = 42,              \
        .def_CTRL = 43,            \
        .def_ADC_IN = 3            \
      }
    And presumably you want GPIO_set to be global.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    Thanks for your reply,

    After using a typecast it compiles but when I try to initialize the GPIO_def structure the members remains to 0 and don't get the values from neither GPIO_EVB_V2_DEF or GPIO_EVB_V3_DEF structs

    What am I missing?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You need to post the smallest code that is not working!

    You do realize the code you posted will not work because you set a variable that is local to the function, right?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    Ok I figure it out!! thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile under gcc fails, but VS2010 works
    By Sorin in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2012, 02:45 AM
  2. Explicit initialization of class members
    By zephon in forum C++ Programming
    Replies: 6
    Last Post: 06-10-2010, 05:09 AM
  3. Initialization of class members
    By misterowakka in forum C++ Programming
    Replies: 3
    Last Post: 02-02-2008, 01:35 PM
  4. RapidMind fails to compile
    By abachler in forum Windows Programming
    Replies: 14
    Last Post: 12-14-2007, 09:01 AM
  5. compile error about member variable initialization
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 12-04-2007, 11:55 AM

Tags for this Thread