View previous topic :: View next topic |
Author |
Message |
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
Macro expansion fails in 5.069 |
Posted: Mon Feb 20, 2017 1:47 am |
|
|
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
|
|
Posted: Mon Feb 20, 2017 2:35 am |
|
|
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
|
|
Posted: Mon Feb 20, 2017 2:40 am |
|
|
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
|
|
Posted: Mon Feb 20, 2017 3:06 am |
|
|
Nice to see someone using Macros. |
|
|
|