Is there a way to control the extension order of macros

I hope that someone might have an idea on how to control / specify the extension order of macros. Here is the context:


// 32 bit increments, processor has registers for set, clear and invert
#define CLR_OFF 1
#define SET_OFF 2
#define INV_OFF 3


#define SET(reg,bits) *((volatile unsigned long*)(& reg+SET_OFF)) = bits
//Now if I use this I can do it quite nicely with
#define STATUS_LED 0x0040;
SET(LATB, STATUS_LED); // LATB is port of the LED.

I actually had to move the hardware a bit, so I decided to group the LATB information using STATUS_LED, like ...


#define STATUS_LED_PORT LATB
#define STATUS_LED_MASK 0x0040;
#define STATUS_LED STATUS_LED_PORT, STATUS_LED_MASK

//And I try to use it via
SET( STATUS_LED );

But, alas, LATB, 0x0040 is passed to argument 1 of the SET macro. If this macro is not used as a macro, this method works correctly:


inline void SET(u32_t *reg, u32_t bits) { ((volatile u32_t *) (((u32_t)reg) + SET_OFF*4 )) = bits; }
//Change the STATUS_LED macro to
#define STATUS_LED &STATUS_LED_PORT, STATUS_LED_MASK
SET( STATUS_LED); //Works great!

But, unfortunately, my compiler does not see the need to embed the function and calls 6 instructions for setting the register, as opposed to 4, so this is unpredictable for use in bitmapping.

I hope someone can learn about a way to first increase the STATUS_LED macro, something like: SET( ##STATUS_LED )

SET SETRM (set register, mask), , , SET ...


#define SETRM(reg,bits) ...
#define SET(args) SETRM(args) //WHY WOULD THIS GET EXPANDED HERE??

, , n- , , , , : (.

, , , , SET .

+3
2

- . , # ##, , , .

, , , , .

, , , , .

. SET

#define SET(reg,bits) *((volatile unsigned long*)(& reg+SET_OFF)) = bits

,

#define STATUS_LED_PORT LATB
#define STATUS_LED_MASK 0x0040;
#define STATUS_LED STATUS_LED_PORT, STATUS_LED_MASK

, .

.

#define SET2(x) SET(x)

SET2( STATUS_LED ) .

SET( LATB , 0x0040; )

*((volatile unsigned long*)(& LATB + 2 )) = 0x0040;

, SET ; , . ; .

SET( STATUS_LED )
+4

, :

#define SET_COMPOSITE(root) SET(root##_PORT, root##_MASK)
+1

Source: https://habr.com/ru/post/1530599/


All Articles