CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Macro expansion fails in 5.069

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

Macro expansion fails in 5.069
PostPosted: Mon Feb 20, 2017 1:47 am     Reply with quote

Goodday.
Have a problem in CCS 5.069 when using a macro expansion. CCS say it works on their side, so any ideas please.
I have not included any FUSES to make program small, but it can compile.
Code:
#include <33EP128GM604.h>
#use delay(internal=60000000)
#word MCU_PWMCON2 = 0xC40
unsigned int16 MCU_Temp;
#define PWMBUC1 2
#define make_pwmcon(x)   MCU_PWMCON##x

#define Enable_deadtime(x) {\
   MCU_Temp = (unsigned int16)make_pwmcon(x) & 0xff3f;\
   MCU_PWMCON##x = (unsigned int16)MCU_Temp | 0x0000;\ }
   
void main() {
  Enable_deadtime(PWMBUC1);  //Works in 5.024, but not 5.069
  Enable_deadtime(2);                     //Works in both versions
   while(TRUE) {
   }
}


CCS Ver 5.024 output:
Memory usage: ROM=1% RAM=1% - 1%
0 Errors, 0 Warnings.
Build Successful.

CCS Ver 5.069 compiler output:
--- Info 300 "main.c" Line 2(1,1): More info: Actual Clock is 59983611
*** Error 12 "main.c" Line 13(27,28): Undefined identifier MCU_PWMCONPWMBUC1
1 Errors, 0 Warnings.
Build Failed.
Ttelmah



Joined: 11 Mar 2010
Posts: 19550

View user's profile Send private message

PostPosted: Mon Feb 20, 2017 2:35 am     Reply with quote

That's actually correct......

Classic C problem of macro expansion. This is why 'make_pwmcon' exists, but you then don't use it on the second expansion.
The correct C syntax would be:
Code:

#include <33EP128GM604.h>
#use delay(internal=60000000)
#word MCU_PWMCON2 = 0xC40
unsigned int16 MCU_Temp;
#define PWMBUC1 2
#define make_pwmcon(x)   MCU_PWMCON##x

#define Enable_deadtime(x) {\
   MCU_Temp = (unsigned int16)make_pwmcon(x) & 0xff3f;\
   make_pwmcon(x) = (unsigned int16)MCU_Temp | 0x0000;\ }
   
void main() {
  Enable_deadtime(PWMBUC1);  //Works in 5.024, but not 5.069
  Enable_deadtime(2);                     //Works in both versions
   while(TRUE) {
   }
}


From the manual of GCC:
Quote:

Macro arguments are completely macro-expanded before they are substituted into a macro body, unless they are stringified or pasted with other tokens.


Note the 'unless'.

So when you use the ## inside the Enable_deadtime, the value is not expanded.

When you use 'make_pwmcon', the macro values are expanded when this is 'called', so it receives '2', not 'PWMBUC1'.

It sounds as if CCS have fixed an error, which just happened to allow your previous expansion to work....
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Mon Feb 20, 2017 2:40 am     Reply with quote

Thank you very much Ttelmah, I missed it that the arg on the left also need the substitution. Appreciate the time spend by you.

Regards
Ttelmah



Joined: 11 Mar 2010
Posts: 19550

View user's profile Send private message

PostPosted: Mon Feb 20, 2017 3:06 am     Reply with quote

Nice to see someone using Macros. Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group