By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,871 Members | 2,418 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,871 IT Pros & Developers. It's quick & easy.

CPP Multi-Line Macro Expansion Needs

P: 1
Good day folks.

Let me open with the statement that I am not a C++/C programmer. The environment that I am programming in is ARMbasic, an embedded BASIC targeted toward ARM-based micro-controllers. So why am I posting herein? Well, the ARMbasic environment makes use of a tool borrowed from your folk's environment - CPP. The build details are:

Expand|Select|Wrap|Line Numbers
  1. C:\Program Files\Coridium>cpp --version
  2. cpp (GCC) 3.2.3 (mingw special 20030504-1)
  3. Copyright (C) 2002 Free Software Foundation, Inc.
CPP was added recently to the ARMbasic environment to enable the building of more complex apps, via the leveraging of existing code libraries, as I am sure all of you are aware of in the C++/C environment.

My question is this: Is there a way to get a proprocessor macro to expand to multiple lines of code?

Scenario: I have a function created where the internal Analog to Digital Converter is queried and the integer result is returned. On the target that I am programming (an NXP 2103 micro), the ADC converter is able to run at 4.5 MHZ (or less) - this equates to approx 3us if coded in ASM. If coded in C (by the ARMbasic developer), the read time is ~5us. With my ARMbasic function, due to system overhead, etc., my function is taking 11us. This is acceptable in most cases, but I have a peer that is needing higher throughput.

If I manually code my ADC reads in-line, via 2 POKES, 2 PEEKS and 1 conditional loop per ADC read, I can get the acquisition time down to 8us. The target, in this case of helping my peer out, is to get the code down to <=10us. So, the problem is solvable by manually coding in a bunch of in-line pokes/peeks. However, with the Preprocessor, it seems that I could substitute in-line code vs. the function calls by simply #define-ing a new in-line ADC input routine:

==================================================
CURRENT IMPLEMENTATION
==================================================

Current Function:

Expand|Select|Wrap|Line Numbers
  1. Function AD (channel as integer) as integer
  2.     CALL POKE (AD_ADCR, ((1 << channel) + AD_ENABLE))
  3.     CALL POKE (AD_ADCR, ((1 << channel) + AD_START))
  4.     DO
  5.     UNTIL (PEEK(AD_ADGDR) AND $80000000) // wait for the conversion
  6.     RETURN (PEEK(AD_ADGDR) AND $FFFF)>>6
  7. ENDFUNCTION
Calling Code:

Expand|Select|Wrap|Line Numbers
  1. blah = AD(ch)  // <<<<<  this is 11us with above function
==================================================
DESIRED IMPLEMENTATION
==================================================

Desired Inline Macro:

Expand|Select|Wrap|Line Numbers
  1. #begin ADC_INLINE (channel,variable)
  2.     CALL POKE (AD_ADCR, ((1 << channel) + AD_ENABLE))
  3.     CALL POKE (AD_ADCR, ((1 << channel) + AD_START))
  4.     DO
  5.     UNTIL (PEEK(AD_ADGDR) AND $80000000) // wait for the conversion
  6.     variable = (PEEK(AD_ADGDR) AND $FFFF)>>6
  7. #end
New Calling Code?:

Expand|Select|Wrap|Line Numbers
  1. ADC_INLINE (6, blah)  // <<< this should yield target speed
==================================================

ARMbasic doesn't allow multiple commands on a single line of code - i.e. 1 command per line, period. If I correctly understand the many CPP guidance texts that I have read, I take it that multi-line macro definitions, when 'expanded' will actually concatenate the multi-line definition to a single line, with new-line operators embedded therein - this is my root issue...

Is there a means to easily get CPP to expand a macro definition, with passed argument substitution, to multiple lines, exactly as defined in the Macro Def?

I have searched high and low, and have come up empty handed (actually dirty handed, as I have found a couple of solutions, but those solutions require skill sets that I do not possess).

The most attractive solution is a patch to CPP that has a #begin/#end macro definition construct/directive. It is located here.

A viable alternate solution seems to be located here.

The problem with both of these approaches is that I simply do not possess the tools or skill sets to take this source and compile it for use on a win32 platform...

So, my plea is this: Can someone point me toward either a win32 CPP binary that has the desired construct built-in already, or advise, in a rather detailed fashion (TIA) how one could use GNU CPP, in it's native form, to expand a macro definition to multiple lines, with parameter replacement and without extra syntax being added or definition syntax being altered? Please.

Thanks for taking the time to read my diatribe.

-Tod Wulff
Dec 19 '07 #1
Share this Question
Share on Google+
1 Reply


100+
P: 256
==================================================
DESIRED IMPLEMENTATION
==================================================

Desired Inline Macro:

Expand|Select|Wrap|Line Numbers
  1. #begin ADC_INLINE (channel,variable)
  2.     CALL POKE (AD_ADCR, ((1 << channel) + AD_ENABLE))
  3.     CALL POKE (AD_ADCR, ((1 << channel) + AD_START))
  4.     DO
  5.     UNTIL (PEEK(AD_ADGDR) AND $80000000) // wait for the conversion
  6.     variable = (PEEK(AD_ADGDR) AND $FFFF)>>6
  7. #end
New Calling Code?:

Expand|Select|Wrap|Line Numbers
  1. ADC_INLINE (6, blah)  // <<< this should yield target speed
==================================================
I didn't read everything you wrote, but it looks like you want something like this:
Expand|Select|Wrap|Line Numbers
  1.       #define ADC_INLINE ((channel),(variable))   \
  2.           CALL POKE (AD_ADCR, ((1 << (channel)) + AD_ENABLE));  \
  3.           CALL POKE (AD_ADCR, ((1 << (channel)) + AD_START));   \
  4.           DO   \
  5.           UNTIL (PEEK(AD_ADGDR) AND $80000000);  \
  6.           variable = (PEEK(AD_ADGDR) AND $FFFF)>>6; 
  7.  

Basically you use \ to extend your macro to multiple lines. I added semi-colons at the end of the lines, but I'm not sure how the DO / UNTIL part works so you'll probably have to adjust
Dec 19 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.