Using #define wrong way ?
-
I am trying to build SAM3x8E registers database. I though I could use serial of #define(s) to do that but run into a problem In this sample I can build PIO_SODR but I cannot add another (port) offset to it - see PIOA_SODR. I think the problem is my defines are not really int (32 bits) , but only symbols. I did compare the (symbol) PIO_SODR to its true hex value and it failed. BUT - I can add the first offset of 0x30 no problem. Ether plain add (+) or (|) or works. Any help would be as always appreciated.
/* MSD upper 16 bits byte base*/
#define SAM_MSB 0x400E //MSD upper 16 bits byte base
/* LSD lower 16 bits byte base */
#define SAM_LSB 0xE00
/* PIO offset from PIO A and up */
#define PIO_OFFSET 0x200
/* status ouput data register */
#define PIO_SODR SAM_MSB << 16 | SAM_LSB + 0x30 // base plus reg offset OK
#define PIOA_SODR PIO_SODR + 0x200 // add in another offset fails -
I am trying to build SAM3x8E registers database. I though I could use serial of #define(s) to do that but run into a problem In this sample I can build PIO_SODR but I cannot add another (port) offset to it - see PIOA_SODR. I think the problem is my defines are not really int (32 bits) , but only symbols. I did compare the (symbol) PIO_SODR to its true hex value and it failed. BUT - I can add the first offset of 0x30 no problem. Ether plain add (+) or (|) or works. Any help would be as always appreciated.
/* MSD upper 16 bits byte base*/
#define SAM_MSB 0x400E //MSD upper 16 bits byte base
/* LSD lower 16 bits byte base */
#define SAM_LSB 0xE00
/* PIO offset from PIO A and up */
#define PIO_OFFSET 0x200
/* status ouput data register */
#define PIO_SODR SAM_MSB << 16 | SAM_LSB + 0x30 // base plus reg offset OK
#define PIOA_SODR PIO_SODR + 0x200 // add in another offset fails#defines are not even symbols - when the pre-processor runs it replaces the #define "tag" with the associated text. You probably want to add brackets to the PIO_SODR and PIOA_SODR definitions e.g. #define PIO_SODR (SAM_MSB<<16|SAM_LSB+0x30) #define PIOA_SODR (PIO_SODR + 0x200)
-
#defines are not even symbols - when the pre-processor runs it replaces the #define "tag" with the associated text. You probably want to add brackets to the PIO_SODR and PIOA_SODR definitions e.g. #define PIO_SODR (SAM_MSB<<16|SAM_LSB+0x30) #define PIOA_SODR (PIO_SODR + 0x200)
I think I got part of the answer - I am mixing logical operations with math so that is why the first #define works ( compare to value) AFTER I added brackets. #define PIO_SODR (SAM_MSB << 16 | SAM_LSB + PIO_OFFSET) now passes the value test OK #define PIOA_SODR (PIO_SODR + PIO_OFFSET) But the additional offset does not work, but I am getting closer. Thank for your help. And it also helps to check if my #define(s) are not duplicated somewhere else !!! Thanks
-
I am trying to build SAM3x8E registers database. I though I could use serial of #define(s) to do that but run into a problem In this sample I can build PIO_SODR but I cannot add another (port) offset to it - see PIOA_SODR. I think the problem is my defines are not really int (32 bits) , but only symbols. I did compare the (symbol) PIO_SODR to its true hex value and it failed. BUT - I can add the first offset of 0x30 no problem. Ether plain add (+) or (|) or works. Any help would be as always appreciated.
/* MSD upper 16 bits byte base*/
#define SAM_MSB 0x400E //MSD upper 16 bits byte base
/* LSD lower 16 bits byte base */
#define SAM_LSB 0xE00
/* PIO offset from PIO A and up */
#define PIO_OFFSET 0x200
/* status ouput data register */
#define PIO_SODR SAM_MSB << 16 | SAM_LSB + 0x30 // base plus reg offset OK
#define PIOA_SODR PIO_SODR + 0x200 // add in another offset fails -
Why don't you use
enum
(see, for instance "Avoiding #defines for constant data and using enums instead"[^])? -
Mainly because the "core" program is build using defines and I am still not too comfortable using enum. BTW after I "solved" this problem I have decided to "improve" on on it a got stuck again. Life is fun.
I can only agree with Carlo on this. More generally, whenever there is a suitable way to use standard language features instead of #define, use that standard language feature. Even if it means you need to type more! Modern editors offer autocompletion that makes typing effort a non-argument! Moreover, code riddled with #define macros is much harder for editors to interpret, preventing it from offering correct and complete autocompletion lists. Therefore the use of #define has the very real potential to actually increase the typing effort by hamstringing the editors capabilities! Besides, as you've already stated, #defines will just keep tripping you up. Most of the time it is due to their entire lack of context-related syntax checking at the time of writing. And sometimes it is because your macro name conflicts with another non-macro symbol in some totally unrelated part of the code - that latter issue will lead to obscure errors that can keep you busy for days. I am speaking from my own experience. Therefore: save time - do not use #define unless there is no other way to achieve the intended results. And if you do, don't place these macros in a header that gets included in unrelated source files - either put them right into the source file that needs them, or, if there are multiple source files that need them, put the macros in a separate header just for that purpose.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)