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.

Variable replacement with #define - help

Other Parts Discussed in Thread: SIMPLICITI

Hello all,

I am trying to put toghether a piece of code that should compile and run on multiple evaluation kits (ez430rf, exp4518 and exp5438). I understand that various functions will be assigned to different GPIOs, so I came up with the following solution:

  //               eZ430RF    EXP461x           EXP5438
  //LED1     P1.0            P2.2                    P1.0
  //LED2     P1.1            P2.1                    P1.1
  //SW1      P1.2             P1.0                    P2.6
  //SW2      null              P1.1                     P2.7

  #if (defined __eZ430RF__)
   #define PLED P1
   #define LED1BIT 0x01
   #define LED2BIT 0x02
   #define PSW P1
   #define SW1BIT 0x04
   #define SW2BIT null
  #elif (defined __EXP461z__)
   #define PLED P2
   #define LED1BIT 0x02
   #define LED2BIT 0x04
   #define PSW P1
   #define SW1BIT 0x01
   #define SW2BIT 0x02
  #elif (defined __EXP5438__)
   #define PLED P1
   #define LED1BIT 0x01
   #define LED2BIT 0x02
   #define PSW P2
   #define SW1BIT 0x40
   #define SW2BIT 0x80
  #else
  #error "Must have a valid board"
  #endif
  #define PortFuncEval(func1)     P ## func1
  #define PortFunc(func1)     PortFuncEval(func1)
  #define step1(x,y)     x ## y
  #define step2(x,y)     step1(x,y)
  #define Port(func,op)    step2(PortFunc(func),op)

  Port(LED,DIR) |= (LED1BIT+LED2BIT);  // P1DIR |= 0x03; Set P1.0,1 Output

The thing works, replacing "Port(LED,DIR)" with P1DIR correctly. Unfortunately  to get the P1OUT, for example, the "Port(LED,OUT)" doesn't work! It gives me the "expression must be a modifiable lvalue" error (I think, because "OUT" is declared in the msp430Fxxxx.h file).

I have tryed to use small letters ( as opposed to CAPS), but I got difficulties when I've tryed to convert to CAPS with "toupper()".

Is any of you guys, having an ideea of how can I accomplish this (without modifying the include/definition files, as the code must be portable)?

Many thanks!

Bogdan

  • Hi Bogdan,

    have a look at this post (http://e2e.ti.com/forums/t/11483.aspx); I've answered a similar question regarding conditional compiling there.

    I would recommend to define macros for all of your connections. Since you only have 4 labels which you will use in your code (LED1, LED2, SW1, SW2) I think this is the easiest way to go.

    Rgds
    aBUGSworstnightmare 

  • Thank you aBUGSworstnightmare, for youe reply!

    Unfortunately, the attched code is just a snippet, and I will need to use the method for more pins and features (ADC, timer...). I looked at the post you mention, and I would agree that if all that I would use would be the 2 LEDs and 2 SW, it would work. But what if you have another 20-50 variable names to change.

    I still hope that there is a way...

    Best regards,

    Bogdan

  • Hi Bogdan,

    I don't understand why you will change every definitions. Why do you want to change i.e. the pre-defined Timer_A or ADC register names? On the other hand, if you have to change all your variables you will only need to do it once (keep your defines in an header file and your initialization in a seperate C-Source).

    aBUGSworstnightmare

  • Ok, let me explain (a little).

    The thing I am working now is just a pre-test that should guide me to make a framework for things to come. We will (hopefully) use 3-4 kinds of MSPs & boards with some similar base code, but different features. As of now, we evaluate, but in the same time we need to be able to take the code and move it to the client's environment, where he will maybe change the mcu. We try to make it easy for him ( I understand that it is not possible to take into account EVERYTHING, but at least a good portion of it). This is, also, the reason we do not want to change the "given" include/def files. Let's call it "good experience out of the box".  Something that a new MSP user will not get easy ( think of trying to combine simpliciti with the user experience demo for exp5438 :-( and then port it to customer's board based on 5418... It is a pain, at least in my experience) 

    I am sorry if what I am looking for is weird. Actually, I think some other users may benefit from this.

    Now, back to my question, I have noticed that all names are defined in CAPS, so if  I only be able to use, for example,  Port(led, out) to get the P1OUT as a result, I think it would be super. I obviously have trouble into converting "out" to "OUT' inside the macros.

    Also, do you know of a way to see what the pre-proc expanded the macros into?

    Thank you again!

    Best regards,

    Bogdan

  • You cannot use toupper() during preprocessing, since it is a library function and not a preprocessing operator.  You may use upper or lowercase letters freely in preprocessor definitions, but remember the preprocessor is case-sensitive.

    You can use the -ppo option to create a file that shows what your source code looks like after all of the preprocessing is finished.  You can use the -ppm option to create a file showing what the compiler thinks each macro definition is.

    When you say "OUT" is defined in another header file, do you mean exactly "OUT"?  If so, you may be inadvertently expanding that macro instead of pasting the identifier "OUT"

  • Thank you all!

    I think I solved my puzzles. The toupper(), the -ppo and the OUT. I #undef OUT at the beggining and #define OUT  (0x0004) at the end.

    Again, thank you Archaeologist and aBUGSworstnightmare!

    Best regards,

    Bogdan