Preprocessor macro with embedded multiple argument define

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hacking existing RF230 SPI driver code to work with the internal ATmega128rfa1 i/o space registers, I redefined the SPI read/write routines

uint8_t hal_register_read(uint8_t address);
void hal_register_write(uint8_t address,uint8_t data);

with

#define hal_register_read(address) address
#define hal_register_write(address,data) address=data

and this works fine, because the address arguments used in the code are predefined in iom128rfa1.h as e.g.

#define TRX_STATUS _SFR_MEM8(0x141)

Then foo=hal_register_read(TRX_STATUS) becomes foo=_SFR_MEM8(0x141).

But the subregister read/write routines

uint8_t hal_subregister_read(uint8_t address, uint8_t mask, uint8_t position)
void hal_subregister_write(uint8_t address, uint8_t mask, uint8_t position, uint8_t value)

are called with e.g.

#define SR_TRX_STATUS  TRX_STATUS, 0x1f, 0
state = hal_subregister_read(SR_TRX_STATUS);

and defining

#define hal_subregister_read(address,mask,position) (address&mask)>>position

doesn't work, SR_TRX_STATUS is seen as one argument by the preprocessor, and it errors out saying three arguments are needed.

Is there any way to force the argument replacement first so the macro sees the three arguments?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dak664 wrote:
Is there any way to force the argument replacement first so the macro sees the three arguments?
You need an intermediate step.

#define x_hal_subregister_read(address,mask,position) (address&mask)>>position 
#define hal_subregister_read(x) x_hal_subregister_read(x) 

Stefan Ernst