bitsmack
bitsmack

Reputation: 1424

Using C Function-like Macros as function aliases

I'm writing an embedded application in C, using SDKs from two different vendors. Each SDK has its own modules to handle serial (SPI) functionality, and they interfere with each other. I have written my own SPI module, and wish to use my functions instead.

Instead of modifying the SDK by replacing all the function calls, I'd like to use function-like macros to cause the old calls to map to the new functions.

My functions mirror theirs, although with an additional argument to declare which device I'm talking with. For example:

Vendor's SDK function declaration:

NTRXSPIRead(uint8_t address, uint8_t *buffer, uint8_t len);

My function:

spiBurstRead(DEVICETYPE, uint8_t address, uint8_t *buffer, uint8_t len);

My proposed solution:

#define NTRXSPIRead(add,buff,len) spiBurstRead(DEVICETYPE,(add),(buff),(len))

Should this work? Is there a better way to do it?

Upvotes: 1

Views: 224

Answers (1)

bitsmack
bitsmack

Reputation: 1424

I tried it, and it worked well! Here is a simpler function as an example, although the function posted in the question also works.

SDK Function:

NTRXSPIWriteByte(uint8_t address, uint8_t buffer);

My function:

void spiWriteByte (DEVICETYPE, uint8_t address, uint8_t byte2send);

The #define statement:

#define NTRXSPIWriteByte(add,buff) spiWriteByte(SPI_NANO,(add),(buff))

.

Looking at the disassembler, a direct call to the function looks like this:

spiWriteByte (SPI_NANO, 0x01, 0xDB); // Address=0x01, Data 0xDB
LDI  R24,0x02  Load immediate // Note: "SPI_NANO" is enumerated as = 2
LDI  R22,0x01  Load immediate
LDI  R20,0xDB  Load immediate
CALL 0x00003519  Call subroutine

The call made by the SDK:

NTRXSPIWriteByte (0x01, 0xDB);
LDI  R24,0x02  Load immediate
LDI  R22,0x01  Load immediate
MOV  R20,0xDB  Load immediate
CALL 0x00003519  Call subroutine

Notice that it passed the DEVICETYPE into the target function, even though it didn't exist in the SDK. Sweet!

Upvotes: 1

Related Questions