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.

passing a variable into a macro



Hi,

I am using CCS6.0, and C compiler v6.2.5.  I have defined a MACRO and am having problems passing a local Uint32 variable into it.  

Here's my code: 

#define SET_ECanaShadow_DEL_CANME_bit_MExx(mbox1, val1) ECanaShadow_DEL.CANME.bit.ME##mbox1 = val1

...

for(j=0; j < NumSymIn; j++)
{

Uint32 tx_mbox=18+j;
// itoa(tx_mbox, chardebug);
//c = '0'+tx_mbox;

ECanaShadow_DEL.CANME.all = ECanaRegs.CANME.all;
SET_ECanaShadow_DEL_CANME_bit_MExx(tx_mbox, 0);

...

}

When I build I get the following error: 

Description Resource Path Location Type
#137 struct "CANME_BITS" has no field "MEtx_mbox" DelfinoSimple.c /CANprot_DEL line 368 C/C++ Problem

If I pass in the following parameters everything works fine: 

SET_ECanaShadow_DEL_CANME_bit_MExx(18, 0);

Do you have any advice to fix this?   

It's odd that the following MACRO and call works just fine: 

#define SET_ECanaMboxes_MBOXxx_MDH_word_HI_WORD(mbox3, val3) ECanaMboxes.MBOX##mbox3.MDH.word.HI_WORD = val3

SET_ECanaMboxes_MBOXxx_MDH_word_HI_WORD(17, NumSymIn);

Thanks,

Rick 

  • I don't have enough context here to help you directly.  I suggest you use the compiler option --gen_acp_raw to help you diagnose this situation.  That option is useful any time you have difficulty understanding how the compiler preprocesses the source.  Read about it in the C2000 compiler manual.

    Thanks and regards,

    -George

  • You can't pass a variable to that macro.  Whatever is passed in that position is considered a C preprocessor token, and is token pasted by the ## operator in the macro.  When you call it with the literal argument 18, what you're really getting is the expression

    SET_ECanaShadow_DEL_CANME_bit_ME18 = 0

    In your last example, what you are really getting is

    ECanaMboxes.MBOX17.MDH.word.HI_WORD = NumSymIn
  • Is there a way to convert my tx_mbox variable to a literal argument before calling the macro?  (Such as itoa, which I showed here in the comments but I didn't include the right library so I haven't actually tested it yet.)  

    Or do you have any other advise to do what I'm trying to do?  I basically would like to set the number 17 in:  

    ECanaMboxes.MBOX17.MDH.word.HI_WORD 

    dynamically from my code - if that's possible.

    Thanks,

    Rick 

  • Rick Crispo said:
    Is there a way to convert my tx_mbox variable to a literal argument before calling the macro?

    No.  Keep in mind all macro expansion occurs at compile time, long before the value of a variable like tx_mbox is evaluated at runtime.  I suggest you make MBOX an array of structures, and index it with tx_mbox.

    Thanks and regards,

    -George