 |
 |
| View previous topic :: View next topic |
| Author |
Message |
lucromain
Joined: 04 Apr 2011 Posts: 6
|
| Using Preprocessor macros for register declaration and index |
Posted: Tue Nov 04, 2025 1:05 pm |
|
|
I am using the following preprocessor macro to declare registers and also create an index for each declared register:
| Code: |
const int8 RegCnt = 0;
#define REG_DEFINE_ARRAY(name,dv,size) \
int8 name[size]; \
const int8 Offset_##name = RegCnt; \
const int8 DefaultValue_##name = dv ; \
const int8 RCT = RegCnt; \
#undef RegCnt \
const int8 RegCnt = (RCT + size); \
#undef RCT
|
This macro is called multiple times as shown here:
| Code: | //
// Register name DefaultValue Size
// ------------- ------------ ---
REG_DEFINE_ARRAY(AdminPwd , 0x01 , 8 )
REG_DEFINE_ARRAY(SiteIDMorse , 0x00 , 6 )
...many other registers declared here...
|
This compiled successfully when using V4.140.
When compiling with V5.121, this error appears:
| Code: |
1 Errors, 0 Warnings, Build Failed., Time: 1 Seconds
--- Info 300 "<path>/MyPIC.h" Line 101(165,171): More info: First Declaration of RegCnt
Error[31] <path>/MyPIC.h 134 : Identifier is already used in this scope
|
Line 134 points to the first REG_DEFINE_ARRAY call.
Is there a way to use preprocessor macros to declare and count registers that would be compatible with V5.121? |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19989
|
|
Posted: Wed Nov 05, 2025 11:05 am |
|
|
The problem is that #undef doesn't do what you think.
It should not have worked. That it did was a fluke....
#undef undefines a macro, not a variable name. Hence the failure.
You don't actually need to undef at all. Just use a different name.
C can be made to generate a unique variable name each time a macro
is called.
There are some bits of this that seem silly/pointless.
const, says to put a variable into the ROM. You are telling the compiler to
put RCT into ROM, and then tell it to forget about it at the end of the macro.
Why?. Just make this a macro definition, and it can then be forgotten with
#undef. It never needs to actually be a variable. |
|
 |
lucromain
Joined: 04 Apr 2011 Posts: 6
|
|
Posted: Tue Nov 25, 2025 10:59 am |
|
|
| Quote: | There are some bits of this that seem silly/pointless.
const, says to put a variable into the ROM. You are telling the compiler to
put RCT into ROM, and then tell it to forget about it at the end of the macro.
Why?. Just make this a macro definition, and it can then be forgotten with
#undef. It never needs to actually be a variable. |
I agree that the initial implementation appears awkward, but that was the only way to achieve my goal. When attempting perform increments inside PP macros, I get compilation errors.
Essentially, I am trying to create a preprocessor macro that:
1) Declares a register
2) Assigns a preprocessor macro that points to the register's creation index, such as Register_##name = <index>
3) Increments a register count preprocessor variable
Below is the code that I'm trying to compile:
| Code: |
1 #include <16F690.h>
2
3 #define RegCount 0
4
5 #define DeclareReg(reg_name) int reg_name; \
6 #define RegMap_##reg_name RegCount \
7 #undef TMP \
8 #define TMP RegCount \
9 #undef RegCount \
10 #define RegCount TMP + 1
11
12 void main(void) {
13
14 //
15 // Declarations using Macros:
16 //
17 DeclareReg(Enable)
18 // DeclareReg(Polarity)
19 const int RegisterCount=RegCount;
20
21 //
22 // Main code
23 //
24 Enable = 10;
25 // Polarity = 15;
26 const int EEPROM_Enable_Index = RegMap_Enable;
27 // const int EEPROM_Polarity_Index = RegMap_Polarity;
28 }
|
This code does not compile because of line 10 in the macro where I'm trying to increment RegCount using TMP+1.
Commenting out line 10 allows the compilation to succeed, but this works only if the macro is called once (line 18 is commented).
The code in the original post was the only way I mangaged to get this working with V4.140, although it doesn't look great. I'd like to get this working in a more elegant fashion and compiles under V5.121. |
|
 |
waffles
Joined: 21 Dec 2021 Posts: 12
|
|
Posted: Tue Nov 25, 2025 2:42 pm |
|
|
You could achieve something similar using an x macro, but you wouldn't be able to generate/declare your register variables in the global namespace.
| Code: |
/* Declare all your registers here */
#define XM_REGISTER_DATA(XMREG) \
XMREG(ADMIN_PWD, 0x01, 8) \
XMREG(SITE_ID_MORSE, 0x00, 6) \
XMREG(OUTPUT_ENABLED, 0x01, 1)
/* enum generator macro */
#define XM_REGISTER_GENERATE_ENUM(name, default_value, size) REG_ID_##name,
/* variable generator macro */
static size_t xm_reg_offset = 0;
#define XM_REGISTER_GENERATE_VARS(name, default_value, size) \
uint8_t name[size] = {0};\
uint8_t ofs_##name = xm_reg_offset;\
uint8_t default_val_##name = default_value;\
xm_reg_offset += size;
typedef enum {
XM_REGISTER_DATA(XM_REGISTER_GENERATE_ENUM)
REG_ID_MAX
} reg_id_t;
void
main(void) {
/* Generate register variables */
XM_REGISTER_DATA(XM_REGISTER_GENERATE_VARS);
printf("Reg id%u - default: %u, offset: %lu\r\n",
REG_ID_ADMIN_PWD,
default_val_ADMIN_PWD,
ofs_ADMIN_PWD
);
printf("Reg id%u - default: %u, offset: %lu\r\n",
REG_ID_SITE_ID_MORSE,
default_val_SITE_ID_MORSE,
ofs_SITE_ID_MORSE
);
printf("Reg id%u - default: %u, offset: %lu\r\n",
REG_ID_OUTPUT_ENABLED,
default_val_OUTPUT_ENABLED,
ofs_OUTPUT_ENABLED
);
}
|
This register mapping implementation is something I'd definitely look at refactoring at a later date, but this *should* get you running again based on my interpretation of your intent. |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|