This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Are DEFINEs in the C51 active in .h header files?

I'm trying to configure some software that has different start addresses based on how the FLASH gets updated. If FLASH is updated via a UART then the code should start at 0 otherwise it should start at 24K.
I thought the following would work - where FLASH_VIA_UART is defined in the C51 preprocessor symbols menu.

#ifdef FLASH_VIA_UART
#define RUNTIME_CODE_START_ADDRESS (RUNTIME_CODE_START_AT_0H)
#else
#define RUNTIME_CODE_START_ADDRESS (RUNTIME_CODE_START_AT_24kH)
#endif


However, this doesn't work. Instead I have to hardcode it by commenting out everything but the 2nd line. Then the software works fine.

The RUNTIME_CODE_START_ADDRESS define could be used in other defines and macros so I suspect that there is a scope problem or something. Note that this code is in a .h file which is then used in multiple files.

Parents
  • Rather than just have the flag defined or not, it may be "safer" to require it to have a specific value - that way you can catch any problems where the definition is accidentally missing; eg,

    #ifdef FLASH_VIA_UART
    // It's defined - check its value, and use it if valid
    #if FLASH_VIA_UART == 2
    #define RUNTIME_CODE_START_ADDRESS (RUNTIME_CODE_START_AT_0H)
    
    #elif FLASH_VIA_UART == 3
    #define RUNTIME_CODE_START_ADDRESS (RUNTIME_CODE_START_AT_24kH)
    
    #else
    #error Invalid value for FLASH_VIA_UART
    #endif
    
    #else
    // It's not defined
    #error FLASH_VIA_UART must be defined
    #endif
    


    Note that I've avoided 0 and 1 as values - as these are likely to be used by (some) compilers when the symbol is defined without a specific value...

Reply
  • Rather than just have the flag defined or not, it may be "safer" to require it to have a specific value - that way you can catch any problems where the definition is accidentally missing; eg,

    #ifdef FLASH_VIA_UART
    // It's defined - check its value, and use it if valid
    #if FLASH_VIA_UART == 2
    #define RUNTIME_CODE_START_ADDRESS (RUNTIME_CODE_START_AT_0H)
    
    #elif FLASH_VIA_UART == 3
    #define RUNTIME_CODE_START_ADDRESS (RUNTIME_CODE_START_AT_24kH)
    
    #else
    #error Invalid value for FLASH_VIA_UART
    #endif
    
    #else
    // It's not defined
    #error FLASH_VIA_UART must be defined
    #endif
    


    Note that I've avoided 0 and 1 as values - as these are likely to be used by (some) compilers when the symbol is defined without a specific value...

Children
No data