I am working on HW programming. I created below Varidic macro.
#define UPDATE_DATA(bit,...) { \
do { \
int i; \
int j = 0; \
int _args[] = {__VA_ARGS__};\
int *addr = (int *) ADDR ; \
for (i = 0; i < (sizeof(_args)/sizeof(_args[0])); i++) {\
*(int *) (DATA_ADDR - j) |= _args[i];\
j = j + 0x4; \
}\
*addr |= 1 << bit;\
}while (0); \
}
its working if I use like UPDATE_DATA(1,2,3);
But if use this macro inside for loop like,
msg_id = MSG_1;
for(j=0; j<15 ; j++)
{
msg_id = msg_id +1;
l_data = data_array[j];
UPDATE_DATA(msg_id,l_data,(SYSTEM_BASE_ADDR6+offset));
offset = (offset + 0x4);
}
I am getting
Error: #28: expression must have a constant value
LOGGER_UPDATE_DATA(msg_id,l_data,(SRAM0_SYSTEM_BASE_ADDR6+offset));
i am not getting what did i do wrong here!!
My suggestion is to not use a variadic macro. They expand code size and make maintenance difficult.
Assuming that you won't need to pass -1 as a value, you can use stdarg.h
Since this will be running in an embedded system, my test case just prints messages to say what it would do. The lines commented out would do the actual memory writes. I have used contrived addresses (0x1234 and 0x5678) for your memory mapped I/O addresses.
This code outputs the following, which meets your specifications: