This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to use asserts for embedded systems? - Store File and Line Details or store PC

Hi, 

I have seen several blog posts on using assertions for embedded systems. For knowing where the assert occurred, there are a few recommended strategies:

a. Pass __FILE__ and __LINE__ as a parameter. However, disadvantage being that __FILE__ will be a huge string with the entire file location. 

b. Pass __FILENAME__ (for eg: file1.c) instead of __FILE__ (for eg: C:/Users/R/file1.c) where the location of the file is not used - just the name of the file. 

c. Store PC and LR registers. 

Questions:

1. __FILENAME__ is not standard. How do we define this during compilation. 
2. How to store PC and LR registers? - can you give the C code for this? 
3. A lot of functions in my code is part of header files. Does it make a difference when asserts are called in functions which are part of header files?


  • disadvantage being that __FILE__ will be a huge string with the entire file location

    An easy way to change what corresponds to __FILE__ is with the preprocessor directive #line.  For instance, you could make the following the first line in a source file or header file ...

    #line 2 "filename.c"

    This tells the compiler the next line is line number 2, and the name of the current file is filename.c.  Is this a practical solution?

    How to store PC and LR registers? - can you give the C code for this? 

    I'm not aware of a method to access the exact PC from C code.  But you can access the start address of the current function by referring to the name of the function.  Something like ...

    typedef void (*FPTR)();
    FPTR function_address = (FPTR) current_function_name;

    As for the LR ... That you refer to this register means you use some Arm CPU.  Which compiler do you use?  The TI proprietary compiler named armcl?  Or the one based on LLVM/Clang named tiarmclang?  

    A lot of functions in my code is part of header files. Does it make a difference when asserts are called in functions which are part of header files?

    No.

    Thanks and regards,

    -George

  • Hi George, 

    Thanks for the reply. 

    An easy way to change what corresponds to __FILE__ is with the preprocessor directive #line

    This looks error prone as it involves a manual step to write it in each and every file?

    As for the LR ... That you refer to this register means you use some Arm CPU.

    Sorry for the confusion. I am using F28004x controller and C28x compiler. I took the LR register from a blog on internet related to assertions.

    But you can access the start address of the current function by referring to the name of the function

    I think this is a good strategy. If I store the function name along with the line number - that should help me locate the code where the assertion failed. This should be equivalent to storing the file name and line number. Is my thinking correct or am I missing some key information? 

    Note: I am new to using assertions. Any help/suggestions from your end will be useful to me. 

    Regards,

  • Rushi,

    George is out and will reply when he returns on Tuesday.

    Regards,

    John

  • An easy way to change what corresponds to __FILE__ is with the preprocessor directive #line

    This looks error prone as it involves a manual step to write it in each and every file?

    It is not as easy as using __FILE__ exactly as predefined by the compiler.  But nothing else will be that easy. 

    Another way to go is to just create your own preprocessor symbol ...

    #define CUSTOM_FILE "filename.c"

    Then write your own macros that use CUSTOM_FILE.  Or other ideas along these lines.  

    I am using F28004x controller and C28x compiler.

    Then you need to capture the contents of the RPC register.  Unfortunately, there is no straightforward way to do that from C.

    Any help/suggestions from your end will be useful to me. 

    The C standards require the compiler supply a header file named assert.h that defines a macro named assert.  It uses printf, which is impractical for most embedded systems.  But you will probably learn something by looking at how it is implemented.

    Thanks and regards,

    -George